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

View File

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

View File

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

View File

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

View File

@ -40,6 +40,26 @@
<validation-mode>NONE</validation-mode> <validation-mode>NONE</validation-mode>
</persistence-unit> </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"> <persistence-unit name="null-none-mode">
<class>org.apache.openjpa.integration.validation.ConstraintNull</class> <class>org.apache.openjpa.integration.validation.ConstraintNull</class>
<validation-mode>NONE</validation-mode> <validation-mode>NONE</validation-mode>