View Javadoc

1   /*
2    * $Id: ServletConfigInterceptorTest.java 471756 2006-11-06 15:01:43Z husted $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.struts2.interceptor;
22  
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import javax.servlet.ServletContext;
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  
30  import org.apache.struts2.StrutsStatics;
31  import org.apache.struts2.StrutsTestCase;
32  import org.apache.struts2.util.ServletContextAware;
33  import org.easymock.MockControl;
34  import org.springframework.mock.web.MockHttpServletRequest;
35  import org.springframework.mock.web.MockHttpServletResponse;
36  import org.springframework.mock.web.MockServletContext;
37  
38  import com.opensymphony.xwork2.Action;
39  import com.opensymphony.xwork2.ActionContext;
40  import com.opensymphony.xwork2.mock.MockActionInvocation;
41  
42  /***
43   * Unit test for {@link ServletConfigInterceptor}.
44   *
45   */
46  public class ServletConfigInterceptorTest extends StrutsTestCase {
47  
48      private ServletConfigInterceptor interceptor;
49  
50      public void testServletRequestAware() throws Exception {
51          MockControl control = MockControl.createControl(ServletRequestAware.class);
52          ServletRequestAware mock = (ServletRequestAware) control.getMock();
53  
54          MockHttpServletRequest req = new MockHttpServletRequest();
55  
56          MockActionInvocation mai = createActionInvocation(mock);
57          mai.getInvocationContext().put(StrutsStatics.HTTP_REQUEST, req);
58  
59          mock.setServletRequest((HttpServletRequest) req);
60          control.setVoidCallable();
61  
62          control.replay();
63          interceptor.intercept(mai);
64          control.verify();
65      }
66  
67      public void testServletResponseAware() throws Exception {
68          MockControl control = MockControl.createControl(ServletResponseAware.class);
69          ServletResponseAware mock = (ServletResponseAware) control.getMock();
70  
71          MockHttpServletResponse res = new MockHttpServletResponse();
72  
73          MockActionInvocation mai = createActionInvocation(mock);
74          mai.getInvocationContext().put(StrutsStatics.HTTP_RESPONSE, res);
75  
76          mock.setServletResponse((HttpServletResponse) res);
77          control.setVoidCallable();
78  
79          control.replay();
80          interceptor.intercept(mai);
81          control.verify();
82      }
83  
84      public void testParameterAware() throws Exception {
85          MockControl control = MockControl.createControl(ParameterAware.class);
86          ParameterAware mock = (ParameterAware) control.getMock();
87  
88          MockActionInvocation mai = createActionInvocation(mock);
89  
90          Map param = new HashMap();
91          mai.getInvocationContext().setParameters(param);
92  
93          mock.setParameters(param);
94          control.setVoidCallable();
95  
96          control.replay();
97          interceptor.intercept(mai);
98          control.verify();
99      }
100 
101     public void testSessionAware() throws Exception {
102         MockControl control = MockControl.createControl(SessionAware.class);
103         SessionAware mock = (SessionAware) control.getMock();
104 
105         MockActionInvocation mai = createActionInvocation(mock);
106 
107         Map session = new HashMap();
108         mai.getInvocationContext().setSession(session);
109 
110         mock.setSession(session);
111         control.setVoidCallable();
112 
113         control.replay();
114         interceptor.intercept(mai);
115         control.verify();
116     }
117 
118     public void testApplicationAware() throws Exception {
119         MockControl control = MockControl.createControl(ApplicationAware.class);
120         ApplicationAware mock = (ApplicationAware) control.getMock();
121 
122         MockActionInvocation mai = createActionInvocation(mock);
123 
124         Map app = new HashMap();
125         mai.getInvocationContext().setApplication(app);
126 
127         mock.setApplication(app);
128         control.setVoidCallable();
129 
130         control.replay();
131         interceptor.intercept(mai);
132         control.verify();
133     }
134 
135     public void testPrincipalAware() throws Exception {
136         MockControl control = MockControl.createControl(PrincipalAware.class);
137         control.setDefaultMatcher(MockControl.ALWAYS_MATCHER); // less strick match is needed for this unit test to be conducted using mocks
138         PrincipalAware mock = (PrincipalAware) control.getMock();
139 
140         MockActionInvocation mai = createActionInvocation(mock);
141 
142         MockServletContext ctx = new MockServletContext();
143         mai.getInvocationContext().put(StrutsStatics.SERVLET_CONTEXT, ctx);
144 
145         mock.setPrincipalProxy(null); // we can do this because of ALWAYS_MATCHER
146         control.setVoidCallable();
147 
148         control.replay();
149         interceptor.intercept(mai);
150         control.verify();
151     }
152 
153     public void testPrincipalProxy() throws Exception {
154         // uni test that does not use mock, but an Action so we also get code coverage for the PrincipalProxy class
155         MockHttpServletRequest req = new MockHttpServletRequest();
156         req.setUserPrincipal(null);
157         req.setRemoteUser("Santa");
158 
159         MyPrincipalAction action = new MyPrincipalAction();
160         MockActionInvocation mai = createActionInvocation(action);
161         mai.getInvocationContext().put(StrutsStatics.HTTP_REQUEST, req);
162 
163         assertNull(action.getProxy());
164         interceptor.intercept(mai);
165         assertNotNull(action.getProxy());
166 
167         PrincipalProxy proxy = action.getProxy();
168         assertEquals(proxy.getRequest(), req);
169         assertNull(proxy.getUserPrincipal());
170         assertTrue(! proxy.isRequestSecure());
171         assertTrue(! proxy.isUserInRole("no.role"));
172         assertEquals("Santa", proxy.getRemoteUser());
173 
174     }
175 
176     public void testServletContextAware() throws Exception {
177         MockControl control = MockControl.createControl(ServletContextAware.class);
178         ServletContextAware mock = (ServletContextAware) control.getMock();
179 
180         MockActionInvocation mai = createActionInvocation(mock);
181 
182         MockServletContext ctx = new MockServletContext();
183         mai.getInvocationContext().put(StrutsStatics.SERVLET_CONTEXT, ctx);
184 
185         mock.setServletContext((ServletContext) ctx);
186         control.setVoidCallable();
187 
188         control.replay();
189         interceptor.intercept(mai);
190         control.verify();
191     }
192 
193     private MockActionInvocation createActionInvocation(Object mock) {
194         MockActionInvocation mai = new MockActionInvocation();
195         mai.setResultCode("success");
196         mai.setInvocationContext(ActionContext.getContext());
197         mai.setAction(mock);
198         return mai;
199     }
200 
201 
202     protected void setUp() throws Exception {
203         super.setUp();
204         interceptor = new ServletConfigInterceptor();
205         interceptor.init();
206     }
207 
208     protected void tearDown() throws Exception {
209         super.tearDown();
210         interceptor.destroy();
211         interceptor = null;
212     }
213 
214     private class MyPrincipalAction implements Action, PrincipalAware {
215 
216         private PrincipalProxy proxy;
217 
218         public String execute() throws Exception {
219             return SUCCESS;
220         }
221 
222         public void setPrincipalProxy(PrincipalProxy proxy) {
223             this.proxy = proxy;
224         }
225 
226         public PrincipalProxy getProxy() {
227             return proxy;
228         }
229     }
230 
231 }