|
|
|
@@ -18,27 +18,29 @@
|
|
|
|
|
*/
|
|
|
|
|
package org.apache.struts2.dispatcher;
|
|
|
|
|
|
|
|
|
|
import com.mockobjects.dynamic.C;
|
|
|
|
|
import com.mockobjects.dynamic.Mock;
|
|
|
|
|
import com.opensymphony.xwork2.ActionContext;
|
|
|
|
|
import com.opensymphony.xwork2.LocalizedTextProvider;
|
|
|
|
|
import com.opensymphony.xwork2.ObjectFactory;
|
|
|
|
|
import com.opensymphony.xwork2.StubValueStack;
|
|
|
|
|
import com.opensymphony.xwork2.config.Configuration;
|
|
|
|
|
import com.opensymphony.xwork2.config.ConfigurationException;
|
|
|
|
|
import com.opensymphony.xwork2.config.ConfigurationManager;
|
|
|
|
|
import com.opensymphony.xwork2.config.entities.InterceptorMapping;
|
|
|
|
|
import com.opensymphony.xwork2.config.entities.InterceptorStackConfig;
|
|
|
|
|
import com.opensymphony.xwork2.config.entities.PackageConfig;
|
|
|
|
|
import com.opensymphony.xwork2.inject.Container;
|
|
|
|
|
import com.opensymphony.xwork2.inject.ContainerBuilder;
|
|
|
|
|
import com.opensymphony.xwork2.interceptor.Interceptor;
|
|
|
|
|
import com.opensymphony.xwork2.mock.MockActionInvocation;
|
|
|
|
|
import com.opensymphony.xwork2.mock.MockActionProxy;
|
|
|
|
|
import com.opensymphony.xwork2.test.StubConfigurationProvider;
|
|
|
|
|
import com.opensymphony.xwork2.util.location.LocatableProperties;
|
|
|
|
|
import org.apache.struts2.ServletActionContext;
|
|
|
|
|
import org.apache.struts2.StrutsConstants;
|
|
|
|
|
import org.apache.struts2.StrutsInternalTestCase;
|
|
|
|
|
import org.apache.struts2.StrutsJUnit4InternalTestCase;
|
|
|
|
|
import org.apache.struts2.dispatcher.mapper.ActionMapping;
|
|
|
|
|
import org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper;
|
|
|
|
|
import org.apache.struts2.util.ObjectFactoryDestroyable;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
import org.springframework.mock.web.MockHttpServletRequest;
|
|
|
|
|
import org.springframework.mock.web.MockHttpServletResponse;
|
|
|
|
|
import org.springframework.mock.web.MockHttpSession;
|
|
|
|
@@ -46,18 +48,35 @@ import org.springframework.mock.web.MockServletContext;
|
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
import java.util.Locale;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
|
|
import static java.nio.charset.StandardCharsets.UTF_8;
|
|
|
|
|
import static java.util.Collections.emptyMap;
|
|
|
|
|
import static java.util.Collections.singletonMap;
|
|
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
|
import static org.junit.Assert.assertFalse;
|
|
|
|
|
import static org.junit.Assert.assertNotNull;
|
|
|
|
|
import static org.junit.Assert.assertTrue;
|
|
|
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
|
|
|
import static org.mockito.ArgumentMatchers.anyBoolean;
|
|
|
|
|
import static org.mockito.Mockito.doReturn;
|
|
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
|
|
import static org.mockito.Mockito.never;
|
|
|
|
|
import static org.mockito.Mockito.spy;
|
|
|
|
|
import static org.mockito.Mockito.verify;
|
|
|
|
|
import static org.mockito.Mockito.when;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test case for Dispatcher.
|
|
|
|
|
*/
|
|
|
|
|
public class DispatcherTest extends StrutsInternalTestCase {
|
|
|
|
|
public class DispatcherTest extends StrutsJUnit4InternalTestCase {
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testDefaultResourceBundlePropertyLoaded() {
|
|
|
|
|
LocalizedTextProvider localizedTextProvider = container.getInstance(LocalizedTextProvider.class);
|
|
|
|
|
|
|
|
|
@@ -71,115 +90,107 @@ public class DispatcherTest extends StrutsInternalTestCase {
|
|
|
|
|
"Error uploading: some error messages");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testPrepareSetEncodingProperly() {
|
|
|
|
|
HttpServletRequest req = new MockHttpServletRequest();
|
|
|
|
|
HttpServletResponse res = new MockHttpServletResponse();
|
|
|
|
|
|
|
|
|
|
Dispatcher du = initDispatcher(new HashMap<String, String>() {{
|
|
|
|
|
put(StrutsConstants.STRUTS_I18N_ENCODING, "utf-8");
|
|
|
|
|
}});
|
|
|
|
|
du.prepare(req, res);
|
|
|
|
|
initDispatcher(singletonMap(StrutsConstants.STRUTS_I18N_ENCODING, UTF_8.name()));
|
|
|
|
|
dispatcher.prepare(req, res);
|
|
|
|
|
|
|
|
|
|
assertEquals(req.getCharacterEncoding(), "utf-8");
|
|
|
|
|
assertEquals(res.getCharacterEncoding(), "utf-8");
|
|
|
|
|
assertEquals(req.getCharacterEncoding(), UTF_8.name());
|
|
|
|
|
assertEquals(res.getCharacterEncoding(), UTF_8.name());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testEncodingForXMLHttpRequest() {
|
|
|
|
|
// given
|
|
|
|
|
MockHttpServletRequest req = new MockHttpServletRequest();
|
|
|
|
|
req.addHeader("X-Requested-With", "XMLHttpRequest");
|
|
|
|
|
req.setCharacterEncoding("UTF-8");
|
|
|
|
|
req.setCharacterEncoding(UTF_8.name());
|
|
|
|
|
HttpServletResponse res = new MockHttpServletResponse();
|
|
|
|
|
|
|
|
|
|
Dispatcher du = initDispatcher(new HashMap<String, String>() {{
|
|
|
|
|
put(StrutsConstants.STRUTS_I18N_ENCODING, "latin-2");
|
|
|
|
|
}});
|
|
|
|
|
initDispatcher(singletonMap(StrutsConstants.STRUTS_I18N_ENCODING, StandardCharsets.ISO_8859_1.name()));
|
|
|
|
|
|
|
|
|
|
// when
|
|
|
|
|
du.prepare(req, res);
|
|
|
|
|
dispatcher.prepare(req, res);
|
|
|
|
|
|
|
|
|
|
// then
|
|
|
|
|
assertEquals(req.getCharacterEncoding(), "UTF-8");
|
|
|
|
|
assertEquals(res.getCharacterEncoding(), "UTF-8");
|
|
|
|
|
assertEquals(req.getCharacterEncoding(), UTF_8.name());
|
|
|
|
|
assertEquals(res.getCharacterEncoding(), UTF_8.name());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testSetEncodingIfDiffer() {
|
|
|
|
|
// given
|
|
|
|
|
Mock mock = new Mock(HttpServletRequest.class);
|
|
|
|
|
mock.expectAndReturn("getCharacterEncoding", "utf-8");
|
|
|
|
|
mock.expectAndReturn("getHeader", "X-Requested-With", "");
|
|
|
|
|
mock.expectAndReturn("getCharacterEncoding", "utf-8");
|
|
|
|
|
HttpServletRequest req = (HttpServletRequest) mock.proxy();
|
|
|
|
|
HttpServletRequest req = mock(HttpServletRequest.class);
|
|
|
|
|
when(req.getCharacterEncoding()).thenReturn(UTF_8.name());
|
|
|
|
|
when(req.getHeader("X-Requested-With")).thenReturn("");
|
|
|
|
|
HttpServletResponse res = new MockHttpServletResponse();
|
|
|
|
|
|
|
|
|
|
Dispatcher du = initDispatcher(new HashMap<String, String>() {{
|
|
|
|
|
put(StrutsConstants.STRUTS_I18N_ENCODING, "utf-8");
|
|
|
|
|
}});
|
|
|
|
|
|
|
|
|
|
initDispatcher(singletonMap(StrutsConstants.STRUTS_I18N_ENCODING, UTF_8.name()));
|
|
|
|
|
|
|
|
|
|
// when
|
|
|
|
|
du.prepare(req, res);
|
|
|
|
|
dispatcher.prepare(req, res);
|
|
|
|
|
|
|
|
|
|
// then
|
|
|
|
|
|
|
|
|
|
assertEquals(req.getCharacterEncoding(), "utf-8");
|
|
|
|
|
assertEquals(res.getCharacterEncoding(), "utf-8");
|
|
|
|
|
mock.verify();
|
|
|
|
|
assertEquals(UTF_8.name(), req.getCharacterEncoding());
|
|
|
|
|
assertEquals(UTF_8.name(), res.getCharacterEncoding());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testPrepareSetEncodingPropertyWithMultipartRequest() {
|
|
|
|
|
MockHttpServletRequest req = new MockHttpServletRequest();
|
|
|
|
|
MockHttpServletResponse res = new MockHttpServletResponse();
|
|
|
|
|
|
|
|
|
|
req.setContentType("multipart/form-data");
|
|
|
|
|
Dispatcher du = initDispatcher(new HashMap<String, String>() {{
|
|
|
|
|
put(StrutsConstants.STRUTS_I18N_ENCODING, "utf-8");
|
|
|
|
|
}});
|
|
|
|
|
du.prepare(req, res);
|
|
|
|
|
initDispatcher(singletonMap(StrutsConstants.STRUTS_I18N_ENCODING, UTF_8.name()));
|
|
|
|
|
dispatcher.prepare(req, res);
|
|
|
|
|
|
|
|
|
|
assertEquals("utf-8", req.getCharacterEncoding());
|
|
|
|
|
assertEquals("utf-8", res.getCharacterEncoding());
|
|
|
|
|
assertEquals(UTF_8.name(), req.getCharacterEncoding());
|
|
|
|
|
assertEquals(UTF_8.name(), res.getCharacterEncoding());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testPrepareMultipartRequest() throws Exception {
|
|
|
|
|
MockHttpServletRequest req = new MockHttpServletRequest();
|
|
|
|
|
MockHttpServletResponse res = new MockHttpServletResponse();
|
|
|
|
|
|
|
|
|
|
req.setMethod("post");
|
|
|
|
|
req.setContentType("multipart/form-data; boundary=asdcvb345asd");
|
|
|
|
|
Dispatcher du = initDispatcher(Collections.emptyMap());
|
|
|
|
|
du.prepare(req, res);
|
|
|
|
|
HttpServletRequest wrapped = du.wrapRequest(req);
|
|
|
|
|
|
|
|
|
|
assertTrue(wrapped instanceof MultiPartRequestWrapper);
|
|
|
|
|
dispatcher.prepare(req, res);
|
|
|
|
|
|
|
|
|
|
assertTrue(dispatcher.wrapRequest(req) instanceof MultiPartRequestWrapper);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testPrepareMultipartRequestAllAllowedCharacters() throws Exception {
|
|
|
|
|
MockHttpServletRequest req = new MockHttpServletRequest();
|
|
|
|
|
MockHttpServletResponse res = new MockHttpServletResponse();
|
|
|
|
|
|
|
|
|
|
req.setMethod("post");
|
|
|
|
|
req.setContentType("multipart/form-data; boundary=01=23a.bC:D((e)d'z?p+o_r,e-");
|
|
|
|
|
Dispatcher du = initDispatcher(Collections.emptyMap());
|
|
|
|
|
du.prepare(req, res);
|
|
|
|
|
HttpServletRequest wrapped = du.wrapRequest(req);
|
|
|
|
|
|
|
|
|
|
assertTrue(wrapped instanceof MultiPartRequestWrapper);
|
|
|
|
|
dispatcher.prepare(req, res);
|
|
|
|
|
|
|
|
|
|
assertTrue(dispatcher.wrapRequest(req) instanceof MultiPartRequestWrapper);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testPrepareMultipartRequestIllegalCharacter() throws Exception {
|
|
|
|
|
MockHttpServletRequest req = new MockHttpServletRequest();
|
|
|
|
|
MockHttpServletResponse res = new MockHttpServletResponse();
|
|
|
|
|
|
|
|
|
|
req.setMethod("post");
|
|
|
|
|
req.setContentType("multipart/form-data; boundary=01=2;3a.bC:D((e)d'z?p+o_r,e-");
|
|
|
|
|
Dispatcher du = initDispatcher(Collections.emptyMap());
|
|
|
|
|
du.prepare(req, res);
|
|
|
|
|
HttpServletRequest wrapped = du.wrapRequest(req);
|
|
|
|
|
|
|
|
|
|
assertFalse(wrapped instanceof MultiPartRequestWrapper);
|
|
|
|
|
dispatcher.prepare(req, res);
|
|
|
|
|
|
|
|
|
|
assertFalse(dispatcher.wrapRequest(req) instanceof MultiPartRequestWrapper);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testDispatcherListener() {
|
|
|
|
|
|
|
|
|
|
final DispatcherListenerState state = new DispatcherListenerState();
|
|
|
|
@@ -198,39 +209,32 @@ public class DispatcherTest extends StrutsInternalTestCase {
|
|
|
|
|
assertFalse(state.isDestroyed);
|
|
|
|
|
assertFalse(state.isInitialized);
|
|
|
|
|
|
|
|
|
|
Dispatcher du = initDispatcher(new HashMap<>());
|
|
|
|
|
dispatcher.init();
|
|
|
|
|
|
|
|
|
|
assertTrue(state.isInitialized);
|
|
|
|
|
|
|
|
|
|
du.cleanup();
|
|
|
|
|
dispatcher.cleanup();
|
|
|
|
|
|
|
|
|
|
assertTrue(state.isDestroyed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testConfigurationManager() {
|
|
|
|
|
Dispatcher du;
|
|
|
|
|
final InternalConfigurationManager configurationManager = new InternalConfigurationManager(Container.DEFAULT_NAME);
|
|
|
|
|
try {
|
|
|
|
|
du = new MockDispatcher(new MockServletContext(), new HashMap<>(), configurationManager);
|
|
|
|
|
du.init();
|
|
|
|
|
Dispatcher.setInstance(du);
|
|
|
|
|
configurationManager = spy(new ConfigurationManager(Container.DEFAULT_NAME));
|
|
|
|
|
dispatcher = spyDispatcherWithConfigurationManager(new Dispatcher(new MockServletContext(), emptyMap()), configurationManager);
|
|
|
|
|
|
|
|
|
|
assertFalse(configurationManager.destroyConfiguration);
|
|
|
|
|
dispatcher.init();
|
|
|
|
|
|
|
|
|
|
du.cleanup();
|
|
|
|
|
verify(configurationManager, never()).destroyConfiguration();
|
|
|
|
|
|
|
|
|
|
assertTrue(configurationManager.destroyConfiguration);
|
|
|
|
|
dispatcher.cleanup();
|
|
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
Dispatcher.setInstance(null);
|
|
|
|
|
}
|
|
|
|
|
verify(configurationManager).destroyConfiguration();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testInitLoadsDefaultConfig() {
|
|
|
|
|
Dispatcher du = new Dispatcher(new MockServletContext(), new HashMap<>());
|
|
|
|
|
du.init();
|
|
|
|
|
Configuration config = du.getConfigurationManager().getConfiguration();
|
|
|
|
|
assertNotNull(config);
|
|
|
|
|
assertNotNull(configuration);
|
|
|
|
|
Set<String> expected = new HashSet<>();
|
|
|
|
|
expected.add("struts-default.xml");
|
|
|
|
|
expected.add("struts-beans.xml");
|
|
|
|
@@ -238,135 +242,113 @@ public class DispatcherTest extends StrutsInternalTestCase {
|
|
|
|
|
expected.add("struts-plugin.xml");
|
|
|
|
|
expected.add("struts.xml");
|
|
|
|
|
expected.add("struts-deferred.xml");
|
|
|
|
|
assertEquals(expected, config.getLoadedFileNames());
|
|
|
|
|
assertTrue(config.getPackageConfigs().size() > 0);
|
|
|
|
|
PackageConfig packageConfig = config.getPackageConfig("struts-default");
|
|
|
|
|
assertTrue(packageConfig.getInterceptorConfigs().size() > 0);
|
|
|
|
|
assertTrue(packageConfig.getResultTypeConfigs().size() > 0);
|
|
|
|
|
assertEquals(expected, configuration.getLoadedFileNames());
|
|
|
|
|
assertFalse(configuration.getPackageConfigs().isEmpty());
|
|
|
|
|
PackageConfig packageConfig = configuration.getPackageConfig("struts-default");
|
|
|
|
|
assertFalse(packageConfig.getInterceptorConfigs().isEmpty());
|
|
|
|
|
assertFalse(packageConfig.getResultTypeConfigs().isEmpty());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testObjectFactoryDestroy() {
|
|
|
|
|
dispatcher = spy(dispatcher);
|
|
|
|
|
Container spiedContainer = spy(container);
|
|
|
|
|
doReturn(spiedContainer).when(dispatcher).getContainer();
|
|
|
|
|
|
|
|
|
|
ConfigurationManager cm = new ConfigurationManager(Container.DEFAULT_NAME);
|
|
|
|
|
Dispatcher du = new MockDispatcher(new MockServletContext(), new HashMap<>(), cm);
|
|
|
|
|
Mock mockConfiguration = new Mock(Configuration.class);
|
|
|
|
|
cm.setConfiguration((Configuration) mockConfiguration.proxy());
|
|
|
|
|
InnerDestroyableObjectFactory destroyedObjectFactory = new InnerDestroyableObjectFactory();
|
|
|
|
|
doReturn(destroyedObjectFactory).when(spiedContainer).getInstance(ObjectFactory.class);
|
|
|
|
|
|
|
|
|
|
Mock mockContainer = new Mock(Container.class);
|
|
|
|
|
final InnerDestroyableObjectFactory destroyedObjectFactory = new InnerDestroyableObjectFactory();
|
|
|
|
|
destroyedObjectFactory.setContainer((Container) mockContainer.proxy());
|
|
|
|
|
mockContainer.expectAndReturn("getInstance", C.args(C.eq(ObjectFactory.class)), destroyedObjectFactory);
|
|
|
|
|
|
|
|
|
|
mockConfiguration.expectAndReturn("getContainer", mockContainer.proxy());
|
|
|
|
|
mockConfiguration.expect("destroy");
|
|
|
|
|
mockConfiguration.matchAndReturn("getPackageConfigs", new HashMap<String, PackageConfig>());
|
|
|
|
|
|
|
|
|
|
du.init();
|
|
|
|
|
assertFalse(destroyedObjectFactory.destroyed);
|
|
|
|
|
du.cleanup();
|
|
|
|
|
dispatcher.cleanup();
|
|
|
|
|
assertTrue(destroyedObjectFactory.destroyed);
|
|
|
|
|
mockConfiguration.verify();
|
|
|
|
|
mockContainer.verify();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testInterceptorDestroy() {
|
|
|
|
|
Mock mockInterceptor = new Mock(Interceptor.class);
|
|
|
|
|
mockInterceptor.matchAndReturn("hashCode", 0);
|
|
|
|
|
mockInterceptor.expect("destroy");
|
|
|
|
|
|
|
|
|
|
InterceptorMapping interceptorMapping = new InterceptorMapping("test", (Interceptor) mockInterceptor.proxy());
|
|
|
|
|
|
|
|
|
|
Interceptor mockedInterceptor = mock(Interceptor.class);
|
|
|
|
|
InterceptorMapping interceptorMapping = new InterceptorMapping("test", mockedInterceptor);
|
|
|
|
|
InterceptorStackConfig isc = new InterceptorStackConfig.Builder("test").addInterceptor(interceptorMapping).build();
|
|
|
|
|
|
|
|
|
|
PackageConfig packageConfig = new PackageConfig.Builder("test").addInterceptorStackConfig(isc).build();
|
|
|
|
|
|
|
|
|
|
Map<String, PackageConfig> packageConfigs = new HashMap<>();
|
|
|
|
|
packageConfigs.put("test", packageConfig);
|
|
|
|
|
configurationManager = spy(new ConfigurationManager(Container.DEFAULT_NAME));
|
|
|
|
|
dispatcher = spyDispatcherWithConfigurationManager(new Dispatcher(new MockServletContext(), emptyMap()), configurationManager);
|
|
|
|
|
|
|
|
|
|
Mock mockContainer = new Mock(Container.class);
|
|
|
|
|
mockContainer.matchAndReturn("getInstance", C.args(C.eq(ObjectFactory.class)), new ObjectFactory());
|
|
|
|
|
|
|
|
|
|
Mock mockConfiguration = new Mock(Configuration.class);
|
|
|
|
|
mockConfiguration.matchAndReturn("getPackageConfigs", packageConfigs);
|
|
|
|
|
mockConfiguration.matchAndReturn("getContainer", mockContainer.proxy());
|
|
|
|
|
mockConfiguration.expect("destroy");
|
|
|
|
|
|
|
|
|
|
ConfigurationManager configurationManager = new ConfigurationManager(Container.DEFAULT_NAME);
|
|
|
|
|
configurationManager.setConfiguration((Configuration) mockConfiguration.proxy());
|
|
|
|
|
|
|
|
|
|
Dispatcher dispatcher = new MockDispatcher(new MockServletContext(), new HashMap<>(), configurationManager);
|
|
|
|
|
dispatcher.init();
|
|
|
|
|
|
|
|
|
|
configuration = spy(configurationManager.getConfiguration());
|
|
|
|
|
configurationManager.setConfiguration(configuration);
|
|
|
|
|
when(configuration.getPackageConfigs()).thenReturn(singletonMap("test", packageConfig));
|
|
|
|
|
|
|
|
|
|
dispatcher.cleanup();
|
|
|
|
|
|
|
|
|
|
mockInterceptor.verify();
|
|
|
|
|
mockContainer.verify();
|
|
|
|
|
mockConfiguration.verify();
|
|
|
|
|
verify(mockedInterceptor).destroy();
|
|
|
|
|
verify(configuration).destroy();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testMultipartSupportEnabledByDefault() {
|
|
|
|
|
HttpServletRequest req = new MockHttpServletRequest();
|
|
|
|
|
HttpServletResponse res = new MockHttpServletResponse();
|
|
|
|
|
|
|
|
|
|
Dispatcher du = initDispatcher(Collections.emptyMap());
|
|
|
|
|
du.prepare(req, res);
|
|
|
|
|
dispatcher.prepare(req, res);
|
|
|
|
|
|
|
|
|
|
assertTrue(du.isMultipartSupportEnabled(req));
|
|
|
|
|
assertTrue(dispatcher.isMultipartSupportEnabled(req));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testIsMultipartRequest() {
|
|
|
|
|
MockHttpServletRequest req = new MockHttpServletRequest();
|
|
|
|
|
HttpServletResponse res = new MockHttpServletResponse();
|
|
|
|
|
|
|
|
|
|
req.setMethod("POST");
|
|
|
|
|
Dispatcher du = initDispatcher(Collections.emptyMap());
|
|
|
|
|
du.prepare(req, res);
|
|
|
|
|
|
|
|
|
|
dispatcher.prepare(req, res);
|
|
|
|
|
|
|
|
|
|
req.setContentType("multipart/form-data");
|
|
|
|
|
assertTrue(du.isMultipartRequest(req));
|
|
|
|
|
assertTrue(dispatcher.isMultipartRequest(req));
|
|
|
|
|
|
|
|
|
|
req.setContentType("multipart/form-data; boundary=---------------------------207103069210263");
|
|
|
|
|
assertTrue(du.isMultipartRequest(req));
|
|
|
|
|
assertTrue(dispatcher.isMultipartRequest(req));
|
|
|
|
|
|
|
|
|
|
req.setContentType("multipart/form-data; boundary=---------------------------207103069210263;charset=UTF-8");
|
|
|
|
|
assertTrue(du.isMultipartRequest(req));
|
|
|
|
|
assertTrue(dispatcher.isMultipartRequest(req));
|
|
|
|
|
|
|
|
|
|
req.setContentType("multipart/form-data; boundary=---------------------------207103069210263;charset=ISO-8859-1");
|
|
|
|
|
assertTrue(du.isMultipartRequest(req));
|
|
|
|
|
assertTrue(dispatcher.isMultipartRequest(req));
|
|
|
|
|
|
|
|
|
|
req.setContentType("multipart/form-data; boundary=---------------------------207103069210263;charset=Windows-1250");
|
|
|
|
|
assertTrue(du.isMultipartRequest(req));
|
|
|
|
|
assertTrue(dispatcher.isMultipartRequest(req));
|
|
|
|
|
|
|
|
|
|
req.setContentType("multipart/form-data; boundary=---------------------------207103069210263;charset=US-ASCII");
|
|
|
|
|
assertTrue(du.isMultipartRequest(req));
|
|
|
|
|
assertTrue(dispatcher.isMultipartRequest(req));
|
|
|
|
|
|
|
|
|
|
req.setContentType("multipart/form-data; boundary=---------------------------207103069210263;charset=UTF-16LE");
|
|
|
|
|
assertTrue(du.isMultipartRequest(req));
|
|
|
|
|
assertTrue(dispatcher.isMultipartRequest(req));
|
|
|
|
|
|
|
|
|
|
req.setContentType("multipart/form-data;boundary=---------------------------207103069210263;charset=UTF-16LE");
|
|
|
|
|
assertTrue(du.isMultipartRequest(req));
|
|
|
|
|
assertTrue(dispatcher.isMultipartRequest(req));
|
|
|
|
|
|
|
|
|
|
req.setContentType("multipart/form-data;boundary=---------------------------207103069210263; charset=UTF-16LE");
|
|
|
|
|
assertTrue(du.isMultipartRequest(req));
|
|
|
|
|
assertTrue(dispatcher.isMultipartRequest(req));
|
|
|
|
|
|
|
|
|
|
req.setContentType("multipart/form-data;boundary=---------------------------207103069210263 ;charset=UTF-16LE");
|
|
|
|
|
assertTrue(du.isMultipartRequest(req));
|
|
|
|
|
assertTrue(dispatcher.isMultipartRequest(req));
|
|
|
|
|
|
|
|
|
|
req.setContentType("multipart/form-data;boundary=---------------------------207103069210263 ; charset=UTF-16LE");
|
|
|
|
|
assertTrue(du.isMultipartRequest(req));
|
|
|
|
|
assertTrue(dispatcher.isMultipartRequest(req));
|
|
|
|
|
|
|
|
|
|
req.setContentType("multipart/form-data ;boundary=---------------------------207103069210263;charset=UTF-16LE");
|
|
|
|
|
assertTrue(du.isMultipartRequest(req));
|
|
|
|
|
assertTrue(dispatcher.isMultipartRequest(req));
|
|
|
|
|
|
|
|
|
|
req.setContentType("multipart/form-data ; boundary=---------------------------207103069210263;charset=UTF-16LE");
|
|
|
|
|
assertTrue(du.isMultipartRequest(req));
|
|
|
|
|
assertTrue(dispatcher.isMultipartRequest(req));
|
|
|
|
|
|
|
|
|
|
req.setContentType("Multipart/Form-Data ; boundary=---------------------------207103069210263;charset=UTF-16LE");
|
|
|
|
|
assertTrue(du.isMultipartRequest(req));
|
|
|
|
|
assertTrue(dispatcher.isMultipartRequest(req));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testServiceActionResumePreviousProxy() throws Exception {
|
|
|
|
|
Dispatcher du = initDispatcher(Collections.emptyMap());
|
|
|
|
|
|
|
|
|
|
MockActionInvocation mai = new MockActionInvocation();
|
|
|
|
|
ActionContext.getContext().withActionInvocation(mai);
|
|
|
|
|
|
|
|
|
@@ -381,17 +363,15 @@ public class DispatcherTest extends StrutsInternalTestCase {
|
|
|
|
|
|
|
|
|
|
assertFalse(actionProxy.isExecutedCalled());
|
|
|
|
|
|
|
|
|
|
du.setDevMode("false");
|
|
|
|
|
du.setHandleException("false");
|
|
|
|
|
du.serviceAction(req, null, new ActionMapping());
|
|
|
|
|
dispatcher.setDevMode("false");
|
|
|
|
|
dispatcher.setHandleException("false");
|
|
|
|
|
dispatcher.serviceAction(req, null, new ActionMapping());
|
|
|
|
|
|
|
|
|
|
assertTrue("should execute previous proxy", actionProxy.isExecutedCalled());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testServiceActionCreatesNewProxyIfDifferentMapping() throws Exception {
|
|
|
|
|
Dispatcher du = initDispatcher(Collections.emptyMap());
|
|
|
|
|
container.inject(du);
|
|
|
|
|
|
|
|
|
|
MockActionInvocation mai = new MockActionInvocation();
|
|
|
|
|
ActionContext.getContext().withActionInvocation(mai);
|
|
|
|
|
|
|
|
|
@@ -412,7 +392,7 @@ public class DispatcherTest extends StrutsInternalTestCase {
|
|
|
|
|
|
|
|
|
|
ActionMapping newActionMapping = new ActionMapping();
|
|
|
|
|
newActionMapping.setName("hello");
|
|
|
|
|
du.serviceAction(request, response, newActionMapping);
|
|
|
|
|
dispatcher.serviceAction(request, response, newActionMapping);
|
|
|
|
|
|
|
|
|
|
assertFalse(previousActionProxy.isExecutedCalled());
|
|
|
|
|
}
|
|
|
|
@@ -421,196 +401,178 @@ public class DispatcherTest extends StrutsInternalTestCase {
|
|
|
|
|
* Verify proper default (true) handleExceptionState for Dispatcher and that
|
|
|
|
|
* it properly reflects a manually configured change to false.
|
|
|
|
|
*/
|
|
|
|
|
@Test
|
|
|
|
|
public void testHandleException() {
|
|
|
|
|
Dispatcher du = initDispatcher(new HashMap<>());
|
|
|
|
|
assertTrue("Default Dispatcher handleException state not true ?", du.isHandleException());
|
|
|
|
|
assertTrue("Default Dispatcher handleException state not true ?", dispatcher.isHandleException());
|
|
|
|
|
|
|
|
|
|
Dispatcher du2 = initDispatcher(new HashMap<String, String>() {{
|
|
|
|
|
put(StrutsConstants.STRUTS_HANDLE_EXCEPTION, "false");
|
|
|
|
|
}});
|
|
|
|
|
assertFalse("Modified Dispatcher handleException state not false ?", du2.isHandleException());
|
|
|
|
|
initDispatcher(singletonMap(StrutsConstants.STRUTS_HANDLE_EXCEPTION, "false"));
|
|
|
|
|
assertFalse("Modified Dispatcher handleException state not false ?", dispatcher.isHandleException());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Verify proper default (false) devMode for Dispatcher and that
|
|
|
|
|
* it properly reflects a manually configured change to true.
|
|
|
|
|
*/
|
|
|
|
|
@Test
|
|
|
|
|
public void testDevMode() {
|
|
|
|
|
Dispatcher du = initDispatcher(new HashMap<>());
|
|
|
|
|
assertFalse("Default Dispatcher devMode state not false ?", du.isDevMode());
|
|
|
|
|
assertFalse("Default Dispatcher devMode state not false ?", dispatcher.isDevMode());
|
|
|
|
|
|
|
|
|
|
Dispatcher du2 = initDispatcher(new HashMap<String, String>() {{
|
|
|
|
|
put(StrutsConstants.STRUTS_DEVMODE, "true");
|
|
|
|
|
}});
|
|
|
|
|
assertTrue("Modified Dispatcher devMode state not true ?", du2.isDevMode());
|
|
|
|
|
initDispatcher(singletonMap(StrutsConstants.STRUTS_DEVMODE, "true"));
|
|
|
|
|
assertTrue("Modified Dispatcher devMode state not true ?", dispatcher.isDevMode());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testGetLocale_With_DefaultLocale_FromConfiguration() {
|
|
|
|
|
// Given
|
|
|
|
|
Mock mock = new Mock(HttpServletRequest.class);
|
|
|
|
|
HttpServletRequest request = mock(HttpServletRequest.class);
|
|
|
|
|
MockHttpSession mockHttpSession = new MockHttpSession();
|
|
|
|
|
mock.expectAndReturn("getCharacterEncoding", "utf-8"); // From Dispatcher prepare().
|
|
|
|
|
mock.expectAndReturn("getHeader", "X-Requested-With", ""); // From Dispatcher prepare().
|
|
|
|
|
mock.expectAndReturn("getParameterMap", new HashMap<String, Object>()); // From Dispatcher prepare().
|
|
|
|
|
mock.expectAndReturn("getSession", false, mockHttpSession); // From Dispatcher prepare().
|
|
|
|
|
mock.expectAndReturn("getSession", true, mockHttpSession); // From createTestContextMap().
|
|
|
|
|
HttpServletRequest request = (HttpServletRequest) mock.proxy();
|
|
|
|
|
when(request.getCharacterEncoding()).thenReturn(UTF_8.name());
|
|
|
|
|
when(request.getHeader("X-Requested-With")).thenReturn("");
|
|
|
|
|
when(request.getParameterMap()).thenReturn(emptyMap());
|
|
|
|
|
when(request.getSession(anyBoolean())).thenReturn(mockHttpSession);
|
|
|
|
|
HttpServletResponse response = new MockHttpServletResponse();
|
|
|
|
|
|
|
|
|
|
Dispatcher testDispatcher = initDispatcher(new HashMap<String, String>() {{
|
|
|
|
|
put(StrutsConstants.STRUTS_I18N_ENCODING, "utf-8");
|
|
|
|
|
// Not setting a Struts Locale here, so we should receive the default "de_DE" from the test configuration.
|
|
|
|
|
}});
|
|
|
|
|
// Not setting a Struts Locale here, so we should receive the default "de_DE" from the test configuration.
|
|
|
|
|
initDispatcher(singletonMap(StrutsConstants.STRUTS_I18N_ENCODING, UTF_8.name()));
|
|
|
|
|
|
|
|
|
|
// When
|
|
|
|
|
testDispatcher.prepare(request, response);
|
|
|
|
|
ActionContext context = ActionContext.of(createTestContextMap(testDispatcher, request, response));
|
|
|
|
|
dispatcher.prepare(request, response);
|
|
|
|
|
ActionContext context = ActionContext.of(createTestContextMap(dispatcher, request, response));
|
|
|
|
|
|
|
|
|
|
// Then
|
|
|
|
|
assertEquals(Locale.GERMANY, context.getLocale()); // Expect the Dispatcher defaultLocale value "de_DE" from the test configuration.
|
|
|
|
|
mock.verify();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testGetLocale_With_DefaultLocale_fr_CA() {
|
|
|
|
|
// Given
|
|
|
|
|
Mock mock = new Mock(HttpServletRequest.class);
|
|
|
|
|
HttpServletRequest request = mock(HttpServletRequest.class);
|
|
|
|
|
MockHttpSession mockHttpSession = new MockHttpSession();
|
|
|
|
|
mock.expectAndReturn("getCharacterEncoding", "utf-8"); // From Dispatcher prepare().
|
|
|
|
|
mock.expectAndReturn("getHeader", "X-Requested-With", ""); // From Dispatcher prepare().
|
|
|
|
|
mock.expectAndReturn("getParameterMap", new HashMap<String, Object>()); // From Dispatcher prepare().
|
|
|
|
|
mock.expectAndReturn("getSession", false, mockHttpSession); // From Dispatcher prepare().
|
|
|
|
|
mock.expectAndReturn("getSession", true, mockHttpSession); // From createTestContextMap().
|
|
|
|
|
HttpServletRequest request = (HttpServletRequest) mock.proxy();
|
|
|
|
|
when(request.getCharacterEncoding()).thenReturn(UTF_8.name());
|
|
|
|
|
when(request.getHeader("X-Requested-With")).thenReturn("");
|
|
|
|
|
when(request.getParameterMap()).thenReturn(emptyMap());
|
|
|
|
|
when(request.getSession(anyBoolean())).thenReturn(mockHttpSession);
|
|
|
|
|
HttpServletResponse response = new MockHttpServletResponse();
|
|
|
|
|
|
|
|
|
|
Dispatcher testDispatcher = initDispatcher(new HashMap<String, String>() {{
|
|
|
|
|
put(StrutsConstants.STRUTS_I18N_ENCODING, "utf-8");
|
|
|
|
|
initDispatcher(new HashMap<String, String>() {{
|
|
|
|
|
put(StrutsConstants.STRUTS_I18N_ENCODING, UTF_8.name());
|
|
|
|
|
put(StrutsConstants.STRUTS_LOCALE, Locale.CANADA_FRENCH.toString()); // Set the Dispatcher defaultLocale to fr_CA.
|
|
|
|
|
}});
|
|
|
|
|
|
|
|
|
|
// When
|
|
|
|
|
testDispatcher.prepare(request, response);
|
|
|
|
|
ActionContext context = ActionContext.of(createTestContextMap(testDispatcher, request, response));
|
|
|
|
|
dispatcher.prepare(request, response);
|
|
|
|
|
ActionContext context = ActionContext.of(createTestContextMap(dispatcher, request, response));
|
|
|
|
|
|
|
|
|
|
// Then
|
|
|
|
|
assertEquals(Locale.CANADA_FRENCH, context.getLocale()); // Expect the Dispatcher defaultLocale value.
|
|
|
|
|
mock.verify();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testGetLocale_With_BadDefaultLocale_RequestLocale_en_UK() {
|
|
|
|
|
// Given
|
|
|
|
|
Mock mock = new Mock(HttpServletRequest.class);
|
|
|
|
|
HttpServletRequest request = mock(HttpServletRequest.class);
|
|
|
|
|
MockHttpSession mockHttpSession = new MockHttpSession();
|
|
|
|
|
mock.expectAndReturn("getCharacterEncoding", "utf-8"); // From Dispatcher prepare().
|
|
|
|
|
mock.expectAndReturn("getHeader", "X-Requested-With", ""); // From Dispatcher prepare().
|
|
|
|
|
mock.expectAndReturn("getLocale", Locale.UK); // From Dispatcher prepare().
|
|
|
|
|
mock.expectAndReturn("getParameterMap", new HashMap<String, Object>()); // From Dispatcher prepare().
|
|
|
|
|
mock.expectAndReturn("getSession", false, mockHttpSession); // From Dispatcher prepare().
|
|
|
|
|
mock.expectAndReturn("getSession", true, mockHttpSession); // From createTestContextMap().
|
|
|
|
|
mock.expectAndReturn("getLocale", Locale.UK); // From createTestContextMap().
|
|
|
|
|
HttpServletRequest request = (HttpServletRequest) mock.proxy();
|
|
|
|
|
when(request.getCharacterEncoding()).thenReturn(UTF_8.name());
|
|
|
|
|
when(request.getHeader("X-Requested-With")).thenReturn("");
|
|
|
|
|
when(request.getParameterMap()).thenReturn(emptyMap());
|
|
|
|
|
when(request.getSession(anyBoolean())).thenReturn(mockHttpSession);
|
|
|
|
|
when(request.getLocale()).thenReturn(Locale.UK);
|
|
|
|
|
HttpServletResponse response = new MockHttpServletResponse();
|
|
|
|
|
|
|
|
|
|
Dispatcher testDispatcher = initDispatcher(new HashMap<String, String>() {{
|
|
|
|
|
put(StrutsConstants.STRUTS_I18N_ENCODING, "utf-8");
|
|
|
|
|
initDispatcher(new HashMap<String, String>() {{
|
|
|
|
|
put(StrutsConstants.STRUTS_I18N_ENCODING, UTF_8.name());
|
|
|
|
|
put(StrutsConstants.STRUTS_LOCALE, "This_is_not_a_valid_Locale_string"); // Set Dispatcher defaultLocale to an invalid value.
|
|
|
|
|
}});
|
|
|
|
|
|
|
|
|
|
// When
|
|
|
|
|
testDispatcher.prepare(request, response);
|
|
|
|
|
ActionContext context = ActionContext.of(createTestContextMap(testDispatcher, request, response));
|
|
|
|
|
dispatcher.prepare(request, response);
|
|
|
|
|
ActionContext context = ActionContext.of(createTestContextMap(dispatcher, request, response));
|
|
|
|
|
|
|
|
|
|
// Then
|
|
|
|
|
assertEquals(Locale.UK, context.getLocale()); // Expect the request set value from Mock.
|
|
|
|
|
mock.verify();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testGetLocale_With_BadDefaultLocale_And_RuntimeException() {
|
|
|
|
|
// Given
|
|
|
|
|
Mock mock = new Mock(HttpServletRequest.class);
|
|
|
|
|
HttpServletRequest request = mock(HttpServletRequest.class);
|
|
|
|
|
MockHttpSession mockHttpSession = new MockHttpSession();
|
|
|
|
|
mock.expectAndReturn("getCharacterEncoding", "utf-8"); // From Dispatcher prepare().
|
|
|
|
|
mock.expectAndReturn("getHeader", "X-Requested-With", ""); // From Dispatcher prepare().
|
|
|
|
|
mock.expectAndReturn("getLocale", Locale.UK); // From Dispatcher prepare().
|
|
|
|
|
mock.expectAndReturn("getParameterMap", new HashMap<String, Object>()); // From Dispatcher prepare().
|
|
|
|
|
mock.expectAndReturn("getSession", false, mockHttpSession); // From Dispatcher prepare().
|
|
|
|
|
mock.expectAndReturn("getSession", true, mockHttpSession); // From createTestContextMap().
|
|
|
|
|
mock.expectAndThrow("getLocale", new IllegalStateException("Test theoretical state preventing HTTP Request Locale access")); // From createTestContextMap().
|
|
|
|
|
HttpServletRequest request = (HttpServletRequest) mock.proxy();
|
|
|
|
|
when(request.getCharacterEncoding()).thenReturn(UTF_8.name());
|
|
|
|
|
when(request.getHeader("X-Requested-With")).thenReturn("");
|
|
|
|
|
when(request.getParameterMap()).thenReturn(emptyMap());
|
|
|
|
|
when(request.getSession(anyBoolean())).thenReturn(mockHttpSession);
|
|
|
|
|
when(request.getLocale()).thenReturn(Locale.UK);
|
|
|
|
|
HttpServletResponse response = new MockHttpServletResponse();
|
|
|
|
|
|
|
|
|
|
Dispatcher testDispatcher = initDispatcher(new HashMap<String, String>() {{
|
|
|
|
|
put(StrutsConstants.STRUTS_I18N_ENCODING, "utf-8");
|
|
|
|
|
initDispatcher(new HashMap<String, String>() {{
|
|
|
|
|
put(StrutsConstants.STRUTS_I18N_ENCODING, UTF_8.name());
|
|
|
|
|
put(StrutsConstants.STRUTS_LOCALE, "This_is_not_a_valid_Locale_string"); // Set the Dispatcher defaultLocale to an invalid value.
|
|
|
|
|
}});
|
|
|
|
|
|
|
|
|
|
// When
|
|
|
|
|
testDispatcher.prepare(request, response);
|
|
|
|
|
ActionContext context = ActionContext.of(createTestContextMap(testDispatcher, request, response));
|
|
|
|
|
dispatcher.prepare(request, response);
|
|
|
|
|
when(request.getLocale()).thenThrow(new IllegalStateException("Test theoretical state preventing HTTP Request Locale access"));
|
|
|
|
|
ActionContext context = ActionContext.of(createTestContextMap(dispatcher, request, response));
|
|
|
|
|
|
|
|
|
|
// Then
|
|
|
|
|
assertEquals(Locale.getDefault(), context.getLocale()); // Expect the system default value, when BOTH Dispatcher default Locale AND request access fail.
|
|
|
|
|
mock.verify();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testGetLocale_With_NullDefaultLocale() {
|
|
|
|
|
// Given
|
|
|
|
|
Mock mock = new Mock(HttpServletRequest.class);
|
|
|
|
|
HttpServletRequest request = mock(HttpServletRequest.class);
|
|
|
|
|
MockHttpSession mockHttpSession = new MockHttpSession();
|
|
|
|
|
mock.expectAndReturn("getCharacterEncoding", "utf-8"); // From Dispatcher prepare().
|
|
|
|
|
mock.expectAndReturn("getHeader", "X-Requested-With", ""); // From Dispatcher prepare().
|
|
|
|
|
mock.expectAndReturn("getLocale", Locale.CANADA_FRENCH); // From Dispatcher prepare().
|
|
|
|
|
mock.expectAndReturn("getParameterMap", new HashMap<String, Object>()); // From Dispatcher prepare().
|
|
|
|
|
mock.expectAndReturn("getSession", false, mockHttpSession); // From Dispatcher prepare().
|
|
|
|
|
mock.expectAndReturn("getSession", true, mockHttpSession); // From createTestContextMap().
|
|
|
|
|
mock.expectAndReturn("getLocale", Locale.CANADA_FRENCH); // From createTestContextMap().
|
|
|
|
|
HttpServletRequest request = (HttpServletRequest) mock.proxy();
|
|
|
|
|
when(request.getCharacterEncoding()).thenReturn(UTF_8.name());
|
|
|
|
|
when(request.getHeader("X-Requested-With")).thenReturn("");
|
|
|
|
|
when(request.getParameterMap()).thenReturn(emptyMap());
|
|
|
|
|
when(request.getSession(anyBoolean())).thenReturn(mockHttpSession);
|
|
|
|
|
when(request.getLocale()).thenReturn(Locale.CANADA_FRENCH);
|
|
|
|
|
HttpServletResponse response = new MockHttpServletResponse();
|
|
|
|
|
|
|
|
|
|
Dispatcher testDispatcher = initDispatcher(new HashMap<String, String>() {{
|
|
|
|
|
put(StrutsConstants.STRUTS_I18N_ENCODING, "utf-8");
|
|
|
|
|
// Attempting to set StrutsConstants.STRUTS_LOCALE to null here via parameters causes an NPE.
|
|
|
|
|
}});
|
|
|
|
|
// Attempting to set StrutsConstants.STRUTS_LOCALE to null here via parameters causes an NPE.
|
|
|
|
|
initDispatcher(singletonMap(StrutsConstants.STRUTS_I18N_ENCODING, UTF_8.name()));
|
|
|
|
|
|
|
|
|
|
testDispatcher.setDefaultLocale(null); // Force a null Struts default locale, otherwise we receive the default "de_DE" from the test configuration.
|
|
|
|
|
dispatcher.setDefaultLocale(null); // Force a null Struts default locale, otherwise we receive the default "de_DE" from the test configuration.
|
|
|
|
|
|
|
|
|
|
// When
|
|
|
|
|
testDispatcher.prepare(request, response);
|
|
|
|
|
ActionContext context = ActionContext.of(createTestContextMap(testDispatcher, request, response));
|
|
|
|
|
dispatcher.prepare(request, response);
|
|
|
|
|
ActionContext context = ActionContext.of(createTestContextMap(dispatcher, request, response));
|
|
|
|
|
|
|
|
|
|
// Then
|
|
|
|
|
assertEquals(Locale.CANADA_FRENCH, context.getLocale()); // Expect the request set value from Mock.
|
|
|
|
|
mock.verify();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testGetLocale_With_NullDefaultLocale_And_RuntimeException() {
|
|
|
|
|
// Given
|
|
|
|
|
Mock mock = new Mock(HttpServletRequest.class);
|
|
|
|
|
HttpServletRequest request = mock(HttpServletRequest.class);
|
|
|
|
|
MockHttpSession mockHttpSession = new MockHttpSession();
|
|
|
|
|
mock.expectAndReturn("getCharacterEncoding", "utf-8"); // From Dispatcher prepare().
|
|
|
|
|
mock.expectAndReturn("getHeader", "X-Requested-With", ""); // From Dispatcher prepare().
|
|
|
|
|
mock.expectAndReturn("getLocale", Locale.CANADA_FRENCH); // From Dispatcher prepare().
|
|
|
|
|
mock.expectAndReturn("getParameterMap", new HashMap<String, Object>()); // From Dispatcher prepare().
|
|
|
|
|
mock.expectAndReturn("getSession", false, mockHttpSession); // From Dispatcher prepare().
|
|
|
|
|
mock.expectAndReturn("getSession", true, mockHttpSession); // From createTestContextMap().
|
|
|
|
|
mock.expectAndThrow("getLocale", new IllegalStateException("Test some theoretical state preventing HTTP Request Locale access")); // From createTestContextMap().
|
|
|
|
|
HttpServletRequest request = (HttpServletRequest) mock.proxy();
|
|
|
|
|
when(request.getCharacterEncoding()).thenReturn(UTF_8.name());
|
|
|
|
|
when(request.getHeader("X-Requested-With")).thenReturn("");
|
|
|
|
|
when(request.getParameterMap()).thenReturn(emptyMap());
|
|
|
|
|
when(request.getSession(anyBoolean())).thenReturn(mockHttpSession);
|
|
|
|
|
when(request.getLocale()).thenReturn(Locale.CANADA_FRENCH);
|
|
|
|
|
HttpServletResponse response = new MockHttpServletResponse();
|
|
|
|
|
|
|
|
|
|
Dispatcher testDispatcher = initDispatcher(new HashMap<String, String>() {{
|
|
|
|
|
put(StrutsConstants.STRUTS_I18N_ENCODING, "utf-8");
|
|
|
|
|
// Attempting to set StrutsConstants.STRUTS_LOCALE to null via parameters causes an NPE.
|
|
|
|
|
}});
|
|
|
|
|
// Attempting to set StrutsConstants.STRUTS_LOCALE to null via parameters causes an NPE.
|
|
|
|
|
initDispatcher(singletonMap(StrutsConstants.STRUTS_I18N_ENCODING, UTF_8.name()));
|
|
|
|
|
|
|
|
|
|
testDispatcher.setDefaultLocale(null); // Force a null Struts default locale, otherwise we receive the default "de_DE" from the test configuration.
|
|
|
|
|
dispatcher.setDefaultLocale(null); // Force a null Struts default locale, otherwise we receive the default "de_DE" from the test configuration.
|
|
|
|
|
|
|
|
|
|
// When
|
|
|
|
|
testDispatcher.prepare(request, response);
|
|
|
|
|
ActionContext context = ActionContext.of(createTestContextMap(testDispatcher, request, response));
|
|
|
|
|
dispatcher.prepare(request, response);
|
|
|
|
|
when(request.getLocale()).thenThrow(new IllegalStateException("Test theoretical state preventing HTTP Request Locale access"));
|
|
|
|
|
ActionContext context = ActionContext.of(createTestContextMap(dispatcher, request, response));
|
|
|
|
|
|
|
|
|
|
// Then
|
|
|
|
|
assertEquals(Locale.getDefault(), context.getLocale()); // Expect the system default value when Mock request access fails.
|
|
|
|
|
mock.verify();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Dispatcher spyDispatcherWithConfigurationManager(Dispatcher dispatcher, ConfigurationManager configurationManager) {
|
|
|
|
|
Dispatcher spiedDispatcher = spy(dispatcher);
|
|
|
|
|
doReturn(configurationManager).when(spiedDispatcher).createConfigurationManager(any());
|
|
|
|
|
return spiedDispatcher;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@@ -641,21 +603,6 @@ public class DispatcherTest extends StrutsInternalTestCase {
|
|
|
|
|
response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static class InternalConfigurationManager extends ConfigurationManager {
|
|
|
|
|
public boolean destroyConfiguration = false;
|
|
|
|
|
|
|
|
|
|
public InternalConfigurationManager(String name) {
|
|
|
|
|
super(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public synchronized void destroyConfiguration() {
|
|
|
|
|
super.destroyConfiguration();
|
|
|
|
|
destroyConfiguration = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static class DispatcherListenerState {
|
|
|
|
|
public boolean isInitialized = false;
|
|
|
|
|
public boolean isDestroyed = false;
|
|
|
|
|