You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by dw...@apache.org on 2009/07/06 22:49:27 UTC
svn commit: r791609 - in
/openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation:
ConstraintDigits.java TestConstraints.java
Author: dwoods
Date: Mon Jul 6 20:49:27 2009
New Revision: 791609
URL: http://svn.apache.org/viewvc?rev=791609&view=rev
Log:
OPENJPA-1157 Integration tests for Bean Validation providers - Part 2. Added @Digits constraint tests.
Added:
openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/ConstraintDigits.java (with props)
Modified:
openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestConstraints.java
Added: openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/ConstraintDigits.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/ConstraintDigits.java?rev=791609&view=auto
==============================================================================
--- openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/ConstraintDigits.java (added)
+++ openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/ConstraintDigits.java Mon Jul 6 20:49:27 2009
@@ -0,0 +1,146 @@
+/*
+ * 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 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.Digits;
+
+
+@NamedQueries( {
+ @NamedQuery(name="FindFirst",
+ query="select c from VDIGITS c where c.id = 1"),
+ @NamedQuery(name="FindAll", query="select c from VDIGITS c")
+})
+
+@Entity(name = "VDIGITS")
+@Table(name = "DIGITS_ENTITY")
+public class ConstraintDigits implements Serializable {
+
+ @Transient
+ private static final long serialVersionUID = 1L;
+
+ @Transient
+ private static final BigDecimal SIX_DIGITS = new BigDecimal("666666.666666");
+
+ @Transient
+ private static final BigDecimal FIVE_DIGITS = new BigDecimal("55555.55555");
+
+ @Transient
+ private static final BigDecimal ONE_DIGITS = new BigDecimal("1.1");
+
+ @Id
+ @GeneratedValue
+ private long id;
+
+ @Basic
+ @Digits(integer = 2, fraction = 2)
+ private BigDecimal twoDigits;
+
+ @Basic
+ private BigDecimal fiveDigits; // @Digits(5,5) constraint is on the getter
+
+
+ /*
+ * Some helper methods to create the entities to test with
+ */
+ public static ConstraintDigits createInvalidTwoDigits() {
+ ConstraintDigits c = new ConstraintDigits();
+ c.setTwoDigits(FIVE_DIGITS);
+ c.setFiveDigits(FIVE_DIGITS);
+ return c;
+ }
+
+ public static ConstraintDigits createInvalidFiveDigits() {
+ ConstraintDigits c = new ConstraintDigits();
+ c.setTwoDigits(ONE_DIGITS);
+ c.setFiveDigits(SIX_DIGITS);
+ return c;
+ }
+
+ public static ConstraintDigits createInvalidDigits() {
+ ConstraintDigits c = new ConstraintDigits();
+ c.setTwoDigits(FIVE_DIGITS);
+ c.setFiveDigits(SIX_DIGITS);
+ return c;
+ }
+
+ public static ConstraintDigits createValid() {
+ ConstraintDigits c = new ConstraintDigits();
+ // extra leading zeros only count as 1 digit
+ c.setTwoDigits("00000000.1");
+ // as long as one of integer/fraction is supplied and valid
+ c.setFiveDigits("1234");
+ return c;
+ }
+
+
+ /*
+ * Main entity code
+ */
+ public ConstraintDigits() {
+ }
+
+ public long getId() {
+ return id;
+ }
+
+ public BigDecimal getTwoDigits() {
+ return twoDigits;
+ }
+
+ public void setTwoDigits(BigDecimal d) {
+ twoDigits = d;
+ }
+
+ public void setTwoDigits(String s) {
+ twoDigits = toBigDecimal(s);
+ }
+
+ @Digits(integer = 5, fraction = 5)
+ public BigDecimal getFiveDigits() {
+ return fiveDigits;
+ }
+
+ public void setFiveDigits(BigDecimal d) {
+ fiveDigits = d;
+ }
+
+ public void setFiveDigits(String s) {
+ fiveDigits = toBigDecimal(s);
+ }
+
+
+ private BigDecimal toBigDecimal(String s) {
+ try {
+ return new BigDecimal(s);
+ } catch (NumberFormatException nfe) {
+ return null;
+ }
+ }
+}
Propchange: openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/ConstraintDigits.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestConstraints.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestConstraints.java?rev=791609&r1=791608&r2=791609&view=diff
==============================================================================
--- openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestConstraints.java (original)
+++ openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestConstraints.java Mon Jul 6 20:49:27 2009
@@ -45,12 +45,15 @@
* 11) Test @DecimalMax constraint exception on getter in mode=AUTO
* 13) Test @Min constraint exception on variables in mode=AUTO
* 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
*
* 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
* 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
*
* @version $Rev$ $Date$
*/
@@ -60,7 +63,8 @@
public void setUp() {
super.setUp(CLEAR_TABLES,
ConstraintNull.class, ConstraintBoolean.class,
- ConstraintDecimal.class, ConstraintNumber.class);
+ ConstraintDecimal.class, ConstraintNumber.class,
+ ConstraintDigits.class);
}
/**
@@ -679,6 +683,113 @@
}
}
+ /**
+ * Scenario being tested:
+ * 16) Test @Digits constraint exception on variables in mode=AUTO
+ * Basic constraint test for a violation exception.
+ */
+ public void testDigitsTwoConstraint() {
+ getLog().trace("testDigitsTwoConstraint() 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();
+ ConstraintDigits c = ConstraintDigits.createInvalidTwoDigits();
+ em.persist(c);
+ em.getTransaction().commit();
+ getLog().trace("testDigitsTwoConstraint() failed");
+ fail("Expected a ConstraintViolationException");
+ } catch (ConstraintViolationException e) {
+ // expected
+ getLog().trace("Caught expected ConstraintViolationException = " + e);
+ getLog().trace("testDigitsTwoConstraint() passed");
+ } finally {
+ if ((em != null) && em.isOpen()) {
+ if (em.getTransaction().isActive())
+ em.getTransaction().rollback();
+ em.close();
+ }
+ }
+ }
+
+ /**
+ * Scenario being tested:
+ * 17) Test @Digits constraint exception on getter in mode=AUTO
+ * Basic constraint test for a violation exception.
+ */
+ public void testDigitsFiveConstraint() {
+ getLog().trace("testDigitsFiveConstraint() 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();
+ ConstraintDigits c = ConstraintDigits.createInvalidFiveDigits();
+ em.persist(c);
+ em.getTransaction().commit();
+ getLog().trace("testDigitsFiveConstraint() failed");
+ fail("Expected a ConstraintViolationException");
+ } catch (Exception e) {
+ // expected
+ getLog().trace("Caught expected ConstraintViolationException = " + e);
+ getLog().trace("testDigitsFiveConstraint() passed");
+ } finally {
+ if ((em != null) && em.isOpen()) {
+ if (em.getTransaction().isActive())
+ em.getTransaction().rollback();
+ em.close();
+ }
+ }
+ }
+
+ /**
+ * Scenario being tested:
+ * 18) Test @Digits constraints pass in mode=AUTO
+ * Basic constraint test for no violations.
+ */
+ public void testDigitsConstraint() {
+ getLog().trace("testDigitsConstraint() 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();
+ ConstraintDigits c = ConstraintDigits.createValid();
+ em.persist(c);
+ em.getTransaction().commit();
+ getLog().trace("testDigitsConstraint() passed");
+ } catch (Exception e) {
+ // unexpected
+ getLog().trace("testDigitsConstraint() 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