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