Commits

Anonymous committed 015e737

Annotation Validator throws OGNL Exception for setting 'methodName' property
o removed methodName property from ValidatorConfig internal params
o added unit test for annotatedMethodOnly property

Issue Number: XW-494

git-svn-id: http://svn.opensymphony.com/svn/xwork/branches/2.0@1438e221344d-f017-0410-9bd5-d282ab1896d7

  • Participants
  • Parent commits 759be1e
  • Branches 2.0, xwork_2_0_7

Comments (0)

Files changed (4)

src/java/com/opensymphony/xwork2/validator/AnnotationActionValidatorManager.java

         ArrayList<Validator> validators = new ArrayList<Validator>(cfgs.size());
         for (ValidatorConfig cfg : cfgs) {
             if (method == null || method.equals(cfg.getParams().get("methodName"))) {
+                cfg.getParams().remove("methodName");
                 Validator validator = ValidatorFactory.getValidator(cfg, ObjectFactory.getObjectFactory());
                 validator.setValidatorType(cfg.getType());
                 validators.add(validator);

src/test/com/opensymphony/xwork2/test/annotations/ValidateAnnotatedMethodOnlyAction.java

+package com.opensymphony.xwork2.test.annotations;
+
+import com.opensymphony.xwork2.validator.annotations.ExpressionValidator;
+import com.opensymphony.xwork2.validator.annotations.Validation;
+import com.opensymphony.xwork2.ActionSupport;
+
+/**
+ * <code>ValidateAnnotatedMethodOnlyAction</code>
+ *
+ * @author <a href="mailto:hermanns@aixcept.de">Rainer Hermanns</a>
+ * @version $Id: $
+ */
+@Validation
+public class ValidateAnnotatedMethodOnlyAction extends ActionSupport {
+
+    String param1;
+    String param2;
+
+
+    public String getParam1() {
+        return param1;
+    }
+
+    public void setParam1(String param1) {
+        this.param1 = param1;
+    }
+
+    public String getParam2() {
+        return param2;
+    }
+
+    public void setParam2(String param2) {
+        this.param2 = param2;
+    }
+
+    @ExpressionValidator(expression = "(param1 != null) || (param2 != null)",
+            message = "Need param1 or param2.")
+    public String annotatedMethod() {
+        try {
+            // do search
+        } catch (Exception e) {
+            return INPUT;
+        }
+        return SUCCESS;
+    }
+
+    public String notAnnotatedMethod() {
+        try {
+            // do different search
+        } catch (Exception e) {
+            return INPUT;
+        }
+        return SUCCESS;
+    }
+}

src/test/com/opensymphony/xwork2/validator/ValidatorAnnotationTest.java

+package com.opensymphony.xwork2.validator;
+
+import com.opensymphony.xwork2.*;
+import com.opensymphony.xwork2.inject.ContainerBuilder;
+import com.opensymphony.xwork2.interceptor.ParametersInterceptor;
+import com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor;
+import com.opensymphony.xwork2.interceptor.StaticParametersInterceptor;
+import com.opensymphony.xwork2.mock.MockResult;
+import com.opensymphony.xwork2.util.ValueStack;
+import com.opensymphony.xwork2.util.ValueStackFactory;
+import com.opensymphony.xwork2.util.location.LocatableProperties;
+import com.opensymphony.xwork2.validator.validators.ExpressionValidator;
+import com.opensymphony.xwork2.config.providers.MockConfigurationProvider;
+import com.opensymphony.xwork2.config.providers.XmlConfigurationProvider;
+import com.opensymphony.xwork2.config.ConfigurationProvider;
+import com.opensymphony.xwork2.config.Configuration;
+import com.opensymphony.xwork2.config.ConfigurationException;
+import com.opensymphony.xwork2.config.entities.PackageConfig;
+import com.opensymphony.xwork2.config.entities.ResultConfig;
+import com.opensymphony.xwork2.config.entities.ActionConfig;
+import com.opensymphony.xwork2.config.entities.InterceptorMapping;
+import com.mockobjects.dynamic.Mock;
+import com.mockobjects.dynamic.C;
+
+import java.util.*;
+
+/**
+ * Unit test for annotated Validators.
+ *
+ * @author Rainer Hermanns
+ */
+public class ValidatorAnnotationTest extends XWorkTestCase {
+
+    public void testNotAnnotatedMethodSuccess() throws Exception {
+        HashMap params = new HashMap();
+        params.put("date", "12/23/2002");
+        params.put("foo", "5");
+        params.put("bar", "7");
+
+        HashMap extraContext = new HashMap();
+        extraContext.put(ActionContext.PARAMETERS, params);
+
+        ActionProxy proxy = actionProxyFactory.createActionProxy("", "notAnnotatedMethod", extraContext);
+        proxy.execute();
+        assertFalse(((ValidationAware) proxy.getAction()).hasActionErrors());
+
+        Collection errors = ((ValidationAware) proxy.getAction()).getActionErrors();
+        assertEquals(0, errors.size());
+    }
+
+    public void testNotAnnotatedMethodSuccess2() throws Exception {
+        HashMap params = new HashMap();
+
+        HashMap extraContext = new HashMap();
+        extraContext.put(ActionContext.PARAMETERS, params);
+
+        ActionProxy proxy = actionProxyFactory.createActionProxy("", "notAnnotatedMethod", extraContext);
+        proxy.execute();
+        assertFalse(((ValidationAware) proxy.getAction()).hasActionErrors());
+
+        Collection errors = ((ValidationAware) proxy.getAction()).getActionErrors();
+        assertEquals(0, errors.size());
+    }
+
+    public void testAnnotatedMethodFailure() throws Exception {
+        HashMap params = new HashMap();
+
+        HashMap extraContext = new HashMap();
+        extraContext.put(ActionContext.PARAMETERS, params);
+
+        ActionProxy proxy = actionProxyFactory.createActionProxy("", "annotatedMethod", extraContext);
+        proxy.execute();
+        assertTrue(((ValidationAware) proxy.getAction()).hasActionErrors());
+        Collection errors = ((ValidationAware) proxy.getAction()).getActionErrors();
+        assertEquals(1, errors.size());
+
+        assertEquals("Need param1 or param2.", errors.iterator().next());
+
+    }
+
+    public void testAnnotatedMethodSuccess() throws Exception {
+        HashMap params = new HashMap();
+
+        //make it not fail
+        params.put("param1", "key1");
+        params.put("param2", "key2");
+
+        HashMap extraContext = new HashMap();
+        extraContext.put(ActionContext.PARAMETERS, params);
+
+        ActionProxy proxy = actionProxyFactory.createActionProxy("", "annotatedMethod", extraContext);
+        proxy.execute();
+        assertFalse(((ValidationAware) proxy.getAction()).hasActionErrors());
+    }
+
+    public void testAnnotatedMethodSuccess2() throws Exception {
+        HashMap params = new HashMap();
+
+        //make it not fail
+        params.put("param2", "key2");
+
+        HashMap extraContext = new HashMap();
+        extraContext.put(ActionContext.PARAMETERS, params);
+
+        ActionProxy proxy = actionProxyFactory.createActionProxy("", "annotatedMethod", extraContext);
+        proxy.execute();
+        assertFalse(((ValidationAware) proxy.getAction()).hasActionErrors());
+    }
+
+    public void testAnnotatedMethodSuccess3() throws Exception {
+        HashMap params = new HashMap();
+
+        //make it not fail
+        params.put("param1", "key1");
+
+        HashMap extraContext = new HashMap();
+        extraContext.put(ActionContext.PARAMETERS, params);
+
+        ActionProxy proxy = actionProxyFactory.createActionProxy("", "annotatedMethod", extraContext);
+        proxy.execute();
+        assertFalse(((ValidationAware) proxy.getAction()).hasActionErrors());
+    }
+
+    protected void setUp() throws Exception {
+        ValueStack stack = ValueStackFactory.getFactory().createValueStack();
+        ActionContext.setContext(new ActionContext(stack.getContext()));
+
+        loadConfigurationProviders(new XmlConfigurationProvider("xwork-test-validation.xml"));
+    }
+
+}

src/test/xwork-test-validation.xml

+<!DOCTYPE xwork PUBLIC
+    "-//OpenSymphony Group//XWork 2.0//EN"
+    "http://www.opensymphony.com/xwork/xwork-2.0.dtd"
+ >
+
+<xwork>
+	<include file="xwork-test-beans.xml" />
+    <package name="xwork-test-default">
+        <result-types>
+            <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult" default="true"/>
+            <result-type name="void" class="com.opensymphony.xwork2.VoidResult"/>
+            <result-type name="mock" class="com.opensymphony.xwork2.mock.MockResult"/>
+        </result-types>
+
+        <interceptors>
+            <interceptor name="timer" class="com.opensymphony.xwork2.interceptor.TimerInterceptor"/>
+            <interceptor name="logger" class="com.opensymphony.xwork2.interceptor.LoggingInterceptor"/>
+            <interceptor name="chain" class="com.opensymphony.xwork2.interceptor.ChainingInterceptor"/>
+            <interceptor name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>
+            <interceptor name="static-params" class="com.opensymphony.xwork2.interceptor.StaticParametersInterceptor"/>
+            <interceptor name="model-driven" class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"/>
+            <interceptor name="validation" class="com.opensymphony.xwork2.validator.ValidationInterceptor">
+                <param name="validateAnnotatedMethodOnly">true</param>
+            </interceptor>
+			<interceptor name="alias" class="com.opensymphony.xwork2.interceptor.AliasInterceptor"/>
+            <interceptor name="test" class="com.opensymphony.xwork2.mock.MockInterceptor">
+                <param name="foo">expectedFoo</param>
+            </interceptor>
+
+            <interceptor-stack name="defaultStack">
+                <interceptor-ref name="static-params"/>
+                <interceptor-ref name="model-driven"/>
+                <interceptor-ref name="params"/>
+            </interceptor-stack>
+
+            <interceptor-stack name="debugStack">
+                <interceptor-ref name="timer"/>
+                <interceptor-ref name="logger"/>
+            </interceptor-stack>
+
+            <interceptor-stack name="validationStack">
+                <interceptor-ref name="static-params"/>
+                <interceptor-ref name="model-driven"/>
+                <interceptor-ref name="params"/>
+                <interceptor-ref name="validation"/>
+            </interceptor-stack>
+        </interceptors>
+
+    </package>
+    <package name="default" extends="xwork-test-default">
+
+        <action name="annotatedMethod" class="com.opensymphony.xwork2.test.annotations.ValidateAnnotatedMethodOnlyAction" method="annotatedMethod">
+            <interceptor-ref name="validationStack"/>
+            <result name="success" type="mock"/>
+        </action>
+        
+        <action name="notAnnotatedMethod" class="com.opensymphony.xwork2.test.annotations.ValidateAnnotatedMethodOnlyAction" method="notAnnotatedMethod">
+            <interceptor-ref name="validationStack"/>
+            <result name="success" type="mock"/>
+        </action>
+
+    </package>
+
+</xwork>