mirror of
https://github.com/apache/struts.git
synced 2026-08-07 07:37:20 +00:00
Merge pull request #236 from sullis/DispatcherTest-init
add unit test for Dispatcher init()
This commit is contained in:
@@ -47,6 +47,7 @@ import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -134,7 +135,7 @@ public class DispatcherTest extends StrutsInternalTestCase {
|
||||
|
||||
assertEquals("utf-8", req.getCharacterEncoding());
|
||||
}
|
||||
|
||||
|
||||
public void testPrepareMultipartRequest() throws Exception {
|
||||
MockHttpServletRequest req = new MockHttpServletRequest();
|
||||
MockHttpServletResponse res = new MockHttpServletResponse();
|
||||
@@ -175,9 +176,9 @@ public class DispatcherTest extends StrutsInternalTestCase {
|
||||
}
|
||||
|
||||
public void testDispatcherListener() throws Exception {
|
||||
|
||||
|
||||
final DispatcherListenerState state = new DispatcherListenerState();
|
||||
|
||||
|
||||
Dispatcher.addDispatcherListener(new DispatcherListener() {
|
||||
public void dispatcherDestroyed(Dispatcher du) {
|
||||
state.isDestroyed = true;
|
||||
@@ -186,21 +187,21 @@ public class DispatcherTest extends StrutsInternalTestCase {
|
||||
state.isInitialized = true;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
assertFalse(state.isDestroyed);
|
||||
assertFalse(state.isInitialized);
|
||||
|
||||
|
||||
Dispatcher du = initDispatcher(new HashMap<String, String>() );
|
||||
|
||||
|
||||
assertTrue(state.isInitialized);
|
||||
|
||||
|
||||
du.cleanup();
|
||||
|
||||
assertTrue(state.isDestroyed);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void testConfigurationManager() {
|
||||
Dispatcher du;
|
||||
final InternalConfigurationManager configurationManager = new InternalConfigurationManager(Container.DEFAULT_NAME);
|
||||
@@ -208,26 +209,42 @@ public class DispatcherTest extends StrutsInternalTestCase {
|
||||
du = new MockDispatcher(new MockServletContext(), new HashMap<String, String>(), configurationManager);
|
||||
du.init();
|
||||
Dispatcher.setInstance(du);
|
||||
|
||||
|
||||
assertFalse(configurationManager.destroyConfiguration);
|
||||
|
||||
|
||||
du.cleanup();
|
||||
|
||||
|
||||
assertTrue(configurationManager.destroyConfiguration);
|
||||
|
||||
|
||||
}
|
||||
finally {
|
||||
Dispatcher.setInstance(null);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void testInitLoadsDefaultConfig() {
|
||||
Dispatcher du = new Dispatcher(new MockServletContext(), new HashMap<String, String>());
|
||||
du.init();
|
||||
Configuration config = du.getConfigurationManager().getConfiguration();
|
||||
assertNotNull(config);
|
||||
HashSet<String> expected = new HashSet<String>();
|
||||
expected.add("struts-default.xml");
|
||||
expected.add("struts-plugin.xml");
|
||||
expected.add("struts.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);
|
||||
}
|
||||
|
||||
public void testObjectFactoryDestroy() throws Exception {
|
||||
|
||||
ConfigurationManager cm = new ConfigurationManager(Container.DEFAULT_NAME);
|
||||
Dispatcher du = new MockDispatcher(new MockServletContext(), new HashMap<String, String>(), cm);
|
||||
Mock mockConfiguration = new Mock(Configuration.class);
|
||||
cm.setConfiguration((Configuration)mockConfiguration.proxy());
|
||||
|
||||
|
||||
Mock mockContainer = new Mock(Container.class);
|
||||
String reloadConfigs = container.getInstance(String.class, StrutsConstants.STRUTS_CONFIGURATION_XML_RELOAD);
|
||||
mockContainer.expectAndReturn("getInstance", C.args(C.eq(String.class), C.eq(StrutsConstants.STRUTS_CONFIGURATION_XML_RELOAD)),
|
||||
@@ -248,18 +265,18 @@ public class DispatcherTest extends StrutsInternalTestCase {
|
||||
mockConfiguration.verify();
|
||||
mockContainer.verify();
|
||||
}
|
||||
|
||||
public void testInterceptorDestroy() throws Exception {
|
||||
|
||||
public void testInterceptorDestroy() throws Exception {
|
||||
Mock mockInterceptor = new Mock(Interceptor.class);
|
||||
mockInterceptor.matchAndReturn("hashCode", 0);
|
||||
mockInterceptor.expect("destroy");
|
||||
|
||||
|
||||
InterceptorMapping interceptorMapping = new InterceptorMapping("test", (Interceptor) mockInterceptor.proxy());
|
||||
|
||||
|
||||
InterceptorStackConfig isc = new InterceptorStackConfig.Builder("test").addInterceptor(interceptorMapping).build();
|
||||
|
||||
|
||||
PackageConfig packageConfig = new PackageConfig.Builder("test").addInterceptorStackConfig(isc).build();
|
||||
|
||||
|
||||
Map<String, PackageConfig> packageConfigs = new HashMap<String, PackageConfig>();
|
||||
packageConfigs.put("test", packageConfig);
|
||||
|
||||
@@ -273,14 +290,14 @@ public class DispatcherTest extends StrutsInternalTestCase {
|
||||
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<String, String>(), configurationManager);
|
||||
dispatcher.init();
|
||||
dispatcher.cleanup();
|
||||
|
||||
|
||||
mockInterceptor.verify();
|
||||
mockContainer.verify();
|
||||
mockConfiguration.verify();
|
||||
@@ -363,8 +380,8 @@ public class DispatcherTest extends StrutsInternalTestCase {
|
||||
destroyConfiguration = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
class DispatcherListenerState {
|
||||
public boolean isInitialized = false;
|
||||
public boolean isDestroyed = false;
|
||||
|
||||
Reference in New Issue
Block a user