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.BufferedWriter;
23 import java.io.FileOutputStream;
24 import java.io.OutputStreamWriter;
25 import java.io.Writer;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.Hashtable;
29 import java.util.Vector;
30
31 import org.apache.velocity.Template;
32 import org.apache.velocity.VelocityContext;
33 import org.apache.velocity.app.FieldMethodizer;
34 import org.apache.velocity.runtime.RuntimeSingleton;
35 import org.apache.velocity.test.provider.BoolObj;
36 import org.apache.velocity.test.provider.NullToStringObject;
37 import org.apache.velocity.test.provider.TestNumber;
38 import org.apache.velocity.test.provider.TestProvider;
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 public class TemplateTestCase extends BaseTestCase implements TemplateTestBase
67 {
68
69
70
71
72 protected String baseFileName;
73
74 private TestProvider provider;
75 private ArrayList al;
76 private Hashtable h;
77 private VelocityContext context;
78 private VelocityContext context1;
79 private VelocityContext context2;
80 private Vector vec;
81
82
83
84
85
86
87
88 public TemplateTestCase (String baseFileName)
89 {
90 super(getTestCaseName(baseFileName));
91 this.baseFileName = baseFileName;
92 }
93
94 public static junit.framework.Test suite()
95 {
96 return new TemplateTestSuite();
97 }
98
99
100
101
102 protected void setUp ()
103 {
104 provider = new TestProvider();
105 al = provider.getCustomers();
106 h = new Hashtable();
107
108 h.put("Bar", "this is from a hashtable!");
109 h.put("Foo", "this is from a hashtable too!");
110
111
112
113
114
115 vec = new Vector();
116
117 vec.addElement(new String("string1"));
118 vec.addElement(new String("string2"));
119
120
121
122
123
124
125 context2 = new VelocityContext();
126 context1 = new VelocityContext( context2 );
127 context = new VelocityContext( context1 );
128
129 context.put("provider", provider);
130 context1.put("name", "jason");
131 context1.put("name2", new StringBuffer("jason"));
132 context1.put("name3", new StringBuffer("geoge"));
133 context2.put("providers", provider.getCustomers2());
134 context.put("list", al);
135 context1.put("hashtable", h);
136 context2.put("hashmap", new HashMap());
137 context2.put("search", provider.getSearch());
138 context.put("relatedSearches", provider.getRelSearches());
139 context1.put("searchResults", provider.getRelSearches());
140 context2.put("stringarray", provider.getArray());
141 context.put("vector", vec );
142 context.put("mystring", new String());
143 context.put("runtime", new FieldMethodizer( "org.apache.velocity.runtime.RuntimeSingleton" ));
144 context.put("fmprov", new FieldMethodizer( provider ));
145 context.put("Floog", "floogie woogie");
146 context.put("boolobj", new BoolObj() );
147
148
149
150
151
152
153 Object[] oarr = { "a","b","c","d" } ;
154 int intarr[] = { 10, 20, 30, 40, 50 };
155
156 context.put( "collection", vec );
157 context2.put("iterator", vec.iterator());
158 context1.put("map", h );
159 context.put("obarr", oarr );
160 context.put("enumerator", vec.elements());
161 context.put("intarr", intarr );
162
163
164 context.put ("int1", new Integer (1000));
165 context.put ("long1", new Long (10000000000l));
166 context.put ("float1", new Float (1000.1234));
167 context.put ("double1", new Double (10000000000d));
168
169
170 context.put ("templatenumber1", new TestNumber (999.125));
171
172
173
174
175 ArrayList nullList = new ArrayList();
176 nullList.add("a");
177 nullList.add("b");
178 nullList.add(null);
179 nullList.add("d");
180 context.put("nullList", nullList);
181
182
183 context.put("nullToString",new NullToStringObject());
184 }
185
186
187
188
189 public void runTest ()
190 throws Exception
191 {
192 Template template = RuntimeSingleton.getTemplate
193 (getFileName(null, baseFileName, TMPL_FILE_EXT));
194
195 assureResultsDirectoryExists(RESULT_DIR);
196
197
198 FileOutputStream fos =
199 new FileOutputStream (getFileName(
200 RESULT_DIR, baseFileName, RESULT_FILE_EXT));
201
202 Writer writer = new BufferedWriter(new OutputStreamWriter(fos));
203
204
205 template.merge( context, writer);
206
207
208 writer.flush();
209 writer.close();
210
211 if (!isMatch(RESULT_DIR,COMPARE_DIR,baseFileName,
212 RESULT_FILE_EXT,CMP_FILE_EXT))
213 {
214 fail("Processed template did not match expected output");
215 }
216 }
217 }