001: /*
002: * $Id: ServletConfigInterceptorTest.java 471756 2006-11-06 15:01:43Z husted $
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.interceptor;
022:
023: import java.util.HashMap;
024: import java.util.Map;
025:
026: import javax.servlet.ServletContext;
027: import javax.servlet.http.HttpServletRequest;
028: import javax.servlet.http.HttpServletResponse;
029:
030: import org.apache.struts2.StrutsStatics;
031: import org.apache.struts2.StrutsTestCase;
032: import org.apache.struts2.util.ServletContextAware;
033: import org.easymock.MockControl;
034: import org.springframework.mock.web.MockHttpServletRequest;
035: import org.springframework.mock.web.MockHttpServletResponse;
036: import org.springframework.mock.web.MockServletContext;
037:
038: import com.opensymphony.xwork2.Action;
039: import com.opensymphony.xwork2.ActionContext;
040: import com.opensymphony.xwork2.mock.MockActionInvocation;
041:
042: /**
043: * Unit test for {@link ServletConfigInterceptor}.
044: *
045: */
046: public class ServletConfigInterceptorTest extends StrutsTestCase {
047:
048: private ServletConfigInterceptor interceptor;
049:
050: public void testServletRequestAware() throws Exception {
051: MockControl control = MockControl
052: .createControl(ServletRequestAware.class);
053: ServletRequestAware mock = (ServletRequestAware) control
054: .getMock();
055:
056: MockHttpServletRequest req = new MockHttpServletRequest();
057:
058: MockActionInvocation mai = createActionInvocation(mock);
059: mai.getInvocationContext().put(StrutsStatics.HTTP_REQUEST, req);
060:
061: mock.setServletRequest((HttpServletRequest) req);
062: control.setVoidCallable();
063:
064: control.replay();
065: interceptor.intercept(mai);
066: control.verify();
067: }
068:
069: public void testServletResponseAware() throws Exception {
070: MockControl control = MockControl
071: .createControl(ServletResponseAware.class);
072: ServletResponseAware mock = (ServletResponseAware) control
073: .getMock();
074:
075: MockHttpServletResponse res = new MockHttpServletResponse();
076:
077: MockActionInvocation mai = createActionInvocation(mock);
078: mai.getInvocationContext()
079: .put(StrutsStatics.HTTP_RESPONSE, res);
080:
081: mock.setServletResponse((HttpServletResponse) res);
082: control.setVoidCallable();
083:
084: control.replay();
085: interceptor.intercept(mai);
086: control.verify();
087: }
088:
089: public void testParameterAware() throws Exception {
090: MockControl control = MockControl
091: .createControl(ParameterAware.class);
092: ParameterAware mock = (ParameterAware) control.getMock();
093:
094: MockActionInvocation mai = createActionInvocation(mock);
095:
096: Map param = new HashMap();
097: mai.getInvocationContext().setParameters(param);
098:
099: mock.setParameters(param);
100: control.setVoidCallable();
101:
102: control.replay();
103: interceptor.intercept(mai);
104: control.verify();
105: }
106:
107: public void testSessionAware() throws Exception {
108: MockControl control = MockControl
109: .createControl(SessionAware.class);
110: SessionAware mock = (SessionAware) control.getMock();
111:
112: MockActionInvocation mai = createActionInvocation(mock);
113:
114: Map session = new HashMap();
115: mai.getInvocationContext().setSession(session);
116:
117: mock.setSession(session);
118: control.setVoidCallable();
119:
120: control.replay();
121: interceptor.intercept(mai);
122: control.verify();
123: }
124:
125: public void testApplicationAware() throws Exception {
126: MockControl control = MockControl
127: .createControl(ApplicationAware.class);
128: ApplicationAware mock = (ApplicationAware) control.getMock();
129:
130: MockActionInvocation mai = createActionInvocation(mock);
131:
132: Map app = new HashMap();
133: mai.getInvocationContext().setApplication(app);
134:
135: mock.setApplication(app);
136: control.setVoidCallable();
137:
138: control.replay();
139: interceptor.intercept(mai);
140: control.verify();
141: }
142:
143: public void testPrincipalAware() throws Exception {
144: MockControl control = MockControl
145: .createControl(PrincipalAware.class);
146: control.setDefaultMatcher(MockControl.ALWAYS_MATCHER); // less strick match is needed for this unit test to be conducted using mocks
147: PrincipalAware mock = (PrincipalAware) control.getMock();
148:
149: MockActionInvocation mai = createActionInvocation(mock);
150:
151: MockServletContext ctx = new MockServletContext();
152: mai.getInvocationContext().put(StrutsStatics.SERVLET_CONTEXT,
153: ctx);
154:
155: mock.setPrincipalProxy(null); // we can do this because of ALWAYS_MATCHER
156: control.setVoidCallable();
157:
158: control.replay();
159: interceptor.intercept(mai);
160: control.verify();
161: }
162:
163: public void testPrincipalProxy() throws Exception {
164: // uni test that does not use mock, but an Action so we also get code coverage for the PrincipalProxy class
165: MockHttpServletRequest req = new MockHttpServletRequest();
166: req.setUserPrincipal(null);
167: req.setRemoteUser("Santa");
168:
169: MyPrincipalAction action = new MyPrincipalAction();
170: MockActionInvocation mai = createActionInvocation(action);
171: mai.getInvocationContext().put(StrutsStatics.HTTP_REQUEST, req);
172:
173: assertNull(action.getProxy());
174: interceptor.intercept(mai);
175: assertNotNull(action.getProxy());
176:
177: PrincipalProxy proxy = action.getProxy();
178: assertEquals(proxy.getRequest(), req);
179: assertNull(proxy.getUserPrincipal());
180: assertTrue(!proxy.isRequestSecure());
181: assertTrue(!proxy.isUserInRole("no.role"));
182: assertEquals("Santa", proxy.getRemoteUser());
183:
184: }
185:
186: public void testServletContextAware() throws Exception {
187: MockControl control = MockControl
188: .createControl(ServletContextAware.class);
189: ServletContextAware mock = (ServletContextAware) control
190: .getMock();
191:
192: MockActionInvocation mai = createActionInvocation(mock);
193:
194: MockServletContext ctx = new MockServletContext();
195: mai.getInvocationContext().put(StrutsStatics.SERVLET_CONTEXT,
196: ctx);
197:
198: mock.setServletContext((ServletContext) ctx);
199: control.setVoidCallable();
200:
201: control.replay();
202: interceptor.intercept(mai);
203: control.verify();
204: }
205:
206: private MockActionInvocation createActionInvocation(Object mock) {
207: MockActionInvocation mai = new MockActionInvocation();
208: mai.setResultCode("success");
209: mai.setInvocationContext(ActionContext.getContext());
210: mai.setAction(mock);
211: return mai;
212: }
213:
214: protected void setUp() throws Exception {
215: super .setUp();
216: interceptor = new ServletConfigInterceptor();
217: interceptor.init();
218: }
219:
220: protected void tearDown() throws Exception {
221: super .tearDown();
222: interceptor.destroy();
223: interceptor = null;
224: }
225:
226: private class MyPrincipalAction implements Action, PrincipalAware {
227:
228: private PrincipalProxy proxy;
229:
230: public String execute() throws Exception {
231: return SUCCESS;
232: }
233:
234: public void setPrincipalProxy(PrincipalProxy proxy) {
235: this .proxy = proxy;
236: }
237:
238: public PrincipalProxy getProxy() {
239: return proxy;
240: }
241: }
242:
243: }
|