View Javadoc

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