From 10bd4e70de8de17ec165e1103fce62b6ae8c33ea Mon Sep 17 00:00:00 2001 From: Donald Woods Date: Mon, 6 Jul 2009 18:08:18 +0000 Subject: [PATCH] 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 --- .../validation/ConstraintBoolean.java | 7 +-- .../validation/ConstraintDecimal.java | 23 ++++----- .../validation/ConstraintNull.java | 4 +- .../validation/TestConstraints.java | 51 ++++++++++--------- .../integration/validation/persistence.xml | 20 ++++++++ 5 files changed, 63 insertions(+), 42 deletions(-) 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 4869607e8..94832326f 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 @@ -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; } 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 index 44bb607dc..abe0b5905 100644 --- 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 @@ -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; } } diff --git a/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/ConstraintNull.java b/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/ConstraintNull.java index 67953b7f0..3e808fb09 100644 --- a/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/ConstraintNull.java +++ b/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/ConstraintNull.java @@ -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; } 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 985e02f98..b604a69f9 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 @@ -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 diff --git a/openjpa-integration/validation/src/test/resources/org/apache/openjpa/integration/validation/persistence.xml b/openjpa-integration/validation/src/test/resources/org/apache/openjpa/integration/validation/persistence.xml index e85dac648..dc82014b9 100644 --- a/openjpa-integration/validation/src/test/resources/org/apache/openjpa/integration/validation/persistence.xml +++ b/openjpa-integration/validation/src/test/resources/org/apache/openjpa/integration/validation/persistence.xml @@ -40,6 +40,26 @@ NONE + + org.apache.openjpa.integration.validation.ConstraintBoolean + NONE + + + + org.apache.openjpa.integration.validation.ConstraintBoolean + CALLBACK + + + + org.apache.openjpa.integration.validation.ConstraintDecimal + NONE + + + + org.apache.openjpa.integration.validation.ConstraintDecimal + CALLBACK + + org.apache.openjpa.integration.validation.ConstraintNull NONE