View Javadoc

1   /*
2    * $Id: ServletConfigInterceptorTest.java 651946 2008-04-27 13:41:38Z apetrelli $
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  
22  package org.apache.struts2.interceptor;
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import javax.servlet.ServletContext;
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  
31  import org.apache.struts2.StrutsStatics;
32  import org.apache.struts2.StrutsTestCase;
33  import org.apache.struts2.util.ServletContextAware;
34  import org.easymock.MockControl;
35  import org.springframework.mock.web.MockHttpServletRequest;
36  import org.springframework.mock.web.MockHttpServletResponse;
37  import org.springframework.mock.web.MockServletContext;
38  
39  import com.opensymphony.xwork2.Action;
40  import com.opensymphony.xwork2.ActionContext;
41  import com.opensymphony.xwork2.mock.MockActionInvocation;
42  
43  /***
44   * Unit test for {@link ServletConfigInterceptor}.
45   *
46   */
47  public class ServletConfigInterceptorTest extends StrutsTestCase {
48  
49      private ServletConfigInterceptor interceptor;
50  
51      public void testServletRequestAware() throws Exception {
52          MockControl control = MockControl.createControl(ServletRequestAware.class);
53          ServletRequestAware mock = (ServletRequestAware) control.getMock();
54  
55          MockHttpServletRequest req = new MockHttpServletRequest();
56  
57          MockActionInvocation mai = createActionInvocation(mock);
58          mai.getInvocationContext().put(StrutsStatics.HTTP_REQUEST, req);
59  
60          mock.setServletRequest((HttpServletRequest) req);
61          control.setVoidCallable();
62  
63          control.replay();
64          interceptor.intercept(mai);
65          control.verify();
66      }
67  
68      public void testServletResponseAware() throws Exception {
69          MockControl control = MockControl.createControl(ServletResponseAware.class);
70          ServletResponseAware mock = (ServletResponseAware) control.getMock();
71  
72          MockHttpServletResponse res = new MockHttpServletResponse();
73  
74          MockActionInvocation mai = createActionInvocation(mock);
75          mai.getInvocationContext().put(StrutsStatics.HTTP_RESPONSE, res);
76  
77          mock.setServletResponse((HttpServletResponse) res);
78          control.setVoidCallable();
79  
80          control.replay();
81          interceptor.intercept(mai);
82          control.verify();
83      }
84  
85      public void testParameterAware() throws Exception {
86          MockControl control = MockControl.createControl(ParameterAware.class);
87          ParameterAware mock = (ParameterAware) control.getMock();
88  
89          MockActionInvocation mai = createActionInvocation(mock);
90  
91          Map param = new HashMap();
92          mai.getInvocationContext().setParameters(param);
93  
94          mock.setParameters(param);
95          control.setVoidCallable();
96  
97          control.replay();
98          interceptor.intercept(mai);
99          control.verify();
100     }
101 
102     public void testSessionAware() throws Exception {
103         MockControl control = MockControl.createControl(SessionAware.class);
104         SessionAware mock = (SessionAware) control.getMock();
105 
106         MockActionInvocation mai = createActionInvocation(mock);
107 
108         Map session = new HashMap();
109         mai.getInvocationContext().setSession(session);
110 
111         mock.setSession(session);
112         control.setVoidCallable();
113 
114         control.replay();
115         interceptor.intercept(mai);
116         control.verify();
117     }
118 
119     public void testApplicationAware() throws Exception {
120         MockControl control = MockControl.createControl(ApplicationAware.class);
121         ApplicationAware mock = (ApplicationAware) control.getMock();
122 
123         MockActionInvocation mai = createActionInvocation(mock);
124 
125         Map app = new HashMap();
126         mai.getInvocationContext().setApplication(app);
127 
128         mock.setApplication(app);
129         control.setVoidCallable();
130 
131         control.replay();
132         interceptor.intercept(mai);
133         control.verify();
134     }
135 
136     public void testPrincipalAware() throws Exception {
137         MockHttpServletRequest req = new MockHttpServletRequest();
138         req.setUserPrincipal(null);
139         req.setRemoteUser("Santa");
140         MockControl control = MockControl.createControl(PrincipalAware.class);
141         control.setDefaultMatcher(MockControl.ALWAYS_MATCHER); // less strick match is needed for this unit test to be conducted using mocks
142         PrincipalAware mock = (PrincipalAware) control.getMock();
143 
144         MockActionInvocation mai = createActionInvocation(mock);
145         mai.getInvocationContext().put(StrutsStatics.HTTP_REQUEST, req);
146         
147         MockServletContext ctx = new MockServletContext();
148         mai.getInvocationContext().put(StrutsStatics.SERVLET_CONTEXT, ctx);
149 
150         mock.setPrincipalProxy(null); // we can do this because of ALWAYS_MATCHER
151         control.setVoidCallable();
152 
153         control.replay();
154         interceptor.intercept(mai);
155         control.verify();
156     }
157 
158     public void testPrincipalProxy() throws Exception {
159         // uni test that does not use mock, but an Action so we also get code coverage for the PrincipalProxy class
160         MockHttpServletRequest req = new MockHttpServletRequest();
161         req.setUserPrincipal(null);
162         req.setRemoteUser("Santa");
163 
164         MyPrincipalAction action = new MyPrincipalAction();
165         MockActionInvocation mai = createActionInvocation(action);
166         mai.getInvocationContext().put(StrutsStatics.HTTP_REQUEST, req);
167 
168         assertNull(action.getProxy());
169         interceptor.intercept(mai);
170         assertNotNull(action.getProxy());
171 
172         PrincipalProxy proxy = action.getProxy();
173         assertEquals(proxy.getRequest(), req);
174         assertNull(proxy.getUserPrincipal());
175         assertTrue(! proxy.isRequestSecure());
176         assertTrue(! proxy.isUserInRole("no.role"));
177         assertEquals("Santa", proxy.getRemoteUser());
178 
179     }
180 
181     public void testServletContextAware() throws Exception {
182         MockControl control = MockControl.createControl(ServletContextAware.class);
183         ServletContextAware mock = (ServletContextAware) control.getMock();
184 
185         MockActionInvocation mai = createActionInvocation(mock);
186 
187         MockServletContext ctx = new MockServletContext();
188         mai.getInvocationContext().put(StrutsStatics.SERVLET_CONTEXT, ctx);
189 
190         mock.setServletContext((ServletContext) ctx);
191         control.setVoidCallable();
192 
193         control.replay();
194         interceptor.intercept(mai);
195         control.verify();
196     }
197 
198     private MockActionInvocation createActionInvocation(Object mock) {
199         MockActionInvocation mai = new MockActionInvocation();
200         mai.setResultCode("success");
201         mai.setInvocationContext(ActionContext.getContext());
202         mai.setAction(mock);
203         return mai;
204     }
205 
206 
207     protected void setUp() throws Exception {
208         super.setUp();
209         interceptor = new ServletConfigInterceptor();
210         interceptor.init();
211     }
212 
213     protected void tearDown() throws Exception {
214         super.tearDown();
215         interceptor.destroy();
216         interceptor = null;
217     }
218 
219     private class MyPrincipalAction implements Action, PrincipalAware {
220 
221         private PrincipalProxy proxy;
222 
223         public String execute() throws Exception {
224             return SUCCESS;
225         }
226 
227         public void setPrincipalProxy(PrincipalProxy proxy) {
228             this.proxy = proxy;
229         }
230 
231         public PrincipalProxy getProxy() {
232             return proxy;
233         }
234     }
235 
236 }