OPENJPA-1107 TraversableResolver testcases contributed by Dianne Richards.

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@908173 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Donald Woods 2010-02-09 19:39:45 +00:00
parent 31265de187
commit 27abdb4149
4 changed files with 394 additions and 1 deletions

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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();
}
}

View File

@ -136,6 +136,7 @@
<class>org.apache.openjpa.integration.validation.ConstraintPattern</class> <class>org.apache.openjpa.integration.validation.ConstraintPattern</class>
<class>org.apache.openjpa.integration.validation.Person</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> <validation-mode>AUTO</validation-mode>
<properties> <properties>
<property name="openjpa.jdbc.SynchronizeMappings" <property name="openjpa.jdbc.SynchronizeMappings"
@ -143,6 +144,19 @@
</properties> </properties>
</persistence-unit> </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"> <persistence-unit name="XMLConstraintPU">
<description>Make sure the mapping file and class listings match the same PU <description>Make sure the mapping file and class listings match the same PU
in META-INF/ehn-persistence.xml</description> in META-INF/ehn-persistence.xml</description>