diff --git a/openjpa-integration/validation/pom.xml b/openjpa-integration/validation/pom.xml index fa82ad3df..0979a35f8 100644 --- a/openjpa-integration/validation/pom.xml +++ b/openjpa-integration/validation/pom.xml @@ -67,7 +67,7 @@ javax.validation validation-api - 1.0.Beta4 + 1.0.CR1 test --> @@ -79,13 +79,13 @@ com.agimatec agimatec-jsr303 - 0.9.0-SNAPSHOT + 0.9.1-SNAPSHOT test com.agimatec agimatec-validation - 0.9.0-SNAPSHOT + 0.9.1-SNAPSHOT test diff --git a/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/ConstraintBoolean.java b/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/ConstraintBoolean.java index 5561ce1ad..4869607e8 100644 --- a/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/ConstraintBoolean.java +++ b/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/ConstraintBoolean.java @@ -23,11 +23,19 @@ 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.AssertFalse; import javax.validation.constraints.AssertTrue; +@NamedQueries( { + @NamedQuery(name="FindFirst", + query="select c from VBOOLEAN c where c.id = 1"), + @NamedQuery(name="FindAll", query="select c from VBOOLEAN c") +}) + @Entity(name = "VBOOLEAN") @Table(name = "BOOLEAN_ENTITY") public class ConstraintBoolean implements Serializable { diff --git a/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/ConstraintDecimal.java b/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/ConstraintDecimal.java new file mode 100644 index 000000000..44bb607dc --- /dev/null +++ b/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/ConstraintDecimal.java @@ -0,0 +1,129 @@ +/* + * 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.math.BigDecimal; + +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.DecimalMax; +import javax.validation.constraints.DecimalMin; + + +@NamedQueries( { + @NamedQuery(name="FindFirst", + query="select c from VDECIMAL c where c.id = 1"), + @NamedQuery(name="FindAll", query="select c from VDECIMAL c") +}) + +@Entity(name = "VDECIMAL") +@Table(name = "DECIMAL_ENTITY") +public class ConstraintDecimal implements Serializable { + + @Transient + private static final long serialVersionUID = 1L; + + @Transient + //private static final BigDecimal negative = new BigDecimal(-99); + private static final long negative = -99; + + @Transient + //private static final BigDecimal positive = new BigDecimal(99); + private static final long positive = 99; + + @Id + @GeneratedValue + private long id; + + @Basic + @DecimalMin(value = "0") + //private BigDecimal minZero; + private long minZero; + + @Basic + @DecimalMax(value = "0") + //private BigDecimal maxZero; + private long maxZero; + + + /* + * Some helper methods to create the entities to test with + */ + public static ConstraintDecimal createInvalidMin() { + ConstraintDecimal c = new ConstraintDecimal(); + c.setMinZero(negative); + c.setMaxZero(negative); + return c; + } + + public static ConstraintDecimal createInvalidMax() { + ConstraintDecimal c = new ConstraintDecimal(); + c.setMinZero(positive); + c.setMaxZero(positive); + return c; + } + + public static ConstraintDecimal createInvalidMinMax() { + ConstraintDecimal c = new ConstraintDecimal(); + c.setMinZero(negative); + c.setMaxZero(positive); + return c; + } + + public static ConstraintDecimal createValid() { + ConstraintDecimal c = new ConstraintDecimal(); + c.setMinZero(positive); + c.setMaxZero(negative); + return c; + } + + + /* + * Main entity code + */ + public ConstraintDecimal() { + } + + public long getId() { + return id; + } + + public long getMinZero() { + return minZero; + } + + public void setMinZero(long d) { + minZero = d; + } + + public long getMaxZero() { + return maxZero; + } + + public void setMaxZero(long d) { + maxZero = d; + } +} diff --git a/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestConstraints.java b/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestConstraints.java index 7bc0f55f1..985e02f98 100644 --- a/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestConstraints.java +++ b/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestConstraints.java @@ -29,22 +29,25 @@ import org.apache.openjpa.persistence.test.SingleEMFTestCase; * focusing on the following Validation scenarios: * * Check special update/delete/ignore cases once: - * 1) Update @Null constraint exception on variables in mode=AUTO - * Tests that a constraint violation will occur on invalid update. - * 2) No invalid Delete @Null constraint exception when mode=AUTO - * Tests that a violation will not occur when deleting invalid entity. - * 3) No invalid Persist constraint exception when mode=NONE - * Tests that no Validation Providers are used when disabled. + * 1) Update @Null constraint exception on variables in mode=AUTO + * Tests that a constraint violation will occur on invalid update. + * 2) No invalid Delete @Null constraint exception when mode=AUTO + * Tests that a violation will not occur when deleting invalid entity. + * 3) No invalid Persist constraint exception when mode=NONE + * Tests that no Validation Providers are used when disabled. * * Basic constraint tests for violation exceptions: - * 4) Persist @Null constraint exception on variables in mode=AUTO - * 5) Persist @NotNull constraint exception on variables in mode=AUTO - * 7) Test @AssertTrue constraint exception on variables in mode=AUTO - * 8) Test @AssertFalse constraint exception on variables in mode=AUTO + * 4) Persist @Null constraint exception on variables in mode=AUTO + * 5) Persist @NotNull constraint exception on variables in mode=AUTO + * 7) Test @AssertTrue constraint exception on variables in mode=AUTO + * 8) Test @AssertFalse constraint exception on variables in mode=AUTO + * 10) Test @DecimalMin constraint exception on variables in mode=AUTO + * 11) Test @DecimalMax constraint exception on variables in mode=AUTO * * Basic constraint test for no violations: - * 6) Persist @NotNull and @Null constraints pass in mode=AUTO - * 9) Test @AssertFalse and @AssertTrue constraints pass in mode=AUTO + * 6) Persist @NotNull and @Null constraints pass in mode=AUTO + * 9) Test @AssertFalse and @AssertTrue constraints pass in mode=AUTO + * 12) Test @DecimalMin and @DecimalMax constraints pass in mode=AUTO * * @version $Rev$ $Date$ */ @@ -53,7 +56,8 @@ public class TestConstraints extends SingleEMFTestCase { @Override public void setUp() { super.setUp(CLEAR_TABLES, - ConstraintNull.class, ConstraintBoolean.class); + ConstraintNull.class, ConstraintBoolean.class, + ConstraintDecimal.class); } /** @@ -86,6 +90,8 @@ public class TestConstraints extends SingleEMFTestCase { fail("Caught unexpected exception = " + e); } finally { if ((em != null) && em.isOpen()) { + if (em.getTransaction().isActive()) + em.getTransaction().rollback(); em.close(); } } @@ -152,6 +158,8 @@ public class TestConstraints extends SingleEMFTestCase { fail("Unexpected Validation exception = " + e); } finally { if ((em != null) && em.isOpen()) { + if (em.getTransaction().isActive()) + em.getTransaction().rollback(); em.close(); } if ((emf != null) && emf.isOpen()) { @@ -230,6 +238,8 @@ public class TestConstraints extends SingleEMFTestCase { fail("Unexpected Validation exception = " + e); } finally { if ((em != null) && em.isOpen()) { + if (em.getTransaction().isActive()) + em.getTransaction().rollback(); em.close(); } if ((emf != null) && emf.isOpen()) { @@ -267,6 +277,8 @@ public class TestConstraints extends SingleEMFTestCase { getLog().trace("testNullConstraint() passed"); } finally { if ((em != null) && em.isOpen()) { + if (em.getTransaction().isActive()) + em.getTransaction().rollback(); em.close(); } } @@ -301,6 +313,8 @@ public class TestConstraints extends SingleEMFTestCase { getLog().trace("testNotNullConstraint() passed"); } finally { if ((em != null) && em.isOpen()) { + if (em.getTransaction().isActive()) + em.getTransaction().rollback(); em.close(); } } @@ -330,9 +344,12 @@ public class TestConstraints extends SingleEMFTestCase { getLog().trace("testNullNotNullConstraint() passed"); } catch (Exception e) { // unexpected + getLog().trace("testNullNotNullConstraint() failed"); fail("Caught unexpected exception = " + e); } finally { if ((em != null) && em.isOpen()) { + if (em.getTransaction().isActive()) + em.getTransaction().rollback(); em.close(); } } @@ -367,6 +384,8 @@ public class TestConstraints extends SingleEMFTestCase { getLog().trace("testAssertTrueConstraint() passed"); } finally { if ((em != null) && em.isOpen()) { + if (em.getTransaction().isActive()) + em.getTransaction().rollback(); em.close(); } } @@ -401,6 +420,8 @@ public class TestConstraints extends SingleEMFTestCase { getLog().trace("testAssertFalseConstraint() passed"); } finally { if ((em != null) && em.isOpen()) { + if (em.getTransaction().isActive()) + em.getTransaction().rollback(); em.close(); } } @@ -430,9 +451,119 @@ public class TestConstraints extends SingleEMFTestCase { getLog().trace("testAssertTrueFalseConstraint() passed"); } catch (Exception e) { // unexpected + getLog().trace("testAssertTrueFalseConstraint() failed"); fail("Caught unexpected exception = " + e); } finally { if ((em != null) && em.isOpen()) { + if (em.getTransaction().isActive()) + em.getTransaction().rollback(); + em.close(); + } + } + } + + /** + * Scenario being tested: + * 10) Test @DecimalMin constraint exception on variables in mode=AUTO + * Basic constraint test for a violation exception. + */ + public void testDecimalMinConstraint() { + getLog().trace("testDecimalMinConstraint() 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(); + ConstraintDecimal c = ConstraintDecimal.createInvalidMin(); + em.persist(c); + em.getTransaction().commit(); + getLog().trace("testDecimalMinConstraint() failed"); + fail("Expected a Validation exception"); + } catch (Exception e) { + // expected + getLog().trace("Caught expected exception = " + e); + getLog().trace("testDecimalMinConstraint() passed"); + } finally { + if ((em != null) && em.isOpen()) { + if (em.getTransaction().isActive()) + em.getTransaction().rollback(); + em.close(); + } + } + } + + /** + * Scenario being tested: + * 11) Test @DecimalMax constraint exception on variables in mode=AUTO + * Basic constraint test for a violation exception. + */ + public void testDecimalMaxConstraint() { + getLog().trace("testDecimalMaxConstraint() 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(); + ConstraintDecimal c = ConstraintDecimal.createInvalidMax(); + em.persist(c); + em.getTransaction().commit(); + getLog().trace("testDecimalMaxConstraint() failed"); + fail("Expected a Validation exception"); + } catch (Exception e) { + // expected + getLog().trace("Caught expected exception = " + e); + getLog().trace("testDecimalMaxConstraint() passed"); + } finally { + if ((em != null) && em.isOpen()) { + if (em.getTransaction().isActive()) + em.getTransaction().rollback(); + em.close(); + } + } + } + + /** + * Scenario being tested: + * 12) Test @DecimalMin and @DecimalMax constraints pass in mode=AUTO + * Basic constraint test for no violations. + */ + public void testDecimalMinMaxConstraint() { + getLog().trace("testDecimalMinMaxConstraint() 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(); + ConstraintDecimal c = ConstraintDecimal.createValid(); + em.persist(c); + em.getTransaction().commit(); + getLog().trace("testDecimalMinMaxConstraint() passed"); + } catch (Exception e) { + // unexpected + getLog().trace("testDecimalMinMaxConstraint() failed"); + fail("Caught unexpected exception = " + e); + } finally { + if ((em != null) && em.isOpen()) { + if (em.getTransaction().isActive()) + em.getTransaction().rollback(); em.close(); } }