Commits

Anonymous committed 2499bcb

Make sure object being validated is on the stack.

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

Comments (0)

Files changed (4)

src/java/com/opensymphony/xwork/validator/validators/ValidatorSupport.java

 
 
 /**
- * ValidatorSupport
+ * Abstract implementation of the Validator interface suitable for subclassing.
+ *
  * @author Jason Carreira
- * Created Feb 15, 2003 3:58:21 PM
  */
 public abstract class ValidatorSupport implements Validator, ShortCircuitableValidator {
     //~ Instance fields ////////////////////////////////////////////////////////
 
     public String getMessage(Object object) {
         String message;
-
         OgnlValueStack stack = ActionContext.getContext().getValueStack();
+        boolean pop = false;
+
+        if (!stack.getRoot().contains(object)) {
+            stack.push(object);
+            pop = true;
+        }
+
         stack.push(this);
 
         if (messageKey != null) {
 
         stack.pop();
 
+        if (pop) {
+            stack.pop();
+        }
+
         return message;
     }
 

src/test/com/opensymphony/xwork/TestBean-beanMessageBundle-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="count">
-        <field-validator type="int">
+        <field-validator type="int" short-circuit="true">
             <param name="min">1</param>
             <param name="max">100</param>
             <message key="invalid.count">Invalid Count!</message>
         </field-validator>
+        <field-validator type="int">
+            <param name="min">20</param>
+            <param name="max">80</param>
+            <message key="invalid.count.bad">Smaller Invalid Count: ${count}</message>
+        </field-validator>
     </field>
 </validators>

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

 package com.opensymphony.xwork.validator;
 
 import com.opensymphony.xwork.SimpleAction;
+import com.opensymphony.xwork.TestBean;
 import com.opensymphony.xwork.test.DataAware2;
 import com.opensymphony.xwork.test.SimpleAction2;
 import com.opensymphony.xwork.test.SimpleAction3;
         assertEquals(8, validatorList.size());
     }
 
+    public void testDefaultMessageInterpolation() {
+        // get validators
+        List validatorList = ActionValidatorManager.getValidators(TestBean.class, "beanMessageBundle");
+        assertEquals(3, validatorList.size());
+
+        try {
+            TestBean bean = new TestBean();
+            bean.setName("foo");
+            bean.setCount(99);
+
+
+            ValidatorContext context = new GenericValidatorContext(bean);
+            ActionValidatorManager.validate(bean, "beanMessageBundle", context);
+            assertTrue(context.hasErrors());
+            assertTrue(context.hasFieldErrors());
+
+            List l = (List) context.getFieldErrors().get("count");
+            assertNotNull(l);
+            assertEquals(1, l.size());
+            assertEquals("Smaller Invalid Count: 99", l.get(0));
+        } catch (ValidationException ex) {
+            ex.printStackTrace();
+            fail("Validation error: " + ex.getMessage());
+        }
+    }
+
+    public void testMessageInterpolation() {
+        // get validators
+        List validatorList = ActionValidatorManager.getValidators(TestBean.class, "beanMessageBundle");
+        assertEquals(3, validatorList.size());
+
+        try {
+            TestBean bean = new TestBean();
+            bean.setName("foo");
+            bean.setCount(150);
+
+
+            ValidatorContext context = new GenericValidatorContext(bean);
+            ActionValidatorManager.validate(bean, "beanMessageBundle", context);
+            assertTrue(context.hasErrors());
+            assertTrue(context.hasFieldErrors());
+
+            List l = (List) context.getFieldErrors().get("count");
+            assertNotNull(l);
+            assertEquals(1, l.size());
+            assertEquals("Count must be between 1 and 100, current value is 150.", l.get(0));
+        } catch (ValidationException ex) {
+            ex.printStackTrace();
+            fail("Validation error: " + ex.getMessage());
+        }
+    }
+
     public void testGetValidatorsForInterface() {
         List validatorList = ActionValidatorManager.getValidators(DataAware2.class, alias);
 

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

         assertTrue(fieldErrors.containsKey("bean.count"));
 
         List beanCountMessages = (List) fieldErrors.get("bean.count");
-        assertEquals(1, beanCountMessages.size());
+        assertEquals(2, beanCountMessages.size());
 
         String beanCountMessage = (String) beanCountMessages.get(0);
         assertEquals("bean: Count must be between 1 and 100, current value is -1.", beanCountMessage);