1. opensymphony
  2. xwork

Commits

jcarreira  committed 082dcdc

XW-130: Made ValidationAwareSupport return a new Map or List for field and Action errors rather than unmodifiable collections so that chained Actions can add errors.
Also reversed the order of chaining in ChainingInterceptor so that Objects further down the stack will be chained first, and newer beans later.

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@129e221344d-f017-0410-9bd5-d282ab1896d7

  • Participants
  • Parent commits cb818d4
  • Branches master

Comments (0)

Files changed (3)

File src/java/com/opensymphony/xwork/ValidationAwareSupport.java

View file
  • Ignore whitespace
     }
 
     public synchronized Collection getActionErrors() {
-        return Collections.unmodifiableCollection(internalGetActionErrors());
+        return new ArrayList(internalGetActionErrors());
     }
 
     public synchronized void setFieldErrors(Map errorMap) {
     * @return an unmodifiable Map with errors mapped from fieldname (String) to Collection of String error messages
     */
     public synchronized Map getFieldErrors() {
-        return Collections.unmodifiableMap(internalGetFieldErrors());
+        return new HashMap(internalGetFieldErrors());
     }
 
     public synchronized void addActionError(String anErrorMessage) {

File src/java/com/opensymphony/xwork/interceptor/ChainingInterceptor.java

View file
  • Ignore whitespace
 import com.opensymphony.xwork.util.OgnlValueStack;
 
 import java.util.Iterator;
+import java.util.List;
+import java.util.Collections;
+import java.util.ArrayList;
 
 
 /**
     protected void before(ActionInvocation invocation) throws Exception {
         OgnlValueStack stack = invocation.getStack();
         CompoundRoot root = stack.getRoot();
-        Iterator iterator = root.iterator();
-        iterator.next();
+        if (root.size() > 1) {
+            List list = new ArrayList(root);
+            list.remove(0);
+            Collections.reverse(list);
+            Iterator iterator = list.iterator();
 
-        while (iterator.hasNext()) {
-            Object o = iterator.next();
-            OgnlUtil.copy(o, invocation.getAction(), ActionContext.getContext().getContextMap());
+            while (iterator.hasNext()) {
+                Object o = iterator.next();
+                OgnlUtil.copy(o, invocation.getAction(), ActionContext.getContext().getContextMap());
+            }
         }
     }
 }

File src/test/com/opensymphony/xwork/interceptor/ChainingInterceptorTest.java

View file
  • Ignore whitespace
+package com.opensymphony.xwork.interceptor;
+
+import junit.framework.TestCase;
+import com.opensymphony.xwork.util.OgnlValueStack;
+import com.opensymphony.xwork.ActionInvocation;
+import com.opensymphony.xwork.TestBean;
+import com.opensymphony.xwork.Action;
+import com.opensymphony.xwork.SimpleAction;
+import com.mockobjects.dynamic.Mock;
+
+import java.util.Date;
+
+
+/**
+ * ChainingInterceptorTest
+ * @author Jason Carreira
+ * Date: Nov 22, 2003 3:11:41 PM
+ */
+public class ChainingInterceptorTest extends TestCase {
+    OgnlValueStack stack;
+    ActionInvocation invocation;
+    Mock mockInvocation;
+    ChainingInterceptor interceptor;
+
+    protected void setUp() throws Exception {
+        super.setUp();
+        stack = new OgnlValueStack();
+        mockInvocation = new Mock(ActionInvocation.class);
+        mockInvocation.expectAndReturn("getStack",stack);
+        mockInvocation.expectAndReturn("invoke",Action.SUCCESS);
+        invocation = (ActionInvocation) mockInvocation.proxy();
+        interceptor = new ChainingInterceptor();
+    }
+
+    public void testPropertiesChained() throws Exception {
+        TestBean bean = new TestBean();
+        TestBeanAction action = new TestBeanAction();
+        mockInvocation.matchAndReturn("getAction",action);
+        bean.setBirth(new Date());
+        bean.setName("foo");
+        bean.setCount(1);
+        stack.push(bean);
+        stack.push(action);
+        interceptor.intercept(invocation);
+        assertEquals(bean.getBirth(),action.getBirth());
+        assertEquals(bean.getName(),action.getName());
+        assertEquals(bean.getCount(),action.getCount());
+    }
+
+    public void testActionErrorsCanBeAddedAfterChain() throws Exception {
+        SimpleAction action1 = new SimpleAction();
+        SimpleAction action2 = new SimpleAction();
+        action1.addActionError("foo");
+        mockInvocation.matchAndReturn("getAction",action2);
+        stack.push(action1);
+        stack.push(action2);
+        interceptor.intercept(invocation);
+        assertEquals(action1.getActionErrors(),action2.getActionErrors());
+        action2.addActionError("bar");
+        assertEquals(1,action1.getActionErrors().size());
+        assertEquals(2,action2.getActionErrors().size());
+        assertTrue(action2.getActionErrors().contains("bar"));
+    }
+
+    private class TestBeanAction extends TestBean implements Action {
+        public String execute() throws Exception {
+            return SUCCESS;
+        }
+    }
+}