Title: [waffle-scm] [600] trunk/waffle-core/src/test/java/org/codehaus/waffle/servlet: WAFFLE-64: Added servletServiceRequested method to ServletMonitor

Diff

Modified: trunk/waffle-core/src/main/java/org/codehaus/waffle/monitor/AbstractWritingMonitor.java (599 => 600)

--- trunk/waffle-core/src/main/java/org/codehaus/waffle/monitor/AbstractWritingMonitor.java	2008-04-01 13:22:43 UTC (rev 599)
+++ trunk/waffle-core/src/main/java/org/codehaus/waffle/monitor/AbstractWritingMonitor.java	2008-04-01 16:25:53 UTC (rev 600)
@@ -90,6 +90,7 @@
         levels.put("instanceRegistered", DEBUG);
         levels.put("nonCachingComponentRegistered", DEBUG);
         levels.put("servletServiceFailed", WARN);
+        levels.put("servletServiceRequested", DEBUG);
         levels.put("controllerValidatorNotFound", WARN);
         levels.put("methodDefinitionNotFound", WARN);        
         levels.put("validationFailed", WARN);  
@@ -140,6 +141,7 @@
         messages.put("instanceRegistered", "Registered instance {1} with key {0}");
         messages.put("nonCachingComponentRegistered", "Registered non-caching component of type {1} with key {0} and parameters {2}");
         messages.put("servletServiceFailed", "Servlet service failed: {0}");
+        messages.put("servletServiceRequested", "Servlet service requested with parameters: {0}");
         messages.put("controllerValidatorNotFound", "Controller validator {0} not found: defaulting to controller {1}");
         messages.put("methodDefinitionNotFound", "Method definition not found in controller definition {0}");        
         messages.put("validationFailed", "Validation failed: {0}");  
@@ -313,6 +315,10 @@
         write("servletServiceFailed", cause);        
     }
     
+    public void servletServiceRequested(Map<String, List<String>> parameters){
+        write("servletServiceRequested", parameters);        
+    }
+
     public void controllerValidatorNotFound(String controllerValidatorName, String controllerName) {
         write("controllerValidatorNotFound", controllerValidatorName, controllerName);
     }

Modified: trunk/waffle-core/src/main/java/org/codehaus/waffle/monitor/ServletMonitor.java (599 => 600)

--- trunk/waffle-core/src/main/java/org/codehaus/waffle/monitor/ServletMonitor.java	2008-04-01 13:22:43 UTC (rev 599)
+++ trunk/waffle-core/src/main/java/org/codehaus/waffle/monitor/ServletMonitor.java	2008-04-01 16:25:53 UTC (rev 600)
@@ -10,6 +10,10 @@
  *****************************************************************************/
 package org.codehaus.waffle.monitor;
 
+import java.util.List;
+import java.util.Map;
+
+
 /**
  * A monitor for servlet-related events
  * 
@@ -19,4 +23,6 @@
 
     void servletServiceFailed(Exception cause);
     
+    void servletServiceRequested(Map<String, List<String>> parameters);
+
 }

Modified: trunk/waffle-core/src/main/java/org/codehaus/waffle/servlet/WaffleServlet.java (599 => 600)

--- trunk/waffle-core/src/main/java/org/codehaus/waffle/servlet/WaffleServlet.java	2008-04-01 13:22:43 UTC (rev 599)
+++ trunk/waffle-core/src/main/java/org/codehaus/waffle/servlet/WaffleServlet.java	2008-04-01 16:25:53 UTC (rev 600)
@@ -10,9 +10,24 @@
  *****************************************************************************/
 package org.codehaus.waffle.servlet;
 
-import org.codehaus.waffle.ComponentRegistry;
+import static java.util.Arrays.asList;
 import static org.codehaus.waffle.Constants.VIEW_PREFIX_KEY;
 import static org.codehaus.waffle.Constants.VIEW_SUFFIX_KEY;
