/*
* 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 javax.servlet.ServletContext;
import javax.servlet.ServletException;
import de.odysseus.calyxo.base.test.TestModuleGroup;
import de.odysseus.calyxo.base.test.TestModuleContext;
import de.odysseus.calyxo.base.test.TestRequest;
import de.odysseus.calyxo.base.test.TestResponse;
import de.odysseus.calyxo.control.conf.DispatchConfig;
import de.odysseus.calyxo.control.impl.DefaultDispatcher;
import de.odysseus.calyxo.control.misc.DynamicDispatchConfig;
import junit.framework.TestCase;
/**
*
* @author Christoph Beck
*/
public class DefaultDispatcherTest extends TestCase {
private TestRequest request;
private TestModuleContext module1, module2;
/**
* Constructor for DispatcherTest.
* @param arg0
*/
public DefaultDispatcherTest(String arg0) {
super(arg0);
}
protected void setUp() throws ServletException {
request = new TestRequest();
// configure module support
ServletContext context = request.getSession().getServletContext();
TestModuleGroup group = TestModuleGroup.getInstance(context);
// configure test modules
module1 = new TestModuleContext("module1", context);
module2 = new TestModuleContext("module2", context);
group.add(module1);
group.add(module2);
// say, we're in module1
group.setTestModuleContext(request, module1);
}
public void testPath() throws Exception {
DefaultDispatcher dispatcher = new DefaultDispatcher(module1);
DispatchConfig dispatch = new DynamicDispatchConfig("/path", false);
TestResponse response = new TestResponse(false);
dispatcher.dispatch(request, response, dispatch);
assertEquals("/path", request.getRecentRequestDispatchPath());
assertNull(response.getRedirectURL());
}
public void testPathRedirect() throws Exception {
DefaultDispatcher dispatcher = new DefaultDispatcher(module1);
DispatchConfig dispatch = new DynamicDispatchConfig("/path", true);
TestResponse response = new TestResponse(false);
dispatcher.dispatch(request, response, dispatch);
assertNull(request.getRecentRequestDispatchPath());
assertEquals("encodeRedirect(/context/path)", response.getRedirectURL());
}
public void testQuery() throws Exception {
DefaultDispatcher dispatcher = new DefaultDispatcher(module1);
DispatchConfig dispatch = new DynamicDispatchConfig("/path?foo=bar", false);
TestResponse response = new TestResponse(false);
dispatcher.dispatch(request, response, dispatch);
assertEquals("/path?foo=bar", request.getRecentRequestDispatchPath());
assertNull(response.getRedirectURL());
}
public void testParamConfigs() throws Exception {
DefaultDispatcher dispatcher = new DefaultDispatcher(module1);
DynamicDispatchConfig dispatch = new DynamicDispatchConfig("/path", false);
TestResponse response = new TestResponse(false);
dispatch.addParam("foo", "bar");
dispatcher.dispatch(request, response, dispatch);
assertEquals("/path?foo=bar", request.getRecentRequestDispatchPath());
assertNull(response.getRedirectURL());
dispatch.addParam("foo2", "bar2");
dispatcher.dispatch(request, response, dispatch);
assertEquals("/path?foo=bar&foo2=bar2", request.getRecentRequestDispatchPath());
assertNull(response.getRedirectURL());
}
public void testQueryAndParamConfig() throws Exception {
DefaultDispatcher dispatcher = new DefaultDispatcher(module1);
DynamicDispatchConfig dispatch = new DynamicDispatchConfig("/path?foo=bar", false);
TestResponse response = new TestResponse(false);
dispatch.addParam("foo2", "bar2");
dispatcher.dispatch(request, response, dispatch);
assertEquals("/path?foo=bar&foo2=bar2", request.getRecentRequestDispatchPath());
assertNull(response.getRedirectURL());
}
public void testAnchor() throws Exception {
DefaultDispatcher dispatcher = new DefaultDispatcher(module1);
DispatchConfig dispatch = new DynamicDispatchConfig("/path#abc", false);
TestResponse response = new TestResponse(false);
dispatcher.dispatch(request, response, dispatch);
assertEquals("/path#abc", request.getRecentRequestDispatchPath());
assertNull(response.getRedirectURL());
}
public void testQueryAndAnchorAndParamConfig() throws Exception {
DefaultDispatcher dispatcher = new DefaultDispatcher(module1);
DynamicDispatchConfig dispatch = new DynamicDispatchConfig("/path?foo=bar#abc", false);
TestResponse response = new TestResponse(false);
dispatch.addParam("foo2", "bar2");
dispatcher.dispatch(request, response, dispatch);
assertEquals("/path?foo=bar&foo2=bar2#abc", request.getRecentRequestDispatchPath());
assertNull(response.getRedirectURL());
}
public void testAction() throws Exception {
DefaultDispatcher dispatcher = new DefaultDispatcher(module1);
DispatchConfig dispatch = new DynamicDispatchConfig(null, "/action", false);
TestResponse response = new TestResponse(false);
dispatcher.dispatch(request, response, dispatch);
assertEquals("/module1/action", request.getRecentRequestDispatchPath());
assertNull(response.getRedirectURL());
}
public void testActionRedirect() throws Exception {
DefaultDispatcher dispatcher = new DefaultDispatcher(module1);
DispatchConfig dispatch = new DynamicDispatchConfig(null, "/action", true);
TestResponse response = new TestResponse(false);
dispatcher.dispatch(request, response, dispatch);
assertNull(request.getRecentRequestDispatchPath());
assertEquals("encodeRedirect(/context/module1/action)", response.getRedirectURL());
}
public static void main(String[] args) {
junit.textui.TestRunner.run(DefaultDispatcherTest.class);
}
}