mirror of https://github.com/apache/openjpa.git
OPENJPA-1157 Integration tests for Bean Validation providers - Part 2. Added @Size constraint tests.
git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@791621 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
1bdb8363f8
commit
5bfc42f725
|
@ -115,7 +115,7 @@
|
|||
</repositories>
|
||||
</profile>
|
||||
|
||||
<!-- Default profile for testing with Derby and Bean Validation RI -->
|
||||
<!-- Profile for testing with Hibernate Bean Validation RI -->
|
||||
<profile>
|
||||
<id>hibernate</id>
|
||||
<activation>
|
||||
|
@ -128,14 +128,14 @@
|
|||
<dependency>
|
||||
<groupId>javax.validation</groupId>
|
||||
<artifactId>validation-api</artifactId>
|
||||
<version>1.0.CR2</version>
|
||||
<version>1.0.CR1</version>
|
||||
<!-- <version>1.0.CR3-SNAPSHOT</version> -->
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
<version>4.0.0.Beta1</version>
|
||||
<version>4.0.0.Alpha3</version>
|
||||
<!-- <version>4.0.0.Beta2-SNAPSHOT</version> -->
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
|
|
@ -0,0 +1,134 @@
|
|||
/*
|
||||
* 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 java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
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.Size;
|
||||
|
||||
|
||||
@NamedQueries( {
|
||||
@NamedQuery(name="FindFirst",
|
||||
query="select c from VSIZE c where c.id = 1"),
|
||||
@NamedQuery(name="FindAll", query="select c from VSIZE c")
|
||||
})
|
||||
|
||||
@Entity(name = "VSIZE")
|
||||
@Table(name = "SIZE_ENTITY")
|
||||
public class ConstraintSize implements Serializable {
|
||||
|
||||
@Transient
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private long id;
|
||||
|
||||
@Basic
|
||||
@Size(min = 0, max = 10)
|
||||
private String myString;
|
||||
|
||||
private Map<String,String> myMap; // @Size(1,2) constraint is on the getter
|
||||
|
||||
|
||||
/*
|
||||
* Some helper methods to create the entities to test with
|
||||
*/
|
||||
public static ConstraintSize createInvalidString() {
|
||||
ConstraintSize c = new ConstraintSize();
|
||||
c.setMyString("abcdefghijklmno");
|
||||
c.setValidMap();
|
||||
return c;
|
||||
}
|
||||
|
||||
public static ConstraintSize createInvalidMap() {
|
||||
ConstraintSize c = new ConstraintSize();
|
||||
c.setMyString("");
|
||||
c.setInvalidMap();
|
||||
return c;
|
||||
}
|
||||
|
||||
public static ConstraintSize createInvalidSize() {
|
||||
ConstraintSize c = new ConstraintSize();
|
||||
c.setMyString("abcdefghijklmno");
|
||||
c.setInvalidMap();
|
||||
return c;
|
||||
}
|
||||
|
||||
public static ConstraintSize createValid() {
|
||||
ConstraintSize c = new ConstraintSize();
|
||||
c.setMyString("abc");
|
||||
c.setValidMap();
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Main entity code
|
||||
*/
|
||||
public ConstraintSize() {
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getMyString() {
|
||||
return myString;
|
||||
}
|
||||
|
||||
public void setMyString(String s) {
|
||||
myString = s;
|
||||
}
|
||||
|
||||
@Size(min = 1, max = 2)
|
||||
public Map<String,String> getMyMap() {
|
||||
return myMap;
|
||||
}
|
||||
|
||||
public void setMyMap(Map<String,String> m) {
|
||||
myMap = m;
|
||||
}
|
||||
|
||||
|
||||
private void setInvalidMap() {
|
||||
Map<String,String> m = new HashMap<String,String>();
|
||||
m.put("a", "a value");
|
||||
m.put("b", "b value");
|
||||
m.put("c", "c value");
|
||||
setMyMap(m);
|
||||
}
|
||||
|
||||
private void setValidMap() {
|
||||
Map<String,String> m = new HashMap<String,String>();
|
||||
m.put("a", "a value");
|
||||
setMyMap(m);
|
||||
}
|
||||
|
||||
}
|
|
@ -47,6 +47,8 @@ import org.apache.openjpa.persistence.test.SingleEMFTestCase;
|
|||
* 14) Test @Max constraint exception on getter in mode=AUTO
|
||||
* 16) Test @Digits constraint exception on variables in mode=AUTO
|
||||
* 17) Test @Digits constraint exception on getter in mode=AUTO
|
||||
* 19) Test @Size constraint exception on variables in mode=AUTO
|
||||
* 20) Test @Size constraint exception on getter in mode=AUTO
|
||||
*
|
||||
* Basic constraint test for no violations:
|
||||
* 6) Persist @NotNull and @Null constraints pass in mode=AUTO
|
||||
|
@ -54,6 +56,7 @@ import org.apache.openjpa.persistence.test.SingleEMFTestCase;
|
|||
* 12) Test @DecimalMin and @DecimalMax constraints pass in mode=AUTO
|
||||
* 15) Test @Min and @Max constraints pass in mode=AUTO
|
||||
* 18) Test @Digits constraints pass in mode=AUTO
|
||||
* 21) Test @Size constraints pass in mode=AUTO
|
||||
*
|
||||
* @version $Rev$ $Date$
|
||||
*/
|
||||
|
@ -64,7 +67,7 @@ public class TestConstraints extends SingleEMFTestCase {
|
|||
super.setUp(CLEAR_TABLES,
|
||||
ConstraintNull.class, ConstraintBoolean.class,
|
||||
ConstraintDecimal.class, ConstraintNumber.class,
|
||||
ConstraintDigits.class);
|
||||
ConstraintDigits.class, ConstraintSize.class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -790,6 +793,113 @@ public class TestConstraints extends SingleEMFTestCase {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Scenario being tested:
|
||||
* 19) Test @Size constraint exception on variables in mode=AUTO
|
||||
* Basic constraint test for a violation exception.
|
||||
*/
|
||||
public void testSizeStringConstraint() {
|
||||
getLog().trace("testSizeStringConstraint() 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 ConstraintBoolean instance
|
||||
em.getTransaction().begin();
|
||||
ConstraintSize c = ConstraintSize.createInvalidString();
|
||||
em.persist(c);
|
||||
em.getTransaction().commit();
|
||||
getLog().trace("testSizeStringConstraint() failed");
|
||||
fail("Expected a ConstraintViolationException");
|
||||
} catch (ConstraintViolationException e) {
|
||||
// expected
|
||||
getLog().trace("Caught expected ConstraintViolationException = " + e);
|
||||
getLog().trace("testSizeStringConstraint() passed");
|
||||
} finally {
|
||||
if ((em != null) && em.isOpen()) {
|
||||
if (em.getTransaction().isActive())
|
||||
em.getTransaction().rollback();
|
||||
em.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Scenario being tested:
|
||||
* 20) Test @Size constraint exception on getter in mode=AUTO
|
||||
* Basic constraint test for a violation exception.
|
||||
*/
|
||||
public void testSizeMapConstraint() {
|
||||
getLog().trace("testSizeMapConstraint() 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 ConstraintBoolean instance
|
||||
em.getTransaction().begin();
|
||||
ConstraintSize c = ConstraintSize.createInvalidMap();
|
||||
em.persist(c);
|
||||
em.getTransaction().commit();
|
||||
getLog().trace("testSizeMapConstraint() failed");
|
||||
fail("Expected a ConstraintViolationException");
|
||||
} catch (Exception e) {
|
||||
// expected
|
||||
getLog().trace("Caught expected ConstraintViolationException = " + e);
|
||||
getLog().trace("testSizeMapConstraint() passed");
|
||||
} finally {
|
||||
if ((em != null) && em.isOpen()) {
|
||||
if (em.getTransaction().isActive())
|
||||
em.getTransaction().rollback();
|
||||
em.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Scenario being tested:
|
||||
* 21) Test @Size constraints pass in mode=AUTO
|
||||
* Basic constraint test for no violations.
|
||||
*/
|
||||
public void testSizeConstraint() {
|
||||
getLog().trace("testSizeConstraint() 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 valid ConstraintBoolean instance
|
||||
em.getTransaction().begin();
|
||||
ConstraintSize c = ConstraintSize.createValid();
|
||||
em.persist(c);
|
||||
em.getTransaction().commit();
|
||||
getLog().trace("testSizeConstraint() passed");
|
||||
} catch (Exception e) {
|
||||
// unexpected
|
||||
getLog().trace("testSizeConstraint() failed");
|
||||
fail("Caught unexpected exception = " + e);
|
||||
} finally {
|
||||
if ((em != null) && em.isOpen()) {
|
||||
if (em.getTransaction().isActive())
|
||||
em.getTransaction().rollback();
|
||||
em.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Internal convenience method for getting the OpenJPA logger
|
||||
|
|
Loading…
Reference in New Issue