View Javadoc

1   /*
2    * $Id: DispatcherTest.java 686328 2008-08-15 19:19:07Z musachy $
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.dispatcher;
23  
24  import java.util.HashMap;
25  import java.util.Locale;
26  import java.util.Map;
27  
28  import javax.servlet.FilterConfig;
29  import javax.servlet.ServletContext;
30  import javax.servlet.http.HttpServletRequest;
31  import javax.servlet.http.HttpServletResponse;
32  
33  import org.apache.struts2.StrutsConstants;
34  import org.apache.struts2.StrutsTestCase;
35  import org.apache.struts2.dispatcher.FilterDispatcherTest.InnerActionMapper;
36  import org.apache.struts2.dispatcher.FilterDispatcherTest.InnerDestroyableObjectFactory;
37  import org.apache.struts2.dispatcher.FilterDispatcherTest.InnerDispatcher;
38  import org.springframework.mock.web.MockFilterConfig;
39  import org.springframework.mock.web.MockHttpServletRequest;
40  import org.springframework.mock.web.MockHttpServletResponse;
41  import org.springframework.mock.web.MockServletContext;
42  
43  import com.mockobjects.dynamic.C;
44  import com.mockobjects.dynamic.Mock;
45  import com.mockobjects.servlet.MockFilterChain;
46  import com.opensymphony.xwork2.ObjectFactory;
47  import com.opensymphony.xwork2.config.Configuration;
48  import com.opensymphony.xwork2.config.ConfigurationManager;
49  import com.opensymphony.xwork2.config.entities.InterceptorMapping;
50  import com.opensymphony.xwork2.config.entities.InterceptorStackConfig;
51  import com.opensymphony.xwork2.config.entities.PackageConfig;
52  import com.opensymphony.xwork2.inject.Container;
53  import com.opensymphony.xwork2.inject.ContainerBuilder;
54  import com.opensymphony.xwork2.inject.Context;
55  import com.opensymphony.xwork2.inject.Factory;
56  import com.opensymphony.xwork2.interceptor.Interceptor;
57  import com.opensymphony.xwork2.util.LocalizedTextUtil;
58  
59  /***
60   * Test case for Dispatcher.
61   *
62   */
63  public class DispatcherTest extends StrutsTestCase {
64  
65      public void testDefaultResurceBundlePropertyLoaded() throws Exception {
66          Locale.setDefault(Locale.US); // force to US locale as we also have _de and _da properties
67  
68          // some i18n messages from xwork-messages.properties
69          assertEquals(
70                  LocalizedTextUtil.findDefaultText("xwork.error.action.execution", Locale.US),
71                  "Error during Action invocation");
72  
73          // some i18n messages from struts-messages.properties
74          assertEquals(
75                  LocalizedTextUtil.findDefaultText("struts.messages.error.uploading", Locale.US,
76                          new Object[] { "some error messages" }),
77                  "Error uploading: some error messages");
78      }
79  
80      public void testPrepareSetEncodingProperly() throws Exception {
81          HttpServletRequest req = new MockHttpServletRequest();
82          HttpServletResponse res = new MockHttpServletResponse();
83  
84          Dispatcher du = initDispatcher(new HashMap() {{
85              put(StrutsConstants.STRUTS_I18N_ENCODING, "utf-8");
86          }});
87          du.prepare(req, res);
88  
89          assertEquals(req.getCharacterEncoding(), "utf-8");
90      }
91  
92      public void testPrepareSetEncodingPropertyWithMultipartRequest() throws Exception {
93          MockHttpServletRequest req = new MockHttpServletRequest();
94          MockHttpServletResponse res = new MockHttpServletResponse();
95  
96          req.setContentType("multipart/form-data");
97          Dispatcher du = initDispatcher(new HashMap() {{
98              put(StrutsConstants.STRUTS_I18N_ENCODING, "utf-8");
99          }});
100         du.prepare(req, res);
101 
102         assertEquals("utf-8", req.getCharacterEncoding());
103     }
104     
105     public void testDispatcherListener() throws Exception {
106     	
107     	final DispatcherListenerState state = new DispatcherListenerState();
108     	
109     	Dispatcher.addDispatcherListener(new DispatcherListener() {
110 			public void dispatcherDestroyed(Dispatcher du) {
111 				state.isDestroyed = true;
112 			}
113 			public void dispatcherInitialized(Dispatcher du) {
114 				state.isInitialized = true;
115 			}
116     	});
117     	
118     	
119     	assertFalse(state.isDestroyed);
120     	assertFalse(state.isInitialized);
121     	
122         Dispatcher du = initDispatcher(new HashMap<String, String>() );
123     	
124     	assertTrue(state.isInitialized);
125     	
126     	du.cleanup();
127     	
128     	assertTrue(state.isDestroyed);
129     }
130     
131     
132     public void testConfigurationManager() {
133     	Dispatcher du = null;
134     	InternalConfigurationManager configurationManager = new InternalConfigurationManager();
135     	try {
136     		du = new Dispatcher(new MockServletContext(), new HashMap<String, String>());
137     		du.setConfigurationManager(configurationManager);
138     		
139     		du.init();
140     		
141             Dispatcher.setInstance(du);
142             
143             assertFalse(configurationManager.destroyConfiguration);
144             
145             du.cleanup();
146             
147             assertTrue(configurationManager.destroyConfiguration);
148             
149     	}
150     	finally {
151     		du.setInstance(null);
152     	}
153     }
154     
155     public void testObjectFactoryDestroy() throws Exception {
156 
157         final InnerDestroyableObjectFactory destroyedObjectFactory = new InnerDestroyableObjectFactory();
158         Dispatcher du = new Dispatcher(new MockServletContext(), new HashMap<String, String>());
159         ConfigurationManager cm = new ConfigurationManager();
160         Mock mockConfiguration = new Mock(Configuration.class);
161         cm.setConfiguration((Configuration)mockConfiguration.proxy());
162         
163         Mock mockContainer = new Mock(Container.class);
164         mockConfiguration.expectAndReturn("getContainer", mockContainer.proxy());
165         mockContainer.expectAndReturn("getInstance", C.args(C.eq(ObjectFactory.class)), destroyedObjectFactory);
166         mockConfiguration.expect("destroy");
167         mockConfiguration.matchAndReturn("getPackageConfigs", new HashMap<String, PackageConfig>());
168         
169         du.setConfigurationManager(cm);
170         assertFalse(destroyedObjectFactory.destroyed);
171         du.cleanup();
172         assertTrue(destroyedObjectFactory.destroyed);
173         mockConfiguration.verify();
174         mockContainer.verify();
175     }
176     
177     public void testInterceptorDestroy() throws Exception {           
178         Mock mockInterceptor = new Mock(Interceptor.class);
179         mockInterceptor.matchAndReturn("hashCode", 0);
180         mockInterceptor.expect("destroy");
181         
182         InterceptorMapping interceptorMapping = new InterceptorMapping("test", (Interceptor) mockInterceptor.proxy());
183         
184         InterceptorStackConfig isc = new InterceptorStackConfig.Builder("test").addInterceptor(interceptorMapping).build();
185         
186         PackageConfig packageConfig = new PackageConfig.Builder("test").addInterceptorStackConfig(isc).build();
187         
188         Map<String, PackageConfig> packageConfigs = new HashMap<String, PackageConfig>();
189         packageConfigs.put("test", packageConfig);
190         
191         Mock mockContainer = new Mock(Container.class);
192         mockContainer.matchAndReturn("getInstance", C.args(C.eq(ObjectFactory.class)), new ObjectFactory());
193         
194         Mock mockConfiguration = new Mock(Configuration.class);
195         mockConfiguration.matchAndReturn("getPackageConfigs", packageConfigs);
196         mockConfiguration.matchAndReturn("getContainer", mockContainer.proxy());
197         mockConfiguration.expect("destroy");
198         
199         ConfigurationManager configurationManager = new ConfigurationManager();
200         configurationManager.setConfiguration((Configuration) mockConfiguration.proxy());
201         
202         Dispatcher dispatcher = new Dispatcher(new MockServletContext(), new HashMap<String, String>());
203         dispatcher.setConfigurationManager(configurationManager);
204         dispatcher.cleanup();
205         
206         mockInterceptor.verify();
207         mockContainer.verify();
208         mockConfiguration.verify();
209     }
210     
211     class InternalConfigurationManager extends ConfigurationManager {
212     	public boolean destroyConfiguration = false;
213     	
214     	@Override
215     	public synchronized void destroyConfiguration() {
216     		super.destroyConfiguration();
217     		destroyConfiguration = true;
218     	}
219     }
220     
221     
222     class DispatcherListenerState {
223     	public boolean isInitialized = false;
224     	public boolean isDestroyed = false;
225     }
226 }