You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by dw...@apache.org on 2009/07/06 23:37:18 UTC

svn commit: r791621 - in /openjpa/trunk/openjpa-integration/validation: pom.xml src/test/java/org/apache/openjpa/integration/validation/ConstraintSize.java src/test/java/org/apache/openjpa/integration/validation/TestConstraints.java

Author: dwoods
Date: Mon Jul  6 21:37:18 2009
New Revision: 791621

URL: http://svn.apache.org/viewvc?rev=791621&view=rev
Log:
OPENJPA-1157 Integration tests for Bean Validation providers - Part 2.  Added @Size constraint tests.

Added:
    openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/ConstraintSize.java   (with props)
Modified:
    openjpa/trunk/openjpa-integration/validation/pom.xml
    openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestConstraints.java

Modified: openjpa/trunk/openjpa-integration/validation/pom.xml
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-integration/validation/pom.xml?rev=791621&r1=791620&r2=791621&view=diff
==============================================================================
--- openjpa/trunk/openjpa-integration/validation/pom.xml (original)
+++ openjpa/trunk/openjpa-integration/validation/pom.xml Mon Jul  6 21:37:18 2009
@@ -115,7 +115,7 @@
             </repositories>
         </profile>
 
-        <!-- Default profile for testing with Derby and Bean Validation RI -->
+        <!-- Profile for testing with Hibernate Bean Validation RI -->
         <profile>
             <id>hibernate</id>
             <activation>
@@ -128,14 +128,14 @@
                 <dependency>
                     <groupId>javax.validation</groupId>
                     <artifactId>validation-api</artifactId>
-                    <version>1.0.CR2</version>
+                    <version>1.0.CR1</version>
                     <!-- <version>1.0.CR3-SNAPSHOT</version> -->
                     <scope>test</scope>
                 </dependency>
                 <dependency>
                     <groupId>org.hibernate</groupId>
                     <artifactId>hibernate-validator</artifactId>
-                    <version>4.0.0.Beta1</version>
+                    <version>4.0.0.Alpha3</version>
                     <!-- <version>4.0.0.Beta2-SNAPSHOT</version> -->
                     <scope>test</scope>
                 </dependency>

Added: openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/ConstraintSize.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/ConstraintSize.java?rev=791621&view=auto
==============================================================================
--- openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/ConstraintSize.java (added)
+++ openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/ConstraintSize.java Mon Jul  6 21:37:18 2009
@@ -0,0 +1,134 @@
+/*
+ * 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.openjpa.integration.validation;
+
+import java.io.Serializable;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.persistence.Basic;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.NamedQueries;
+import javax.persistence.NamedQuery;
+import javax.persistence.Table;
+import javax.persistence.Transient;
+import javax.validation.constraints.Size;
+
+
+@NamedQueries( {
+    @NamedQuery(name="FindFirst",
+            query="select c from VSIZE c where c.id = 1"),
+    @NamedQuery(name="FindAll", query="select c from VSIZE c")
+})
+
+@Entity(name = "VSIZE")
+@Table(name = "SIZE_ENTITY")
+public class ConstraintSize implements Serializable {
+
+    @Transient
+    private static final long serialVersionUID = 1L;
+    
+    @Id
+    @GeneratedValue
+    private long id;
+
+    @Basic
+    @Size(min = 0, max = 10)
+    private String myString;
+
+    private Map<String,String> myMap;  // @Size(1,2) constraint is on the getter
+
+    
+    /* 
+     * Some helper methods to create the entities to test with
+     */
+    public static ConstraintSize createInvalidString() {
+        ConstraintSize c = new ConstraintSize();
+        c.setMyString("abcdefghijklmno");
+        c.setValidMap();
+        return c;
+    }
+
+    public static ConstraintSize createInvalidMap() {
+        ConstraintSize c = new ConstraintSize();
+        c.setMyString("");
+        c.setInvalidMap();
+        return c;
+    }
+
+    public static ConstraintSize createInvalidSize() {
+        ConstraintSize c = new ConstraintSize();
+        c.setMyString("abcdefghijklmno");
+        c.setInvalidMap();
+        return c;
+    }
+
+    public static ConstraintSize createValid() {
+        ConstraintSize c = new ConstraintSize();
+        c.setMyString("abc");
+        c.setValidMap();
+        return c;
+    }
+
+    
+    /*
+     * Main entity code
+     */
+    public ConstraintSize() {
+    }
+
+    public long getId() {
+        return id;
+    }
+
+    public String getMyString() {
+        return myString;
+    }
+
+    public void setMyString(String s) {
+        myString = s;
+    }
+
+    @Size(min = 1, max = 2)
+    public Map<String,String> getMyMap() {
+        return myMap;
+    }
+
+    public void setMyMap(Map<String,String> m) {
+        myMap = m;
+    }
+    
+    
+    private void setInvalidMap() {
+        Map<String,String> m = new HashMap<String,String>();
+        m.put("a", "a value");
+        m.put("b", "b value");
+        m.put("c", "c value");
+        setMyMap(m);
+    }
+    
+    private void setValidMap() {
+        Map<String,String> m = new HashMap<String,String>();
+        m.put("a", "a value");
+        setMyMap(m);
+    }
+    
+}

