Author: musachy
Date: Wed Apr 1 17:26:03 2009
New Revision: 760971
URL: http://svn.apache.org/viewvc?rev=760971&view=rev
Log:
Add support for JPA annotations
Added:
struts/sandbox/trunk/struts2-oval-plugin/src/test/java/org/apache/struts2/interceptor/SimpleFieldJPAAnnotations.java
Modified:
struts/sandbox/trunk/struts2-oval-plugin/pom.xml
struts/sandbox/trunk/struts2-oval-plugin/src/main/java/org/apache/struts2/interceptor/DefaultOValValidationManager.java
struts/sandbox/trunk/struts2-oval-plugin/src/main/resources/struts-plugin.xml
struts/sandbox/trunk/struts2-oval-plugin/src/test/java/org/apache/struts2/interceptor/OValValidationInterceptorTest.java
struts/sandbox/trunk/struts2-oval-plugin/src/test/resources/oval-test.xml
Modified: struts/sandbox/trunk/struts2-oval-plugin/pom.xml
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-oval-plugin/pom.xml?rev=760971&r1=760970&r2=760971&view=diff
==============================================================================
--- struts/sandbox/trunk/struts2-oval-plugin/pom.xml (original)
+++ struts/sandbox/trunk/struts2-oval-plugin/pom.xml Wed Apr 1 17:26:03 2009
@@ -56,6 +56,13 @@
<version>1.3.1</version>
</dependency>
+ <dependency>
+ <groupId>javax.persistence</groupId>
+ <artifactId>persistence-api</artifactId>
+ <version>1.0</version>
+ <scope>test</scope>
+ </dependency>
+
<!-- Testing -->
<dependency>
<groupId>org.easymock</groupId>
Modified:
struts/sandbox/trunk/struts2-oval-plugin/src/main/java/org/apache/struts2/interceptor/DefaultOValValidationManager.java
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-oval-plugin/src/main/java/org/apache/struts2/interceptor/DefaultOValValidationManager.java?rev=760971&r1=760970&r2=760971&view=diff
==============================================================================
---
struts/sandbox/trunk/struts2-oval-plugin/src/main/java/org/apache/struts2/interceptor/DefaultOValValidationManager.java
(original)
+++
struts/sandbox/trunk/struts2-oval-plugin/src/main/java/org/apache/struts2/interceptor/DefaultOValValidationManager.java
Wed Apr 1 17:26:03 2009
@@ -1,19 +1,20 @@
package org.apache.struts2.interceptor;
-import com.opensymphony.xwork2.ActionContext;
+import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.util.FileManager;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
-import net.sf.oval.configuration.xml.XMLConfigurer;
import net.sf.oval.configuration.Configurer;
import net.sf.oval.configuration.annotation.AnnotationsConfigurer;
+import net.sf.oval.configuration.annotation.JPAAnnotationsConfigurer;
+import net.sf.oval.configuration.xml.XMLConfigurer;
+import java.util.ArrayList;
+import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import java.util.ArrayList;
import java.util.Set;
import java.util.TreeSet;
-import java.util.HashMap;
public class DefaultOValValidationManager implements OValValidationManager {
@@ -23,6 +24,13 @@
protected final Map<String, List<Configurer>> validatorCache = new
HashMap<String, List<Configurer>>();
protected final Map<String, Configurer> validatorFileCache = new
HashMap<String, Configurer>();
+ protected boolean validateJPAAnnotations;
+
+ @Inject("struts.oval.validateJPAAnnotations")
+ public void setValidateJPAAnnotations(String validateJPAAnnotations) {
+ this.validateJPAAnnotations =
"true".equalsIgnoreCase(validateJPAAnnotations);
+ }
+
public synchronized List<Configurer> getConfigurers(Class clazz, String
context) {
final String validatorKey = buildValidatorKey(clazz, context);
@@ -31,18 +39,14 @@
List<Configurer> configurers = buildXMLConfigurers(clazz,
context, true, null);
//add an annotation configurer
- AnnotationsConfigurer annotationsConfigurer = new
AnnotationsConfigurer();
- configurers.add(annotationsConfigurer);
-
+ addAditionalConfigurers(configurers);
validatorCache.put(validatorKey, configurers);
}
} else {
List<Configurer> configurers = buildXMLConfigurers(clazz, context,
false, null);
//add an annotation configurer
- AnnotationsConfigurer annotationsConfigurer = new
AnnotationsConfigurer();
- configurers.add(annotationsConfigurer);
-
+ addAditionalConfigurers(configurers);
validatorCache.put(validatorKey, configurers);
}
@@ -50,6 +54,17 @@
return validatorCache.get(validatorKey);
}
+ private void addAditionalConfigurers(List<Configurer> configurers) {
+ AnnotationsConfigurer annotationsConfigurer = new
AnnotationsConfigurer();
+ configurers.add(annotationsConfigurer);
+
+ if (validateJPAAnnotations) {
+ if (LOG.isDebugEnabled())
+ LOG.debug("Adding support for JPA annotations validations in
OVal");
+ configurers.add(new JPAAnnotationsConfigurer());
+ }
+ }
+
protected static String buildValidatorKey(Class clazz, String context) {
StringBuilder sb = new StringBuilder(clazz.getName());
sb.append("/");
Modified:
struts/sandbox/trunk/struts2-oval-plugin/src/main/resources/struts-plugin.xml
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-oval-plugin/src/main/resources/struts-plugin.xml?rev=760971&r1=760970&r2=760971&view=diff
==============================================================================
---
struts/sandbox/trunk/struts2-oval-plugin/src/main/resources/struts-plugin.xml
(original)
+++
struts/sandbox/trunk/struts2-oval-plugin/src/main/resources/struts-plugin.xml
Wed Apr 1 17:26:03 2009
@@ -27,6 +27,7 @@
<struts>
<bean type="org.apache.struts2.interceptor.OValValidationManager"
class="org.apache.struts2.interceptor.DefaultOValValidationManager" />
+ <constant name="struts.oval.validateJPAAnnotations" value="false" />
<package name="oval-default" extends="struts-default">
<interceptors>
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=760971&r1=760970&r2=760971&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
Wed Apr 1 17:26:03 2009
@@ -43,6 +43,16 @@
assertValue(fieldErrors, "lastName", Arrays.asList("lastName cannot be
null"));
}
+ public void testSimpleFieldsJPAAnnotations() throws Exception {
+ ActionProxy baseActionProxy =
actionProxyFactory.createActionProxy("oval", "simpleFieldsJPA", null, null);
+ baseActionProxy.execute();
+
+ Map<String, List<String>> fieldErrors = ((ValidationAware)
baseActionProxy.getAction()).getFieldErrors();
+ assertNotNull(fieldErrors);
+ assertEquals(1, fieldErrors.size());
+ assertValue(fieldErrors, "firstName", Arrays.asList("firstName cannot
be null"));
+ }
+
public void testValidationInMethods() throws Exception {
ActionProxy baseActionProxy =
actionProxyFactory.createActionProxy("oval", "validationInMethods", null, null);
baseActionProxy.execute();
Added:
struts/sandbox/trunk/struts2-oval-plugin/src/test/java/org/apache/struts2/interceptor/SimpleFieldJPAAnnotations.java
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-oval-plugin/src/test/java/org/apache/struts2/interceptor/SimpleFieldJPAAnnotations.java?rev=760971&view=auto
==============================================================================
---
struts/sandbox/trunk/struts2-oval-plugin/src/test/java/org/apache/struts2/interceptor/SimpleFieldJPAAnnotations.java
(added)
+++
struts/sandbox/trunk/struts2-oval-plugin/src/test/java/org/apache/struts2/interceptor/SimpleFieldJPAAnnotations.java
Wed Apr 1 17:26:03 2009
@@ -0,0 +1,38 @@
+/*
+ * $Id$
+ *
+ * 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;
+
+import com.opensymphony.xwork2.ActionSupport;
+
+import javax.persistence.Basic;
+
+public class SimpleFieldJPAAnnotations extends ActionSupport {
+ @Basic(optional = false)
+ private String firstName;
+
+ public String getFirstName() {
+ return firstName;
+ }
+
+ public void setFirstName(String firstName) {
+ this.firstName = firstName;
+ }
+}
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=760971&r1=760970&r2=760971&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
Wed Apr 1 17:26:03 2009
@@ -6,6 +6,7 @@
<xwork>
<bean type="org.apache.struts2.interceptor.OValValidationManager"
class="org.apache.struts2.interceptor.DummyDefaultOValValidationManager"/>
<constant name="struts.enable.SlashesInActionNames" value="true"/>
+ <constant name="struts.oval.validateJPAAnnotations" value="true" />
<package namespace="oval" name="oval-test">
<result-types>
<result-type name="void"
class="org.apache.struts2.interceptor.VoidResult"/>
@@ -14,6 +15,10 @@
<interceptor name="ovalValidation"
class="org.apache.struts2.interceptor.OValValidationInterceptor"/>
</interceptors>
+ <action name="simpleFieldsJPA"
class="org.apache.struts2.interceptor.SimpleFieldJPAAnnotations">
+ <interceptor-ref name="ovalValidation"/>
+ <result type="void"></result>
+ </action>
<action name="simpleFieldsXMLChild"
class="org.apache.struts2.interceptor.SimpleFieldsXMLChild">
<interceptor-ref name="ovalValidation"/>
<result type="void"></result>