1. opensymphony
  2. xwork

Commits

rainerh  committed 2cdc65d

Allow easy overriding of "excludeMethods" and "includeMethods" of ValidationInterceptor
o added wildcard support for excludeMethods/includeMethods
o added wildcard support for Result mappings
Issue number: WW-1187
Obtained from:
Submitted by:
Reviewed by:

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

  • Participants
  • Parent commits f2e1e7e
  • Branches master

Comments (0)

Files changed (9)

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

View file
  • Ignore whitespace
 import com.opensymphony.xwork.config.entities.ResultConfig;
 import com.opensymphony.xwork.interceptor.Interceptor;
 import com.opensymphony.xwork.interceptor.PreResultListener;
-import com.opensymphony.xwork.util.OgnlUtil;
 import com.opensymphony.xwork.util.OgnlValueStack;
 import com.opensymphony.xwork.util.XWorkContinuationConfig;
 import com.uwyn.rife.continuations.ContinuableObject;
         Map results = config.getResults();
 
         ResultConfig resultConfig = null;
+
+        System.out.println("results: " + results);
         synchronized (config) {
             try {
                 resultConfig = (ResultConfig) results.get(resultCode);
             } catch (NullPointerException e) {
-                return null;
+            }
+            if (resultConfig == null ) {
+                // If no result is found for the given resultCode, try to get a wildcard '*' match.
+                resultConfig = (ResultConfig) results.get("*");
             }
         }
 

File src/java/com/opensymphony/xwork/interceptor/MethodFilterInterceptor.java

View file
  • Ignore whitespace
  * includeMethods takes precedence over excludeMethods.
  * 
  * @author <a href='mailto:the_mindstorm[at]evolva[dot]ro'>Alexandru Popescu</a>
