1 package org.apache.velocity.tools.test.blackbox;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.regex.Pattern;
23 import java.util.regex.Matcher;
24 import java.io.PrintWriter;
25 import java.io.IOException;
26
27 import org.junit.*;
28
29 import static org.junit.Assert.*;
30
31 import com.meterware.httpunit.HTMLElement;
32 import com.meterware.httpunit.WebResponse;
33 import com.meterware.httpunit.WebConversation;
34 import com.meterware.httpunit.WebRequest;
35 import com.meterware.httpunit.GetMethodWebRequest;
36 import com.meterware.httpunit.WebForm;
37 import com.meterware.httpunit.HttpUnitOptions;
38
39
40
41
42
43
44
45
46
47
48
49 public class ViewToolsTests {
50
51 private static final String ROOT_URL = "http://localhost:@test.webcontainer.port@/";
52
53 public static @BeforeClass void initViewToolsTests() throws Exception {
54 }
55
56
57
58
59
60
61
62
63
64
65 private void checkText(WebResponse resp,String id,String text) throws Exception {
66 HTMLElement element = resp.getElementWithID(id);
67 assertNotNull(element);
68 assertEquals(text,element.getText());
69 }
70
71
72
73
74
75
76
77
78 private void checkTextStart(WebResponse resp,String id,String text) throws Exception {
79 HTMLElement element = resp.getElementWithID(id);
80 assertNotNull(element);
81 assertTrue(element.getText().startsWith(text));
82 }
83
84
85
86
87
88
89
90
91
92 private void checkTextStartEnd(WebResponse resp,String id,String start,String end) throws Exception {
93 HTMLElement element = resp.getElementWithID(id);
94 assertNotNull(element);
95 assertTrue(element.getText().startsWith(start));
96 assertTrue(element.getText().endsWith(end));
97 }
98
99
100
101
102
103
104
105
106 private void checkTextContent(WebResponse resp,String id,String text) throws Exception {
107 HTMLElement element = resp.getElementWithID(id);
108 assertNotNull(element);
109 assertTrue(element.getText().indexOf(text) != -1);
110 }
111
112
113
114
115
116
117
118
119 private void checkTextRegex(WebResponse resp,String id,String regex) throws Exception {
120 HTMLElement element = resp.getElementWithID(id);
121 assertNotNull(element);
122 Pattern pattern = Pattern.compile(regex);
123
124 String text = element.getText().replace("\n","");
125 Matcher matcher = pattern.matcher(text);
126 if (!matcher.matches())
127 {
128 fail(element.getText()+" did not match "+regex);
129 }
130 }
131
132
133
134
135
136
137
138
139
140
141 private WebResponse submitWithParam(WebResponse orig, String formname, String paramname, String value) throws Exception {
142 WebForm form = orig.getFormWithName(formname);
143 form.setParameter(paramname,value);
144 return form.submit();
145 }
146
147
148
149
150
151 private void dump(WebResponse resp) {
152 try {
153 PrintWriter pw = new PrintWriter("/tmp/dump.html");
154 pw.println(resp.getText());
155 pw.flush();
156 pw.close();
157 } catch (IOException ioe) {
158
159 }
160 }
161
162
163
164
165 public @Test void testBrowserSnifferTool() throws Exception {
166
167 WebConversation conv = new WebConversation();
168 WebRequest req = new GetMethodWebRequest(ROOT_URL+"browser.vm");
169 WebResponse resp = conv.getResponse(req);
170 checkText(resp,"Java","true");
171
172
173 req.setHeaderField("Accept-Language","en");
174 resp = conv.getResponse(req);
175 checkText(resp,"preferredLanguage","en");
176 req.setHeaderField("Accept-Language","en-US,en;q=0.8");
177 resp = conv.getResponse(req);
178 checkText(resp,"preferredLanguage","en");
179 }
180
181 public @Test void testContextTool() throws Exception {
182 WebConversation conv = new WebConversation();
183 WebRequest req = new GetMethodWebRequest(ROOT_URL+"context.vm");
184 WebResponse resp = conv.getResponse(req);
185
186
187 checkTextStart(resp,"getThis()","org.apache.velocity.tools.view.ViewToolContext");
188
189
190 resp = submitWithParam(resp,"contains_Object","contains_Object1","'context'");
191 checkText(resp,"contains(java.lang.Object)","true");
192
193
194 resp = submitWithParam(resp,"get_Object","get_Object1","'context'");
195 checkTextStart(resp,"get(java.lang.Object)","org.apache.velocity.tools.view.ViewContextTool");
196
197
198 checkTextRegex(resp,"getKeys()","^\\[[a-z_A-Z]+(?:,\\s*[a-z_A-Z]+)*\\]$");
199
200
201 checkTextRegex(resp,"getToolbox()","^\\{[a-z_A-Z]+=.*(?:,\\s*[a-z_A-Z]+=.*)*\\}$");
202
203
204 checkTextStartEnd(resp,"getValues()","[","]");
205 }
206
207 public @Test void testLinkTool() throws Exception {
208 WebConversation conv = new WebConversation();
209 String page = ROOT_URL+"link.vm";
210 WebRequest req = new GetMethodWebRequest(page);
211 WebResponse resp = conv.getResponse(req);
212
213
214 resp = submitWithParam(resp,"anchor","anchor","foo");
215 checkText(resp,"anchor",page+"#foo");
216 checkText(resp,"altanchor",page+"#foo");
217
218
219 resp = submitWithParam(resp,"path","path","bar");
220 checkText(resp,"path","http://localhost:8081/bar");
221 checkText(resp,"altpath","/link.vm");
222
223
224 resp = submitWithParam(resp,"relative","relative","foo");
225 checkText(resp,"relative","/foo");
226
227
228 resp = submitWithParam(resp,"absolute","absolute","bar");
229 checkText(resp,"absolute",ROOT_URL + "bar");
230
231
232 checkText(resp,"contextURL",ROOT_URL);
233
234
235 checkText(resp,"contextPath","");
236
237
238 checkText(resp,"requestPath","/link.vm");
239
240
241 checkText(resp,"baseRef",page);
242
243
244 checkText(resp,"self",page);
245
246
247 resp = submitWithParam(resp,"encode","encode",": /");
248 checkText(resp,"encode","%3A+%2F");
249 }
250
251 public @Test void testParameterParserTool() throws Exception {
252 WebConversation conv = new WebConversation();
253 WebRequest req = new GetMethodWebRequest(ROOT_URL+"params.vm?foo=bar&b=false&n=55&d=1.2");
254 WebResponse resp = conv.getResponse(req);
255
256
257 resp = submitWithParam(resp,"exists","exists","foo");
258 checkText(resp,"exists","true");
259
260
261 resp = submitWithParam(resp,"get","get","foo");
262 checkText(resp,"get","bar");
263
264
265 resp = submitWithParam(resp,"getString","getString","foo");
266 checkText(resp,"getString","bar");
267
268
269 resp = submitWithParam(resp,"getBoolean","getBoolean","b");
270 checkText(resp,"getBoolean","false");
271
272
273 resp = submitWithParam(resp,"getNumber","getNumber","n");
274 checkText(resp,"getNumber","55");
275
276
277 resp = submitWithParam(resp,"getDouble","getDouble","d");
278 checkText(resp,"getDouble","1.2");
279
280
281 resp = submitWithParam(resp,"getInteger","getInteger","n");
282 checkText(resp,"getInteger","55");
283
284
285 resp = submitWithParam(resp,"getStrings","getStrings","foo");
286 checkTextStart(resp,"getStrings","[Ljava.lang.String;@");
287
288
289 resp = submitWithParam(resp,"getBooleans","getBooleans","b");
290 checkTextStart(resp,"getBooleans","[Ljava.lang.Boolean;@");
291
292
293 resp = submitWithParam(resp,"getNumbers","getNumbers","n");
294 checkTextStart(resp,"getNumbers","[Ljava.lang.Number;@");
295
296
297 resp = submitWithParam(resp,"getDoubles","getDoubles","d");
298 checkTextStart(resp,"getDoubles","[D@");
299
300
301 resp = submitWithParam(resp,"getInts","getInts","n");
302 checkTextStart(resp,"getInts","[I@");
303
304
305 WebForm form = resp.getFormWithName("getString2");
306 form.setParameter("getString1","'bar'");
307 form.setParameter("getString2","'foo'");
308 resp = form.submit();
309 checkText(resp,"getString2","foo");
310
311
312
313
314 checkTextRegex(resp,"all","^\\{.*\\}$");
315 }
316 }