Commits

Anonymous committed 4709a29

Adding a unit test for getting the field description using the field name as the message key in a text from a validator message.

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

Comments (0)

Files changed (4)

src/test/com/opensymphony/xwork/SimpleAction-validation.xml

             <message key="foo.range">Could not find foo.range!</message>
         </field-validator>
     </field>
+    <field name="baz">
+        <field-validator type="int">
+            <param name="min">0</param>
+            <message key="baz.range">Could not find baz.range!</message>
+        </field-validator>
+    </field>
     <validator type="expression">
         <param name="expression">foo > bar</param>
         <message>Foo must be greater than Bar. Foo = ${foo}, Bar = ${bar}.</message>

src/test/com/opensymphony/xwork/SimpleAction.properties

-foo.range=Foo Range Message
+foo.range=Foo Range Message
+baz.range=${getText(fieldName)} must be greater than ${min}
+baz=Baz Field

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

     public void testBuildsValidatorsForAlias() {
         List validatorList = ActionValidatorManager.getValidators(SimpleAction.class, alias);
 
-        // 5 in the class level + 2 in the alias
-        assertEquals(7, validatorList.size());
+        // 6 in the class level + 2 in the alias
+        assertEquals(8, validatorList.size());
     }
 
     public void testGetValidatorsForInterface() {
     public void testGetValidatorsFromInterface() {
         List validatorList = ActionValidatorManager.getValidators(SimpleAction3.class, alias);
 
-        // 5 in the class hierarchy + 1 in the interface
-        assertEquals(9, validatorList.size());
+        // 8 in the class hierarchy + 1 in the interface + 1 in interface alias
+        assertEquals(10, validatorList.size());
 
         final FieldValidator barValidator1 = (FieldValidator) validatorList.get(0);
         assertEquals("bar", barValidator1.getFieldName());
         assertEquals("foo", fooValidator.getFieldName());
         assertTrue(fooValidator instanceof IntRangeFieldValidator);
 
-        final Validator expressionValidator = (Validator) validatorList.get(4);
+        final FieldValidator bazValidator = (FieldValidator) validatorList.get(4);
+        assertEquals("baz", bazValidator.getFieldName());
+        assertTrue(bazValidator instanceof IntRangeFieldValidator);
+
+        final Validator expressionValidator = (Validator) validatorList.get(5);
         assertTrue(expressionValidator instanceof ExpressionValidator);
 
-        final FieldValidator bazValidator1 = (FieldValidator) validatorList.get(5);
+        final FieldValidator bazValidator1 = (FieldValidator) validatorList.get(6);
         assertEquals("baz", bazValidator1.getFieldName());
         assertTrue(bazValidator1 instanceof RequiredFieldValidator);
 
-        final FieldValidator bazValidator2 = (FieldValidator) validatorList.get(6);
+        final FieldValidator bazValidator2 = (FieldValidator) validatorList.get(7);
         assertEquals("baz", bazValidator2.getFieldName());
         assertTrue(bazValidator2 instanceof IntRangeFieldValidator);
 
-        final FieldValidator dataValidator1 = (FieldValidator) validatorList.get(7);
+        final FieldValidator dataValidator1 = (FieldValidator) validatorList.get(8);
         assertEquals("data", dataValidator1.getFieldName());
         assertTrue(dataValidator1 instanceof RequiredFieldValidator);
 
-        final FieldValidator dataValidator2 = (FieldValidator) validatorList.get(8);
+        final FieldValidator dataValidator2 = (FieldValidator) validatorList.get(9);
         assertEquals("data", dataValidator2.getFieldName());
         assertTrue(dataValidator2 instanceof RequiredStringValidator);
     }

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

         }
     }
 
+    public void testLookingUpFieldNameAsTextKey() {
+        HashMap params = new HashMap();
+
+        // should cause a message
+        params.put("baz", "-1");
+
+        //valid values
+        params.put("bar", "7");
+
+        HashMap extraContext = new HashMap();
+        extraContext.put(ActionContext.PARAMETERS, params);
+
+        try {
+            ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy("", MockConfigurationProvider.VALIDATION_ACTION_NAME, extraContext);
+            proxy.execute();
+            assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors());
+
+            Map errors = ((ValidationAware) proxy.getAction()).getFieldErrors();
+            List bazErrors = (List) errors.get("baz");
+            assertEquals(1, bazErrors.size());
+
+            String errorMessage = (String) bazErrors.get(0);
+            assertNotNull(errorMessage);
+            assertEquals("Baz Field must be greater than 0", errorMessage);
+        } catch (Exception e) {
+            e.printStackTrace();
+            fail();
+        }
+    }
+
     public void testMessageKey() {
         HashMap params = new HashMap();
         params.put("foo", "200");