Title: [waffle-scm] [324] trunk: Updated waffle servlet to follow Post/Redirect/Get design pattern when handling actions that return null (or have void return type) when the request is a POST
Revision
324
Author
mward
Date
2007-10-15 07:48:18 -0500 (Mon, 15 Oct 2007)

Log Message

Updated waffle servlet to follow Post/Redirect/Get design pattern when handling actions that return null (or have void return type) when the request is a POST

Modified Paths

Diff

Modified: trunk/core/src/main/java/org/codehaus/waffle/servlet/WaffleServlet.java (323 => 324)

--- trunk/core/src/main/java/org/codehaus/waffle/servlet/WaffleServlet.java	2007-10-11 12:44:28 UTC (rev 323)
+++ trunk/core/src/main/java/org/codehaus/waffle/servlet/WaffleServlet.java	2007-10-15 12:48:18 UTC (rev 324)
@@ -10,15 +10,15 @@
  *****************************************************************************/
 package org.codehaus.waffle.servlet;
 
+import org.codehaus.waffle.ComponentRegistry;
 import static org.codehaus.waffle.Constants.ERRORS_KEY;
 import static org.codehaus.waffle.Constants.VIEW_PREFIX_KEY;
 import static org.codehaus.waffle.Constants.VIEW_SUFFIX_KEY;
-import org.codehaus.waffle.ComponentRegistry;
 import org.codehaus.waffle.action.ActionMethodExecutor;
+import org.codehaus.waffle.action.ActionMethodInvocationException;
 import org.codehaus.waffle.action.ActionMethodResponse;
 import org.codehaus.waffle.action.ActionMethodResponseHandler;
 import org.codehaus.waffle.action.MethodDefinition;
-import org.codehaus.waffle.action.ActionMethodInvocationException;
 import org.codehaus.waffle.bind.DataBinder;
 import org.codehaus.waffle.bind.RequestAttributeBinder;
 import org.codehaus.waffle.controller.ControllerDefinition;
@@ -26,6 +26,7 @@
 import org.codehaus.waffle.validation.DefaultErrorsContext;
 import org.codehaus.waffle.validation.ErrorsContext;
 import org.codehaus.waffle.validation.Validator;
+import org.codehaus.waffle.view.RedirectView;
 import org.codehaus.waffle.view.View;
 
 import javax.servlet.ServletException;
