1
2
3 package org.virtualmock.rule;
4
5 import java.util.ArrayList;
6 import junit.framework.Test;
7 import org.easymock.MockControl;
8 import org.hansel.CoverageDecorator;
9 import org.virtualmock.test.testcase.EasyMockTestCase;
10
11
12 /***
13 * Tests the VerificationManager class
14 *
15 * @author Chad Woolley
16 * @version $Revision: 1.5 $
17 */
18 public class RuleManagerTest extends EasyMockTestCase {
19 ArrayList rules = null;
20 MockControl controlRule = null;
21 Rule mockRule = null;
22 RuleManager ruleManager = null;
23 String ruleKey = null;
24
25 /***
26 * Set up the test
27 *
28 * @throws Exception any exception thrown during setup.
29 */
30 public void setUp() throws Exception {
31 super.setUp();
32 ruleKey = "RULE_KEY";
33
34 MockControl controlRule = MockControl.createControl(Rule.class);
35 mockRule = (Rule) controlRule.getMock();
36
37 mockRule.getKey();
38 controlRule.setDefaultReturnValue(ruleKey);
39 controlRule.replay();
40
41 rules = new ArrayList();
42 }
43
44 /***
45 * Hansel support
46 *
47 * @return the decorated Test
48 */
49 public static Test suite() {
50 CoverageDecorator cd =
51 new CoverageDecorator(RuleManagerTest.class,
52 new Class[] {RuleManager.class});
53 cd.setDisplayStatistics(true);
54
55 return cd;
56 }
57
58 /***
59 * Tear down the test
60 *
61 * @throws Exception any exception thrown during teardown.
62 */
63 public void tearDown() throws Exception {
64 super.tearDown();
65 }
66
67 /***
68 * Tests the addRule and getRules method
69 */
70 public void testCanAddRuleAndGetRules() {
71 rules.add(mockRule);
72 ruleManager = new RuleManager(rules, null);
73 assertEquals(1, ruleManager.getRules().size());
74 }
75
76 /***
77 * Tests the checkAllRulesInCurrentPhase method when it fails
78 */
79 public void testCheckAllRulesInCurrentPhaseFails() {
80 MockControl controlRule = MockControl.createControl(Rule.class);
81 Rule mockRule = (Rule) controlRule.getMock();
82
83 mockRule.getViolationMessages();
84
85 String violationMessage = "rule violation message";
86 controlRule.setReturnValue(new String[] {violationMessage});
87
88 ruleManager = new RuleManager(new ArrayList(), null);
89 ruleManager.addRule(mockRule);
90
91 controlRule.replay();
92
93 try {
94 ruleManager.checkAllRulesInCurrentPhase();
95 fail("Expected error");
96 } catch (Error e) {
97 assertEquals("Error message should be the same as violation message",
98 violationMessage, e.getMessage());
99
100
101 }
102
103 controlRule.verify();
104 }
105
106 /***
107 * Tests the checkAllRulesInCurrentPhase method when it passes
108 */
109 public void testCheckAllRulesInCurrentPhasePasses() {
110 MockControl controlRule = MockControl.createControl(Rule.class);
111 Rule mockRule = (Rule) controlRule.getMock();
112
113 mockRule.getViolationMessages();
114 controlRule.setReturnValue(new String[0]);
115
116 ruleManager = new RuleManager(new ArrayList(), null);
117 ruleManager.addRule(mockRule);
118
119 controlRule.replay();
120 ruleManager.checkAllRulesInCurrentPhase();
121 controlRule.verify();
122 }
123
124 /***
125 * Tests the getRule method
126 */
127 public void testGetRule() {
128 rules.add(mockRule);
129 ruleManager = new RuleManager(rules, null);
130
131 Rule retrievedRule = ruleManager.getRule(ruleKey);
132
133 assertEquals("Key of retrieved rule should be the same as the key by which it was retrieved",
134 ruleKey, retrievedRule.getKey());
135 }
136 }