001: /*
002: * $Id: URLTagTest.java 565422 2007-08-13 17:09:16Z jholmes $
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: package org.apache.struts2.views.jsp;
022:
023: import java.util.HashMap;
024: import java.util.Map;
025:
026: import javax.servlet.jsp.JspWriter;
027:
028: import org.apache.struts2.components.URL;
029:
030: /**
031: * Unit test for {@link URLTag}.
032: *
033: */
034: public class URLTagTest extends AbstractUITagTest {
035:
036: private URLTag tag;
037:
038: /**
039: * To test priority of parameter passed in to url component though
040: * various way
041: * - current request url
042: * - tag's value attribute
043: * - tag's nested param tag
044: *
045: * id1
046: * ===
047: * - found in current request url
048: * - found in tag's value attribute
049: * - found in tag's param tag
050: * CONCLUSION: tag's param tag takes precedence (paramId1)
051: *
052: * id2
053: * ===
054: * - found in current request url
055: * - found in tag's value attribute
056: * CONCLUSION: tag's value attribute take precedence (tagId2)
057: *
058: * urlParam1
059: * =========
060: * - found in current request url
061: * CONCLUSION: param in current request url will be used (urlValue1)
062: *
063: * urlParam2
064: * =========
065: * - found in current request url
066: * CONCLUSION: param in current request url will be used. (urlValue2)
067: *
068: * tagId
069: * =====
070: * - found in tag's value attribute
071: * CONCLUSION: param in tag's value attribute wil; be used. (tagValue)
072: *
073: * param1
074: * ======
075: * - found in nested param tag
076: * CONCLUSION: param in nested param tag will be used. (param1value)
077: *
078: * param2
079: * ======
080: * - found in nested param tag
081: * CONCLUSION: param in nested param tag will be used. (param2value)
082: */
083: public void testParametersPriority() throws Exception {
084: request
085: .setQueryString("id1=urlId1&id2=urlId2&urlParam1=urlValue1&urlParam2=urlValue2");
086:
087: tag
088: .setValue("testAction.action?id1=tagId1&id2=tagId2&tagId=tagValue");
089:
090: ParamTag param1 = new ParamTag();
091: param1.setPageContext(pageContext);
092: param1.setName("param1");
093: param1.setValue("%{'param1value'}");
094:
095: ParamTag param2 = new ParamTag();
096: param2.setPageContext(pageContext);
097: param2.setName("param2");
098: param2.setValue("%{'param2value'}");
099:
100: ParamTag param3 = new ParamTag();
101: param3.setPageContext(pageContext);
102: param3.setName("id1");
103: param3.setValue("%{'paramId1'}");
104:
105: tag.doStartTag();
106: param1.doStartTag();
107: param1.doEndTag();
108: param2.doStartTag();
109: param2.doEndTag();
110: param3.doStartTag();
111: param3.doEndTag();
112:
113: URL url = (URL) tag.getComponent();
114: Map parameters = url.getParameters();
115:
116: assertNotNull(parameters);
117: assertEquals(parameters.size(), 7);
118: assertEquals(parameters.get("id1"), "paramId1");
119: assertEquals(parameters.get("id2"), "tagId2");
120: assertEquals(parameters.get("urlParam1"), "urlValue1");
121: assertEquals(parameters.get("urlParam2"), "urlValue2");
122: assertEquals(parameters.get("tagId"), "tagValue");
123: assertEquals(parameters.get("param1"), "param1value");
124: assertEquals(parameters.get("param2"), "param2value");
125: }
126:
127: /**
128: * To test priority of parameter passed in to url component though
129: * various way, with includeParams="NONE"
130: * - current request url
131: * - tag's value attribute
132: * - tag's nested param tag
133: *
134: * In this case only parameters from the tag itself is taken into account.
135: * Those from request will not count, only those in tag's value attribute
136: * and nested param tag.
137: *
138: * @throws Exception
139: */
140: public void testParametersPriorityWithIncludeParamsAsNONE()
141: throws Exception {
142: request
143: .setQueryString("id1=urlId1&id2=urlId2&urlParam1=urlValue1&urlParam2=urlValue2");
144:
145: tag
146: .setValue("testAction.action?id1=tagId1&id2=tagId2&tagId=tagValue");
147: tag.setIncludeParams("NONE");
148:
149: ParamTag param1 = new ParamTag();
150: param1.setPageContext(pageContext);
151: param1.setName("param1");
152: param1.setValue("%{'param1value'}");
153:
154: ParamTag param2 = new ParamTag();
155: param2.setPageContext(pageContext);
156: param2.setName("param2");
157: param2.setValue("%{'param2value'}");
158:
159: ParamTag param3 = new ParamTag();
160: param3.setPageContext(pageContext);
161: param3.setName("id1");
162: param3.setValue("%{'paramId1'}");
163:
164: tag.doStartTag();
165: param1.doStartTag();
166: param1.doEndTag();
167: param2.doStartTag();
168: param2.doEndTag();
169: param3.doStartTag();
170: param3.doEndTag();
171:
172: URL url = (URL) tag.getComponent();
173: Map parameters = url.getParameters();
174:
175: assertEquals(parameters.size(), 5);
176: assertEquals(parameters.get("id1"), "paramId1");
177: assertEquals(parameters.get("id2"), "tagId2");
178: assertEquals(parameters.get("tagId"), "tagValue");
179: assertEquals(parameters.get("param1"), "param1value");
180: assertEquals(parameters.get("param2"), "param2value");
181: }
182:
183: public void testIncludeParamsDefaultToGET() throws Exception {
184: request.setQueryString("one=oneVal&two=twoVal&three=threeVal");
185:
186: // request parameter map should not have any effect, as includeParams
187: // default to GET, which get its param from request.getQueryString()
188: Map tmp = new HashMap();
189: tmp.put("one", "aaa");
190: tmp.put("two", "bbb");
191: tmp.put("three", "ccc");
192: request.setParameterMap(tmp);
193:
194: tag.setValue("TestAction.acton");
195:
196: tag.doStartTag();
197:
198: URL url = (URL) tag.getComponent();
199: Map parameters = url.getParameters();
200:
201: tag.doEndTag();
202:
203: assertEquals(parameters.get("one"), "oneVal");
204: assertEquals(parameters.get("two"), "twoVal");
205: assertEquals(parameters.get("three"), "threeVal");
206: }
207:
208: public void testActionURL() throws Exception {
209: tag.setValue("TestAction.action");
210:
211: tag.doStartTag();
212: tag.doEndTag();
213: assertEquals("TestAction.action", writer.toString());
214: }
215:
216: public void testAddParameters() throws Exception {
217: request.setAttribute("struts.request_uri", "/Test.action");
218:
219: request
220: .setAttribute("struts.request_uri",
221: "/TestAction.action");
222: request.setQueryString("param0=value0");
223:
224: tag.doStartTag();
225: tag.component.addParameter("param1", "value1");
226: tag.component.addParameter("param2", "value2");
227: tag.doEndTag();
228: assertEquals(
229: "/TestAction.action?param0=value0&param1=value1&param2=value2",
230: writer.toString());
231: }
232:
233: public void testEvaluateValue() throws Exception {
234: Foo foo = new Foo();
235: foo.setTitle("test");
236: stack.push(foo);
237: tag.setValue("%{title}");
238:
239: tag.doStartTag();
240: tag.doEndTag();
241: assertEquals("test", writer.toString());
242: }
243:
244: public void testHttps() throws Exception {
245: request.setScheme("https");
246: request.setServerName("localhost");
247: request.setServerPort(443);
248:
249: tag.setValue("list-members.action");
250:
251: tag.doStartTag();
252: tag.doEndTag();
253: assertEquals("list-members.action", writer.toString());
254: }
255:
256: public void testAnchor() throws Exception {
257: request.setScheme("https");
258: request.setServerName("localhost");
259: request.setServerPort(443);
260:
261: tag.setValue("list-members.action");
262: tag.setAnchor("test");
263:
264: tag.doStartTag();
265: tag.doEndTag();
266: assertEquals("list-members.action#test", writer.toString());
267: }
268:
269: public void testParamPrecedence() throws Exception {
270: request.setRequestURI("/context/someAction.action");
271: request.setQueryString("id=22&name=John");
272:
273: URLTag urlTag = new URLTag();
274: urlTag.setPageContext(pageContext);
275: urlTag.setIncludeParams("get");
276: urlTag.setEncode("%{false}");
277:
278: ParamTag paramTag = new ParamTag();
279: paramTag.setPageContext(pageContext);
280: paramTag.setName("id");
281: paramTag.setValue("%{'33'}");
282:
283: urlTag.doStartTag();
284: paramTag.doStartTag();
285: paramTag.doEndTag();
286: urlTag.doEndTag();
287:
288: assertEquals("/context/someAction.action?id=33&name=John",
289: writer.getBuffer().toString());
290: }
291:
292: public void testParamPrecedenceWithAnchor() throws Exception {
293: request.setRequestURI("/context/someAction.action");
294: request.setQueryString("id=22&name=John");
295:
296: URLTag urlTag = new URLTag();
297: urlTag.setPageContext(pageContext);
298: urlTag.setIncludeParams("get");
299: urlTag.setEncode("%{false}");
300: urlTag.setAnchor("testAnchor");
301:
302: ParamTag paramTag = new ParamTag();
303: paramTag.setPageContext(pageContext);
304: paramTag.setName("id");
305: paramTag.setValue("%{'33'}");
306:
307: urlTag.doStartTag();
308: paramTag.doStartTag();
309: paramTag.doEndTag();
310: urlTag.doEndTag();
311:
312: assertEquals(
313: "/context/someAction.action?id=33&name=John#testAnchor",
314: writer.getBuffer().toString());
315: }
316:
317: public void testPutId() throws Exception {
318: tag.setValue("/public/about");
319: assertEquals(null, stack.findString("myId")); // nothing in stack
320: tag.setId("myId");
321: tag.doStartTag();
322: tag.doEndTag();
323: assertEquals("", writer.toString());
324: assertEquals("/public/about", stack.findString("myId")); // is in stack now
325: }
326:
327: public void testUsingValueOnly() throws Exception {
328: tag.setValue("/public/about/team.jsp");
329: tag.doStartTag();
330: tag.doEndTag();
331: assertEquals("/public/about/team.jsp", writer.toString());
332: }
333:
334: public void testRequestURIActionIncludeNone() throws Exception {
335: request.setRequestURI("/public/about");
336: request.setQueryString("section=team&company=acme inc");
337:
338: tag.setAction("team");
339: tag.setIncludeParams("none");
340: tag.doStartTag();
341: tag.doEndTag();
342:
343: assertEquals("/team.action", writer.toString());
344: }
345:
346: public void testRequestURIActionIncludeGet() throws Exception {
347: request.setRequestURI("/public/about");
348: request.setQueryString("section=team&company=acme inc");
349:
350: tag.setAction("team");
351: tag.setIncludeParams("get");
352: tag.doStartTag();
353: tag.doEndTag();
354:
355: assertEquals("/team.action?section=team&company=acme+inc",
356: writer.toString());
357: }
358:
359: public void testRequestURIActionIncludeGetDoNotEscapeAmp()
360: throws Exception {
361: request.setRequestURI("/public/about");
362: request.setQueryString("section=team&company=acme inc");
363:
364: tag.setAction("team");
365: tag.setIncludeParams("get");
366: tag.setEscapeAmp("false");
367: tag.doStartTag();
368: tag.doEndTag();
369:
370: assertEquals("/team.action?section=team&company=acme+inc",
371: writer.toString());
372: }
373:
374: public void testRequestURINoActionIncludeNone() throws Exception {
375: request.setRequestURI("/public/about");
376: request.setQueryString("section=team&company=acme inc");
377:
378: tag.setAction(null);
379: tag.setIncludeParams("none");
380: tag.doStartTag();
381: tag.doEndTag();
382:
383: assertEquals("/public/about", writer.toString());
384: }
385:
386: public void testNoActionIncludeGet() throws Exception {
387: request.setRequestURI("/public/about");
388: request.setQueryString("section=team&company=acme inc");
389:
390: tag.setAction(null);
391: tag.setIncludeParams("get");
392: tag.doStartTag();
393: tag.doEndTag();
394:
395: assertEquals("/public/about?section=team&company=acme+inc",
396: writer.toString());
397: }
398:
399: public void testRequestURIActionIncludeAll() throws Exception {
400: request.setRequestURI("/public/about");
401: request.setQueryString("section=team&company=acme inc");
402:
403: tag.setAction("team");
404: tag.setIncludeParams("all");
405:
406: tag.doStartTag();
407:
408: // include nested param tag
409: ParamTag paramTag = new ParamTag();
410: paramTag.setPageContext(pageContext);
411: paramTag.setName("year");
412: paramTag.setValue("2006");
413: paramTag.doStartTag();
414: paramTag.doEndTag();
415:
416: tag.doEndTag();
417:
418: assertEquals(
419: "/team.action?section=team&company=acme+inc&year=2006",
420: writer.toString());
421: }
422:
423: public void testRequestURINoActionIncludeAll() throws Exception {
424: request.setRequestURI("/public/about");
425: request.setQueryString("section=team&company=acme inc");
426:
427: tag.setAction(null);
428: tag.setIncludeParams("all");
429:
430: tag.doStartTag();
431:
432: // include nested param tag
433: ParamTag paramTag = new ParamTag();
434: paramTag.setPageContext(pageContext);
435: paramTag.setName("year");
436: paramTag.setValue("2006");
437: paramTag.doStartTag();
438: paramTag.doEndTag();
439:
440: tag.doEndTag();
441:
442: assertEquals(
443: "/public/about?section=team&company=acme+inc&year=2006",
444: writer.toString());
445: }
446:
447: public void testUnknownIncludeParam() throws Exception {
448: request.setRequestURI("/public/about");
449: request.setQueryString("section=team");
450:
451: tag.setIncludeParams("unknown"); // will log at WARN level
452: tag.doStartTag();
453: tag.doEndTag();
454: assertEquals("/public/about", writer.toString()); // should not add any request parameters
455: }
456:
457: public void testRequestURIWithAnchor() throws Exception {
458: request.setRequestURI("/public/about");
459: request.setQueryString("company=acme inc#canada");
460:
461: tag.setAction("company");
462: tag.setIncludeParams("get");
463: tag.doStartTag();
464: tag.doEndTag();
465:
466: assertEquals("/company.action?company=acme+inc", writer
467: .toString()); // will always chop anchor if using requestURI
468: }
469:
470: public void testIncludeContext() throws Exception {
471: request.setupGetContext("/myapp");
472:
473: tag.setIncludeContext("true");
474: tag.setAction("company");
475: tag.doStartTag();
476: tag.doEndTag();
477:
478: assertEquals("/myapp/company.action", writer.toString());
479: }
480:
481: public void testForceAddSchemeHostAndPort() throws Exception {
482: tag.setForceAddSchemeHostAndPort("true");
483: tag.setAction("company");
484: tag.doStartTag();
485: tag.doEndTag();
486:
487: assertEquals("http://localhost/company.action", writer
488: .toString());
489: }
490:
491: protected void setUp() throws Exception {
492: super .setUp();
493:
494: request.setScheme("http");
495: request.setServerName("localhost");
496: request.setServerPort(80);
497:
498: tag = new URLTag();
499: tag.setPageContext(pageContext);
500: JspWriter jspWriter = new StrutsMockJspWriter(writer);
501: pageContext.setJspWriter(jspWriter);
502: }
503:
504: public class Foo {
505: private String title;
506:
507: public void setTitle(String title) {
508: this .title = title;
509: }
510:
511: public String getTitle() {
512: return title;
513: }
514:
515: public String toString() {
516: return "Foo is: " + title;
517: }
518: }
519: }
|