View Javadoc

1   /*
2    * $Id: DispatcherTest.java 476696 2006-11-19 03:56:18Z tmjee $
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.dispatcher;
22  
23  import java.util.HashMap;
24  import java.util.Locale;
25  
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  
29  import org.apache.struts2.StrutsConstants;
30  import org.apache.struts2.StrutsTestCase;
31  import org.springframework.mock.web.MockHttpServletRequest;
32  import org.springframework.mock.web.MockHttpServletResponse;
33  import org.springframework.mock.web.MockServletContext;
34  
35  import com.opensymphony.xwork2.config.ConfigurationManager;
36  import com.opensymphony.xwork2.util.LocalizedTextUtil;
37  
38  /***
39   * Test case for Dispatcher.
40   *
41   */
42  public class DispatcherTest extends StrutsTestCase {
43  
44      public void testDefaultResurceBundlePropertyLoaded() throws Exception {
45          Locale.setDefault(Locale.US); // force to US locale as we also have _de and _da properties
46  
47          // some i18n messages from xwork-messages.properties
48          assertEquals(
49                  LocalizedTextUtil.findDefaultText("xwork.error.action.execution", Locale.US),
50                  "Error during Action invocation");
51  
52          // some i18n messages from struts-messages.properties
53          assertEquals(
54                  LocalizedTextUtil.findDefaultText("struts.messages.error.uploading", Locale.US,
55                          new Object[] { "some error messages" }),
56                  "Error uploading: some error messages");
57      }
58  
59      public void testPrepareSetEncodingProperly() throws Exception {
60          HttpServletRequest req = new MockHttpServletRequest();
61          HttpServletResponse res = new MockHttpServletResponse();
62  
63          Dispatcher du = initDispatcher(new HashMap() {{
64              put(StrutsConstants.STRUTS_I18N_ENCODING, "utf-8");
65          }});
66          du.prepare(req, res);
67  
68          assertEquals(req.getCharacterEncoding(), "utf-8");
69      }
70  
71      public void testPrepareSetEncodingPropertyWithMultipartRequest() throws Exception {
72          MockHttpServletRequest req = new MockHttpServletRequest();
73          MockHttpServletResponse res = new MockHttpServletResponse();
74  
75          req.setContentType("multipart/form-data");
76          Dispatcher du = initDispatcher(new HashMap() {{
77              put(StrutsConstants.STRUTS_I18N_ENCODING, "utf-8");
78          }});
79          du.prepare(req, res);
80  
81          assertEquals("utf-8", req.getCharacterEncoding());
82      }
83      
84      public void testDispatcherListener() throws Exception {
85      	
86      	final DispatcherListenerState state = new DispatcherListenerState();
87      	
88      	Dispatcher.addDispatcherListener(new DispatcherListener() {
89  			public void dispatcherDestroyed(Dispatcher du) {
90  				state.isDestroyed = true;
91  			}
92  			public void dispatcherInitialized(Dispatcher du) {
93  				state.isInitialized = true;
94  			}
95      	});
96      	
97      	
98      	assertFalse(state.isDestroyed);
99      	assertFalse(state.isInitialized);
100     	
101         Dispatcher du = initDispatcher(new HashMap<String, String>() );
102     	
103     	assertTrue(state.isInitialized);
104     	
105     	du.cleanup();
106     	
107     	assertTrue(state.isDestroyed);
108     }
109     
110     
111     public void testConfigurationManager() {
112     	Dispatcher du = null;
113     	InternalConfigurationManager configurationManager = new InternalConfigurationManager();
114     	try {
115     		du = new Dispatcher(new MockServletContext(), new HashMap<String, String>());
116     		du.setConfigurationManager(configurationManager);
117     		
118     		du.init();
119     		
120             Dispatcher.setInstance(du);
121             
122             assertFalse(configurationManager.destroyConfiguration);
123             
124             du.cleanup();
125             
126             assertTrue(configurationManager.destroyConfiguration);
127             
128     	}
129     	finally {
130     		du.setInstance(null);
131     	}
132     }
133     
134     class InternalConfigurationManager extends ConfigurationManager {
135     	public boolean destroyConfiguration = false;
136     	
137     	@Override
138     	public synchronized void destroyConfiguration() {
139     		super.destroyConfiguration();
140     		destroyConfiguration = true;
141     	}
142     }
143     
144     
145     class DispatcherListenerState {
146     	public boolean isInitialized = false;
147     	public boolean isDestroyed = false;
148     }
149 }