001: /*
002: * $Id: DispatcherTest.java 476696 2006-11-19 03:56:18Z tmjee $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.dispatcher;
022:
023: import java.util.HashMap;
024: import java.util.Locale;
025:
026: import javax.servlet.http.HttpServletRequest;
027: import javax.servlet.http.HttpServletResponse;
028:
029: import org.apache.struts2.StrutsConstants;
030: import org.apache.struts2.StrutsTestCase;
031: import org.springframework.mock.web.MockHttpServletRequest;
032: import org.springframework.mock.web.MockHttpServletResponse;
033: import org.springframework.mock.web.MockServletContext;
034:
035: import com.opensymphony.xwork2.config.ConfigurationManager;
036: import com.opensymphony.xwork2.util.LocalizedTextUtil;
037:
038: /**
039: * Test case for Dispatcher.
040: *
041: */
042: public class DispatcherTest extends StrutsTestCase {
043:
044: public void testDefaultResurceBundlePropertyLoaded()
045: throws Exception {
046: Locale.setDefault(Locale.US); // force to US locale as we also have _de and _da properties
047:
048: // some i18n messages from xwork-messages.properties
049: assertEquals(LocalizedTextUtil.findDefaultText(
050: "xwork.error.action.execution", Locale.US),
051: "Error during Action invocation");
052:
053: // some i18n messages from struts-messages.properties
054: assertEquals(LocalizedTextUtil.findDefaultText(
055: "struts.messages.error.uploading", Locale.US,
056: new Object[] { "some error messages" }),
057: "Error uploading: some error messages");
058: }
059:
060: public void testPrepareSetEncodingProperly() throws Exception {
061: HttpServletRequest req = new MockHttpServletRequest();
062: HttpServletResponse res = new MockHttpServletResponse();
063:
064: Dispatcher du = initDispatcher(new HashMap() {
065: {
066: put(StrutsConstants.STRUTS_I18N_ENCODING, "utf-8");
067: }
068: });
069: du.prepare(req, res);
070:
071: assertEquals(req.getCharacterEncoding(), "utf-8");
072: }
073:
074: public void testPrepareSetEncodingPropertyWithMultipartRequest()
075: throws Exception {
076: MockHttpServletRequest req = new MockHttpServletRequest();
077: MockHttpServletResponse res = new MockHttpServletResponse();
078:
079: req.setContentType("multipart/form-data");
080: Dispatcher du = initDispatcher(new HashMap() {
081: {
082: put(StrutsConstants.STRUTS_I18N_ENCODING, "utf-8");
083: }
084: });
085: du.prepare(req, res);
086:
087: assertEquals("utf-8", req.getCharacterEncoding());
088: }
089:
090: public void testDispatcherListener() throws Exception {
091:
092: final DispatcherListenerState state = new DispatcherListenerState();
093:
094: Dispatcher.addDispatcherListener(new DispatcherListener() {
095: public void dispatcherDestroyed(Dispatcher du) {
096: state.isDestroyed = true;
097: }
098:
099: public void dispatcherInitialized(Dispatcher du) {
100: state.isInitialized = true;
101: }
102: });
103:
104: assertFalse(state.isDestroyed);
105: assertFalse(state.isInitialized);
106:
107: Dispatcher du = initDispatcher(new HashMap<String, String>());
108:
109: assertTrue(state.isInitialized);
110:
111: du.cleanup();
112:
113: assertTrue(state.isDestroyed);
114: }
115:
116: public void testConfigurationManager() {
117: Dispatcher du = null;
118: InternalConfigurationManager configurationManager = new InternalConfigurationManager();
119: try {
120: du = new Dispatcher(new MockServletContext(),
121: new HashMap<String, String>());
122: du.setConfigurationManager(configurationManager);
123:
124: du.init();
125:
126: Dispatcher.setInstance(du);
127:
128: assertFalse(configurationManager.destroyConfiguration);
129:
130: du.cleanup();
131:
132: assertTrue(configurationManager.destroyConfiguration);
133:
134: } finally {
135: du.setInstance(null);
136: }
137: }
138:
139: class InternalConfigurationManager extends ConfigurationManager {
140: public boolean destroyConfiguration = false;
141:
142: @Override
143: public synchronized void destroyConfiguration() {
144: super .destroyConfiguration();
145: destroyConfiguration = true;
146: }
147: }
148:
149: class DispatcherListenerState {
150: public boolean isInitialized = false;
151: public boolean isDestroyed = false;
152: }
153: }
|