Title: [waffle-scm] [271] trunk/core/src/test/java/org/codehaus/waffle/action: Logging stacktraces from exceptions returned by actionMethods

Diff

Modified: trunk/core/src/main/java/org/codehaus/waffle/action/DefaultActionMethodResponseHandler.java (270 => 271)

--- trunk/core/src/main/java/org/codehaus/waffle/action/DefaultActionMethodResponseHandler.java	2007-07-17 18:07:44 UTC (rev 270)
+++ trunk/core/src/main/java/org/codehaus/waffle/action/DefaultActionMethodResponseHandler.java	2007-07-18 11:53:40 UTC (rev 271)
@@ -12,6 +12,7 @@
 
 import org.codehaus.waffle.view.View;
 import org.codehaus.waffle.view.ViewDispatcher;
+import org.codehaus.waffle.monitor.ActionMonitor;
 
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
@@ -31,13 +32,18 @@
  */
 public class DefaultActionMethodResponseHandler implements ActionMethodResponseHandler {
     private final ViewDispatcher viewDispatcher;
+    private final ActionMonitor monitor;
 
-    public DefaultActionMethodResponseHandler(ViewDispatcher viewDispatcher) {
+    public DefaultActionMethodResponseHandler(ViewDispatcher viewDispatcher, ActionMonitor monitor) {
         if (viewDispatcher == null) {
             throw new IllegalArgumentException("ViewDispatcher cannot be null");
         }
+        if (monitor == null) {
+            throw new IllegalArgumentException("ActionMonitor cannot be null");
+        }
 
         this.viewDispatcher = viewDispatcher;
+        this.monitor = monitor;
     }
 
     public void handle(HttpServletRequest request,
@@ -54,7 +60,7 @@
             viewDispatcher.dispatch(request, response, view);
         } else if (returnValue instanceof Exception) {
             Exception exception = (Exception) returnValue;
-            // todo log this occurance
+            monitor.actionMethodReturnedException(exception);
             response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
             handleResponse(response, exception.getMessage());
         } else {

Modified: trunk/core/src/main/java/org/codehaus/waffle/monitor/AbstractWritingMonitor.java (270 => 271)

--- trunk/core/src/main/java/org/codehaus/waffle/monitor/AbstractWritingMonitor.java	2007-07-17 18:07:44 UTC (rev 270)
+++ trunk/core/src/main/java/org/codehaus/waffle/monitor/AbstractWritingMonitor.java	2007-07-18 11:53:40 UTC (rev 271)
@@ -10,13 +10,12 @@
  *****************************************************************************/
 package org.codehaus.waffle.monitor;
 
+import org.codehaus.waffle.action.MethodDefinition;
 import static org.codehaus.waffle.monitor.MonitorLevel.DEBUG;
 import static org.codehaus.waffle.monitor.MonitorLevel.INFO;
 
 import java.util.Set;
 
-import org.codehaus.waffle.action.MethodDefinition;
-
 /**
  * Abstract implementation of Monitor that delegates writing to concrete subclasses.
  * 
@@ -32,6 +31,13 @@
      */
     protected abstract void write(MonitorLevel level, String message);
 
+    /**
+     * Traces an exception. Concrete implementations should provide writing functionality.
+     *
+     * @param exception
+     */
+    protected abstract void trace(Exception exception);
+
     public void defaultActionMethodFound(MethodDefinition methodDefinition) {
         write(INFO, "Default ActionMethod found: " + methodDefinition);
     }
@@ -52,4 +58,7 @@
         write(INFO, "Method name '" + methodName + "' found for key '" + methodKey + "' among keys " + keys);
     }
 
+    public void actionMethodReturnedException(Exception exception) {
+        trace(exception);
+    }
 }

Modified: trunk/core/src/main/java/org/codehaus/waffle/monitor/ActionMonitor.java (270 => 271)

--- trunk/core/src/main/java/org/codehaus/waffle/monitor/ActionMonitor.java	2007-07-17 18:07:44 UTC (rev 270)
+++ trunk/core/src/main/java/org/codehaus/waffle/monitor/ActionMonitor.java	2007-07-18 11:53:40 UTC (rev 271)
@@ -10,10 +10,10 @@
  *****************************************************************************/
 package org.codehaus.waffle.monitor;
 
+import org.codehaus.waffle.action.MethodDefinition;
+
 import java.util.Set;
 
-import org.codehaus.waffle.action.MethodDefinition;
-
 /**
  * Defines events that need to be monitored, eg for debugging purposes.
  * 
@@ -32,6 +32,7 @@
 
     void actionMethodFound(MethodDefinition methodDefinition);
 
-    void methodNameResolved(String methodName, String methodKey, Set<String> keys);    
+    void methodNameResolved(String methodName, String methodKey, Set<String> keys);
 
+    void actionMethodReturnedException(Exception exception);
 }

Modified: trunk/core/src/main/java/org/codehaus/waffle/monitor/CommonsLoggingMonitor.java (270 => 271)

--- trunk/core/src/main/java/org/codehaus/waffle/monitor/CommonsLoggingMonitor.java	2007-07-17 18:07:44 UTC (rev 270)
+++ trunk/core/src/main/java/org/codehaus/waffle/monitor/CommonsLoggingMonitor.java	2007-07-18 11:53:40 UTC (rev 271)
@@ -70,4 +70,10 @@
         }
     }
 
+    @Override
+    protected void trace(Exception exception) {
+        if (log.isErrorEnabled()) {
+            log.error(exception.getMessage(), exception);
+        }
+    }
 }

Modified: trunk/core/src/main/java/org/codehaus/waffle/monitor/SilentMonitor.java (270 => 271)

--- trunk/core/src/main/java/org/codehaus/waffle/monitor/SilentMonitor.java	2007-07-17 18:07:44 UTC (rev 270)
+++ trunk/core/src/main/java/org/codehaus/waffle/monitor/SilentMonitor.java	2007-07-18 11:53:40 UTC (rev 271)
@@ -21,5 +21,8 @@
     protected void write(MonitorLevel level, String message) {
         // write nothing
     }
-    
+
+    protected void trace(Exception exception) {
+        // write nothing
+    }
 }

Modified: trunk/core/src/test/java/org/codehaus/waffle/action/DefaultActionResponseHandlerTest.java (270 => 271)

--- trunk/core/src/test/java/org/codehaus/waffle/action/DefaultActionResponseHandlerTest.java	2007-07-17 18:07:44 UTC (rev 270)
+++ trunk/core/src/test/java/org/codehaus/waffle/action/DefaultActionResponseHandlerTest.java	2007-07-18 11:53:40 UTC (rev 271)
@@ -1,10 +1,10 @@
 package org.codehaus.waffle.action;
 
+import org.codehaus.waffle.monitor.ActionMonitor;
+import org.codehaus.waffle.testmodel.StubMonitor;
+import org.codehaus.waffle.testmodel.StubViewDispatcher;
 import org.codehaus.waffle.view.View;
 import org.codehaus.waffle.view.ViewDispatcher;
-import org.codehaus.waffle.action.ActionMethodResponseHandler;
-import org.codehaus.waffle.action.DefaultActionMethodResponseHandler;
-import org.codehaus.waffle.action.ActionMethodResponse;
 import org.jmock.Mock;
 import org.jmock.MockObjectTestCase;
 
@@ -16,13 +16,25 @@
 
 public class DefaultActionResponseHandlerTest extends MockObjectTestCase {
 
-    public void testConstructor() {
+    public void testConstructorDoesNotAcceptNull() {
         try {
-            new DefaultActionMethodResponseHandler(null);
+            new DefaultActionMethodResponseHandler(null, null);
             fail("IllegalArgumentException expected, null is not a valid argument");
         } catch (IllegalArgumentException expected) {
             // expected
         }
+        try {
+            new DefaultActionMethodResponseHandler(null, new StubMonitor());
+            fail("IllegalArgumentException expected, null is not a valid argument");
+        } catch (IllegalArgumentException expected) {
+            // expected
+        }
+        try {
+            new DefaultActionMethodResponseHandler(new StubViewDispatcher(), null);
+            fail("IllegalArgumentException expected, null is not a valid argument");
+        } catch (IllegalArgumentException expected) {
+            // expected
+        }
     }
 
     public void testHandleWhenResponseIsCommitted() throws Exception {
@@ -37,7 +49,11 @@
         Mock mockViewResolver = mock(ViewDispatcher.class);
         ViewDispatcher viewDispatcher = (ViewDispatcher) mockViewResolver.proxy();
 
-        ActionMethodResponseHandler handler = new DefaultActionMethodResponseHandler(viewDispatcher);
+        // Mock ActionMonitor
+        Mock mockActionMonitor = mock(ActionMonitor.class);
+        ActionMonitor actionMonitor = (ActionMonitor) mockActionMonitor.proxy();
+
+        ActionMethodResponseHandler handler = new DefaultActionMethodResponseHandler(viewDispatcher, actionMonitor);
         handler.handle(null, response, null);
     }
 
@@ -62,7 +78,11 @@
         mockViewResolver.expects(once()).method("dispatch");
         ViewDispatcher viewDispatcher = (ViewDispatcher) mockViewResolver.proxy();
 
-        ActionMethodResponseHandler handler = new DefaultActionMethodResponseHandler(viewDispatcher);
+        // Mock ActionMonitor
+        Mock mockActionMonitor = mock(ActionMonitor.class);
+        ActionMonitor actionMonitor = (ActionMonitor) mockActionMonitor.proxy();
+
+        ActionMethodResponseHandler handler = new DefaultActionMethodResponseHandler(viewDispatcher, actionMonitor);
         handler.handle(request, response, actionMethodResponse);
     }
 
@@ -89,15 +109,20 @@
         Mock mockViewResolver = mock(ViewDispatcher.class);
         ViewDispatcher viewDispatcher = (ViewDispatcher) mockViewResolver.proxy();
 
-        ActionMethodResponseHandler handler = new DefaultActionMethodResponseHandler(viewDispatcher);
+        // Mock ActionMonitor
+        Mock mockActionMonitor = mock(ActionMonitor.class);
+        ActionMonitor actionMonitor = (ActionMonitor) mockActionMonitor.proxy();
+
+        ActionMethodResponseHandler handler = new DefaultActionMethodResponseHandler(viewDispatcher, actionMonitor);
         handler.handle(request, response, actionMethodResponse);
 
         assertEquals("Mmmmm Waffles!", out.buffer.toString());
     }
 
     public void testResponseIsAnException() throws IOException, ServletException {
+        Exception exception = new Exception("error for testing");
         ActionMethodResponse actionMethodResponse = new ActionMethodResponse();
-        actionMethodResponse.setReturnValue(new Exception("error for testing"));
+        actionMethodResponse.setReturnValue(exception);
 
         // Mock HttpServletRequest
         Mock mockRequest = mock(HttpServletRequest.class);
@@ -120,7 +145,13 @@
         Mock mockViewResolver = mock(ViewDispatcher.class);
         ViewDispatcher viewDispatcher = (ViewDispatcher) mockViewResolver.proxy();
 
-        ActionMethodResponseHandler handler = new DefaultActionMethodResponseHandler(viewDispatcher);
+        // Mock ActionMonitor
+        Mock mockActionMonitor = mock(ActionMonitor.class);
+        // must fire the exception to the monitor
+        mockActionMonitor.expects(once()).method("actionMethodReturnedException").with(same(exception));
+        ActionMonitor actionMonitor = (ActionMonitor) mockActionMonitor.proxy();
+
+        ActionMethodResponseHandler handler = new DefaultActionMethodResponseHandler(viewDispatcher, actionMonitor);
         handler.handle(request, response, actionMethodResponse);
 
         assertEquals("error for testing", out.buffer.toString());
@@ -149,7 +180,11 @@
         Mock mockViewResolver = mock(ViewDispatcher.class);
         ViewDispatcher viewDispatcher = (ViewDispatcher) mockViewResolver.proxy();
 
-        ActionMethodResponseHandler handler = new DefaultActionMethodResponseHandler(viewDispatcher);
+        // Mock ActionMonitor
+        Mock mockActionMonitor = mock(ActionMonitor.class);
+        ActionMonitor actionMonitor = (ActionMonitor) mockActionMonitor.proxy();
+
+        ActionMethodResponseHandler handler = new DefaultActionMethodResponseHandler(viewDispatcher, actionMonitor);
         handler.handle(request, response, null);
     }
 

Modified: trunk/core/src/test/java/org/codehaus/waffle/monitor/AbstractWritingMonitorTest.java (270 => 271)

--- trunk/core/src/test/java/org/codehaus/waffle/monitor/AbstractWritingMonitorTest.java	2007-07-17 18:07:44 UTC (rev 270)
+++ trunk/core/src/test/java/org/codehaus/waffle/monitor/AbstractWritingMonitorTest.java	2007-07-18 11:53:40 UTC (rev 271)
@@ -10,18 +10,17 @@
  *****************************************************************************/
 package org.codehaus.waffle.monitor;
 
-import static org.junit.Assert.assertEquals;
-
-import java.lang.reflect.Method;
-import java.util.Set;
-
 import org.codehaus.waffle.action.MethodDefinition;
 import org.jmock.Mockery;
 import org.jmock.integration.junit4.JMock;
 import org.jmock.integration.junit4.JUnit4Mockery;
+import static org.junit.Assert.assertEquals;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
+import java.lang.reflect.Method;
+import java.util.Set;
+
 /**
  * 
  * @author Mauro Talevi
@@ -32,7 +31,7 @@
     private final Mockery mockery = new JUnit4Mockery();
 
     @Test
-    public void canWriteEvents() {
+    public void canWriteMessages() {
         final StringBuffer sb = new StringBuffer();
         final AbstractWritingMonitor monitor = new AbstractWritingMonitor() {
 
@@ -41,6 +40,10 @@
                 sb.append(message).append("\n");
             }
 
+            @Override
+            protected void trace(Exception exception) {
+                // will not be tested here
+            }
         };
         MethodDefinition methodDefinition = mockMethodDefinition();
         monitor.actionMethodFound(methodDefinition);

Modified: trunk/core/src/test/java/org/codehaus/waffle/testmodel/StubMonitor.java (270 => 271)

--- trunk/core/src/test/java/org/codehaus/waffle/testmodel/StubMonitor.java	2007-07-17 18:07:44 UTC (rev 270)
+++ trunk/core/src/test/java/org/codehaus/waffle/testmodel/StubMonitor.java	2007-07-18 11:53:40 UTC (rev 271)
@@ -1,10 +1,10 @@
 package org.codehaus.waffle.testmodel;
 
-import java.util.Set;
-
 import org.codehaus.waffle.action.MethodDefinition;
 import org.codehaus.waffle.monitor.ActionMonitor;
 
+import java.util.Set;
+
 public class StubMonitor implements ActionMonitor {
 
     public void defaultActionMethodFound(MethodDefinition methodDefinition) {
@@ -22,5 +22,6 @@
     public void methodNameResolved(String methodName, String methodKey, Set<String> keys) {
     }
 
-
+    public void actionMethodReturnedException(Exception exception) {
+    }
 }


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to