Commits

Anonymous committed 675c12d

XW-83 - Create a new "Validateable" interface for Actions
XW-84 - Remove workflow from ActionSupport
Made DefaultWorkflowInterceptor call "validate()" on Validateable Actions before checking for errors.

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

Comments (0)

Files changed (6)

src/java/com/opensymphony/xwork/ActionSupport.java

  */
 package com.opensymphony.xwork;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import java.io.Serializable;
+
+import java.util.Collection;
+import java.util.Locale;
+import java.util.Map;
+import java.util.ResourceBundle;
+
 
 /**
- * ActionSupport
- * @author Jason Carreira
- * Created Feb 10, 2003 9:47:39 AM
+ * Created by IntelliJ IDEA.
+ * User: Mike
+ * Date: May 27, 2003
+ * Time: 3:45:29 PM
+ * To change this template use Options | File Templates.
  */
-public class ActionSupport extends BaseActionSupport {
+public class ActionSupport implements Action, Serializable, Validateable, ValidationAware, LocaleAware {
+    //~ Static fields/initializers /////////////////////////////////////////////
+
+    protected transient static final Log LOG = LogFactory.getLog(ActionSupport.class);
+
+    //~ Instance fields ////////////////////////////////////////////////////////
+
+    private final LocaleAware localeAware = new LocaleAwareSupport(getClass());
+    private final ValidationAware validationAware = new ValidationAwareSupport();
+
     //~ Methods ////////////////////////////////////////////////////////////////
 
-    public String doDefault() throws Exception {
-        return INPUT;
+    public void setActionErrors(Collection errorMessages) {
+        validationAware.setActionErrors(errorMessages);
     }
 
-    public String doExecute() throws Exception {
-        return SUCCESS;
+    public Collection getActionErrors() {
+        return validationAware.getActionErrors();
+    }
+
+    public void setFieldErrors(Map errorMap) {
+        validationAware.setFieldErrors(errorMap);
+    }
+
+    /**
+     * @return a Map of field names -> List of errors or null if no errors have been added
+     */
+    public Map getFieldErrors() {
+        return validationAware.getFieldErrors();
+    }
+
+    public Locale getLocale() {
+        return localeAware.getLocale();
+    }
+
+    public String getText(String aTextName) {
+        return localeAware.getText(aTextName);
+    }
+
+    public String getText(String aTextName, String defaultValue) {
+        return localeAware.getText(aTextName, defaultValue);
+    }
+
+    public ResourceBundle getTexts(String aBundleName) {
+        return localeAware.getTexts(aBundleName);
+    }
+
+    public ResourceBundle getTexts() {
+        return localeAware.getTexts();
+    }
+
+    public void addActionError(String anErrorMessage) {
+        validationAware.addActionError(anErrorMessage);
+    }
+
+    public void addFieldError(String fieldName, String errorMessage) {
+        validationAware.addFieldError(fieldName, errorMessage);
     }
 
     public String execute() throws Exception {
-        doValidation();
+        return SUCCESS;
+    }
+
+    public boolean hasActionErrors() {
+        return validationAware.hasActionErrors();
+    }
 
-        if (hasErrors()) {
-            return INPUT;
-        }
+    public boolean hasErrors() {
+        return validationAware.hasErrors();
+    }
 
-        return doExecute();
+    public boolean hasFieldErrors() {
+        return validationAware.hasFieldErrors();
     }
 
     /**
-     * Subclasses may override this method to provide validation
-     * of input data. The execute() method calls doValidation()
-     * in the beginning of its code (which will delegate to this method),
-     * so as to check input data before doing the actual processing.
-     *
-     * <p>If any application errors arise these should be registered
-     * by calling addActionError() or addFieldError().</p>
+     * Subclasses should override this method to provide validations.
      */
-    protected void doValidation() {
+    public void validate() {
     }
 }

src/java/com/opensymphony/xwork/BaseActionSupport.java

-/*
- * Copyright (c) 2002-2003 by OpenSymphony
- * All rights reserved.
- */
-package com.opensymphony.xwork;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
-import java.io.Serializable;
-
-import java.util.Collection;
-import java.util.Locale;
-import java.util.Map;
-import java.util.ResourceBundle;
-
-
-/**
- * Created by IntelliJ IDEA.
- * User: Mike
- * Date: May 27, 2003
- * Time: 3:45:29 PM
- * To change this template use Options | File Templates.
- */
-public class BaseActionSupport implements Action, Serializable, ValidationAware, LocaleAware {
-    //~ Static fields/initializers /////////////////////////////////////////////
-
-    protected transient static final Log LOG = LogFactory.getLog(ActionSupport.class);
-
-    //~ Instance fields ////////////////////////////////////////////////////////
-
-    private final LocaleAware localeAware = new LocaleAwareSupport(getClass());
-    private final ValidationAware validationAware = new ValidationAwareSupport();
-
-    //~ Methods ////////////////////////////////////////////////////////////////
-
-    public void setActionErrors(Collection errorMessages) {
-        validationAware.setActionErrors(errorMessages);
-    }
-
-    public Collection getActionErrors() {
-        return validationAware.getActionErrors();
-    }
-
-    public void setFieldErrors(Map errorMap) {
-        validationAware.setFieldErrors(errorMap);
-    }
-
-    public Map getFieldErrors() {
-        return validationAware.getFieldErrors();
-    }
-
-    public Locale getLocale() {
-        return localeAware.getLocale();
-    }
-
-    public String getText(String aTextName) {
-        return localeAware.getText(aTextName);
-    }
-
-    public String getText(String aTextName, String defaultValue) {
-        return localeAware.getText(aTextName, defaultValue);
-    }
-
-    public ResourceBundle getTexts(String aBundleName) {
-        return localeAware.getTexts(aBundleName);
-    }
-
-    public ResourceBundle getTexts() {
-        return localeAware.getTexts();
-    }
-
-    public void addActionError(String anErrorMessage) {
-        validationAware.addActionError(anErrorMessage);
-    }
-
-    public void addFieldError(String fieldName, String errorMessage) {
-        validationAware.addFieldError(fieldName, errorMessage);
-    }
-
-    public String execute() throws Exception {
-        return SUCCESS;
-    }
-
-    public boolean hasActionErrors() {
-        return validationAware.hasActionErrors();
-    }
-
-    public boolean hasErrors() {
-        return validationAware.hasErrors();
-    }
-
-    public boolean hasFieldErrors() {
-        return validationAware.hasFieldErrors();
-    }
-}

src/java/com/opensymphony/xwork/Validateable.java

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.xwork;
+
+
+/**
+ * Validateable provides an Interface for Actions which can be validated.
+ * @author Jason Carreira
+ * @see ActionSupport
+ * @see com.opensymphony.xwork.interceptor.DefaultWorkflowInterceptor
+ * Created Aug 29, 2003 1:42:21 PM
+ */
+public interface Validateable {
+    //~ Methods ////////////////////////////////////////////////////////////////
+
+    void validate();
+}

src/java/com/opensymphony/xwork/interceptor/DefaultWorkflowInterceptor.java

 
 import com.opensymphony.xwork.Action;
 import com.opensymphony.xwork.ActionInvocation;
+import com.opensymphony.xwork.Validateable;
 import com.opensymphony.xwork.ValidationAware;
 
 
     public String intercept(ActionInvocation invocation) throws Exception {
         Action action = invocation.getAction();
 
+        if (action instanceof Validateable) {
+            Validateable validateable = (Validateable) action;
+            validateable.validate();
+        }
+
         if (action instanceof ValidationAware) {
             ValidationAware validationAwareAction = (ValidationAware) action;
 

src/test/com/opensymphony/xwork/VisitorValidatorTestAction.java

  * @author Jason Carreira
  * Created Aug 4, 2003 1:00:04 AM
  */
-public class VisitorValidatorTestAction extends BaseActionSupport {
+public class VisitorValidatorTestAction extends ActionSupport {
     //~ Instance fields ////////////////////////////////////////////////////////
 
     private List testBeanList = new ArrayList();

src/test/com/opensymphony/xwork/interceptor/DefaultWorkflowInterceptorTest.java

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.xwork.interceptor;
+
+import com.mockobjects.dynamic.Mock;
+
+import com.opensymphony.xwork.Action;
+import com.opensymphony.xwork.ActionInvocation;
+import com.opensymphony.xwork.Validateable;
+import com.opensymphony.xwork.ValidationAware;
+
+import junit.framework.TestCase;
+
+
+/**
+ * DefaultWorkflowInterceptorTest
+ * @author Jason Carreira
+ * Created Aug 29, 2003 1:51:00 PM
+ */
+public class DefaultWorkflowInterceptorTest extends TestCase {
+    //~ Instance fields ////////////////////////////////////////////////////////
+
+    DefaultWorkflowInterceptor interceptor;
+    private ActionInvocation invocation;
+    private Mock actionMock;
+    private Mock invocationMock;
+    private ValidateAction action;
+
+    //~ Methods ////////////////////////////////////////////////////////////////
+
+    public void testInvokesActionInvocationIfNoErrors() throws Exception {
+        actionMock.expectAndReturn("hasErrors", false);
+
+        final String result = "testing123";
+        invocationMock.expectAndReturn("invoke", result);
+        assertEquals(result, interceptor.intercept(invocation));
+    }
+
+    public void testReturnsInputWithoutExecutingIfHasErrors() throws Exception {
+        actionMock.expectAndReturn("hasErrors", true);
+        assertEquals(Action.INPUT, interceptor.intercept(invocation));
+    }
+
+    protected void setUp() throws Exception {
+        super.setUp();
+        actionMock = new Mock(ValidateAction.class);
+        actionMock.expect("validate");
+        action = (ValidateAction) actionMock.proxy();
+        invocationMock = new Mock(ActionInvocation.class);
+        invocationMock.expectAndReturn("getAction", action);
+        invocation = (ActionInvocation) invocationMock.proxy();
+        interceptor = new DefaultWorkflowInterceptor();
+    }
+
+    //~ Inner Interfaces ///////////////////////////////////////////////////////
+
+    private interface ValidateAction extends Action, Validateable, ValidationAware {
+    }
+}