OPENJPA-1157 Integration tests for Bean Validation providers - Part 2. Convert some of the existing attribute constraints to be method/getter constraints.

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@791554 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Donald Woods 2009-07-06 18:08:18 +00:00
parent 4c9e036e8a
commit 10bd4e70de
5 changed files with 63 additions and 42 deletions

View File

@ -27,6 +27,7 @@ import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.Valid;
import javax.validation.constraints.AssertFalse;
import javax.validation.constraints.AssertTrue;
@ -52,8 +53,7 @@ public class ConstraintBoolean implements Serializable {
private Boolean trueRequired;
@Basic
@AssertFalse
private Boolean falseRequired;
private Boolean falseRequired; // @AssertFalse constraint is on the getter
/*
@ -98,7 +98,8 @@ public class ConstraintBoolean implements Serializable {
public void setTrueRequired(Boolean b) {
trueRequired = b;
}
@AssertFalse
public Boolean getFalseRequired() {
return falseRequired;
}

View File

@ -19,6 +19,7 @@
package org.apache.openjpa.integration.validation;
import java.io.Serializable;
import java.math.BigDecimal;
//import java.math.BigDecimal;
import javax.persistence.Basic;
@ -47,12 +48,10 @@ public class ConstraintDecimal implements Serializable {
private static final long serialVersionUID = 1L;
@Transient
//private static final BigDecimal negative = new BigDecimal(-99);
private static final long negative = -99;
private static final BigDecimal negative = new BigDecimal(-99.99);
@Transient
//private static final BigDecimal positive = new BigDecimal(99);
private static final long positive = 99;
private static final BigDecimal positive = new BigDecimal(99.99);
@Id
@GeneratedValue
@ -60,13 +59,10 @@ public class ConstraintDecimal implements Serializable {
@Basic
@DecimalMin(value = "0")
//private BigDecimal minZero;
private long minZero;
private BigDecimal minZero;
@Basic
@DecimalMax(value = "0")
//private BigDecimal maxZero;
private long maxZero;
private BigDecimal maxZero; // @DecimalMax(value = "0") constraint is on the getter
/*
@ -111,19 +107,20 @@ public class ConstraintDecimal implements Serializable {
return id;
}
public long getMinZero() {
public BigDecimal getMinZero() {
return minZero;
}
public void setMinZero(long d) {
public void setMinZero(BigDecimal d) {
minZero = d;
}
public long getMaxZero() {
@DecimalMax(value = "0")
public BigDecimal getMaxZero() {
return maxZero;
}
public void setMaxZero(long d) {
public void setMaxZero(BigDecimal d) {
maxZero = d;
}
}

View File

@ -53,8 +53,7 @@ public class ConstraintNull implements Serializable {
private String nullRequired;
@Basic
@NotNull
private String nullInvalid;
private String nullInvalid; // @NotNull constraint is on the getter
/*
@ -98,6 +97,7 @@ public class ConstraintNull implements Serializable {
nullRequired = s;
}
@NotNull
public String getNullInvalid() {
return nullInvalid;
}

View File

@ -13,6 +13,8 @@
*/
package org.apache.openjpa.integration.validation;
import java.util.List;
import javax.persistence.Query;
import javax.persistence.ValidationMode;
import javax.validation.ConstraintViolationException;
@ -38,11 +40,11 @@ import org.apache.openjpa.persistence.test.SingleEMFTestCase;
*
* 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
* 5) Persist @NotNull constraint exception on getter in mode=AUTO
* 7) Test @AssertTrue constraint exception on variables in mode=AUTO
* 8) Test @AssertFalse constraint exception on variables in mode=AUTO
* 8) Test @AssertFalse constraint exception on getter in mode=AUTO
* 10) Test @DecimalMin constraint exception on variables in mode=AUTO
* 11) Test @DecimalMax constraint exception on variables in mode=AUTO
* 11) Test @DecimalMax constraint exception on getter in mode=AUTO
*
* Basic constraint test for no violations:
* 6) Persist @NotNull and @Null constraints pass in mode=AUTO
@ -108,10 +110,10 @@ public class TestConstraints extends SingleEMFTestCase {
em.flush();
em.getTransaction().commit();
getLog().trace("testNullUpdateConstraint() Part 2 of 2 failed");
fail("Expected a Validation exception");
fail("Expected a ConstraintViolationException");
} catch (ConstraintViolationException e) {
// expected
getLog().trace("Caught expected exception = " + e);
getLog().trace("Caught expected ConstraintViolationException = " + e);
getLog().trace("testNullUpdateConstraint() Part 2 of 2 passed");
} finally {
if ((em != null) && em.isOpen()) {
@ -155,7 +157,7 @@ public class TestConstraints extends SingleEMFTestCase {
} catch (Exception e) {
// unexpected
getLog().trace("testNullDeleteIgnored() Part 1 of 2 failed");
fail("Unexpected Validation exception = " + e);
fail("Caught unexpected exception = " + e);
} finally {
if ((em != null) && em.isOpen()) {
if (em.getTransaction().isActive())
@ -191,7 +193,7 @@ public class TestConstraints extends SingleEMFTestCase {
} catch (Exception e) {
// unexpected
getLog().trace("testNullDeleteIgnored() Part 2 of 2 failed");
fail("Unexpected Validation exception = " + e);
fail("Caught unexpected exception = " + e);
} finally {
if ((em != null) && em.isOpen()) {
if (em.getTransaction().isActive())
@ -235,7 +237,7 @@ public class TestConstraints extends SingleEMFTestCase {
} catch (Exception e) {
// unexpected
getLog().trace("testNullConstraintIgnored() failed");
fail("Unexpected Validation exception = " + e);
fail("Caught unexpected exception = " + e);
} finally {
if ((em != null) && em.isOpen()) {
if (em.getTransaction().isActive())
@ -270,10 +272,10 @@ public class TestConstraints extends SingleEMFTestCase {
em.persist(c);
em.getTransaction().commit();
getLog().trace("testNullConstraint() failed");
fail("Expected a Validation exception");
fail("Expected a ConstraintViolationException");
} catch (ConstraintViolationException e) {
// expected
getLog().trace("Caught expected exception = " + e);
getLog().trace("Caught expected ConstraintViolationException = " + e);
getLog().trace("testNullConstraint() passed");
} finally {
if ((em != null) && em.isOpen()) {
@ -286,7 +288,7 @@ public class TestConstraints extends SingleEMFTestCase {
/**
* Scenario being tested:
* 5) Test @NotNull constraint exception on variables in mode=AUTO
* 5) Test @NotNull constraint exception on getter in mode=AUTO
* Basic constraint test for a violation exception.
*/
public void testNotNullConstraint() {
@ -306,10 +308,10 @@ public class TestConstraints extends SingleEMFTestCase {
em.persist(c);
em.getTransaction().commit();
getLog().trace("testNotNullConstraint() failed");
fail("Expected a Validation exception");
fail("Expected a ConstraintViolationException");
} catch (ConstraintViolationException e) {
// expected
getLog().trace("Caught expected exception = " + e);
getLog().trace("Caught expected ConstraintViolationException = " + e);
getLog().trace("testNotNullConstraint() passed");
} finally {
if ((em != null) && em.isOpen()) {
@ -377,10 +379,10 @@ public class TestConstraints extends SingleEMFTestCase {
em.persist(c);
em.getTransaction().commit();
getLog().trace("testAssertTrueConstraint() failed");
fail("Expected a Validation exception");
fail("Expected a ConstraintViolationException");
} catch (ConstraintViolationException e) {
// expected
getLog().trace("Caught expected exception = " + e);
getLog().trace("Caught expected ConstraintViolationException = " + e);
getLog().trace("testAssertTrueConstraint() passed");
} finally {
if ((em != null) && em.isOpen()) {
@ -393,7 +395,7 @@ public class TestConstraints extends SingleEMFTestCase {
/**
* Scenario being tested:
* 8) Test @AssertFalse constraint exception on variables in mode=AUTO
* 8) Test @AssertFalse constraint exception on getter in mode=AUTO
* Basic constraint test for a violation exception.
*/
public void testAssertFalseConstraint() {
@ -413,10 +415,10 @@ public class TestConstraints extends SingleEMFTestCase {
em.persist(c);
em.getTransaction().commit();
getLog().trace("testAssertFalseConstraint() failed");
fail("Expected a Validation exception");
fail("Expected a ConstraintViolationException");
} catch (ConstraintViolationException e) {
// expected
getLog().trace("Caught expected exception = " + e);
getLog().trace("Caught expected ConstraintViolationException = " + e);
getLog().trace("testAssertFalseConstraint() passed");
} finally {
if ((em != null) && em.isOpen()) {
@ -484,10 +486,10 @@ public class TestConstraints extends SingleEMFTestCase {
em.persist(c);
em.getTransaction().commit();
getLog().trace("testDecimalMinConstraint() failed");
fail("Expected a Validation exception");
} catch (Exception e) {
fail("Expected a ConstraintViolationException");
} catch (ConstraintViolationException e) {
// expected
getLog().trace("Caught expected exception = " + e);
getLog().trace("Caught expected ConstraintViolationException = " + e);
getLog().trace("testDecimalMinConstraint() passed");
} finally {
if ((em != null) && em.isOpen()) {
@ -500,7 +502,7 @@ public class TestConstraints extends SingleEMFTestCase {
/**
* Scenario being tested:
* 11) Test @DecimalMax constraint exception on variables in mode=AUTO
* 11) Test @DecimalMax constraint exception on getter in mode=AUTO
* Basic constraint test for a violation exception.
*/
public void testDecimalMaxConstraint() {
@ -520,10 +522,10 @@ public class TestConstraints extends SingleEMFTestCase {
em.persist(c);
em.getTransaction().commit();
getLog().trace("testDecimalMaxConstraint() failed");
fail("Expected a Validation exception");
fail("Expected a ConstraintViolationException");
} catch (Exception e) {
// expected
getLog().trace("Caught expected exception = " + e);
getLog().trace("Caught expected ConstraintViolationException = " + e);
getLog().trace("testDecimalMaxConstraint() passed");
} finally {
if ((em != null) && em.isOpen()) {
@ -569,6 +571,7 @@ public class TestConstraints extends SingleEMFTestCase {
}
}
/**
* Internal convenience method for getting the OpenJPA logger

View File

@ -40,6 +40,26 @@
<validation-mode>NONE</validation-mode>
</persistence-unit>
<persistence-unit name="boolean-none-mode">
<class>org.apache.openjpa.integration.validation.ConstraintBoolean</class>
<validation-mode>NONE</validation-mode>
</persistence-unit>
<persistence-unit name="boolean-callback-mode">
<class>org.apache.openjpa.integration.validation.ConstraintBoolean</class>
<validation-mode>CALLBACK</validation-mode>
</persistence-unit>
<persistence-unit name="decimal-none-mode">
<class>org.apache.openjpa.integration.validation.ConstraintDecimal</class>
<validation-mode>NONE</validation-mode>
</persistence-unit>
<persistence-unit name="decimal-callback-mode">
<class>org.apache.openjpa.integration.validation.ConstraintDecimal</class>
<validation-mode>CALLBACK</validation-mode>
</persistence-unit>
<persistence-unit name="null-none-mode">
<class>org.apache.openjpa.integration.validation.ConstraintNull</class>
<validation-mode>NONE</validation-mode>