001: /*
002: * $Id: ServletRedirectResultTest.java 539819 2007-05-20 03:06:34Z 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;
022:
023: import java.util.HashMap;
024:
025: import javax.servlet.http.HttpServletRequest;
026: import javax.servlet.http.HttpServletResponse;
027:
028: import ognl.Ognl;
029:
030: import org.apache.struts2.ServletActionContext;
031: import org.apache.struts2.StrutsStatics;
032: import org.apache.struts2.StrutsTestCase;
033: import org.apache.struts2.config.StrutsXmlConfigurationProvider;
034: import org.springframework.mock.web.MockServletContext;
035:
036: import com.mockobjects.dynamic.C;
037: import com.mockobjects.dynamic.Mock;
038: import com.opensymphony.xwork2.ActionContext;
039: import com.opensymphony.xwork2.ActionInvocation;
040: import com.opensymphony.xwork2.config.ConfigurationManager;
041: import com.opensymphony.xwork2.config.entities.PackageConfig;
042: import com.opensymphony.xwork2.mock.MockActionInvocation;
043: import com.opensymphony.xwork2.util.ValueStackFactory;
044:
045: /**
046: */
047: public class ServletRedirectResultTest extends StrutsTestCase implements
048: StrutsStatics {
049:
050: protected ServletRedirectResult view;
051: private Mock requestMock;
052: private Mock responseMock;
053: protected ActionInvocation ai;
054:
055: public void testAbsoluteRedirect() {
056: view.setLocation("/bar/foo.jsp");
057: responseMock.expectAndReturn("encodeRedirectURL",
058: "/context/bar/foo.jsp", "/context/bar/foo.jsp");
059: responseMock.expect("sendRedirect", C.args(C
060: .eq("/context/bar/foo.jsp")));
061:
062: try {
063: view.execute(ai);
064: requestMock.verify();
065: responseMock.verify();
066: } catch (Exception e) {
067: e.printStackTrace();
068: fail();
069: }
070: }
071:
072: public void testPrependServletContextFalse() {
073: view.setLocation("/bar/foo.jsp");
074: view.setPrependServletContext(false);
075: responseMock.expectAndReturn("encodeRedirectURL",
076: "/bar/foo.jsp", "/bar/foo.jsp");
077: responseMock.expect("sendRedirect", C
078: .args(C.eq("/bar/foo.jsp")));
079:
080: try {
081: view.execute(ai);
082: requestMock.verify();
083: responseMock.verify();
084: } catch (Exception e) {
085: e.printStackTrace();
086: fail();
087: }
088: }
089:
090: public void testRelativeRedirect() throws Exception {
091: view.setLocation("foo.jsp");
092: requestMock.expectAndReturn("getParameterMap", new HashMap());
093: requestMock.expectAndReturn("getServletPath",
094: "/namespace/some.action");
095: requestMock.expectAndReturn("getAttribute", C.ANY_ARGS, null);
096: responseMock.expectAndReturn("encodeRedirectURL",
097: "/context/namespace/foo.jsp",
098: "/context/namespace/foo.jsp");
099: responseMock.expect("sendRedirect", C.args(C
100: .eq("/context/namespace/foo.jsp")));
101:
102: view.execute(ai);
103:
104: requestMock.verify();
105: responseMock.verify();
106: }
107:
108: public void testMultipleParametersRedirect() throws Exception {
109: view.setLocation("foo.jsp?foo=bar&baz=jim");
110: requestMock.expectAndReturn("getParameterMap", new HashMap());
111: requestMock.expectAndReturn("getServletPath",
112: "/namespace/some.action");
113: requestMock.expectAndReturn("getAttribute", C.ANY_ARGS, null);
114: responseMock.expectAndReturn("encodeRedirectURL",
115: "/context/namespace/foo.jsp?foo=bar&baz=jim",
116: "/context/namespace/foo.jsp?foo=bar&baz=jim");
117: responseMock.expect("sendRedirect", C.args(C
118: .eq("/context/namespace/foo.jsp?foo=bar&baz=jim")));
119:
120: view.execute(ai);
121:
122: requestMock.verify();
123: responseMock.verify();
124: }
125:
126: protected void setUp() throws Exception {
127: super .setUp();
128: configurationManager.getConfiguration().addPackageConfig("foo",
129: new PackageConfig("foo", "/namespace", false, null));
130:
131: view = new ServletRedirectResult();
132: container.inject(view);
133:
134: responseMock = new Mock(HttpServletResponse.class);
135:
136: requestMock = new Mock(HttpServletRequest.class);
137: requestMock.matchAndReturn("getContextPath", "/context");
138:
139: ActionContext ac = new ActionContext(Ognl
140: .createDefaultContext(null));
141: ac.put(ServletActionContext.HTTP_REQUEST, requestMock.proxy());
142: ac
143: .put(ServletActionContext.HTTP_RESPONSE, responseMock
144: .proxy());
145: MockActionInvocation ai = new MockActionInvocation();
146: ai.setInvocationContext(ac);
147: this.ai = ai;
148: ai.setStack(ValueStackFactory.getFactory().createValueStack());
149: }
150: }
|