View Javadoc

1   package org.apache.velocity.tools.test.blackbox;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * <p>View tools blackbox tests.</p>
42   *
43   * @author <a href="mailto:cbrisson@apache.org">Claude Brisson</a>
44   * @since Velocity Tools 1.3
45   * @version $Id$
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      /******* Helpers **********/
57  
58      /**
59       * Utility function to check the text content of an HTML element
60       * @param resp web response
61       * @param id HTML element id
62       * @param text expected text
63       * @throws Exception
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       * Utility function to check the text content of an HTML element
73       * @param resp web response
74       * @param id HTML element id
75       * @param text expected start of the text
76       * @throws Exception
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       * Utility function to check the text content of an HTML element
86       * @param resp web response
87       * @param id HTML element id
88       * @param start expected start of the text
89       * @param end expected end of the text
90       * @throws Exception
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      * Utility function to check the text content of an HTML element
101      * @param resp web response
102      * @param id HTML element id
103      * @param text expected contained text
104      * @throws Exception
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      * Utility function to check the text content of an HTML element
114      * @param resp web response
115      * @param id HTML element id
116      * @param regex expected regex
117      * @throws Exception
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         // strip new lines from string to be tested
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      * @param orig original web response
135      * @param formname form name
136      * @param paramname parameter name
137      * @param value parameter value
138      * @return new web response
139      * @throws Exception
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      * Used for debugging testcases
149      * @param resp webresponse
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     /******* Tests **********/
164 
165     public @Test void testBrowserSnifferTool() throws Exception {
166         /* check we are identified as a Java (HttpUnit) client */
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         /* check language */
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         /* check that getThis() is a ViewToolContext instance */
187         checkTextStart(resp,"getThis()","org.apache.velocity.tools.view.ViewToolContext");
188 
189         /* check contains('context') */
190         resp = submitWithParam(resp,"contains_Object","contains_Object1","'context'");
191         checkText(resp,"contains(java.lang.Object)","true");
192 
193         /* check get('context') */
194         resp = submitWithParam(resp,"get_Object","get_Object1","'context'");
195         checkTextStart(resp,"get(java.lang.Object)","org.apache.velocity.tools.view.ViewContextTool");
196 
197         /* check keys (the only expected uppercase is in 'velocityCount') */
198         checkTextRegex(resp,"getKeys()","^\\[[a-z_A-Z]+(?:,\\s*[a-z_A-Z]+)*\\]$");
199 
200         /* check toolbox */
201         checkTextRegex(resp,"getToolbox()","^\\{[a-z_A-Z]+=.*(?:,\\s*[a-z_A-Z]+=.*)*\\}$");
202 
203         /* check values */
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         /* check anchor(foo) and anchor */
214         resp = submitWithParam(resp,"anchor","anchor","foo");
215         checkText(resp,"anchor",page+"#foo");
216         checkText(resp,"altanchor",page+"#foo");
217 
218         /* check path(bar) and path */
219         resp = submitWithParam(resp,"path","path","bar");
220         checkText(resp,"path","http://localhost:8081/bar");
221         checkText(resp,"altpath","/link.vm");
222 
223         /* check relative(foo) */
224         resp = submitWithParam(resp,"relative","relative","foo");
225         checkText(resp,"relative","/foo");
226 
227         /* check absolute(bar) */
228         resp = submitWithParam(resp,"absolute","absolute","bar");
229         checkText(resp,"absolute",ROOT_URL + "bar");
230 
231         /* check contextURL */
232         checkText(resp,"contextURL",ROOT_URL);
233 
234         /* check contextPath */
235         checkText(resp,"contextPath","");
236 
237         /* check requestPath */
238         checkText(resp,"requestPath","/link.vm");
239 
240         /* check baseRef */
241         checkText(resp,"baseRef",page);
242 
243         /* check self */
244         checkText(resp,"self",page);
245 
246         /* check encode */
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         /* check exists(foo) */
257         resp = submitWithParam(resp,"exists","exists","foo");
258         checkText(resp,"exists","true");
259 
260         /* check get(foo) */
261         resp = submitWithParam(resp,"get","get","foo");
262         checkText(resp,"get","bar");
263 
264         /* check getString(foo) */
265         resp = submitWithParam(resp,"getString","getString","foo");
266         checkText(resp,"getString","bar");
267 
268         /* check getBoolean(b) */
269         resp = submitWithParam(resp,"getBoolean","getBoolean","b");
270         checkText(resp,"getBoolean","false");
271 
272         /* check getNumber(n) */
273         resp = submitWithParam(resp,"getNumber","getNumber","n");
274         checkText(resp,"getNumber","55");
275 
276         /* check getDouble(d) */
277         resp = submitWithParam(resp,"getDouble","getDouble","d");
278         checkText(resp,"getDouble","1.2");
279 
280         /* check getInteger(n) */
281         resp = submitWithParam(resp,"getInteger","getInteger","n");
282         checkText(resp,"getInteger","55");
283 
284         /* check getStrings(foo) */
285         resp = submitWithParam(resp,"getStrings","getStrings","foo");
286         checkTextStart(resp,"getStrings","[Ljava.lang.String;@");
287 
288         /* check getBooleans(b) */
289         resp = submitWithParam(resp,"getBooleans","getBooleans","b");
290         checkTextStart(resp,"getBooleans","[Ljava.lang.Boolean;@");
291 
292         /* check getNumbers(n) */
293         resp = submitWithParam(resp,"getNumbers","getNumbers","n");
294         checkTextStart(resp,"getNumbers","[Ljava.lang.Number;@");
295 
296         /* check getDoubles(d) */
297         resp = submitWithParam(resp,"getDoubles","getDoubles","d");
298         checkTextStart(resp,"getDoubles","[D@");
299 
300         /* check getInts(n) */
301         resp = submitWithParam(resp,"getInts","getInts","n");
302         checkTextStart(resp,"getInts","[I@");
303 
304         /* check getString(bar,foo) */
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         /* TODO other getters with default values */
312 
313         /* check all */
314         checkTextRegex(resp,"all","^\\{.*\\}$");
315     }
316 }