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 2010/02/09 20:39:46 UTC
svn commit: r908173 - in
/openjpa/trunk/openjpa-integration/validation/src/test:
java/org/apache/openjpa/integration/validation/
resources/org/apache/openjpa/integration/validation/
Author: dwoods
Date: Tue Feb 9 19:39:45 2010
New Revision: 908173
URL: http://svn.apache.org/viewvc?rev=908173&view=rev
Log:
OPENJPA-1107 TraversableResolver testcases contributed by Dianne Richards.
Added:
openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/Book.java (with props)
openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/Publisher.java (with props)
openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestTraversableResolver.java (with props)
Modified:
openjpa/trunk/openjpa-integration/validation/src/test/resources/org/apache/openjpa/integration/validation/persistence.xml
Added: openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/Book.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/Book.java?rev=908173&view=auto
==============================================================================
--- openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/Book.java (added)
+++ openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/Book.java Tue Feb 9 19:39:45 2010
@@ -0,0 +1,103 @@
+/*
+ * 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 javax.persistence.Basic;
+import javax.persistence.Embedded;
+import javax.persistence.Entity;
+import javax.persistence.FetchType;
+import javax.persistence.Id;
+import javax.validation.Valid;
+import javax.validation.constraints.Min;
+import javax.validation.constraints.Size;
+
+@Entity(name="Vbook")
+public class Book {
+ int id;
+ String title;
+ int pages;
+ Publisher publisher;
+
+ public Book() {}
+
+ public Book(int id) {
+ this.id = id;
+ }
+
+ /**
+ * @return the id
+ */
+ @Id
+ public int getId() {
+ return id;
+ }
+
+ /**
+ * @param id the id to set
+ */
+ public void setId(int id) {
+ this.id = id;
+ }
+
+ /**
+ * @return the title
+ */
+ @Basic(fetch = FetchType.LAZY)
+ @Size(min = 0, max = 9)
+ public String getTitle() {
+ return title;
+ }
+
+ /**
+ * @param title the title to set
+ */
+ public void setTitle(String title) {
+ this.title = title;
+ }
+
+ /**
+ * @return the pages
+ */
+ public int getPages() {
+ return pages;
+ }
+
+ /**
+ * @param pages the pages to set
+ */
+ public void setPages(int pages) {
+ this.pages = pages;
+ }
+
+ /**
+ * @return the publisher
+ */
+ @Valid
+ @Embedded
+ public Publisher getPublisher() {
+ return publisher;
+ }
+
+ /**
+ * @param publisher the publisher to set
+ */
+ public void setPublisher(Publisher publisher) {
+ this.publisher = publisher;
+ }
+}
Propchange: openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/Book.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/Publisher.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/Publisher.java?rev=908173&view=auto
==============================================================================
--- openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/Publisher.java (added)
+++ openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/Publisher.java Tue Feb 9 19:39:45 2010
@@ -0,0 +1,62 @@
+/*
+ * 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 javax.persistence.Basic;
+import javax.persistence.Embeddable;
+import javax.validation.constraints.Size;
+
+@Embeddable
+public class Publisher {
+ @Basic
+ @Size(min = 0, max = 5)
+ String name;
+
+ String publisherID;
+
+ public Publisher() {}
+
+ /**
+ * @return the name
+ */
+ public String getName() {
+ return name;
+ }
+
+ /**
+ * @param name the name to set
+ */
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ /**
+ * @return the publisherID
+ */
+ public String getPublisherID() {
+ return publisherID;
+ }
+
+ /**
+ * @param publisherID the publisherID to set
+ */
+ public void setPublisherID(String publisherID) {
+ this.publisherID = publisherID;
+ }
+}
Propchange: openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/Publisher.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestTraversableResolver.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestTraversableResolver.java?rev=908173&view=auto
==============================================================================
--- openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestTraversableResolver.java (added)
+++ openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestTraversableResolver.java Tue Feb 9 19:39:45 2010
@@ -0,0 +1,214 @@
+/*
+ * 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.lang.annotation.ElementType;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.validation.ConstraintViolationException;
+import javax.validation.Path;
+import javax.validation.TraversableResolver;
+
+import junit.framework.TestCase;
+
+import org.apache.openjpa.persistence.OpenJPAEntityManager;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
+import org.apache.openjpa.persistence.OpenJPAPersistence;
+import org.apache.openjpa.persistence.validation.TraversableResolverImpl;
+
+import org.hibernate.validator.engine.PathImpl;
+
+/**
+ * Test the TraversableResolver methods
+ *
+ * First run several testcases from a user perspective. These test the methods
+ * indirectly:
+ * 1) testLoadedTitle()
+ * 2} testUnloaded()
+ * 3) testCascading()
+ *
+ * Then test the methods directly:
+ * 1) testPages()
+ * 2) testTitle
+ */
+public class TestTraversableResolver extends TestCase {
+ private static OpenJPAEntityManagerFactorySPI emf = null;
+ private OpenJPAEntityManager em;
+ private Book book;
+
+ /**
+ * Create a book with a title that is too long, and the embedded
+ * publisher has a name that is also too long. However, use a
+ * persistence unit with validation-mode set to NONE. Therefore,
+ * the create should be successful. This is to setup a situation
+ * where fields to be potentially validated are not necessarily loaded.
+ */
+ @Override
+ public void setUp() {
+ createEMF("non-validation-pu", "SchemaAction='drop,add')");
+ createBook(1, "long title", 234);
+ emf.close();
+ }
+
+ private void createEMF(String pu, String schemaAction) {
+ Map<String, Object> map = new HashMap<String, Object>();
+ map.put("openjpa.jdbc.SynchronizeMappings", "buildSchema(ForeignKeys=true,"
+ + schemaAction);
+ emf = (OpenJPAEntityManagerFactorySPI)OpenJPAPersistence.createEntityManagerFactory(
+ pu,
+ "org/apache/openjpa/integration/validation/persistence.xml",
+ map);
+ assertNotNull(emf);
+ }
+
+ /**
+ * By default, the title is not loaded. Make sure it gets loaded,
+ * make a change in a different field, and commit. The isLoaded() method
+ * of the TraversableResolver should return true, resulting in a validation
+ * being performed and a ConstraintViolationException should be returned
+ * because the title is too long.
+ */
+ public void testLoadedTitle() {
+ createEMF("validation-pu", "SchemaAction='add')");
+ em = emf.createEntityManager();
+ em.getTransaction().begin();
+ book = em.find(org.apache.openjpa.integration.validation.Book.class, 1);
+ assertNotNull(book);
+ book.setPages(124);
+ // load the title
+ String title = book.getTitle();
+ assertEquals("long title", title);
+ boolean exceptionCaught = false;
+ try {
+ em.getTransaction().commit();
+ } catch (ConstraintViolationException e) {
+ exceptionCaught = true;
+ }
+ assertTrue(exceptionCaught);
+ em.close();
+ emf.close();
+ }
+
+ /**
+ * By default, the title and publisher are not loaded. Make a change in a different field
+ * and commit. The isLoaded() method of the TraversableResolver should return
+ * false for both of these. Therefore a validation should not be performed.
+ * The commit should succeed with no exception.
+ */
+ public void testUnloaded() {
+ createEMF("non-validation-pu", "SchemaAction='add')");
+ em = emf.createEntityManager();
+ em.getTransaction().begin();
+ book = em.find(org.apache.openjpa.integration.validation.Book.class, 1);
+ assertNotNull(book);
+ book.setPages(124);
+ boolean exceptionCaught = false;
+ try {
+ em.getTransaction().commit();
+ } catch (ConstraintViolationException e) {
+ exceptionCaught = true;
+ }
+ assertFalse(exceptionCaught);
+ em.close();
+ emf.close();
+ }
+
+ /**
+ * By default, the publisher is not loaded. Make sure it gets loaded.
+ * The isLoaded() and isCascadable() methods should both return true,
+ * resulting in a validation being performed. A ConstraintViolation
+ * should be thrown since the publisher name is too long.
+ */
+ public void testCascading() {
+ createEMF("validation-pu", "SchemaAction='add')");
+ em = emf.createEntityManager();
+ em.getTransaction().begin();
+ book = em.find(org.apache.openjpa.integration.validation.Book.class, 1);
+ assertNotNull(book);
+ book.setPages(124);
+ // load the embedded publisher
+ Publisher publisher = book.getPublisher();
+ assertNotNull(publisher);
+ publisher.setPublisherID("yyy");
+ boolean exceptionCaught = false;
+ try {
+ em.getTransaction().commit();
+ } catch (Exception e) {
+ exceptionCaught = true;
+ }
+ assertTrue(exceptionCaught);
+ em.close();
+ emf.close();
+ }
+
+ /**
+ * Test the isReachable() and isCascadable() methods on the pages element of Book,
+ * which is eagerly fetched by default.
+ */
+ public void testPages() {
+ createEMF("validation-pu", "SchemaAction='add')");
+ em = emf.createEntityManager();
+ em.getTransaction().begin();
+ book = em.find(org.apache.openjpa.integration.validation.Book.class, 1);
+ assertNotNull(book);
+ PathImpl path = PathImpl.createPathFromString("org.apache.openjpa.integration.validation.Book.pages");
+ Path.Node node = path.getLeafNode();
+ TraversableResolver tr = new TraversableResolverImpl();
+ assertTrue(tr.isReachable(book, node, Book.class, null, ElementType.METHOD));
+ assertTrue(tr.isCascadable(book, node, Book.class, null, ElementType.METHOD));
+ em.getTransaction().commit();
+ em.close();
+ emf.close();
+ }
+
+ /**
+ * Test the isReachable() method on the title.
+ * It is configured with fetch=FetvhType.LAZY.
+ */
+ public void testTitle() {
+ createEMF("validation-pu", "SchemaAction='add')");
+ em = emf.createEntityManager();
+ em.getTransaction().begin();
+ book = em.find(org.apache.openjpa.integration.validation.Book.class, 1);
+ assertNotNull(book);
+ PathImpl path = PathImpl.createPathFromString("org.apache.openjpa.integration.validation.Book.title");
+ Path.Node node = path.getLeafNode();
+ TraversableResolver tr = new TraversableResolverImpl();
+ assertFalse(tr.isReachable(book, node, Book.class, null, ElementType.FIELD));
+ em.getTransaction().commit();
+ em.close();
+ emf.close();
+ }
+
+ private void createBook(int id, String title, int pages) {
+ em = emf.createEntityManager();
+ book = new Book(id);
+ book.setTitle(title);
+ book.setPages(pages);
+ Publisher publisher = new Publisher();
+ publisher.setName("long name");
+ publisher.setPublisherID("xxx");
+ book.setPublisher(publisher);
+ em.getTransaction().begin();
+ em.persist(book);
+ em.getTransaction().commit();
+ em.close();
+ }
+}
Propchange: openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestTraversableResolver.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: openjpa/trunk/openjpa-integration/validation/src/test/resources/org/apache/openjpa/integration/validation/persistence.xml
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-integration/validation/src/test/resources/org/apache/openjpa/integration/validation/persistence.xml?rev=908173&r1=908172&r2=908173&view=diff
==============================================================================
--- openjpa/trunk/openjpa-integration/validation/src/test/resources/org/apache/openjpa/integration/validation/persistence.xml (original)
+++ openjpa/trunk/openjpa-integration/validation/src/test/resources/org/apache/openjpa/integration/validation/persistence.xml Tue Feb 9 19:39:45 2010
@@ -135,7 +135,8 @@
<class>org.apache.openjpa.integration.validation.ConstraintDates</class>
<class>org.apache.openjpa.integration.validation.ConstraintPattern</class>
<class>org.apache.openjpa.integration.validation.Person</class>
- <class>org.apache.openjpa.integration.validation.Address</class>
+ <class>org.apache.openjpa.integration.validation.Address</class>
+ <class>org.apache.openjpa.integration.validation.Book</class>
<validation-mode>AUTO</validation-mode>
<properties>
<property name="openjpa.jdbc.SynchronizeMappings"
@@ -143,6 +144,19 @@
</properties>
</persistence-unit>
+ <persistence-unit name="validation-pu">
+ <class>org.apache.openjpa.integration.validation.Book</class>
+ <class>org.apache.openjpa.integration.validation.Publisher</class>
+ <validation-mode>AUTO</validation-mode>
+ </persistence-unit>
+
+ <persistence-unit name="non-validation-pu">
+ <class>org.apache.openjpa.integration.validation.Book</class>
+ <class>org.apache.openjpa.integration.validation.Publisher</class>
+ <validation-mode>NONE</validation-mode>
+ </persistence-unit>
+
+
<persistence-unit name="XMLConstraintPU">
<description>Make sure the mapping file and class listings match the same PU
in META-INF/ehn-persistence.xml</description>