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 | e472550fe154f0afa5b36d2a7e2334d4680d7884 (patch) | |
tree | 8131e3fa7db162f9a072ef4d89e7fb25d338cdd4 /tst/com/p4square/grow/model/AnswerTest.java | |
parent | 31303114ef03b13ab320ee553f11a73346be7f4a (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/AnswerTest.java')
-rw-r--r-- | tst/com/p4square/grow/model/AnswerTest.java | 137 |
1 files changed, 137 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()); + } +} |