Clover Coverage Report - KS Common 1.1-SNAPSHOT (Aggregated)
Coverage timestamp: Thu Mar 3 2011 04:37:11 EST
109   180   5   27.25
0   140   0.05   4
4     1.25  
1    
 
  SuccessFailureMessageBuilderTest       Line # 17 109 0% 5 1 99.1% 0.99115044
 
  (3)
 
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.SuccessFailureMessageBuilder;
15    import org.kuali.student.common.messagebuilder.impl.exceptions.MessageBuilderException;
16   
 
17    public class SuccessFailureMessageBuilderTest {
18    private final static CommonTreeAdaptor adapter = new CommonTreeAdaptor();
19    private SuccessFailureMessageBuilder successFailureMessageBuilder;
20   
 
21  3 toggle @Before
22    public void setUp() throws Exception {
23  3 BooleanOperators bo = new BooleanOperators();
24  3 successFailureMessageBuilder = new SuccessFailureMessageBuilder(bo);
25    }
26   
 
27  1 toggle @Test
28    public void testBuildFailureMessage() throws Exception {
29    // Rule: (A OR B) AND (C OR D)
30  1 Token and = adapter.createToken(BooleanFunctionParser.AND, "*");
31  1 Token or1 = adapter.createToken(BooleanFunctionParser.OR, "+");
32  1 Token or2 = adapter.createToken(BooleanFunctionParser.OR, "+");
33  1 Token a = adapter.createToken(BooleanFunctionParser.ALPHA, "A");
34  1 Token b = adapter.createToken(BooleanFunctionParser.ALPHA, "B");
35  1 Token c = adapter.createToken(BooleanFunctionParser.ALPHA, "C");
36  1 Token d = adapter.createToken(BooleanFunctionParser.ALPHA, "D");
37   
38    // root node
39  1 BooleanNode andNode = new BooleanNode(and);
40   
41  1 BooleanNode orNode1 = new BooleanNode(or1);
42  1 orNode1.setParent(andNode);
43  1 orNode1.setValue(Boolean.TRUE);
44   
45  1 BooleanNode orNode2 = new BooleanNode(or2);
46  1 orNode2.setParent(andNode);
47  1 orNode2.setValue(Boolean.TRUE);
48   
49  1 BooleanNode aNode = new BooleanNode(a);
50  1 aNode.setParent(orNode1);
51  1 aNode.setValue(Boolean.TRUE);
52  1 aNode.setNodeMessage("MATH101");
53   
54  1 BooleanNode bNode = new BooleanNode(b);
55  1 bNode.setParent(orNode1);
56  1 bNode.setValue(Boolean.FALSE);
57  1 bNode.setNodeMessage("MATH201");
58   
59  1 BooleanNode cNode = new BooleanNode(c);
60  1 cNode.setParent(orNode2);
61  1 cNode.setValue(Boolean.FALSE);
62  1 cNode.setNodeMessage("MATH301");
63   
64  1 BooleanNode dNode = new BooleanNode(d);
65  1 dNode.setParent(orNode2);
66  1 dNode.setValue(Boolean.TRUE);
67  1 dNode.setNodeMessage("MATH401");
68   
69  1 orNode1.addChild(aNode); // left node
70  1 orNode1.addChild(bNode); // right node
71  1 orNode2.addChild(cNode); // left node
72  1 orNode2.addChild(dNode); // right node
73  1 andNode.addChild(orNode1); // left node
74  1 andNode.addChild(orNode2); // right node
75   
76    // List of nodes order is important for building failure message
77  1 List<BooleanNode> list = new ArrayList<BooleanNode>();
78  1 list.add(aNode);
79  1 list.add(bNode);
80  1 list.add(cNode);
81  1 list.add(dNode);
82  1 list.add(orNode1);
83  1 list.add(orNode2);
84  1 list.add(andNode);
85   
86  1 String failureMsg = successFailureMessageBuilder.buildMessage(list);
87   
88  1 Assert.assertEquals(failureMsg, andNode.getNodeMessage());
89  1 Assert.assertEquals("MATH101 AND MATH401", failureMsg);
90    }
91   
 
92  1 toggle @Test
93    public void testBuildSuccessMessageForListOfNodes_Complex() throws Exception {
94    // Rule: (A OR B) AND (C OR D)
95  1 Token and = adapter.createToken(BooleanFunctionParser.AND, "*");
96  1 Token or1 = adapter.createToken(BooleanFunctionParser.OR, "+");
97  1 Token or2 = adapter.createToken(BooleanFunctionParser.OR, "+");
98  1 Token a = adapter.createToken(BooleanFunctionParser.ALPHA, "A");
99  1 Token b = adapter.createToken(BooleanFunctionParser.ALPHA, "B");
100  1 Token c = adapter.createToken(BooleanFunctionParser.ALPHA, "C");
101  1 Token d = adapter.createToken(BooleanFunctionParser.ALPHA, "D");
102   
103    // root node
104  1 BooleanNode andNode = new BooleanNode(and);
105   
106  1 BooleanNode orNode1 = new BooleanNode(or1);
107  1 orNode1.setParent(andNode);
108  1 orNode1.setValue(Boolean.TRUE);
109   
110  1 BooleanNode orNode2 = new BooleanNode(or2);
111  1 orNode2.setParent(andNode);
112  1 orNode2.setValue(Boolean.TRUE);
113   
114  1 BooleanNode aNode = new BooleanNode(a);
115  1 aNode.setParent(orNode1);
116  1 aNode.setValue(Boolean.TRUE);
117  1 aNode.setNodeMessage("MATH101");
118   
119  1 BooleanNode bNode = new BooleanNode(b);
120  1 bNode.setParent(orNode1);
121  1 bNode.setValue(Boolean.FALSE);
122  1 bNode.setNodeMessage("MATH201");
123   
124  1 BooleanNode cNode = new BooleanNode(c);
125  1 cNode.setParent(orNode2);
126  1 cNode.setValue(Boolean.FALSE);
127  1 cNode.setNodeMessage("MATH301");
128   
129  1 BooleanNode dNode = new BooleanNode(d);
130  1 dNode.setParent(orNode2);
131  1 dNode.setValue(Boolean.TRUE);
132  1 dNode.setNodeMessage("MATH401");
133   
134  1 orNode1.addChild(aNode); // left node
135  1 orNode1.addChild(bNode); // right node
136  1 orNode2.addChild(cNode); // left node
137  1 orNode2.addChild(dNode); // right node
138  1 andNode.addChild(orNode1); // left node
139  1 andNode.addChild(orNode2); // right node
140   
141    // List of nodes order is important for building failure message
142  1 List<BooleanNode> list = new ArrayList<BooleanNode>();
143  1 list.add(aNode);
144  1 list.add(bNode);
145  1 list.add(cNode);
146  1 list.add(dNode);
147  1 list.add(orNode1);
148  1 list.add(orNode2);
149  1 list.add(andNode);
150   
151  1 String failureMsg = successFailureMessageBuilder.buildMessage(list);
152   
153  1 Assert.assertEquals(failureMsg, andNode.getNodeMessage());
154  1 Assert.assertEquals("MATH101 AND MATH401", failureMsg);
155    }
156   
 
157  1 toggle @Test
158    public void testBuildSuccessMessageForListOfNodes_MultipleRootNodes() throws Exception {
159  1 Token or = adapter.createToken(BooleanFunctionParser.OR, "+");
160  1 Token and = adapter.createToken(BooleanFunctionParser.AND, "*");
161   
162    // root node1
163  1 BooleanNode orNode = new BooleanNode(or);
164    // root node1
165  1 BooleanNode andNode = new BooleanNode(and);
166   
167  1 List<BooleanNode> list = new ArrayList<BooleanNode>();
168  1 list.add(orNode);
169  1 list.add(andNode);
170   
171  1 try {
172  1 successFailureMessageBuilder.buildMessage(list);
173  0 Assert.fail("buildMessage should have failed since list has 2 root nodes");
174    } catch (MessageBuilderException e) {
175  1 Assert.assertTrue(e.getMessage() != null);
176  1 Assert.assertTrue(e.getMessage().contains("label=+"));
177  1 Assert.assertTrue(e.getMessage().contains("label=*"));
178    }
179    }
180    }