Anonymous avatar Anonymous committed 4e05653

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@994 e221344d-f017-0410-9bd5-d282ab1896d7

Comments (0)

Files changed (8)

src/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);
         }
     }
 

src/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 {

src/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;

src/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

src/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());
 

src/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>

src/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");

src/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());
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.