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