001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.dispatcher.mapper;
006:
007: import java.util.HashMap;
008: import java.util.Map;
009:
010: import com.opensymphony.webwork.dispatcher.ServletRedirectResult;
011: import com.opensymphony.webwork.views.jsp.WebWorkMockHttpServletRequest;
012: import com.opensymphony.xwork.Result;
013:
014: import junit.framework.TestCase;
015:
016: /**
017: * DefaultActionMapper test case.
018: *
019: * @author tm_jee
020: * @version $Date: 2006-03-17 10:46:18 +0100 (Fri, 17 Mar 2006) $ $Id: DefaultActionMapperTest.java 2439 2006-03-17 09:46:18Z rainerh $
021: */
022: public class DefaultActionMapperTest extends TestCase {
023:
024: // =============================
025: // === test name & namespace ===
026: // =============================
027:
028: public void testParseNameAndNamespace1() throws Exception {
029: ActionMapping actionMapping = new ActionMapping();
030:
031: DefaultActionMapper defaultActionMapper = new DefaultActionMapper();
032: defaultActionMapper.parseNameAndNamespace("someAction.action",
033: actionMapping);
034:
035: assertEquals(actionMapping.getName(), "someAction");
036: assertEquals(actionMapping.getNamespace(), "");
037: }
038:
039: public void testParseNameAndNamespace2() throws Exception {
040: ActionMapping actionMapping = new ActionMapping();
041:
042: DefaultActionMapper defaultActionMapper = new DefaultActionMapper();
043: defaultActionMapper.parseNameAndNamespace("/someAction.action",
044: actionMapping);
045:
046: assertEquals(actionMapping.getName(), "someAction");
047: assertEquals(actionMapping.getNamespace(), "/");
048: }
049:
050: public void testParseNameAndNamespace3() throws Exception {
051: ActionMapping actionMapping = new ActionMapping();
052:
053: DefaultActionMapper defaultActionMapper = new DefaultActionMapper();
054: defaultActionMapper.parseNameAndNamespace(
055: "/one/two/three/someAction.action", actionMapping);
056:
057: assertEquals(actionMapping.getName(), "someAction");
058: assertEquals(actionMapping.getNamespace(), "/one/two/three");
059: }
060:
061: // ===========================
062: // === test special prefix ===
063: // ===========================
064: public void testMethodPrefix() throws Exception {
065: Map parameterMap = new HashMap();
066: parameterMap.put(
067: DefaultActionMapper.METHOD_PREFIX + "myMethod", "");
068:
069: WebWorkMockHttpServletRequest request = new WebWorkMockHttpServletRequest();
070: request.setParameterMap(parameterMap);
071: request.setupGetServletPath("/someServletPath.action");
072:
073: DefaultActionMapper defaultActionMapper = new DefaultActionMapper();
074: ActionMapping actionMapping = defaultActionMapper
075: .getMapping(request);
076:
077: assertEquals(actionMapping.getMethod(), "myMethod");
078: }
079:
080: public void testActionPrefix() throws Exception {
081: Map parameterMap = new HashMap();
082: parameterMap.put(
083: DefaultActionMapper.ACTION_PREFIX + "myAction", "");
084:
085: WebWorkMockHttpServletRequest request = new WebWorkMockHttpServletRequest();
086: request.setParameterMap(parameterMap);
087: request.setupGetServletPath("/someServletPath.action");
088:
089: DefaultActionMapper defaultActionMapper = new DefaultActionMapper();
090: ActionMapping actionMapping = defaultActionMapper
091: .getMapping(request);
092:
093: assertEquals(actionMapping.getName(), "myAction");
094: }
095:
096: public void testRedirectPrefix() throws Exception {
097: Map parameterMap = new HashMap();
098: parameterMap.put(DefaultActionMapper.REDIRECT_PREFIX
099: + "www.google.com", "");
100:
101: WebWorkMockHttpServletRequest request = new WebWorkMockHttpServletRequest();
102: request.setupGetServletPath("/someServletPath.action");
103: request.setParameterMap(parameterMap);
104:
105: DefaultActionMapper defaultActionMapper = new DefaultActionMapper();
106: ActionMapping actionMapping = defaultActionMapper
107: .getMapping(request);
108:
109: Result result = actionMapping.getResult();
110: assertNotNull(result);
111: assertTrue(result instanceof ServletRedirectResult);
112:
113: //TODO: need to test location but there's noaccess to the property/method, unless we use reflection
114: }
115:
116: public void testRedirectActionPrefix() throws Exception {
117: Map parameterMap = new HashMap();
118: parameterMap.put(DefaultActionMapper.REDIRECT_ACTION_PREFIX
119: + "myAction", "");
120:
121: WebWorkMockHttpServletRequest request = new WebWorkMockHttpServletRequest();
122: request.setupGetServletPath("/someServletPath.action");
123: request.setParameterMap(parameterMap);
124:
125: DefaultActionMapper defaultActionMapper = new DefaultActionMapper();
126: ActionMapping actionMapping = defaultActionMapper
127: .getMapping(request);
128:
129: Result result = actionMapping.getResult();
130: assertNotNull(result);
131: assertTrue(result instanceof ServletRedirectResult);
132:
133: // TODO: need to test location but there's noaccess to the property/method, unless we use reflection
134: }
135:
136: // ==========================
137: // === test action!method ===
138: // ==========================
139: public void testActionBangMethod() throws Exception {
140:
141: WebWorkMockHttpServletRequest request = new WebWorkMockHttpServletRequest();
142: request.setupGetServletPath("/someName!someMethod.action");
143:
144: DefaultActionMapper defaultActionMapper = new DefaultActionMapper();
145: ActionMapping actionMapping = defaultActionMapper
146: .getMapping(request);
147:
148: assertEquals(actionMapping.getMethod(), "someMethod");
149: assertEquals(actionMapping.getName(), "someName");
150: }
151:
152: public void testGetUriFromActionMapper1() throws Exception {
153: DefaultActionMapper mapper = new DefaultActionMapper();
154: ActionMapping actionMapping = new ActionMapping();
155: actionMapping.setMethod("myMethod");
156: actionMapping.setName("myActionName");
157: actionMapping.setNamespace("/myNamespace");
158: String uri = mapper.getUriFromActionMapping(actionMapping);
159:
160: assertEquals("/myNamespace/myActionName!myMethod.action", uri);
161: }
162:
163: public void testGetUriFromActionMapper2() throws Exception {
164: DefaultActionMapper mapper = new DefaultActionMapper();
165: ActionMapping actionMapping = new ActionMapping();
166: actionMapping.setMethod("myMethod");
167: actionMapping.setName("myActionName");
168: actionMapping.setNamespace("/");
169: String uri = mapper.getUriFromActionMapping(actionMapping);
170:
171: assertEquals("/myActionName!myMethod.action", uri);
172: }
173:
174: public void testGetUriFromActionMapper3() throws Exception {
175: DefaultActionMapper mapper = new DefaultActionMapper();
176: ActionMapping actionMapping = new ActionMapping();
177: actionMapping.setMethod("myMethod");
178: actionMapping.setName("myActionName");
179: actionMapping.setNamespace("");
180: String uri = mapper.getUriFromActionMapping(actionMapping);
181:
182: assertEquals("/myActionName!myMethod.action", uri);
183: }
184:
185: public void testGetUriFromActionMapper4() throws Exception {
186: DefaultActionMapper mapper = new DefaultActionMapper();
187: ActionMapping actionMapping = new ActionMapping();
188: actionMapping.setName("myActionName");
189: actionMapping.setNamespace("");
190: String uri = mapper.getUriFromActionMapping(actionMapping);
191:
192: assertEquals("/myActionName.action", uri);
193: }
194:
195: public void testGetUriFromActionMapper5() throws Exception {
196: DefaultActionMapper mapper = new DefaultActionMapper();
197: ActionMapping actionMapping = new ActionMapping();
198: actionMapping.setName("myActionName");
199: actionMapping.setNamespace("/");
200: String uri = mapper.getUriFromActionMapping(actionMapping);
201:
202: assertEquals("/myActionName.action", uri);
203: }
204:
205: //
206: public void testGetUriFromActionMapper6() throws Exception {
207: DefaultActionMapper mapper = new DefaultActionMapper();
208: ActionMapping actionMapping = new ActionMapping();
209: actionMapping.setMethod("myMethod");
210: actionMapping.setName("myActionName?test=bla");
211: actionMapping.setNamespace("/myNamespace");
212: String uri = mapper.getUriFromActionMapping(actionMapping);
213:
214: assertEquals(
215: "/myNamespace/myActionName!myMethod.action?test=bla",
216: uri);
217: }
218:
219: public void testGetUriFromActionMapper7() throws Exception {
220: DefaultActionMapper mapper = new DefaultActionMapper();
221: ActionMapping actionMapping = new ActionMapping();
222: actionMapping.setMethod("myMethod");
223: actionMapping.setName("myActionName?test=bla");
224: actionMapping.setNamespace("/");
225: String uri = mapper.getUriFromActionMapping(actionMapping);
226:
227: assertEquals("/myActionName!myMethod.action?test=bla", uri);
228: }
229:
230: public void testGetUriFromActionMapper8() throws Exception {
231: DefaultActionMapper mapper = new DefaultActionMapper();
232: ActionMapping actionMapping = new ActionMapping();
233: actionMapping.setMethod("myMethod");
234: actionMapping.setName("myActionName?test=bla");
235: actionMapping.setNamespace("");
236: String uri = mapper.getUriFromActionMapping(actionMapping);
237:
238: assertEquals("/myActionName!myMethod.action?test=bla", uri);
239: }
240:
241: public void testGetUriFromActionMapper9() throws Exception {
242: DefaultActionMapper mapper = new DefaultActionMapper();
243: ActionMapping actionMapping = new ActionMapping();
244: actionMapping.setName("myActionName?test=bla");
245: actionMapping.setNamespace("");
246: String uri = mapper.getUriFromActionMapping(actionMapping);
247:
248: assertEquals("/myActionName.action?test=bla", uri);
249: }
250:
251: public void testGetUriFromActionMapper10() throws Exception {
252: DefaultActionMapper mapper = new DefaultActionMapper();
253: ActionMapping actionMapping = new ActionMapping();
254: actionMapping.setName("myActionName?test=bla");
255: actionMapping.setNamespace("/");
256: String uri = mapper.getUriFromActionMapping(actionMapping);
257:
258: assertEquals("/myActionName.action?test=bla", uri);
259: }
260:
261: }
|