/*
* Copyright 2004, 2005, 2006 Odysseus Software GmbH
*
* Licensed 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 de.odysseus.calyxo.control.impl;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import de.odysseus.calyxo.base.I18nSupport;
import de.odysseus.calyxo.base.conf.ConfigException;
import de.odysseus.calyxo.base.test.TestRequest;
import de.odysseus.calyxo.base.test.TestResponse;
import de.odysseus.calyxo.base.test.TestServletConfig;
import de.odysseus.calyxo.base.test.TestServletContext;
import de.odysseus.calyxo.base.test.TestSession;
import de.odysseus.calyxo.base.access.AccessSupport;
import de.odysseus.calyxo.control.base.ControlModuleContext;
import de.odysseus.calyxo.control.base.ControlModuleGroup;
import de.odysseus.calyxo.control.base.ControlModuleMapping;
import de.odysseus.calyxo.control.impl.DefaultModule;
import junit.framework.TestCase;
/**
*
* @author Christoph Beck
*/
public class DefaultModuleTest extends TestCase {
/**
* Constructor for ModuleTest.
* @param arg0
*/
public DefaultModuleTest(String arg0) {
super(arg0);
}
private DefaultModule createModule(ServletContext context, String name, String pattern, Map params) throws ConfigException, ServletException {
TestServletConfig config = new TestServletConfig(name, context, params);
DefaultModule module = new DefaultModule();
module.init(new ControlModuleContext(config, new ControlModuleMapping(pattern), null));
return module;
}
public void testInitDestroy() throws ConfigException, ServletException {
HashMap params = new HashMap();
params.put(
"config",
"/de/odysseus/calyxo/control/calyxo-control-config-empty.xml"
);
ServletContext context = new TestServletContext();
DefaultModule module = createModule(context, "module1", "/module1/*", params);
assertNotNull(I18nSupport.getInstance(module.getContext()));
assertNotNull(AccessSupport.getInstance(module.getContext()));
module.destroy();
assertNull(I18nSupport.getInstance(module.getContext()));
assertNull(AccessSupport.getInstance(module.getContext()));
}
public void testProcess() throws ConfigException, ServletException, IOException {
HashMap params = new HashMap();
params.put(
"config",
"/de/odysseus/calyxo/control/calyxo-control-config.xml"
);
ServletContext context = new TestServletContext();
ControlModuleGroup group = ControlModuleGroup.getInstance(context);
DefaultModule module1 = createModule(context, "module1", "/module1/*", params);
DefaultModule module2 = createModule(context, "module2", "/module2/*", params);
group.add((ControlModuleContext)module1.getContext());
group.add((ControlModuleContext)module2.getContext());
TestRequest request = new TestRequest(new TestSession(context));
TestResponse response = new TestResponse();
module1.process(request, response, "/index");
assertEquals("/module1/index.jsp", request.getRecentRequestDispatchPath());
module2.process(request, response, "/index");
assertEquals("/module2/index.jsp", request.getRecentRequestDispatchPath());
module1.process(request, response, "/module2");
assertEquals("/module2/index", request.getRecentRequestDispatchPath());
}
public static void main(String[] args) {
junit.textui.TestRunner.run(DefaultModuleTest.class);
}
}