From 6f1e1222bcf2d5e899f03b1a5e23aef1eb250071 Mon Sep 17 00:00:00 2001 From: Kusal Kithul-Godage Date: Tue, 2 Jan 2024 01:46:28 +1100 Subject: [PATCH] WW-5382 Rework existing Dispatcher tests and base test classes --- .../xwork2/XWorkJUnit4TestCase.java | 4 - .../opensymphony/xwork2/XWorkTestCase.java | 4 - .../struts2/dispatcher/ContainerHolder.java | 12 +- .../apache/struts2/dispatcher/Dispatcher.java | 2 +- .../struts2/dispatcher/MockDispatcher.java | 59 --- .../struts2/util/StrutsTestCaseHelper.java | 24 +- .../struts2/StrutsInternalTestCase.java | 7 +- .../struts2/StrutsJUnit4InternalTestCase.java | 61 +++ .../struts2/dispatcher/DispatcherTest.java | 463 ++++++++---------- .../struts2/views/jsp/AbstractTagTest.java | 6 +- .../struts2/junit/StrutsJUnit4TestCase.java | 10 +- .../apache/struts2/junit/StrutsTestCase.java | 11 +- .../struts2/StrutsTestCasePortletTests.java | 24 +- .../apache/struts2/testng/StrutsTestCase.java | 12 +- 14 files changed, 314 insertions(+), 385 deletions(-) delete mode 100644 core/src/main/java/org/apache/struts2/dispatcher/MockDispatcher.java create mode 100644 core/src/test/java/org/apache/struts2/StrutsJUnit4InternalTestCase.java diff --git a/core/src/main/java/com/opensymphony/xwork2/XWorkJUnit4TestCase.java b/core/src/main/java/com/opensymphony/xwork2/XWorkJUnit4TestCase.java index b22789cc2..fe39c0c2a 100644 --- a/core/src/main/java/com/opensymphony/xwork2/XWorkJUnit4TestCase.java +++ b/core/src/main/java/com/opensymphony/xwork2/XWorkJUnit4TestCase.java @@ -51,10 +51,6 @@ public abstract class XWorkJUnit4TestCase { @After public void tearDown() throws Exception { XWorkTestCaseHelper.tearDown(configurationManager); - configurationManager = null; - configuration = null; - container = null; - actionProxyFactory = null; } protected void loadConfigurationProviders(ConfigurationProvider... providers) { diff --git a/core/src/main/java/com/opensymphony/xwork2/XWorkTestCase.java b/core/src/main/java/com/opensymphony/xwork2/XWorkTestCase.java index 941a9e37c..dc6bb2508 100644 --- a/core/src/main/java/com/opensymphony/xwork2/XWorkTestCase.java +++ b/core/src/main/java/com/opensymphony/xwork2/XWorkTestCase.java @@ -64,10 +64,6 @@ public abstract class XWorkTestCase extends TestCase { @Override protected void tearDown() throws Exception { XWorkTestCaseHelper.tearDown(configurationManager); - configurationManager = null; - configuration = null; - container = null; - actionProxyFactory = null; } protected void loadConfigurationProviders(ConfigurationProvider... providers) { diff --git a/core/src/main/java/org/apache/struts2/dispatcher/ContainerHolder.java b/core/src/main/java/org/apache/struts2/dispatcher/ContainerHolder.java index 9565732ac..2d1c61657 100644 --- a/core/src/main/java/org/apache/struts2/dispatcher/ContainerHolder.java +++ b/core/src/main/java/org/apache/struts2/dispatcher/ContainerHolder.java @@ -23,25 +23,25 @@ import com.opensymphony.xwork2.inject.Container; /** * Simple class to hold Container instance per thread to minimise number of attempts * to read configuration and build each time a new configuration. - * + *

* As ContainerHolder operates just per thread (which means per request) there is no need * to check if configuration changed during the same request. If changed between requests, * first call to store Container in ContainerHolder will be with the new configuration. */ class ContainerHolder { - private static ThreadLocal instance = new ThreadLocal<>(); + private static final ThreadLocal instance = new ThreadLocal<>(); - public static void store(Container instance) { - ContainerHolder.instance.set(instance); + public static void store(Container newInstance) { + instance.set(newInstance); } public static Container get() { - return ContainerHolder.instance.get(); + return instance.get(); } public static void clear() { - ContainerHolder.instance.remove(); + instance.remove(); } } diff --git a/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java b/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java index 7234dfe6c..a53398b77 100644 --- a/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java +++ b/core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java @@ -367,7 +367,7 @@ public class Dispatcher { } // clean up Dispatcher itself for this thread - instance.set(null); + instance.remove(); servletContext.setAttribute(StrutsStatics.SERVLET_DISPATCHER, null); // clean up DispatcherListeners diff --git a/core/src/main/java/org/apache/struts2/dispatcher/MockDispatcher.java b/core/src/main/java/org/apache/struts2/dispatcher/MockDispatcher.java deleted file mode 100644 index 500e1b541..000000000 --- a/core/src/main/java/org/apache/struts2/dispatcher/MockDispatcher.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.struts2.dispatcher; - -import com.opensymphony.xwork2.config.ConfigurationManager; -import com.opensymphony.xwork2.inject.Container; - -import javax.servlet.ServletContext; -import java.util.Map; - -/** - * We really shouldn't test with this class because it relies on changing the ConfigurationManager mid-lifecycle, - * but retaining the stale injections - this will prevent us from detecting exactly these kind of bugs in our tests... - */ -public class MockDispatcher extends Dispatcher { - - private final ConfigurationManager copyConfigurationManager; - private boolean injectedOnce = false; - - public MockDispatcher(ServletContext servletContext, Map context, ConfigurationManager configurationManager) { - super(servletContext, context); - this.copyConfigurationManager = configurationManager; - } - - @Override - public void init() { - super.init(); - ContainerHolder.clear(); - this.configurationManager = copyConfigurationManager; - } - - @Override - public Container getContainer() { - if (!injectedOnce) { - injectedOnce = true; - return super.getContainer(); - } - if (ContainerHolder.get() == null) { - ContainerHolder.store(getConfigurationManager().getConfiguration().getContainer()); - } - return ContainerHolder.get(); - } -} diff --git a/core/src/main/java/org/apache/struts2/util/StrutsTestCaseHelper.java b/core/src/main/java/org/apache/struts2/util/StrutsTestCaseHelper.java index 42e107f40..0f9a1bdf9 100644 --- a/core/src/main/java/org/apache/struts2/util/StrutsTestCaseHelper.java +++ b/core/src/main/java/org/apache/struts2/util/StrutsTestCaseHelper.java @@ -28,19 +28,17 @@ import org.apache.struts2.dispatcher.DispatcherErrorHandler; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import java.util.HashMap; import java.util.Map; +import static java.util.Collections.emptyMap; + /** - * Generic test setup methods to be used with any unit testing framework. + * Generic test setup methods to be used with any unit testing framework. */ public class StrutsTestCaseHelper { - - public static Dispatcher initDispatcher(ServletContext ctx, Map params) { - if (params == null) { - params = new HashMap<>(); - } - Dispatcher du = new DispatcherWrapper(ctx, params); + + public static Dispatcher initDispatcher(ServletContext ctx, Map params) { + Dispatcher du = new DispatcherWrapper(ctx, params != null ? params : emptyMap()); du.init(); Dispatcher.setInstance(du); @@ -52,7 +50,15 @@ public class StrutsTestCaseHelper { return du; } - public static void tearDown() throws Exception { + public static void tearDown(Dispatcher dispatcher) { + if (dispatcher != null && dispatcher.getConfigurationManager() != null) { + dispatcher.cleanup(); + } + tearDown(); + } + + public static void tearDown() { + (new Dispatcher(null, null)).cleanUpAfterInit(); // Clear ContainerHolder Dispatcher.setInstance(null); ActionContext.clear(); } diff --git a/core/src/test/java/org/apache/struts2/StrutsInternalTestCase.java b/core/src/test/java/org/apache/struts2/StrutsInternalTestCase.java index 5aad82969..ade928efb 100644 --- a/core/src/test/java/org/apache/struts2/StrutsInternalTestCase.java +++ b/core/src/test/java/org/apache/struts2/StrutsInternalTestCase.java @@ -75,12 +75,7 @@ public abstract class StrutsInternalTestCase extends XWorkTestCase { @Override protected void tearDown() throws Exception { - // maybe someone else already destroyed Dispatcher - if (dispatcher != null && dispatcher.getConfigurationManager() != null) { - dispatcher.cleanup(); - dispatcher = null; - } - StrutsTestCaseHelper.tearDown(); + StrutsTestCaseHelper.tearDown(dispatcher); } /** diff --git a/core/src/test/java/org/apache/struts2/StrutsJUnit4InternalTestCase.java b/core/src/test/java/org/apache/struts2/StrutsJUnit4InternalTestCase.java new file mode 100644 index 000000000..8d7275392 --- /dev/null +++ b/core/src/test/java/org/apache/struts2/StrutsJUnit4InternalTestCase.java @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.struts2; + +import com.opensymphony.xwork2.ActionProxyFactory; +import com.opensymphony.xwork2.XWorkJUnit4TestCase; +import org.apache.struts2.dispatcher.Dispatcher; +import org.apache.struts2.util.StrutsTestCaseHelper; +import org.apache.struts2.views.jsp.StrutsMockServletContext; +import org.junit.After; +import org.junit.Before; + +import java.util.Map; + +public class StrutsJUnit4InternalTestCase extends XWorkJUnit4TestCase { + + protected StrutsMockServletContext servletContext; + protected Dispatcher dispatcher; + + @Override + @Before + public void setUp() throws Exception { + initDispatcher(); + } + + @Override + @After + public void tearDown() throws Exception { + StrutsTestCaseHelper.tearDown(dispatcher); + } + + protected void initDispatcher() { + initDispatcher(null); + } + + protected void initDispatcher(Map params) { + StrutsTestCaseHelper.tearDown(); + servletContext = new StrutsMockServletContext(); + dispatcher = StrutsTestCaseHelper.initDispatcher(servletContext, params); + configurationManager = dispatcher.getConfigurationManager(); + configuration = configurationManager.getConfiguration(); + container = configuration.getContainer(); + actionProxyFactory = container.getInstance(ActionProxyFactory.class); + } +} diff --git a/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java b/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java index 73a526535..267fb4e22 100644 --- a/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java +++ b/core/src/test/java/org/apache/struts2/dispatcher/DispatcherTest.java @@ -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() {{ - 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() {{ - 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() {{ - 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() {{ - 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 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()); - - 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 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() {{ - 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() {{ - 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()); // 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() {{ - 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()); // 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() {{ - put(StrutsConstants.STRUTS_I18N_ENCODING, "utf-8"); + initDispatcher(new HashMap() {{ + 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()); // 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() {{ - put(StrutsConstants.STRUTS_I18N_ENCODING, "utf-8"); + initDispatcher(new HashMap() {{ + 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()); // 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() {{ - put(StrutsConstants.STRUTS_I18N_ENCODING, "utf-8"); + initDispatcher(new HashMap() {{ + 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()); // 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() {{ - 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()); // 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() {{ - 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; diff --git a/core/src/test/java/org/apache/struts2/views/jsp/AbstractTagTest.java b/core/src/test/java/org/apache/struts2/views/jsp/AbstractTagTest.java index c1b8188fa..59afa3a1c 100644 --- a/core/src/test/java/org/apache/struts2/views/jsp/AbstractTagTest.java +++ b/core/src/test/java/org/apache/struts2/views/jsp/AbstractTagTest.java @@ -34,7 +34,6 @@ import org.apache.struts2.TestAction; import org.apache.struts2.dispatcher.ApplicationMap; import org.apache.struts2.dispatcher.Dispatcher; import org.apache.struts2.dispatcher.HttpParameters; -import org.apache.struts2.dispatcher.MockDispatcher; import org.apache.struts2.dispatcher.RequestMap; import org.apache.struts2.dispatcher.SessionMap; @@ -42,9 +41,10 @@ import javax.servlet.http.HttpServletResponse; import javax.servlet.jsp.JspWriter; import java.io.File; import java.io.StringWriter; -import java.util.HashMap; import java.util.Map; +import static java.util.Collections.emptyMap; + /** * Base class to extend for unit testing UI Tags. */ @@ -111,7 +111,7 @@ public abstract class AbstractTagTest extends StrutsInternalTestCase { pageContext.setServletContext(servletContext); mockContainer = new Mock(Container.class); - MockDispatcher du = new MockDispatcher(pageContext.getServletContext(), new HashMap<>(), configurationManager); + Dispatcher du = new Dispatcher(pageContext.getServletContext(), emptyMap()); du.init(); Dispatcher.setInstance(du); session = new SessionMap(request); diff --git a/plugins/junit/src/main/java/org/apache/struts2/junit/StrutsJUnit4TestCase.java b/plugins/junit/src/main/java/org/apache/struts2/junit/StrutsJUnit4TestCase.java index 84b76a1b6..ee486d6cc 100644 --- a/plugins/junit/src/main/java/org/apache/struts2/junit/StrutsJUnit4TestCase.java +++ b/plugins/junit/src/main/java/org/apache/struts2/junit/StrutsJUnit4TestCase.java @@ -203,9 +203,9 @@ public abstract class StrutsJUnit4TestCase extends XWorkJUnit4TestCase { * Sets up the configuration settings, XWork configuration, and * message resources */ + @Override @Before public void setUp() throws Exception { - super.setUp(); initServletMockObjects(); setupBeforeInitDispatcher(); initDispatcherParams(); @@ -239,14 +239,10 @@ public abstract class StrutsJUnit4TestCase extends XWorkJUnit4TestCase { return null; } + @Override @After public void tearDown() throws Exception { - super.tearDown(); - if (dispatcher != null && dispatcher.getConfigurationManager() != null) { - dispatcher.cleanup(); - dispatcher = null; - } - StrutsTestCaseHelper.tearDown(); + StrutsTestCaseHelper.tearDown(dispatcher); } } diff --git a/plugins/junit/src/main/java/org/apache/struts2/junit/StrutsTestCase.java b/plugins/junit/src/main/java/org/apache/struts2/junit/StrutsTestCase.java index f64a9966f..6280de8a8 100644 --- a/plugins/junit/src/main/java/org/apache/struts2/junit/StrutsTestCase.java +++ b/plugins/junit/src/main/java/org/apache/struts2/junit/StrutsTestCase.java @@ -168,8 +168,8 @@ public abstract class StrutsTestCase extends XWorkTestCase { * Sets up the configuration settings, XWork configuration, and * message resources */ + @Override protected void setUp() throws Exception { - super.setUp(); initServletMockObjects(); setupBeforeInitDispatcher(); dispatcher = initDispatcher(dispatcherInitParams); @@ -199,14 +199,9 @@ public abstract class StrutsTestCase extends XWorkTestCase { return du; } + @Override protected void tearDown() throws Exception { - super.tearDown(); - // maybe someone else already destroyed Dispatcher - if (dispatcher != null && dispatcher.getConfigurationManager() != null) { - dispatcher.cleanup(); - dispatcher = null; - } - StrutsTestCaseHelper.tearDown(); + StrutsTestCaseHelper.tearDown(dispatcher); } } diff --git a/plugins/portlet/src/test/java/org/apache/struts2/StrutsTestCasePortletTests.java b/plugins/portlet/src/test/java/org/apache/struts2/StrutsTestCasePortletTests.java index acea0bec3..0da8d1043 100644 --- a/plugins/portlet/src/test/java/org/apache/struts2/StrutsTestCasePortletTests.java +++ b/plugins/portlet/src/test/java/org/apache/struts2/StrutsTestCasePortletTests.java @@ -23,12 +23,6 @@ import com.opensymphony.xwork2.ActionProxy; import com.opensymphony.xwork2.ActionProxyFactory; import com.opensymphony.xwork2.XWorkTestCase; import com.opensymphony.xwork2.config.Configuration; -import java.io.UnsupportedEncodingException; -import java.util.HashMap; -import java.util.Map; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; import org.apache.struts2.dispatcher.Dispatcher; import org.apache.struts2.dispatcher.HttpParameters; import org.apache.struts2.dispatcher.mapper.ActionMapper; @@ -41,6 +35,13 @@ import org.springframework.mock.web.MockHttpSession; import org.springframework.mock.web.MockPageContext; import org.springframework.mock.web.MockServletContext; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.UnsupportedEncodingException; +import java.util.HashMap; +import java.util.Map; + /* * Changes: This is a copy of org.apache.struts2.StrutsTestCase from the Struts 2 junit-plugin, kept in * in the same package org.apache.struts2 and renamed. Removed some unused imports, made @@ -180,8 +181,8 @@ public abstract class StrutsTestCasePortletTests extends XWorkTestCase { * Sets up the configuration settings, XWork configuration, and * message resources */ + @Override protected void setUp() throws Exception { - super.setUp(); initServletMockObjects(); setupBeforeInitDispatcher(); dispatcher = initDispatcher(dispatcherInitParams); @@ -211,14 +212,9 @@ public abstract class StrutsTestCasePortletTests extends XWorkTestCase { return du; } + @Override protected void tearDown() throws Exception { - super.tearDown(); - // maybe someone else already destroyed Dispatcher - if (dispatcher != null && dispatcher.getConfigurationManager() != null) { - dispatcher.cleanup(); - dispatcher = null; - } - StrutsTestCaseHelper.tearDown(); + StrutsTestCaseHelper.tearDown(dispatcher); } } diff --git a/plugins/testng/src/main/java/org/apache/struts2/testng/StrutsTestCase.java b/plugins/testng/src/main/java/org/apache/struts2/testng/StrutsTestCase.java index 630eb2df6..6df81ff66 100644 --- a/plugins/testng/src/main/java/org/apache/struts2/testng/StrutsTestCase.java +++ b/plugins/testng/src/main/java/org/apache/struts2/testng/StrutsTestCase.java @@ -18,13 +18,13 @@ */ package org.apache.struts2.testng; -import java.util.Map; - import org.apache.struts2.dispatcher.Dispatcher; import org.apache.struts2.util.StrutsTestCaseHelper; +import org.springframework.mock.web.MockServletContext; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; -import org.springframework.mock.web.MockServletContext; + +import java.util.Map; /** * Base test class for TestNG unit tests. Provides common Struts variables @@ -33,12 +33,12 @@ import org.springframework.mock.web.MockServletContext; public class StrutsTestCase extends TestNGXWorkTestCase { @BeforeTest + @Override protected void setUp() throws Exception { - super.setUp(); initDispatcher(null); } - protected Dispatcher initDispatcher(Map params) { + protected Dispatcher initDispatcher(Map params) { Dispatcher du = StrutsTestCaseHelper.initDispatcher(new MockServletContext(), params); configurationManager = du.getConfigurationManager(); configuration = configurationManager.getConfiguration(); @@ -56,8 +56,8 @@ public class StrutsTestCase extends TestNGXWorkTestCase { } @AfterTest + @Override protected void tearDown() throws Exception { - super.tearDown(); StrutsTestCaseHelper.tearDown(); } }