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/08 18:03:03 UTC
svn commit: r792203 - in
/openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation:
ConstraintPattern.java TestConstraints.java
Author: dwoods
Date: Wed Jul 8 16:03:03 2009
New Revision: 792203
URL: http://svn.apache.org/viewvc?rev=792203&view=rev
Log:
OPENJPA-1157 adding @Pattern constraint tests
Added:
openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/ConstraintPattern.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/ConstraintPattern.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/ConstraintPattern.java?rev=792203&view=auto
==============================================================================
--- openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/ConstraintPattern.java (added)
+++ openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/ConstraintPattern.java Wed Jul 8 16:03:03 2009
@@ -0,0 +1,112 @@
+/*
+ * 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.NamedQueries;
+import javax.persistence.NamedQuery;
+import javax.persistence.Table;
+import javax.persistence.Transient;
+import javax.validation.constraints.Pattern;
+
+
+@NamedQueries( {
+ @NamedQuery(name="FindFirst",
+ query="select c from VPATTERN c where c.id = 1"),
+ @NamedQuery(name="FindAll", query="select c from VPATTERN c")
+})
+
+@Entity(name = "VPATTERN")
+@Table(name = "PATTERN_ENTITY")
+public class ConstraintPattern implements Serializable {
+
+ @Transient
+ private static final long serialVersionUID = 1L;
+
+ @Id
+ @GeneratedValue
+ private long id;
+
+ @Basic
+ @Pattern(regexp = "^[A-Z0-9-]+$", flags = Pattern.Flag.CASE_INSENSITIVE,
+ message = "can only contain alphanumeric characters")
+ private String myString;
+
+ @Basic
+ private String zipcode; // @Pattern([0-9]) constraint is on the getter
+
+
+ /*
+ * Some helper methods to create the entities to test with
+ */
+ public static ConstraintPattern createInvalidString() {
+ ConstraintPattern c = new ConstraintPattern();
+ c.setMyString("a1!b2@c3#");
+ c.setZipcode("90210");
+ return c;
+ }
+
+ public static ConstraintPattern createInvalidZipcode() {
+ ConstraintPattern c = new ConstraintPattern();
+ c.setMyString("");
+ c.setZipcode("1a2b3c");
+ return c;
+ }
+
+ public static ConstraintPattern createValid() {
+ ConstraintPattern c = new ConstraintPattern();
+ c.setMyString("a1b2c3");
+ c.setZipcode("90210");
+ return c;
+ }
+
+
+ /*
+ * Main entity code
+ */
+ public ConstraintPattern() {
+ }
+
+ public long getId() {
+ return id;
+ }
+
+ public String getMyString() {
+ return myString;
+ }
+
+ public void setMyString(String s) {
+ myString = s;
+ }
+
+ @Pattern(regexp = "^[0-9]+$", flags = Pattern.Flag.CASE_INSENSITIVE,
+ message = "can only contain numeric characters")
+ public String getZipcode() {
+ return zipcode;
+ }
+
+ public void setZipcode(String s) {
+ zipcode = s;
+ }
+}
Propchange: openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/ConstraintPattern.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=792203&r1=792202&r2=792203&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 Wed Jul 8 16:03:03 2009
@@ -52,6 +52,8 @@
* 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
+ * 25) Test @Pattern constraint exception on variables in mode=AUTO
+ * 26) Test @Pattern constraint exception on getter in mode=AUTO
*
* Basic constraint test for no violations:
* 6) Persist @NotNull and @Null constraints pass in mode=AUTO
@@ -61,6 +63,7 @@
* 18) Test @Digits constraints pass in mode=AUTO
* 21) Test @Size constraints pass in mode=AUTO
* 24) Test @Past and @Future constraints pass in mode=AUTO
+ * 27) Test @Pattern constraints pass in mode=AUTO
*
* @version $Rev$ $Date$
*/
@@ -72,7 +75,7 @@
ConstraintNull.class, ConstraintBoolean.class,
ConstraintDecimal.class, ConstraintNumber.class,
ConstraintDigits.class, ConstraintSize.class,
- ConstraintDates.class);
+ ConstraintDates.class, ConstraintPattern.class);
}
/**
@@ -1001,6 +1004,113 @@
}
}
+ /**
+ * Scenario being tested:
+ * 25) Test @Pattern constraint exception on variables in mode=AUTO
+ * Basic constraint test for a violation exception.
+ */
+ public void testPatternAlphaConstraint() {
+ getLog().trace("testPatternAlphaConstraint() 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();
+ ConstraintPattern c = ConstraintPattern.createInvalidString();
+ em.persist(c);
+ em.getTransaction().commit();
+ getLog().trace("testPatternAlphaConstraint() failed");
+ fail("Expected a ConstraintViolationException");
+ } catch (ConstraintViolationException e) {
+ // expected
+ getLog().trace("Caught expected ConstraintViolationException = " + e);
+ getLog().trace("testPatternAlphaConstraint() passed");
+ } finally {
+ if ((em != null) && em.isOpen()) {
+ if (em.getTransaction().isActive())
+ em.getTransaction().rollback();
+ em.close();
+ }
+ }
+ }
+
+ /**
+ * Scenario being tested:
+ * 26) Test @Pattern constraint exception on getter in mode=AUTO
+ * Basic constraint test for a violation exception.
+ */
+ public void testPatternNumericConstraint() {
+ getLog().trace("testPatternNumericConstraint() 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();
+ ConstraintPattern c = ConstraintPattern.createInvalidZipcode();
+ em.persist(c);
+ em.getTransaction().commit();
+ getLog().trace("testPatternNumericConstraint() failed");
+ fail("Expected a ConstraintViolationException");
+ } catch (ConstraintViolationException e) {
+ // expected
+ getLog().trace("Caught expected ConstraintViolationException = " + e);
+ getLog().trace("testPatternNumericConstraint() passed");
+ } finally {
+ if ((em != null) && em.isOpen()) {
+ if (em.getTransaction().isActive())
+ em.getTransaction().rollback();
+ em.close();
+ }
+ }
+ }
+
+ /**
+ * Scenario being tested:
+ * 27) Test @Pattern constraints pass in mode=AUTO
+ * Basic constraint test for no violations.
+ */
+ public void testPatternConstraint() {
+ getLog().trace("testPatternConstraint() 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();
+ ConstraintPattern c = ConstraintPattern.createValid();
+ em.persist(c);
+ em.getTransaction().commit();
+ getLog().trace("testPatternConstraint() passed");
+ } catch (Exception e) {
+ // unexpected
+ getLog().trace("testPatternConstraint() 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.