OPENJPA-1106 Add tests for DecimalMin/Max constraints. Upgrade to agimatec 0.9.1-SNAPSHOT which has been updated to the 1.0.CR1 API level and now includes support for DecimalMin/Max constraints.

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@790722 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Donald Woods 2009-07-02 19:44:18 +00:00
parent 8730eddc29
commit 617e6ddff4
4 changed files with 284 additions and 16 deletions

View File

@ -67,7 +67,7 @@
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.Beta4</version>
<version>1.0.CR1</version>
<scope>test</scope>
</dependency>
-->
@ -79,13 +79,13 @@
<dependency>
<groupId>com.agimatec</groupId>
<artifactId>agimatec-jsr303</artifactId>
<version>0.9.0-SNAPSHOT</version>
<version>0.9.1-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.agimatec</groupId>
<artifactId>agimatec-validation</artifactId>
<version>0.9.0-SNAPSHOT</version>
<version>0.9.1-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>

View File

@ -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 {

View File

@ -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;
}
}

View File

@ -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();
}
}