001: package com.mockrunner.test.web;
002:
003: import java.io.IOException;
004: import java.rmi.RemoteException;
005:
006: import javax.servlet.ServletException;
007: import javax.servlet.http.HttpServletRequest;
008: import javax.servlet.http.HttpServletResponse;
009:
010: import junit.framework.TestCase;
011:
012: import org.apache.struts.action.ActionForm;
013: import org.apache.struts.action.ActionForward;
014: import org.apache.struts.action.ActionMapping;
015: import org.apache.struts.action.DynaActionForm;
016: import org.apache.struts.action.ExceptionHandler;
017: import org.apache.struts.config.ExceptionConfig;
018:
019: import com.mockrunner.mock.web.MockActionForward;
020: import com.mockrunner.mock.web.MockActionMapping;
021: import com.mockrunner.mock.web.MockHttpServletRequest;
022: import com.mockrunner.mock.web.MockHttpServletResponse;
023: import com.mockrunner.struts.DefaultExceptionHandlerConfig;
024:
025: public class DefaultExceptionHandlerConfigTest extends TestCase {
026: public void testConfigConstructor() throws Exception {
027: ExceptionConfig config = new ExceptionConfig();
028: config.setHandler(TestExceptionHandler.class.getName());
029: config.setType(IOException.class.getName());
030: DefaultExceptionHandlerConfig handlerConfig = new DefaultExceptionHandlerConfig(
031: config);
032: doTestCanHandle(handlerConfig);
033: doTestHandle(handlerConfig);
034: assertSame(config, handlerConfig.getExceptionConfig());
035: assertTrue(handlerConfig.getExceptionHandler() instanceof TestExceptionHandler);
036: }
037:
038: public void testConfigAndHandlerConstructor() throws Exception {
039: ExceptionConfig config = new ExceptionConfig();
040: TestExceptionHandler handler = new TestExceptionHandler();
041: config.setType(IOException.class.getName());
042: DefaultExceptionHandlerConfig handlerConfig = new DefaultExceptionHandlerConfig(
043: handler, config);
044: doTestCanHandle(handlerConfig);
045: doTestHandle(handlerConfig);
046: assertSame(config, handlerConfig.getExceptionConfig());
047: assertSame(handler, handlerConfig.getExceptionHandler());
048: assertSame(TestExceptionHandler.class.getName(), config
049: .getHandler());
050: }
051:
052: public void testExceptionAndHandlerConstructor() throws Exception {
053: TestExceptionHandler handler = new TestExceptionHandler();
054: DefaultExceptionHandlerConfig handlerConfig = new DefaultExceptionHandlerConfig(
055: handler, IOException.class);
056: doTestCanHandle(handlerConfig);
057: doTestHandle(handlerConfig);
058: ExceptionConfig config = handlerConfig.getExceptionConfig();
059: assertSame(TestExceptionHandler.class.getName(), config
060: .getHandler());
061: assertSame(IOException.class.getName(), config.getType());
062: }
063:
064: public void testExceptionConstructor() throws Exception {
065: DefaultExceptionHandlerConfig handlerConfig = new DefaultExceptionHandlerConfig(
066: IOException.class);
067: doTestCanHandle(handlerConfig);
068: ExceptionConfig config = handlerConfig.getExceptionConfig();
069: assertSame(ExceptionHandler.class.getName(), config
070: .getHandler());
071: assertSame(IOException.class.getName(), config.getType());
072: }
073:
074: private void doTestCanHandle(DefaultExceptionHandlerConfig config) {
075: assertFalse(config.canHandle(null));
076: assertFalse(config.canHandle(new Exception()));
077: assertFalse(config.canHandle(new IllegalArgumentException()));
078: assertTrue(config.canHandle(new IOException()));
079: assertTrue(config.canHandle(new RemoteException()));
080: }
081:
082: private void doTestHandle(DefaultExceptionHandlerConfig config)
083: throws Exception {
084: MockActionMapping mapping = new MockActionMapping();
085: ActionForm form = new DynaActionForm();
086: MockHttpServletRequest request = new MockHttpServletRequest();
087: MockHttpServletResponse response = new MockHttpServletResponse();
088: assertNull(config
089: .handle(null, mapping, form, request, response));
090: doTestNotCalled(config);
091: assertNull(config.handle(new Exception(), mapping, form,
092: request, response));
093: Exception exception = new RemoteException();
094: ActionForward forward = (ActionForward) config.handle(
095: exception, mapping, form, request, response);
096: doTestCalled(config, exception, mapping, form, request,
097: response);
098: assertTrue(forward instanceof MockActionForward);
099: assertEquals("testname", ((MockActionForward) forward)
100: .getName());
101: }
102:
103: private void doTestNotCalled(DefaultExceptionHandlerConfig config) {
104: TestExceptionHandler handler = (TestExceptionHandler) config
105: .getExceptionHandler();
106: assertFalse(handler.wasExecuteCalled());
107: }
108:
109: private void doTestCalled(DefaultExceptionHandlerConfig config,
110: Exception exc, ActionMapping mapping, ActionForm form,
111: HttpServletRequest request, HttpServletResponse response) {
112: TestExceptionHandler handler = (TestExceptionHandler) config
113: .getExceptionHandler();
114: assertTrue(handler.wasExecuteCalled());
115: assertSame(exc, handler.getException());
116: assertSame(config.getExceptionConfig(), handler
117: .getExceptionConfig());
118: assertSame(mapping, handler.getMapping());
119: assertSame(form, handler.getForm());
120: assertSame(request, handler.getRequest());
121: assertSame(response, handler.getResponse());
122: }
123:
124: public static class TestExceptionHandler extends ExceptionHandler {
125: private Exception exception;
126: private ExceptionConfig exceptionConfig;
127: private ActionMapping mapping;
128: private ActionForm form;
129: private HttpServletRequest request;
130: private HttpServletResponse response;
131: private boolean executeCalled = false;
132:
133: public ActionForward execute(Exception exc,
134: ExceptionConfig config, ActionMapping mapping,
135: ActionForm form, HttpServletRequest request,
136: HttpServletResponse response) throws ServletException {
137: executeCalled = true;
138: this .exception = exc;
139: this .exceptionConfig = config;
140: this .mapping = mapping;
141: this .form = form;
142: this .request = request;
143: this .response = response;
144: return new MockActionForward("testname");
145: }
146:
147: public Exception getException() {
148: return exception;
149: }
150:
151: public ExceptionConfig getExceptionConfig() {
152: return exceptionConfig;
153: }
154:
155: public ActionForm getForm() {
156: return form;
157: }
158:
159: public ActionMapping getMapping() {
160: return mapping;
161: }
162:
163: public HttpServletRequest getRequest() {
164: return request;
165: }
166:
167: public HttpServletResponse getResponse() {
168: return response;
169: }
170:
171: public boolean wasExecuteCalled() {
172: return executeCalled;
173: }
174: }
175: }
|