001: package org.apache.velocity.tools.test.blackbox;
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.regex.Pattern;
023: import java.util.regex.Matcher;
024: import java.io.PrintWriter;
025: import java.io.IOException;
026:
027: import org.junit.*;
028:
029: import static org.junit.Assert.*;
030:
031: import com.meterware.httpunit.HTMLElement;
032: import com.meterware.httpunit.WebResponse;
033: import com.meterware.httpunit.WebConversation;
034: import com.meterware.httpunit.WebRequest;
035: import com.meterware.httpunit.GetMethodWebRequest;
036: import com.meterware.httpunit.WebForm;
037: import com.meterware.httpunit.HttpUnitOptions;
038:
039: /**
040: * <p>View tools blackbox tests.</p>
041: *
042: * @author <a href="mailto:cbrisson@apache.org">Claude Brisson</a>
043: * @since Velocity Tools 1.3
044: * @version $Id$
045: */
046:
047: public class ViewToolsTests {
048:
049: private static final String ROOT_URL = "http://localhost:@test.webcontainer.port@/";
050:
051: public static @BeforeClass
052: void initViewToolsTests() throws Exception {
053: }
054:
055: /******* Helpers **********/
056:
057: /**
058: * Utility function to check the text content of an HTML element
059: * @param resp web response
060: * @param id HTML element id
061: * @param text expected text
062: * @throws Exception
063: */
064: private void checkText(WebResponse resp, String id, String text)
065: throws Exception {
066: HTMLElement element = resp.getElementWithID(id);
067: assertNotNull(element);
068: assertEquals(text, element.getText());
069: }
070:
071: /**
072: * Utility function to check the text content of an HTML element
073: * @param resp web response
074: * @param id HTML element id
075: * @param text expected start of the text
076: * @throws Exception
077: */
078: private void checkTextStart(WebResponse resp, String id, String text)
079: throws Exception {
080: HTMLElement element = resp.getElementWithID(id);
081: assertNotNull(element);
082: assertTrue(element.getText().startsWith(text));
083: }
084:
085: /**
086: * Utility function to check the text content of an HTML element
087: * @param resp web response
088: * @param id HTML element id
089: * @param text expected contained text
090: * @throws Exception
091: */
092: private void checkTextContent(WebResponse resp, String id,
093: String text) throws Exception {
094: HTMLElement element = resp.getElementWithID(id);
095: assertNotNull(element);
096: assertTrue(element.getText().indexOf(text) != -1);
097: }
098:
099: /**
100: * Utility function to check the text content of an HTML element
101: * @param resp web response
102: * @param id HTML element id
103: * @param regex expected regex
104: * @throws Exception
105: */
106: private void checkTextRegex(WebResponse resp, String id,
107: String regex) throws Exception {
108: HTMLElement element = resp.getElementWithID(id);
109: assertNotNull(element);
110: Pattern pattern = Pattern.compile(regex);
111: Matcher matcher = pattern.matcher(element.getText());
112: assertTrue(matcher.matches());
113: }
114:
115: /**
116: *
117: * @param orig original web response
118: * @param formname form name
119: * @param paramname parameter name
120: * @param value parameter value
121: * @return new web response
122: * @throws Exception
123: */
124: private WebResponse submitWithParam(WebResponse orig,
125: String formname, String paramname, String value)
126: throws Exception {
127: WebForm form = orig.getFormWithName(formname);
128: form.setParameter(paramname, value);
129: return form.submit();
130: }
131:
132: /**
133: * Used for debugging testcases
134: * @param resp webresponse
135: */
136: private void dump(WebResponse resp) {
137: try {
138: PrintWriter pw = new PrintWriter("/tmp/dump.html");
139: pw.println(resp.getText());
140: pw.flush();
141: pw.close();
142: } catch (IOException ioe) {
143:
144: }
145: }
146:
147: /******* Tests **********/
148:
149: public @Test
150: void testBrowserSnifferTool() throws Exception {
151: WebConversation conv = new WebConversation();
152: WebRequest req = new GetMethodWebRequest(ROOT_URL
153: + "browser.vm");
154: WebResponse resp = conv.getResponse(req);
155:
156: /* check we are identified as a Java (HttpUnit) client */
157: checkText(resp, "Java", "true");
158: }
159:
160: public @Test
161: void testContextTool() throws Exception {
162: WebConversation conv = new WebConversation();
163: WebRequest req = new GetMethodWebRequest(ROOT_URL
164: + "context.vm");
165: WebResponse resp = conv.getResponse(req);
166:
167: /* check that getThis() is a ChainedContext instance */
168: checkTextStart(resp, "this",
169: "org.apache.velocity.tools.view.context.ChainedContext");
170:
171: /* check contains('context') */
172: resp = submitWithParam(resp, "contains", "contains", "context");
173: checkText(resp, "contains", "true");
174:
175: /* check get('context') */
176: resp = submitWithParam(resp, "get", "get", "context");
177: checkTextStart(resp, "get",
178: "org.apache.velocity.tools.view.tools.ContextTool");
179:
180: /* check keys (the only expected uppercase is in 'velocityCount') */
181: checkTextRegex(resp, "keys",
182: "^\\[[a-z_C]+(?:,\\s*[a-z_C]+)*\\]$");
183:
184: /* check toolbox */
185: checkTextRegex(resp, "toolbox",
186: "^\\{[a-z_C]+=.*(?:,\\s*[a-z_C]+=.*)*\\}$");
187:
188: /* check values */
189: checkTextRegex(resp, "values", "^\\[.*\\]$");
190: }
191:
192: public @Test
193: void testCookiesTool() throws Exception {
194: WebConversation conv = new WebConversation();
195: WebRequest req = new GetMethodWebRequest(ROOT_URL
196: + "cookies.vm");
197: WebResponse resp = conv.getResponse(req);
198:
199: /* check all */
200: checkTextStart(resp, "all", "[Ljavax.servlet.http.Cookie;");
201:
202: /* check get('JSESSIONID') */
203: resp = submitWithParam(resp, "get", "get", "JSESSIONID");
204: checkTextStart(resp, "get", "javax.servlet.http.Cookie");
205:
206: /* check add('foo','bar') */
207: WebForm form = resp.getFormWithName("add2");
208: form.setParameter("add1", "foo");
209: form.setParameter("add2", "bar");
210: resp = form.submit();
211: resp = submitWithParam(resp, "get", "get", "foo");
212: checkTextStart(resp, "get", "javax.servlet.http.Cookie");
213: }
214:
215: public @Test
216: void testLinkTool() throws Exception {
217: WebConversation conv = new WebConversation();
218: WebRequest req = new GetMethodWebRequest(ROOT_URL + "link.vm");
219: WebResponse resp = conv.getResponse(req);
220:
221: /* check anchor(foo) and anchor */
222: resp = submitWithParam(resp, "anchor", "anchor", "foo");
223: checkText(resp, "anchor", "#foo");
224: checkText(resp, "altanchor", "#foo");
225:
226: /* check uri(bar) and uri */
227: resp = submitWithParam(resp, "uri", "uri", "bar");
228: checkText(resp, "uri", "bar");
229: checkText(resp, "alturi", "bar");
230:
231: /* check relative(foo) */
232: resp = submitWithParam(resp, "relative", "relative", "foo");
233: checkText(resp, "relative", "/foo");
234:
235: /* check absolute(bar) */
236: resp = submitWithParam(resp, "absolute", "absolute", "bar");
237: checkText(resp, "absolute", ROOT_URL + "bar");
238:
239: /* check contextURL */
240: checkText(resp, "contextURL", ROOT_URL.substring(0, ROOT_URL
241: .length() - 1));
242:
243: /* check contextPath */
244: checkText(resp, "contextPath", "");
245:
246: /* check requestPath */
247: checkText(resp, "requestPath", "/link.vm");
248:
249: /* check baseRef */
250: checkText(resp, "baseRef", ROOT_URL + "link.vm");
251:
252: /* check self */
253: checkText(resp, "self", "/link.vm");
254:
255: /* check encodeURL */
256: resp = submitWithParam(resp, "encodeURL", "encodeURL", ": /");
257: checkText(resp, "encodeURL", "%3A+%2F");
258: }
259:
260: public @Test
261: void testParameterParserTool() throws Exception {
262: WebConversation conv = new WebConversation();
263: WebRequest req = new GetMethodWebRequest(ROOT_URL
264: + "params.vm?foo=bar&b=false&n=55&d=1.2");
265: WebResponse resp = conv.getResponse(req);
266:
267: /* check exists(foo) */
268: resp = submitWithParam(resp, "exists", "exists", "foo");
269: checkText(resp, "exists", "true");
270:
271: /* check get(foo) */
272: resp = submitWithParam(resp, "get", "get", "foo");
273: checkText(resp, "get", "bar");
274:
275: /* check getString(foo) */
276: resp = submitWithParam(resp, "getString", "getString", "foo");
277: checkText(resp, "getString", "bar");
278:
279: /* check getBoolean(b) */
280: resp = submitWithParam(resp, "getBoolean", "getBoolean", "b");
281: checkText(resp, "getBoolean", "false");
282:
283: /* check getNumber(n) */
284: resp = submitWithParam(resp, "getNumber", "getNumber", "n");
285: checkText(resp, "getNumber", "55");
286:
287: /* check getDouble(d) */
288: resp = submitWithParam(resp, "getDouble", "getDouble", "d");
289: checkText(resp, "getDouble", "1.2");
290:
291: /* check getInteger(n) */
292: resp = submitWithParam(resp, "getInteger", "getInteger", "n");
293: checkText(resp, "getInteger", "55");
294:
295: /* check getStrings(foo) */
296: resp = submitWithParam(resp, "getStrings", "getStrings", "foo");
297: checkTextStart(resp, "getStrings", "[Ljava.lang.String;@");
298:
299: /* check getBooleans(b) */
300: resp = submitWithParam(resp, "getBooleans", "getBooleans", "b");
301: checkTextStart(resp, "getBooleans", "[Ljava.lang.Boolean;@");
302:
303: /* check getNumbers(n) */
304: resp = submitWithParam(resp, "getNumbers", "getNumbers", "n");
305: checkTextStart(resp, "getNumbers", "[Ljava.lang.Number;@");
306:
307: /* check getDoubles(d) */
308: resp = submitWithParam(resp, "getDoubles", "getDoubles", "d");
309: checkTextStart(resp, "getDoubles", "[D@");
310:
311: /* check getInts(n) */
312: resp = submitWithParam(resp, "getInts", "getInts", "n");
313: checkTextStart(resp, "getInts", "[I@");
314:
315: /* check getString(bar,foo) */
316: WebForm form = resp.getFormWithName("getString2");
317: form.setParameter("getString1", "'bar'");
318: form.setParameter("getString2", "'foo'");
319: resp = form.submit();
320: checkText(resp, "getString2", "foo");
321:
322: /* TODO other getters with default values */
323:
324: /* check all */
325: checkTextRegex(resp, "all", "^\\{.*\\}$");
326: }
327: }
|