openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dwo...@apache.org
Subject svn commit: r792248 - in /openjpa/trunk/openjpa-integration/validation/src/test: java/org/apache/openjpa/integration/validation/ resources/org/apache/openjpa/integration/validation/
Date Wed, 08 Jul 2009 18:22:16 GMT
Author: dwoods
Date: Wed Jul  8 18:22:16 2009
New Revision: 792248

URL: http://svn.apache.org/viewvc?rev=792248&view=rev
Log:
OPENJPA-1157 adding @Valid tests of embedded one-to-one entity and tracing of the included
ConstraintViolation exceptions contained within the thrown ConstraintViolationException while
validating the expected number are in the Set.

Modified:
    openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/Address.java
    openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/IAddress.java
    openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/IPerson.java
    openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/Person.java
    openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestConstraints.java
    openjpa/trunk/openjpa-integration/validation/src/test/resources/org/apache/openjpa/integration/validation/persistence.xml

Modified: openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/Address.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/Address.java?rev=792248&r1=792247&r2=792248&view=diff
==============================================================================
--- openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/Address.java
(original)
+++ openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/Address.java
Wed Jul  8 18:22:16 2009
@@ -27,7 +27,8 @@
 import javax.persistence.Inheritance;
 import javax.persistence.InheritanceType;
 import javax.persistence.Transient;
-import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Pattern;
+import javax.validation.constraints.Size;
 
 @Entity(name="VAddress")
 @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@@ -40,24 +41,31 @@
     private long id;
 
     @Basic
-    @NotNull
-    private String streetAddress;
+    @Pattern(regexp = "^.*$", flags = Pattern.Flag.CASE_INSENSITIVE,
+        message = "can contain any character")
+    private String streetAddress;   // @NotNull is on IAddress getter
 
     @Basic
-    @NotNull
-    private String city;
+    @Pattern(regexp = "^[A-Z .-]*$", flags = Pattern.Flag.CASE_INSENSITIVE,
+        message = "can only contain alpha, '.', '-' and ' ' characters")
+    private String city;            // @NotNull is on IAddress getter
 
     @Basic
-    @NotNull
-    private String state;
+    @Size(min = 2, max = 2)
+    @Pattern(regexp = "^[A-Z]+$", flags = Pattern.Flag.CASE_INSENSITIVE,
+        message = "can only contain alpha characters")
+    private String state;           // @NotNull is on IAddress getter
 
     @Basic
-    @NotNull
-    private String postalCode;
+    @Size(min = 5, max = 5)
+    @Pattern(regexp = "^[0-9]+$", flags = Pattern.Flag.CASE_INSENSITIVE,
+        message = "can only contain numeric characters")
+    private String postalCode;      // @NotNull is on IAddress getter
 
     @Basic
     private String phoneNumber;
 
+    
     public void setStreetAddress(String streetAddress) {
         this.streetAddress = streetAddress;
     }
@@ -66,7 +74,6 @@
         return this.streetAddress;
     }
 
-
     public void setCity(String city) {
         this.city = city;
     }
@@ -75,7 +82,6 @@
         return this.city;
     }
 
-
     public void setState(String state) {
         this.state = state;
     }
@@ -84,7 +90,6 @@
         return this.state;
     }
 
-
     public void setPostalCode(String postalCode) {
         this.postalCode = postalCode;
     }
@@ -93,7 +98,6 @@
         return this.postalCode;
     }
 
-
     public void setPhoneNumber(String phoneNumber) {
         this.phoneNumber = phoneNumber;
     }
@@ -102,7 +106,6 @@
         return this.phoneNumber;
     }
 
-
     public void setId(long id) {
         this.id = id;
     }

Modified: openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/IAddress.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/IAddress.java?rev=792248&r1=792247&r2=792248&view=diff
==============================================================================
--- openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/IAddress.java
(original)
+++ openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/IAddress.java
Wed Jul  8 18:22:16 2009
@@ -18,20 +18,26 @@
  */
 package org.apache.openjpa.integration.validation;
 