@@ -57,7 +58,7 @@
     }
 
     /**
-     * Needed for builder
+     * Needed for builder ... and helpful for testing
      */
     public WaffleServlet(ControllerDefinitionFactory controllerDefinitionFactory,
                          DataBinder dataBinder,
@@ -131,18 +132,31 @@
         try {
             ActionMethodResponse actionMethodResponse = new ActionMethodResponse();
             MethodDefinition methodDefinition = controllerDefinition.getMethodDefinition();
+            View view = null;
 
             if (errorsContext.hasErrorMessages() || methodDefinition == null) {
-                buildViewToReferrer(controllerDefinition, actionMethodResponse);
+                view = buildViewToReferrer(controllerDefinition);
             } else {
                 actionMethodExecutor.execute(actionMethodResponse, controllerDefinition);
 
-                if (errorsContext.hasErrorMessages() || actionMethodResponse.getReturnValue() == null) {
-                    // Null and VOID need to build a view back to the referring page
-                    buildViewToReferrer(controllerDefinition, actionMethodResponse);
+                if (errorsContext.hasErrorMessages()) {
+                    view = buildViewToReferrer(controllerDefinition);
+                } else if (actionMethodResponse.getReturnValue() == null) {
+                    // Null or VOID indicate a Waffle convention (return to referring page)
+                    if (request.getMethod().equalsIgnoreCase("POST")) {
+                        // PRG (Post/Redirect/Get): see http://en.wikipedia.org/wiki/Post/Redirect/Get
+                        String url = ""
+                        view = new RedirectView(url, controllerDefinition.getController());
+                    } else { // was a GET
+                        view = buildViewToReferrer(controllerDefinition);
+                    }
                 }
             }
 
+            if (view != null) {
+                actionMethodResponse.setReturnValue(view);
+            }
+
             requestAttributeBinder.bind(request, controllerDefinition.getController());
             actionMethodResponseHandler.handle(request, response, actionMethodResponse);
         } catch (ActionMethodInvocationException e) {
@@ -154,10 +168,9 @@
     /**
      * Build a view back to the referring page (use the Controller's name as the View name).
      */
-    protected void buildViewToReferrer(ControllerDefinition controllerDefinition, ActionMethodResponse actionMethodResponse) {
+    protected View buildViewToReferrer(ControllerDefinition controllerDefinition) {
         String controllerValue = viewPrefix + controllerDefinition.getName() + viewSuffix;
-        View view = new View(controllerValue, controllerDefinition.getController());
-        actionMethodResponse.setReturnValue(view);
+        return new View(controllerValue, controllerDefinition.getController());
     }
 
 }

Modified: trunk/core/src/main/java/org/codehaus/waffle/servlet/WaffleXMLServlet.java (323 => 324)

--- trunk/core/src/main/java/org/codehaus/waffle/servlet/WaffleXMLServlet.java	2007-10-11 12:44:28 UTC (rev 323)
+++ trunk/core/src/main/java/org/codehaus/waffle/servlet/WaffleXMLServlet.java	2007-10-15 12:48:18 UTC (rev 324)
@@ -10,10 +10,12 @@
  *****************************************************************************/
 package org.codehaus.waffle.servlet;
 
-import org.codehaus.waffle.action.ActionMethodResponse;
 import org.codehaus.waffle.controller.ControllerDefinition;
+import org.codehaus.waffle.view.View;
 import org.codehaus.waffle.view.XMLView;
 
+import javax.servlet.http.HttpServletRequest;
+
 /**
  * Waffle's FrontController for XML serialization.
  *
@@ -21,8 +23,11 @@
  */
 public class WaffleXMLServlet extends WaffleServlet {
 
-    protected void buildViewToReferrer(ControllerDefinition controllerDefinition,
-                                       ActionMethodResponse actionMethodResponse) {
-        actionMethodResponse.setReturnValue(new XMLView());
+    protected View buildViewToReferrer(ControllerDefinition controllerDefinition) {
+        return new XMLView();
     }
+
+    protected View buildRedirectViewToReferrer(ControllerDefinition controllerDefinition, HttpServletRequest request) {
+        return new XMLView();
+    }
 }

Modified: trunk/core/src/test/java/org/codehaus/waffle/servlet/WaffleServletTest.java (323 => 324)

--- trunk/core/src/test/java/org/codehaus/waffle/servlet/WaffleServletTest.java	2007-10-11 12:44:28 UTC (rev 323)
+++ trunk/core/src/test/java/org/codehaus/waffle/servlet/WaffleServletTest.java	2007-10-15 12:48:18 UTC (rev 324)
@@ -11,27 +11,31 @@
 package org.codehaus.waffle.servlet;
 
 import ognl.DefaultTypeConverter;
+import org.codehaus.waffle.ComponentRegistry;
 import org.codehaus.waffle.Constants;
-import org.codehaus.waffle.ComponentRegistry;
 import org.codehaus.waffle.WaffleException;
 import org.codehaus.waffle.action.ActionMethodExecutor;
+import org.codehaus.waffle.action.ActionMethodInvocationException;
 import org.codehaus.waffle.action.ActionMethodResponse;
 import org.codehaus.waffle.action.ActionMethodResponseHandler;
 import org.codehaus.waffle.action.InterceptingActionMethodExecutor;
 import org.codehaus.waffle.action.MethodDefinition;
-import org.codehaus.waffle.action.ActionMethodInvocationException;
 import org.codehaus.waffle.bind.OgnlDataBinder;
 import org.codehaus.waffle.bind.RequestAttributeBinder;
 import org.codehaus.waffle.context.RequestLevelContainer;
 import org.codehaus.waffle.context.pico.PicoContextContainer;
 import org.codehaus.waffle.controller.ControllerDefinition;
 import org.codehaus.waffle.controller.ControllerDefinitionFactory;
-import org.codehaus.waffle.testmodel.StubComponentRegistry;
 import org.codehaus.waffle.validation.ErrorsContext;
 import org.codehaus.waffle.validation.Validator;
 import org.codehaus.waffle.view.View;
-import org.jmock.Mock;
-import org.jmock.MockObjectTestCase;
+import org.jmock.Expectations;
+import org.jmock.Mockery;
+import org.jmock.integration.junit4.JMock;
+import org.jmock.integration.junit4.JUnit4Mockery;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
 import org.picocontainer.defaults.DefaultPicoContainer;
 
 import javax.servlet.ServletConfig;
@@ -47,34 +51,32 @@
 import java.util.Enumeration;
 import java.util.List;
 
-public class WaffleServletTest extends MockObjectTestCase {
[EMAIL PROTECTED](JMock.class)
+public class WaffleServletTest {
+    private final Mockery mockery = new JUnit4Mockery();
 
+    @Test
     public void testInitSetsAttributeOnServletContext() throws ServletException {
         // Mock ServletConfig
-        Mock mockServletConfig = mock(ServletConfig.class);
-        mockServletConfig.expects(once())
-                .method("getInitParameter")
-                .with(eq(Constants.VIEW_PREFIX_KEY))
-                .will(returnValue(null));
-        mockServletConfig.expects(once())
-                .method("getInitParameter")
-                .with(eq(Constants.VIEW_SUFFIX_KEY))
-                .will(returnValue(".jsp"));
-        final ServletConfig servletConfig = (ServletConfig) mockServletConfig.proxy();
+        final ServletConfig servletConfig = mockery.mock(ServletConfig.class);
+        final ServletContext servletContext = mockery.mock(ServletContext.class);
+        final ComponentRegistry componentRegistry = mockery.mock(ComponentRegistry.class);
 
-        // Mock ServletContext
-        Mock mockServletContext = mock(ServletContext.class);
-        final ServletContext servletContext = (ServletContext) mockServletContext.proxy();
-        mockServletContext.expects(once())
-                .method("getInitParameterNames")
-                .will(returnValue(null));
-        mockServletContext.expects(atLeastOnce())
-                .method("getInitParameter")
-                .will(returnValue(null));
-        mockServletContext.expects(once())
-                .method("getAttribute")
-                .with(eq(ComponentRegistry.class.getName()))
-                .will(returnValue(new StubComponentRegistry(servletContext)));
+        mockery.checking(new Expectations() {{
+            one(servletConfig).getInitParameter(Constants.VIEW_PREFIX_KEY);
+            will(returnValue(null));
+            one(servletConfig).getInitParameter(Constants.VIEW_SUFFIX_KEY);
+            will(returnValue(".jsp"));
+            one(servletContext).getAttribute(ComponentRegistry.class.getName());
+            will(returnValue(componentRegistry));
+            // Component Registry...
+            one(componentRegistry).getControllerDefinitionFactory();
+            one(componentRegistry).getDataBinder();
+            one(componentRegistry).getActionMethodExecutor();
+            one(componentRegistry).getActionMethodResponseHandler();
+            one(componentRegistry).getValidator();
+            one(componentRegistry).getRequestAttributeBinder();
+        }});
 
         WaffleServlet servlet = new WaffleServlet() {
             @Override
@@ -91,137 +93,140 @@
         servlet.init();
     }
 
-    public void testInitRequiresInitParameter() throws ServletException {
-        Mock mockServletConext = mock(ServletContext.class);
-        final ServletContext servletContext = (ServletContext) mockServletConext.proxy();
-        mockServletConext.expects(once())
-                .method("getAttribute")
-                .with(eq("org.codehaus.waffle.ComponentRegistry"))
-                .will(returnValue(null));
+    @Test
+    public void testServiceForNonDispatchingController() throws Exception {
+        RequestLevelContainer.set(new PicoContextContainer(new DefaultPicoContainer()));
+        final NonDispatchingController nonDispatchingController = new NonDispatchingController();
+        List<?> list = Collections.EMPTY_LIST;
+        final Enumeration enumeration = Collections.enumeration(list);
 
-        // Mock ServletConfig
-        Mock mockServletConfig = mock(ServletConfig.class);
+        // Mock HttpServletRequest
+        final HttpServletRequest request = mockery.mock(HttpServletRequest.class);
+        mockery.checking(new Expectations() {{
+            one(request).getParameterNames();
+            will(returnValue(enumeration));
+            one(request).setAttribute(with(equal(Constants.ERRORS_KEY)), with(a(ErrorsContext.class)));
+            one(request).getMethod(); // todo need to test post
+            will(returnValue("get"));
+        }});
 
-        mockServletConfig.expects(once())
-                .method("getInitParameter")
-                .with(eq(Constants.VIEW_PREFIX_KEY))
-                .will(returnValue("/WEB-INF/jsp"));
-        mockServletConfig.expects(once())
-                .method("getInitParameter")
-                .with(eq(Constants.VIEW_SUFFIX_KEY))
-                .will(returnValue(null));
-        final ServletConfig servletConfig = (ServletConfig) mockServletConfig.proxy();
+        // Mock HttpServletResponse
+        final HttpServletResponse response = mockery.mock(HttpServletResponse.class);
 
-        WaffleServlet servlet = new WaffleServlet() {
-            @Override
-            public ServletConfig getServletConfig() {
-                return servletConfig;
-            }
+        Method method = NonDispatchingController.class.getMethod("increment");
+        final MethodDefinition methodDefinition = new MethodDefinition(method);
 
+        // Mock ActionMethodResponseHandler
+        final ActionMethodResponseHandler actionMethodResponseHandler = mockery.mock(ActionMethodResponseHandler.class);
+        mockery.checking(new Expectations() {{
+            one(actionMethodResponseHandler).handle(with(same(request)), with(same(response)), with(any(ActionMethodResponse.class)));
+        }});
+
+        // Mock Validator
+        final Validator validator = mockery.mock(Validator.class);
+        mockery.checking(new Expectations() {{
+            one(validator).validate(with(any(ControllerDefinition.class)), with(any(ErrorsContext.class)));
+        }});
+
+        // Mock RequestAttributeBinder
+        final RequestAttributeBinder requestAttributeBinder = mockery.mock(RequestAttributeBinder.class);
+        mockery.checking(new Expectations() {{
+            one(requestAttributeBinder).bind(with(same(request)), with(any(NonDispatchingController.class)));
+        }});
+
+        // stub out what we don't want called ... execute it
+        WaffleServlet waffleServlet = new WaffleServlet(null,
+                                                        new OgnlDataBinder(new DefaultTypeConverter(), null),
+                                                        new InterceptingActionMethodExecutor(),
+                                                        actionMethodResponseHandler,
+                                                        validator,
+                                                        requestAttributeBinder) {
             @Override
-            public ServletContext getServletContext() {
-                return servletContext;
+            protected ControllerDefinition getControllerDefinition(HttpServletRequest request, HttpServletResponse response) {
+                return new ControllerDefinition("no name", nonDispatchingController, methodDefinition);
             }
         };
 
-        try {
-            servlet.init();
-            fail("WaffleException was expected");
-        } catch (WaffleException expected) {
-            // expected
-        }
+        waffleServlet.service(request, response);
+        Assert.assertEquals(1, nonDispatchingController.getCount());
     }
 
-    public void testServiceForNonDispatchingController() throws Exception {
+    @Test // Testing Post/Redirect/Get - see http://en.wikipedia.org/wiki/Post/Redirect/Get
+    public void serviceShouldCreateRedirectViewWhenReturnValueIsNullAndRequestWasAPost() throws Exception {
         RequestLevelContainer.set(new PicoContextContainer(new DefaultPicoContainer()));
         final NonDispatchingController nonDispatchingController = new NonDispatchingController();
         List<?> list = Collections.EMPTY_LIST;
-        Enumeration enumeration = Collections.enumeration(list);
+        final Enumeration enumeration = Collections.enumeration(list);
 
         // Mock HttpServletRequest
-        Mock mockRequest = mock(HttpServletRequest.class);
-        mockRequest.expects(once())
-                .method("getParameterNames")
-                .will(returnValue(enumeration));
-        mockRequest.expects(once())
-                .method("setAttribute")
-                .with(eq(Constants.ERRORS_KEY), isA(ErrorsContext.class));
-        HttpServletRequest request = (HttpServletRequest) mockRequest.proxy();
+        final HttpServletRequest request = mockery.mock(HttpServletRequest.class);
+        mockery.checking(new Expectations() {{
+            one(request).getParameterNames();
+            will(returnValue(enumeration));
+            one(request).setAttribute(with(equal(Constants.ERRORS_KEY)), with(a(ErrorsContext.class)));
+            one(request).getMethod();
+            will(returnValue("post"));
+            one(request).getRequestURL();
+            will(returnValue(new StringBuffer("www.chicagobears.com")));
+        }});
 
         // Mock HttpServletResponse
-        Mock mockResponse = mock(HttpServletResponse.class);
-        HttpServletResponse response = (HttpServletResponse) mockResponse.proxy();
+        final HttpServletResponse response = mockery.mock(HttpServletResponse.class);
 
         Method method = NonDispatchingController.class.getMethod("increment");
         final MethodDefinition methodDefinition = new MethodDefinition(method);
 
+        // Mock ActionMethodResponseHandler
+        final ActionMethodResponseHandler actionMethodResponseHandler = mockery.mock(ActionMethodResponseHandler.class);
+        mockery.checking(new Expectations() {{
+            one(actionMethodResponseHandler).handle(with(same(request)), with(same(response)), with(any(ActionMethodResponse.class)));
+        }});
+
+        // Mock Validator
+        final Validator validator = mockery.mock(Validator.class);
+        mockery.checking(new Expectations() {{
+            one(validator).validate(with(any(ControllerDefinition.class)), with(any(ErrorsContext.class)));
+        }});
+
+        // Mock RequestAttributeBinder
+        final RequestAttributeBinder requestAttributeBinder = mockery.mock(RequestAttributeBinder.class);
+        mockery.checking(new Expectations() {{
+            one(requestAttributeBinder).bind(with(same(request)), with(any(NonDispatchingController.class)));
+        }});
+
         // stub out what we don't want called ... execute it
-        WaffleServlet waffleServlet = new WaffleServlet() {
+        WaffleServlet waffleServlet = new WaffleServlet(null,
+                                                        new OgnlDataBinder(new DefaultTypeConverter(), null),
+                                                        new InterceptingActionMethodExecutor(),
+                                                        actionMethodResponseHandler,
+                                                        validator,
+                                                        requestAttributeBinder) {
             @Override
             protected ControllerDefinition getControllerDefinition(HttpServletRequest request, HttpServletResponse response) {
                 return new ControllerDefinition("no name", nonDispatchingController, methodDefinition);
             }
         };
 
-        // Mock ActionMethodResponseHandler
-        Mock mockMethodResponseHandler = mock(ActionMethodResponseHandler.class);
-        mockMethodResponseHandler.expects(once())
-                .method("handle")
-                .with(eq(request), ANYTHING, isA(ActionMethodResponse.class));
-        ActionMethodResponseHandler actionMethodResponseHandler = (ActionMethodResponseHandler) mockMethodResponseHandler.proxy();
-
-        // Mock Validator
-        Mock mockValidator = mock(Validator.class);
-        mockValidator.expects(once())
-                .method("validate")
-                .with(isA(ControllerDefinition.class), isA(ErrorsContext.class));
-        Validator validator = (Validator) mockValidator.proxy();
-
-        // Mock RequestAttributeBinder
-        Mock mockRequestAttributeBinder = mock(RequestAttributeBinder.class);
-        mockRequestAttributeBinder.expects(once())
-                .method("bind")
-                .with(same(request), isA(NonDispatchingController.class));
-        RequestAttributeBinder requestAttributeBinder = (RequestAttributeBinder) mockRequestAttributeBinder.proxy();
-
-        // Set up what normally would happen via "init()"
-        Field dataBinderField = WaffleServlet.class.getDeclaredField("dataBinder");
-        dataBinderField.setAccessible(true);
-        dataBinderField.set(waffleServlet, new OgnlDataBinder(new DefaultTypeConverter(), null));
-        Field actionMethodExecutorField = WaffleServlet.class.getDeclaredField("actionMethodExecutor");
-        actionMethodExecutorField.setAccessible(true);
-        actionMethodExecutorField.set(waffleServlet, new InterceptingActionMethodExecutor());
-        Field methodResponseHandlerField = WaffleServlet.class.getDeclaredField("actionMethodResponseHandler");
-        methodResponseHandlerField.setAccessible(true);
-        methodResponseHandlerField.set(waffleServlet, actionMethodResponseHandler);
-        Field validatorFactoryField = WaffleServlet.class.getDeclaredField("validator");
-        validatorFactoryField.setAccessible(true);
-        validatorFactoryField.set(waffleServlet, validator);
-        Field requestAttributeBinderField = WaffleServlet.class.getDeclaredField("requestAttributeBinder");
-        requestAttributeBinderField.setAccessible(true);
-        requestAttributeBinderField.set(waffleServlet, requestAttributeBinder);
-
         waffleServlet.service(request, response);
-        assertEquals(1, nonDispatchingController.getCount());
+        Assert.assertEquals(1, nonDispatchingController.getCount());
     }
 
+    @Test(expected = ServletException.class)
     public void testControllerNotFound() throws Exception {
         // Mock HttpServletRequest
-        Mock mockRequest = mock(HttpServletRequest.class);
-        mockRequest.expects(once())
-                .method("setAttribute")
-                .with(eq(Constants.ERRORS_KEY), isA(ErrorsContext.class));
-        mockRequest.expects(once())
-                .method("getServletPath")
-                .will(returnValue("/foobar"));
-        HttpServletRequest request = (HttpServletRequest) mockRequest.proxy();
+        final HttpServletRequest request = mockery.mock(HttpServletRequest.class);
+        mockery.checking(new Expectations() {{
+            one(request).setAttribute(with(equal(Constants.ERRORS_KEY)), with(any(ErrorsContext.class)));
+            one(request).getServletPath();
+            will(returnValue("/foobar"));
+        }});
 
         // Mock ControllerDefinitionFactory
-        Mock mockFactory = mock(ControllerDefinitionFactory.class);
-        mockFactory.expects(once())
-                .method("getControllerDefinition")
-                .will(returnValue(new ControllerDefinition("junk", null, null)));
-        ControllerDefinitionFactory controllerDefinitionFactory = (ControllerDefinitionFactory) mockFactory.proxy();
+        final ControllerDefinitionFactory controllerDefinitionFactory = mockery.mock(ControllerDefinitionFactory.class);
+        mockery.checking(new Expectations() {{
+            one(controllerDefinitionFactory).getControllerDefinition(request, null);
+            will(returnValue(new ControllerDefinition("junk", null, null)));
+        }});
 
         WaffleServlet waffleServlet = new WaffleServlet();
 
@@ -230,53 +235,44 @@
         actionFactoryField.setAccessible(true);
         actionFactoryField.set(waffleServlet, controllerDefinitionFactory);
 
-        try {
-            waffleServlet.service(request, null);
-            fail("ServletException expected when an invalid controller is requested");
-        } catch (ServletException expected) {
-            assertTrue(expected.getMessage().startsWith("Unable to locate the Waffle Controller"));
-        }
+        waffleServlet.service(request, null);
     }
 
+    @Test
     public void testMethodDefinitionIsNull() throws Exception {
         final NonDispatchingController nonDispatchingController = new NonDispatchingController();
         List<?> list = Collections.EMPTY_LIST;
-        Enumeration enumeration = Collections.enumeration(list);
+        final Enumeration enumeration = Collections.enumeration(list);
 
         // Mock HttpServletRequest
-        Mock mockRequest = mock(HttpServletRequest.class);
-        mockRequest.expects(once()).method("getParameterNames").will(returnValue(enumeration));
-        mockRequest.expects(once())
-                .method("setAttribute")
-                .with(eq(Constants.ERRORS_KEY), isA(ErrorsContext.class));
-        HttpServletRequest request = (HttpServletRequest) mockRequest.proxy();
+        final HttpServletRequest request  = mockery.mock(HttpServletRequest.class);
+        mockery.checking(new Expectations() {{
+            one(request).getParameterNames();
+            will(returnValue(enumeration));
+            one(request).setAttribute(with(equal(Constants.ERRORS_KEY)), with(any(ErrorsContext.class)));
+        }});
 
         // Mock HttpServletResponse
-        Mock mockResponse = mock(HttpServletResponse.class);
-        HttpServletResponse response = (HttpServletResponse) mockResponse.proxy();
+        final HttpServletResponse response = mockery.mock(HttpServletResponse.class);
 
         // Mock ActionMethodResponseHandler
-        Mock mockActionMethodResponseHandler = mock(ActionMethodResponseHandler.class);
-        mockActionMethodResponseHandler.expects(once())
-                .method("handle")
-                .with(eq(request), ANYTHING, isA(ActionMethodResponse.class));
-        ActionMethodResponseHandler actionMethodResponseHandler =
-                (ActionMethodResponseHandler) mockActionMethodResponseHandler.proxy();
+        final ActionMethodResponseHandler actionMethodResponseHandler = mockery.mock(ActionMethodResponseHandler.class);
+        mockery.checking(new Expectations() {{
+            one(actionMethodResponseHandler).handle(with(same(request)), with(same(response)), with(any(ActionMethodResponse.class)));
+        }});
 
         // Mock Validator
-        Mock mockValidator = mock(Validator.class);
-        mockValidator.expects(once())
-                .method("validate")
-                .with(isA(ControllerDefinition.class), isA(ErrorsContext.class));
-        Validator validator = (Validator) mockValidator.proxy();
+        final Validator validator = mockery.mock(Validator.class);
+        mockery.checking(new Expectations() {{
+            one(validator).validate(with(any(ControllerDefinition.class)), with(any(ErrorsContext.class)));
+        }});
 
         // Mock
-        Mock mockRequestAttributeBinder = mock(RequestAttributeBinder.class);
-        mockRequestAttributeBinder.expects(once())
-                .method("bind")
-                .with(same(request), same(nonDispatchingController));
-        RequestAttributeBinder requestAttributeBinder = (RequestAttributeBinder) mockRequestAttributeBinder.proxy();
-        
+        final RequestAttributeBinder requestAttributeBinder = mockery.mock(RequestAttributeBinder.class);
+        mockery.checking(new Expectations() {{
+            one(requestAttributeBinder).bind(with(same(request)), with(any(NonDispatchingController.class)));
+        }});
+
         // stub out what we don't want called ... execute it
         WaffleServlet waffleServlet = new WaffleServlet(null,
                 new OgnlDataBinder(new DefaultTypeConverter(), null),
@@ -298,25 +294,26 @@
         waffleServlet.service(request, response);
     }
 
+    @SuppressWarnings({"ThrowableInstanceNeverThrown"})
+    @Test
     public void testMethodInvocationExceptionThrown() throws Exception {
         final NonDispatchingController nonDispatchingController = new NonDispatchingController();
         List<?> list = Collections.EMPTY_LIST;
-        Enumeration enumeration = Collections.enumeration(list);
+        final Enumeration enumeration = Collections.enumeration(list);
 
         // Mock HttpServletRequest
-        Mock mockRequest = mock(HttpServletRequest.class);
-        mockRequest.expects(once()).method("getParameterNames").will(returnValue(enumeration));
-        mockRequest.expects(once())
-                .method("setAttribute")
-                .with(eq(Constants.ERRORS_KEY), isA(ErrorsContext.class));
-        HttpServletRequest request = (HttpServletRequest) mockRequest.proxy();
+        final HttpServletRequest request  = mockery.mock(HttpServletRequest.class);
+        mockery.checking(new Expectations() {{
+            one(request).getParameterNames();
+            will(returnValue(enumeration));
+            one(request).setAttribute(with(equal(Constants.ERRORS_KEY)), with(any(ErrorsContext.class)));
+        }});
 
         // Mock HttpServletResponse
-        Mock mockResponse = mock(HttpServletResponse.class);
-        mockResponse.expects(once())
-                .method("sendError")
-                .with(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), isA(String.class));
-        HttpServletResponse response = (HttpServletResponse) mockResponse.proxy();
+        final HttpServletResponse response = mockery.mock(HttpServletResponse.class);
+        mockery.checking(new Expectations() {{
+            one(response).sendError(with(equal(HttpServletResponse.SC_INTERNAL_SERVER_ERROR)), with(any(String.class)));
+        }});
 
         // stub out what we don't want called ... execute it
         WaffleServlet waffleServlet = new WaffleServlet() {
@@ -332,19 +329,17 @@
         };
 
         // Mock ActionMethodExecutor
-        Mock mockMethodExecutor = mock(ActionMethodExecutor.class);
-        mockMethodExecutor.expects(once())
-                .method("execute")
-                .with(isA(ActionMethodResponse.class), isA(ControllerDefinition.class))
-                .will(throwException(new ActionMethodInvocationException("fake from test")));
-        ActionMethodExecutor actionMethodExecutor = (ActionMethodExecutor) mockMethodExecutor.proxy();
+        final ActionMethodExecutor actionMethodExecutor = mockery.mock(ActionMethodExecutor.class);
+        mockery.checking(new Expectations() {{
+            one(actionMethodExecutor).execute(with(any(ActionMethodResponse.class)), with(any(ControllerDefinition.class)));
+            will(throwException(new ActionMethodInvocationException("fake from test")));
+        }});
 
         // Mock Validator
-        Mock mockValidator = mock(Validator.class);
-        mockValidator.expects(once())
-                .method("validate")
-                .with(isA(ControllerDefinition.class), isA(ErrorsContext.class));
-        Validator validator = (Validator) mockValidator.proxy();
+        final Validator validator = mockery.mock(Validator.class);
+        mockery.checking(new Expectations() {{
+            one(validator).validate(with(any(ControllerDefinition.class)), with(any(ErrorsContext.class)));
+        }});
 
         // Set up what normally would happen via "init()"
         Field dataBinderField = WaffleServlet.class.getDeclaredField("dataBinder");
@@ -362,6 +357,7 @@
         waffleServlet.service(request, response);
     }
 
+    @Test
     public void testBuildViewToReferrer() throws Exception {
         WaffleServlet waffleServlet = new WaffleServlet();
 
@@ -374,13 +370,42 @@
         viewSuffixField.set(waffleServlet, "-suffix");
 
         ControllerDefinition controllerDefinition = new ControllerDefinition("foobar", null, null);
-        ActionMethodResponse actionMethodResponse = new ActionMethodResponse();
-        waffleServlet.buildViewToReferrer(controllerDefinition, actionMethodResponse);
+        View view = waffleServlet.buildViewToReferrer(controllerDefinition);
 
-        View view = (View) actionMethodResponse.getReturnValue();
-        assertEquals("prefix-foobar-suffix", view.getValue());
+        Assert.assertEquals("prefix-foobar-suffix", view.getValue());
     }
 
+    @Test(expected = WaffleException.class)
+    public void testInitRequiresInitParameter() throws ServletException {
+        final ServletContext servletContext = mockery.mock(ServletContext.class);
+        mockery.checking(new Expectations() {{
+            one(servletContext).getAttribute(ComponentRegistry.class.getName());
+            will(returnValue(null));
+        }});
+
+        // Mock ServletConfig
+        final ServletConfig servletConfig = mockery.mock(ServletConfig.class);
+        mockery.checking(new Expectations() {{
+            one(servletConfig).getInitParameter(Constants.VIEW_PREFIX_KEY);
+            will(returnValue("/WEB-INF/jsp"));
+            one(servletConfig).getInitParameter(Constants.VIEW_SUFFIX_KEY);
+        }});
+
+        WaffleServlet servlet = new WaffleServlet() {
+            @Override
+            public ServletConfig getServletConfig() {
+                return servletConfig;
+            }
+
+            @Override
+            public ServletContext getServletContext() {
+                return servletContext;
+            }
+        };
+
+        servlet.init();
+    }
+
     public class NonDispatchingController {
         private int count = 0;
 

Modified: trunk/core/src/test/java/org/codehaus/waffle/servlet/WaffleXMLServletTest.java (323 => 324)

--- trunk/core/src/test/java/org/codehaus/waffle/servlet/WaffleXMLServletTest.java	2007-10-11 12:44:28 UTC (rev 323)
+++ trunk/core/src/test/java/org/codehaus/waffle/servlet/WaffleXMLServletTest.java	2007-10-15 12:48:18 UTC (rev 324)
@@ -2,6 +2,7 @@
 
 import org.codehaus.waffle.action.ActionMethodResponse;
 import org.codehaus.waffle.view.XMLView;
+import org.codehaus.waffle.view.View;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -13,8 +14,8 @@
         response.setReturnValue("a value that will be overriden....");
 
         WaffleXMLServlet servlet = new WaffleXMLServlet();
-        servlet.buildViewToReferrer(null, response);
-        Assert.assertTrue(response.getReturnValue() instanceof XMLView);
+        View view = servlet.buildViewToReferrer(null);
+        Assert.assertTrue(view instanceof XMLView);
     }
 
 }

Modified: trunk/waffle-parent.ipr (323 => 324)

--- trunk/waffle-parent.ipr	2007-10-11 12:44:28 UTC (rev 323)
+++ trunk/waffle-parent.ipr	2007-10-15 12:48:18 UTC (rev 324)
@@ -200,7 +200,9 @@
           </module>
           <module name="waffle-simple-example">
             <files>
+              <file url="" />
               <file url="" />
+              <file url="" />
             </files>
           </module>
           <module name="waffle-freemarker-example">
@@ -210,6 +212,7 @@
           </module>
           <module name="waffle-paranamer-example">
             <files>
+              <file url="" />
               <file url="" />
             </files>
           </module>


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to