struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mr...@apache.org
Subject svn commit: r589240 - in /struts/struts2/trunk/core/src: main/java/org/apache/struts2/interceptor/MessageStoreInterceptor.java test/java/org/apache/struts2/interceptor/MessageStoreInterceptorTest.java
Date Sat, 27 Oct 2007 23:36:17 GMT
Author: mrdon
Date: Sat Oct 27 16:36:15 2007
New Revision: 589240

URL: http://svn.apache.org/viewvc?rev=589240&view=rev
Log:
Adding automatic mode for message store interceptor
WW-2276

Modified:
    struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/MessageStoreInterceptor.java
    struts/struts2/trunk/core/src/test/java/org/apache/struts2/interceptor/MessageStoreInterceptorTest.java

Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/MessageStoreInterceptor.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/MessageStoreInterceptor.java?rev=589240&r1=589239&r2=589240&view=diff
==============================================================================
--- struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/MessageStoreInterceptor.java
(original)
+++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/MessageStoreInterceptor.java
Sat Oct 27 16:36:15 2007
@@ -25,6 +25,8 @@
 import java.util.LinkedHashMap;
 import java.util.Map;
 
+import org.apache.struts2.dispatcher.ServletRedirectResult;
+
 import com.opensymphony.xwork2.ActionContext;
 import com.opensymphony.xwork2.ActionInvocation;
 import com.opensymphony.xwork2.ValidationAware;
@@ -42,12 +44,19 @@
  * <p/>
  *
  * In the 'STORE' mode, the interceptor will store the {@link ValidationAware} action's message
/ errors
- * and field errors into Http session.
+ * and field errors into HTTP session.
  *
  * <p/>
  *
  * In the 'RETRIEVE' mode, the interceptor will retrieve the stored action's message / errors
 and field
  * errors and put them back into the {@link ValidationAware} action.
+ * 
+ * <p/>
+ *
+ * In the 'AUTOMATIC' mode, the interceptor will always retrieve the stored action's message
/ errors 
+ * and field errors and put them back into the {@link ValidationAware} action, and after
Action execution, 
+ * if the {@link Result} is an instance of {@link ServletRedirectResult}, the action's message
/ errors 
+ * and field errors into automatically be stored in the HTTP session..
  *
  * <p/>
  *
@@ -84,7 +93,7 @@
  *      <li>requestParameterSwitch - The request parameter that will indicate what
mode this
  *                                   interceptor is in. </li>
  *      <li>operationMode - The operation mode this interceptor should be in
- *                          (either 'STORE', 'RETRIEVE' or 'NONE'). 'NONE' being the default.</li>
+ *                          (either 'STORE', 'RETRIEVE', 'AUTOMATIC', or 'NONE'). 'NONE'
being the default.</li>
  * </ul>
  *
  * <!-- END SNIPPET: parameters -->
@@ -141,7 +150,7 @@
 
     private static final Logger LOG = LoggerFactory.getLogger(MessageStoreInterceptor.class);
 
-
+    public static final String AUTOMATIC_MODE = "AUTOMATIC";
     public static final String STORE_MODE = "STORE";
     public static final String RETRIEVE_MODE = "RETRIEVE";
     public static final String NONE = "NONE";
