001: package org.apache.velocity.tools.test.whitebox;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.util.Locale;
023: import java.util.Map;
024: import java.util.Date;
025: import java.util.GregorianCalendar;
026:
027: import org.junit.*;
028: import static org.junit.Assert.*;
029:
030: import org.apache.velocity.tools.generic.Alternator;
031: import org.apache.velocity.tools.generic.AlternatorTool;
032: import org.apache.velocity.tools.generic.DateTool;
033: import org.apache.velocity.tools.generic.EscapeTool;
034: import org.apache.velocity.tools.generic.MathTool;
035: import org.apache.velocity.tools.generic.NumberTool;
036: import org.apache.velocity.tools.generic.ResourceTool;
037: import org.apache.velocity.tools.view.XMLToolboxManager;
038:
039: /**
040: * <p>Generic tools whitebox tests.</p>
041: *
042: * @author <a href="mailto:cbrisson@apache.org">Claude Brisson</a>
043: * @author Nathan Bubna
044: * @since Velocity Tools 1.3
045: * @version $Id$
046: */
047:
048: public class GenericToolsTests {
049:
050: private static final String TOOLBOX_PATH = "@test.conf.dir@/whiteboxtest-toolbox.xml";
051:
052: private static Map toolbox = null;
053:
054: public static @BeforeClass
055: void initGenericToolsTests() throws Exception {
056: XMLToolboxManager manager = new XMLToolboxManager();
057: manager.load(TOOLBOX_PATH);
058: toolbox = manager.getToolbox(null);
059: }
060:
061: public @Test
062: void testAlternatorTool() {
063: AlternatorTool alternatorTool = (AlternatorTool) toolbox
064: .get("alternator");
065: assertNotNull(alternatorTool);
066: /* test automatic alternator */
067: Alternator auto = alternatorTool.auto(new String[] { "red",
068: "blue", "yellow" });
069: assertEquals("red", auto.getCurrent());
070: assertEquals("red", auto.getNext());
071: assertEquals("blue", auto.toString());
072: assertEquals("yellow", auto.toString());
073: assertEquals("red", auto.toString());
074: /* test manual alternator (use 'make()' and not 'manual()' since we define the default to be manual in toolbox.xml*/
075: Alternator manual = alternatorTool.make(new String[] { "red",
076: "blue", "yellow" });
077: assertEquals("red", manual.toString());
078: assertEquals("red", manual.toString());
079: manual.shift();
080: assertEquals("blue", manual.toString());
081: manual.shift();
082: manual.shift();
083: assertEquals("red", manual.toString());
084: }
085:
086: public @Test
087: void testDateTool() { /* TODO still incomplete */
088: DateTool dateTool = (DateTool) toolbox.get("date");
089: assertNotNull(dateTool);
090: Date date = new GregorianCalendar(2007, 0, 2).getTime();
091: String disp = "2007-01-02";
092: String disp2 = "2007/01/02";
093: /* check configured format */
094: assertEquals("yyyy-MM-dd", dateTool.getFormat());
095: /* test formatting */
096: assertEquals(disp, dateTool.format(date));
097: assertEquals(disp2, dateTool.format("yyyy/MM/dd", date));
098: /* test parsing */
099: assertEquals(2007, dateTool.getYear(disp));
100: assertEquals(0, dateTool.getMonth(disp));
101: assertEquals(2, dateTool.getDay(disp));
102: }
103:
104: public @Test
105: void testEscapeTool() {
106: EscapeTool escapeTool = (EscapeTool) toolbox.get("esc");
107: assertNotNull(escapeTool);
108: /* java */
109: assertEquals("\\uFFFF\\b\\n\\t\\f\\r\\\"\\\\", escapeTool
110: .java("\uFFFF\b\n\t\f\r\"\\"));
111: /* javascript */
112: assertEquals("\\uFFFF\\b\\n\\t\\f\\r\\\"\\'\\\\", escapeTool
113: .javascript("\uFFFF\b\n\t\f\r\"'\\"));
114: /* html */
115: assertEquals(""&<> ", escapeTool
116: .html("\"&<>" + (char) 160));
117: /* url */
118: assertEquals("%40%2F%3F%3D+%26", escapeTool.url("@/?= &"));
119: /* sql */
120: assertEquals("''", escapeTool.sql("'"));
121: /* xml */
122: assertEquals(""&<>", escapeTool.html("\"&<>"));
123: }
124:
125: public @Test
126: void testMathTool() {
127: MathTool mathTool = (MathTool) toolbox.get("math");
128: assertNotNull(mathTool);
129: assertEquals(1, mathTool.abs(-1));
130: assertEquals(2, mathTool.add(1, 1));
131: assertEquals(3, mathTool.ceil(2.5));
132: assertEquals(4, mathTool.div(8, 2));
133: assertEquals(5, mathTool.floor(5.1));
134: assertEquals(6, mathTool.getAverage(new long[] { 5, 6, 7 }));
135: /* getTotal() watches the type of its first argument, so assertEquals needs a long */
136: assertEquals((long) 7, mathTool
137: .getTotal(new long[] { 2, 2, 3 }));
138: assertEquals(8, mathTool.idiv(130, 16));
139: assertEquals(9, mathTool.max(9, -10));
140: assertEquals(10, mathTool.min(10, 20));
141: assertEquals(11, mathTool.mod(37, 13));
142: assertEquals(12, mathTool.mul(3, 4));
143: assertEquals(13, mathTool.round(12.8));
144: assertEquals(14.2, mathTool.roundTo(1, 14.18));
145: assertEquals(15, mathTool.sub(30, 15));
146: assertEquals(16, mathTool.pow(4, 2));
147: assertEquals(17, mathTool.toInteger("17"));
148: assertEquals(18.1, mathTool.toDouble("18.1"));
149: }
150:
151: public @Test
152: void testNumberTool() {
153: NumberTool numberTool = (NumberTool) toolbox.get("number");
154: assertNotNull(numberTool);
155: // assertEquals()
156: }
157:
158: public @Test
159: void testResourceTool() {
160: ResourceTool textTool = (ResourceTool) toolbox.get("text");
161: assertNotNull(textTool);
162:
163: ResourceTool.Key foo = textTool.get("foo");
164: assertEquals("bar", foo.toString());
165:
166: ResourceTool.Key frenchFoo = foo.locale(Locale.FRENCH);
167: assertEquals("barre", frenchFoo.toString());
168:
169: ResourceTool.Key otherFoo = foo.bundle("resources2");
170: assertEquals("woogie", otherFoo.toString());
171:
172: ResourceTool.Key helloWhoever = textTool.get("hello").get(
173: "whoever");
174: assertEquals("Hello {0}!", helloWhoever.toString());
175:
176: ResourceTool.Key helloWorld = helloWhoever.insert(textTool
177: .get("world"));
178: assertEquals("Hello World!", helloWorld.toString());
179:
180: ResourceTool.Key halfFrenchHelloWorld = helloWorld
181: .locale(Locale.FRENCH);
182: assertEquals("Bonjour World!", halfFrenchHelloWorld.toString());
183:
184: ResourceTool.Key frenchTool = textTool.locale(Locale.FRENCH);
185: ResourceTool.Key frenchHelloWorld = frenchTool.get(
186: "hello.whoever").insert(frenchTool.get("world"));
187: assertEquals("Bonjour Monde!", frenchHelloWorld.toString());
188: }
189: }
|