Author: mrdon
Date: Sat Jan 20 14:55:16 2007
New Revision: 498203

URL: http://svn.apache.org/viewvc?view=rev&rev=498203
Log:
Adding @SkipValidation annotation and associated interceptor
WW-1664

Added:
    
struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/validation/
    
struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/validation/AnnotationValidationInterceptor.java
    
struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/validation/SkipValidation.java
    
struts/struts2/trunk/core/src/test/java/org/apache/struts2/interceptor/validation/
    
struts/struts2/trunk/core/src/test/java/org/apache/struts2/interceptor/validation/AnnotationValidationInterceptorTest.java
Modified:
    struts/struts2/trunk/core/src/main/resources/struts-default.xml

Added: 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/validation/AnnotationValidationInterceptor.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/validation/AnnotationValidationInterceptor.java?view=auto&rev=498203
==============================================================================
--- 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/validation/AnnotationValidationInterceptor.java
 (added)
+++ 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/validation/AnnotationValidationInterceptor.java
 Sat Jan 20 14:55:16 2007
@@ -0,0 +1,68 @@
+/*
+ * $Id: Result.java 490514 2006-12-27 15:25:48Z ddewolf $
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.struts2.interceptor.validation;
+
+import java.lang.reflect.Method;
+
+import com.opensymphony.xwork2.ActionInvocation;
+import com.opensymphony.xwork2.validator.ValidationInterceptor;
+
+/**
+ * Extends the xwork validation interceptor to also check for a @SkipValidation
+ * annotation, and if found, don't validate this action method
+ */
+public class AnnotationValidationInterceptor extends ValidationInterceptor {
+    
+    /** Auto-generated serialization id */
+    private static final long serialVersionUID = 1813272797367431184L;
+
+    protected String doIntercept(ActionInvocation invocation) throws Exception 
{
+        
+        Object action = invocation.getAction();
+        if (action != null) {
+            Method method = getActionMethod(action.getClass(), 
invocation.getProxy().getMethod());
+            SkipValidation skip = (SkipValidation) 
method.getAnnotation(SkipValidation.class);
+            if (skip != null) {
+                return invocation.invoke();
+            }
+        }
+        
+        return super.doIntercept(invocation);
+    }
+    
+    // FIXME: This is copied from DefaultActionInvocation but should be 
exposed through the interface
+    protected Method getActionMethod(Class actionClass, String methodName) 
throws NoSuchMethodException {
+        Method method;
+        try {
+            method = actionClass.getMethod(methodName, new Class[0]);
+        } catch (NoSuchMethodException e) {
+            // hmm -- OK, try doXxx instead
+            try {
+                String altMethodName = "do" + methodName.substring(0, 
1).toUpperCase() + methodName.substring(1);
+                method = actionClass.getMethod(altMethodName, new Class[0]);
+            } catch (NoSuchMethodException e1) {
+                // throw the original one
+                throw e;
+            }
+        }
+        return method;
+    }
+}

Added: 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/validation/SkipValidation.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/validation/SkipValidation.java?view=auto&rev=498203
==============================================================================
--- 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/validation/SkipValidation.java
 (added)
+++ 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/validation/SkipValidation.java
 Sat Jan 20 14:55:16 2007
@@ -0,0 +1,38 @@
+/*
+ * $Id: Result.java 490514 2006-12-27 15:25:48Z ddewolf $
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.struts2.interceptor.validation;
+
+import static java.lang.annotation.ElementType.METHOD;
+
+import com.opensymphony.xwork2.Action;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Marks an Action method to not be validated
+ */
[EMAIL PROTECTED](RetentionPolicy.RUNTIME)
[EMAIL PROTECTED](METHOD)
+public @interface SkipValidation {
+}
+

Modified: struts/struts2/trunk/core/src/main/resources/struts-default.xml
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/resources/struts-default.xml?view=diff&rev=498203&r1=498202&r2=498203
==============================================================================
--- struts/struts2/trunk/core/src/main/resources/struts-default.xml (original)
+++ struts/struts2/trunk/core/src/main/resources/struts-default.xml Sat Jan 20 
14:55:16 2007
@@ -84,7 +84,7 @@
             <interceptor name="timer" 
class="com.opensymphony.xwork2.interceptor.TimerInterceptor"/>
             <interceptor name="token" 
class="org.apache.struts2.interceptor.TokenInterceptor"/>
             <interceptor name="token-session" 
class="org.apache.struts2.interceptor.TokenSessionStoreInterceptor"/>
-            <interceptor name="validation" 
class="com.opensymphony.xwork2.validator.ValidationInterceptor"/>
+            <interceptor name="validation" 
class="org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor"/>
             <interceptor name="workflow" 
class="com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor"/>
             <interceptor name="store" 
class="org.apache.struts2.interceptor.MessageStoreInterceptor" />
             <interceptor name="checkbox" 
class="org.apache.struts2.interceptor.CheckboxInterceptor" />

Added: 
struts/struts2/trunk/core/src/test/java/org/apache/struts2/interceptor/validation/AnnotationValidationInterceptorTest.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/interceptor/validation/AnnotationValidationInterceptorTest.java?view=auto&rev=498203
==============================================================================
--- 
struts/struts2/trunk/core/src/test/java/org/apache/struts2/interceptor/validation/AnnotationValidationInterceptorTest.java
 (added)
+++ 
struts/struts2/trunk/core/src/test/java/org/apache/struts2/interceptor/validation/AnnotationValidationInterceptorTest.java
 Sat Jan 20 14:55:16 2007
@@ -0,0 +1,72 @@
+/*
+ * $Id: AnnotationValidationInterceptorTest.java 478625 2006-11-23 17:31:52Z 
wsmoak $
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.struts2.interceptor.validation;
+
+import org.apache.struts2.StrutsTestCase;
+
+import com.mockobjects.dynamic.Mock;
+import com.opensymphony.xwork2.ActionInvocation;
+import com.opensymphony.xwork2.ActionProxy;
+import com.opensymphony.xwork2.Validateable;
+
+public class AnnotationValidationInterceptorTest extends StrutsTestCase {
+
+    private AnnotationValidationInterceptor interceptor = new 
AnnotationValidationInterceptor();
+    private Mock mockActionInvocation;
+    private Mock mockActionProxy;
+    private TestAction test;
+
+    public void setUp() throws Exception {
+        super.setUp();
+        test = new TestAction();
+        interceptor = new AnnotationValidationInterceptor();
+        mockActionInvocation = new Mock(ActionInvocation.class);
+        mockActionProxy = new Mock(ActionProxy.class);
+        mockActionInvocation.matchAndReturn("getProxy", (ActionProxy) 
mockActionProxy.proxy());
+        mockActionInvocation.matchAndReturn("getAction", test);
+        mockActionInvocation.expect("invoke");
+    }
+
+    public void testShouldNotSkip() throws Exception {
+        mockActionProxy.expectAndReturn("getMethod", "execute");
+        mockActionProxy.expectAndReturn("getActionName", "foo");
+        
interceptor.doIntercept((ActionInvocation)mockActionInvocation.proxy());
+        mockActionProxy.verify();
+    }
+    
+    public void testShouldSkip() throws Exception {
+        mockActionProxy.expectAndReturn("getMethod", "skipMe");
+        
interceptor.doIntercept((ActionInvocation)mockActionInvocation.proxy());
+        mockActionProxy.verify();
+    }
+    
+    public static class TestAction  {
+        
+        public String execute() {
+            return "execute";
+        }
+        
+        @SkipValidation
+        public String skipMe() {
+            return "skipme";
+        }
+    }
+}


Reply via email to