+import javax.validation.constraints.NotNull;
+
 public interface IAddress {
 
-    public void setStreetAddress(String streetAddress);
+    @NotNull
     public String getStreetAddress();
+    public void setStreetAddress(String streetAddress);
 
-    public void setCity(String city);
+    @NotNull
     public String getCity();
+    public void setCity(String city);
 
-    public void setState(String state);
+    @NotNull
     public String getState();
+    public void setState(String state);
 
-    public void setPostalCode(String postalCode);
+    @NotNull
     public String getPostalCode();
+    public void setPostalCode(String postalCode);
 
-    public void setPhoneNumber(String phoneNumber);
     public String getPhoneNumber();
+    public void setPhoneNumber(String phoneNumber);
 }

Modified: openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/IPerson.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/IPerson.java?rev=792248&r1=792247&r2=792248&view=diff
==============================================================================
--- openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/IPerson.java
(original)
+++ openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/IPerson.java
Wed Jul  8 18:22:16 2009
@@ -18,14 +18,19 @@
  */
 package org.apache.openjpa.integration.validation;
 
+import javax.validation.constraints.NotNull;
+
 public interface IPerson {
 
-    public void setFirstName(String firstName);
+    @NotNull
     public String getFirstName();
+    public void setFirstName(String firstName);
 
-    public void setLastName(String lastName);
+    @NotNull
     public String getLastName();
+    public void setLastName(String lastName);
 
-    public void setHomeAddress(IAddress homeAddress);
+    @NotNull
     public IAddress getHomeAddress();
+    public void setHomeAddress(IAddress homeAddress);
 }

Modified: openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/Person.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/Person.java?rev=792248&r1=792247&r2=792248&view=diff
==============================================================================
--- openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/Person.java
(original)
+++ openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/Person.java
Wed Jul  8 18:22:16 2009
@@ -30,10 +30,11 @@
 import javax.persistence.Transient;
 import javax.validation.Valid;
 import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Pattern;
 
 @Entity(name="VPerson")
 @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
-public abstract class Person implements IPerson, Serializable {
+public class Person implements IPerson, Serializable {
     @Transient
     private static final long serialVersionUID = 1L;
 
@@ -42,18 +43,21 @@
     private long id;
 
     @Basic
-    @NotNull
-    private String firstName;
+    @Pattern(regexp = "^[A-Z0-9-]+$", flags = Pattern.Flag.CASE_INSENSITIVE,
+        message = "can only contain alphanumeric characters")
+    private String firstName;   // @NotNull is on IPerson getter
 
     @Basic
-    @NotNull
-    private String lastName;
+    @Pattern(regexp = "^[A-Z0-9-]+$", flags = Pattern.Flag.CASE_INSENSITIVE,
+        message = "can only contain alphanumeric characters")
+    private String lastName;    // @NotNull is on IPerson getter
 
     @OneToOne
-    @Valid
     @NotNull
+    @Valid
     private Address homeAddress;
 
+    
     public void setFirstName(String firstName) {
         this.firstName = firstName;
     }

Modified: openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestConstraints.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestConstraints.java?rev=792248&r1=792247&r2=792248&view=diff
==============================================================================
--- openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestConstraints.java
(original)
+++ openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestConstraints.java
Wed Jul  8 18:22:16 2009
@@ -13,8 +13,11 @@
  */
 package org.apache.openjpa.integration.validation;
 
+import java.util.Set;
+
 import javax.persistence.Query;
 import javax.persistence.ValidationMode;
+import javax.validation.ConstraintViolation;
 import javax.validation.ConstraintViolationException;
 
 import org.apache.openjpa.conf.OpenJPAConfiguration;
@@ -54,6 +57,7 @@
  *   23) Test @Past constraint exception on getter in mode=AUTO
  *   25) Test @Pattern constraint exception on variables in mode=AUTO
  *   26) Test @Pattern constraint exception on getter in mode=AUTO