+
+import java.io.IOException;
+import java.lang.reflect.Method;
+import java.util.Arrays;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.codehaus.waffle.ComponentRegistry;
 import org.codehaus.waffle.action.ActionMethodExecutor;
 import org.codehaus.waffle.action.ActionMethodInvocationException;
 import org.codehaus.waffle.action.ActionMethodResponse;
@@ -31,13 +46,6 @@
 import org.codehaus.waffle.view.RedirectView;
 import org.codehaus.waffle.view.View;
 
-import javax.servlet.ServletException;
-import javax.servlet.http.HttpServlet;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import java.io.IOException;
-import java.lang.reflect.Method;
-
 /**
  * Waffle's FrontController for handling user requests.
  *
@@ -154,6 +162,7 @@
      */
     protected void service(HttpServletRequest request,
                            HttpServletResponse response) throws ServletException, IOException {
+        servletMonitor.servletServiceRequested(parametersOf(request));
         ContextContainer requestContainer = RequestLevelContainer.get();
         ErrorsContext errorsContext = requestContainer.getComponentInstanceOfType(ErrorsContext.class);
 
@@ -204,6 +213,16 @@
         }
     }
 
+    @SuppressWarnings("unchecked")
+    private Map<String, List<String>> parametersOf(HttpServletRequest request) {
+        Map<String, List<String>> parameters = new HashMap<String, List<String>>();
+        for ( Enumeration<String> e = request.getParameterNames(); e.hasMoreElements(); ){
+            String name = e.nextElement();
+            parameters.put(name, asList(request.getParameterValues(name)));
+        }
+        return parameters;
+    }
+
     /**
      * Determine if PRG paradigm is used from the @PRG annotation of the action method
      * 

Modified: trunk/waffle-core/src/test/java/org/codehaus/waffle/servlet/WaffleServletTest.java (599 => 600)

--- trunk/waffle-core/src/test/java/org/codehaus/waffle/servlet/WaffleServletTest.java	2008-04-01 13:22:43 UTC (rev 599)
+++ trunk/waffle-core/src/test/java/org/codehaus/waffle/servlet/WaffleServletTest.java	2008-04-01 16:25:53 UTC (rev 600)
@@ -10,6 +10,22 @@
  *****************************************************************************/
 package org.codehaus.waffle.servlet;
 
+import java.io.IOException;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.Map;
+
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
 import org.codehaus.waffle.ComponentRegistry;
 import org.codehaus.waffle.Constants;
 import org.codehaus.waffle.WaffleException;