@@ -209,7 +218,8 @@
         String reqOperationMode = getRequestOperationMode(invocation);
 
         if (RETRIEVE_MODE.equalsIgnoreCase(reqOperationMode) ||
-                RETRIEVE_MODE.equalsIgnoreCase(operationMode)) {
+                RETRIEVE_MODE.equalsIgnoreCase(operationMode) ||
+                AUTOMATIC_MODE.equalsIgnoreCase(operationMode)) {
 
             Object action = invocation.getAction();
             if (action instanceof ValidationAware) {
@@ -255,8 +265,10 @@
     protected void after(ActionInvocation invocation, String result) throws Exception {
 
         String reqOperationMode = getRequestOperationMode(invocation);
+        boolean isRedirect = invocation.getResult() instanceof ServletRedirectResult;
         if (STORE_MODE.equalsIgnoreCase(reqOperationMode) ||
-                STORE_MODE.equalsIgnoreCase(operationMode)) {
+                STORE_MODE.equalsIgnoreCase(operationMode) ||
+                (AUTOMATIC_MODE.equalsIgnoreCase(operationMode) && isRedirect)) {
 
             Object action = invocation.getAction();
             if (action instanceof ValidationAware) {

Modified: struts/struts2/trunk/core/src/test/java/org/apache/struts2/interceptor/MessageStoreInterceptorTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/interceptor/MessageStoreInterceptorTest.java?rev=589240&r1=589239&r2=589240&view=diff
==============================================================================
--- struts/struts2/trunk/core/src/test/java/org/apache/struts2/interceptor/MessageStoreInterceptorTest.java
(original)
+++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/interceptor/MessageStoreInterceptorTest.java
Sat Oct 27 16:36:15 2007
@@ -27,6 +27,7 @@
 import java.util.Map;
 
 import org.apache.struts2.StrutsTestCase;
+import org.apache.struts2.dispatcher.ServletActionRedirectResult;
 import org.easymock.EasyMock;
 
 import com.opensymphony.xwork2.Action;
@@ -74,7 +75,9 @@
 
         mockActionInvocation.getAction();
         EasyMock.expectLastCall().andReturn(action);
-
+        
+        mockActionInvocation.getResult();
+        EasyMock.expectLastCall().andReturn(new ServletActionRedirectResult());
 
         EasyMock.replay(mockActionInvocation);
 
@@ -154,6 +157,10 @@
 
         mockActionInvocation.getAction();
         EasyMock.expectLastCall().andReturn(action);
+        EasyMock.expectLastCall().anyTimes();
+        
+        mockActionInvocation.getResult();
+        EasyMock.expectLastCall().andReturn(new ServletActionRedirectResult());
 
         EasyMock.replay(mockActionInvocation);
 
@@ -168,6 +175,122 @@
         assertTrue(action.getActionErrors().contains("some action error 2"));
         assertTrue(action.getActionMessages().contains("some action messages 1"));
         assertTrue(action.getActionMessages().contains("some action messages 2"));
+        assertEquals(((List)action.getFieldErrors().get("field1")).size(), 1);
+        assertEquals(((List)action.getFieldErrors().get("field2")).size(), 1);
+        assertEquals(((List)action.getFieldErrors().get("field1")).get(0), "some field error
1");
+        assertEquals(((List)action.getFieldErrors().get("field2")).get(0), "some field error
2");
+
+        EasyMock.verify(mockActionInvocation);
+    }
+    
+    public void testAutomatic() throws Exception {
+        MessageStoreInterceptor interceptor = new MessageStoreInterceptor();
+        interceptor.setAllowRequestParameterSwitch(true);
+        interceptor.setOperationMode(MessageStoreInterceptor.AUTOMATIC_MODE);
+
+
+        Map paramMap = new LinkedHashMap();
+        Map sessionMap = new LinkedHashMap();
+
+        ActionSupport action = new ActionSupport();
+        action.addActionError("some action error 1");
+        action.addActionError("some action error 2");
+        action.addActionMessage("some action message 1");
+        action.addActionMessage("some action message 2");
+        action.addFieldError("field1", "some field error 1");
+        action.addFieldError("field2", "some field error 2");
+
+        ActionContext actionContext = new ActionContext(new HashMap());
+        actionContext.put(ActionContext.PARAMETERS, paramMap);
+        actionContext.put(ActionContext.SESSION, sessionMap);
+
+        // Mock (ActionInvocation)
+        ActionInvocation mockActionInvocation = EasyMock.createControl().createMock(ActionInvocation.class);
+        mockActionInvocation.getInvocationContext();
+        EasyMock.expectLastCall().andReturn(actionContext);
+        EasyMock.expectLastCall().anyTimes();
+
+        mockActionInvocation.invoke();
+        EasyMock.expectLastCall().andReturn(Action.SUCCESS);
+
+        mockActionInvocation.getAction();
+        EasyMock.expectLastCall().andReturn(action);
+        EasyMock.expectLastCall().anyTimes();
+
+        mockActionInvocation.getResult();
+        EasyMock.expectLastCall().andReturn(new ServletActionRedirectResult());
+        
+        EasyMock.replay(mockActionInvocation);
+
+        interceptor.init();
+        interceptor.intercept(mockActionInvocation);
+        interceptor.destroy();
+
+        assertEquals(3, sessionMap.size());
+        assertTrue(sessionMap.containsKey(MessageStoreInterceptor.actionErrorsSessionKey));
+        assertTrue(sessionMap.containsKey(MessageStoreInterceptor.actionMessagesSessionKey));
+        assertTrue(sessionMap.containsKey(MessageStoreInterceptor.fieldErrorsSessionKey));
+
+        List actionErrors = (List) sessionMap.get(MessageStoreInterceptor.actionErrorsSessionKey);
+        List actionMessages = (List) sessionMap.get(MessageStoreInterceptor.actionMessagesSessionKey);
+        Map fieldErrors = (Map) sessionMap.get(MessageStoreInterceptor.fieldErrorsSessionKey);
+
+        assertEquals(2, actionErrors.size());
+        assertEquals(2, actionMessages.size());
+        assertEquals(2, fieldErrors.size());
+
+        assertTrue(actionErrors.contains("some action error 1"));
+        assertTrue(actionErrors.contains("some action error 2"));
+        assertTrue(actionMessages.contains("some action message 1"));
+        assertTrue(actionMessages.contains("some action message 2"));
+        assertTrue(fieldErrors.containsKey("field1"));
+        assertTrue(fieldErrors.containsKey("field2"));
+        assertEquals(((List)fieldErrors.get("field1")).size(), 1);
+        assertEquals(((List)fieldErrors.get("field2")).size(), 1);
+        assertEquals(((List)fieldErrors.get("field1")).get(0), "some field error 1");
+        assertEquals(((List)fieldErrors.get("field2")).get(0), "some field error 2");
+
+        EasyMock.verify(mockActionInvocation);
+        
+        action = new ActionSupport();
+
+        mockActionInvocation = EasyMock.createControl().createMock(ActionInvocation.class);
+        mockActionInvocation.invoke();
+        EasyMock.expectLastCall().andReturn(Action.SUCCESS);
+
+        sessionMap.put(MessageStoreInterceptor.actionErrorsSessionKey, actionErrors);
+        sessionMap.put(MessageStoreInterceptor.actionMessagesSessionKey, actionMessages);
+        sessionMap.put(MessageStoreInterceptor.fieldErrorsSessionKey, fieldErrors);
+
+
+        actionContext = new ActionContext(new HashMap());
+        actionContext.put(ActionContext.PARAMETERS, paramMap);
+        actionContext.put(ActionContext.SESSION, sessionMap);
+
+        mockActionInvocation.getInvocationContext();
+        EasyMock.expectLastCall().andReturn(actionContext);
+        EasyMock.expectLastCall().anyTimes();
+
+        mockActionInvocation.getAction();
+        EasyMock.expectLastCall().andReturn(action);
+        EasyMock.expectLastCall().anyTimes();
+        
+        mockActionInvocation.getResult();
+        EasyMock.expectLastCall().andReturn(new ServletActionRedirectResult());
+
+        EasyMock.replay(mockActionInvocation);
+
+        interceptor.init();
+        interceptor.intercept(mockActionInvocation);
+        interceptor.destroy();
+
+        assertEquals(action.getActionErrors().size(), 2);
+        assertEquals(action.getActionMessages().size(), 2);
+        assertEquals(action.getFieldErrors().size(), 2);
+        assertTrue(action.getActionErrors().contains("some action error 1"));
+        assertTrue(action.getActionErrors().contains("some action error 2"));
+        assertTrue(action.getActionMessages().contains("some action message 1"));
+        assertTrue(action.getActionMessages().contains("some action message 2"));
         assertEquals(((List)action.getFieldErrors().get("field1")).size(), 1);
         assertEquals(((List)action.getFieldErrors().get("field2")).size(), 1);
         assertEquals(((List)action.getFieldErrors().get("field1")).get(0), "some field error
1");



Mime
View raw message