Commits

Anonymous committed 3fdfe7c

Changed the validation code to throw more exceptions and not log them as errors, added the location object
to ValidatorConfig, added tests to ensure correct behaviors

Issue number: XW-383 and XW-379

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

Comments (0)

Files changed (8)

java/com/opensymphony/xwork/validator/ActionValidatorManagerFactory.java

 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import com.opensymphony.util.ClassLoaderUtil;
+import com.opensymphony.xwork.XworkException;
 
 /**
  * <code>ActionValidatorManagerFactory</code>
         } catch (ClassNotFoundException e) {
             // this is fine, just fall back to the default object type determiner
         } catch (Exception e) {
-            LOG.error("Exception when trying to create new AnnotationActionValidatorManager", e);
+            throw new XworkException(e);
         }
     }
 

java/com/opensymphony/xwork/validator/DefaultActionValidatorManager.java

                 if (is != null) {
                     retList = new ArrayList(ValidatorFileParser.parseActionValidatorConfigs(is, fileName));
                 }
-            } catch (Exception e) {
-                LOG.error("Caught exception while loading file " + fileName, e);
             } finally {
                 if (is != null) {
                     try {

java/com/opensymphony/xwork/validator/ValidatorConfig.java

 package com.opensymphony.xwork.validator;
 
 import java.util.Map;
+import com.opensymphony.xwork.util.location.Located;
 
 /**
  * Holds the necessary information for configuring an instance of a Validator.
  * @author James House
  * @author Rainer Hermanns
  */
-public class ValidatorConfig {
+public class ValidatorConfig extends Located {
 
     private String type;
     private Map params;

java/com/opensymphony/xwork/validator/ValidatorFactory.java

 
 import com.opensymphony.util.ClassLoaderUtil;
 import com.opensymphony.xwork.ObjectFactory;
+import com.opensymphony.xwork.XworkException;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
             validator = ObjectFactory.getObjectFactory().buildValidator(className, cfg.getParams(), null); // ActionContext.getContext().getContextMap());
         } catch (Exception e) {
             final String msg = "There was a problem creating a Validator of type " + className + " : caused by " + e.getMessage();
-            LOG.error(msg, e);
-            throw new IllegalArgumentException(msg);
+            throw new XworkException(msg, e, cfg);
         }
 
         // set other configured properties

java/com/opensymphony/xwork/validator/ValidatorFileParser.java

             ValidatorFactory.lookupRegisteredValidatorType(validatorType);
 
             ValidatorConfig vCfg = new ValidatorConfig(validatorType, params);
+            vCfg.setLocation(DomHelper.getLocationObject(validatorElement));
 
             vCfg.setShortCircuit(Boolean.valueOf(validatorElement.getAttribute("short-circuit")).booleanValue());
 

test/com/opensymphony/xwork/TestBean-badtest-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="requiredstring" foo="bar">
+            <message>You must enter a name.</message>
+        </field-validator>
+    </field>
+</validators>

test/com/opensymphony/xwork/validator/DefaultActionValidatorManagerTest.java

 import com.opensymphony.xwork.TestBean;
 import com.opensymphony.xwork.ValidationOrderAction;
 import com.opensymphony.xwork.XWorkTestCase;
+import com.opensymphony.xwork.XworkException;
 import com.opensymphony.xwork.test.DataAware2;
 import com.opensymphony.xwork.test.SimpleAction2;
 import com.opensymphony.xwork.test.SimpleAction3;
         assertEquals(9, validatorList.size());
     }
 
+    public void testBuildsValidatorsForAliasError() {
+        boolean pass = false;
+        try {
+            List validatorList = actionValidatorManager.getValidators(TestBean.class, "badtest");
+        } catch (XworkException ex) {
+            pass = true;
+        }
+        assertTrue("Didn't throw exception on load failure", pass);
+    }
+
+
     public void testDefaultMessageInterpolation() {
         // get validators
         List validatorList = actionValidatorManager.getValidators(TestBean.class, "beanMessageBundle");

test/com/opensymphony/xwork/validator/ValidatorFileParserTest.java

         assertEquals("required", cfg.getType());
         assertEquals("foo", cfg.getParams().get("fieldName"));
         assertEquals("You must enter a value for foo.", cfg.getDefaultMessage());
+        assertEquals(4, cfg.getLocation().getLineNumber());
         
         cfg = (ValidatorConfig) configs.get(3);
         assertEquals("required", cfg.getType());