1. opensymphony
  2. xwork

Commits

jcarreira  committed c1a70e8

WW-211: Refactored ActionValidatormanager to use only class and context to load validations
XW-57: Validation file name pattern is now ClassName-alias-validation.xml for aliases and ClassName-validation.xml

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

  • Participants
  • Parent commits 78fa704
  • Branches master

Comments (0)

Files changed (8)

File java/com/opensymphony/xwork/DefaultActionInvocation.java

View file
                 try {
                     return (String) Proxy.getInvocationHandler(action).invoke(action, method, new Object[0]);
                 } catch (Throwable throwable) {
-                    throw new Exception("Error invoking on proxy: " + throwable.getMessage(), throwable);
+                    throwable.printStackTrace();
+                    throw new Exception("Error invoking on proxy: " + throwable.getMessage());
                 }
             } else {
                 return (String) method.invoke(action, new Object[0]);

File java/com/opensymphony/xwork/validator/ActionValidatorManager.java

View file
 
 import com.opensymphony.util.FileManager;
 
-import com.opensymphony.xwork.ActionInvocation;
-import com.opensymphony.xwork.ActionProxy;
-
 import java.io.InputStream;
 
 import java.util.ArrayList;
 
     //~ Methods ////////////////////////////////////////////////////////////////
 
-    public static synchronized List getValidators(ActionInvocation invocation) {
-        final String validatorKey = buildValidatorKey(invocation);
+    public static synchronized List getValidators(Class clazz, String context) {
+        final String validatorKey = buildValidatorKey(clazz, context);
 
         if (validatorCache.containsKey(validatorKey)) {
-            conditionalReload(invocation);
+            conditionalReload(clazz, context);
         } else {
-            List validators = buildValidators(invocation, false);
+            List validators = buildValidators(clazz, context, false);
             validatorCache.put(validatorKey, validators);
         }
 
         return (List) validatorCache.get(validatorKey);
     }
 
-    protected static String buildValidatorKey(ActionInvocation invocation) {
-        final ActionProxy proxy = invocation.getProxy();
-        StringBuffer sb = new StringBuffer(proxy.getNamespace());
+    protected static String buildValidatorKey(Class clazz, String context) {
+        StringBuffer sb = new StringBuffer(clazz.getName());
         sb.append("/");
-        sb.append(proxy.getActionName());
+        sb.append(context);
 
         return sb.toString();
     }
 
-    private static List buildAliasValidators(Class aClass, ActionInvocation invocation, boolean checkFile) {
-        final String actionClassName = aClass.getName();
-        final String actionName = actionClassName.replace('.', '/');
-        final String actionPath = actionName.substring(0, actionName.lastIndexOf('/') + 1);
-        final String aliasName = actionPath + invocation.getProxy().getActionName();
-
-        if (!aliasName.equals(actionName)) {
-            String fileName = aliasName + VALIDATION_CONFIG_SUFFIX;
+    private static List buildAliasValidators(Class aClass, String context, boolean checkFile) {
+        final String fullClassName = aClass.getName();
+        final String className = fullClassName.substring(fullClassName.lastIndexOf('.') + 1);
+        final String fullPath = fullClassName.replace('.', '/');
+        final String path = fullPath.substring(0, fullPath.lastIndexOf('/') + 1);
+        final String fileName = path + className + "-" + context + VALIDATION_CONFIG_SUFFIX;
 
-            return loadFile(fileName, aClass, checkFile);
-        }
-
-        return Collections.EMPTY_LIST;
+        return loadFile(fileName, aClass, checkFile);
     }
 
     private static List buildClassValidators(Class aClass, boolean checkFile) {
     * It will traverse up the class hierarchy looking for validators for every super class
     * of the current action, as well as adding validators for any alias of this invocation. Nifty!
     */
-    private static List buildValidators(ActionInvocation invocation, boolean checkFile) {
+    private static List buildValidators(Class clazz, String context, boolean checkFile) {
         List validators = new ArrayList();
-        final Class actionClass = invocation.getAction().getClass();
 
         // validators for the action class validators.addAll(buildClassValidators(actionClass, checkFile)); validators.addAll(buildAliasValidators(actionClass, invocation, checkFile));
         // looking for validators for every super class
-        Class anActionClass = actionClass;
+        Class anActionClass = clazz;
         anActionClass = anActionClass.getSuperclass();
 
         while (!anActionClass.equals(Object.class)) {
             anActionClass = anActionClass.getSuperclass();
         }
 
-        validators.addAll(buildClassValidators(actionClass, checkFile));
-        validators.addAll(buildAliasValidators(actionClass, invocation, checkFile));
+        validators.addAll(buildClassValidators(clazz, checkFile));
+        validators.addAll(buildAliasValidators(clazz, context, checkFile));
 
         return validators;
     }
 
-    private static void conditionalReload(ActionInvocation invocation) {
+    private static void conditionalReload(Class clazz, String context) {
         if (FileManager.isReloadingConfigs()) {
-            final String actionName = buildValidatorKey(invocation);
-            validatorCache.put(actionName, buildValidators(invocation, true));
+            final String actionName = buildValidatorKey(clazz, context);
+            validatorCache.put(actionName, buildValidators(clazz, context, true));
         }
     }
 
         if ((checkFile && FileManager.fileNeedsReloading(fileName)) || !validatorFileCache.containsKey(fileName)) {
             InputStream is = FileManager.loadFile(fileName, clazz);
 
-            //            InputStream is = ClassLoaderUtil.getResourceAsStream(fileName, clazz);
             if (is != null) {
                 retList = new ArrayList(ValidatorFileParser.parseActionValidators(is));
             } else {

File java/com/opensymphony/xwork/validator/ValidationInterceptor.java

View file
     }
 
     protected void before(ActionInvocation invocation) throws Exception {
-        List validators = ActionValidatorManager.getValidators(invocation);
+        List validators = ActionValidatorManager.getValidators(invocation.getAction().getClass(), invocation.getProxy().getActionName());
         Action action = invocation.getAction();
 
         if (log.isDebugEnabled()) {

File test/com/opensymphony/xwork/SimpleAction-validationAlias-validation.xml

View file
+<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">
+<validators>
+    <field name="baz">
+        <field-validator type="required">
+            <message>You must enter a value for baz.</message>
+        </field-validator>
+        <field-validator type="int">
+            <param name="min">2</param>
+            <param name="max">4</param>
+            <message>baz out of range.</message>
+        </field-validator>
+    </field>
+</validators>

File test/com/opensymphony/xwork/test/SimpleAction2-validationAlias-validation.xml

View file
+<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">
+<validators>
+    <field name="baz">
+        <field-validator type="required">
+            <message>You must enter a value for baz.</message>
+        </field-validator>
+        <field-validator type="int">
+            <param name="min">2"</param>
+            <param name="max">4"</param>
+            <message>baz out of range.</message>
+        </field-validator>
+    </field>
+</validators>

File test/com/opensymphony/xwork/test/validationAlias-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="baz">
-        <field-validator type="required">
-            <message>You must enter a value for baz.</message>
-        </field-validator>
-        <field-validator type="int">
-            <param name="min">2"</param>
-            <param name="max">4"</param>
-            <message>baz out of range.</message>
-        </field-validator>
-    </field>
-</validators>

File test/com/opensymphony/xwork/validationAlias-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="baz">
-        <field-validator type="required">
-            <message>You must enter a value for baz.</message>
-        </field-validator>
-        <field-validator type="int">
-            <param name="min">2"</param>
-            <param name="max">4"</param>
-            <message>baz out of range.</message>
-        </field-validator>
-    </field>
-</validators>

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

View file
  */
 package com.opensymphony.xwork.validator;
 
-import com.mockobjects.dynamic.Mock;
-
 import com.opensymphony.xwork.Action;
-import com.opensymphony.xwork.ActionInvocation;
-import com.opensymphony.xwork.ActionProxy;
+import com.opensymphony.xwork.SimpleAction;
 import com.opensymphony.xwork.test.SimpleAction2;
+import com.opensymphony.xwork.validator.validators.DateRangeFieldValidator;
+import com.opensymphony.xwork.validator.validators.ExpressionValidator;
+import com.opensymphony.xwork.validator.validators.IntRangeFieldValidator;
+import com.opensymphony.xwork.validator.validators.RequiredFieldValidator;
 
 import junit.framework.TestCase;
 
 public class ActionValidatorManagerTest extends TestCase {
     //~ Instance fields ////////////////////////////////////////////////////////
 
-    ActionInvocation invocation;
-    ActionProxy actionProxy;
-    Mock mockActionInvocation;
-    Mock mockActionProxy;
+    protected Action action;
+    protected final String alias = "validationAlias";
 
     //~ Methods ////////////////////////////////////////////////////////////////
 
     public void testBuildValidatorKey() {
-        String validatorKey = ActionValidatorManager.buildValidatorKey(invocation);
-        assertEquals("/namespace/validationAlias", validatorKey);
-        mockActionInvocation.verify();
-        mockActionProxy.verify();
+        String validatorKey = ActionValidatorManager.buildValidatorKey(action.getClass(), alias);
+        assertEquals(action.getClass().getName() + "/" + alias, validatorKey);
+    }
+
+    public void testBuildsValidatorsForAlias() {
+        List validatorList = ActionValidatorManager.getValidators(action.getClass(), alias);
+
+        // 2 in the class level + 2 in the alias
+        assertEquals(7, validatorList.size());
+
+        final FieldValidator barValidator1 = (FieldValidator) validatorList.get(0);
+        assertEquals("bar", barValidator1.getFieldName());
+        assertTrue(barValidator1 instanceof RequiredFieldValidator);
+
+        final FieldValidator barValidator2 = (FieldValidator) validatorList.get(1);
+        assertEquals("bar", barValidator2.getFieldName());
+        assertTrue(barValidator2 instanceof IntRangeFieldValidator);
+
+        final FieldValidator dateValidator = (FieldValidator) validatorList.get(2);
+        assertEquals("date", dateValidator.getFieldName());
+        assertTrue(dateValidator instanceof DateRangeFieldValidator);
+
+        final FieldValidator fooValidator = (FieldValidator) validatorList.get(3);
+        assertEquals("foo", fooValidator.getFieldName());
+        assertTrue(fooValidator instanceof IntRangeFieldValidator);
+
+        final Validator expressionValidator = (Validator) validatorList.get(4);
+        assertTrue(expressionValidator instanceof ExpressionValidator);
+
+        final FieldValidator bazValidator1 = (FieldValidator) validatorList.get(5);
+        assertEquals("baz", bazValidator1.getFieldName());
+        assertTrue(bazValidator1 instanceof RequiredFieldValidator);
+
+        final FieldValidator bazValidator2 = (FieldValidator) validatorList.get(6);
+        assertEquals("baz", bazValidator2.getFieldName());
+        assertTrue(bazValidator2 instanceof IntRangeFieldValidator);
     }
 
-    public void testSameAliasWithDifferentNamespace() {
-        Action action = new SimpleAction2();
-        mockActionInvocation.expectAndReturn("getAction", action);
-
-        //        mockActionProxy.expectAndReturn("getNamespace", "/namespace");
-        //        mockActionProxy.expectAndReturn("getActionName", "validationAlias");
-        mockActionInvocation.expectAndReturn("getProxy", actionProxy);
-        mockActionProxy.expectAndReturn("getActionName", "validationAlias");
-
-        List validatorList = ActionValidatorManager.getValidators(invocation);
-
-        // setup another call, with a different namespace. If it doesn't realize that there should be different
-        // validation configurations for the different namespaces, then some of these expected calls won't be made
-        // and things should fail.
-        mockActionInvocation.expectAndReturn("getProxy", actionProxy);
-        mockActionProxy.expectAndReturn("getNamespace", "/namespace2");
-        mockActionProxy.expectAndReturn("getActionName", "validationAlias");
-        mockActionInvocation.expectAndReturn("getAction", action);
-        mockActionInvocation.expectAndReturn("getProxy", actionProxy);
-        mockActionProxy.expectAndReturn("getActionName", "validationAlias");
-
-        List validatorList2 = ActionValidatorManager.getValidators(invocation);
-        assertNotSame(validatorList, validatorList2);
-        mockActionInvocation.verify();
-        mockActionProxy.verify();
+    public void testSameAliasWithDifferentClass() {
+        List validatorList = ActionValidatorManager.getValidators(action.getClass(), alias);
+        Action action2 = new SimpleAction2();
+        List validatorList2 = ActionValidatorManager.getValidators(action2.getClass(), alias);
+        assertFalse(validatorList.size() == validatorList2.size());
     }
 
     protected void setUp() {
-        mockActionInvocation = new Mock(ActionInvocation.class);
-        invocation = (ActionInvocation) mockActionInvocation.proxy();
-        mockActionProxy = new Mock(ActionProxy.class);
-        actionProxy = (ActionProxy) mockActionProxy.proxy();
-        mockActionInvocation.expectAndReturn("getProxy", actionProxy);
-        mockActionProxy.expectAndReturn("getNamespace", "/namespace");
-        mockActionProxy.expectAndReturn("getActionName", "validationAlias");
+        action = new SimpleAction();
     }
 }