diff options
author | Jesse Morgan <jesse@jesterpm.net> | 2013-10-20 23:14:51 -0700 |
---|---|---|
committer | Jesse Morgan <jesse@jesterpm.net> | 2013-10-20 23:14:51 -0700 |
commit | e77c9b418fdfb935ff8e99f10f607a4bbd7e1c8c (patch) | |
tree | 9005f8c0e3b18f853fb8093b01f6aad64949bc13 /tst/com/p4square/grow/model | |
parent | 5037f4797461649994068d97a8433b6cd793c523 (diff) |
First stage of a major refactoring.
Question and Answer can now be serialized and deserialized to/from JSON.
As such, I no longer have to pass awkward maps around. As part of this
change I have introduced a Provider interface to abstract out loading
and persisting these beans.
The scoring logic has been completed factored out of
SurveyResultsResource and into the various ScoringEngines. Tests have
been added for Question, Answer, and the ScoringEngines. A bug has been
fixed in computing the value for slider questions.
The label identifiers in the circle questions have changed from all
lower case to camel case. That is, topleft is now topLeft. Several
issues have been corrected in the circle answers where the point values
did not match the labels.
Testing and code coverage support and reports have been added.
Diffstat (limited to 'tst/com/p4square/grow/model')
-rw-r--r-- | tst/com/p4square/grow/model/AnswerTest.java | 137 | ||||
-rw-r--r-- | tst/com/p4square/grow/model/PointTest.java | 129 | ||||
-rw-r--r-- | tst/com/p4square/grow/model/QuadScoringEngineTest.java | 85 | ||||
-rw-r--r-- | tst/com/p4square/grow/model/QuestionTest.java | 80 | ||||
-rw-r--r-- | tst/com/p4square/grow/model/SimpleScoringEngineTest.java | 86 | ||||
-rw-r--r-- | tst/com/p4square/grow/model/SliderScoringEngineTest.java | 93 |
6 files changed, 610 insertions, 0 deletions
diff --git a/tst/com/p4square/grow/model/AnswerTest.java b/tst/com/p4square/grow/model/AnswerTest.java new file mode 100644 index 0000000..1747773 --- /dev/null +++ b/tst/com/p4square/grow/model/AnswerTest.java @@ -0,0 +1,137 @@ +/* + * Copyright 2013 Jesse Morgan + */ + +package com.p4square.grow.model; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Tests for the Answer class. + * + * @author Jesse Morgan <jesse@jesterpm.net> + */ +public class AnswerTest { + private static final double DELTA = 1e-15; + + public static void main(String... args) { + org.junit.runner.JUnitCore.main(AnswerTest.class.getName()); + } + + /** + * Verify that the correct default values are returned. + */ + @Test + public void testDefaults() { + Answer a = new Answer(); + + // Type should default to AVERAGE + assertEquals(Answer.ScoreType.AVERAGE, a.getType()); + + // NextQuestion should default to null + assertNull(a.getNextQuestion()); + } + + /** + * Verify that getters and setters function correctly. + */ + @Test + public void testGetAndSet() { + Answer a = new Answer(); + + a.setText("Answer Text"); + assertEquals("Answer Text", a.getText()); + + a.setType(Answer.ScoreType.TRUMP); + assertEquals(Answer.ScoreType.TRUMP, a.getType()); + + a.setScore(10); + assertEquals(10, a.getScore(), DELTA); + + a.setNextQuestion("nextQuestion"); + assertEquals("nextQuestion", a.getNextQuestion()); + } + + /** + * Verify that when the ScoreType is NONE, the score is 0. + */ + @Test + public void testScoreTypeNone() { + Answer a = new Answer(); + + a.setScore(10); + assertEquals(10, a.getScore(), DELTA); + + a.setType(Answer.ScoreType.NONE); + assertEquals(0, a.getScore(), DELTA); + } + + /** + * Test score() with type TRUMP. + */ + @Test + public void testScoreTrump() { + Score score = new Score(); + score.sum = 10; + score.count = 2; + + Answer a = new Answer(); + a.setType(Answer.ScoreType.TRUMP); + a.setScore(5); + + assertFalse(a.score(score)); + + assertEquals(5, score.getSum(), DELTA); + assertEquals(1, score.getCount()); + } + + /** + * Test score() with type NONE. + */ + @Test + public void testScoreNone() { + Score score = new Score(); + score.sum = 10; + score.count = 2; + + Answer a = new Answer(); + a.setScore(5); + a.setType(Answer.ScoreType.NONE); + + assertTrue(a.score(score)); + + assertEquals(10, score.getSum(), DELTA); + assertEquals(2, score.getCount()); + } + + /** + * Test score() with type AVERAGE. + */ + @Test + public void testScoreAverage() { + Score score = new Score(); + score.sum = 10; + score.count = 2; + + Answer a = new Answer(); + a.setScore(5); + a.setType(Answer.ScoreType.AVERAGE); + + assertTrue(a.score(score)); + + assertEquals(15, score.getSum(), DELTA); + assertEquals(3, score.getCount()); + } + + /** + * Verify that ScoreType.toString() returns the proper strings. + */ + @Test + public void testScoreTypeToString() { + assertEquals("none", Answer.ScoreType.NONE.toString()); + assertEquals("average", Answer.ScoreType.AVERAGE.toString()); + assertEquals("trump", Answer.ScoreType.TRUMP.toString()); + } +} diff --git a/tst/com/p4square/grow/model/PointTest.java b/tst/com/p4square/grow/model/PointTest.java new file mode 100644 index 0000000..a4e7cc0 --- /dev/null +++ b/tst/com/p4square/grow/model/PointTest.java @@ -0,0 +1,129 @@ +/* + * Copyright 2013 Jesse Morgan + */ + +package com.p4square.grow.model; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Tests for the Point class. + * + * @author Jesse Morgan <jesse@jesterpm.net> + */ +public class PointTest { + private static final double DELTA = 1e-15; + + public static void main(String... args) { + org.junit.runner.JUnitCore.main(PointTest.class.getName()); + } + + /** + * Verify that the constructor works properly. + */ + @Test + public void testHappyCase() { + Point p = new Point(1, 2); + assertEquals(1, p.getX(), DELTA); + assertEquals(2, p.getY(), DELTA); + } + + /** + * Verify distance is computed correctly. + */ + @Test + public void testDistance() { + Point p1, p2; + + // Simple line + p1 = new Point(2, 1); + p2 = new Point(-2, 1); + assertEquals(4, p1.distance(p2), DELTA); + assertEquals(4, p2.distance(p1), DELTA); + + // Across origin + p1 = new Point(5, 1); + p2 = new Point(-3, -2); + assertEquals(Math.sqrt(73), p1.distance(p2), DELTA); + assertEquals(Math.sqrt(73), p2.distance(p1), DELTA); + } + + /** + * Verify toString returns the expected string. + */ + @Test + public void testToString() { + Point p = new Point(-1.12345, 2.3); + assertEquals("-1.12,2.30", p.toString()); + } + + /** + * Verify that valueOf correctly parses a variety of strings. + */ + @Test + public void testValueOfHappyCase() { + Point p; + + p = Point.valueOf("1,2"); + assertEquals(1, p.getX(), DELTA); + assertEquals(2, p.getY(), DELTA); + + p = Point.valueOf("1.5,2.0"); + assertEquals(1.5, p.getX(), DELTA); + assertEquals(2.0, p.getY(), DELTA); + + p = Point.valueOf("-1.5,2.0"); + assertEquals(-1.5, p.getX(), DELTA); + assertEquals(2.0, p.getY(), DELTA); + + p = Point.valueOf("1.5,-2.0"); + assertEquals(1.5, p.getX(), DELTA); + assertEquals(-2.0, p.getY(), DELTA); + + p = Point.valueOf("-1.5,-2.0"); + assertEquals(-1.5, p.getX(), DELTA); + assertEquals(-2.0, p.getY(), DELTA); + } + + /** + * Verify that valueOf fails on null string. + */ + @Test(expected = NullPointerException.class) + public void testValueOfNull() { + Point.valueOf(null); + } + + /** + * Verify that valueOf fails on empty string. + */ + @Test(expected = IllegalArgumentException.class) + public void testValueOfEmptyString() { + Point.valueOf(""); + } + + /** + * Verify that valueOf fails on missing comma. + */ + @Test(expected = IllegalArgumentException.class) + public void testValueOfMissingComma() { + Point.valueOf("123"); + } + + /** + * Verify that valueOf fails on missing x. + */ + @Test(expected = IllegalArgumentException.class) + public void testValueOfMissingX() { + Point.valueOf(",12"); + } + + /** + * Verify that valueOf fails on missing y. + */ + @Test(expected = IllegalArgumentException.class) + public void testValueOfMissingY() { + Point.valueOf("12,"); + } +} diff --git a/tst/com/p4square/grow/model/QuadScoringEngineTest.java b/tst/com/p4square/grow/model/QuadScoringEngineTest.java new file mode 100644 index 0000000..246a59f --- /dev/null +++ b/tst/com/p4square/grow/model/QuadScoringEngineTest.java @@ -0,0 +1,85 @@ +/* + * Copyright 2013 Jesse Morgan + */ + +package com.p4square.grow.model; + +import java.util.Map; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Test the QuadScoringEngine. + * + * @author Jesse Morgan <jesse@jesterpm.net> + */ +public class QuadScoringEngineTest { + private static final double DELTA = 1e-4; + + public static void main(String... args) { + org.junit.runner.JUnitCore.main(QuadScoringEngineTest.class.getName()); + } + + private Question mQuestion; + private ScoringEngine mEngine; + + @Before + public void setup() { + // Setup the Question + mQuestion = new QuadQuestion(); + Map<String, Answer> answers = mQuestion.getAnswers(); + + // Create four answers at (-1,-1), (1, -1), (-1, 1), (1, 1) + for (int i = 1; i <= 4; i++) { + int x = i % 2 == 0 ? 1 : -1; + int y = i > 2 ? 1 : -1; + + Answer a = new Answer(); + a.setScore(i); + answers.put(x + ".00," + y + ".00", a); + } + + mEngine = new QuadScoringEngine(); + } + + /** + * Test a point inside each quadrant. + */ + @Test + public void testEachQuadrant() { + Score score; + RecordedAnswer answer = new RecordedAnswer(); + + // 0.5,0.5 == 4 + score = new Score(); + answer.setAnswerId("0.5,0.5"); + assertTrue(mEngine.scoreAnswer(score, mQuestion, answer)); + assertEquals(4, score.getSum(), DELTA); + assertEquals(1, score.getCount()); + + // 0.5,-0.5 == 2 + score = new Score(); + answer.setAnswerId("0.5,-0.5"); + assertTrue(mEngine.scoreAnswer(score, mQuestion, answer)); + assertEquals(2, score.getSum(), DELTA); + assertEquals(1, score.getCount()); + + // -0.5,0.5 == 3 + score = new Score(); + answer.setAnswerId("-0.5,0.5"); + assertTrue(mEngine.scoreAnswer(score, mQuestion, answer)); + assertEquals(3, score.getSum(), DELTA); + assertEquals(1, score.getCount()); + + // -0.5,-0.5 == 0.5 + score = new Score(); + answer.setAnswerId("-0.5,-0.5"); + assertTrue(mEngine.scoreAnswer(score, mQuestion, answer)); + assertEquals(1, score.getSum(), DELTA); + assertEquals(1, score.getCount()); + } + +} diff --git a/tst/com/p4square/grow/model/QuestionTest.java b/tst/com/p4square/grow/model/QuestionTest.java new file mode 100644 index 0000000..d09d2d8 --- /dev/null +++ b/tst/com/p4square/grow/model/QuestionTest.java @@ -0,0 +1,80 @@ +/* + * Copyright 2013 Jesse Morgan + */ + +package com.p4square.grow.model; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Tests for the Question class. + * + * @author Jesse Morgan <jesse@jesterpm.net> + */ +public class QuestionTest { + public static void main(String... args) { + org.junit.runner.JUnitCore.main(QuestionTest.class.getName()); + } + + /** + * Verify that all the getters and setters function. + */ + @Test + public void testGetAndSet() { + TextQuestion q = new TextQuestion(); + + q.setId("123"); + assertEquals("123", q.getId()); + + q.setQuestion("Hello World"); + assertEquals("Hello World", q.getQuestion()); + + q.setPreviousQuestion("122"); + assertEquals("122", q.getPreviousQuestion()); + + q.setNextQuestion("124"); + assertEquals("124", q.getNextQuestion()); + } + + /** + * Verify the correct next question is returned. + */ + @Test + public void testGetNextQuestion() { + // Setup the Question + TextQuestion q = new TextQuestion(); + q.setNextQuestion("defaultNext"); + + Answer answerWithNext = new Answer(); + answerWithNext.setNextQuestion("answerNext"); + + q.getAnswers().put("withNext", answerWithNext); + q.getAnswers().put("withoutNext", new Answer()); + + // Answer without a nextQuestion should return default. + assertEquals("defaultNext", q.getNextQuestion("withoutNext")); + + // Answer with a nextQuestion should return it's next question. + assertEquals("answerNext", q.getNextQuestion("withNext")); + + // Unknown answer should also return the default + assertEquals("defaultNext", q.getNextQuestion("unknownAnswer")); + } + + /** + * Validate the toString() results for the enum. + * + * This may seem like an odd test, but it is very important for these to be + * lowercase to match the values in the JSON files. + */ + @Test + public void testToString() { + assertEquals("text", Question.QuestionType.TEXT.toString()); + assertEquals("image", Question.QuestionType.IMAGE.toString()); + assertEquals("slider", Question.QuestionType.SLIDER.toString()); + assertEquals("quad", Question.QuestionType.QUAD.toString()); + assertEquals("circle", Question.QuestionType.CIRCLE.toString()); + } +} diff --git a/tst/com/p4square/grow/model/SimpleScoringEngineTest.java b/tst/com/p4square/grow/model/SimpleScoringEngineTest.java new file mode 100644 index 0000000..1a1bc95 --- /dev/null +++ b/tst/com/p4square/grow/model/SimpleScoringEngineTest.java @@ -0,0 +1,86 @@ +/* + * Copyright 2013 Jesse Morgan + */ + +package com.p4square.grow.model; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Test the SimpleScoringEngine. + * + * @author Jesse Morgan <jesse@jesterpm.net> + */ +public class SimpleScoringEngineTest { + private static final double DELTA = 1e-15; + + public static void main(String... args) { + org.junit.runner.JUnitCore.main(SimpleScoringEngineTest.class.getName()); + } + + private Question mQuestion; + private ScoringEngine mEngine; + + @Before + public void setup() { + // Setup the Question + mQuestion = new TextQuestion(); + + for (int i = 0; i <= 4; i++) { + Answer a = new Answer(); + a.setScore(i); + mQuestion.getAnswers().put("a" + i, a); + } + + mEngine = new SimpleScoringEngine(); + } + + /** + * Test that each individual answer is scored correctly. + */ + @Test + public void testAllAnswers() { + for (int i = 1; i <= 4; i++) { + Score score = new Score(); + RecordedAnswer answer = new RecordedAnswer(); + answer.setAnswerId("a" + i); + + assertTrue(mEngine.scoreAnswer(score, mQuestion, answer)); + + assertEquals(1, score.count); + assertEquals(i, score.sum, DELTA); + } + } + + /** + * Test that each answer score forms an increasing sum. + */ + @Test + public void testAllAnswersIncremental() { + Score score = new Score(); + + for (int i = 1; i <= 4; i++) { + RecordedAnswer answer = new RecordedAnswer(); + answer.setAnswerId("a" + i); + + assertTrue(mEngine.scoreAnswer(score, mQuestion, answer)); + } + + assertEquals(4, score.count); + assertEquals(10, score.sum, DELTA); + } + + /** + * Verify exception is thrown for undefined answer. + */ + @Test(expected = IllegalArgumentException.class) + public void testUnknownAnswer() { + Score score = new Score(); + RecordedAnswer answer = new RecordedAnswer(); + answer.setAnswerId("unknown"); + mEngine.scoreAnswer(score, mQuestion, answer); + } +} diff --git a/tst/com/p4square/grow/model/SliderScoringEngineTest.java b/tst/com/p4square/grow/model/SliderScoringEngineTest.java new file mode 100644 index 0000000..fdbfd6f --- /dev/null +++ b/tst/com/p4square/grow/model/SliderScoringEngineTest.java @@ -0,0 +1,93 @@ +/* + * Copyright 2013 Jesse Morgan + */ + +package com.p4square.grow.model; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Test the SliderScoringEngine. + * + * @author Jesse Morgan <jesse@jesterpm.net> + */ +public class SliderScoringEngineTest { + private static final double DELTA = 1e-4; + + public static void main(String... args) { + org.junit.runner.JUnitCore.main(SliderScoringEngineTest.class.getName()); + } + + private Question mQuestion; + private ScoringEngine mEngine; + + @Before + public void setup() { + // Setup the Question + mQuestion = new SliderQuestion(); + mEngine = new SliderScoringEngine(); + } + + /** + * Test the scoreAnswer() method. + */ + @Test + public void testScoreAnswer() { + Score score = new Score(); + RecordedAnswer answer = new RecordedAnswer(); + + // Test 0 + answer.setAnswerId("0"); + assertTrue(mEngine.scoreAnswer(score, mQuestion, answer)); + assertEquals(1, score.count); + assertEquals(1, score.sum, DELTA); + + // Test 1 + answer.setAnswerId("1"); + assertTrue(mEngine.scoreAnswer(score, mQuestion, answer)); + assertEquals(2, score.count); + assertEquals(5, score.sum, DELTA); + + // Test fraction (0.33) + answer.setAnswerId("0.33333"); + assertTrue(mEngine.scoreAnswer(score, mQuestion, answer)); + assertEquals(3, score.count); + assertEquals(7, score.sum, DELTA); + } + + /** + * Verify exception is thrown for non-numeric answer. + */ + @Test(expected = IllegalArgumentException.class) + public void testNonNumericAnswer() { + Score score = new Score(); + RecordedAnswer answer = new RecordedAnswer(); + answer.setAnswerId("unknown"); + mEngine.scoreAnswer(score, mQuestion, answer); + } + + /** + * Verify exception is thrown for negative answer. + */ + @Test(expected = IllegalArgumentException.class) + public void testNegativeAnswer() { + Score score = new Score(); + RecordedAnswer answer = new RecordedAnswer(); + answer.setAnswerId("-1"); + mEngine.scoreAnswer(score, mQuestion, answer); + } + + /** + * Verify exception is thrown for out of bounds answer. + */ + @Test(expected = IllegalArgumentException.class) + public void testAnswerOutOfBounds() { + Score score = new Score(); + RecordedAnswer answer = new RecordedAnswer(); + answer.setAnswerId("1.1"); + mEngine.scoreAnswer(score, mQuestion, answer); + } +} |