Clover Coverage Report - Kuali Student 1.3.0-SNAPSHOT (Aggregated)
Coverage timestamp: Thu Apr 28 2011 05:03:32 EDT
70   167   11   6.36
0   116   0.16   11
11     1  
1    
 
  TestVelocityTemplateEngine       Line # 30 70 0% 11 81 0% 0.0
 
No Tests
 
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.util;
17   
18    import java.io.StringReader;
19    import java.math.BigDecimal;
20    import java.util.Arrays;
21    import java.util.Calendar;
22    import java.util.HashMap;
23    import java.util.List;
24    import java.util.Map;
25   
26    import org.apache.velocity.tools.generic.DateTool;
27    import org.junit.Assert;
28    import org.junit.Test;
29   
 
30    public class TestVelocityTemplateEngine {
31   
 
32  0 toggle @Test
33    public void testEvaluateString_ContextMap() throws Exception {
34  0 VelocityTemplateEngine templateEngine = new VelocityTemplateEngine();
35  0 Map<String, Object> map = new HashMap<String, Object>();
36  0 map.put("prop.1", "Kamal");
37  0 map.put("prop.2", "Larry");
38   
39  0 String s = "List of Developers: $prop.1, $prop.2";
40  0 String eval = templateEngine.evaluate(map, s);
41    // Velocity does not support dot notation in context keys
42  0 Assert.assertEquals("List of Developers: $prop.1, $prop.2", eval);
43    }
44   
 
45  0 toggle @Test
46    public void testEvaluateString1() throws Exception {
47  0 VelocityTemplateEngine templateEngine = new VelocityTemplateEngine();
48  0 Map<String, Object> map = new HashMap<String, Object>();
49  0 map.put("developers", "Kamal, Larry, Len, Sherman, Zdenek");
50   
51  0 String s = "List of Developers: $developers";
52  0 String eval = templateEngine.evaluate(map, s);
53   
54  0 Assert.assertEquals("List of Developers: Kamal, Larry, Len, Sherman, Zdenek", eval);
55    }
56   
 
57  0 toggle @Test
58    public void testEvaluateString2() throws Exception {
59  0 VelocityTemplateEngine templateEngine = new VelocityTemplateEngine();
60  0 List<String> devList = Arrays.asList(new String[] {"Kamal", "Larry", "Len", "Sherman", "Zdenek"});
61  0 Map<String, Object> map = new HashMap<String, Object>();
62  0 map.put("developers", devList);
63   
64  0 String s = "List of Developers: $developers";
65  0 String eval = templateEngine.evaluate(map, s);
66   
67  0 Assert.assertEquals("List of Developers: [Kamal, Larry, Len, Sherman, Zdenek]", eval);
68    }
69   
 
70  0 toggle @Test
71    public void testEvaluateReader() throws Exception {
72  0 VelocityTemplateEngine templateEngine = new VelocityTemplateEngine();
73  0 Map<String, Object> map = new HashMap<String, Object>();
74  0 map.put("developers", "Kamal, Larry, Len, Sherman, Zdenek");
75   
76  0 StringReader sr = new StringReader("List of Developers: $developers");
77  0 String eval = templateEngine.evaluate(map, sr);
78   
79  0 Assert.assertEquals("List of Developers: Kamal, Larry, Len, Sherman, Zdenek", eval);
80    }
81   
 
82  0 toggle @Test
83    public void testEvaluateNumber() throws Exception {
84  0 VelocityTemplateEngine templateEngine = new VelocityTemplateEngine();
85  0 Map<String, Object> map = new HashMap<String, Object>();
86  0 map.put("expectedValue", new BigDecimal("100"));
87  0 map.put("resultValue", new BigDecimal("60"));
88   
89  0 String s = "#set( $difference = ($expectedValue - $resultValue) )expectedValue=$expectedValue, resultValue=$resultValue, difference=$difference";
90  0 String eval = templateEngine.evaluate(map, s);
91   
92  0 Assert.assertEquals("expectedValue=100, resultValue=60, difference=40", eval);
93    }
94   
 
95  0 toggle @Test
96    public void testEvaluate_MathTool() throws Exception {
97  0 VelocityTemplateEngine templateEngine = new VelocityTemplateEngine();
98  0 Map<String, Object> map = new HashMap<String, Object>();
99  0 map.put("expectedValue", "100");
100  0 map.put("resultValue", "60");
101   
102  0 String s = "#set( $difference = ( $mathTool.toNumber($expectedValue) - $mathTool.toNumber($resultValue)) )expectedValue=$expectedValue, resultValue=$resultValue, difference=$difference";
103  0 String eval = templateEngine.evaluate(map, s);
104   
105  0 Assert.assertEquals("expectedValue=100, resultValue=60, difference=40", eval);
106    }
107   
 
108  0 toggle @Test
109    public void testEvaluate_DateTool() throws Exception {
110  0 VelocityTemplateEngine templateEngine = new VelocityTemplateEngine();
111  0 Map<String, Object> map = new HashMap<String, Object>();
112  0 map.put("expectedValue", "100");
113  0 map.put("resultValue", "60");
114   
115  0 String s = "Date created $dateTool.get('yyyy-MM-dd HH:mm z')";
116  0 String eval = templateEngine.evaluate(map, s);
117   
118  0 Assert.assertEquals("Date created " + new DateTool().get("yyyy-MM-dd HH:mm z"), eval);
119    }
120   
 
121  0 toggle @Test
122    public void testEvaluate_DateComparisonTool() throws Exception {
123  0 VelocityTemplateEngine templateEngine = new VelocityTemplateEngine();
124  0 Map<String, Object> map = new HashMap<String, Object>();
125  0 map.put("date1", Calendar.getInstance());
126   
127  0 String s = "$dateComparisonTool.whenIs($date1, $date1)";
128  0 String eval = templateEngine.evaluate(map, s);
129   
130  0 Assert.assertEquals("same time", eval);
131    }
132   
 
133  0 toggle @Test
134    public void testEvaluate_DateComparisonTool_NullContextMap() throws Exception {
135  0 VelocityTemplateEngine templateEngine = new VelocityTemplateEngine();
136   
137  0 String s = "$dateComparisonTool.difference($dateTool.calendar, $dateTool.calendar).hours";
138  0 String eval = templateEngine.evaluate(null, s);
139   
140  0 Assert.assertEquals("0", eval);
141    }
142   
 
143  0 toggle @Test
144    public void testEvaluate_NumberTool() throws Exception {
145  0 VelocityTemplateEngine templateEngine = new VelocityTemplateEngine();
146  0 Map<String, Object> map = new HashMap<String, Object>();
147  0 map.put("expectedValue", "100.12");
148   
149  0 String s = "$numberTool.currency($expectedValue)";
150  0 String eval = templateEngine.evaluate(map, s);
151   
152  0 Assert.assertEquals("$100.12", eval);
153    }
154   
 
155  0 toggle @Test
156    public void testEvaluate_SortTool() throws Exception {
157  0 VelocityTemplateEngine templateEngine = new VelocityTemplateEngine();
158  0 List<String> devList = Arrays.asList(new String[] {"Len", "Larry", "Kamal", "Zdenek", "Sherman"});
159  0 Map<String, Object> map = new HashMap<String, Object>();
160  0 map.put("developers", devList);
161   
162  0 String s = "$sortTool.sort($developers)";
163  0 String eval = templateEngine.evaluate(map, s);
164   
165  0 Assert.assertEquals("[Kamal, Larry, Len, Sherman, Zdenek]", eval);
166    }
167    }