diff --git a/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/ConstraintPattern.java b/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/ConstraintPattern.java new file mode 100644 index 000000000..82bfe513a --- /dev/null +++ b/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/ConstraintPattern.java @@ -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; + } +} diff --git a/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestConstraints.java b/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestConstraints.java index 4a4f629f4..c4e69cdb9 100644 --- a/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestConstraints.java +++ b/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestConstraints.java @@ -52,6 +52,8 @@ import org.apache.openjpa.persistence.test.SingleEMFTestCase; * 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 @@ import org.apache.openjpa.persistence.test.SingleEMFTestCase; * 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 @@ public class TestConstraints extends SingleEMFTestCase { ConstraintNull.class, ConstraintBoolean.class, ConstraintDecimal.class, ConstraintNumber.class, ConstraintDigits.class, ConstraintSize.class, - ConstraintDates.class); + ConstraintDates.class, ConstraintPattern.class); } /** @@ -1001,6 +1004,113 @@ public class TestConstraints extends SingleEMFTestCase { } } + /** + * 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.