mirror of https://github.com/apache/openjpa.git
OPENJPA-1157 adding @Future and @Past constraint tests
git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@792186 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
ba49a316e7
commit
edf98f5f91
|
@ -0,0 +1,145 @@
|
||||||
|
/*
|
||||||
|
* 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.math.BigDecimal;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.GregorianCalendar;
|
||||||
|
|
||||||
|
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.DecimalMax;
|
||||||
|
import javax.validation.constraints.DecimalMin;
|
||||||
|
import javax.validation.constraints.Future;
|
||||||
|
import javax.validation.constraints.Past;
|
||||||
|
|
||||||
|
|
||||||
|
@NamedQueries( {
|
||||||
|
@NamedQuery(name="FindFirst",
|
||||||
|
query="select c from VDATES c where c.id = 1"),
|
||||||
|
@NamedQuery(name="FindAll", query="select c from VDATES c")
|
||||||
|
})
|
||||||
|
|
||||||
|
@Entity(name = "VDATES")
|
||||||
|
@Table(name = "DATES_ENTITY")
|
||||||
|
public class ConstraintDates implements Serializable {
|
||||||
|
|
||||||
|
@Transient
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
// current time when class loaded
|
||||||
|
@Transient
|
||||||
|
private static final Date CURRENT_DATE = new Date();
|
||||||
|
|
||||||
|
// Eight hours in the past
|
||||||
|
@Transient
|
||||||
|
private static final Date PAST_DATE = new Date(
|
||||||
|
CURRENT_DATE.getTime() - (8 * 3600 * 1000));
|
||||||
|
|
||||||
|
// Eight hours in the future
|
||||||
|
@Transient
|
||||||
|
private static final Date FUTURE_DATE = new Date(
|
||||||
|
CURRENT_DATE.getTime() + (8* 3600 * 1000));
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue
|
||||||
|
private long id;
|
||||||
|
|
||||||
|
@Basic
|
||||||
|
@Future
|
||||||
|
private Date futureDate;
|
||||||
|
|
||||||
|
@Basic
|
||||||
|
private GregorianCalendar pastCalendar; // @Past constraint is on the getter
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Some helper methods to create the entities to test with
|
||||||
|
*/
|
||||||
|
public static ConstraintDates createInvalidFuture() {
|
||||||
|
ConstraintDates c = new ConstraintDates();
|
||||||
|
c.setFutureDate(PAST_DATE);
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ConstraintDates createInvalidPast() {
|
||||||
|
GregorianCalendar cal = new GregorianCalendar();
|
||||||
|
cal.add(Calendar.HOUR, 8);
|
||||||
|
|
||||||
|
ConstraintDates c = new ConstraintDates();
|
||||||
|
c.setPastCalendar(cal);
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ConstraintDates createInvalidFuturePast() {
|
||||||
|
GregorianCalendar cal = new GregorianCalendar();
|
||||||
|
cal.add(Calendar.HOUR, 8);
|
||||||
|
|
||||||
|
ConstraintDates c = new ConstraintDates();
|
||||||
|
c.setFutureDate(PAST_DATE);
|
||||||
|
c.setPastCalendar(cal);
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ConstraintDates createValid() {
|
||||||
|
ConstraintDates c = new ConstraintDates();
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Main entity code
|
||||||
|
* Create a valid entity by default
|
||||||
|
*/
|
||||||
|
public ConstraintDates() {
|
||||||
|
GregorianCalendar cal = new GregorianCalendar();
|
||||||
|
cal.add(Calendar.HOUR, -8);
|
||||||
|
setPastCalendar(cal);
|
||||||
|
setFutureDate(FUTURE_DATE);
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Date getFutureDate() {
|
||||||
|
return futureDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFutureDate(Date d) {
|
||||||
|
futureDate = d;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Past
|
||||||
|
public GregorianCalendar getPastCalendar() {
|
||||||
|
return pastCalendar;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPastCalendar(GregorianCalendar d) {
|
||||||
|
pastCalendar = d;
|
||||||
|
}
|
||||||
|
}
|
|
@ -50,6 +50,8 @@ import org.apache.openjpa.persistence.test.SingleEMFTestCase;
|
||||||
* 17) Test @Digits constraint exception on getter in mode=AUTO
|
* 17) Test @Digits constraint exception on getter in mode=AUTO
|
||||||
* 19) Test @Size constraint exception on variables in mode=AUTO
|
* 19) Test @Size constraint exception on variables in mode=AUTO
|
||||||
* 20) Test @Size constraint exception on getter in mode=AUTO
|
* 20) Test @Size constraint exception on getter in mode=AUTO
|
||||||
|
* 22) Test @Future constraint exception on variables in mode=AUTO
|
||||||
|
* 23) Test @Past 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
|
||||||
|
@ -58,6 +60,7 @@ import org.apache.openjpa.persistence.test.SingleEMFTestCase;
|
||||||
* 15) Test @Min and @Max constraints pass in mode=AUTO
|
* 15) Test @Min and @Max constraints pass in mode=AUTO
|
||||||
* 18) Test @Digits constraints pass in mode=AUTO
|
* 18) Test @Digits constraints pass in mode=AUTO
|
||||||
* 21) Test @Size constraints pass in mode=AUTO
|
* 21) Test @Size constraints pass in mode=AUTO
|
||||||
|
* 24) Test @Past and @Future constraints pass in mode=AUTO
|
||||||
*
|
*
|
||||||
* @version $Rev$ $Date$
|
* @version $Rev$ $Date$
|
||||||
*/
|
*/
|
||||||
|
@ -68,7 +71,8 @@ public class TestConstraints extends SingleEMFTestCase {
|
||||||
super.setUp(CLEAR_TABLES,
|
super.setUp(CLEAR_TABLES,
|
||||||
ConstraintNull.class, ConstraintBoolean.class,
|
ConstraintNull.class, ConstraintBoolean.class,
|
||||||
ConstraintDecimal.class, ConstraintNumber.class,
|
ConstraintDecimal.class, ConstraintNumber.class,
|
||||||
ConstraintDigits.class, ConstraintSize.class);
|
ConstraintDigits.class, ConstraintSize.class,
|
||||||
|
ConstraintDates.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -521,7 +525,7 @@ public class TestConstraints extends SingleEMFTestCase {
|
||||||
em.getTransaction().commit();
|
em.getTransaction().commit();
|
||||||
getLog().trace("testDecimalMaxConstraint() failed");
|
getLog().trace("testDecimalMaxConstraint() failed");
|
||||||
fail("Expected a ConstraintViolationException");
|
fail("Expected a ConstraintViolationException");
|
||||||
} catch (Exception e) {
|
} catch (ConstraintViolationException e) {
|
||||||
// expected
|
// expected
|
||||||
getLog().trace("Caught expected ConstraintViolationException = " + e);
|
getLog().trace("Caught expected ConstraintViolationException = " + e);
|
||||||
getLog().trace("testDecimalMaxConstraint() passed");
|
getLog().trace("testDecimalMaxConstraint() passed");
|
||||||
|
@ -628,7 +632,7 @@ public class TestConstraints extends SingleEMFTestCase {
|
||||||
em.getTransaction().commit();
|
em.getTransaction().commit();
|
||||||
getLog().trace("testMaxConstraint() failed");
|
getLog().trace("testMaxConstraint() failed");
|
||||||
fail("Expected a ConstraintViolationException");
|
fail("Expected a ConstraintViolationException");
|
||||||
} catch (Exception e) {
|
} catch (ConstraintViolationException e) {
|
||||||
// expected
|
// expected
|
||||||
getLog().trace("Caught expected ConstraintViolationException = " + e);
|
getLog().trace("Caught expected ConstraintViolationException = " + e);
|
||||||
getLog().trace("testMaxConstraint() passed");
|
getLog().trace("testMaxConstraint() passed");
|
||||||
|
@ -735,7 +739,7 @@ public class TestConstraints extends SingleEMFTestCase {
|
||||||
em.getTransaction().commit();
|
em.getTransaction().commit();
|
||||||
getLog().trace("testDigitsFiveConstraint() failed");
|
getLog().trace("testDigitsFiveConstraint() failed");
|
||||||
fail("Expected a ConstraintViolationException");
|
fail("Expected a ConstraintViolationException");
|
||||||
} catch (Exception e) {
|
} catch (ConstraintViolationException e) {
|
||||||
// expected
|
// expected
|
||||||
getLog().trace("Caught expected ConstraintViolationException = " + e);
|
getLog().trace("Caught expected ConstraintViolationException = " + e);
|
||||||
getLog().trace("testDigitsFiveConstraint() passed");
|
getLog().trace("testDigitsFiveConstraint() passed");
|
||||||
|
@ -842,7 +846,7 @@ public class TestConstraints extends SingleEMFTestCase {
|
||||||
em.getTransaction().commit();
|
em.getTransaction().commit();
|
||||||
getLog().trace("testSizeMapConstraint() failed");
|
getLog().trace("testSizeMapConstraint() failed");
|
||||||
fail("Expected a ConstraintViolationException");
|
fail("Expected a ConstraintViolationException");
|
||||||
} catch (Exception e) {
|
} catch (ConstraintViolationException e) {
|
||||||
// expected
|
// expected
|
||||||
getLog().trace("Caught expected ConstraintViolationException = " + e);
|
getLog().trace("Caught expected ConstraintViolationException = " + e);
|
||||||
getLog().trace("testSizeMapConstraint() passed");
|
getLog().trace("testSizeMapConstraint() passed");
|
||||||
|
@ -890,6 +894,114 @@ public class TestConstraints extends SingleEMFTestCase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Scenario being tested:
|
||||||
|
* 22) Test @Future constraint exception on variables in mode=AUTO
|
||||||
|
* Basic constraint test for a violation exception.
|
||||||
|
*/
|
||||||
|
public void testDatesFutureConstraint() {
|
||||||
|
getLog().trace("testDatesFutureConstraint() 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();
|
||||||
|
ConstraintDates c = ConstraintDates.createInvalidFuture();
|
||||||
|
em.persist(c);
|
||||||
|
em.getTransaction().commit();
|
||||||
|
getLog().trace("testDatesFutureConstraint() failed");
|
||||||
|
fail("Expected a ConstraintViolationException");
|
||||||
|
} catch (ConstraintViolationException e) {
|
||||||
|
// expected
|
||||||
|
getLog().trace("Caught expected ConstraintViolationException = " + e);
|
||||||
|
getLog().trace("testDatesFutureConstraint() passed");
|
||||||
|
} finally {
|
||||||
|
if ((em != null) && em.isOpen()) {
|
||||||
|
if (em.getTransaction().isActive())
|
||||||
|
em.getTransaction().rollback();
|
||||||
|
em.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Scenario being tested:
|
||||||
|
* 23) Test @Past constraint exception on getter in mode=AUTO
|
||||||
|
* Basic constraint test for a violation exception.
|
||||||
|
*/
|
||||||
|
public void testDatesPastConstraint() {
|
||||||
|
getLog().trace("testDatesPastConstraint() 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();
|
||||||
|
ConstraintDates c = ConstraintDates.createInvalidPast();
|
||||||
|
em.persist(c);
|
||||||
|
em.getTransaction().commit();
|
||||||
|
getLog().trace("testDatesPastConstraint() failed");
|
||||||
|
fail("Expected a ConstraintViolationException");
|
||||||
|
} catch (ConstraintViolationException e) {
|
||||||
|
// expected
|
||||||
|
getLog().trace("Caught expected ConstraintViolationException = " + e);
|
||||||
|
getLog().trace("testDatesPastConstraint() passed");
|
||||||
|
} finally {
|
||||||
|
if ((em != null) && em.isOpen()) {
|
||||||
|
if (em.getTransaction().isActive())
|
||||||
|
em.getTransaction().rollback();
|
||||||
|
em.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Scenario being tested:
|
||||||
|
* 24) Test @Past and @Future constraints pass in mode=AUTO
|
||||||
|
* Basic constraint test for no violations.
|
||||||
|
*/
|
||||||
|
public void testDatesConstraint() {
|
||||||
|
getLog().trace("testDatesConstraint() 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();
|
||||||
|
ConstraintDates c = ConstraintDates.createValid();
|
||||||
|
em.persist(c);
|
||||||
|
em.getTransaction().commit();
|
||||||
|
getLog().trace("testDatesConstraint() passed");
|
||||||
|
} catch (Exception e) {
|
||||||
|
// unexpected
|
||||||
|
getLog().trace("testDatesConstraint() failed");
|
||||||
|
fail("Caught unexpected exception = " + e);
|
||||||
|
} finally {
|
||||||
|
if ((em != null) && em.isOpen()) {
|
||||||
|
if (em.getTransaction().isActive())
|
||||||
|
em.getTransaction().rollback();
|
||||||
|
em.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper method to remove entities and close the emf an any open em's.
|
* Helper method to remove entities and close the emf an any open em's.
|
||||||
* @param emf
|
* @param emf
|
||||||
|
|
Loading…
Reference in New Issue