- Dispatcher's setConfigurationManager() method has no effect



git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@476696 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Toby Jee
2006-11-19 03:56:18 +00:00
parent d67a68a044
commit ddbff80121
9 changed files with 75 additions and 22 deletions
@@ -53,6 +53,7 @@ public abstract class StrutsTestCase extends XWorkTestCase {
params = new HashMap<String,String>();
}
Dispatcher du = new Dispatcher(new MockServletContext(), params);
du.init();
Dispatcher.setInstance(du);
configurationManager = du.getConfigurationManager();
configuration = configurationManager.getConfiguration();
@@ -189,14 +189,19 @@ public class Dispatcher {
dispatcherListeners.remove(listener);
}
private ServletContext servletContext;
private Map<String, String> initParams;
/**
* Create the Dispatcher instance for a given ServletContext and set of initialization parameters.
*
* @param servletContext Our servlet context
* @param initParams The set of initialization parameters
*/
public Dispatcher(ServletContext servletContext, Map initParams) {
init(servletContext, initParams);
public Dispatcher(ServletContext servletContext, Map<String, String> initParams) {
this.servletContext = servletContext;
this.initParams = initParams;
}
/**
@@ -275,15 +280,18 @@ public class Dispatcher {
/**
* Load configurations, including both XML and zero-configuration strategies,
* and update optional settings, including whether to reload configurations and resource files.
*
* @param servletContext Our servlet context
* @param initParams The set of initialization parameters
*/
private void init(final ServletContext servletContext, final Map<String,String> initParams) {
public void init() {
configurationManager = new ConfigurationManager(BeanSelectionProvider.DEFAULT_BEAN_NAME);
if (configurationManager == null) {
configurationManager = new ConfigurationManager(BeanSelectionProvider.DEFAULT_BEAN_NAME);
}
// 1] Configuration: legacy properties file (struts.properties)
configurationManager.addConfigurationProvider(new LegacyPropertiesConfigurationProvider());
// 2] Configuration: traditional xml configuration (eg. struts-default.xml, struts-plugin.xml, struts.xml)
// Load traditional xml configuration
String configPaths = initParams.get("config");
if (configPaths == null) {
@@ -302,6 +310,7 @@ public class Dispatcher {
}
}
// 3] Configuration: zero-configuration stuff
// Load configuration from a scan of the classloader
String packages = initParams.get("actionPackages");
if (packages != null) {
@@ -314,6 +323,7 @@ public class Dispatcher {
}
}
// 4] Configuration: custom ConfigurationProviders
String configProvs = initParams.get("configProviders");
if (configProvs != null) {
String[] classes = configProvs.split("\\s*[,]\\s*");
@@ -332,6 +342,7 @@ public class Dispatcher {
}
}
// 5] Configurations: Filter's init-parameters as constants to be injected
// Load filter init params as constants
configurationManager.addConfigurationProvider(new ConfigurationProvider() {
public void destroy() {}
@@ -344,6 +355,8 @@ public class Dispatcher {
}
});
// 6] Configurations: Alias standard Struts2 beans
configurationManager.addConfigurationProvider(new BeanSelectionProvider());
// Preload the configuration
Configuration config = configurationManager.getConfiguration();
@@ -207,8 +207,11 @@ public class FilterDispatcher implements StrutsStatics, Filter {
* @param filterConfig The filter configuration
*/
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
dispatcher = createDispatcher(filterConfig);
this.filterConfig = filterConfig;
dispatcher.init();
String param = filterConfig.getInitParameter("packages");
String packages = "org.apache.struts2.static template org.apache.struts2.interceptor.debugging";
if (param != null) {
@@ -388,6 +391,7 @@ public class FilterDispatcher implements StrutsStatics, Filter {
*/
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
ServletContext servletContext = getServletContext();
@@ -187,6 +187,7 @@ public class Jsr168Dispatcher extends GenericPortlet implements StrutsStatics,
Dispatcher.setPortletSupportActive(true);
dispatcherUtils = new Dispatcher(ServletContextHolderListener.getServletContext(), params);
dispatcherUtils.init();
// For testability
if (factory == null) {
@@ -110,13 +110,13 @@ public class DispatcherTest extends StrutsTestCase {
public void testConfigurationManager() {
Dispatcher du = null;
final InternalConfigurationManager configurationManager = new InternalConfigurationManager();
InternalConfigurationManager configurationManager = new InternalConfigurationManager();
try {
du = new Dispatcher(new MockServletContext(), new HashMap<String, String>()) {
{
setConfigurationManager(configurationManager);
}
};
du = new Dispatcher(new MockServletContext(), new HashMap<String, String>());
du.setConfigurationManager(configurationManager);
du.init();
Dispatcher.setInstance(du);
assertFalse(configurationManager.destroyConfiguration);
@@ -44,6 +44,8 @@ import com.mockobjects.servlet.MockFilterChain;
import com.opensymphony.xwork2.ObjectFactory;
import com.opensymphony.xwork2.config.ConfigurationManager;
import com.opensymphony.xwork2.config.impl.DefaultConfiguration;
import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.inject.ContainerBuilder;
/**
* FilterDispatcher TestCase.
@@ -128,8 +130,15 @@ public class FilterDispatcherTest extends StrutsTestCase {
final InnerDispatcher _dispatcher = new InnerDispatcher(servletContext);
Dispatcher.setInstance(null);
DefaultConfiguration conf = new DefaultConfiguration() {
@Override
public Container getContainer() {
return new ContainerBuilder().create(false);
}
};
ConfigurationManager confManager = new ConfigurationManager();
confManager.setConfiguration(new DefaultConfiguration());
confManager.setConfiguration(conf);
_dispatcher.setConfigurationManager(confManager);
@@ -142,8 +151,8 @@ public class FilterDispatcherTest extends StrutsTestCase {
return _dispatcher;
}
};
filter.init(filterConfig);
filter.setActionMapper(new InnerActionMapper());
filter.init(filterConfig);
filter.doFilter(req, res, chain);
assertTrue(_dispatcher.wrappedRequest);
@@ -33,7 +33,6 @@ import javax.servlet.http.HttpServletResponse;
import junit.framework.TestCase;
import org.apache.struts2.StrutsConstants;
import org.apache.struts2.dispatcher.mapper.ActionMapper;
import org.apache.struts2.dispatcher.mapper.ActionMapping;
import org.springframework.mock.web.MockFilterConfig;
@@ -124,7 +123,7 @@ public class FilterTest extends TestCase {
filterDispatcherCreateDispatcherCount++;
return _dispatcher2;
}
@Override
public String toString() {
return "filterDispatcher";
@@ -132,32 +131,35 @@ public class FilterTest extends TestCase {
};
}
public void testUsingFilterDispatcherOnly() throws Exception {
ObjectFactory oldObjecFactory = ObjectFactory.getObjectFactory();
try {
ObjectFactory.setObjectFactory(new InnerObjectFactory());
filterDispatcher.setActionMapper(new FilterTest.InnerMapper());
assertEquals(cleanUpFilterCreateDispatcherCount, 0);
assertEquals(filterDispatcherCreateDispatcherCount, 0);
assertFalse(_dispatcher1.init);
assertFalse(_dispatcher1.prepare);
assertFalse(_dispatcher1.wrapRequest);
assertFalse(_dispatcher1.service);
assertFalse(_dispatcher2.init);
assertFalse(_dispatcher2.prepare);
assertFalse(_dispatcher2.wrapRequest);
assertFalse(_dispatcher2.service);
filterDispatcher.init(filterConfig);
FilterDispatcher.setActionMapper(new FilterTest.InnerMapper());
filterDispatcher.doFilter(request, response, filterChain2);
filterDispatcher.destroy();
// we are using FilterDispatcher only, so cleanUp filter's Dispatcher should not be created.
assertEquals(cleanUpFilterCreateDispatcherCount, 0);
assertEquals(filterDispatcherCreateDispatcherCount, 1);
assertFalse(_dispatcher1.init);
assertFalse(_dispatcher1.prepare);
assertFalse(_dispatcher1.wrapRequest);
assertFalse(_dispatcher1.service);
assertTrue(_dispatcher2.init);
assertTrue(_dispatcher2.prepare);
assertTrue(_dispatcher2.wrapRequest);
assertTrue(_dispatcher2.service);
@@ -168,6 +170,7 @@ public class FilterTest extends TestCase {
}
}
public void testUsingFilterDispatcherOnly_Multiple() throws Exception {
ObjectFactory oldObjecFactory = ObjectFactory.getObjectFactory();
try {
@@ -179,11 +182,14 @@ public class FilterTest extends TestCase {
assertFalse(_dispatcher1.prepare);
assertFalse(_dispatcher1.wrapRequest);
assertFalse(_dispatcher1.service);
assertFalse(_dispatcher1.cleanUp);
assertFalse(_dispatcher2.prepare);
assertFalse(_dispatcher2.wrapRequest);
assertFalse(_dispatcher2.service);
assertFalse(_dispatcher2.cleanUp);
filterDispatcher.init(filterConfig);
FilterDispatcher.setActionMapper(new FilterTest.InnerMapper());
filterDispatcher.doFilter(request, response, filterChain2);
filterDispatcher.doFilter(request, response, filterChain2);
filterDispatcher.destroy();
@@ -194,9 +200,11 @@ public class FilterTest extends TestCase {
assertFalse(_dispatcher1.prepare);
assertFalse(_dispatcher1.wrapRequest);
assertFalse(_dispatcher1.service);
assertFalse(_dispatcher1.cleanUp);
assertTrue(_dispatcher2.prepare);
assertTrue(_dispatcher2.wrapRequest);
assertTrue(_dispatcher2.service);
assertTrue(_dispatcher2.cleanUp);
assertTrue(Dispatcher.getInstance() == null);
}
finally {
@@ -204,6 +212,8 @@ public class FilterTest extends TestCase {
}
}
/*public void testUsingCleanUpAndFilterDispatcher() throws Exception {
ObjectFactory oldObjecFactory = ObjectFactory.getObjectFactory();
try {
@@ -281,12 +291,19 @@ public class FilterTest extends TestCase {
class InnerDispatcher extends Dispatcher {
public boolean init = false;
public boolean prepare = false;
public boolean wrapRequest = false;
public boolean service = false;
public boolean cleanUp = false;
public InnerDispatcher(ServletContext servletContext) {
super(servletContext, new HashMap());
super(servletContext, new HashMap<String, String>());
}
@Override
public void init() {
init= true;
}
@Override
@@ -304,6 +321,11 @@ public class FilterTest extends TestCase {
public void serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext context, ActionMapping mapping) throws ServletException {
service = true;
}
@Override
public void cleanup() {
cleanUp = true;
}
}
class NullInnerMapper implements ActionMapper {
@@ -108,6 +108,7 @@ public abstract class AbstractTagTest extends StrutsTestCase {
mockContainer = new Mock(Container.class);
Dispatcher du = new Dispatcher(pageContext.getServletContext(), new HashMap());
du.init();
Dispatcher.setInstance(du);
du.setConfigurationManager(configurationManager);
session = new SessionMap(request);
@@ -80,7 +80,9 @@ public class PortletUrlTagTest extends MockObjectTestCase {
public void setUp() throws Exception {
super.setUp();
Dispatcher.setInstance(new Dispatcher(null, new HashMap()));
Dispatcher du = new Dispatcher(null, new HashMap());
du.init();
Dispatcher.setInstance(du);
mockPortletApiAvailable();