Bring some consistency to test case structure. Now all persistent test cases

extend SingleEMFTestCase or SingleEMTestCase as appropriate.  These base test
cases contain utilties for initializing the EMF on setup and deleting any
inserted database records and closing the EMF on teardown.



git-svn-id: https://svn.apache.org/repos/asf/incubator/openjpa/trunk@524228 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
A. Abram White 2007-03-30 18:45:15 +00:00
parent 3d929be170
commit 8167400e51
35 changed files with 457 additions and 1039 deletions

View File

@ -577,7 +577,8 @@ public class ClassMetaData
/**
* Returns the alias for the described type, or <code>null</code> if none
* has been set.
* #see setTypeAlias
*
* @see #setTypeAlias
*/
public String getTypeAlias() {
if (_alias == null)

View File

@ -7,47 +7,20 @@ import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.persistence.RollbackException;
import junit.framework.TestCase;
import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
import org.apache.openjpa.persistence.OpenJPAPersistence;
import org.apache.openjpa.persistence.callbacks.ExceptionsFromCallbacksEntity.CallbackTestException;
import junit.framework.TestCase;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
/**
* Tests against JPA section 3.5's description of callback exception handling.
*/
public class TestExceptionsFromCallbacks
extends TestCase {
private OpenJPAEntityManagerFactory emf;
extends SingleEMFTestCase {
public void setUp() {
String types = ExceptionsFromCallbacksEntity.class.getName();
Map props = new HashMap(System.getProperties());
props.put("openjpa.MetaDataFactory", "jpa(Types=" + types + ")");
emf = (OpenJPAEntityManagerFactory) Persistence.
createEntityManagerFactory("test", props);
deleteAll();
}
public void tearDown() {
if (emf == null)
return;
try {
deleteAll();
emf.close();
} catch (Exception e) {
}
}
private void deleteAll() {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.createQuery("delete from ExceptionsFromCallbacksEntity").
executeUpdate();
em.getTransaction().commit();
em.close();
setUp(ExceptionsFromCallbacksEntity.class);
}
public void testPrePersistException() {

View File

@ -4,15 +4,15 @@ import java.util.Map;
import java.util.Arrays;
import javax.persistence.EntityManager;
import org.apache.openjpa.persistence.test.SingleEMTest;
import org.apache.openjpa.persistence.simple.AllFieldTypes;
import org.apache.openjpa.persistence.OpenJPAPersistence;
import org.apache.openjpa.datacache.DataCache;
import org.apache.openjpa.kernel.PCData;
import org.apache.openjpa.meta.ClassMetaData;
import org.apache.openjpa.persistence.OpenJPAPersistence;
import org.apache.openjpa.persistence.simple.AllFieldTypes;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
public class TestArrayFieldsInDataCache
extends SingleEMTest {
extends SingleEMFTestCase {
private static final String[] STRINGS = new String[]{ "a", "b", "c" };
private static final int[] INTS = new int[]{ 1, 2, 3 };
@ -20,25 +20,10 @@ public class TestArrayFieldsInDataCache
private Object jpaOid;
private Object internalOid;
public TestArrayFieldsInDataCache() {
super(AllFieldTypes.class);
}
@Override
protected void setEMFProps(Map props) {
super.setEMFProps(props);
props.put("openjpa.DataCache", "true");
props.put("openjpa.RemoteCommitProvider", "sjvm");
}
@Override
protected boolean clearDatabaseInSetUp() {
return true;
}
@Override
public void setUp() throws Exception {
super.setUp();
public void setUp() {
setUp("openjpa.DataCache", "true",
"openjpa.RemoteCommitProvider", "sjvm",
AllFieldTypes.class);
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
@ -70,7 +55,7 @@ public class TestArrayFieldsInDataCache
cachedFieldData.getClass().getComponentType());
// make sure that the returned results are correct
em = emf.createEntityManager();
EntityManager em = emf.createEntityManager();
AllFieldTypes aft = em.find(AllFieldTypes.class, jpaOid);
assertTrue(Arrays.equals(STRINGS, aft.getArrayOfStrings()));
assertNotSame(STRINGS, aft.getArrayOfStrings());
@ -90,10 +75,10 @@ public class TestArrayFieldsInDataCache
assertEquals(int.class, cachedFieldData.getClass().getComponentType());
// make sure that the returned results are correct
em = emf.createEntityManager();
EntityManager em = emf.createEntityManager();
AllFieldTypes aft = em.find(AllFieldTypes.class, jpaOid);
assertTrue(Arrays.equals(INTS, aft.getArrayOfInts()));
assertNotSame(INTS, aft.getArrayOfInts());
em.close();
}
}
}

View File

@ -7,34 +7,19 @@ import javax.persistence.EntityManager;
import org.apache.openjpa.persistence.OpenJPAEntityManager;
import org.apache.openjpa.persistence.OpenJPAPersistence;
import org.apache.openjpa.persistence.simple.AllFieldTypes;
import org.apache.openjpa.persistence.test.SingleEMFTest;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
public class TestBulkJPQLAndDataCache
extends SingleEMFTest {
extends SingleEMFTestCase {
private Object oid;
public TestBulkJPQLAndDataCache() {
super(AllFieldTypes.class, CascadeParent.class, CascadeChild.class);
}
@Override
protected boolean clearDatabaseInSetUp() {
return true;
}
protected void setEMFProps(Map props) {
super.setEMFProps(props);
props.put("openjpa.DataCache", "true");
props.put("openjpa.RemoteCommitProvider", "sjvm");
}
@Override
public void setUp() throws Exception {
super.setUp();
setUp("openjpa.DataCache", "true",
"openjpa.RemoteCommitProvider", "sjvm",
AllFieldTypes.class, CascadeParent.class, CascadeChild.class);
OpenJPAEntityManager em =
OpenJPAPersistence.cast(emf.createEntityManager());
OpenJPAEntityManager em = emf.createEntityManager();
em.getTransaction().begin();
AllFieldTypes pc = new AllFieldTypes();
pc.setStringField("DeleteMe");
@ -44,28 +29,8 @@ public class TestBulkJPQLAndDataCache
em.close();
}
public void tearDown()
throws Exception {
if (emf == null)
return;
try {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.createQuery("DELETE FROM AllFieldTypes").executeUpdate();
em.createQuery("DELETE FROM CascadeParent").executeUpdate();
em.createQuery("DELETE FROM CascadeChild").executeUpdate();
em.getTransaction().commit();
em.close();
} catch (Exception e) {
}
super.tearDown();
}
public void testBulkDelete() {
OpenJPAEntityManager em =
OpenJPAPersistence.cast(emf.createEntityManager());
OpenJPAEntityManager em = emf.createEntityManager();
em.getTransaction().begin();
List result = em.createQuery("SELECT o FROM AllFieldTypes o")
.getResultList();
@ -74,7 +39,7 @@ public class TestBulkJPQLAndDataCache
em.getTransaction().commit();
em.close();
em = OpenJPAPersistence.cast(emf.createEntityManager());
em = emf.createEntityManager();
// this assumes that we invalidate the cache, rather than update it
// according to the bulk rule.
@ -86,8 +51,7 @@ public class TestBulkJPQLAndDataCache
}
public void testBulkUpdate() {
OpenJPAEntityManager em =
OpenJPAPersistence.cast(emf.createEntityManager());
OpenJPAEntityManager em = emf.createEntityManager();
em.getTransaction().begin();
List result = em.createQuery("SELECT o FROM AllFieldTypes o "
@ -98,7 +62,7 @@ public class TestBulkJPQLAndDataCache
em.getTransaction().commit();
em.close();
em = OpenJPAPersistence.cast(emf.createEntityManager());
em = emf.createEntityManager();
// this assumes that we invalidate the cache, rather than update it
// according to the bulk rule.

View File

@ -15,62 +15,36 @@
*/
package org.apache.openjpa.persistence.datacache;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.RollbackException;
import javax.persistence.LockModeType;
import junit.framework.TestCase;
import java.util.HashMap;
import java.util.Map;
import org.apache.openjpa.persistence.OpenJPAPersistence;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.persistence.EntityManager;
import javax.persistence.RollbackException;
import javax.persistence.LockModeType;
import javax.sql.DataSource;
public class TestDataCacheOptimisticLockRecovery
extends TestCase {
import org.apache.openjpa.persistence.OpenJPAPersistence;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
public class TestDataCacheOptimisticLockRecovery
extends SingleEMFTestCase {
private EntityManagerFactory emf;
private int pk;
public void setUp() {
Map options = new HashMap(System.getProperties());
// turn on caching
options.put("openjpa.DataCache", "true");
options.put("openjpa.RemoteCommitProvider", "sjvm");
// ensure that OpenJPA knows about our type, so that
// auto-schema-creation works
options.put("openjpa.MetaDataFactory",
"jpa(Types=" + OptimisticLockInstance.class.getName() + ")");
emf = Persistence.createEntityManagerFactory("test", options);
setUp("openjpa.DataCache", "true",
"openjpa.RemoteCommitProvider", "sjvm",
OptimisticLockInstance.class);
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.createQuery("delete from OptimisticLockInstance").executeUpdate();
OptimisticLockInstance oli = new OptimisticLockInstance("foo");
try {
em.persist(oli);
em.getTransaction().commit();
} finally {
if (em.getTransaction().isActive())
em.getTransaction().rollback();
}
em.persist(oli);
em.getTransaction().commit();
pk = oli.getPK();
em.close();
}
public void tearDown() {
emf.close();
}
public void testOptimisticLockRecovery()
throws SQLException {

View File

@ -15,15 +15,11 @@
*/
package org.apache.openjpa.persistence.detachment;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.OptimisticLockException;
import javax.persistence.Persistence;
import junit.framework.TestCase;
import junit.textui.TestRunner;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
/**
* Test that attaching an instance without having changed it still overwrites
@ -32,37 +28,13 @@ import junit.textui.TestRunner;
* @author Abe White
*/
public class TestAttachWithNoChanges
extends TestCase {
private EntityManagerFactory emf;
extends SingleEMFTestCase {
public void setUp() {
String types = DetachmentOneManyParent.class.getName() + ";"
+ DetachmentOneManyChild.class.getName();
Map props = new HashMap(System.getProperties());
props.put("openjpa.MetaDataFactory", "jpa(Types=" + types + ")");
emf = Persistence.createEntityManagerFactory("test", props);
setUp(DetachmentOneManyParent.class, DetachmentOneManyChild.class);
}
public void tearDown() {
if (emf == null)
return;
try {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.createQuery("delete from DetachmentOneManyChild").
executeUpdate();
em.createQuery("delete from DetachmentOneManyParent").
executeUpdate();
em.getTransaction().commit();
em.close();
emf.close();
} catch (Exception e) {
}
}
public void testAttachWithNoChangesChecksVersion() {
try {
DetachmentOneManyChild e = new DetachmentOneManyChild();
DetachmentOneManyParent p = new DetachmentOneManyParent();
e.setName("orig");
@ -91,10 +63,6 @@ try {
em.getTransaction().rollback();
em.close();
}
} catch (RuntimeException re) {
re.printStackTrace();
throw re;
}
}
public static void main(String[] args) {

View File

@ -15,16 +15,12 @@
*/
package org.apache.openjpa.persistence.detachment;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import junit.framework.TestCase;
import junit.textui.TestRunner;
import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
import org.apache.openjpa.persistence.OpenJPAPersistence;
import org.apache.openjpa.kernel.AutoDetach;
import org.apache.openjpa.persistence.OpenJPAPersistence;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
/**
* Tests detachment for bidirectional one-many relationship
@ -32,37 +28,12 @@ import org.apache.openjpa.kernel.AutoDetach;
* @author David Ezzio
*/
public class TestDetachmentOneMany
extends TestCase {
extends SingleEMFTestCase {
private OpenJPAEntityManagerFactory emf;
@SuppressWarnings("unchecked")
public void setUp() {
String types = DetachmentOneManyParent.class.getName() + ";"
+ DetachmentOneManyChild.class.getName();
Map props = new HashMap(System.getProperties());
props.put("openjpa.MetaDataFactory", "jpa(Types=" + types + ")");
emf = (OpenJPAEntityManagerFactory) Persistence.
createEntityManagerFactory("test", props);
setUp(DetachmentOneManyParent.class, DetachmentOneManyChild.class);
}
public void tearDown() {
if (emf == null)
return;
try {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.createQuery("delete from DetachmentOneManyChild").
executeUpdate();
em.createQuery("delete from DetachmentOneManyParent").
executeUpdate();
em.getTransaction().commit();
em.close();
emf.close();
} catch (Exception e) {
}
}
public void testDetachment() {
long id = createParentAndChildren();
@ -72,6 +43,7 @@ public class TestDetachmentOneMany
id);
assertNotNull(parent);
assertFalse("The parent was not detached", em.contains(parent));
em.close();
}
public void testFetchWithDetach() {

View File

@ -1,54 +1,19 @@
package org.apache.openjpa.persistence.detachment;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.persistence.RollbackException;
import org.apache.openjpa.enhance.PersistenceCapable;
import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
import org.apache.openjpa.persistence.OpenJPAPersistence;
import junit.framework.TestCase;
import org.apache.openjpa.enhance.PersistenceCapable;
import org.apache.openjpa.persistence.OpenJPAPersistence;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
public class TestGetReferenceAndImplicitDetachment
extends TestCase {
private OpenJPAEntityManagerFactory emf;
extends SingleEMFTestCase {
public void setUp() {
String types = DetachmentOneManyParent.class.getName() + ";"
+ DetachmentOneManyChild.class.getName();
Map props = new HashMap(System.getProperties());
props.put("openjpa.MetaDataFactory", "jpa(Types=" + types + ")");
props.put("openjpa.DetachState", "fgs");
emf = (OpenJPAEntityManagerFactory) Persistence.
createEntityManagerFactory("test", props);
deleteAll();
}
public void tearDown() {
if (emf == null)
return;
try {
deleteAll();
emf.close();
} catch (Exception e) {
}
}
private void deleteAll() {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.createQuery("delete from DetachmentOneManyChild").
executeUpdate();
em.createQuery("delete from DetachmentOneManyParent").
executeUpdate();
em.getTransaction().commit();
em.close();
setUp("openjpa.DetachState", "fgs",
DetachmentOneManyParent.class, DetachmentOneManyChild.class);
}
public void testNonexistentGetReferenceDetachmentInTxWithCommit() {

View File

@ -15,20 +15,16 @@
*/
package org.apache.openjpa.persistence.identity;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;
import junit.framework.TestCase;
import junit.textui.TestRunner;
import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
import org.apache.openjpa.persistence.OpenJPAEntityManager;
import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
/**
* Simple test case to test the GenerationType for @Id...
@ -36,16 +32,11 @@ import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
* @author Kevin Sutter
*/
public class TestGenerationType
extends TestCase {
private OpenJPAEntityManagerFactory emf;
extends SingleEMFTestCase {
public void setUp() {
Map props = new HashMap(System.getProperties());
props.put("openjpa.MetaDataFactory",
"jpa(Types=" + IdentityGenerationType.class.getName() + ")");
emf = (OpenJPAEntityManagerFactory) Persistence.
createEntityManagerFactory("test", props);
setUp(IdentityGenerationType.class);
/*
* If the DBDictionary doesn't support AutoAssign(ment) of column
* values, then null out the emf instance to prevent the rest of
@ -58,20 +49,6 @@ public class TestGenerationType
}
public void tearDown() {
if (emf == null)
return;
try {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.createQuery("delete from IdentityGenerationType").executeUpdate();
em.getTransaction().commit();
em.close();
emf.close();
} catch (Exception e) {
}
}
public void testCreateEntityManager() {
if (emf == null)
return;

View File

@ -16,14 +16,10 @@
package org.apache.openjpa.persistence.identity;
import java.sql.Date;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import junit.framework.TestCase;
import junit.textui.TestRunner;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
/**
* Test that entities can use SQL dates as identity values.
@ -31,29 +27,10 @@ import junit.textui.TestRunner;
* @author Abe White
*/
public class TestSQLDateId
extends TestCase {
private EntityManagerFactory emf;
extends SingleEMFTestCase {
public void setUp() {
String types = SQLDateIdEntity.class.getName();
Map props = new HashMap(System.getProperties());
props.put("openjpa.MetaDataFactory", "jpa(Types=" + types + ")");
emf = Persistence.createEntityManagerFactory("test", props);
}
public void tearDown() {
if (emf == null)
return;
try {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.createQuery("delete from SQLDateIdEntity").executeUpdate();
em.getTransaction().commit();
em.close();
emf.close();
} catch (Exception e) {
}
setUp(SQLDateIdEntity.class);
}
public void testPersist() {

View File

@ -15,14 +15,10 @@
*/
package org.apache.openjpa.persistence.inheritance;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import junit.framework.TestCase;
import junit.textui.TestRunner;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
/**
* Test that you can find a concrete subclass record when passing in its
@ -31,16 +27,10 @@ import junit.textui.TestRunner;
* @author Abe White
*/
public class TestFindAbstractClass
extends TestCase {
private EntityManagerFactory emf;
extends SingleEMFTestCase {
public void setUp() {
String types = AbstractBase.class.getName() + ";"
+ ConcreteSubclass.class.getName();
Map props = new HashMap(System.getProperties());
props.put("openjpa.MetaDataFactory", "jpa(Types=" + types + ")");
emf = Persistence.createEntityManagerFactory("test", props);
setUp(AbstractBase.class, ConcreteSubclass.class);
ConcreteSubclass e = new ConcreteSubclass();
e.setId("id");
@ -53,20 +43,6 @@ public class TestFindAbstractClass
em.close();
}
public void tearDown() {
if (emf == null)
return;
try {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.createQuery("delete from ConcreteSubclass").executeUpdate();
em.getTransaction().commit();
em.close();
emf.close();
} catch (Exception e) {
}
}
public void testFind() {
EntityManager em = emf.createEntityManager();
AbstractBase e = em.find(AbstractBase.class, "id");

View File

@ -15,14 +15,10 @@
*/
package org.apache.openjpa.persistence.inheritance;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import junit.framework.TestCase;
import junit.textui.TestRunner;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
/**
* Perform basic operations on an inheritance hierarchy involving multiple
@ -31,31 +27,11 @@ import junit.textui.TestRunner;
* @author Abe White
*/
public class TestMultipleMappedSuperclassHierarchy
extends TestCase {
private EntityManagerFactory emf;
extends SingleEMFTestCase {
public void setUp() {
String types = MappedSuperclassBase.class.getName() + ";"
+ MappedSuperclassL2.class.getName() + ";"
+ EntityL3.class.getName();
Map props = new HashMap(System.getProperties());
props.put("openjpa.MetaDataFactory", "jpa(Types=" + types + ")");
emf = Persistence.createEntityManagerFactory("test", props);
}
public void tearDown() {
if (emf == null)
return;
try {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.createQuery("delete from EntityL3").executeUpdate();
em.getTransaction().commit();
em.close();
emf.close();
} catch (Exception e) {
}
setUp(MappedSuperclassBase.class, MappedSuperclassL2.class,
EntityL3.class);
}
public void testPersist() {

View File

@ -15,14 +15,10 @@
*/
package org.apache.openjpa.persistence.inheritance;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import junit.framework.TestCase;
import junit.textui.TestRunner;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
/**
* Test that sibling classes with a shared id value declared in their
@ -31,18 +27,11 @@ import junit.textui.TestRunner;
* @author Abe White
*/
public class TestSharedMappedSuperclassIdValue
extends TestCase {
private EntityManagerFactory emf;
extends SingleEMFTestCase {
public void setUp() {
String types = MappedSuperclassBase.class.getName() + ";"
+ MappedSuperclassL2.class.getName() + ";"
+ EntityL3.class.getName() + ";"
+ EntityL3Sibling.class.getName();
Map props = new HashMap(System.getProperties());
props.put("openjpa.MetaDataFactory", "jpa(Types=" + types + ")");
emf = Persistence.createEntityManagerFactory("test", props);
setUp(MappedSuperclassBase.class, MappedSuperclassL2.class,
EntityL3.class, EntityL3Sibling.class);
EntityL3 ent = new EntityL3();
ent.setId(1);
@ -59,22 +48,6 @@ public class TestSharedMappedSuperclassIdValue
em.persist(sib);
em.getTransaction().commit();
em.close();
}
public void tearDown() {
if (emf == null)
return;
try {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.createQuery("delete from EntityL3").executeUpdate();
em.createQuery("delete from EntityL3Sibling").executeUpdate();
em.getTransaction().commit();
em.close();
emf.close();
} catch (Exception e) {
}
}
public void testFind() {
@ -108,7 +81,6 @@ public class TestSharedMappedSuperclassIdValue
em.close();
}
public static void main(String[] args) {
TestRunner.run(TestSharedMappedSuperclassIdValue.class);
}

View File

@ -28,12 +28,13 @@ import org.apache.openjpa.persistence.test.*;
*
* @author Marc Prud'hommeaux
*/
public abstract class CompanyModelTest extends SingleEMTest {
public abstract class CompanyModelTest
extends SingleEMTestCase {
private static Map<Class,Class> factoryClasses;
private Map<Class,Class> impls;
private final Map<Class,Class> impls;
public CompanyModelTest() {
public void setUp() {
// make a map of the implementations based on the class names in
// the current package of the test subclass
impls = new HashMap<Class,Class>();
@ -48,7 +49,8 @@ public abstract class CompanyModelTest extends SingleEMTest {
impls.put(IPartTimeEmployee.class, localClass("PartTimeEmployee"));
impls.put(IProduct.class, localClass("Product"));
classes = (Class[]) impls.values().toArray(new Class[impls.size()]);
setUp(impls.values().toArray(new Class[impls.size()]));
checkModel();
}
private Class localClass(String name) {
@ -60,16 +62,6 @@ public abstract class CompanyModelTest extends SingleEMTest {
}
}
public void setUp() throws Exception {
super.setUp();
try {
checkModel();
} catch (Exception e) {
closeEMF();
throw e;
}
}
/**
* Runs through basic queries against all of the properties of all
* of the known persistent classes. We're just checking here to
@ -174,7 +166,7 @@ public abstract class CompanyModelTest extends SingleEMTest {
verifyModel();
} catch (AssertionFailedError e) {
// clear all existing instances
delete(impls.values().toArray(new Class[0]));
clear(emf, impls.values().toArray(new Class[impls.size()]));
// since the factory method needs to be static, we need to store
// the classes statically

View File

@ -15,16 +15,12 @@
*/
package org.apache.openjpa.persistence.query;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import junit.framework.TestCase;
import junit.textui.TestRunner;
import org.apache.openjpa.persistence.simple.NamedEntity;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
/**
* Test that we can query by an entity's abstract schema name.
@ -32,15 +28,10 @@ import org.apache.openjpa.persistence.simple.NamedEntity;
* @author Abe White
*/
public class TestAbstractSchemaName
extends TestCase {
private EntityManagerFactory emf;
extends SingleEMFTestCase {
public void setUp() {
Map props = new HashMap(System.getProperties());
props.put("openjpa.MetaDataFactory", "jpa(Types="
+ NamedEntity.class.getName() + ")");
emf = Persistence.createEntityManagerFactory("test", props);
setUp(NamedEntity.class);
NamedEntity e = new NamedEntity();
e.setName("e");
@ -52,21 +43,6 @@ public class TestAbstractSchemaName
em.close();
}
public void tearDown() {
if (emf == null)
return;
try {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.createQuery("delete from named").executeUpdate();
em.getTransaction().commit();
em.close();
emf.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void testQuery() {
EntityManager em = emf.createEntityManager();
Query q = em.createQuery("select e from named e");

View File

@ -15,33 +15,18 @@
*/
package org.apache.openjpa.persistence.query;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.FlushModeType;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.apache.openjpa.persistence.OpenJPAQuery;
import org.apache.openjpa.persistence.simple.AllFieldTypes;
import junit.framework.TestCase;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
public class TestInMemoryQueryMatchEscapes
extends TestCase {
private EntityManagerFactory emf;
extends SingleEMFTestCase {
public void setUp() {
Map options = new HashMap(System.getProperties());
// ensure that OpenJPA knows about our type, so that
// auto-schema-creation works
options.put("openjpa.MetaDataFactory",
"jpa(Types=" + AllFieldTypes.class.getName() + ")");
emf = Persistence.createEntityManagerFactory("test", options);
setUp(AllFieldTypes.class);
}
public void testDatabaseEscape() {

View File

@ -15,17 +15,13 @@
*/
package org.apache.openjpa.persistence.query;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import junit.framework.TestCase;
import junit.textui.TestRunner;
import org.apache.openjpa.persistence.OpenJPAQuery;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
/**
* Test that we can exclude subclass instances from query results.
@ -33,16 +29,10 @@ import org.apache.openjpa.persistence.OpenJPAQuery;
* @author Abe White
*/
public class TestQueryExcludingSubclasses
extends TestCase {
private EntityManagerFactory emf;
extends SingleEMFTestCase {
public void setUp() {
Map props = new HashMap(System.getProperties());
props.put("openjpa.MetaDataFactory", "jpa(Types="
+ ManyOneEntity.class.getName() + ";"
+ ManyOneEntitySub.class.getName() + ")");
emf = Persistence.createEntityManagerFactory("test", props);
setUp(ManyOneEntity.class, ManyOneEntitySub.class);
ManyOneEntity e1 = new ManyOneEntity();
e1.setName("e1");
@ -69,21 +59,6 @@ public class TestQueryExcludingSubclasses
em.close();
}
public void tearDown() {
if (emf == null)
return;
try {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.createQuery("delete from ManyOneEntity").executeUpdate();
em.getTransaction().commit();
em.close();
emf.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void testQuery() {
EntityManager em = emf.createEntityManager();
Query q = em.createQuery("select e from ManyOneEntity e "
@ -115,7 +90,6 @@ public class TestQueryExcludingSubclasses
em.close();
}
public static void main(String[] args) {
TestRunner.run(TestQueryExcludingSubclasses.class);
}

View File

@ -15,16 +15,12 @@
*/
package org.apache.openjpa.persistence.query;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import junit.framework.TestCase;
import junit.textui.TestRunner;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
/**
* Test that querying the id of a related many-one (or one-one) does not create
@ -33,17 +29,12 @@ import junit.textui.TestRunner;
* @author Abe White
*/
public class TestQueryIdOfRelationDoesNotJoin
extends TestCase {
extends SingleEMFTestCase {
private EntityManagerFactory emf;
private long e3Id;
public void setUp() {
Map props = new HashMap(System.getProperties());
props.put("openjpa.MetaDataFactory", "jpa(Types="
+ ManyOneEntity.class.getName() + ";"
+ ManyOneEntitySub.class.getName() + ")");
emf = Persistence.createEntityManagerFactory("test", props);
setUp(ManyOneEntity.class, ManyOneEntitySub.class);
ManyOneEntity e1 = new ManyOneEntity();
e1.setName("e1");
@ -67,21 +58,6 @@ public class TestQueryIdOfRelationDoesNotJoin
em.close();
}
public void tearDown() {
if (emf == null)
return;
try {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.createQuery("delete from ManyOneEntity").executeUpdate();
em.getTransaction().commit();
em.close();
emf.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void testQuery() {
EntityManager em = emf.createEntityManager();
Query q = em.createQuery("select e from ManyOneEntity e "

View File

@ -15,28 +15,19 @@
*/
package org.apache.openjpa.persistence.query;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import junit.framework.TestCase;
import junit.textui.TestRunner;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
public class TestResultSetMapping extends TestCase {
private EntityManagerFactory emf;
public class TestResultSetMapping
extends SingleEMFTestCase {
public void setUp() {
Map props = new HashMap();
props.put("openjpa.MetaDataFactory", "jpa(Types=" +
org.apache.openjpa.persistence.query.SimpleEntity.class.getName() + ")");
emf = Persistence.createEntityManagerFactory("test", props);
setUp(SimpleEntity.class);
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
@ -45,21 +36,6 @@ public class TestResultSetMapping extends TestCase {
em.close();
}
public void tearDown() {
if (emf == null)
return;
try {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.createQuery("delete from simple").executeUpdate();
em.getTransaction().commit();
em.close();
emf.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void testSimpleQuery() {
EntityManager em = emf.createEntityManager();
Query q = em.createNamedQuery("findSimpleEntitites");
@ -74,4 +50,4 @@ public class TestResultSetMapping extends TestCase {
public static void main(String[] args) {
TestRunner.run(TestResultSetMapping.class);
}
}
}

View File

@ -15,17 +15,13 @@
*/
package org.apache.openjpa.persistence.relations;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import junit.framework.TestCase;
import junit.textui.TestRunner;
import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
import org.apache.openjpa.persistence.OpenJPAEntityManager;
import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
/**
* Tests a cascading one-many backed by a foreign key.
@ -33,33 +29,10 @@ import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
* @author Abe White
*/
public class TestCascadingOneManyWithForeignKey
extends TestCase {
private OpenJPAEntityManagerFactory emf;
extends SingleEMFTestCase {
public void setUp() {
String types = CascadingOneManyParent.class.getName() + ";"
+ CascadingOneManyChild.class.getName();
Map props = new HashMap(System.getProperties());
props.put("openjpa.MetaDataFactory", "jpa(Types=" + types + ")");
emf = (OpenJPAEntityManagerFactory) Persistence.
createEntityManagerFactory("test", props);
}
public void tearDown() {
if (emf == null)
return;
try {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.createQuery("delete from CascadingOneManyChild").executeUpdate();
em.createQuery("delete from CascadingOneManyParent").
executeUpdate();
em.getTransaction().commit();
em.close();
emf.close();
} catch (Exception e) {
}
setUp(CascadingOneManyParent.class, CascadingOneManyChild.class);
}
public void testPersist() {

View File

@ -15,15 +15,10 @@
*/
package org.apache.openjpa.persistence.relations;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import junit.framework.TestCase;
import junit.textui.TestRunner;
import org.apache.openjpa.persistence.OpenJPAEntityManager;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
/**
* Test ordering a one-many field on the primary key of the related entity.
@ -31,17 +26,12 @@ import org.apache.openjpa.persistence.OpenJPAEntityManager;
* @author Abe White
*/
public class TestIdOrderedOneMany
extends TestCase {
extends SingleEMFTestCase {
private EntityManagerFactory emf;
private long id;
public void setUp() {
Map props = new HashMap(System.getProperties());
props.put("openjpa.MetaDataFactory", "jpa(Types="
+ IdOrderedOneManyParent.class.getName() + ";"
+ IdOrderedOneManyChild.class.getName() + ")");
emf = Persistence.createEntityManagerFactory("test", props);
setUp(IdOrderedOneManyParent.class, IdOrderedOneManyChild.class);
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
@ -71,22 +61,6 @@ public class TestIdOrderedOneMany
em.close();
}
public void tearDown() {
if (emf == null)
return;
try {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.createQuery("delete from IdOrderedOneManyChild").executeUpdate();
em.createQuery("delete from IdOrderedOneManyParent").
executeUpdate();
em.getTransaction().commit();
em.close();
emf.close();
} catch (Exception e) {
}
}
public void testExplicitOrdering() {
EntityManager em = emf.createEntityManager();
IdOrderedOneManyParent parent = em.find(IdOrderedOneManyParent.class,

View File

@ -15,16 +15,12 @@
*/
package org.apache.openjpa.persistence.relations;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import junit.framework.TestCase;
import junit.textui.TestRunner;
import org.apache.openjpa.persistence.OpenJPAEntityManager;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
/**
* Perform basic operations on an entity with a many-one relation as its id
@ -33,22 +29,16 @@ import org.apache.openjpa.persistence.OpenJPAEntityManager;
* @author Abe White
*/
public class TestManyOneAsId
extends TestCase {
extends SingleEMFTestCase {
private EntityManagerFactory emf;
private long id;
private long dsid;
private long cid;
public void setUp() {
Map props = new HashMap(System.getProperties());
props.put("openjpa.MetaDataFactory", "jpa(Types="
+ BasicEntity.class.getName() + ";"
+ DataStoreBasicEntity.class.getName() + ";"
+ ManyOneIdOwner.class.getName() + ";"
+ DataStoreManyOneIdOwner.class.getName() + ";"
+ ManyOneCompoundIdOwner.class.getName() + ")");
emf = Persistence.createEntityManagerFactory("test", props);
setUp(BasicEntity.class, DataStoreBasicEntity.class,
ManyOneIdOwner.class, DataStoreManyOneIdOwner.class,
ManyOneCompoundIdOwner.class);
BasicEntity id1 = new BasicEntity();
id1.setName("id1");
@ -108,26 +98,6 @@ public class TestManyOneAsId
em.close();
}
public void tearDown() {
if (emf == null)
return;
try {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.createQuery("delete from ManyOneIdOwner").executeUpdate();
em.createQuery("delete from DataStoreManyOneIdOwner").
executeUpdate();
em.createQuery("delete from ManyOneCompoundIdOwner").
executeUpdate();
em.createQuery("delete from BasicEntity").executeUpdate();
em.createQuery("delete from DataStoreBasicEntity").executeUpdate();
em.getTransaction().commit();
em.close();
emf.close();
} catch (Exception e) {
}
}
private void assertRelations(EntityManager em, ManyOneIdOwner parent) {
assertEquals("parent", parent.getName());
BasicEntity id1 = parent.getId();

View File

@ -15,16 +15,12 @@
*/
package org.apache.openjpa.persistence.relations;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import junit.framework.TestCase;
import junit.textui.TestRunner;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
/**
* Test that querying and retrieving entities with multiple same-typed embedded
@ -33,16 +29,10 @@ import junit.textui.TestRunner;
* @author Abe White
*/
public class TestMultipleSameTypedEmbeddedWithEagerRelations
extends TestCase {
private EntityManagerFactory emf;
extends SingleEMFTestCase {
public void setUp() {
Map props = new HashMap(System.getProperties());
props.put("openjpa.MetaDataFactory", "jpa(Types="
+ EmbeddableWithRelation.class.getName() + ";"
+ MultipleSameTypedEmbedded.class.getName() + ")");
emf = Persistence.createEntityManagerFactory("test", props);
setUp(EmbeddableWithRelation.class, MultipleSameTypedEmbedded.class);
EmbeddableWithRelation embed1 = new EmbeddableWithRelation();
embed1.setName("embed1");
@ -69,22 +59,6 @@ public class TestMultipleSameTypedEmbeddedWithEagerRelations
em.close();
}
public void tearDown() {
if (emf == null)
return;
try {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.createQuery("delete from MultipleSameTypedEmbedded").
executeUpdate();
em.getTransaction().commit();
em.close();
emf.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void testQuery() {
EntityManager em = emf.createEntityManager();
Query q = em.createQuery("select m from MultipleSameTypedEmbedded m "

View File

@ -15,14 +15,10 @@
*/
package org.apache.openjpa.persistence.relations;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import junit.framework.TestCase;
import junit.textui.TestRunner;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
/**
* Perform basic operations on an entity with interface relations that use
@ -31,30 +27,10 @@ import junit.textui.TestRunner;
* @author Abe White
*/
public class TestTargetedIFaceRelations
extends TestCase {
private EntityManagerFactory emf;
extends SingleEMFTestCase {
public void setUp() {
Map props = new HashMap(System.getProperties());
props.put("openjpa.MetaDataFactory", "jpa(Types="
+ TargetedIFaceRelationParent.class.getName() + ")");
emf = Persistence.createEntityManagerFactory("test", props);
}
public void tearDown() {
if (emf == null)
return;
try {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.createQuery("delete from TargetedIFaceRelationParent").
executeUpdate();
em.getTransaction().commit();
em.close();
emf.close();
} catch (Exception e) {
}
setUp(TargetedIFaceRelationParent.class);
}
public void testPersist() {

View File

@ -1,15 +1,30 @@
/*
* Copyright 2006 The Apache Software Foundation.
*
* Licensed 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.persistence.simple;
import javax.persistence.Query;
import javax.persistence.EntityManager;
import org.apache.openjpa.persistence.test.PersistenceTestCase;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
public class TestCaseInsensitiveKeywordsInJPQL
extends PersistenceTestCase {
extends SingleEMFTestCase {
public Class[] getEntityTypes() {
return new Class[] { AllFieldTypes.class };
public void setUp() {
setUp(AllFieldTypes.class);
}
public void testCaseInsensitiveBooleans() {
@ -38,4 +53,4 @@ public class TestCaseInsensitiveKeywordsInJPQL
em.getTransaction().rollback();
}
}
}

View File

@ -15,14 +15,10 @@
*/
package org.apache.openjpa.persistence.simple;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import junit.framework.TestCase;
import junit.textui.TestRunner;
import org.apache.openjpa.persistence.test.SingleEMTestCase;
/**
* Test case to ensure that the proper JPA clear semantics are processed.
@ -30,71 +26,46 @@ import junit.textui.TestRunner;
* @author Kevin Sutter
*/
public class TestEntityManagerClear
extends TestCase {
private EntityManagerFactory emf;
private EntityManager em;
extends SingleEMTestCase {
public void setUp() {
Map props = new HashMap(System.getProperties());
props.put("openjpa.MetaDataFactory",
"jpa(Types=" + AllFieldTypes.class.getName() + ")");
emf = Persistence.createEntityManagerFactory("test", props);
setUp(AllFieldTypes.class);
}
public void tearDown() {
if (emf == null)
return;
try {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.createQuery("delete from AllFieldTypes").executeUpdate();
em.getTransaction().commit();
em.close();
emf.close();
} catch (Exception e) {
}
}
public void testClear() {
try {
// Create EntityManager and Start a transaction (1)
em = emf.createEntityManager();
em.getTransaction().begin();
// Create EntityManager and Start a transaction (1)
begin();
// Insert a new object and flush
AllFieldTypes testObject1 = new AllFieldTypes();
testObject1.setStringField("my test object1");
em.persist(testObject1);
em.flush();
// Insert a new object and flush
AllFieldTypes testObject1 = new AllFieldTypes();
testObject1.setStringField("my test object1");
persist(testObject1);
em.flush();
// Clear the PC for new object 2
AllFieldTypes testObject2 = new AllFieldTypes();
testObject1.setStringField("my test object2");
em.persist(testObject2);
em.clear();
// Clear the PC for new object 2
AllFieldTypes testObject2 = new AllFieldTypes();
testObject1.setStringField("my test object2");
persist(testObject2);
em.clear();
// Commit the transaction (only object 1 should be in database)
em.getTransaction().commit();
// Commit the transaction (only object 1 should be in database)
commit();
// Start a new transaction
em.getTransaction().begin();
// Start a new transaction
begin();
// Attempt retrieve of Object1 from previous PC (should exist)
assertEquals(1, em.createQuery
("select x from AllFieldTypes x where x.stringField = 'my test object1'").
getResultList().size());
// Attempt retrieve of Object1 from previous PC (should exist)
assertEquals(1, query("select x from AllFieldTypes x "
+ "where x.stringField = 'my test object1'").
getResultList().size());
// Attempt retrieve of Object2 from previous PC (should not exist)
assertEquals(0, em.createQuery
("select x from AllFieldTypes x where x.stringField = 'my test object2'").
getResultList().size());
// Attempt retrieve of Object2 from previous PC (should not exist)
assertEquals(0, query("select x from AllFieldTypes x "
+ "where x.stringField = 'my test object2'").
getResultList().size());
// Rollback the transaction and close everything
em.getTransaction().rollback();
em.close();
} catch (Exception ex) {
fail("Unexpected Exception ex = " + ex);
}
// Rollback the transaction and close everything
rollback();
}
public static void main(String[] args) {

View File

@ -15,16 +15,12 @@
*/
package org.apache.openjpa.persistence.simple;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.FlushModeType;
import javax.persistence.LockModeType;
import javax.persistence.Persistence;
import junit.framework.TestCase;
import junit.textui.TestRunner;
import org.apache.openjpa.persistence.test.SingleEMTestCase;
/**
* Negative test case to verify that EntityManager throws required exceptions
@ -33,34 +29,15 @@ import junit.textui.TestRunner;
* @author Craig Russell
*/
public class TestEntityManagerMethodsThrowAfterClose
extends TestCase {
extends SingleEMTestCase {
private EntityManagerFactory emf;
private EntityManager em;
private AllFieldTypes aft = new AllFieldTypes();
public void setUp() {
Map props = new HashMap(System.getProperties());
props.put("openjpa.MetaDataFactory",
"jpa(Types=" + AllFieldTypes.class.getName() + ")");
emf = Persistence.createEntityManagerFactory("test", props);
em = emf.createEntityManager();
em.close();
setUp(AllFieldTypes.class);
close();
}
public void tearDown() {
if (emf == null)
return;
try {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.createQuery("delete from AllFieldTypes").executeUpdate();
em.getTransaction().commit();
em.close();
emf.close();
} catch (Exception e) {
}
}
public void testPersistAfterClose() {
try {
em.persist(aft);

View File

@ -15,16 +15,12 @@
*/
package org.apache.openjpa.persistence.simple;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import junit.framework.TestCase;
import junit.textui.TestRunner;
import org.apache.openjpa.persistence.OpenJPAEntityManager;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
/**
* Simple test case to get an EntityManager and perform some basic operations.
@ -32,29 +28,10 @@ import org.apache.openjpa.persistence.OpenJPAEntityManager;
* @author Marc Prud'hommeaux
*/
public class TestPersistence
extends TestCase {
private EntityManagerFactory emf;
extends SingleEMFTestCase {
public void setUp() {
Map props = new HashMap(System.getProperties());
props.put("openjpa.MetaDataFactory",
"jpa(Types=" + AllFieldTypes.class.getName() + ")");
emf = Persistence.createEntityManagerFactory("test", props);
}
public void tearDown() {
if (emf == null)
return;
try {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.createQuery("delete from AllFieldTypes").executeUpdate();
em.getTransaction().commit();
em.close();
emf.close();
} catch (Exception e) {
}
setUp(AllFieldTypes.class);
}
public void testCreateEntityManager() {

View File

@ -1,20 +1,19 @@
package org.apache.openjpa.persistence.simple;
import org.apache.openjpa.persistence.OpenJPAPersistence;
import org.apache.openjpa.persistence.test.PersistenceTestCase;
import org.apache.openjpa.jdbc.meta.ClassMapping;
import org.apache.openjpa.persistence.OpenJPAPersistence;
import org.apache.openjpa.persistence.test.SingleEMFTestCase;
public class TestTableNamesDefaultToEntityNames
extends PersistenceTestCase {
extends SingleEMFTestCase {
@Override
protected Class[] getEntityTypes() {
return new Class[] { NamedEntity.class };
public void setUp() {
setUp(NamedEntity.class);
}
public void testEntityNames() {
ClassMapping cm = (ClassMapping) OpenJPAPersistence.getMetaData(
getEntityManagerFactory(), NamedEntity.class);
emf, NamedEntity.class);
assertEquals("named", cm.getTable().getName());
}
}

View File

@ -1,39 +1,150 @@
package org.apache.openjpa.persistence.test;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import junit.framework.TestCase;
import org.apache.openjpa.kernel.AbstractBrokerFactory;
import org.apache.openjpa.kernel.Broker;
import org.apache.openjpa.meta.ClassMetaData;
import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
import org.apache.openjpa.persistence.OpenJPAPersistence;
/**
* Base test class providing persistence utilities.
*/
public abstract class PersistenceTestCase
extends TestCase {
protected OpenJPAEntityManagerFactory emf;
/**
* Marker object you an pass to {@link #setUp} to indicate that the
* database tables should be cleared.
*/
protected static final Object CLEAR_TABLES = new Object();
protected Class[] getEntityTypes() {
return new Class[0];
}
/**
* Create an entity manager factory.
*
* @param props list of persistent types used in testing and/or
* configuration values in the form key,value,key,value...
*/
protected OpenJPAEntityManagerFactory createEMF(Object... props) {
Map map = new HashMap(System.getProperties());
List<Class> types = new ArrayList<Class>();
boolean prop = false;
for (int i = 0; i < props.length; i++) {
if (prop) {
map.put(props[i - 1], props[i]);
prop = false;
} else if (props[i] == CLEAR_TABLES) {
map.put("openjpa.jdbc.SynchronizeMappings",
"buildSchema(ForeignKeys=true,"
+ "SchemaAction='add,deleteTableContents')");
} else if (props[i] instanceof Class)
types.add((Class) props[i]);
else
prop = true;
}
public void setUp() {
Map props = new HashMap(System.getProperties());
Class[] types = getEntityTypes();
if (types != null && types.length > 0) {
if (!types.isEmpty()) {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < types.length; i++) {
buf.append(types[i].getName());
if (i != types.length - 1)
buf.append(",");
for (Class c : types) {
if (buf.length() > 0)
buf.append(";");
buf.append(c.getName());
}
props.put("openjpa.MetaDataFactory",
map.put("openjpa.MetaDataFactory",
"jpa(Types=" + buf.toString() + ")");
}
emf = (OpenJPAEntityManagerFactory)
Persistence.createEntityManagerFactory("test", props);
return (OpenJPAEntityManagerFactory) Persistence.
createEntityManagerFactory("test", map);
}
public OpenJPAEntityManagerFactory getEntityManagerFactory() {
return emf;
/**
* Safely close the given factory.
*/
protected boolean closeEMF(EntityManagerFactory emf) {
if (emf == null)
return false;
if (!emf.isOpen())
return false;
for (Iterator iter = ((AbstractBrokerFactory) OpenJPAPersistence
.toBrokerFactory(emf)).getOpenBrokers().iterator();
iter.hasNext(); ) {
Broker b = (Broker) iter.next();
if (b != null && !b.isClosed()) {
EntityManager em = OpenJPAPersistence.toEntityManager(b);
if (em.getTransaction().isActive())
em.getTransaction().rollback();
em.close();
}
}
emf.close();
return !emf.isOpen();
}
/**
* Delete all instances of the given types using bulk delete queries.
*/
protected void clear(EntityManagerFactory emf, Class... types) {
if (emf == null || types.length == 0)
return;
List<ClassMetaData> metas = new ArrayList<ClassMetaData>(types.length);
for (Class c : types) {
ClassMetaData meta = OpenJPAPersistence.getMetaData(emf, c);
if (meta != null)
metas.add(meta);
}
clear(emf, metas.toArray(new ClassMetaData[metas.size()]));
}
/**
* Delete all instances of the persistent types registered with the given
* factory using bulk delete queries.
*/
protected void clear(EntityManagerFactory emf) {
if (emf == null)
return;
clear(emf, ((OpenJPAEntityManagerFactory) emf).getConfiguration().
getMetaDataRepositoryInstance().getMetaDatas());
}
/**
* Delete all instances of the given types using bulk delete queries.
*/
private void clear(EntityManagerFactory emf, ClassMetaData... types) {
if (emf == null || types.length == 0)
return;
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
for (ClassMetaData meta : types) {
if (!meta.isMapped() || meta.isEmbeddedOnly()
|| Modifier.isAbstract(meta.getDescribedType().getModifiers()))
continue;
em.createQuery("DELETE FROM " + meta.getTypeAlias() + " o").
executeUpdate();
}
em.getTransaction().commit();
em.close();
}
/**
* Return the entity name for the given type.
*/
protected String entityName(EntityManagerFactory emf, Class c) {
ClassMetaData meta = OpenJPAPersistence.getMetaData(emf, c);
return (meta == null) ? null : meta.getTypeAlias();
}
}

View File

@ -0,0 +1,72 @@
package org.apache.openjpa.persistence.test;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import org.apache.openjpa.lib.jdbc.AbstractJDBCListener;
import org.apache.openjpa.lib.jdbc.JDBCEvent;
import org.apache.openjpa.lib.jdbc.JDBCListener;
/**
* Base class for tests that need to check generated SQL.
*
* @author Patrick Linskey
*/
public abstract class SQLListenerTestCase
extends SingleEMFTestCase {
protected List<String> sql = new ArrayList<String>();
@Override
public void setUp(Object... props) {
Object[] copy = new Object[props.length + 2];
System.arraycopy(props, 0, copy, 0, props.length);
copy[copy.length - 2] = "openjpa.jdbc.JDBCListeners";
copy[copy.length - 1] = new JDBCListener[] { new Listener() };
super.setUp(copy);
}
/**
* Confirm that the specified SQL has been executed.
*
* @param sqlExp the SQL expression. E.g., "SELECT FOO .*"
*/
public void assertSQL(String sqlExp) {
for (String statement : sql) {
if (statement.matches(sqlExp))
return;
}
fail("Expected regular expression <" + sqlExp + "> to have"
+ " existed in SQL statements: " + sql);
}
/**
* Confirm that the specified SQL has not been executed.
*
* @param sqlExp the SQL expression. E.g., "SELECT BADCOLUMN .*"
*/
public void assertNotSQL(String sqlExp) {
boolean failed = false;
for (String statement : sql) {
if (statement.matches(sqlExp))
failed = true;
}
if (failed)
fail("Regular expression <" + sqlExp + ">"
+ " should not have been executed in SQL statements: " + sql);
}
public class Listener
extends AbstractJDBCListener {
@Override
public void beforeExecuteStatement(JDBCEvent event) {
if (event.getSQL() != null && sql != null)
sql.add(event.getSQL());
}
}
}

View File

@ -1,105 +0,0 @@
package org.apache.openjpa.persistence.test;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.EntityManager;
import junit.framework.TestCase;
import org.apache.openjpa.persistence.OpenJPAPersistence;
import org.apache.openjpa.kernel.AbstractBrokerFactory;
import org.apache.openjpa.kernel.Broker;
public abstract class SingleEMFTest extends TestCase {
protected EntityManagerFactory emf;
protected Class[] classes;
public SingleEMFTest(Class... classes) {
this.classes = classes;
}
/**
* Can be overridden to return a list of classes that will be used
* for this test.
*/
protected Class[] getClasses() {
return classes;
}
/**
* Modify the properties that are used to create the EntityManagerFactory.
* By default, this will set up the MetaDataFactory with the
* persistent classes for this test case. This method can be overridden
* to add more properties to the map.
*/
protected void setEMFProps(Map props) {
// if we have specified a list of persistent classes to examine,
// then set it in the MetaDataFactory so that our automatic
// schema generation will work.
Class[] pclasses = getClasses();
if (pclasses != null) {
StringBuilder str = new StringBuilder();
for (Class c : pclasses)
str.append(str.length() > 0 ? ";" : "").append(c.getName());
props.put("openjpa.MetaDataFactory", "jpa(Types=" + str + ")");
}
if (clearDatabaseInSetUp()) {
props.put("openjpa.jdbc.SynchronizeMappings",
"buildSchema(ForeignKeys=true," +
"SchemaAction='add,deleteTableContents')");
}
}
protected boolean clearDatabaseInSetUp() {
return false;
}
public EntityManagerFactory emf() {
return emf;
}
public boolean closeEMF() {
if (emf == null)
return false;
if (!emf.isOpen())
return false;
for (Iterator iter = ((AbstractBrokerFactory) OpenJPAPersistence
.toBrokerFactory(emf)).getOpenBrokers().iterator();
iter.hasNext(); ) {
Broker b = (Broker) iter.next();
if (b != null && !b.isClosed()) {
EntityManager em = OpenJPAPersistence.toEntityManager(b);
if (em.getTransaction().isActive())
em.getTransaction().rollback();
em.close();
}
}
emf.close();
return !emf.isOpen();
}
@Override
public void setUp() throws Exception {
super.setUp();
Map props = new HashMap(System.getProperties());
setEMFProps(props);
emf = Persistence.createEntityManagerFactory("test", props);
if (clearDatabaseInSetUp()) // get an EM to trigger schema manipulations
emf.createEntityManager().close();
}
@Override
public void tearDown() throws Exception {
closeEMF();
super.tearDown();
}
}

View File

@ -0,0 +1,38 @@
package org.apache.openjpa.persistence.test;
import java.util.Map;
import java.util.HashMap;
import javax.persistence.Persistence;
import junit.framework.TestCase;
import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
public abstract class SingleEMFTestCase
extends PersistenceTestCase {
protected OpenJPAEntityManagerFactory emf;
/**
* Initialize entity manager factory.
*
* @param props list of persistent types used in testing and/or
* configuration values in the form key,value,key,value...
*/
protected void setUp(Object... props) {
emf = createEMF(props);
}
/**
* Closes the entity manager factory.
*/
public void tearDown() {
if (emf == null)
return;
try {
clear(emf);
} finally {
closeEMF(emf);
}
}
}

View File

@ -15,9 +15,12 @@
*/
package org.apache.openjpa.persistence.test;
import java.util.*;
import java.util.Collections;
import java.util.List;
import javax.persistence.EntityTransaction;
import javax.persistence.*;
import org.apache.openjpa.persistence.OpenJPAEntityManager;
import org.apache.openjpa.persistence.OpenJPAQuery;
/**
* A base test case that can be used to easily test scenarios where there
@ -25,42 +28,30 @@ import javax.persistence.*;
*
* @author Marc Prud'hommeaux
*/
public abstract class SingleEMTest extends SingleEMFTest {
public abstract class SingleEMTestCase
extends SingleEMFTestCase {
protected EntityManager em;
protected OpenJPAEntityManager em;
public SingleEMTest(Class... classes) {
super(classes);
@Override
public void setUp(Object... props) {
super.setUp(props);
em = emf.createEntityManager();
}
/**
* Rolls back the current transaction and closes the EntityManager.
*/
@Override
public void tearDown() throws Exception {
public void tearDown() {
rollback();
close();
super.tearDown();
}
/**
* Returns the current EntityManager, creating one from the
* EntityManagerFactory if it doesn't already exist.
*/
public EntityManager em() {
if (em == null) {
em = emf().createEntityManager();
}
return em;
}
/**
* Start a new transaction if there isn't currently one active.
* @return true if a transaction was started, false if one already existed
*/
public boolean begin() {
EntityTransaction tx = em().getTransaction();
protected boolean begin() {
EntityTransaction tx = em.getTransaction();
if (tx.isActive())
return false;
@ -72,8 +63,8 @@ public abstract class SingleEMTest extends SingleEMFTest {
* Commit the current transaction, if it is active.
* @return true if the transaction was committed
*/
public boolean commit() {
EntityTransaction tx = em().getTransaction();
protected boolean commit() {
EntityTransaction tx = em.getTransaction();
if (!tx.isActive())
return false;
@ -85,8 +76,8 @@ public abstract class SingleEMTest extends SingleEMFTest {
* Rollback the current transaction, if it is active.
* @return true if the transaction was rolled back
*/
public boolean rollback() {
EntityTransaction tx = em().getTransaction();
protected boolean rollback() {
EntityTransaction tx = em.getTransaction();
if (!tx.isActive())
return false;
@ -98,7 +89,7 @@ public abstract class SingleEMTest extends SingleEMFTest {
* Closes the current EntityManager if it is open.
* @return false if the EntityManager was already closed.
*/
public boolean close() {
protected boolean close() {
if (em == null)
return false;
@ -111,41 +102,18 @@ public abstract class SingleEMTest extends SingleEMFTest {
return !em.isOpen();
}
@Override
public boolean closeEMF() {
close();
return super.closeEMF();
}
/**
* Returns the entity name of the specified class. If the class
* declares an @Entity, then it will be used, otherwise the base
* name of the class will be returned.
*
* Note that this will not correctly return the entity name of
* a class declared in an orm.xml file.
*/
public String entityName(Class c) {
Entity e = (Entity) c.getAnnotation(Entity.class);
if (e != null && e.name() != null && e.name().length() > 0)
return e.name();
String name = c.getSimpleName();
name = name.substring(name.lastIndexOf(".") + 1);
return name;
}
/**
* Delete all of the instances.
*
* If no transaction is running, then one will be started and committed.
* Otherwise, the operation will take place in the current transaction.
*/
public void remove(Object... obs) {
protected void remove(Object... obs) {
boolean tx = begin();
for (Object ob : obs)
em().remove(ob);
if (tx) commit();
em.remove(ob);
if (tx)
commit();
}
/**
@ -154,18 +122,19 @@ public abstract class SingleEMTest extends SingleEMFTest {
* If no transaction is running, then one will be started and committed.
* Otherwise, the operation will take place in the current transaction.
*/
public void persist(Object... obs) {
protected void persist(Object... obs) {
boolean tx = begin();
for (Object ob : obs)
em().persist(ob);
if (tx) commit();
em.persist(ob);
if (tx)
commit();
}
/**
* Creates a query in the current EntityManager with the specified string.
*/
public Query query(String str) {
return em().createQuery(str);
protected OpenJPAQuery query(String str) {
return em.createQuery(str);
}
/**
@ -178,10 +147,10 @@ public abstract class SingleEMTest extends SingleEMFTest {
* @param params the parameters, if any
* @return the Query object
*/
public Query query(Class c, String str, Object... params) {
String query = "select x from " + entityName(c) + " x "
protected OpenJPAQuery query(Class c, String str, Object... params) {
String query = "select x from " + entityName(emf, c) + " x "
+ (str == null ? "" : str);
Query q = em().createQuery(query);
OpenJPAQuery q = em.createQuery(query);
for (int i = 0; params != null && i < params.length; i++)
q.setParameter(i + 1, params[i]);
return q;
@ -196,31 +165,14 @@ public abstract class SingleEMTest extends SingleEMFTest {
*
* @see #query(java.lang.Class,java.lang.String)
*/
public <E> List<E> find(Class<E> c, String q, Object... params) {
protected <E> List<E> find(Class<E> c, String q, Object... params) {
return Collections.checkedList(query(c, q, params).getResultList(), c);
}
public <E> List<E> find(Class<E> c) {
/**
* Returns a list of all instances of the specific class in the database.
*/
protected <E> List<E> find(Class<E> c) {
return find(c, null);
}
/**
* Deletes all instances of the specific class from the database.
*
* If no transaction is running, then one will be started and committed.
* Otherwise, the operation will take place in the current transaction.
*
* @return the total number of instanes deleted
*/
public int delete(Class... classes) {
boolean tx = begin();
int total = 0;
for (Class c : classes) {
total += query("delete from " + entityName(c) + " x").
executeUpdate();
}
if (tx) commit();
return total;
}
}
}

View File

@ -1,30 +1,20 @@
package org.apache.openjpa.persistence.xml;
import java.util.Map;
import javax.persistence.EntityManager;
import org.apache.openjpa.persistence.test.SingleEMTest;
import org.apache.openjpa.persistence.ArgumentException;
import org.apache.openjpa.persistence.test.SingleEMTestCase;
public class TestSimpleXmlEntity
extends SingleEMTest {
extends SingleEMTestCase {
public TestSimpleXmlEntity() {
super(SimpleXmlEntity.class);
}
protected void setEMFProps(Map props) {
super.setEMFProps(props);
public void setUp() {
setUp(SimpleXmlEntity.class);
}
public void testNamedQueryInXmlNamedEntity() {
EntityManager em = emf.createEntityManager();
em.createNamedQuery("SimpleXml.findAll").getResultList();
em.close();
}
public void testNamedQueryInXmlUsingShortClassName() {
EntityManager em = emf.createEntityManager();
try {
em.createNamedQuery("SimpleXmlEntity.findAll").getResultList();
fail("should not be able to execute query using short class name " +
@ -32,17 +22,13 @@ public class TestSimpleXmlEntity
} catch (ArgumentException ae) {
// expected
}
em.close();
}
public void testNamedEntityInDynamicQuery() {
EntityManager em = emf.createEntityManager();
em.createQuery("select o from SimpleXml o").getResultList();
em.close();
}
public void testShortClassNameInDynamicQuery() {
EntityManager em = emf.createEntityManager();
try {
em.createQuery("select o from SimpleXmlEntity o").getResultList();
fail("should not be able to execute query using short class name " +
@ -50,6 +36,5 @@ public class TestSimpleXmlEntity
} catch (ArgumentException ae) {
// expected
}
em.close();
}
}