View Javadoc

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