OPENJPA-1106 Integration tests for Bean Validation providers. Add tests for special update/delete/ignore cases.

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@789450 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Donald Woods 2009-06-29 21:16:06 +00:00
parent 2fc4ba2c45
commit de7f5c1189
3 changed files with 253 additions and 87 deletions

View File

@ -23,11 +23,20 @@ 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.NotNull;
import javax.validation.constraints.Null;
@NamedQueries( {
@NamedQuery(name="FindFirst",
query="select c from VNULL c where c.id = 1"),
@NamedQuery(name="FindAll", query="select c from VNULL c")
})
@Entity(name = "VNULL")
@Table(name = "NULL_ENTITY")
public class ConstraintNull implements Serializable {

View File

@ -13,6 +13,7 @@
*/
package org.apache.openjpa.integration.validation;
import javax.persistence.Query;
import javax.persistence.ValidationMode;
import org.apache.openjpa.conf.OpenJPAConfiguration;
@ -20,19 +21,29 @@ import org.apache.openjpa.lib.log.Log;
import org.apache.openjpa.persistence.OpenJPAEntityManager;
import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
import org.apache.openjpa.persistence.OpenJPAPersistence;
import org.apache.openjpa.persistence.test.AllowFailure;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
/**
* Tests the new Bean Validation constraint support in the JPA 2.0 spec by
* focusing on the following Validation scenarios:
* 1) Test @Null constraint exception on variables in mode=AUTO
* 2) Test @NotNull constraint exception on variables in mode=AUTO
* 3) Test @NotNull and @Null constraints pass in mode=AUTO
* 4) Test no constraint exception when mode=NONE
* 5) Test @AssertTrue constraint exception on variables in mode=AUTO
* 6) Test @AssertFalse constraint exception on variables in mode=AUTO
* 7) Test @AssertFalse and @AssertTrue constraints pass in mode=AUTO
*
* 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.
*
* 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
*
* 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
*
* @version $Rev$ $Date$
*/
@ -46,43 +57,13 @@ public class TestConstraints extends SingleEMFTestCase {
/**
* Scenario being tested:
* 1) Test @Null constraint exception on variables in mode=AUTO
* 1) Update @Null constraint exception on variables in mode=AUTO
* Tests that a constraint violation will occur on invalid update.
*/
public void testNullConstraint() {
getLog().trace("testNullConstraint() 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 ConstraintNull instance
em.getTransaction().begin();
ConstraintNull c = ConstraintNull.createInvalidNull();
em.persist(c);
em.getTransaction().commit();
getLog().trace("testNullConstraint() failed");
fail("Expected a Validation exception");
} catch (Exception e) {
// expected
getLog().trace("Caught expected exception = " + e);
getLog().trace("testNullConstraint() passed");
} finally {
if ((em != null) && em.isOpen()) {
em.close();
}
}
}
public void testNullUpdateConstraint() {
getLog().trace("testNullUpdateConstraint() started");
/**
* Scenario being tested:
* 2) Test @NotNull constraint exception on variables in mode=AUTO
*/
public void testNotNullConstraint() {
getLog().trace("testNotNullConstraint() started");
// Part 1 - Create and persist a valid entity
// create EM from default EMF
OpenJPAEntityManager em = emf.createEntityManager();
assertNotNull(em);
@ -92,58 +73,126 @@ public class TestConstraints extends SingleEMFTestCase {
assertNotNull(conf);
assertTrue("ValidationMode",
conf.getValidationMode().equalsIgnoreCase("AUTO"));
// create invalid ConstraintNull instance
em.getTransaction().begin();
ConstraintNull c = ConstraintNull.createInvalidNotNull();
em.persist(c);
em.getTransaction().commit();
getLog().trace("testNotNullConstraint() failed");
fail("Expected a Validation exception");
} catch (Exception e) {
// expected
getLog().trace("Caught expected exception = " + e);
getLog().trace("testNotNullConstraint() passed");
} finally {
if ((em != null) && em.isOpen()) {
em.close();
}
}
}
/**
* Scenario being tested:
* 3) Test @NotNull and @Null constraints pass in mode=AUTO
*/
public void testNullNotNullConstraint() {
getLog().trace("testNullNotNullConstraint() 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 ConstraintNull instance
// create valid ConstraintNull instance
em.getTransaction().begin();
ConstraintNull c = ConstraintNull.createValid();
em.persist(c);
em.getTransaction().commit();
getLog().trace("testNullNotNullConstraint() passed");
getLog().trace("testNullUpdateConstraint() Part 1 of 2 passed");
} catch (Exception e) {
// unexpected
getLog().trace("testNullUpdateConstraint() Part 1 of 2 failed");
fail("Caught unexpected exception = " + e);
} finally {
if ((em != null) && em.isOpen()) {
em.close();
}
}
// Part 2 - Verify that invalid properties are caught on an update
// create EM from default EMF
em = emf.createEntityManager();
assertNotNull(em);
try {
// update entity to be invalid
ConstraintNull c = em.find(ConstraintNull.class, new Integer(1));
em.getTransaction().begin();
c.setNullRequired(new String("not null"));
em.flush();
em.getTransaction().commit();
getLog().trace("testNullUpdateConstraint() Part 2 of 2 failed");
fail("Expected a Validation exception");
} catch (Exception e) {
// expected
getLog().trace("Caught expected exception = " + e);
getLog().trace("testNullUpdateConstraint() Part 2 of 2 passed");
} finally {
if ((em != null) && em.isOpen()) {
if (em.getTransaction().isActive())
em.getTransaction().rollback();
em.close();
}
}
}
/**
* Scenario being tested:
* 4) Test no constraint exception when mode=NONE
* 2) No invalid Delete @Null constraint exception when mode=AUTO
* Tests that a violation will not occur when deleting invalid entity.
*/
public void testNullDeleteIgnored() {
getLog().trace("testNullDeleteIgnored() started");
// Part 1 - Create an invalid entity
// create our EMF w/ props
OpenJPAEntityManagerFactory emf = OpenJPAPersistence
.createEntityManagerFactory(
"null-none-mode",
"org/apache/openjpa/integration/validation/persistence.xml");
assertNotNull(emf);
// create EM
OpenJPAEntityManager em = emf.createEntityManager();
assertNotNull(em);
try {
// verify Validation Mode
OpenJPAConfiguration conf = em.getConfiguration();
assertNotNull(conf);
assertTrue("ValidationMode",
conf.getValidationMode().equalsIgnoreCase("NONE"));
// create invalid ConstraintNull instance
em.getTransaction().begin();
ConstraintNull c = ConstraintNull.createInvalidNull();
em.persist(c);
em.getTransaction().commit();
getLog().trace("testNullDeleteIgnored() Part 1 of 2 passed");
} catch (Exception e) {
// unexpected
getLog().trace("testNullDeleteIgnored() Part 1 of 2 failed");
fail("Unexpected Validation exception = " + e);
} finally {
if ((em != null) && em.isOpen()) {
em.close();
}
}
// Part 2 - Verify delete using default group does not cause Validation
// create our EMF w/ validation mode=CALLBACK
emf = OpenJPAPersistence.createEntityManagerFactory(
"null-callback-mode",
"org/apache/openjpa/integration/validation/persistence.xml");
assertNotNull(emf);
// create EM
em = emf.createEntityManager();
assertNotNull(em);
try {
// verify Validation Mode
OpenJPAConfiguration conf = em.getConfiguration();
assertNotNull(conf);
assertTrue("ValidationMode",
conf.getValidationMode().equalsIgnoreCase("CALLBACK"));
// get the invalid entity to delete
Query q = em.createQuery("DELETE FROM VNULL c WHERE c.id = 1");
em.getTransaction().begin();
int count = q.executeUpdate();
em.getTransaction().commit();
getLog().trace("testNullDeleteIgnored() Part 2 of 2 passed");
} catch (Exception e) {
// unexpected
getLog().trace("testNullDeleteIgnored() Part 2 of 2 failed");
fail("Unexpected Validation exception = " + e);
} finally {
if ((em != null) && em.isOpen()) {
if (em.getTransaction().isActive())
em.getTransaction().rollback();
em.close();
}
}
}
/**
* Scenario being tested:
* 3) No invalid Persist constraint exception when mode=NONE
* Tests that no Validation Providers are used when disabled.
*/
public void testNullConstraintIgnored() {
getLog().trace("testNullConstraintIgnored() started");
@ -181,7 +230,108 @@ public class TestConstraints extends SingleEMFTestCase {
/**
* Scenario being tested:
* 5) Test @AssertTrue constraint exception on variables in mode=AUTO
* 4) Test @Null constraint exception on variables in mode=AUTO
* Basic constraint test for a violation exception.
*/
public void testNullConstraint() {
getLog().trace("testNullConstraint() 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 ConstraintNull instance
em.getTransaction().begin();
ConstraintNull c = ConstraintNull.createInvalidNull();
em.persist(c);
em.getTransaction().commit();
getLog().trace("testNullConstraint() failed");
fail("Expected a Validation exception");
} catch (Exception e) {
// expected
getLog().trace("Caught expected exception = " + e);
getLog().trace("testNullConstraint() passed");
} finally {
if ((em != null) && em.isOpen()) {
em.close();
}
}
}
/**
* Scenario being tested:
* 5) Test @NotNull constraint exception on variables in mode=AUTO
* Basic constraint test for a violation exception.
*/
public void testNotNullConstraint() {
getLog().trace("testNotNullConstraint() 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 ConstraintNull instance
em.getTransaction().begin();
ConstraintNull c = ConstraintNull.createInvalidNotNull();
em.persist(c);
em.getTransaction().commit();
getLog().trace("testNotNullConstraint() failed");
fail("Expected a Validation exception");
} catch (Exception e) {
// expected
getLog().trace("Caught expected exception = " + e);
getLog().trace("testNotNullConstraint() passed");
} finally {
if ((em != null) && em.isOpen()) {
em.close();
}
}
}
/**
* Scenario being tested:
* 6) Test @NotNull and @Null constraints pass in mode=AUTO
* Basic constraint test for no violations.
*/
public void testNullNotNullConstraint() {
getLog().trace("testNullNotNullConstraint() 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 ConstraintNull instance
em.getTransaction().begin();
ConstraintNull c = ConstraintNull.createValid();
em.persist(c);
em.getTransaction().commit();
getLog().trace("testNullNotNullConstraint() passed");
} catch (Exception e) {
// unexpected
fail("Caught unexpected exception = " + e);
} finally {
if ((em != null) && em.isOpen()) {
em.close();
}
}
}
/**
* Scenario being tested:
* 7) Test @AssertTrue constraint exception on variables in mode=AUTO
* Basic constraint test for a violation exception.
*/
public void testAssertTrueConstraint() {
getLog().trace("testAssertTrueConstraint() started");
@ -194,7 +344,7 @@ public class TestConstraints extends SingleEMFTestCase {
assertNotNull(conf);
assertTrue("ValidationMode",
conf.getValidationMode().equalsIgnoreCase("AUTO"));
// create invalid ConstraintNull instance
// create invalid ConstraintBoolean instance
em.getTransaction().begin();
ConstraintBoolean c = ConstraintBoolean.createInvalidTrue();
em.persist(c);
@ -214,7 +364,8 @@ public class TestConstraints extends SingleEMFTestCase {
/**
* Scenario being tested:
* 6) Test @AssertFalse constraint exception on variables in mode=AUTO
* 8) Test @AssertFalse constraint exception on variables in mode=AUTO
* Basic constraint test for a violation exception.
*/
public void testAssertFalseConstraint() {
getLog().trace("testAssertFalseConstraint() started");
@ -227,7 +378,7 @@ public class TestConstraints extends SingleEMFTestCase {
assertNotNull(conf);
assertTrue("ValidationMode",
conf.getValidationMode().equalsIgnoreCase("AUTO"));
// create invalid ConstraintNull instance
// create invalid ConstraintBoolean instance
em.getTransaction().begin();
ConstraintBoolean c = ConstraintBoolean.createInvalidFalse();
em.persist(c);
@ -247,7 +398,8 @@ public class TestConstraints extends SingleEMFTestCase {
/**
* Scenario being tested:
* 7) Test @AssertFalse and @AssertTrue constraints pass in mode=AUTO
* 9) Test @AssertFalse and @AssertTrue constraints pass in mode=AUTO
* Basic constraint test for no violations.
*/
public void testAssertTrueFalseConstraint() {
getLog().trace("testAssertTrueFalseConstraint() started");
@ -260,7 +412,7 @@ public class TestConstraints extends SingleEMFTestCase {
assertNotNull(conf);
assertTrue("ValidationMode",
conf.getValidationMode().equalsIgnoreCase("AUTO"));
// create invalid ConstraintNull instance
// create valid ConstraintBoolean instance
em.getTransaction().begin();
ConstraintBoolean c = ConstraintBoolean.createValid();
em.persist(c);

View File

@ -45,4 +45,9 @@
<validation-mode>NONE</validation-mode>
</persistence-unit>
<persistence-unit name="null-callback-mode">
<class>org.apache.openjpa.integration.validation.ConstraintNull</class>
<validation-mode>CALLBACK</validation-mode>
</persistence-unit>
</persistence>