@@ -41,20 +57,6 @@
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
-import javax.servlet.ServletConfig;
-import javax.servlet.ServletContext;
-import javax.servlet.ServletException;
-import javax.servlet.ServletOutputStream;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import java.io.IOException;
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Enumeration;
-import java.util.List;
-
 /**
  * 
  * @author Michael Ward
@@ -140,7 +142,7 @@
         // Mock HttpServletRequest
         final HttpServletRequest request = mockery.mock(HttpServletRequest.class);
         mockery.checking(new Expectations() {{
-            one(request).getParameterNames();
+            atLeast(1).of(request).getParameterNames();
             will(returnValue(enumeration));
             one(request).getMethod();
             will(returnValue("get"));
@@ -221,7 +223,7 @@
         // Mock HttpServletRequest
         final HttpServletRequest request = mockery.mock(HttpServletRequest.class);
         mockery.checking(new Expectations() {{
-            one(request).getParameterNames();
+            atLeast(1).of(request).getParameterNames();
             will(returnValue(enumeration));
             one(request).getMethod();
             will(returnValue("post"));
@@ -275,7 +277,7 @@
         Assert.assertEquals(1, nonDispatchingController.getCount());
     }
 
-    @SuppressWarnings("serial")
+    @SuppressWarnings({ "serial", "unchecked" })
     @Test(expected = ServletException.class)
     public void cannotServiceIfControllerNotFound() throws Exception {
         // Mock ErrorsContext
@@ -298,9 +300,14 @@
         // Mock ServletConfig
         final ServletConfig servletConfig = mockery.mock(ServletConfig.class);
 
+        List<?> list = Collections.EMPTY_LIST;
+        final Enumeration<?> enumeration = Collections.enumeration(list);
+
         // Mock HttpServletRequest
         final HttpServletRequest request = mockery.mock(HttpServletRequest.class);
         mockery.checking(new Expectations() {{
+            atLeast(1).of(request).getParameterNames();
+            will(returnValue(enumeration));
             one(request).getServletPath();
             will(returnValue("/foobar"));
         }});
@@ -319,11 +326,21 @@
             }
         };
 
+        // Mock ServletMonitor
+        final ServletMonitor servletMonitor = mockery.mock(ServletMonitor.class);
+        mockery.checking(new Expectations() {{
+            allowing(servletMonitor).servletServiceRequested(with(any(Map.class)));
+        }});
+        
         // Set up what normally would happen via "init()"
         Field actionFactoryField = WaffleServlet.class.getDeclaredField("controllerDefinitionFactory");
         actionFactoryField.setAccessible(true);
         actionFactoryField.set(waffleServlet, controllerDefinitionFactory);
 
+        Field servletMonitorField = WaffleServlet.class.getDeclaredField("servletMonitor");
+        servletMonitorField.setAccessible(true);
+        servletMonitorField.set(waffleServlet, servletMonitor);
+
         waffleServlet.service(request, null);
     }
 
@@ -353,7 +370,7 @@
         // Mock HttpServletRequest
         final HttpServletRequest request  = mockery.mock(HttpServletRequest.class);
         mockery.checking(new Expectations() {{
-            one(request).getParameterNames();
+            atLeast(1).of(request).getParameterNames();
             will(returnValue(enumeration));
         }});
 
@@ -402,7 +419,7 @@
         waffleServlet.service(request, response);
     }
 
-    @SuppressWarnings({"serial", "ThrowableInstanceNeverThrown"})
+    @SuppressWarnings({"serial", "ThrowableInstanceNeverThrown", "unchecked"})
     @Test
     public void canThrowExceptionInMethodInvocation() throws Exception {
         // Mock ErrorsContext
@@ -429,7 +446,7 @@
         // Mock HttpServletRequest
         final HttpServletRequest request  = mockery.mock(HttpServletRequest.class);
         mockery.checking(new Expectations() {{
-            one(request).getParameterNames();
+            atLeast(1).of(request).getParameterNames();
             will(returnValue(enumeration));
         }});
 
@@ -466,6 +483,7 @@
         // Mock ServletMonitor
         final ServletMonitor servletMonitor = mockery.mock(ServletMonitor.class);
         mockery.checking(new Expectations() {{
+            allowing(servletMonitor).servletServiceRequested(with(any(Map.class)));
             allowing(servletMonitor).servletServiceFailed(actionMethodInvocationException);
         }});
 

Modified: trunk/waffle-core/src/test/java/org/codehaus/waffle/testmodel/StubMonitor.java (599 => 600)

--- trunk/waffle-core/src/test/java/org/codehaus/waffle/testmodel/StubMonitor.java	2008-04-01 13:22:43 UTC (rev 599)
+++ trunk/waffle-core/src/test/java/org/codehaus/waffle/testmodel/StubMonitor.java	2008-04-01 16:25:53 UTC (rev 600)
@@ -21,6 +21,7 @@
 
 import javax.servlet.http.HttpServletResponse;
 import java.lang.reflect.Method;
+import java.util.Map;
 import java.util.Set;
 
 public class StubMonitor implements ActionMonitor, BindMonitor, ContextMonitor, ControllerMonitor, RegistrarMonitor,
@@ -112,6 +113,9 @@
 
     public void servletServiceFailed(Exception cause) {
     }
+    
+    public void servletServiceRequested(Map parameters) {
+    }
 
     public void controllerValidatorNotFound(String controllerValidatorName, String controllerName) {
     }


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to