webwork/src/test/webwork/dispatcher ServletDispatcherTestCase.java,NONE,1.1
[email protected] Fri, 31 Oct 2003 23:17:03 -0800
| Newsgroups | gmane.comp.java.open-symphony.cvs |
|---|---|
| Message-ID | <[email protected]> |
Update of /cvsroot/opensymphony/webwork/src/test/webwork/dispatcher
In directory sc8-pr-cvs1:/tmp/cvs-serv25627/test/webwork/dispatcher
Added Files:
ServletDispatcherTestCase.java
Log Message:
Moved test classes to test tree
--- NEW FILE: ServletDispatcherTestCase.java ---
package webwork.dispatcher;
import com.mockobjects.dynamic.C;
import com.mockobjects.dynamic.Mock;
import com.mockobjects.servlet.MockServletConfig;
import com.mockobjects.servlet.MockServletContext;
import com.mockobjects.servlet.MockHttpSession;
import webwork.action.ActionContext;
import webwork.action.Action;
import webwork.action.factory.ActionFactory;
import webwork.dispatcher.ServletDispatcher;
import webwork.TestCaseSupport;
import webwork.test.actions.RuntimeExceptionAction;
import webwork.test.actions.ErrorAction;
import webwork.test.actions.CheckedExceptionAction;
import webwork.util.ServletValueStack;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;
import java.util.Map;
import java.util.List;
import java.security.Principal;
/**
*
* ServletDispatcherTestCase
* @author Jason Carreira
* Created Jul 21, 2003 10:56:03 AM
*/
public class ServletDispatcherTestCase extends TestCaseSupport {
protected HttpServletRequest request;
protected HttpServletResponse response;
protected ServletConfig servletConfig;
protected ServletDispatcher dispatcher;
protected Mock requestMock;
protected Mock responseMock;
protected ServletContext servletContext;
private static final String RUNTIME_EXCEPTION_ACTION_NAME = "runtimeException";
private static final String ERROR_ACTION_NAME = "error";
private static final String CHECKED_EXCEPTION_ACTION_NAME = "checkedException";
static {
ActionFactory.setActionFactory(new ActionFactory() {
/**
* Returns the action object for the specified action or
* a matching action on the action factory delegation chain.
*
* @param aName name of action to check for a match
*/
public Action getActionImpl(String aName)
throws Exception {
if (RUNTIME_EXCEPTION_ACTION_NAME.equals(aName)) {
return new RuntimeExceptionAction();
} else if (ERROR_ACTION_NAME.equals(aName)) {
return new ErrorAction();
} else if (CHECKED_EXCEPTION_ACTION_NAME.equals(aName)) {
return new CheckedExceptionAction();
}
throw new IllegalArgumentException("Action name " + aName + " not known.");
}
});
}
public ServletDispatcherTestCase(String testName) {
super(testName);
}
public void testActionContextCleanupAfterRuntimeException() throws Exception {
doTestActionContextCleanup(500,RUNTIME_EXCEPTION_ACTION_NAME);
}
public void testActionContextCleanupAfterError() throws Exception {
doTestActionContextCleanup(404,ERROR_ACTION_NAME);
}
public void testActionContextCleanupAfterCheckedException() throws Exception {
doTestActionContextCleanup(500,CHECKED_EXCEPTION_ACTION_NAME);
}
public void doTestActionContextCleanup(int errorCode, final String actionName) throws Exception {
try {
ActionContext context = ActionContext.getContext();
Map startTable = context.getTable();
assertNotNull(context);
for (int i=0; i<20; i++) {
if (i != 0) {
setUp();
}
requestMock.expectAndReturn("getServletPath","/" + actionName + ".action");
responseMock.expect("sendError",C.args(C.eq(errorCode),C.isA(String.class)));
dispatcher.init(servletConfig);
dispatcher.service(request,response);
final ActionContext newContext = ActionContext.getContext();
Map endTable = newContext.getTable();
assertEquals(startTable,endTable);
List actions = (List) newContext.get("action.chain");
assertNull(actions);
verify();
}
} catch (ServletException e) {
e.printStackTrace();
fail();
}
}
protected void verify() throws Exception {
super.tearDown();
requestMock.verify();
responseMock.verify();
((MockServletConfig)servletConfig).verify();
((MockServletContext)servletContext).verify();
}
protected void setUp() throws Exception {
super.setUp();
requestMock = new Mock(HttpServletRequest.class);
requestMock.expectAndReturn("getHeader",C.eq("Content-Type"),null);
requestMock.expectAndReturn("getAttribute",C.eq("javax.servlet.include.servlet_path"),null);
requestMock.expectAndReturn("getLocale",Locale.getDefault());
requestMock.expectAndReturn("getSession",new MockHttpSession());
requestMock.expectAndReturn("getUserPrincipal",new Principal() {
/**
* Returns the name of this principal.
*
* @return the name of this principal.
*/
public String getName() {
return "ServletDispatcherTestCase";
}
});
requestMock.matchAndReturn("getAttribute",C.eq(ServletValueStack.STACK_NAME),null);
requestMock.expect("setAttribute",C.args(C.eq(ServletValueStack.STACK_NAME),C.IS_ANYTHING));
request = (HttpServletRequest) requestMock.proxy();
responseMock = new Mock(HttpServletResponse.class);
response = (HttpServletResponse) responseMock.proxy();
servletConfig = new MockServletConfig();
servletContext = new MockServletContext();
((MockServletConfig)servletConfig).setServletContext(servletContext);
((MockServletContext)servletContext).setupGetAttribute("javax.servlet.context.tempdir",null);
dispatcher = new ServletDispatcher();
}
}
-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
Does SourceForge.net help you be more productive? Does it
help you create better code? SHARE THE LOVE, and help us help
YOU! Click Here: http://sourceforge.net/donate/