Propchange: openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/ConstraintSize.java
------------------------------------------------------------------------------
    svn:eol-style = native

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=791621&r1=791620&r2=791621&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 Mon Jul  6 21:37:18 2009
@@ -47,6 +47,8 @@
  *   14) Test @Max constraint exception on getter in mode=AUTO
  *   16) Test @Digits constraint exception on variables in mode=AUTO
  *   17) Test @Digits constraint exception on getter in mode=AUTO
+ *   19) Test @Size constraint exception on variables in mode=AUTO
+ *   20) Test @Size constraint exception on getter in mode=AUTO
  *   
  *   Basic constraint test for no violations:
  *   6)  Persist @NotNull and @Null constraints pass in mode=AUTO
@@ -54,6 +56,7 @@
  *   12) Test @DecimalMin and @DecimalMax constraints pass in mode=AUTO
  *   15) Test @Min and @Max constraints pass in mode=AUTO
  *   18) Test @Digits constraints pass in mode=AUTO
+ *   21) Test @Size constraints pass in mode=AUTO
  *
  * @version $Rev$ $Date$
  */
@@ -64,7 +67,7 @@
         super.setUp(CLEAR_TABLES,
             ConstraintNull.class, ConstraintBoolean.class,
             ConstraintDecimal.class, ConstraintNumber.class,
-            ConstraintDigits.class);
+            ConstraintDigits.class, ConstraintSize.class);
     }
 
     /**
@@ -790,6 +793,113 @@
         }
     }
 
+    /**
+     * Scenario being tested:
+     *   19) Test @Size constraint exception on variables in mode=AUTO
+     *       Basic constraint test for a violation exception.
+     */
+    public void testSizeStringConstraint() {
+        getLog().trace("testSizeStringConstraint() started");
+        // create EM from default EMF
+        OpenJPAEntityManager em = emf.createEntityManager();
+        assertNotNull(em);
+        try {
+            // verify Validation Mode
+            OpenJPAConfiguration conf = em.getConfiguration();
+            assertNotNull(conf);
+            assertTrue("ValidationMode",
+                conf.getValidationMode().equalsIgnoreCase("AUTO"));
+            // create invalid ConstraintBoolean instance
+            em.getTransaction().begin();
+            ConstraintSize c = ConstraintSize.createInvalidString();
+            em.persist(c);
+            em.getTransaction().commit();
+            getLog().trace("testSizeStringConstraint() failed");
+            fail("Expected a ConstraintViolationException");
+        } catch (ConstraintViolationException e) {
+            // expected
+            getLog().trace("Caught expected ConstraintViolationException = " + e);
+            getLog().trace("testSizeStringConstraint() passed");
+        } finally {
+            if ((em != null) && em.isOpen()) {
+                if (em.getTransaction().isActive())
+                    em.getTransaction().rollback();
+                em.close();
+            }
+        }
+    }
+
+    /**
+     * Scenario being tested:
+     *   20) Test @Size constraint exception on getter in mode=AUTO
+     *       Basic constraint test for a violation exception.
+     */
+    public void testSizeMapConstraint() {
+        getLog().trace("testSizeMapConstraint() started");
+        // create EM from default EMF
+        OpenJPAEntityManager em = emf.createEntityManager();
+        assertNotNull(em);
+        try {
+            // verify Validation Mode
+            OpenJPAConfiguration conf = em.getConfiguration();
+            assertNotNull(conf);
+            assertTrue("ValidationMode",
+                conf.getValidationMode().equalsIgnoreCase("AUTO"));
+            // create invalid ConstraintBoolean instance
+            em.getTransaction().begin();
+            ConstraintSize c = ConstraintSize.createInvalidMap();
+            em.persist(c);
+            em.getTransaction().commit();
+            getLog().trace("testSizeMapConstraint() failed");
+            fail("Expected a ConstraintViolationException");
+        } catch (Exception e) {
+            // expected
+            getLog().trace("Caught expected ConstraintViolationException = " + e);
+            getLog().trace("testSizeMapConstraint() passed");
+        } finally {
+            if ((em != null) && em.isOpen()) {
+                if (em.getTransaction().isActive())
+                    em.getTransaction().rollback();
+                em.close();
+            }
+        }
+    }
+
+    /**
+     * Scenario being tested:
+     *   21) Test @Size constraints pass in mode=AUTO
+     *       Basic constraint test for no violations.
+     */
+    public void testSizeConstraint() {
+        getLog().trace("testSizeConstraint() started");
+        // create EM from default EMF
+        OpenJPAEntityManager em = emf.createEntityManager();
+        assertNotNull(em);
+        try {
+            // verify Validation Mode
+            OpenJPAConfiguration conf = em.getConfiguration();
+            assertNotNull(conf);
+            assertTrue("ValidationMode",
+                conf.getValidationMode().equalsIgnoreCase("AUTO"));
+            // create valid ConstraintBoolean instance
+            em.getTransaction().begin();
+            ConstraintSize c = ConstraintSize.createValid();
+            em.persist(c);
+            em.getTransaction().commit();
+            getLog().trace("testSizeConstraint() passed");
+        } catch (Exception e) {
+            // unexpected
+            getLog().trace("testSizeConstraint() failed");
+            fail("Caught unexpected exception = " + e);
+        } finally {
+            if ((em != null) && em.isOpen()) {
+                if (em.getTransaction().isActive())
+                    em.getTransaction().rollback();
+                em.close();
+            }
+        }
+    }
+
         
     /**
      * Internal convenience method for getting the OpenJPA logger