Clover Coverage Report - Kuali Student 1.2-M5-SNAPSHOT (Aggregated)
Coverage timestamp: Mon Aug 29 2011 05:04:27 EDT
216   371   22   10.8
0   302   0.1   20
20     1.1  
1    
 
  MessageBuilderImplTest       Line # 34 216 0% 22 2 99.2% 0.9915254
 
  (19)
 
1    /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10    * software distributed under the License is distributed on an "AS IS"
11    * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12    * or implied. See the License for the specific language governing
13    * permissions and limitations under the License.
14    */
15   
16    package org.kuali.student.common.messagebuilder.impl;
17   
18    import java.util.HashMap;
19    import java.util.Map;
20   
21    import org.apache.velocity.tools.generic.DateTool;
22    import org.junit.Assert;
23    import org.junit.Before;
24    import org.junit.Test;
25    import org.kuali.student.common.messagebuilder.MessageBuilder;
26    import org.kuali.student.common.messagebuilder.MessageTreeBuilder;
27    import org.kuali.student.common.messagebuilder.booleanmessage.BooleanMessage;
28    import org.kuali.student.common.messagebuilder.booleanmessage.MessageContainer;
29    import org.kuali.student.common.messagebuilder.booleanmessage.ast.BooleanFunctionResult;
30    import org.kuali.student.common.messagebuilder.booleanmessage.ast.BooleanMessageImpl;
31    import org.kuali.student.common.messagebuilder.impl.MessageBuilderImpl;
32    import org.kuali.student.common.messagebuilder.impl.exceptions.MessageBuilderException;
33   
 
