Commits

Anonymous committed 9e58da3

XW-138 & XW-144:
message key is now used as default if the message is not found in a message bundle and there is no default message.
For conversion errors using the ConversionErrorFieldValidator if the default message is provided it will be used as the default (after looking up from the message bundle if there's a message key) if there's no default message, the XWorkConverter.getConversionErrorMessage() method is used to get a default message.

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

Comments (0)

Files changed (4)

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

     //~ Methods ////////////////////////////////////////////////////////////////
 
     /**
-     * The validation implementation must guarantee that setValidatorContext will
-     * be called with a non-null ValidatorContext before validate is called.
-     * @param object
-     * @throws ValidationException
-     */
+ * The validation implementation must guarantee that setValidatorContext will
+ * be called with a non-null ValidatorContext before validate is called.
+ * @param object
+ * @throws ValidationException
+ */
     public void validate(Object object) throws ValidationException {
-        String fullFieldName = getValidatorContext().getFullFieldName(getFieldName());
+        String fieldName = getFieldName();
+        String fullFieldName = getValidatorContext().getFullFieldName(fieldName);
         ActionContext context = ActionContext.getContext();
         Map conversionErrors = context.getConversionErrors();
 
         if (conversionErrors.containsKey(fullFieldName)) {
-            addFieldError(getFieldName(), XWorkConverter.getConversionErrorMessage(fullFieldName, context.getValueStack()));
+            if ((defaultMessage == null) || (defaultMessage.trim().equals(""))) {
+                defaultMessage = XWorkConverter.getConversionErrorMessage(fullFieldName, context.getValueStack());
+            }
+
+            addFieldError(fieldName, object);
         }
     }
 }

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

     //~ Instance fields ////////////////////////////////////////////////////////
 
     protected final Log log = LogFactory.getLog(this.getClass());
