OPENJPA-1106 Integration tests for Bean Validation providers. Tests for @AssertTrue and @AssertFalse.

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@789421 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Donald Woods 2009-06-29 19:45:27 +00:00
parent 6ec1122a7e
commit 2fc4ba2c45
2 changed files with 239 additions and 4 deletions

View File

@ -0,0 +1,102 @@
/*
* 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 javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.AssertFalse;
import javax.validation.constraints.AssertTrue;
@Entity(name = "VBOOLEAN")
@Table(name = "BOOLEAN_ENTITY")
public class ConstraintBoolean implements Serializable {
@Transient
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private long id;
@Basic
@AssertTrue
private Boolean trueRequired;
@Basic
@AssertFalse
private Boolean falseRequired;
/*
* Some helper methods to create the entities to test with
*/
public static ConstraintBoolean createInvalidTrue() {
ConstraintBoolean c = new ConstraintBoolean();
c.setTrueRequired(Boolean.FALSE);
c.setFalseRequired(Boolean.FALSE);
return c;
}
public static ConstraintBoolean createInvalidFalse() {
ConstraintBoolean c = new ConstraintBoolean();
c.setTrueRequired(Boolean.TRUE);
c.setFalseRequired(Boolean.TRUE);
return c;
}
public static ConstraintBoolean createValid() {
ConstraintBoolean c = new ConstraintBoolean();
c.setTrueRequired(Boolean.TRUE);
c.setFalseRequired(Boolean.FALSE);
return c;
}
/*
* Main entity code
*/
public ConstraintBoolean() {
}
public long getId() {
return id;
}
public Boolean getTrueRequired() {
return trueRequired;
}
public void setTrueRequired(Boolean b) {
trueRequired = b;
}
public Boolean getFalseRequired() {
return falseRequired;
}
public void setFalseRequired(Boolean b) {
falseRequired = b;
}
}

View File

@ -28,7 +28,11 @@ import org.apache.openjpa.persistence.test.SingleEMFTestCase;
* 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 no constraint exception when mode=NONE
* 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
*
* @version $Rev$ $Date$
*/
@ -36,7 +40,8 @@ public class TestConstraints extends SingleEMFTestCase {
@Override
public void setUp() {
super.setUp(CLEAR_TABLES, ConstraintNull.class);
super.setUp(CLEAR_TABLES,
ConstraintNull.class, ConstraintBoolean.class);
}
/**
@ -107,7 +112,38 @@ public class TestConstraints extends SingleEMFTestCase {
/**
* Scenario being tested:
* 3) Test no constraint exception when mode=NONE
* 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
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:
* 4) Test no constraint exception when mode=NONE
*/
public void testNullConstraintIgnored() {
getLog().trace("testNullConstraintIgnored() started");
@ -143,7 +179,104 @@ public class TestConstraints extends SingleEMFTestCase {
}
}
/**
* Scenario being tested:
* 5) Test @AssertTrue constraint exception on variables in mode=AUTO
*/
public void testAssertTrueConstraint() {
getLog().trace("testAssertTrueConstraint() 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();
ConstraintBoolean c = ConstraintBoolean.createInvalidTrue();
em.persist(c);
em.getTransaction().commit();
getLog().trace("testAssertTrueConstraint() failed");
fail("Expected a Validation exception");
} catch (Exception e) {
// expected
getLog().trace("Caught expected exception = " + e);
getLog().trace("testAssertTrueConstraint() passed");
} finally {
if ((em != null) && em.isOpen()) {
em.close();
}
}
}
/**
* Scenario being tested:
* 6) Test @AssertFalse constraint exception on variables in mode=AUTO
*/
public void testAssertFalseConstraint() {
getLog().trace("testAssertFalseConstraint() 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();
ConstraintBoolean c = ConstraintBoolean.createInvalidFalse();
em.persist(c);
em.getTransaction().commit();
getLog().trace("testAssertFalseConstraint() failed");
fail("Expected a Validation exception");
} catch (Exception e) {
// expected
getLog().trace("Caught expected exception = " + e);
getLog().trace("testAssertFalseConstraint() passed");
} finally {
if ((em != null) && em.isOpen()) {
em.close();
}
}
}
/**
* Scenario being tested:
* 7) Test @AssertFalse and @AssertTrue constraints pass in mode=AUTO
*/
public void testAssertTrueFalseConstraint() {
getLog().trace("testAssertTrueFalseConstraint() 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();
ConstraintBoolean c = ConstraintBoolean.createValid();
em.persist(c);
em.getTransaction().commit();
getLog().trace("testAssertTrueFalseConstraint() passed");
} catch (Exception e) {
// unexpected
fail("Caught unexpected exception = " + e);
} finally {
if ((em != null) && em.isOpen()) {
em.close();
}
}
}
/**
* Internal convenience method for getting the OpenJPA logger
*