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