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/06/29 21:45:27 UTC

svn commit: r789421 - in /openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation: ConstraintBoolean.java TestConstraints.java

Author: dwoods
Date: Mon Jun 29 19:45:27 2009
New Revision: 789421

URL: http://svn.apache.org/viewvc?rev=789421&view=rev
Log:
OPENJPA-1106 Integration tests for Bean Validation providers.  Tests for @AssertTrue and @AssertFalse.

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

Added: openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/ConstraintBoolean.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/ConstraintBoolean.java?rev=789421&view=auto
==============================================================================
--- openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/ConstraintBoolean.java (added)
+++ openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/ConstraintBoolean.java Mon Jun 29 19:45:27 2009
@@ -0,0 +1,102 @@
+/*
+ * 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 javax.persistence.Basic;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.Table;
+import javax.persistence.Transient;
+import javax.validation.constraints.AssertFalse;
+import javax.validation.constraints.AssertTrue;
+
+@Entity(name = "VBOOLEAN")
+@Table(name = "BOOLEAN_ENTITY")
+public class ConstraintBoolean implements Serializable {
+
+    @Transient
+    private static final long serialVersionUID = 1L;
+
+    @Id
+    @GeneratedValue
+    private long id;
+
+    @Basic
+    @AssertTrue
+    private Boolean trueRequired;
+
+    @Basic
+    @AssertFalse
+    private Boolean falseRequired;
+
+    
+    /* 
+     * Some helper methods to create the entities to test with
+     */
+    public static ConstraintBoolean createInvalidTrue() {
+        ConstraintBoolean c = new ConstraintBoolean();
+        c.setTrueRequired(Boolean.FALSE);
+        c.setFalseRequired(Boolean.FALSE);
+        return c;
+    }
+    
+    public static ConstraintBoolean createInvalidFalse() {
+        ConstraintBoolean c = new ConstraintBoolean();
+        c.setTrueRequired(Boolean.TRUE);
+        c.setFalseRequired(Boolean.TRUE);
+        return c;
+    }
+
+    public static ConstraintBoolean createValid() {
+        ConstraintBoolean c = new ConstraintBoolean();
+        c.setTrueRequired(Boolean.TRUE);
+        c.setFalseRequired(Boolean.FALSE);
+        return c;
+    }
+
+    
+    /*
+     * Main entity code
+     */
+    public ConstraintBoolean() {
+    }
+
+    public long getId() {
+        return id;
+    }
+
+    public Boolean getTrueRequired() {
+        return trueRequired;
+    }
+
+    public void setTrueRequired(Boolean b) {
+        trueRequired = b;
+    }
+
+    public Boolean getFalseRequired() {
+        return falseRequired;
+    }
+
+    public void setFalseRequired(Boolean b) {
+        falseRequired = b;
+    }
+
+}

Propchange: openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/ConstraintBoolean.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=789421&r1=789420&r2=789421&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 Jun 29 19:45:27 2009
@@ -28,7 +28,11 @@
  * focusing on the following Validation scenarios:
  *   1) Test @Null constraint exception on variables in mode=AUTO
  *   2) Test @NotNull constraint exception on variables in mode=AUTO
- *   3) Test no constraint exception when mode=NONE
+ *   3) Test @NotNull and @Null constraints pass in mode=AUTO
+ *   4) Test no constraint exception when mode=NONE
+ *   5) Test @AssertTrue constraint exception on variables in mode=AUTO
+ *   6) Test @AssertFalse constraint exception on variables in mode=AUTO
+ *   7) Test @AssertFalse and @AssertTrue constraints pass in mode=AUTO
  *    
  * @version $Rev$ $Date$
  */
@@ -36,7 +40,8 @@
 
     @Override
     public void setUp() {
-        super.setUp(CLEAR_TABLES, ConstraintNull.class);
+        super.setUp(CLEAR_TABLES,
+            ConstraintNull.class, ConstraintBoolean.class);
     }
 
     /**
@@ -107,7 +112,38 @@
 
     /**
      * Scenario being tested:
-     *   3) Test no constraint exception when mode=NONE
+     *   3) Test @NotNull and @Null constraints pass in mode=AUTO
+     */
+    public void testNullNotNullConstraint() {
+        getLog().trace("testNullNotNullConstraint() 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 ConstraintNull instance
+            em.getTransaction().begin();
+            ConstraintNull c = ConstraintNull.createValid();
+            em.persist(c);
+            em.getTransaction().commit();
+            getLog().trace("testNullNotNullConstraint() passed");
+        } catch (Exception e) {
+            // unexpected
+            fail("Caught unexpected exception = " + e);
+        } finally {
+            if ((em != null) && em.isOpen()) {
+                em.close();
+            }
+        }
+    }
+
+    /**
+     * Scenario being tested:
+     *   4) Test no constraint exception when mode=NONE
      */
     public void testNullConstraintIgnored() {
         getLog().trace("testNullConstraintIgnored() started");
@@ -143,7 +179,104 @@
         }
     }
 
-    
+    /**
+     * Scenario being tested:
+     *   5) Test @AssertTrue constraint exception on variables in mode=AUTO
+     */
+    public void testAssertTrueConstraint() {
+        getLog().trace("testAssertTrueConstraint() 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 ConstraintNull instance
+            em.getTransaction().begin();
+            ConstraintBoolean c = ConstraintBoolean.createInvalidTrue();
+            em.persist(c);
+            em.getTransaction().commit();
+            getLog().trace("testAssertTrueConstraint() failed");
+            fail("Expected a Validation exception");
+        } catch (Exception e) {
+            // expected
+            getLog().trace("Caught expected exception = " + e);
+            getLog().trace("testAssertTrueConstraint() passed");
+        } finally {
+            if ((em != null) && em.isOpen()) {
+                em.close();
+            }
+        }
+    }
+
+    /**
+     * Scenario being tested:
+     *   6) Test @AssertFalse constraint exception on variables in mode=AUTO
+     */
+    public void testAssertFalseConstraint() {
+        getLog().trace("testAssertFalseConstraint() 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 ConstraintNull instance
+            em.getTransaction().begin();
+            ConstraintBoolean c = ConstraintBoolean.createInvalidFalse();
+            em.persist(c);
+            em.getTransaction().commit();
+            getLog().trace("testAssertFalseConstraint() failed");
+            fail("Expected a Validation exception");
+        } catch (Exception e) {
+            // expected
+            getLog().trace("Caught expected exception = " + e);
+            getLog().trace("testAssertFalseConstraint() passed");
+        } finally {
+            if ((em != null) && em.isOpen()) {
+                em.close();
+            }
+        }
+    }
+
+    /**
+     * Scenario being tested:
+     *   7) Test @AssertFalse and @AssertTrue constraints pass in mode=AUTO
+     */
+    public void testAssertTrueFalseConstraint() {
+        getLog().trace("testAssertTrueFalseConstraint() 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 ConstraintNull instance
+            em.getTransaction().begin();
+            ConstraintBoolean c = ConstraintBoolean.createValid();
+            em.persist(c);
+            em.getTransaction().commit();
+            getLog().trace("testAssertTrueFalseConstraint() passed");
+        } catch (Exception e) {
+            // unexpected
+            fail("Caught unexpected exception = " + e);
+        } finally {
+            if ((em != null) && em.isOpen()) {
+                em.close();
+            }
+        }
+    }
+
+
     /**
      * Internal convenience method for getting the OpenJPA logger
      *