Commits

Anonymous committed 0035971

XW-54: verifying that expression validations can check string lengths...

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

Comments (0)

Files changed (2)

src/test/com/opensymphony/xwork/TestBean-expressionValidation-validation.xml

+<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">
+<validators>
+    <field name="name">
+        <field-validator type="fieldexpression">
+            <param name="expression">name.length() > 5</param>
+            <message>Name must be greater than 5 characters, it is currently '${name}'</message>
+        </field-validator>
+    </field>
+</validators>

src/test/com/opensymphony/xwork/validator/ExpressionValidatorTest.java

 import com.opensymphony.xwork.ActionProxy;
 import com.opensymphony.xwork.ActionProxyFactory;
 import com.opensymphony.xwork.ValidationAware;
+import com.opensymphony.xwork.TestBean;
+import com.opensymphony.xwork.ValidationAwareSupport;
 import com.opensymphony.xwork.config.ConfigurationManager;
 import com.opensymphony.xwork.config.providers.MockConfigurationProvider;
 
 
 import java.util.Collection;
 import java.util.HashMap;
+import java.util.Map;
+import java.util.List;
 
 
 /**
         }
     }
 
+    public void testExpressionValidationOfStringLength() throws ValidationException {
+        TestBean bean = new TestBean();
+        bean.setName("abc");
+        ActionContext.getContext().getValueStack().push(bean);
+        DelegatingValidatorContext context = new DelegatingValidatorContext(new ValidationAwareSupport());
+        ActionValidatorManager.validate(bean,"expressionValidation",context);
+        assertTrue(context.hasFieldErrors());
+        final Map fieldErrors = context.getFieldErrors();
+        assertTrue(fieldErrors.containsKey("name"));
+        List nameErrors = (List) fieldErrors.get("name");
+        assertEquals(1,nameErrors.size());
+        assertEquals("Name must be greater than 5 characters, it is currently 'abc'",nameErrors.get(0));
+
+        bean.setName("abcdefg");
+        context = new DelegatingValidatorContext(new ValidationAwareSupport());
+        ActionValidatorManager.validate(bean,"expressionValidation",context);
+        assertFalse(context.hasFieldErrors());
+    }
+
     protected void setUp() throws Exception {
         ConfigurationManager.clearConfigurationProviders();
         ConfigurationManager.addConfigurationProvider(new MockConfigurationProvider());