Clover Coverage Report - Kuali Student 1.1 (Aggregated)
Coverage timestamp: Sun Mar 6 2011 20:32:39 EST
147   251   8   21
0   187   0.05   7
7     1.14  
1    
 
  FailureMessageBuilderTest       Line # 17 147 0% 8 1 99.4% 0.9935065
 
  (6)
 
1    package org.kuali.student.common.messagebuilder.impl;
2   
3    import java.util.ArrayList;
4    import java.util.List;
5   
6    import org.antlr.runtime.Token;
7    import org.antlr.runtime.tree.CommonTreeAdaptor;
8    import org.junit.Assert;
9    import org.junit.Before;
10    import org.junit.Test;
11    import org.kuali.student.common.messagebuilder.booleanmessage.ast.BooleanNode;
12    import org.kuali.student.common.messagebuilder.booleanmessage.ast.parsers.BooleanFunctionParser;
13    import org.kuali.student.common.messagebuilder.impl.BooleanOperators;
14    import org.kuali.student.common.messagebuilder.impl.FailureMessageBuilder;
15    import org.kuali.student.common.messagebuilder.impl.exceptions.MessageBuilderException;
16   
 
17    public class FailureMessageBuilderTest {
18    private final static CommonTreeAdaptor adapter = new CommonTreeAdaptor();
19    private FailureMessageBuilder failureMessageBuilder;
20   
 
21  6 toggle @Before
22    public void setUp() throws Exception {
23  6 BooleanOperators bo = new BooleanOperators();
24  6 failureMessageBuilder = new FailureMessageBuilder(bo);
25    }
26   
 
27  1 toggle @Test
28    public void testBuildFailureMessageForSingleOrNode() throws Exception {
29    // Rule: A OR B
30  1 Token or = adapter.createToken(BooleanFunctionParser.OR, "+");
31  1 Token a = adapter.createToken(BooleanFunctionParser.ALPHA, "A");
32  1 Token b = adapter.createToken(BooleanFunctionParser.ALPHA, "B");
33   
34    // root node
35  1 BooleanNode orNode = new BooleanNode(or);
36   
37  1 BooleanNode aNode = new BooleanNode(a);
38  1 aNode.setParent(orNode);
39  1 aNode.setValue(Boolean.FALSE);
40  1 aNode.setNodeMessage("MATH101");
41   
42  1 BooleanNode bNode = new BooleanNode(b);
43  1 bNode.setParent(orNode);
44  1 bNode.setValue(Boolean.FALSE);
45  1 bNode.setNodeMessage("MATH201");
46   
47  1 orNode.addChild(aNode); // left node
48  1 orNode.addChild(bNode); // right node
49   
50  1 String failureMsg = failureMessageBuilder.buildFailureMessage(orNode);
51   
52  1 Assert.assertEquals(failureMsg, orNode.getNodeMessage());
53  1 Assert.assertEquals("MATH101 OR MATH201", failureMsg);
54    }
55   
 
56  1 toggle @Test
57    public void testBuildFailureMessageForSingleAnd1Node() throws Exception {
58    // Rule: A AND B
59  1 Token and = adapter.createToken(BooleanFunctionParser.AND, "*");
60  1 Token a = adapter.createToken(BooleanFunctionParser.ALPHA, "A");
61  1 Token b = adapter.createToken(BooleanFunctionParser.ALPHA, "B");
62   
63    // root node
64  1 BooleanNode andNode = new BooleanNode(and);
65   
66  1 BooleanNode aNode = new BooleanNode(a);
67  1 aNode.setParent(andNode);
68  1 aNode.setValue(Boolean.TRUE);
69  1 aNode.setNodeMessage("MATH101");
70   
71  1 BooleanNode bNode = new BooleanNode(b);
72  1 bNode.setParent(andNode);
73  1 bNode.setValue(Boolean.FALSE);
74  1 bNode.setNodeMessage("MATH201");
75   
76  1 andNode.addChild(aNode); // left node
77  1 andNode.addChild(bNode); // right node
78   
79  1 String failureMsg = failureMessageBuilder.buildFailureMessage(andNode);
80   
81  1 Assert.assertEquals(failureMsg, andNode.getNodeMessage());
82  1 Assert.assertEquals("MATH201", failureMsg);
83    }
84   
 
85  1 toggle @Test
86    public void testBuildFailureMessageForSingleAnd2Node() throws Exception {
87    // Rule: A AND B
88  1 Token and = adapter.createToken(BooleanFunctionParser.AND, "*");
89  1 Token a = adapter.createToken(BooleanFunctionParser.ALPHA, "A");
90  1 Token b = adapter.createToken(BooleanFunctionParser.ALPHA, "B");
91   
92    // root node
93  1 BooleanNode andNode = new BooleanNode(and);
94   
95  1 BooleanNode aNode = new BooleanNode(a);
96  1 aNode.setParent(andNode);
97  1 aNode.setValue(Boolean.FALSE);
98  1 aNode.setNodeMessage("MATH101");
99   
100  1 BooleanNode bNode = new BooleanNode(b);
101  1 bNode.setParent(andNode);
102  1 bNode.setValue(Boolean.FALSE);
103  1 bNode.setNodeMessage("MATH201");
104   
105  1 andNode.addChild(aNode); // left node
106  1 andNode.addChild(bNode); // right node
107   
108  1 String failureMsg = failureMessageBuilder.buildFailureMessage(andNode);
109   
110  1 Assert.assertEquals(failureMsg, andNode.getNodeMessage());
111  1 Assert.assertEquals("MATH101 AND MATH201", failureMsg);
112    }
113   
 
114  1 toggle @Test
115    public void testBuildFailureMessageForListOfNodes_Simple() throws Exception {
116    // Rule: A AND (B OR C)
117  1 Token and = adapter.createToken(BooleanFunctionParser.AND, "*");
118  1 Token or = adapter.createToken(BooleanFunctionParser.OR, "+");
119  1 Token a = adapter.createToken(BooleanFunctionParser.ALPHA, "A");
120  1 Token b = adapter.createToken(BooleanFunctionParser.ALPHA, "B");
121  1 Token c = adapter.createToken(BooleanFunctionParser.ALPHA, "C");
122   
123    // root node
124  1 BooleanNode andNode = new BooleanNode(and);
125  1 BooleanNode orNode = new BooleanNode(or);
126  1 orNode.setParent(andNode);
127  1 orNode.setValue(Boolean.FALSE);
128   
129  1 BooleanNode aNode = new BooleanNode(a);
130  1 aNode.setParent(andNode);
131  1 aNode.setValue(Boolean.FALSE);
132  1 aNode.setNodeMessage("MATH101");
133   
134  1 BooleanNode bNode = new BooleanNode(b);
135  1 bNode.setParent(orNode);
136  1 bNode.setValue(Boolean.FALSE);
137  1 bNode.setNodeMessage("MATH201");
138   
139  1 BooleanNode cNode = new BooleanNode(c);
140  1 cNode.setParent(orNode);
141  1 cNode.setValue(Boolean.FALSE);
142  1 cNode.setNodeMessage("MATH301");
143   
144  1 orNode.addChild(bNode); // left node
145  1 orNode.addChild(cNode); // right node
146  1 andNode.addChild(aNode); // left node
147  1 andNode.addChild(orNode); // right node
148   
149    // List of nodes order is important for building failure message
150  1 List<BooleanNode> list = new ArrayList<BooleanNode>();
151  1 list.add(cNode);
152  1 list.add(bNode);
153  1 list.add(orNode);
154  1 list.add(aNode);
155  1 list.add(andNode);
156   
157  1 String failureMsg = failureMessageBuilder.buildFailureMessage(list);
158   
159  1 Assert.assertEquals(failureMsg, andNode.getNodeMessage());
160  1 Assert.assertEquals("MATH101 AND (MATH201 OR MATH301)", failureMsg);
161    }
162   
 
163  1 toggle @Test
164    public void testBuildFailureMessageForListOfNodes_Complex() throws Exception {
165    // Rule: (A OR B) AND (C OR D)
166  1 Token and = adapter.createToken(BooleanFunctionParser.AND, "*");
167  1 Token or1 = adapter.createToken(BooleanFunctionParser.OR, "+");
168  1 Token or2 = adapter.createToken(BooleanFunctionParser.OR, "+");
169  1 Token a = adapter.createToken(BooleanFunctionParser.ALPHA, "A");
170  1 Token b = adapter.createToken(BooleanFunctionParser.ALPHA, "B");
171  1 Token c = adapter.createToken(BooleanFunctionParser.ALPHA, "C");
172  1 Token d = adapter.createToken(BooleanFunctionParser.ALPHA, "D");
173   
174    // root node
175  1 BooleanNode andNode = new BooleanNode(and);
176   
177  1 BooleanNode orNode1 = new BooleanNode(or1);
178  1 orNode1.setParent(andNode);
179  1 orNode1.setValue(Boolean.TRUE);
180   
181  1 BooleanNode orNode2 = new BooleanNode(or2);
182  1 orNode2.setParent(andNode);
183  1 orNode2.setValue(Boolean.FALSE);
184   
185  1 BooleanNode aNode = new BooleanNode(a);
186  1 aNode.setParent(orNode1);
187  1 aNode.setValue(Boolean.TRUE);
188  1 aNode.setNodeMessage("MATH101");
189   
190  1 BooleanNode bNode = new BooleanNode(b);
191  1 bNode.setParent(orNode1);
192  1 bNode.setValue(Boolean.TRUE);
193  1 bNode.setNodeMessage("MATH201");
194   
195  1 BooleanNode cNode = new BooleanNode(c);
196  1 cNode.setParent(orNode2);
197  1 cNode.setValue(Boolean.FALSE);
198  1 cNode.setNodeMessage("MATH301");
199   
200  1 BooleanNode dNode = new BooleanNode(d);
201  1 dNode.setParent(orNode2);
202  1 dNode.setValue(Boolean.FALSE);
203  1 dNode.setNodeMessage("MATH401");
204   
205  1 orNode1.addChild(aNode); // left node
206  1 orNode1.addChild(bNode); // right node
207  1 orNode2.addChild(cNode); // left node
208  1 orNode2.addChild(dNode); // right node
209  1 andNode.addChild(orNode1); // left node
210  1 andNode.addChild(orNode2); // right node
211   
212    // List of nodes order is important for building failure message
213  1 List<BooleanNode> list = new ArrayList<BooleanNode>();
214  1 list.add(aNode);
215  1 list.add(bNode);
216  1 list.add(cNode);
217  1 list.add(dNode);
218  1 list.add(orNode1);
219  1 list.add(orNode2);
220  1 list.add(andNode);
221   
222  1 String failureMsg = failureMessageBuilder.buildFailureMessage(list);
223   
224  1 Assert.assertEquals(failureMsg, andNode.getNodeMessage());
225  1 Assert.assertEquals("(MATH301 OR MATH401)", failureMsg);
226    }
227   
 
228  1 toggle @Test
229    public void testBuildFailureMessageForListOfNodes_MultipleRootNodes() throws Exception {
230  1 Token or = adapter.createToken(BooleanFunctionParser.OR, "+");
231  1 Token and = adapter.createToken(BooleanFunctionParser.AND, "*");
232   
233    // root node1
234  1 BooleanNode orNode = new BooleanNode(or);
235    // root node1
236  1 BooleanNode andNode = new BooleanNode(and);
237   
238  1 List<BooleanNode> list = new ArrayList<BooleanNode>();
239  1 list.add(orNode);
240  1 list.add(andNode);
241   
242  1 try {
243  1 failureMessageBuilder.buildFailureMessage(list);
244  0 Assert.fail("buildFailureMessage should have failed since list has 2 root nodes");
245    } catch (MessageBuilderException e) {
246  1 Assert.assertTrue(e.getMessage() != null);
247  1 Assert.assertTrue(e.getMessage().contains("label=+"));
248  1 Assert.assertTrue(e.getMessage().contains("label=*"));
249    }
250    }
251    }