001: /*
002: * $Id: DefaultActionMapperTest.java 541982 2007-05-27 13:04:02Z mrdon $
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.dispatcher.mapper;
022:
023: import java.util.HashMap;
024: import java.util.Map;
025:
026: import org.apache.struts2.StrutsConstants;
027: import org.apache.struts2.StrutsTestCase;
028: import org.apache.struts2.ServletActionContext;
029: import org.apache.struts2.dispatcher.ServletRedirectResult;
030: import org.apache.struts2.views.jsp.StrutsMockHttpServletRequest;
031: import org.apache.struts2.views.jsp.StrutsMockHttpServletResponse;
032:
033: import com.mockobjects.servlet.MockHttpServletRequest;
034: import com.mockobjects.dynamic.Mock;
035: import com.opensymphony.xwork2.Result;
036: import com.opensymphony.xwork2.DefaultActionInvocation;
037: import com.opensymphony.xwork2.ActionInvocation;
038: import com.opensymphony.xwork2.ActionContext;
039: import com.opensymphony.xwork2.config.Configuration;
040: import com.opensymphony.xwork2.config.ConfigurationManager;
041: import com.opensymphony.xwork2.config.entities.PackageConfig;
042: import com.opensymphony.xwork2.config.impl.DefaultConfiguration;
043:
044: import javax.servlet.http.HttpServletResponse;
045:
046: /**
047: * DefaultActionMapper test case.
048: *
049: */
050: public class DefaultActionMapperTest extends StrutsTestCase {
051:
052: private MockHttpServletRequest req;
053: private ConfigurationManager configManager;
054: private Configuration config;
055:
056: protected void setUp() throws Exception {
057: super .setUp();
058: req = new MockHttpServletRequest();
059: req.setupGetParameterMap(new HashMap());
060: req.setupGetContextPath("/my/namespace");
061:
062: config = new DefaultConfiguration();
063: PackageConfig pkg = new PackageConfig("myns", "/my/namespace",
064: false, null);
065: PackageConfig pkg2 = new PackageConfig("my", "/my", false, null);
066: config.addPackageConfig("mvns", pkg);
067: config.addPackageConfig("my", pkg2);
068: configManager = new ConfigurationManager() {
069: public Configuration getConfiguration() {
070: return config;
071: }
072: };
073: }
074:
075: public void testGetMapping() throws Exception {
076: setUp();
077: req.setupGetRequestURI("/my/namespace/actionName.action");
078: req.setupGetServletPath("/my/namespace/actionName.action");
079: req.setupGetAttribute(null);
080: req
081: .addExpectedGetAttributeName("javax.servlet.include.servlet_path");
082:
083: DefaultActionMapper mapper = new DefaultActionMapper();
084: ActionMapping mapping = mapper.getMapping(req, configManager);
085:
086: assertEquals("/my/namespace", mapping.getNamespace());
087: assertEquals("actionName", mapping.getName());
088: assertNull(mapping.getMethod());
089: }
090:
091: public void testGetMappingWithMethod() throws Exception {
092: req.setupGetParameterMap(new HashMap());
093: req.setupGetRequestURI("/my/namespace/actionName!add.action");
094: req.setupGetServletPath("/my/namespace/actionName!add.action");
095: req.setupGetAttribute(null);
096: req
097: .addExpectedGetAttributeName("javax.servlet.include.servlet_path");
098:
099: DefaultActionMapper mapper = new DefaultActionMapper();
100: ActionMapping mapping = mapper.getMapping(req, configManager);
101:
102: assertEquals("/my/namespace", mapping.getNamespace());
103: assertEquals("actionName", mapping.getName());
104: assertEquals("add", mapping.getMethod());
105: }
106:
107: public void testGetMappingWithSlashedName() throws Exception {
108:
109: req.setupGetRequestURI("/my/foo/actionName.action");
110: req.setupGetServletPath("/my/foo/actionName.action");
111: req.setupGetAttribute(null);
112: req
113: .addExpectedGetAttributeName("javax.servlet.include.servlet_path");
114:
115: DefaultActionMapper mapper = new DefaultActionMapper();
116: mapper.setSlashesInActionNames("true");
117: ActionMapping mapping = mapper.getMapping(req, configManager);
118:
119: assertEquals("/my", mapping.getNamespace());
120: assertEquals("foo/actionName", mapping.getName());
121: assertNull(mapping.getMethod());
122: }
123:
124: public void testGetMappingWithNamespaceSlash() throws Exception {
125:
126: req.setupGetRequestURI("/my.hh/abc.action");
127: req.setupGetServletPath("/my.hh/abc.action");
128: req.setupGetAttribute(null);
129: req
130: .addExpectedGetAttributeName("javax.servlet.include.servlet_path");
131:
132: DefaultActionMapper mapper = new DefaultActionMapper();
133: ActionMapping mapping = mapper.getMapping(req, configManager);
134:
135: assertEquals("", mapping.getNamespace());
136: assertEquals("abc", mapping.getName());
137:
138: req.setupGetAttribute(null);
139: req
140: .addExpectedGetAttributeName("javax.servlet.include.servlet_path");
141: mapper = new DefaultActionMapper();
142: mapper.setSlashesInActionNames("true");
143: mapping = mapper.getMapping(req, configManager);
144:
145: assertEquals("", mapping.getNamespace());
146: assertEquals("my.hh/abc", mapping.getName());
147: }
148:
149: public void testGetMappingWithUnknownNamespace() throws Exception {
150: setUp();
151: req.setupGetRequestURI("/bo/foo/actionName.action");
152: req.setupGetServletPath("/bo/foo/actionName.action");
153: req.setupGetAttribute(null);
154: req
155: .addExpectedGetAttributeName("javax.servlet.include.servlet_path");
156:
157: DefaultActionMapper mapper = new DefaultActionMapper();
158: ActionMapping mapping = mapper.getMapping(req, configManager);
159:
160: assertEquals("", mapping.getNamespace());
161: assertEquals("actionName", mapping.getName());
162: assertNull(mapping.getMethod());
163: }
164:
165: public void testGetMappingWithUnknownNamespaceButFullNamespaceSelect()
166: throws Exception {
167: setUp();
168: req.setupGetRequestURI("/bo/foo/actionName.action");
169: req.setupGetServletPath("/bo/foo/actionName.action");
170: req.setupGetAttribute(null);
171: req
172: .addExpectedGetAttributeName("javax.servlet.include.servlet_path");
173:
174: DefaultActionMapper mapper = new DefaultActionMapper();
175: mapper.setAlwaysSelectFullNamespace("true");
176: ActionMapping mapping = mapper.getMapping(req, configManager);
177:
178: assertEquals("/bo/foo", mapping.getNamespace());
179: assertEquals("actionName", mapping.getName());
180: assertNull(mapping.getMethod());
181: }
182:
183: public void testGetUri() throws Exception {
184: req.setupGetParameterMap(new HashMap());
185: req.setupGetRequestURI("/my/namespace/actionName.action");
186: req.setupGetServletPath("/my/namespace/actionName.action");
187: req.setupGetAttribute(null);
188: req
189: .addExpectedGetAttributeName("javax.servlet.include.servlet_path");
190:
191: DefaultActionMapper mapper = new DefaultActionMapper();
192: ActionMapping mapping = mapper.getMapping(req, configManager);
193: assertEquals("/my/namespace/actionName.action", mapper
194: .getUriFromActionMapping(mapping));
195: }
196:
197: public void testGetUriWithMethod() throws Exception {
198: req.setupGetParameterMap(new HashMap());
199: req.setupGetRequestURI("/my/namespace/actionName!add.action");
200: req.setupGetServletPath("/my/namespace/actionName!add.action");
201: req.setupGetAttribute(null);
202: req
203: .addExpectedGetAttributeName("javax.servlet.include.servlet_path");
204:
205: DefaultActionMapper mapper = new DefaultActionMapper();
206: ActionMapping mapping = mapper.getMapping(req, configManager);
207:
208: assertEquals("/my/namespace/actionName!add.action", mapper
209: .getUriFromActionMapping(mapping));
210: }
211:
212: public void testGetMappingWithNoExtension() throws Exception {
213: req.setupGetParameterMap(new HashMap());
214: req.setupGetRequestURI("/my/namespace/actionName");
215: req.setupGetServletPath("/my/namespace/actionName");
216: req.setupGetAttribute(null);
217: req
218: .addExpectedGetAttributeName("javax.servlet.include.servlet_path");
219:
220: DefaultActionMapper mapper = new DefaultActionMapper();
221: mapper.setExtensions("");
222: ActionMapping mapping = mapper.getMapping(req, configManager);
223:
224: assertEquals("/my/namespace", mapping.getNamespace());
225: assertEquals("actionName", mapping.getName());
226: assertNull(mapping.getMethod());
227: }
228:
229: public void testGetMappingWithNoExtensionButUriHasExtension()
230: throws Exception {
231: req.setupGetParameterMap(new HashMap());
232: req.setupGetRequestURI("/my/namespace/actionName.html");
233: req.setupGetServletPath("/my/namespace/actionName.html");
234: req.setupGetAttribute(null);
235: req
236: .addExpectedGetAttributeName("javax.servlet.include.servlet_path");
237:
238: DefaultActionMapper mapper = new DefaultActionMapper();
239: mapper.setExtensions("");
240: ActionMapping mapping = mapper.getMapping(req, configManager);
241:
242: assertEquals("/my/namespace", mapping.getNamespace());
243: assertEquals("actionName.html", mapping.getName());
244: assertNull(mapping.getMethod());
245: }
246:
247: // =============================
248: // === test name & namespace ===
249: // =============================
250:
251: public void testParseNameAndNamespace1() throws Exception {
252: ActionMapping actionMapping = new ActionMapping();
253:
254: DefaultActionMapper defaultActionMapper = new DefaultActionMapper();
255: defaultActionMapper.parseNameAndNamespace("someAction",
256: actionMapping, configManager);
257:
258: assertEquals(actionMapping.getName(), "someAction");
259: assertEquals(actionMapping.getNamespace(), "");
260: }
261:
262: public void testParseNameAndNamespace2() throws Exception {
263: ActionMapping actionMapping = new ActionMapping();
264:
265: DefaultActionMapper defaultActionMapper = new DefaultActionMapper();
266: defaultActionMapper.parseNameAndNamespace("/someAction",
267: actionMapping, configManager);
268:
269: assertEquals(actionMapping.getName(), "someAction");
270: assertEquals(actionMapping.getNamespace(), "/");
271: }
272:
273: public void testParseNameAndNamespace3() throws Exception {
274: ActionMapping actionMapping = new ActionMapping();
275:
276: DefaultActionMapper defaultActionMapper = new DefaultActionMapper();
277: defaultActionMapper.parseNameAndNamespace("/my/someAction",
278: actionMapping, configManager);
279:
280: assertEquals(actionMapping.getName(), "someAction");
281: assertEquals(actionMapping.getNamespace(), "/my");
282: }
283:
284: public void testParseNameAndNamespace_NoSlashes() throws Exception {
285: ActionMapping actionMapping = new ActionMapping();
286:
287: DefaultActionMapper defaultActionMapper = new DefaultActionMapper();
288: defaultActionMapper.setSlashesInActionNames("false");
289: defaultActionMapper.parseNameAndNamespace("/foo/someAction",
290: actionMapping, configManager);
291:
292: assertEquals(actionMapping.getName(), "someAction");
293: assertEquals(actionMapping.getNamespace(), "");
294: }
295:
296: public void testParseNameAndNamespace_AllowSlashes()
297: throws Exception {
298: ActionMapping actionMapping = new ActionMapping();
299:
300: DefaultActionMapper defaultActionMapper = new DefaultActionMapper();
301: defaultActionMapper.setSlashesInActionNames("true");
302: defaultActionMapper.parseNameAndNamespace("/foo/someAction",
303: actionMapping, configManager);
304:
305: assertEquals(actionMapping.getName(), "foo/someAction");
306: assertEquals(actionMapping.getNamespace(), "");
307: }
308:
309: // ===========================
310: // === test special prefix ===
311: // ===========================
312:
313: public void testActionPrefix() throws Exception {
314: Map parameterMap = new HashMap();
315: parameterMap.put(
316: DefaultActionMapper.ACTION_PREFIX + "myAction", "");
317:
318: StrutsMockHttpServletRequest request = new StrutsMockHttpServletRequest();
319: request.setParameterMap(parameterMap);
320: request.setupGetServletPath("/someServletPath.action");
321:
322: DefaultActionMapper defaultActionMapper = new DefaultActionMapper();
323: ActionMapping actionMapping = defaultActionMapper.getMapping(
324: request, configManager);
325:
326: assertEquals(actionMapping.getName(), "myAction");
327: }
328:
329: public void testActionPrefix_fromImageButton() throws Exception {
330: Map parameterMap = new HashMap();
331: parameterMap.put(
332: DefaultActionMapper.ACTION_PREFIX + "myAction", "");
333: parameterMap.put(DefaultActionMapper.ACTION_PREFIX
334: + "myAction.x", "");
335: parameterMap.put(DefaultActionMapper.ACTION_PREFIX
336: + "myAction.y", "");
337:
338: StrutsMockHttpServletRequest request = new StrutsMockHttpServletRequest();
339: request.setParameterMap(parameterMap);
340: request.setupGetServletPath("/someServletPath.action");
341:
342: DefaultActionMapper defaultActionMapper = new DefaultActionMapper();
343: ActionMapping actionMapping = defaultActionMapper.getMapping(
344: request, configManager);
345:
346: assertEquals(actionMapping.getName(), "myAction");
347: }
348:
349: public void testActionPrefix_fromIEImageButton() throws Exception {
350: Map parameterMap = new HashMap();
351: parameterMap.put(DefaultActionMapper.ACTION_PREFIX
352: + "myAction.x", "");
353: parameterMap.put(DefaultActionMapper.ACTION_PREFIX
354: + "myAction.y", "");
355:
356: StrutsMockHttpServletRequest request = new StrutsMockHttpServletRequest();
357: request.setParameterMap(parameterMap);
358: request.setupGetServletPath("/someServletPath.action");
359:
360: DefaultActionMapper defaultActionMapper = new DefaultActionMapper();
361: ActionMapping actionMapping = defaultActionMapper.getMapping(
362: request, configManager);
363:
364: assertEquals(actionMapping.getName(), "myAction");
365: }
366:
367: public void testRedirectPrefix() throws Exception {
368: Map parameterMap = new HashMap();
369: parameterMap.put(DefaultActionMapper.REDIRECT_PREFIX
370: + "http://www.google.com", "");
371:
372: StrutsMockHttpServletRequest request = new StrutsMockHttpServletRequest();
373: request.setupGetServletPath("/someServletPath.action");
374: request.setParameterMap(parameterMap);
375:
376: DefaultActionMapper defaultActionMapper = new DefaultActionMapper();
377: defaultActionMapper.setContainer(container);
378: ActionMapping actionMapping = defaultActionMapper.getMapping(
379: request, configManager);
380:
381: Result result = actionMapping.getResult();
382: assertNotNull(result);
383: assertTrue(result instanceof ServletRedirectResult);
384:
385: Mock invMock = new Mock(ActionInvocation.class);
386: ActionInvocation inv = (ActionInvocation) invMock.proxy();
387: ActionContext ctx = ActionContext.getContext();
388: ctx.put(ServletActionContext.HTTP_REQUEST, request);
389: StrutsMockHttpServletResponse response = new StrutsMockHttpServletResponse();
390: ctx.put(ServletActionContext.HTTP_RESPONSE, response);
391: invMock.expectAndReturn("getInvocationContext", ctx);
392: invMock.expectAndReturn("getStack", ctx.getValueStack());
393: result.execute(inv);
394: assertEquals("http://www.google.com", response.getRedirectURL());
395: //TODO: need to test location but there's noaccess to the property/method, unless we use reflection
396: }
397:
398: public void testRedirectActionPrefix() throws Exception {
399: Map parameterMap = new HashMap();
400: parameterMap.put(DefaultActionMapper.REDIRECT_ACTION_PREFIX
401: + "myAction", "");
402:
403: StrutsMockHttpServletRequest request = new StrutsMockHttpServletRequest();
404: request.setupGetServletPath("/someServletPath.action");
405: request.setParameterMap(parameterMap);
406:
407: DefaultActionMapper defaultActionMapper = new DefaultActionMapper();
408: defaultActionMapper.setContainer(container);
409: ActionMapping actionMapping = defaultActionMapper.getMapping(
410: request, configManager);
411:
412: Result result = actionMapping.getResult();
413: assertNotNull(result);
414: assertTrue(result instanceof ServletRedirectResult);
415:
416: // TODO: need to test location but there's noaccess to the property/method, unless we use reflection
417: }
418:
419: public void testDropExtension() throws Exception {
420: DefaultActionMapper mapper = new DefaultActionMapper();
421: String name = mapper.dropExtension("foo.action");
422: assertTrue("Name not right: " + name, "foo".equals(name));
423:
424: name = mapper.dropExtension("foo.action.action");
425: assertTrue("Name not right: " + name, "foo.action".equals(name));
426:
427: }
428:
429: public void testGetUriFromActionMapper1() throws Exception {
430: DefaultActionMapper mapper = new DefaultActionMapper();
431: ActionMapping actionMapping = new ActionMapping();
432: actionMapping.setMethod("myMethod");
433: actionMapping.setName("myActionName");
434: actionMapping.setNamespace("/myNamespace");
435: String uri = mapper.getUriFromActionMapping(actionMapping);
436:
437: assertEquals("/myNamespace/myActionName!myMethod.action", uri);
438: }
439:
440: public void testGetUriFromActionMapper2() throws Exception {
441: DefaultActionMapper mapper = new DefaultActionMapper();
442: ActionMapping actionMapping = new ActionMapping();
443: actionMapping.setMethod("myMethod");
444: actionMapping.setName("myActionName");
445: actionMapping.setNamespace("/");
446: String uri = mapper.getUriFromActionMapping(actionMapping);
447:
448: assertEquals("/myActionName!myMethod.action", uri);
449: }
450:
451: public void testGetUriFromActionMapper3() throws Exception {
452: DefaultActionMapper mapper = new DefaultActionMapper();
453: ActionMapping actionMapping = new ActionMapping();
454: actionMapping.setMethod("myMethod");
455: actionMapping.setName("myActionName");
456: actionMapping.setNamespace("");
457: String uri = mapper.getUriFromActionMapping(actionMapping);
458:
459: assertEquals("/myActionName!myMethod.action", uri);
460: }
461:
462: public void testGetUriFromActionMapper4() throws Exception {
463: DefaultActionMapper mapper = new DefaultActionMapper();
464: ActionMapping actionMapping = new ActionMapping();
465: actionMapping.setName("myActionName");
466: actionMapping.setNamespace("");
467: String uri = mapper.getUriFromActionMapping(actionMapping);
468:
469: assertEquals("/myActionName.action", uri);
470: }
471:
472: public void testGetUriFromActionMapper5() throws Exception {
473: DefaultActionMapper mapper = new DefaultActionMapper();
474: ActionMapping actionMapping = new ActionMapping();
475: actionMapping.setName("myActionName");
476: actionMapping.setNamespace("/");
477: String uri = mapper.getUriFromActionMapping(actionMapping);
478:
479: assertEquals("/myActionName.action", uri);
480: }
481:
482: //
483: public void testGetUriFromActionMapper6() throws Exception {
484: DefaultActionMapper mapper = new DefaultActionMapper();
485: ActionMapping actionMapping = new ActionMapping();
486: actionMapping.setMethod("myMethod");
487: actionMapping.setName("myActionName?test=bla");
488: actionMapping.setNamespace("/myNamespace");
489: String uri = mapper.getUriFromActionMapping(actionMapping);
490:
491: assertEquals(
492: "/myNamespace/myActionName!myMethod.action?test=bla",
493: uri);
494: }
495:
496: public void testGetUriFromActionMapper7() throws Exception {
497: DefaultActionMapper mapper = new DefaultActionMapper();
498: ActionMapping actionMapping = new ActionMapping();
499: actionMapping.setMethod("myMethod");
500: actionMapping.setName("myActionName?test=bla");
501: actionMapping.setNamespace("/");
502: String uri = mapper.getUriFromActionMapping(actionMapping);
503:
504: assertEquals("/myActionName!myMethod.action?test=bla", uri);
505: }
506:
507: public void testGetUriFromActionMapper8() throws Exception {
508: DefaultActionMapper mapper = new DefaultActionMapper();
509: ActionMapping actionMapping = new ActionMapping();
510: actionMapping.setMethod("myMethod");
511: actionMapping.setName("myActionName?test=bla");
512: actionMapping.setNamespace("");
513: String uri = mapper.getUriFromActionMapping(actionMapping);
514:
515: assertEquals("/myActionName!myMethod.action?test=bla", uri);
516: }
517:
518: public void testGetUriFromActionMapper9() throws Exception {
519: DefaultActionMapper mapper = new DefaultActionMapper();
520: ActionMapping actionMapping = new ActionMapping();
521: actionMapping.setName("myActionName?test=bla");
522: actionMapping.setNamespace("");
523: String uri = mapper.getUriFromActionMapping(actionMapping);
524:
525: assertEquals("/myActionName.action?test=bla", uri);
526: }
527:
528: public void testGetUriFromActionMapper10() throws Exception {
529: DefaultActionMapper mapper = new DefaultActionMapper();
530: ActionMapping actionMapping = new ActionMapping();
531: actionMapping.setName("myActionName?test=bla");
532: actionMapping.setNamespace("/");
533: String uri = mapper.getUriFromActionMapping(actionMapping);
534:
535: assertEquals("/myActionName.action?test=bla", uri);
536: }
537:
538: public void testGetUriFromActionMapper11() throws Exception {
539: DefaultActionMapper mapper = new DefaultActionMapper();
540: ActionMapping actionMapping = new ActionMapping();
541: actionMapping.setName("myActionName.action");
542: actionMapping.setNamespace("/");
543: String uri = mapper.getUriFromActionMapping(actionMapping);
544:
545: assertEquals("/myActionName.action", uri);
546: }
547:
548: public void testGetUriFromActionMapper12() throws Exception {
549: DefaultActionMapper mapper = new DefaultActionMapper();
550: ActionMapping actionMapping = new ActionMapping();
551: actionMapping.setName("myActionName.action");
552: actionMapping.setNamespace("/");
553: String uri = mapper.getUriFromActionMapping(actionMapping);
554:
555: assertEquals("/myActionName.action", uri);
556: }
557:
558: }
|