Author: musachy
Date: Sun Mar 29 20:37:40 2009
New Revision: 759781
URL: http://svn.apache.org/viewvc?rev=759781&view=rev
Log:
Add alwaysInvokevalidate and programmatic attributes
Modified:
struts/sandbox/trunk/struts2-oval-plugin/src/main/java/org/apache/struts2/interceptor/OValValidationInterceptor.java
struts/sandbox/trunk/struts2-oval-plugin/src/test/java/org/apache/struts2/interceptor/OValValidationInterceptorTest.java
struts/sandbox/trunk/struts2-oval-plugin/src/test/java/org/apache/struts2/interceptor/SimpleField.java
struts/sandbox/trunk/struts2-oval-plugin/src/test/resources/oval-test.xml
Modified:
struts/sandbox/trunk/struts2-oval-plugin/src/main/java/org/apache/struts2/interceptor/OValValidationInterceptor.java
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-oval-plugin/src/main/java/org/apache/struts2/interceptor/OValValidationInterceptor.java?rev=759781&r1=759780&r2=759781&view=diff
==============================================================================
---
struts/sandbox/trunk/struts2-oval-plugin/src/main/java/org/apache/struts2/interceptor/OValValidationInterceptor.java
(original)
+++
struts/sandbox/trunk/struts2-oval-plugin/src/main/java/org/apache/struts2/interceptor/OValValidationInterceptor.java
Sun Mar 29 20:37:40 2009
@@ -21,8 +21,10 @@
package org.apache.struts2.interceptor;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
+import com.opensymphony.xwork2.interceptor.PrefixMethodInvocationUtil;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionProxy;
+import com.opensymphony.xwork2.Validateable;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.validator.ValidatorContext;
import com.opensymphony.xwork2.validator.DelegatingValidatorContext;
@@ -48,6 +50,32 @@
public class OValValidationInterceptor extends MethodFilterInterceptor {
private static final Logger LOG =
LoggerFactory.getLogger(OValValidationInterceptor.class);
+ private final static String VALIDATE_PREFIX = "validate";
+ private final static String ALT_VALIDATE_PREFIX = "validateDo";
+
+ private boolean alwaysInvokeValidate = true;
+ private boolean programmatic = true;
+
+ /**
+ * Determines if {...@link com.opensymphony.xwork2.Validateable}'s
<code>validate()</code> should be called,
+ * as well as methods whose name that start with "validate". Defaults to
"true".
+ *
+ * @param programmatic <tt>true</tt> then <code>validate()</code> is
invoked.
+ */
+ public void setProgrammatic(boolean programmatic) {
+ this.programmatic = programmatic;
+ }
+
+ /**
+ * Determines if {...@link com.opensymphony.xwork2.Validateable}'s
<code>validate()</code> should always
+ * be invoked. Default to "true".
+ *
+ * @param alwaysInvokeValidate <tt>true</tt> then <code>validate()</code>
is always invoked.
+ */
+ public void setAlwaysInvokeValidate(String alwaysInvokeValidate) {
+ this.alwaysInvokeValidate = Boolean.parseBoolean(alwaysInvokeValidate);
+ }
+
protected String doIntercept(ActionInvocation invocation) throws Exception
{
Object action = invocation.getAction();
ActionProxy proxy = invocation.getProxy();
@@ -58,6 +86,50 @@
LOG.debug("Validating [#0/#1] with method [#2]",
invocation.getProxy().getNamespace(), invocation.getProxy().getActionName(),
methodName);
}
+ //OVal vallidatio (no XML yet)
+ performOValValidation(action, valueStack, methodName);
+
+ //Validatable.valiedate() and validateX()
+ performProgrammaticValidation(invocation, action);
+
+ return invocation.invoke();
+ }
+
+ private void performProgrammaticValidation(ActionInvocation invocation,
Object action) throws Exception {
+ if (action instanceof Validateable && programmatic) {
+ // keep exception that might occured in validateXXX or
validateDoXXX
+ Exception exception = null;
+
+ Validateable validateable = (Validateable) action;
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Invoking validate() on action [#0]",
validateable.toString());
+ }
+
+ try {
+ PrefixMethodInvocationUtil.invokePrefixMethod(
+ invocation,
+ new String[]{VALIDATE_PREFIX, ALT_VALIDATE_PREFIX});
+ }
+ catch (Exception e) {
+ // If any exception occurred while doing reflection, we want
+ // validate() to be executed
+ LOG.warn("An exception occured while executing the prefix
method", e);
+ exception = e;
+ }
+
+
+ if (alwaysInvokeValidate) {
+ validateable.validate();
+ }
+
+ if (exception != null) {
+ // rethrow if something is wrong while doing validateXXX /
validateDoXXX
+ throw exception;
+ }
+ }
+ }
+
+ protected void performOValValidation(Object action, ValueStack valueStack,
String methodName) throws NoSuchMethodException {
Validator validator = new Validator();
//if the method is annotated with a @Profiles annotation, use those
profiles
Method method = action.getClass().getMethod(methodName, new Class[0]);
@@ -102,8 +174,6 @@
}
}
}
-
- return invocation.invoke();
}
/**
Modified:
struts/sandbox/trunk/struts2-oval-plugin/src/test/java/org/apache/struts2/interceptor/OValValidationInterceptorTest.java
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-oval-plugin/src/test/java/org/apache/struts2/interceptor/OValValidationInterceptorTest.java?rev=759781&r1=759780&r2=759781&view=diff
==============================================================================
---
struts/sandbox/trunk/struts2-oval-plugin/src/test/java/org/apache/struts2/interceptor/OValValidationInterceptorTest.java
(original)
+++
struts/sandbox/trunk/struts2-oval-plugin/src/test/java/org/apache/struts2/interceptor/OValValidationInterceptorTest.java
Sun Mar 29 20:37:40 2009
@@ -86,7 +86,7 @@
assertNotNull(fieldErrors.get("name"));
}
- public void testFieldsWithMultipleProfiles() throws Exception {
+ public void testFieldsWithMultipleProfiles() throws Exception {
ActionProxy baseActionProxy =
actionProxyFactory.createActionProxy("oval", "fieldsWidthProfiles13", null,
null);
FieldsWithProfiles action = (FieldsWithProfiles)
baseActionProxy.getAction();
baseActionProxy.execute();
@@ -121,7 +121,7 @@
assertValue(fieldErrors, "name", Arrays.asList("name cannot be null"));
}
- public void testSimpleFieldI18nDefaultKey() throws Exception {
+ public void testSimpleFieldI18nDefaultKey() throws Exception {
ActionProxy baseActionProxy =
actionProxyFactory.createActionProxy("oval", "simpleFieldI18nDefaultKey", null,
null);
SimpleFieldI18nDefaultKey action = (SimpleFieldI18nDefaultKey)
baseActionProxy.getAction();
baseActionProxy.execute();
@@ -133,6 +133,47 @@
assertValue(fieldErrors, "name", Arrays.asList("notnull.field"));
}
+
+ public void testProgrammaticValidation() throws Exception {
+ ActionProxy baseActionProxy =
actionProxyFactory.createActionProxy("oval", "simpleField", null, null);
+ SimpleField action = (SimpleField) baseActionProxy.getAction();
+ baseActionProxy.execute();
+
+ Map<String, List<String>> fieldErrors = ((ValidationAware)
baseActionProxy.getAction()).getFieldErrors();
+ assertNotNull(fieldErrors);
+ assertEquals(1, fieldErrors.size());
+ assertValue(fieldErrors, "name", Arrays.asList("name cannot be null"));
+ assertTrue(action.isValidateCalled());
+ assertTrue(action.isValidateExecuteCalled());
+ }
+
+ public void testProgrammaticValidationDontInvokeValidate() throws
Exception {
+ ActionProxy baseActionProxy =
actionProxyFactory.createActionProxy("oval", "simpleFieldNoValidate", null,
null);
+ SimpleField action = (SimpleField) baseActionProxy.getAction();
+ baseActionProxy.execute();
+
+ Map<String, List<String>> fieldErrors = ((ValidationAware)
baseActionProxy.getAction()).getFieldErrors();
+ assertNotNull(fieldErrors);
+ assertEquals(1, fieldErrors.size());
+ assertValue(fieldErrors, "name", Arrays.asList("name cannot be null"));
+ assertFalse(action.isValidateCalled());
+ assertTrue(action.isValidateExecuteCalled());
+ }
+
+ public void testProgrammaticValidationDontInvokeProgrammatic() throws
Exception {
+ ActionProxy baseActionProxy =
actionProxyFactory.createActionProxy("oval", "simpleFieldNoProgrammatic", null,
null);
+ SimpleField action = (SimpleField) baseActionProxy.getAction();
+ baseActionProxy.execute();
+
+ Map<String, List<String>> fieldErrors = ((ValidationAware)
baseActionProxy.getAction()).getFieldErrors();
+ assertNotNull(fieldErrors);
+ assertEquals(1, fieldErrors.size());
+ assertValue(fieldErrors, "name", Arrays.asList("name cannot be null"));
+ assertFalse(action.isValidateCalled());
+ assertFalse(action.isValidateExecuteCalled());
+ }
+
+
private void assertValue(Map<String, List<String>> map, String key,
List<String> expectedValues) {
assertNotNull(map);
assertNotNull(key);
Modified:
struts/sandbox/trunk/struts2-oval-plugin/src/test/java/org/apache/struts2/interceptor/SimpleField.java
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-oval-plugin/src/test/java/org/apache/struts2/interceptor/SimpleField.java?rev=759781&r1=759780&r2=759781&view=diff
==============================================================================
---
struts/sandbox/trunk/struts2-oval-plugin/src/test/java/org/apache/struts2/interceptor/SimpleField.java
(original)
+++
struts/sandbox/trunk/struts2-oval-plugin/src/test/java/org/apache/struts2/interceptor/SimpleField.java
Sun Mar 29 20:37:40 2009
@@ -30,6 +30,8 @@
@NotEmpty
@Length(max = 3)
private String name;
+ private boolean validateCalled;
+ private boolean validateExecuteCalled;
public String getName() {
return name;
@@ -38,4 +40,20 @@
public void setName(String name) {
this.name = name;
}
+
+ public void validate() {
+ this.validateCalled = true;
+ }
+
+ public void validateExecute() {
+ this.validateExecuteCalled = true;
+ }
+
+ public boolean isValidateCalled() {
+ return validateCalled;
+ }
+
+ public boolean isValidateExecuteCalled() {
+ return validateExecuteCalled;
+ }
}
Modified:
struts/sandbox/trunk/struts2-oval-plugin/src/test/resources/oval-test.xml
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-oval-plugin/src/test/resources/oval-test.xml?rev=759781&r1=759780&r2=759781&view=diff
==============================================================================
--- struts/sandbox/trunk/struts2-oval-plugin/src/test/resources/oval-test.xml
(original)
+++ struts/sandbox/trunk/struts2-oval-plugin/src/test/resources/oval-test.xml
Sun Mar 29 20:37:40 2009
@@ -1,7 +1,7 @@
<!DOCTYPE xwork PUBLIC
- "-//OpenSymphony Group//XWork 2.0//EN"
- "http://www.opensymphony.com/xwork/xwork-2.0.dtd"
- >
+ "-//OpenSymphony Group//XWork 2.0//EN"
+ "http://www.opensymphony.com/xwork/xwork-2.0.dtd"
+ >
<xwork>
<package namespace="oval" name="oval-test">
@@ -13,31 +13,43 @@
</interceptors>
<action name="simpleField"
class="org.apache.struts2.interceptor.SimpleField">
- <interceptor-ref name="ovalValidation" />
+ <interceptor-ref name="ovalValidation"/>
<result type="void"></result>
</action>
<action name="simpleMethod"
class="org.apache.struts2.interceptor.SimpleMethod">
- <interceptor-ref name="ovalValidation" />
+ <interceptor-ref name="ovalValidation"/>
<result type="void"></result>
</action>
<action name="simpleFieldOGNL"
class="org.apache.struts2.interceptor.SimpleFieldOGNLExpression">
- <interceptor-ref name="ovalValidation" />
+ <interceptor-ref name="ovalValidation"/>
<result type="void"></result>
</action>
<action name="simpleFieldI18n"
class="org.apache.struts2.interceptor.SimpleFieldI18n">
- <interceptor-ref name="ovalValidation" />
+ <interceptor-ref name="ovalValidation"/>
<result type="void"></result>
</action>
<action name="simpleFieldI18nDefaultKey"
class="org.apache.struts2.interceptor.SimpleFieldI18nDefaultKey">
- <interceptor-ref name="ovalValidation" />
+ <interceptor-ref name="ovalValidation"/>
<result type="void"></result>
</action>
<action name="fieldsWidthProfiles13"
class="org.apache.struts2.interceptor.FieldsWithProfiles" method="run1">
- <interceptor-ref name="ovalValidation" />
+ <interceptor-ref name="ovalValidation"/>
<result type="void"></result>
</action>
<action name="fieldsWidthProfiles2"
class="org.apache.struts2.interceptor.FieldsWithProfiles" method="run2">
- <interceptor-ref name="ovalValidation" />
+ <interceptor-ref name="ovalValidation"/>
+ <result type="void"></result>
+ </action>
+ <action name="simpleFieldNoValidate"
class="org.apache.struts2.interceptor.SimpleField">
+ <interceptor-ref name="ovalValidation">
+ <param name="alwaysInvokeValidate">false</param>
+ </interceptor-ref>
+ <result type="void"></result>
+ </action>
+ <action name="simpleFieldNoProgrammatic"
class="org.apache.struts2.interceptor.SimpleField">
+ <interceptor-ref name="ovalValidation">
+ <param name="programmatic">false</param>
+ </interceptor-ref>
<result type="void"></result>
</action>
</package>