OPENJPA-1787: setting eol-style (nit)

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1080789 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Dick 2011-03-11 23:09:21 +00:00
parent 6fdcebd673
commit 6309bff2f0

View File

@ -1,226 +1,226 @@
/* /*
* Licensed to the Apache Software Foundation (ASF) under one * Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file * or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information * distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file * regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the * to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance * "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at * with the License. You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, * Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an * software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the * KIND, either express or implied. See the License for the
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*/ */
package org.apache.openjpa.integration.validation; package org.apache.openjpa.integration.validation;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.validation.ConstraintViolationException; import javax.validation.ConstraintViolationException;
import org.apache.openjpa.conf.OpenJPAConfiguration; import org.apache.openjpa.conf.OpenJPAConfiguration;
import org.apache.openjpa.lib.log.Log; import org.apache.openjpa.lib.log.Log;
import org.apache.openjpa.persistence.OpenJPAEntityManager; import org.apache.openjpa.persistence.OpenJPAEntityManager;
import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI; import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
import org.apache.openjpa.persistence.OpenJPAPersistence; import org.apache.openjpa.persistence.OpenJPAPersistence;
import org.apache.openjpa.persistence.test.AbstractPersistenceTestCase; import org.apache.openjpa.persistence.test.AbstractPersistenceTestCase;
/** /**
* Tests the Bean Validation support when using the em.merge() * Tests the Bean Validation support when using the em.merge()
* operation. * operation.
* *
* @version $Rev$ $Date$ * @version $Rev$ $Date$
*/ */
public class TestMerge extends AbstractPersistenceTestCase { public class TestMerge extends AbstractPersistenceTestCase {
private static OpenJPAEntityManagerFactorySPI emf = null; private static OpenJPAEntityManagerFactorySPI emf = null;
@Override @Override
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
emf = (OpenJPAEntityManagerFactorySPI) emf = (OpenJPAEntityManagerFactorySPI)
OpenJPAPersistence.createEntityManagerFactory( OpenJPAPersistence.createEntityManagerFactory(
"ConstraintPU", "ConstraintPU",
"org/apache/openjpa/integration/validation/persistence.xml"); "org/apache/openjpa/integration/validation/persistence.xml");
} }
@Override @Override
public void tearDown() throws Exception { public void tearDown() throws Exception {
closeEMF(emf); closeEMF(emf);
emf = null; emf = null;
super.tearDown(); super.tearDown();
} }
/** /**
* Verifies constraint validation occurs on a "new" merged entity only after * Verifies constraint validation occurs on a "new" merged entity only after
* the state of the persistent entity is properly set. * the state of the persistent entity is properly set.
*/ */
public void testMergeNew() { public void testMergeNew() {
getLog().trace("testMergeNew() started"); getLog().trace("testMergeNew() started");
// Part 1 - Create and persist a valid entity // Part 1 - Create and persist a valid entity
// create EM from default EMF // create EM from default EMF
OpenJPAEntityManager em = emf.createEntityManager(); OpenJPAEntityManager em = emf.createEntityManager();
assertNotNull(em); assertNotNull(em);
try { try {
// verify Validation Mode // verify Validation Mode
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
OpenJPAConfiguration conf = em.getConfiguration(); OpenJPAConfiguration conf = em.getConfiguration();
assertNotNull(conf); assertNotNull(conf);
assertTrue("ValidationMode", assertTrue("ValidationMode",
conf.getValidationMode().equalsIgnoreCase("AUTO")); conf.getValidationMode().equalsIgnoreCase("AUTO"));
Person p = createPerson(em); Person p = createPerson(em);
em.getTransaction().begin(); em.getTransaction().begin();
p = em.merge(p); p = em.merge(p);
em.getTransaction().commit(); em.getTransaction().commit();
getLog().trace("testMergeNew() Part 1 of 2 passed"); getLog().trace("testMergeNew() Part 1 of 2 passed");
} catch (Exception e) { } catch (Exception e) {
// unexpected // unexpected
getLog().trace("testMergeNew() Part 1 of 2 failed"); getLog().trace("testMergeNew() Part 1 of 2 failed");
fail("Caught unexpected exception = " + e); fail("Caught unexpected exception = " + e);
} finally { } finally {
closeEM(em); closeEM(em);
} }
// Part 2 - Verify that merge throws a CVE when a constraint is not met. // Part 2 - Verify that merge throws a CVE when a constraint is not met.
em = emf.createEntityManager(); em = emf.createEntityManager();
assertNotNull(em); assertNotNull(em);
try { try {
Person p = createPerson(em); Person p = createPerson(em);
em.getTransaction().begin(); em.getTransaction().begin();
p.setLastName(null); // Force a CVE p.setLastName(null); // Force a CVE
p = em.merge(p); p = em.merge(p);
getLog().trace("testMergeNew() Part 2 of 2 failed"); getLog().trace("testMergeNew() Part 2 of 2 failed");
fail("Expected a ConstraintViolationException"); fail("Expected a ConstraintViolationException");
} catch (ConstraintViolationException e) { } catch (ConstraintViolationException e) {
// expected // expected
getLog().trace("Caught expected ConstraintViolationException = " + e); getLog().trace("Caught expected ConstraintViolationException = " + e);
getLog().trace("testMergeNew() Part 2 of 2 passed"); getLog().trace("testMergeNew() Part 2 of 2 passed");
} finally { } finally {
closeEM(em); closeEM(em);
} }
} }
/** /**
* Verifies constraint validation occurs on a "new" merged entity only after * Verifies constraint validation occurs on a "new" merged entity only after
* the state of the persistent entity is properly set. * the state of the persistent entity is properly set.
*/ */
public void testMergeExisting() { public void testMergeExisting() {
getLog().trace("testMergeExisting() started"); getLog().trace("testMergeExisting() started");
// Part 1 - Create and persist a valid entity // Part 1 - Create and persist a valid entity
// create EM from default EMF // create EM from default EMF
OpenJPAEntityManager em = emf.createEntityManager(); OpenJPAEntityManager em = emf.createEntityManager();
assertNotNull(em); assertNotNull(em);
try { try {
// verify Validation Mode // verify Validation Mode
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
OpenJPAConfiguration conf = em.getConfiguration(); OpenJPAConfiguration conf = em.getConfiguration();
assertNotNull(conf); assertNotNull(conf);
assertTrue("ValidationMode", assertTrue("ValidationMode",
conf.getValidationMode().equalsIgnoreCase("AUTO")); conf.getValidationMode().equalsIgnoreCase("AUTO"));
// Create and persist a new entity // Create and persist a new entity
Person p = createPerson(em); Person p = createPerson(em);
em.getTransaction().begin(); em.getTransaction().begin();
em.persist(p); em.persist(p);
em.getTransaction().commit(); em.getTransaction().commit();
em.clear(); em.clear();
// find the entity // find the entity
p = em.find(Person.class, p.getId()); p = em.find(Person.class, p.getId());
// modify the entity and merge // modify the entity and merge
em.getTransaction().begin(); em.getTransaction().begin();
p.setFirstName("NewFirst"); p.setFirstName("NewFirst");
// merge should not throw a CVE // merge should not throw a CVE
p = em.merge(p); p = em.merge(p);
em.getTransaction().commit(); em.getTransaction().commit();
em.clear(); em.clear();
p = em.find(Person.class, p.getId()); p = em.find(Person.class, p.getId());
assertEquals("NewFirst", p.getFirstName()); assertEquals("NewFirst", p.getFirstName());
getLog().trace("testMergeExisting() Part 1 of 2 passed"); getLog().trace("testMergeExisting() Part 1 of 2 passed");
} catch (Exception e) { } catch (Exception e) {
// unexpected // unexpected
getLog().trace("testMergeExisting() Part 1 of 2 failed"); getLog().trace("testMergeExisting() Part 1 of 2 failed");
fail("Caught unexpected exception = " + e); fail("Caught unexpected exception = " + e);
} finally { } finally {
closeEM(em); closeEM(em);
} }
// Part 2 - Verify that merge throws a CVE when a constraint is not met. // Part 2 - Verify that merge throws a CVE when a constraint is not met.
em = emf.createEntityManager(); em = emf.createEntityManager();
assertNotNull(em); assertNotNull(em);
try { try {
// Create and persist a new entity // Create and persist a new entity
Person p = createPerson(em); Person p = createPerson(em);
em.getTransaction().begin(); em.getTransaction().begin();
em.persist(p); em.persist(p);
em.getTransaction().commit(); em.getTransaction().commit();
em.clear(); em.clear();
// find the entity // find the entity
p = em.find(Person.class, p.getId()); p = em.find(Person.class, p.getId());
// detach the entity // detach the entity
em.detach(p); em.detach(p);
assertFalse(em.contains(p)); assertFalse(em.contains(p));
// Set name to an invalid value (contains a space) to force a CVE upon merge+update // Set name to an invalid value (contains a space) to force a CVE upon merge+update
p.setFirstName("First Name"); p.setFirstName("First Name");
em.getTransaction().begin(); em.getTransaction().begin();
try { try {
p = em.merge(p); p = em.merge(p);
} catch (Throwable t) { } catch (Throwable t) {
fail("Did not expect a CVE upon merge."); fail("Did not expect a CVE upon merge.");
} }
// Commit should throw a CVE // Commit should throw a CVE
em.getTransaction().commit(); em.getTransaction().commit();
getLog().trace("testMergeExisting() Part 2 of 2 failed"); getLog().trace("testMergeExisting() Part 2 of 2 failed");
fail("Expected a ConstraintViolationException"); fail("Expected a ConstraintViolationException");
} catch (ConstraintViolationException e) { } catch (ConstraintViolationException e) {
// expected // expected
getLog().trace("Caught expected ConstraintViolationException = " + e); getLog().trace("Caught expected ConstraintViolationException = " + e);
getLog().trace("testMergeExisting() Part 2 of 2 passed"); getLog().trace("testMergeExisting() Part 2 of 2 passed");
} finally { } finally {
closeEM(em); closeEM(em);
} }
} }
private Person createPerson(EntityManager em) { private Person createPerson(EntityManager em) {
Person p = new Person(); Person p = new Person();
p.setFirstName("First"); p.setFirstName("First");
p.setLastName("Last"); p.setLastName("Last");
p.setHomeAddress(createAddress(em)); p.setHomeAddress(createAddress(em));
return p; return p;
} }
private IAddress createAddress(EntityManager em) { private IAddress createAddress(EntityManager em) {
Address addr = new Address(); Address addr = new Address();
addr.setCity("City"); addr.setCity("City");
addr.setPhoneNumber("555-555-5555"); addr.setPhoneNumber("555-555-5555");
addr.setPostalCode("55555"); addr.setPostalCode("55555");
addr.setState("ST"); addr.setState("ST");
addr.setStreetAddress("Some Street"); addr.setStreetAddress("Some Street");
if (!em.getTransaction().isActive()) { if (!em.getTransaction().isActive()) {
em.getTransaction().begin(); em.getTransaction().begin();
} }
em.persist(addr); em.persist(addr);
em.getTransaction().commit(); em.getTransaction().commit();
return addr; return addr;
} }
/** /**
* Internal convenience method for getting the OpenJPA logger * Internal convenience method for getting the OpenJPA logger
* *
* @return * @return
*/ */
private Log getLog() { private Log getLog() {
return emf.getConfiguration().getLog("Tests"); return emf.getConfiguration().getLog("Tests");
} }
} }