001: package org.apache.velocity.test;
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.io.StringWriter;
023: import java.math.BigDecimal;
024: import java.math.BigInteger;
025:
026: import junit.framework.Test;
027: import junit.framework.TestCase;
028: import junit.framework.TestSuite;
029:
030: import org.apache.velocity.VelocityContext;
031: import org.apache.velocity.app.VelocityEngine;
032: import org.apache.velocity.context.Context;
033: import org.apache.velocity.runtime.RuntimeServices;
034: import org.apache.velocity.test.provider.NumberMethods;
035:
036: /**
037: * Used to check that method calls with number parameters are executed correctly.
038: *
039: * @author <a href="mailto:wglass@forio.com">Peter Romianowski</a>
040: * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
041: */
042: public class NumberMethodCallsTestCase extends TestCase {
043: private VelocityEngine ve = null;
044:
045: private final static boolean PRINT_RESULTS = false;
046:
047: /**
048: * Default constructor.
049: */
050: public NumberMethodCallsTestCase(String name) {
051: super (name);
052: }
053:
054: public void setUp() throws Exception {
055: ve = new VelocityEngine();
056: ve.init();
057: }
058:
059: public void init(RuntimeServices rs) {
060: // do nothing with it
061: }
062:
063: public static Test suite() {
064: return new TestSuite(NumberMethodCallsTestCase.class);
065: }
066:
067: /**
068: * Runs the test.
069: */
070: public void testNumberMethodCalls() throws Exception {
071: VelocityContext vc = new VelocityContext();
072:
073: // context object with overloaded methods with number arguments
074: vc.put("Test", new NumberMethods());
075:
076: // numbers for context
077: vc.put("AByte", new Byte("10"));
078: vc.put("AShort", new Short("10"));
079: vc.put("AInteger", new Integer(10));
080: vc.put("ALong", new Long(10));
081: vc.put("ADouble", new Double(10));
082: vc.put("AFloat", new Float(10));
083: vc.put("ABigDecimal", new BigDecimal(10));
084: vc.put("ABigInteger", new BigInteger("10"));
085:
086: // check context objects
087: System.out
088: .println("Testing: method calls with arguments as context objects");
089: checkResults(vc, "$Test.numMethod($AByte)", "byte (10)");
090: checkResults(vc, "$Test.numMethod($AShort)", "short (10)");
091: checkResults(vc, "$Test.numMethod($AInteger)", "int (10)");
092: checkResults(vc, "$Test.numMethod($ADouble)", "double (10.0)");
093: checkResults(vc, "$Test.numMethod($AFloat)", "double (10.0)");
094: checkResults(vc, "$Test.numMethod($ALong)", "long (10)");
095: checkResults(vc, "$Test.numMethod($ABigDecimal)",
096: "BigDecimal (10)");
097: checkResults(vc, "$Test.numMethod($ABigInteger)",
098: "BigInteger (10)");
099:
100: // check literals
101: // -- will cast floating point literal to smallest possible of Double, BigDecimal
102: // -- will cast integer literal to smallest possible of Integer, Long, BigInteger
103: System.out
104: .println("Testing: method calls with arguments as literals");
105: checkResults(vc, "$Test.numMethod(10.0)", "double (10.0)");
106: checkResults(vc, "$Test.numMethod(10)", "int (10)");
107: checkResults(vc, "$Test.numMethod(10000000000)",
108: "long (10000000000)");
109: checkResults(vc, "$Test.numMethod(10000000000000000000000)",
110: "BigInteger (10000000000000000000000)");
111:
112: // check calculated results
113: // -- note calculated value is cast to smallest possible type
114: // -- method invoked is smallest relevant method
115: // -- it's an unusual case here of both byte and int methods, but this works as expected
116: System.out
117: .println("Testing: method calls with arguments as calculated values");
118: checkResults(vc,
119: "#set($val = 10.0 + 1.5)$Test.numMethod($val)",
120: "double (11.5)");
121: checkResults(vc, "#set($val = 100 + 1)$Test.numMethod($val)",
122: "int (101)");
123: checkResults(vc,
124: "#set($val = 100 * 1000)$Test.numMethod($val)",
125: "int (100000)");
126: checkResults(vc, "#set($val = 100 + 1.5)$Test.numMethod($val)",
127: "double (101.5)");
128: checkResults(vc,
129: "#set($val = $ALong + $AInteger)$Test.numMethod($val)",
130: "long (20)");
131: checkResults(
132: vc,
133: "#set($val = $ABigInteger + $AInteger)$Test.numMethod($val)",
134: "BigInteger (20)");
135: }
136:
137: private void checkResults(Context vc, String template,
138: String compare) throws Exception {
139:
140: StringWriter writer = new StringWriter();
141: ve.evaluate(vc, writer, "test", template);
142: assertEquals("Incorrect results for template '" + template
143: + "'.", compare, writer.toString());
144:
145: if (PRINT_RESULTS)
146: System.out.println("Method call successful: " + template);
147:
148: }
149:
150: }
|