package org.ijsolutions.testing; import org.ijsolutions.testbench.Category; import org.ijsolutions.testbench.PlanEntry; import org.ijsolutions.testbench.Question; import org.ijsolutions.testbench.Response; import org.ijsolutions.testbench.Test; import org.ijsolutions.testbench.TestBench; import org.ijsolutions.testbench.TestNode; import org.ijsolutions.testbench.TestPlan; import org.ijsolutions.testbench.TestSearcher; import org.ijsolutions.testbench.modules.BaseTestingModule; import org.ijsolutions.testbench.modules.TestBenchModule; import org.ijsolutions.testbench.util.TestTimer; import org.openedit.BaseTestCase; import org.openedit.WebPageRequest; import org.openedit.hittracker.HitTracker; import org.openedit.hittracker.SearchQuery; import org.openedit.users.User; public class TestFramework extends BaseTestCase { public void testPlanArchive() throws Exception { TestBenchModule mod = (TestBenchModule) getBean("TestBenchModule"); WebPageRequest req = getFixture().createPageRequest( "/testbench/courses/index.html"); TestBench bench = mod.getTestBench(req); TestPlan inStructions = new TestPlan(); inStructions.setName("PATI Test"); inStructions.setId("pati"); inStructions.setTestType("category"); inStructions.setTime(60 * 60 * 1000);// 1 minute? inStructions.setTotalQuestions(35); inStructions.setTimed(true); createSampleQuestions(req, bench, inStructions); bench.getTestPlanArchive().saveTestPlan(inStructions, req.getUser()); inStructions = bench.getTestPlanArchive().getTestPlan("pati"); assertEquals("PATI Test", inStructions.getName()); assertEquals("pati", inStructions.getId()); assertEquals(60 * 60 * 1000, inStructions.getTime()); assertEquals(35, inStructions.getTotalQuestions()); assertEquals(true, inStructions.isTimed()); assertEquals(inStructions.getCategoryList().size(), 2); assertEquals(inStructions.getQuestionCount("math"), 15); Test test = bench.buildTest("category", inStructions); assertEquals("check total questions", 35, test.getNodes().size()); assertEquals("check 2 categories in test", 2, test.getCategories() .size()); assertEquals("check math category", 15, test.getNodesForCategory( "math").size(), 15); assertEquals("check symbols category", 20, test .getNodesForCategory("symbols").size(), 20); test.jumpToCategory("symbols"); assertTrue(test.getCurrentQuestion().isInCategory("symbols")); } private void createSampleQuestions(WebPageRequest req, TestBench bench, TestPlan inStructions) throws Exception { Category cat = bench.getCategory("math"); if (cat == null) { cat = new Category(); cat.setId("math"); cat.setName("math"); bench.getCategoryArchive().getRootCategory().addChild(cat); bench.getCategoryArchive().saveCategory(cat); } for (int i = 0; i < 25; i++) { String id = "math:" + i; getTestQuestion(req, bench, id, cat); } inStructions.addCategory(cat, 15); cat = bench.getCategory("symbols"); if (cat == null) { cat = new Category(); cat.setId("symbols"); cat.setName("symbols"); bench.getCategoryArchive().getRootCategory().addChild(cat); bench.getCategoryArchive().saveCategory(cat); } for (int i = 0; i < 25; i++) { String id = "symbols:" + i; getTestQuestion(req, bench, id, cat); } inStructions.addCategory(cat, 20); } public void testLoadAndSave() throws Exception { BaseTestingModule mod = (BaseTestingModule) getBean("TestBenchModule"); WebPageRequest req = getFixture().createPageRequest( "/testbench/courses/index.html"); TestBench bench = mod.getTestBench(req); Question question = getTestQuestion(req, bench, "testquestion", null); assertNotNull(question.getId()); HitTracker hits = bench.getQuestionSearcher().fieldSearch("id", "testquestion"); assertTrue(hits.size() > 0); bench.getQuestionArchive().clear(); Question question2 = bench.getQuestionArchive().getQuestionById( "testquestion"); assertNotNull(question2); assertNotNull(question2.get("shortdescription")); assertEquals(question.get("shortdescription"), question2 .get("shortdescription")); assertEquals(question.get("status"), "open"); assertEquals(4, question2.getResponses().size()); assertEquals("1", question2.getCorrectResponse().getId()); assertTrue(question2.isInCategory("math")); } private Question getTestQuestion(WebPageRequest req, TestBench testbench, String questionid, Category inCat) throws Exception { Question question = testbench.getQuestionArchive().getQuestion( questionid); if (question == null) { question = testbench.getQuestionArchive() .createQuestion(questionid); } question.setProperty("status", "open"); question.setProperty("shortdescription", "Short Description"); question.setName("RMCC Aurora"); question .setText("What is the airspeed velocity of an unladen swallow?"); if (inCat != null) { question.addCategory(inCat); } Response one = new Response(); one.setId("1"); one.setText("The answer is one."); Response two = new Response(); two.setId("2"); two.setText("The answer is two."); Response three = new Response(); three.setId("3"); three.setText("The answer is three."); Response four = new Response(); four.setId("4"); four.setText("The answer is 4."); question.addResponse(one); question.addResponse(two); question.addResponse(three); question.addResponse(four); question.setCorrectResponse(one); if (inCat == null) { Category category = testbench.getCategoryArchive().getCategory( "math"); if (category == null) { category = new Category(); category.setId("math"); category.setName("Math Questions"); testbench.getCategoryArchive().getRootCategory().addChild( category); testbench.getCategoryArchive().saveCategory(category); question.addCategory(category); } question.addCategory(category); } else { question.addCategory(inCat); } testbench.getQuestionSearcher().saveData(question, req.getUser()); return question; } public void testCreateTests() throws Exception { TestBenchModule mod = (TestBenchModule) getBean("TestBenchModule"); WebPageRequest req = getFixture().createPageRequest( "/testbench/courses/index.html"); TestBench bench = mod.getTestBench(req); User student = getFixture().getUserManager().getUser("student1"); req.setUser(student); TestPlan inStructions = new TestPlan(); inStructions.setTime(60 * 60 * 1000);// 1 minute? inStructions.setTotalQuestions(35); inStructions.setTimed(true); Test test = bench.buildTest("random", inStructions); assertNotNull(test); assertEquals(0, test.getCategories().size()); assertEquals(35, test.getNodes().size()); req.setRequestParameter("planid", "pati"); mod.createTest(req); test = (Test) mod.getActiveTest(req); assertEquals(2, test.getCategories().size()); assertNotNull(test.getUser()); assertNotNull(test); assertEquals(test.getRemainingNodes().size(), 35); TestNode node = test.getCurrentNode(); Question question = test.getCurrentQuestion(); assertNotNull(question); assertTrue(question.getResponses().size() == 4); question.getResponses(); assertTrue(test.isTimed()); assertFalse(test.isStarted()); Response one = question.getCorrectResponse(); test.recordResponse(node, one.getId(), true, true); assertEquals(test.getRemainingNodes().size(), 34); assertEquals(test.getCompletedQuestions().size(), 1); assertEquals(test.getCorrectResponses().size(), 1); assertEquals(test.getCurrentQuestion().getId(), ((TestNode) test .getNodes().get(1)).getQuestion().getId()); assertTrue(test.isStarted()); mod.pauseActiveTest(req); assertTrue(test.isPaused()); int seconds = test.getTestTimer().getSeconds(); // remaining seconds Thread.sleep(2000); // 2 seconds assertEquals(test.getTestTimer().getSeconds(), seconds); mod.resumeActiveTest(req); Thread.sleep(2000); assertNotSame(test.getTestTimer().getSeconds(), seconds); req .setRequestParameter("questionid", test.getCurrentNode() .getId()); req.setRequestParameter("response", new String[] { test .getCurrentQuestion().getCorrectResponse().getId() }); req.setRequestParameter(test.getCurrentQuestion().getCorrectResponse() .getId() + ".value", test.getCurrentQuestion().getCorrectResponse() .getId()); mod.recordResponse(req); assertEquals(33, test.getRemainingNodes().size()); assertEquals(test.getCompletedQuestions().size(), 2); assertEquals(test.getCorrectResponses().size(), 2); assertEquals(test.getCurrentQuestion().getId(), ((TestNode) test .getNodes().get(2)).getQuestion().getId()); // loading and saving the test mod.saveTest(req); String testid = test.getId(); assertNotNull(testid); mod.resetActiveTest(req); test = (Test) mod.getActiveTest(req); assertNull(test); // test = bench.loadTest(testid, req.getUserName()); test = bench.getTestArchive().getTestById(testid, req.getUser()); assertNotNull(test); assertNotNull(test.getSourcePath()); //test = bench.getTestArchive().getTestBySourcePath(test.getSourcePath()); assertEquals(33, test.getRemainingNodes().size()); assertEquals(test.getCompletedQuestions().size(), 2); assertEquals(test.getCorrectResponses().size(), 2); assertEquals(test.getCurrentQuestion().getId(), ((TestNode) test .getNodes().get(2)).getQuestion().getId()); assertEquals(test.getUser().getUserName(), req.getUserName()); test = null; req.setRequestParameter("testid", testid); req.setRequestParameter("userid", "student1"); test = mod.loadTest(req); assertEquals(33, test.getRemainingNodes().size()); assertEquals(test.getCompletedQuestions().size(), 2); assertEquals(test.getCorrectResponses().size(), 2); assertEquals(test.getCurrentQuestion().getId(), ((TestNode) test .getNodes().get(2)).getQuestion().getId()); assertEquals(test.getUser().getUserName(), req.getUserName()); } public void testTestSearches() throws Exception { TestBenchModule mod = (TestBenchModule) getBean("TestBenchModule"); WebPageRequest req = getFixture().createPageRequest( "/testbench/courses/index.html"); TestBench bench = mod.getTestBench(req); TestSearcher searcher = bench.getTestSearcher(); // test finding users tests SearchQuery q = searcher.createSearchQuery(); q.addMatches("user", "admin"); } public void testTestTimer() throws Exception { TestTimer timer = new TestTimer(); timer.setPeriod(100); // every second timer.setSeconds(4); int seconds = timer.getSeconds(); timer.start(); Thread.sleep(110); assertTrue(timer.isRunning()); int now = timer.getSeconds(); assertNotSame(seconds, now); now = timer.getSeconds(); timer.stop(); Thread.sleep(200); int again = timer.getSeconds(); assertEquals(now, again); timer.start(); Thread.sleep(400); assertTrue(timer.isComplete()); assertFalse(timer.isRunning()); assertEquals(0, timer.getSeconds()); } public void xtestCreateSelectRandomCategoryTest() throws Exception { TestBenchModule mod = (TestBenchModule) getBean("TestBenchModule"); WebPageRequest req = getFixture().createPageRequest( "/testbench/courses/index.html"); TestBench bench = mod.getTestBench(req); User student = getFixture().getUserManager().getUser("student1"); req.setUser(student); req.setRequestParameter("planid", "29"); mod.createTest(req); Test test = (Test) mod.getActiveTest(req); assertEquals(1, test.getCategories().size()); assertEquals(9, test.getNodes().size()); } public void xtestNodeTypes() throws Exception { TestBenchModule mod = (TestBenchModule) getBean("TestBenchModule"); WebPageRequest req = getFixture().createPageRequest( "/testbench/courses/index.html"); TestBench bench = mod.getTestBench(req); TestPlan inStructions = new TestPlan(); inStructions.setName("PlanEntry based test"); inStructions.setId("planentries"); inStructions.setTime(60 * 60 * 1000);// 1 minute? inStructions.setTotalQuestions(35); inStructions.setTimed(true); PlanEntry entry = new PlanEntry(); entry.setId("1"); entry.setProperty("type", "category"); entry.setName("Math"); entry.setProperty("categoryid", "1"); entry.setProperty("count", "15"); inStructions.addPlanEntry(entry); entry = new PlanEntry(); entry.setId("2"); entry.setProperty("type", "instructions"); entry.setName("Instructions"); entry.setProperty("instructions", "Read all questions carefully"); entry.setProperty("count", "15"); inStructions.addPlanEntry(entry); inStructions.addPlanEntry(entry); Test test = bench.buildTest("category", inStructions); assertEquals("check total questions", 35, test.getNodes().size()); assertEquals("check 2 categories in test", 2, test.getCategories() .size()); assertEquals("check math category", 15, test.getNodesForCategory( "math").size(), 15); assertEquals("check symbols category", 20, test .getNodesForCategory("symbols").size(), 20); test.jumpToCategory("symbols"); assertTrue(test.getCurrentQuestion().isInCategory("symbols")); } // public void testFilters() throws Exception { // TestBenchModule mod = (TestBenchModule) getBean("TestBenchModule"); // WebPageRequest req = getFixture().createPageRequest( // "/testbench/courses/index.html"); // TestBench bench = mod.getTestBench(req); // // // TestPlan inStructions = bench.getTestPlanArchive().getTestPlan("filtertest"); // assertNotNull(inStructions.getPlanEntries()); // PlanEntry first = (PlanEntry) inStructions.getPlanEntries().get(0); // Filter filter = first.getFilter(); // assertNotNull(filter); // assertTrue(filter.passes(req)); // req.setUser(null); // //req.getSession().invalidate(); // assertFalse(filter.passes(req)); // // } }