+ * @author Rainer Hermanns
  * @version $Date$ $Id$
  */
 public abstract class MethodFilterInterceptor implements Interceptor {
     protected boolean applyInterceptor(ActionInvocation invocation) {
         String method = invocation.getProxy().getMethod();
         // ValidationInterceptor
-        if (excludeMethods.contains(method) && !includeMethods.contains(method)) {
-            log.debug("Skipping validation. Method [" + method + "] found in exclude list.");
+        if (((excludeMethods.contains("*") && !includeMethods.contains("*"))
+                || excludeMethods.contains(method))
+                && !includeMethods.contains(method)) {
+            log.debug("Skipping Interceptor... Method [" + method + "] found in exclude list.");
             return false;
         }
 
-        if ( includeMethods.size() == 0 || includeMethods.contains(method) ) {
-            return true;
-        }
-        
-        return false;
+        return includeMethods.size() == 0 || includeMethods.contains(method) || includeMethods.contains("*");
+
     }
 
     /**

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

View file
  • Ignore whitespace
      * @throws Exception if an error occurs validating the action.
      */
     protected void doBeforeInvocation(ActionInvocation invocation) throws Exception {
-    	String method = invocation.getProxy().getMethod();
-        if (excludeMethods.contains(method) && !includeMethods.contains(method)) {
-            if (log.isDebugEnabled()) {
-                log.debug("Skipping validation. Method ["+method+"] found in exclude list.");
-            }
-            return;
-        }
-
-        if ( includeMethods.size() == 0 || includeMethods.contains(method) ) {
-            Object action = invocation.getAction();
-            String context = invocation.getProxy().getActionName();
+        Object action = invocation.getAction();
+        String context = invocation.getProxy().getActionName();
 
-            if (log.isDebugEnabled()) {
-                log.debug("Validating "
-                        + invocation.getProxy().getNamespace() + "/" + invocation.getProxy().getActionName() + " with method "+invocation.getProxy().getMethod()+".");
-            }
-
-            ActionValidatorManagerFactory.getInstance().validate(action, context);
+        if (log.isDebugEnabled()) {
+            log.debug("Validating "
+                    + invocation.getProxy().getNamespace() + "/" + invocation.getProxy().getActionName() + " with method "+invocation.getProxy().getMethod()+".");
         }
+
+        ActionValidatorManagerFactory.getInstance().validate(action, context);
     }
 
     protected String doIntercept(ActionInvocation invocation) throws Exception {

File src/test/com/opensymphony/xwork/WildCardResultTest.java

View file
  • Ignore whitespace
+/*
+ * Copyright (c) 2002-2006 by OpenSymphony
+ * All rights reserved.
+ */
+
+package com.opensymphony.xwork;
+
+import junit.framework.TestCase;
+import com.opensymphony.xwork.config.ConfigurationManager;
+import com.opensymphony.xwork.mock.MockResult;
+
+/**
+ * <code>WildCardResultTest</code>
+ *
+ * @author <a href="mailto:hermanns@aixcept.de">Rainer Hermanns</a>
+ * @version $Id$
+ */
+public class WildCardResultTest extends TestCase {
+
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        // ensure we're using the default configuration, not simple config
+        ConfigurationManager.clearConfigurationProviders();
+        ConfigurationManager.getConfiguration().reload();
+    }
+
+    public void testWildCardEvaluation() throws Exception {
+        ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(null, "WildCard", null);
+        assertEquals("success", proxy.execute());
+        assertEquals(VoidResult.class, proxy.getInvocation().getResult().getClass());
+
+        proxy = ActionProxyFactory.getFactory().createActionProxy(null, "WildCardInput", null);
+        assertEquals("input", proxy.execute());
+        assertEquals(MockResult.class, proxy.getInvocation().getResult().getClass());
+
+        proxy = ActionProxyFactory.getFactory().createActionProxy(null, "WildCardError", null);
+        assertEquals("error", proxy.execute());
+        assertEquals(MockResult.class, proxy.getInvocation().getResult().getClass());
+    }
+
+}

File src/test/com/opensymphony/xwork/config/providers/XmlConfigurationProviderActionsTest.java

View file
  • Ignore whitespace
 
 
 /**
- * Created by IntelliJ IDEA.
- * User: Mike
- * Date: May 6, 2003
- * Time: 3:10:16 PM
- * To change this template use Options | File Templates.
+ * @author Mike
+ * @author Rainer Hermanns
  */
 public class XmlConfigurationProviderActionsTest extends ConfigurationTestBase {
 
         ActionConfig barAction = new ActionConfig(null, SimpleAction.class, params, new HashMap(), new ArrayList());
 
         // foo action is a little more complex, two params, a result and an interceptor stack
+        results = new HashMap();
         params = new HashMap();
         params.put("foo", "18");
         params.put("bar", "24");
 
         ActionConfig fooAction = new ActionConfig(null, SimpleAction.class, params, results, interceptors);
 
+        // wildcard action is simple wildcard example
+        results = new HashMap();
+        results.put("*", new ResultConfig("*", MockResult.class, new HashMap()));
+
+        ActionConfig wildcardAction = new ActionConfig(null, SimpleAction.class, new HashMap(), results, interceptors);
+
         // fooBar action is a little more complex, two params, a result and an interceptor stack
         params = new HashMap();
         params.put("foo", "18");
         params.put("bar", "24");
+        results = new HashMap();
         results.put("success", new ResultConfig("success", MockResult.class, new HashMap()));
 
         ExceptionMappingConfig exceptionConfig = new ExceptionMappingConfig("runtime", "java.lang.RuntimeException", "exception");
         Map actionConfigs = pkg.getActionConfigs();
 
         // assertions
-        assertEquals(5, actionConfigs.size());
+        assertEquals(6, actionConfigs.size());
         assertEquals(barAction, actionConfigs.get("Bar"));
         assertEquals(fooAction, actionConfigs.get("Foo"));
+        assertEquals(wildcardAction, actionConfigs.get("WildCard"));
         assertEquals(fooBarAction, actionConfigs.get("FooBar"));
         assertEquals(intAction, actionConfigs.get("TestInterceptorParam"));
         assertEquals(intOverAction, actionConfigs.get("TestInterceptorParamOverride"));

File src/test/com/opensymphony/xwork/config/providers/xwork-test-actions.xml

View file
  • Ignore whitespace
             <interceptor-ref name="defaultStack"/>
         </action>
 
+        <action name="WildCard" class="com.opensymphony.xwork.SimpleAction">
+            <result name="*" type="mock" />
+            <interceptor-ref name="defaultStack"/>
+        </action>
+
         <action name="FooBar" class="com.opensymphony.xwork.SimpleAction">
             <param name="foo">18</param>
             <param name="bar">24</param>

File src/test/com/opensymphony/xwork/interceptor/DefaultWorkflowInterceptorTest.java

View file
  • Ignore whitespace
         assertEquals(result, interceptor.intercept(invocation));
     }
 
+    public void testExcludesMethodWithWildCard() throws Exception {
+        interceptor.setExcludeMethods("*");
+        final String result = "testing123";
+        invocationMock.expectAndReturn("invoke", result);
+        assertEquals(result, interceptor.intercept(invocation));
+    }
+
+    public void testIncludesMethodWithWildcard() throws Exception {
+        interceptor.setIncludeMethods("*");
+        actionMock.expectAndReturn("hasErrors", false);
+        actionMock.expect("validate");
+        final String result = "testing123";
+        invocationMock.expectAndReturn("invoke", result);
+        invocationMock.expectAndReturn("getAction", action);
+        assertEquals(result, interceptor.intercept(invocation));
+    }
+
+
     public void testIncludesMethod() throws Exception {
         interceptor.setIncludeMethods("execute");
         actionMock.expectAndReturn("hasErrors", false);
         assertEquals(result, interceptor.intercept(invocation));
     }
 
+    public void testIncludesAndExcludesMethodAllWildCarded() throws Exception {
+        interceptor.setExcludeMethods("*");
+        interceptor.setIncludeMethods("*");
+        actionMock.expectAndReturn("hasErrors", false);
+        actionMock.expect("validate");
+        final String result = "testing123";
+        invocationMock.expectAndReturn("invoke", result);
+        invocationMock.expectAndReturn("getAction", action);
+        assertEquals(result, interceptor.intercept(invocation));
+    }
+
+    public void testIncludesAndExcludesMethodWithExcludeWildcard() throws Exception {
+        interceptor.setExcludeMethods("*");
+        interceptor.setIncludeMethods("execute");
+        actionMock.expectAndReturn("hasErrors", false);
+        actionMock.expect("validate");
+        final String result = "testing123";
+        invocationMock.expectAndReturn("invoke", result);
+        invocationMock.expectAndReturn("getAction", action);
+        assertEquals(result, interceptor.intercept(invocation));
+    }
+
+    public void testIncludesAndExcludesMethodWithIncludeWildcardAndNoMatches() throws Exception {
+        interceptor.setExcludeMethods("execute,input,validate");
+        interceptor.setIncludeMethods("*");
+        final String result = "testing123";
+        invocationMock.expectAndReturn("invoke", result);
+        assertEquals(result, interceptor.intercept(invocation));
+    }
+
+    public void testIncludesAndExcludesMethodWithIncludeWildcard() throws Exception {
+        interceptor.setExcludeMethods("input,validate");
+        interceptor.setIncludeMethods("*");
+        actionMock.expectAndReturn("hasErrors", false);
+        actionMock.expect("validate");
+        final String result = "testing123";
+        invocationMock.expectAndReturn("invoke", result);
+        invocationMock.expectAndReturn("getAction", action);
+        assertEquals(result, interceptor.intercept(invocation));
+    }
+
     protected void setUp() throws Exception {
         super.setUp();
         actionMock = new Mock(ValidateAction.class);

File src/test/xwork-default.xml

View file
  • Ignore whitespace
         <result-types>
             <result-type name="chain" class="com.opensymphony.xwork.ActionChainResult" default="true"/>
             <result-type name="void" class="com.opensymphony.xwork.VoidResult"/>
+            <result-type name="mock" class="com.opensymphony.xwork.mock.MockResult"/>
         </result-types>
 
         <interceptors>

File src/test/xwork.xml

View file
  • Ignore whitespace
             <interceptor-ref name="debugStack"/>
             <interceptor-ref name="defaultStack"/>
         </action>
-		
 
-		<action name="aliasTest" class="com.opensymphony.xwork.SimpleAction">
+        <action name="WildCard" class="com.opensymphony.xwork.SimpleAction">
+            <param name="foo">17</param>
+            <param name="bar">23</param>
+            <result name="success" type="void" />
+            <result name="*" type="mock" />
+            <interceptor-ref name="defaultStack"/>
+        </action>
+
+        <action name="WildCardInput" class="com.opensymphony.xwork.SimpleAction" method="input">
+            <param name="foo">17</param>
+            <param name="bar">23</param>
+            <result name="success" type="void" />
+            <result name="*" type="mock" />
+            <interceptor-ref name="defaultStack"/>
+        </action>
+
+        <action name="WildCardError" class="com.opensymphony.xwork.SimpleAction">
+            <result name="success" type="void" />
+            <result name="*" type="mock" />
+            <interceptor-ref name="defaultStack"/>
+        </action>
+
+        <action name="aliasTest" class="com.opensymphony.xwork.SimpleAction">
             <param name="foo">17</param>
             <param name="bar">23</param>
            	<param name="aliases">#{ "aliasSource" : "aliasDest", "bar":"baz" }</param>