View Javadoc

1   /*
2    * $Id: DispatcherTest.java 439747 2006-09-03 09:22:46Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.dispatcher;
19  
20  import java.util.Locale;
21  
22  import javax.servlet.http.HttpServletRequest;
23  import javax.servlet.http.HttpServletResponse;
24  
25  import org.apache.struts2.StrutsConstants;
26  import org.apache.struts2.StrutsTestCase;
27  import org.apache.struts2.config.Settings;
28  import org.springframework.mock.web.MockHttpServletRequest;
29  import org.springframework.mock.web.MockHttpServletResponse;
30  
31  import com.opensymphony.xwork2.util.LocalizedTextUtil;
32  
33  /***
34   * Test case for Dispatcher.
35   * 
36   */
37  public class DispatcherTest extends StrutsTestCase {
38  
39  	public void testDefaultResurceBundlePropertyLoaded() throws Exception {
40          Locale.setDefault(Locale.US); // force to US locale as we also have _de and _da properties
41  		
42  		// some i18n messages from xwork-messages.properties
43  		assertEquals(
44  				LocalizedTextUtil.findDefaultText("xwork.error.action.execution", Locale.US), 
45  				"Error during Action invocation");
46  		
47  		// some i18n messages from struts-messages.properties
48  		assertEquals(
49  				LocalizedTextUtil.findDefaultText("struts.messages.error.uploading", Locale.US, 
50  						new Object[] { "some error messages" }), 
51  				"Error uploading: some error messages");
52  	}
53  	
54  	public void testPrepareSetEncodingProperly() throws Exception {
55  		HttpServletRequest req = new MockHttpServletRequest();
56  		HttpServletResponse res = new MockHttpServletResponse();
57  		
58  		Settings.set(StrutsConstants.STRUTS_I18N_ENCODING, "utf-8");
59  		
60  		
61  		Dispatcher du = Dispatcher.getInstance();
62  		du.prepare(req, res);
63  		
64  		assertEquals(req.getCharacterEncoding(), "utf-8");
65  	}
66  	
67  	public void testPrepareSetEncodingPropertyWithMultipartRequest() throws Exception {
68  		MockHttpServletRequest req = new MockHttpServletRequest();
69  		MockHttpServletResponse res = new MockHttpServletResponse();
70  		
71  		req.setContentType("multipart/form-data");
72  		Settings.set(StrutsConstants.STRUTS_I18N_ENCODING, "utf-8");
73  		
74  		
75  		Dispatcher du = Dispatcher.getInstance();
76  		du.prepare(req, res);
77  		
78  		assertEquals(req.getCharacterEncoding(), "utf-8");
79  	}
80  }