+ *   28) Test @Valid constraint exceptions in mode=AUTO
  *   
  *   Basic constraint test for no violations:
  *   6)  Persist @NotNull and @Null constraints pass in mode=AUTO
@@ -64,6 +68,7 @@
  *   21) Test @Size constraints pass in mode=AUTO
  *   24) Test @Past and @Future constraints pass in mode=AUTO
  *   27) Test @Pattern constraints pass in mode=AUTO
+ *   29) Test @Valid constraints pass in mode=AUTO
  *
  * @version $Rev$ $Date$
  */
@@ -75,7 +80,8 @@
             ConstraintNull.class, ConstraintBoolean.class,
             ConstraintDecimal.class, ConstraintNumber.class,
             ConstraintDigits.class, ConstraintSize.class,
-            ConstraintDates.class, ConstraintPattern.class);
+            ConstraintDates.class, ConstraintPattern.class,
+            Person.class, Address.class);
     }
 
     /**
@@ -1111,6 +1117,146 @@
         }
     }
 
+    /**
+     * Scenario being tested:
+     *   28) Test @Valid constraint exceptions in mode=AUTO
+     *       Basic constraint test for violation exceptions.
+     */
+    public void testValidFailuresConstraint() {
+        Address a = new Address();
+        getLog().trace("testValidFailuresConstraint() started");
+        
+        // Part 1 - Create an invalid Address entity
+        try {
+            OpenJPAEntityManagerFactorySPI emf = (OpenJPAEntityManagerFactorySPI)
+            OpenJPAPersistence.createEntityManagerFactory(
+                    "address-none-mode",
+                    "org/apache/openjpa/integration/validation/persistence.xml");
+            assertNotNull(emf);
+            // create EM
+            OpenJPAEntityManager em = emf.createEntityManager();
+            assertNotNull(em);
+            try{
+                // verify Validation Mode
+                OpenJPAConfiguration conf = em.getConfiguration();
+                assertNotNull(conf);
+                assertTrue("ValidationMode",
+                    conf.getValidationMode().equalsIgnoreCase("NONE"));
+                // provide an invalid Address (every value is invalid)
+                em.getTransaction().begin();
+                a.setStreetAddress(null);
+                a.setCity("a1!b2@c3#");
+                a.setState("00");
+                a.setPostalCode("a1b2c3");
+                // persist, which should NOT cause a CVE
+                em.persist(a);
+                em.getTransaction().commit();
+                getLog().trace("testValidFailuresConstraint() Part 1 of 2 passed");
+            } catch (Exception e) {
+                // unexpected
+                getLog().trace("testValidFailuresConstraint() Part 1 of 2 failed");
+                fail("Caught unexpected exception = " + e);
+            } finally {
+                if (em.getTransaction().isActive()) {
+                    em.getTransaction().rollback();
+                }
+                cleanup(emf);
+                em = null;
+            }
+        } catch (Exception e) {
+            // unexpected
+            getLog().trace("testValidFailuresConstraint() Part 1 of 2 failed");
+            fail("Caught unexpected exception = " + e);
+        }
+
+        // Part 2 - Create a Person entity that uses the invalid address above
+        OpenJPAEntityManager em = emf.createEntityManager();
+        assertNotNull(em);
+        try {
+            // verify Validation Mode
+            OpenJPAConfiguration conf = em.getConfiguration();
+            assertNotNull(conf);
+            assertTrue("ValidationMode",
+                conf.getValidationMode().equalsIgnoreCase("AUTO"));
+            // create invalid Person instance
+            em.getTransaction().begin();
+            // create a valid Person, minus the address
+            Person p = new Person();
+            p.setFirstName("Java");
+            p.setLastName("Joe");
+            // use invalid Address, which should cause CVEs due to @Valid
+            assertNotNull(a);
+            p.setHomeAddress(a);
+            // persist, which should cause a CVE
+            em.persist(p);
+            em.getTransaction().commit();
+            getLog().trace("testValidFailuresConstraint() Part 2 of 2 failed");
+            fail("Expected a ConstraintViolationException");
+        } catch (ConstraintViolationException e) {
+            // expected
+            getLog().trace("Caught expected ConstraintViolationException = " + e);
+            Set<ConstraintViolation<?>> cves = e.getConstraintViolations();
+            assertNotNull(cves);
+            for (ConstraintViolation<?> cv: cves) {
+                getLog().trace("CVE Contains ConstraintViolation = " + cv);
+            }
+            assertEquals("Wrong number of embedded ConstraintViolation failures",
+                5, cves.size());
+            getLog().trace("testValidFailuresConstraint() Part 2 of 2 passed");
+        } finally {
+            if ((em != null) && em.isOpen()) {
+                if (em.getTransaction().isActive())
+                    em.getTransaction().rollback();
+                em.close();
+            }
+        }            
+    }
+    
+    /**
+     * Scenario being tested:
+     *   29) Test @Pattern constraints pass in mode=AUTO
+     *       Basic constraint test for no violations.
+     */
+    public void testValidPassConstraint() {
+        getLog().trace("testValidPassConstraint() started");
+        // create EM from default EMF
+        OpenJPAEntityManager em = emf.createEntityManager();
+        assertNotNull(em);
+        try {
+            // provide a valid Address
+            em.getTransaction().begin();
+            Address a = new Address();
+            a.setStreetAddress("4205 South Miami Blvd.");
+            a.setCity("R.T.P.");
+            a.setState("NC");
+            a.setPostalCode("27709");
+            // persist, which should NOT cause a CVE
+            em.persist(a);
+            em.getTransaction().commit();
+
+            // create a valid Person
+            em.getTransaction().begin();
+            Person p = new Person();
+            p.setFirstName("Java");
+            p.setLastName("Joe");
+            p.setHomeAddress(a);
+            // persist, which should NOT cause a CVE
+            em.persist(p);
+            em.getTransaction().commit();
+            getLog().trace("testValidPassConstraint() passed");
+        } catch (Exception e) {
+            // unexpected
+            getLog().trace("testValidPassConstraint() failed");
+            fail("Caught unexpected exception = " + e);
+        } finally {
+            if ((em != null) && em.isOpen()) {
+                if (em.getTransaction().isActive())
+                    em.getTransaction().rollback();
+                em.close();
+            }
+        }
+    }
+
 
     /**
      * Helper method to remove entities and close the emf an any open em's.

Modified: openjpa/trunk/openjpa-integration/validation/src/test/resources/org/apache/openjpa/integration/validation/persistence.xml
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-integration/validation/src/test/resources/org/apache/openjpa/integration/validation/persistence.xml?rev=792248&r1=792247&r2=792248&view=diff
==============================================================================
--- openjpa/trunk/openjpa-integration/validation/src/test/resources/org/apache/openjpa/integration/validation/persistence.xml
(original)
+++ openjpa/trunk/openjpa-integration/validation/src/test/resources/org/apache/openjpa/integration/validation/persistence.xml
Wed Jul  8 18:22:16 2009
@@ -70,6 +70,16 @@
         <validation-mode>CALLBACK</validation-mode>
     </persistence-unit>
 
+    <persistence-unit name="address-none-mode">
+        <class>org.apache.openjpa.integration.validation.Address</class>
+        <validation-mode>NONE</validation-mode>
+    </persistence-unit>
+
+    <persistence-unit name="person-none-mode">
+        <class>org.apache.openjpa.integration.validation.Person</class>
+        <validation-mode>NONE</validation-mode>
+    </persistence-unit>
+
     <persistence-unit name="default-validation-group">
         <class>org.apache.openjpa.integration.validation.DefGrpEntity</class>
         <validation-mode>CALLBACK</validation-mode>



Mime
View raw message