1 package org.apache.velocity.test;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.StringWriter;
23 import java.math.BigDecimal;
24 import java.math.BigInteger;
25
26 import junit.framework.Test;
27 import junit.framework.TestCase;
28 import junit.framework.TestSuite;
29
30 import org.apache.velocity.VelocityContext;
31 import org.apache.velocity.app.VelocityEngine;
32 import org.apache.velocity.context.Context;
33 import org.apache.velocity.runtime.RuntimeServices;
34 import org.apache.velocity.test.provider.NumberMethods;
35
36
37
38
39
40
41
42
43 public class NumberMethodCallsTestCase extends TestCase
44 {
45 private VelocityEngine ve = null;
46
47 private final static boolean PRINT_RESULTS = false;
48
49
50
51
52 public NumberMethodCallsTestCase(String name)
53 {
54 super(name);
55 }
56
57 public void setUp()
58 throws Exception
59 {
60 ve = new VelocityEngine();
61 ve.init();
62 }
63
64 public void init( RuntimeServices rs )
65 {
66
67 }
68
69 public static Test suite ()
70 {
71 return new TestSuite(NumberMethodCallsTestCase.class);
72 }
73
74
75
76
77 public void testNumberMethodCalls ()
78 throws Exception
79 {
80 VelocityContext vc = new VelocityContext();
81
82
83 vc.put("Test",new NumberMethods());
84
85
86 vc.put("AByte",new Byte("10"));
87 vc.put("AShort",new Short("10"));
88 vc.put("AInteger",new Integer(10));
89 vc.put("ALong",new Long(10));
90 vc.put("ADouble",new Double(10));
91 vc.put("AFloat",new Float(10));
92 vc.put("ABigDecimal",new BigDecimal(10));
93 vc.put("ABigInteger",new BigInteger("10"));
94
95
96 System.out.println("Testing: method calls with arguments as context objects");
97 checkResults(vc,"$Test.numMethod($AByte)","byte (10)");
98 checkResults(vc,"$Test.numMethod($AShort)","short (10)");
99 checkResults(vc,"$Test.numMethod($AInteger)","int (10)");
100 checkResults(vc,"$Test.numMethod($ADouble)","double (10.0)");
101 checkResults(vc,"$Test.numMethod($AFloat)","double (10.0)");
102 checkResults(vc,"$Test.numMethod($ALong)","long (10)");
103 checkResults(vc,"$Test.numMethod($ABigDecimal)","BigDecimal (10)");
104 checkResults(vc,"$Test.numMethod($ABigInteger)","BigInteger (10)");
105
106
107
108
109 System.out.println("Testing: method calls with arguments as literals");
110 checkResults(vc,"$Test.numMethod(10.0)","double (10.0)");
111 checkResults(vc,"$Test.numMethod(10)","int (10)");
112 checkResults(vc,"$Test.numMethod(10000000000)","long (10000000000)");
113 checkResults(vc,"$Test.numMethod(10000000000000000000000)","BigInteger (10000000000000000000000)");
114
115
116
117
118
119 System.out.println("Testing: method calls with arguments as calculated values");
120 checkResults(vc,"#set($val = 10.0 + 1.5)$Test.numMethod($val)","double (11.5)");
121 checkResults(vc,"#set($val = 100 + 1)$Test.numMethod($val)","int (101)");
122 checkResults(vc,"#set($val = 100 * 1000)$Test.numMethod($val)","int (100000)");
123 checkResults(vc,"#set($val = 100 + 1.5)$Test.numMethod($val)","double (101.5)");
124 checkResults(vc,"#set($val = $ALong + $AInteger)$Test.numMethod($val)","long (20)");
125 checkResults(vc,"#set($val = $ABigInteger + $AInteger)$Test.numMethod($val)","BigInteger (20)");
126 }
127
128
129 private void checkResults(Context vc, String template, String compare) throws Exception
130 {
131
132 StringWriter writer = new StringWriter();
133 ve.evaluate( vc, writer, "test", template);
134 assertEquals("Incorrect results for template '" + template + "'.",compare,writer.toString());
135
136 if (PRINT_RESULTS)
137 System.out.println ("Method call successful: " + template);
138
139 }
140
141
142 }
143
144