View Javadoc

1   package org.apache.velocity.tools.test.whitebox;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Locale;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Calendar;
26  import java.util.Date;
27  import java.util.GregorianCalendar;
28  
29  import org.junit.*;
30  import static org.junit.Assert.*;
31  
32  import org.apache.velocity.tools.generic.Alternator;
33  import org.apache.velocity.tools.generic.AlternatorTool;
34  import org.apache.velocity.tools.generic.ComparisonDateTool;
35  import org.apache.velocity.tools.generic.DateTool;
36  import org.apache.velocity.tools.generic.EscapeTool;
37  import org.apache.velocity.tools.generic.FieldTool;
38  import org.apache.velocity.tools.generic.MathTool;
39  import org.apache.velocity.tools.generic.NumberTool;
40  import org.apache.velocity.tools.generic.ResourceTool;
41  import org.apache.velocity.tools.ToolManager;
42  import org.apache.velocity.tools.ToolContext;
43  
44  /**
45   * <p>Generic tools whitebox tests.</p>
46   *
47   * @author <a href="mailto:cbrisson@apache.org">Claude Brisson</a>
48   * @author Nathan Bubna
49   * @since Velocity Tools 1.3
50   * @version $Id$
51   */
52  
53  public class GenericToolsTests {
54  
55      private static final String TOOLBOX_PATH = "@test.conf.dir@/whiteboxtest-toolbox.xml";
56  
57      private static ToolContext toolbox = null;
58  
59      public static @BeforeClass void initGenericToolsTests() throws Exception {
60          ToolManager manager = new ToolManager(false);
61          manager.configure(TOOLBOX_PATH);
62          toolbox = manager.createContext();
63      }
64  
65      protected void assertStringEquals(String expected, Object testThis) {
66          assertEquals(expected, String.valueOf(testThis));
67      }
68  
69      public @Test void testDateTool() { /* TODO still incomplete */
70          DateTool dateTool = (DateTool)toolbox.get("date");
71          assertNotNull(dateTool);
72          Date date = new GregorianCalendar(2007,0,2).getTime();
73          String disp = "2007-01-02";
74          String disp2 = "2007/01/02";
75          /* check configured format */
76          assertEquals("yyyy-MM-dd",dateTool.getFormat());
77          /* test formatting */
78          assertEquals(disp,dateTool.format(date));
79          assertEquals(disp2,dateTool.format("yyyy/MM/dd",date));
80          /* test parsing */
81          assertEquals(2007,dateTool.getYear(disp));
82          assertEquals(0,dateTool.getMonth(disp));
83          assertEquals(2,dateTool.getDay(disp));
84      }
85  
86      public @Test void testEscapeTool() {
87          EscapeTool escapeTool = (EscapeTool)toolbox.get("esc");
88          assertNotNull(escapeTool);
89          assertEquals("${esc.d}foo ${esc.h}bar()",escapeTool.velocity("$foo #bar()"));
90          /* propertyKey */
91          assertEquals("\\ C\\:\\\\Program\\ Files",escapeTool.propertyKey(" C:\\Program Files"));
92          /* propertyValue */
93          assertEquals("\\ C\\:\\\\Program Files",escapeTool.propertyValue(" C:\\Program Files"));
94          /* java */
95          assertEquals("\\uFFFF\\b\\n\\t\\f\\r\\\"\\\\",escapeTool.java("\uFFFF\b\n\t\f\r\"\\"));
96          /* javascript */
97          assertEquals("\\uFFFF\\b\\n\\t\\f\\r\\\"\\'\\\\",escapeTool.javascript("\uFFFF\b\n\t\f\r\"'\\"));
98          /* html */
99          assertEquals("&quot;&amp;&lt;&gt;&nbsp;",escapeTool.html("\"&<>"+(char)160));
100         /* url */
101         assertEquals("%40%2F%3F%3D+%26",escapeTool.url("@/?= &"));
102         /* sql */
103         assertEquals("''",escapeTool.sql("'"));
104         /* xml */
105         assertEquals("&quot;&amp;&lt;&gt;",escapeTool.html("\"&<>"));
106         /* unicode */
107         assertEquals("\uf00b", escapeTool.unicode("f00b"));
108         assertEquals("\u1010", escapeTool.unicode("\\u1010"));
109         assertEquals("\u1111", escapeTool.unicode(1111));
110     }
111 
112     public static String MUTABLE_FIELD = "foo";
113 
114     public @Test void testFieldTool() {
115         FieldTool fieldTool = (FieldTool)toolbox.get("field");
116         assertNotNull(fieldTool);
117 
118         // read a constant from the configured included Class
119         assertEquals(Integer.MAX_VALUE, fieldTool.get("MAX_VALUE"));
120 
121         // read a constant from java.lang.Boolean and make sure it is the same
122         assertSame(Boolean.TRUE, fieldTool.in("java.lang.Boolean").get("TRUE"));
123 
124         // tell it to read constants from a non-existant class
125         // (which should return null)
126         assertNull(fieldTool.in("no.such.Class"));
127 
128         // tell field tool to read constants from this instance's Class
129         // (which should NOT return null)
130         assertNotNull(fieldTool.in(this));
131         assertEquals(MUTABLE_FIELD, fieldTool.get("MUTABLE_FIELD"));
132         // grab the mutable field
133         String foo = MUTABLE_FIELD;
134         // change it
135         MUTABLE_FIELD = MUTABLE_FIELD + MUTABLE_FIELD;
136         // make sure it changed
137         assertFalse(foo.equals(MUTABLE_FIELD));
138         // make sure the fieldtool recognized that it changed
139         assertEquals(MUTABLE_FIELD, fieldTool.get("MUTABLE_FIELD"));
140 
141         // pass a full field path to the get() method
142         assertEquals(Long.MIN_VALUE, fieldTool.get("java.lang.Long.MIN_VALUE"));
143     }
144 
145     public @Test void testMathTool() {
146         MathTool mathTool = (MathTool)toolbox.get("math");
147         assertNotNull(mathTool);
148         assertEquals(1,mathTool.abs(-1));
149         assertEquals(2,mathTool.add(1,1));
150         assertEquals(3,mathTool.ceil(2.5));
151         assertEquals(4,mathTool.div(8,2));
152         assertEquals(5,mathTool.floor(5.1));
153         assertEquals(6,mathTool.getAverage(new long[] {5,6,7}));
154         /* getTotal() watches the type of its first argument, so assertEquals needs a long */
155         assertEquals((long)7,mathTool.getTotal(new long[] {2,2,3}));
156         assertEquals(8,mathTool.idiv(130,16));
157         assertEquals(9,mathTool.max(9,-10));
158         assertEquals(10,mathTool.min(10,20));
159         assertEquals(11,mathTool.mod(37,13));
160         assertEquals(12,mathTool.mul(3,4));
161         assertEquals(13,mathTool.round(12.8));
162         assertEquals(14.2,mathTool.roundTo(1,14.18));
163         assertEquals(-5.0,mathTool.roundTo(2,-4.999));
164         assertEquals(15,mathTool.sub(30,15));
165         assertEquals(16,mathTool.pow(4,2));
166         assertEquals(17,mathTool.toInteger("17"));
167         assertEquals(18.1,mathTool.toDouble("18.1"));
168     }
169 
170     public @Test void testNumberTool() {
171         NumberTool numberTool = (NumberTool)toolbox.get("number");
172         assertNotNull(numberTool);
173 //        assertEquals()
174     }
175 
176     public @Test void testResourceTool() {
177         ResourceTool textTool = (ResourceTool)toolbox.get("text");
178         assertNotNull(textTool);
179 
180         List<String> keys = textTool.getKeys();
181         assertTrue(keys.contains("foo"));
182         assertTrue(keys.contains("hello.whoever"));
183         assertTrue(keys.contains("world"));
184 
185         keys = textTool.get("hello").getKeys();
186         assertTrue(keys.contains("whoever"));
187         assertFalse(keys.contains("foo"));
188 
189         ResourceTool.Key foo = textTool.get("foo");
190         assertStringEquals("bar", foo);
191 
192         ResourceTool.Key frenchFoo = foo.locale(Locale.FRENCH);
193         assertStringEquals("barre", frenchFoo);
194 
195         ResourceTool.Key otherFoo = foo.bundle("resources2");
196         assertStringEquals("woogie", otherFoo);
197 
198         ResourceTool.Key helloWhoever = textTool.get("hello").get("whoever");
199         assertStringEquals("Hello {0}!", helloWhoever);
200 
201         ResourceTool.Key helloWorld = helloWhoever.insert(textTool.get("world"));
202         assertStringEquals("Hello World!", helloWorld);
203 
204         ResourceTool.Key halfFrenchHelloWorld = helloWorld.locale(Locale.FRENCH);
205         assertStringEquals("Bonjour World!", halfFrenchHelloWorld);
206 
207         ResourceTool.Key frenchTool = textTool.locale("fr");
208         ResourceTool.Key frenchHelloWorld =
209             frenchTool.get("hello.whoever").insert(frenchTool.get("world"));
210         assertStringEquals("Bonjour Monde!", frenchHelloWorld);
211     }
212 
213     public @Test void testComparisonDateTool() { /* TODO still incomplete */
214         ComparisonDateTool dateTool = (ComparisonDateTool)toolbox.get("date");
215         assertNotNull(dateTool);
216         Calendar date1 = new GregorianCalendar(2007,0,2);
217         Calendar date2 = new GregorianCalendar(2007,1,15);
218         /* test comparing */
219         ComparisonDateTool.Comparison whenIs = dateTool.whenIs(date1, date2);
220         assertEquals(0l, whenIs.getYears());
221         assertEquals(1l, whenIs.getMonths());
222         assertEquals(44l, whenIs.getDays());
223         // the toolbox config says to skip months, so this should be in weeks
224         assertStringEquals("6 weeks later", whenIs);
225     }
226 }