34    public class MessageBuilderImplTest {
35    private MessageBuilder builder;
36   
 
37  19 toggle @Before
38    public void setUp() throws Exception {
39  19 BooleanOperators bo = new BooleanOperators();
40  19 MessageTreeBuilder tnmBuilder = new SuccessFailureMessageBuilder(bo);
41  19 builder = new MessageBuilderImpl("en", tnmBuilder);
42    }
43   
 
44  1 toggle @Test
45    public void testBuildMessage1_InvalidBooleanExpression() throws Exception {
46  1 MessageContainer messageContainer = new MessageContainer();
47  1 BooleanMessage bm1 = new BooleanMessageImpl("M1", true, "Message 1 is true");
48  1 BooleanMessage bm2 = new BooleanMessageImpl("M2", true, "Message 2 is true");
49  1 BooleanMessage bm3 = new BooleanMessageImpl("M3", true, "Message 3 is true");
50  1 messageContainer.addMessage(bm1);
51  1 messageContainer.addMessage(bm2);
52  1 messageContainer.addMessage(bm3);
53   
54  1 try {
55  1 builder.buildMessage("M1**M2**M3", messageContainer);
56  0 Assert.fail("Building message should have failed due to boolean expression syntax error");
57    } catch(MessageBuilderException e) {
58  1 Assert.assertTrue(e.getMessage().startsWith("Building message failed: Boolean Function Parser Error. Invalid Boolean Expression: 'M1**M2**M3'"));
59    }
60    }
61   
 
62  1 toggle @Test
63    public void testBuildMessage1_True() throws Exception {
64  1 MessageContainer messageContainer = new MessageContainer();
65  1 BooleanMessage bm1 = new BooleanMessageImpl("M1", true, "Message 1 is true");
66  1 BooleanMessage bm2 = new BooleanMessageImpl("M2", true, "Message 2 is true");
67  1 BooleanMessage bm3 = new BooleanMessageImpl("M3", true, "Message 3 is true");
68  1 messageContainer.addMessage(bm1);
69  1 messageContainer.addMessage(bm2);
70  1 messageContainer.addMessage(bm3);
71   
72  1 String message = builder.buildMessage("M1*M2*M3", messageContainer);
73  1 Assert.assertEquals("Message 1 is true AND Message 2 is true AND Message 3 is true", message);
74    }
75   
 
76  1 toggle @Test
77    public void testBuildMessage2_True() throws Exception {
78  1 MessageContainer messageContainer = new MessageContainer();
79  1 BooleanMessage bm1 = new BooleanMessageImpl("M1", true, "Message 1 is true");
80  1 BooleanMessage bm2 = new BooleanMessageImpl("M2", false, "Message 2 is false");
81  1 BooleanMessage bm3 = new BooleanMessageImpl("M3", true, "Message 3 is true");
82  1 messageContainer.addMessage(bm1);
83  1 messageContainer.addMessage(bm2);
84  1 messageContainer.addMessage(bm3);
85   
86  1 String message = builder.buildMessage("(M1*M2)+M3", messageContainer);
87  1 Assert.assertEquals("Message 3 is true", message);
88    }
89   
 
90  1 toggle @Test
91    public void testBuildMessage3_False() throws Exception {
92  1 MessageContainer messageContainer = new MessageContainer();
93  1 BooleanMessage bm1 = new BooleanMessageImpl("M1", true, "Message 1 is true");
94  1 BooleanMessage bm2 = new BooleanMessageImpl("M2", false, "Must have CHEM 101");
95  1 messageContainer.addMessage(bm1);
96  1 messageContainer.addMessage(bm2);
97   
98  1 String message = builder.buildMessage("M1*M2", messageContainer);
99  1 Assert.assertEquals("Must have CHEM 101", message);
100    }
101   
 
102  1 toggle @Test
103    public void testBuildMessage4_False() throws Exception {
104  1 MessageContainer messageContainer = new MessageContainer();
105  1 BooleanMessage bm1 = new BooleanMessageImpl("M1", true, "Requirement Fulfilled 1");
106  1 BooleanMessage bm2 = new BooleanMessageImpl("M2", false, "Must have CHEM 101");
107  1 BooleanMessage bm3 = new BooleanMessageImpl("M3", false, "Must have PHYS 101");
108  1 messageContainer.addMessage(bm1);
109  1 messageContainer.addMessage(bm2);
110  1 messageContainer.addMessage(bm3);
111   
112  1 BooleanFunctionResult result = builder.build("(M1*M2)+M3", messageContainer);
113  1 Assert.assertFalse(result.getResult());
114  1 Assert.assertEquals("Must have CHEM 101 OR Must have PHYS 101", result.getMessage());
115    }
116   
 
117  1 toggle @Test
118    public void testBuildMessage5_True() throws Exception {
119  1 MessageContainer messageContainer = new MessageContainer();
120  1 BooleanMessage bm1 = new BooleanMessageImpl("M1", true, "Message 1 is true");
121  1 BooleanMessage bm2 = new BooleanMessageImpl("M2", true, "Message 2 is true");
122  1 BooleanMessage bm3 = new BooleanMessageImpl("M3", false, "Message 3 is false");
123  1 messageContainer.addMessage(bm1);
124  1 messageContainer.addMessage(bm2);
125  1 messageContainer.addMessage(bm3);
126   
127    // result = True : M1*M2+M3 = T and T or F -> (M1*M2)+M3 (T and T) or F
128  1 String bf = "M1*M2+M3";
129  1 BooleanFunctionResult result = builder.build(bf, messageContainer);
130    // Order of boolean operation: AND before OR, operations inside parentheses before anything else
131  1 Assert.assertEquals(bf, result.getBooleanFunction());
132  1 Assert.assertTrue(result.getResult());
133  1 Assert.assertEquals("Message 1 is true AND Message 2 is true", result.getMessage());
134    }
135   
 
136  1 toggle @Test
137    public void testBuildMessage6() throws Exception {
138  1 MessageContainer messageContainer = new MessageContainer();
139  1 BooleanMessage bm1 = new BooleanMessageImpl("M1", true, "Message 1 is true");
140  1 BooleanMessage bm2 = new BooleanMessageImpl("M2", true, "Message 2 is true");
141  1 BooleanMessage bm3 = new BooleanMessageImpl("M3", false, "Message 3 is false");
142  1 BooleanMessage bm4 = new BooleanMessageImpl("M4", false, "Message 4 is false");
143  1 messageContainer.addMessage(bm1);
144  1 messageContainer.addMessage(bm2);
145  1 messageContainer.addMessage(bm3);
146  1 messageContainer.addMessage(bm4);
147   
148  1 BooleanFunctionResult result1 = builder.build("M1*M2+M3*M4", messageContainer);
149  1 BooleanFunctionResult result2 = builder.build("(M1*M2)+(M3*M4)", messageContainer);
150  1 BooleanFunctionResult result3 = builder.build("((M1*M2)+M3)*M4", messageContainer);
151   
152    // Order of boolean operation: ANDs before ORs and
153    // operations inside parentheses before anything else.
154    // result1 = True : T and T or F and F
155    // result2 = True : (T and T) or (F and F)
156    // result3 = False : ((T and T) or F) and F
157  1 Assert.assertEquals("M1*M2+M3*M4", result1.getBooleanFunction());
158  1 Assert.assertEquals("(M1*M2)+(M3*M4)", result2.getBooleanFunction());
159  1 Assert.assertEquals("((M1*M2)+M3)*M4", result3.getBooleanFunction());
160  1 Assert.assertTrue(result1.getResult());
161  1 Assert.assertTrue(result2.getResult());
162  1 Assert.assertFalse(result3.getResult());
163  1 Assert.assertTrue(result1.getResult() == result2.getResult());
164  1 Assert.assertFalse(result1.getResult() == result3.getResult());
165  1 Assert.assertFalse(result2.getResult() == result3.getResult());
166  1 Assert.assertTrue(result1.getMessage().equals(result2.getMessage()));
167  1 Assert.assertFalse(result1.getMessage().equals(result3.getMessage()));
168  1 Assert.assertFalse(result2.getMessage().equals(result3.getMessage()));
169    }
170   
 
171  1 toggle @Test
172    public void testBuildMessage7() throws Exception {
173  1 MessageContainer messageContainer = new MessageContainer();
174  1 BooleanMessage bm1 = new BooleanMessageImpl("A", true, "A is true");
175  1 BooleanMessage bm2 = new BooleanMessageImpl("B", true, "B is true");
176  1 BooleanMessage bm3 = new BooleanMessageImpl("C", true, "C is true");
177  1 messageContainer.addMessage(bm1);
178  1 messageContainer.addMessage(bm2);
179  1 messageContainer.addMessage(bm3);
180   
181  1 String message = builder.buildMessage("(A*B)+(A*C)+(B*C)", messageContainer);
182  1 Assert.assertEquals("(A is true AND B is true) OR (A is true AND C is true) OR (B is true AND C is true)", message);
183    }
184   
 
185  1 toggle @Test
186    public void testBuildMessage8() throws Exception {
187  1 MessageContainer messageContainer = new MessageContainer();
188  1 BooleanMessage bm1 = new BooleanMessageImpl("A", true, "A");
189  1 BooleanMessage bm2 = new BooleanMessageImpl("B", true, "B");
190  1 BooleanMessage bm3 = new BooleanMessageImpl("C", true, "C");
191  1 BooleanMessage bm4 = new BooleanMessageImpl("D", true, "D");
192  1 messageContainer.addMessage(bm1);
193  1 messageContainer.addMessage(bm2);
194  1 messageContainer.addMessage(bm3);
195  1 messageContainer.addMessage(bm4);
196   
197  1 String message = builder.buildMessage("A*(B+C+D)", messageContainer);
198  1 Assert.assertEquals("A AND (B OR C OR D)", message);
199    }
200   
 
201  1 toggle @Test
202    public void testBuildMessage9() throws Exception {
203  1 MessageContainer messageContainer = new MessageContainer();
204  1 BooleanMessage bm1 = new BooleanMessageImpl("A", true, "A");
205  1 BooleanMessage bm2 = new BooleanMessageImpl("B", true, "B");
206  1 BooleanMessage bm3 = new BooleanMessageImpl("C", true, "C");
207  1 BooleanMessage bm4 = new BooleanMessageImpl("D", true, "D");
208  1 messageContainer.addMessage(bm1);
209  1 messageContainer.addMessage(bm2);
210  1 messageContainer.addMessage(bm3);
211  1 messageContainer.addMessage(bm4);
212   
213  1 String message = builder.buildMessage("A*(B+(C+D))", messageContainer);
214  1 Assert.assertEquals("A AND (B OR C OR D)", message);
215    }
216   
 
217  1 toggle @Test
218    public void testBuildMessage10() throws Exception {
219  1 MessageContainer messageContainer = new MessageContainer();
220  1 BooleanMessage bm1 = new BooleanMessageImpl("A", true, "A");
221  1 BooleanMessage bm2 = new BooleanMessageImpl("B", true, "B");
222  1 BooleanMessage bm3 = new BooleanMessageImpl("C", true, "C");
223  1 BooleanMessage bm4 = new BooleanMessageImpl("D", true, "D");
224  1 messageContainer.addMessage(bm1);
225  1 messageContainer.addMessage(bm2);
226  1 messageContainer.addMessage(bm3);
227  1 messageContainer.addMessage(bm4);
228   
229  1 String message = builder.buildMessage("(A*(B+C))+D", messageContainer);
230  1 Assert.assertEquals("(A AND (B OR C)) OR D", message);
231    }
232   
 
233  1 toggle @Test
234    public void testBuildMessage11() throws Exception {
235  1 MessageContainer messageContainer = new MessageContainer();
236  1 BooleanMessage bm1 = new BooleanMessageImpl("A", true, "A");
237  1 BooleanMessage bm2 = new BooleanMessageImpl("B", true, "B");
238  1 BooleanMessage bm3 = new BooleanMessageImpl("C", false, "C");
239  1 BooleanMessage bm4 = new BooleanMessageImpl("D", false, "D");
240  1 messageContainer.addMessage(bm1);
241  1 messageContainer.addMessage(bm2);
242  1 messageContainer.addMessage(bm3);
243  1 messageContainer.addMessage(bm4);
244   
245  1 String message = builder.buildMessage("(A*(B+C))+D", messageContainer);
246  1 Assert.assertEquals("A AND B", message);
247    }
248   
 
249  1 toggle @Test
250    public void testBuildMessage12() throws Exception {
251  1 MessageContainer messageContainer = new MessageContainer();
252  1 BooleanMessage bm1 = new BooleanMessageImpl("A", true, "A");
253  1 BooleanMessage bm2 = new BooleanMessageImpl("B", true, "B");
254  1 BooleanMessage bm3 = new BooleanMessageImpl("C", true, "C");
255  1 BooleanMessage bm4 = new BooleanMessageImpl("D", true, "D");
256  1 messageContainer.addMessage(bm1);
257  1 messageContainer.addMessage(bm2);
258  1 messageContainer.addMessage(bm3);
259  1 messageContainer.addMessage(bm4);
260   
261  1 String message = builder.buildMessage("(A*B)+(C+D)", messageContainer);
262  1 Assert.assertEquals("(A AND B) OR C OR D", message);
263    }
264   
 
265  1 toggle @Test
266    public void testBuildMessage13() throws Exception {
267  1 MessageContainer messageContainer = new MessageContainer();
268  1 BooleanMessage bm1 = new BooleanMessageImpl("A", true, "A");
269  1 BooleanMessage bm2 = new BooleanMessageImpl("B", false, "B");
270  1 BooleanMessage bm3 = new BooleanMessageImpl("C", false, "C");
271  1 BooleanMessage bm4 = new BooleanMessageImpl("D", false, "D");
272  1 messageContainer.addMessage(bm1);
273  1 messageContainer.addMessage(bm2);
274  1 messageContainer.addMessage(bm3);
275  1 messageContainer.addMessage(bm4);
276   
277  1 String message = builder.buildMessage("(A*B)+(C+D)", messageContainer);
278  1 Assert.assertEquals("B OR C OR D", message);
279    }
280   
 
281  1 toggle @Test
282    public void testBuildMessageTemplate1_NullContextMap_True() throws Exception {
283  1 MessageContainer messageContainer = new MessageContainer();
284  1 BooleanMessage bm1 = new BooleanMessageImpl("M1", true, "Message 1 is true");
285  1 BooleanMessage bm2 = new BooleanMessageImpl("M2", true, "Message 2 is true");
286  1 BooleanMessage bm3 = new BooleanMessageImpl("M3", true, "Message 3 is true");
287  1 messageContainer.addMessage(bm1);
288  1 messageContainer.addMessage(bm2);
289  1 messageContainer.addMessage(bm3);
290   
291  1 BooleanFunctionResult result = builder.build("M1*M2*M3", messageContainer, null);
292  1 Assert.assertEquals("Message 1 is true AND Message 2 is true AND Message 3 is true", result.getMessage());
293    }
294   
 
295  1 toggle @Test
296    public void testBuildMessageTemplate2_NullContextMap_False() throws Exception {
297  1 MessageContainer messageContainer = new MessageContainer();
298  1 BooleanMessage bm1 = new BooleanMessageImpl("M1", true, "Message 1 is true");
299  1 BooleanMessage bm2 = new BooleanMessageImpl("M2", false, "Message 2 is false");
300  1 BooleanMessage bm3 = new BooleanMessageImpl("M3", true, "Message 3 is true");
301  1 messageContainer.addMessage(bm1);
302  1 messageContainer.addMessage(bm2);
303  1 messageContainer.addMessage(bm3);
304   
305  1 BooleanFunctionResult result = builder.build("M1*M2*M3", messageContainer, null);
306  1 Assert.assertEquals("Message 2 is false", result.getMessage());
307    }
308   
 
309  1 toggle @Test
310    public void testBuildMessageTemplate3_True() throws Exception {
311  1 Map<String, Object> map = new HashMap<String, Object>();
312  1 map.put("expectedValue", "100");
313  1 map.put("resultValue", "60");
314   
315  1 MessageContainer messageContainer = new MessageContainer();
316  1 String successMsg1 = "Date created $dateTool.get('yyyy-MM-dd')";
317  1 String successMsg2 = "#set( $difference = ( $mathTool.toNumber($expectedValue) - $mathTool.toNumber($resultValue)) )" +
318    "expectedValue=$expectedValue, resultValue=$resultValue, difference=$difference";
319  1 BooleanMessage bm1 = new BooleanMessageImpl("M1", true, successMsg1);
320  1 BooleanMessage bm2 = new BooleanMessageImpl("M2", true, successMsg2);
321  1 messageContainer.addMessage(bm1);
322  1 messageContainer.addMessage(bm2);
323   
324  1 BooleanFunctionResult result = builder.build("M1*M2", messageContainer, map);
325  1 Assert.assertEquals("Date created " + new DateTool().get("yyyy-MM-dd") +
326    " AND expectedValue=100, resultValue=60, difference=40", result.getMessage());
327    }
328   
 
329  1 toggle @Test
330    public void testBuildMessageTemplate4_False() throws Exception {
331  1 Map<String, Object> map = new HashMap<String, Object>();
332  1 map.put("expectedValue", "100");
333  1 map.put("resultValue", "60");
334   
335  1 MessageContainer messageContainer = new MessageContainer();
336  1 String failureMsg1 = "Date created $dateTool.get('yyyy-MM-dd')";
337  1 String failureMsg2 = "#set( $difference = ( $mathTool.toNumber($expectedValue) - $mathTool.toNumber($resultValue)) )" +
338    "expectedValue=$expectedValue, resultValue=$resultValue, difference=$difference";
339  1 BooleanMessage bm1 = new BooleanMessageImpl("M1", false, failureMsg1);
340  1 BooleanMessage bm2 = new BooleanMessageImpl("M2", false, failureMsg2);
341  1 messageContainer.addMessage(bm1);
342  1 messageContainer.addMessage(bm2);
343   
344  1 BooleanFunctionResult result = builder.build("M1*M2", messageContainer, map);
345  1 Assert.assertEquals("Date created " + new DateTool().get("yyyy-MM-dd") +
346    " AND expectedValue=100, resultValue=60, difference=40", result.getMessage());
347    }
348   
 
349  1 toggle @Test
350    public void testBuildMessageTemplate5_False_Exception() throws Exception {
351  1 Map<String, Object> map = new HashMap<String, Object>();
352  1 map.put("expectedValue", "100");
353  1 map.put("resultValue", "60");
354   
355  1 MessageContainer messageContainer = new MessageContainer();
356  1 String failureMsg1 = "Date created $dateTool.get('yyyy-MM-dd HH:mm:ss z')";
357    // VTL syntax error
358  1 String failureMsg2 = "#set( $difference = expectedValue=$expectedValue, resultValue=$resultValue, difference=$difference";
359  1 BooleanMessage bm1 = new BooleanMessageImpl("M1", false, failureMsg1);
360  1 BooleanMessage bm2 = new BooleanMessageImpl("M2", false, failureMsg2);
361  1 messageContainer.addMessage(bm1);
362  1 messageContainer.addMessage(bm2);
363   
364  1 try {
365  1 builder.build("M1*M2", messageContainer, map);
366  0 Assert.fail("Building message should have failed due to VTL syntax error");
367    } catch(MessageBuilderException e) {
368  1 Assert.assertTrue(true);
369    }
370    }
371    }