-    private String defaultMessage = "";
-    private String messageKey = null;
+    protected String defaultMessage = "";
+    protected String messageKey = null;
     private ValidatorContext validatorContext;
 
     //~ Methods ////////////////////////////////////////////////////////////////
         stack.push(this);
 
         if (messageKey != null) {
+            if ((defaultMessage == null) || (defaultMessage.trim().equals(""))) {
+                defaultMessage = messageKey;
+            }
+
             message = validatorContext.getText(messageKey, defaultMessage);
         } else {
             message = defaultMessage;

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

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
 package com.opensymphony.xwork.validator;
 
-import junit.framework.TestCase;
 import com.opensymphony.xwork.ActionContext;
 import com.opensymphony.xwork.ValidationAware;
 import com.opensymphony.xwork.ValidationAwareSupport;
-import com.opensymphony.xwork.validator.validators.ConversionErrorFieldValidator;
 import com.opensymphony.xwork.util.OgnlValueStack;
+import com.opensymphony.xwork.validator.validators.ConversionErrorFieldValidator;
+
+import junit.framework.TestCase;
 
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
+
 /**
  * ConversionErrorFieldValidatorTest
  * @author Jason Carreira
  * Date: Nov 28, 2003 3:45:37 PM
  */
 public class ConversionErrorFieldValidatorTest extends TestCase {
+    //~ Static fields/initializers /////////////////////////////////////////////
+
+    private static final String defaultFooMessage = "Invalid field value for field \"foo\".";
+
+    //~ Instance fields ////////////////////////////////////////////////////////
+
+    private ActionContext oldContext;
+    private ConversionErrorFieldValidator validator;
+    private ValidationAware validationAware;
+
+    //~ Methods ////////////////////////////////////////////////////////////////
+
+    public void setUp() {
+        oldContext = ActionContext.getContext();
+
+        OgnlValueStack stack = new OgnlValueStack();
+        ActionContext context = new ActionContext(stack.getContext());
+        ActionContext.setContext(context);
+
+        Map conversionErrors = new HashMap();
+        conversionErrors.put("foo", "bar");
+        context.setConversionErrors(conversionErrors);
+        validator = new ConversionErrorFieldValidator();
+        validationAware = new ValidationAwareSupport();
+
+        DelegatingValidatorContext validatorContext = new DelegatingValidatorContext(validationAware);
+        stack.push(validatorContext);
+        validator.setValidatorContext(validatorContext);
+        validator.setFieldName("foo");
+        assertEquals(0, validationAware.getFieldErrors().size());
+    }
+
+    public void testConversionErrorMessageUsesProvidedMessage() throws ValidationException {
+        String message = "default message";
+        validator.setDefaultMessage(message);
+        validator.validate(validationAware);
+
+        Map fieldErrors = validationAware.getFieldErrors();
+        assertTrue(fieldErrors.containsKey("foo"));
+        assertEquals(message, ((List) fieldErrors.get("foo")).get(0));
+    }
+
     public void testConversionErrorsAreAddedToFieldErrors() throws ValidationException {
-        ActionContext oldContext = ActionContext.getContext();
-        try {
-            OgnlValueStack stack = new OgnlValueStack();
-            ActionContext context = new ActionContext(stack.getContext());
-            ActionContext.setContext(context);
-            Map conversionErrors = new HashMap();
-            conversionErrors.put("foo","bar");
-            context.setConversionErrors(conversionErrors);
-            ConversionErrorFieldValidator validator = new ConversionErrorFieldValidator();
-            ValidationAware validationAware = new ValidationAwareSupport();
-            DelegatingValidatorContext validatorContext = new DelegatingValidatorContext(validationAware);
-            validator.setValidatorContext(validatorContext);
-            validator.setFieldName("foo");
-            assertEquals(0,validationAware.getFieldErrors().size());
-            validator.validate(validationAware);
-            Map fieldErrors = validationAware.getFieldErrors();
-            assertTrue(fieldErrors.containsKey("foo"));
-        } finally {
-            ActionContext.setContext(oldContext);
-        }
+        validator.validate(validationAware);
+
+        Map fieldErrors = validationAware.getFieldErrors();
+        assertTrue(fieldErrors.containsKey("foo"));
+        assertEquals(defaultFooMessage, ((List) fieldErrors.get("foo")).get(0));
+    }
+
+    protected void tearDown() throws Exception {
+        ActionContext.setContext(oldContext);
     }
 }

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

  */
 package com.opensymphony.xwork.validator;
 
-import com.opensymphony.xwork.ActionContext;
-import com.opensymphony.xwork.ActionProxy;
-import com.opensymphony.xwork.ActionProxyFactory;
-import com.opensymphony.xwork.ValidationAware;
+import com.opensymphony.xwork.*;
 import com.opensymphony.xwork.config.ConfigurationManager;
 import com.opensymphony.xwork.config.providers.MockConfigurationProvider;
 import com.opensymphony.xwork.util.OgnlValueStack;
+import com.opensymphony.xwork.validator.validators.ValidatorSupport;
 
 import junit.framework.TestCase;
 
-import java.util.HashMap;
-import java.util.List;
-import java.util.Locale;
-import java.util.Map;
+import java.util.*;
 
 
 /**
         }
     }
 
+    public void testMessageKeyIsReturnedIfNoOtherDefault() throws ValidationException {
+        Validator validator = new ValidatorSupport() {
+            public void validate(Object object) throws ValidationException {
+                addActionError(object);
+            }
+        };
+
+        String messageKey = "does.not.exist";
+        validator.setMessageKey(messageKey);
+
+        ValidatorContext validatorContext = new DelegatingValidatorContext(new SimpleAction());
+        validator.setValidatorContext(validatorContext);
+        validator.validate(this);
+        assertTrue(validatorContext.hasActionErrors());
+
+        Collection errors = validatorContext.getActionErrors();
+        assertEquals(1, errors.size());
+        assertEquals(messageKey, errors.toArray()[0]);
+    }
+
     public void testParamterizedMessage() {
         HashMap params = new HashMap();
         params.put("bar", "42");