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