From e9b5c53fd00be984157e131b94a7d5efee720a3c Mon Sep 17 00:00:00 2001 From: Milosz Tylenda Date: Sun, 13 Sep 2009 10:02:01 +0000 Subject: [PATCH] OPENJPA-1153: Speed tests up by using only needed entities rather than all entities from persistence.xml git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@814295 13f79535-47bb-0310-9956-ffa450edef68 --- .../common/utils/AbstractTestCase.java | 36 ++++++++++++++++--- .../kernel/TestCalendarFields.java | 10 +++--- .../kernel/TestCheckConsistency.java | 16 ++++----- .../kernel/TestClassStringConstructor.java | 4 +-- .../persistence/kernel/TestDateQueries.java | 5 +-- .../persistence/kernel/TestExtents.java | 6 ++-- .../persistence/kernel/TestExtents2.java | 6 ++-- 7 files changed, 54 insertions(+), 29 deletions(-) diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/common/utils/AbstractTestCase.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/common/utils/AbstractTestCase.java index dae55dbe7..85832a883 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/common/utils/AbstractTestCase.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/common/utils/AbstractTestCase.java @@ -67,6 +67,7 @@ import java.util.ListIterator; import java.util.Map; import java.util.NoSuchElementException; import java.util.StringTokenizer; +import java.util.Map.Entry; import javax.management.IntrospectionException; import javax.persistence.EntityManager; @@ -93,6 +94,7 @@ public abstract class AbstractTestCase extends AbstractCachedEMFTestCase { private Map emfs = new HashMap(); private OpenJPAEntityManager currentEntityManager; + private Object[] props; protected enum Platform { EMPRESS, @@ -118,6 +120,18 @@ public abstract class AbstractTestCase extends AbstractCachedEMFTestCase { persistenceXmlResource = computePersistenceXmlResource(s); } + /** + * Use the given persistent types during the test. + * + * @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) throws Exception { + super.setUp(); + this.props = props; + } + public void tearDown() throws Exception { try { super.tearDown(); @@ -194,11 +208,25 @@ public abstract class AbstractTestCase extends AbstractCachedEMFTestCase { addProperties(map); OpenJPAEntityManagerFactory emf = emfs.get(map); - if (emf == null) { - emf = OpenJPAPersistence.createEntityManagerFactory( - "TestConv", persistenceXmlResource, map); - emfs.put(map, emf); + if (emf != null) { + return emf; } + + if (props != null) { + // Join properties passed in setUp (usually entities) with the given map and use them to create EMF. + Object[] propsAndMap = new Object[props.length + map.size() * 2]; + System.arraycopy(props, 0, propsAndMap, 0, props.length); + int i = props.length; + for (Object o : map.entrySet()) { + Entry mapEntry = (Entry) o; + propsAndMap[i++] = mapEntry.getKey(); + propsAndMap[i++] = mapEntry.getValue(); + } + emf = createEMF(propsAndMap); + } else { + emf = OpenJPAPersistence.createEntityManagerFactory("TestConv", persistenceXmlResource, map); + } + emfs.put(map, emf); return emf; } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestCalendarFields.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestCalendarFields.java index 359e51430..8aa418627 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestCalendarFields.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestCalendarFields.java @@ -48,8 +48,8 @@ public class TestCalendarFields extends BaseKernelTest { super(name); } - public void setUp() { - deleteAll(CalendarFields.class); + public void setUp() throws Exception { + super.setUp(CalendarFields.class); } public void testFieldDefaultTimeZone() { @@ -67,7 +67,7 @@ public class TestCalendarFields extends BaseKernelTest { endEm(pm); pm = getPM(); - cal = (CalendarFields) pm.find(CalendarFields.class, id); + cal = pm.find(CalendarFields.class, id); assertEquals(TimeZone.getTimeZone("Asia/Singapore"), cal.getSingapore().getTimeZone()); endEm(pm); @@ -90,9 +90,9 @@ public class TestCalendarFields extends BaseKernelTest { endEm(pm); pm = getPM(); - c2 = (CalendarFields) pm.find(CalendarFields.class, id2); + c2 = pm.find(CalendarFields.class, id2); assertTimeZonesEquals(c1, c2); - assertTimeZonesEquals(c1, (CalendarFields) pm.detachCopy(c2)); + assertTimeZonesEquals(c1, pm.detachCopy(c2)); endEm(pm); } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestCheckConsistency.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestCheckConsistency.java index 47206acf5..f80d70972 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestCheckConsistency.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestCheckConsistency.java @@ -45,8 +45,8 @@ public class TestCheckConsistency extends BaseKernelTest { super(name); } - public void setUp() { - deleteAll(RuntimeTest1.class); + public void setUp() throws Exception { + super.setUp(RuntimeTest1.class); RuntimeTest1 pc = new RuntimeTest1(); pc.setIntField(1); @@ -61,7 +61,7 @@ public class TestCheckConsistency extends BaseKernelTest { pm.validateChanges(); // no-op outside trans startTx(pm); - RuntimeTest1 pc = (RuntimeTest1) pm.find(RuntimeTest1.class, _oid); + RuntimeTest1 pc = pm.find(RuntimeTest1.class, _oid); pc.setIntField1(100); RuntimeTest1 npc = new RuntimeTest1(); @@ -89,7 +89,7 @@ public class TestCheckConsistency extends BaseKernelTest { pm.validateChanges(); // no-op outside trans startTx(pm); - RuntimeTest1 pc = (RuntimeTest1) pm.find(RuntimeTest1.class, _oid); + RuntimeTest1 pc = pm.find(RuntimeTest1.class, _oid); pc.setIntField1(100); RuntimeTest1 npc = new RuntimeTest1(); @@ -112,7 +112,7 @@ public class TestCheckConsistency extends BaseKernelTest { pm = getPM(); try { RuntimeTest1 temp = - (RuntimeTest1) pm.find(RuntimeTest1.class, noid); + pm.find(RuntimeTest1.class, noid); fail("Object should not exist." + temp.getIntField() + "::" + temp.getIntField1()); } catch (Exception jonfe) { @@ -176,7 +176,7 @@ try { startTx(pm); boolean hasConn = hasConnection(pm); - RuntimeTest1 pc = (RuntimeTest1) pm.find(RuntimeTest1.class, _oid); + RuntimeTest1 pc = pm.find(RuntimeTest1.class, _oid); pc.setIntField1(100); RuntimeTest1 npc = new RuntimeTest1(); @@ -212,7 +212,7 @@ try { startTx(pm); boolean hasConn = hasConnection(pm); - RuntimeTest1 pc = (RuntimeTest1) pm.find(RuntimeTest1.class, _oid); + RuntimeTest1 pc = pm.find(RuntimeTest1.class, _oid); pc.setIntField1(100); RuntimeTest1 npc = new RuntimeTest1(); @@ -238,7 +238,7 @@ try { pm = getPM(); try { RuntimeTest1 temp = - (RuntimeTest1) pm.find(RuntimeTest1.class, noid); + pm.find(RuntimeTest1.class, noid); fail("Object should not exist." + temp.getIntField() + "::" + temp.getIntField1()); diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestClassStringConstructor.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestClassStringConstructor.java index 0c8d683af..419bd3015 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestClassStringConstructor.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestClassStringConstructor.java @@ -41,8 +41,8 @@ public class TestClassStringConstructor extends BaseKernelTest { super(name); } - public void setUp() { - deleteAll(AppIdClassString.class); + public void setUp() throws Exception { + super.setUp(AppIdClassString.class); } public void testConstructor() { diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestDateQueries.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestDateQueries.java index 8c1f2904f..37aaba46b 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestDateQueries.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestDateQueries.java @@ -60,7 +60,7 @@ public class TestDateQueries extends BaseKernelTest { public void setUp() throws Exception { - super.setUp(); + super.setUp(AllFieldTypesTest.class); SimpleDateFormat sdf = new SimpleDateFormat("MMMMM dd, yyyy", Locale.US); @@ -69,9 +69,6 @@ public class TestDateQueries extends BaseKernelTest { _after = sdf.parse("April 27, 1978"); _pm = getPM(); - // delete all existing instances - deleteAll(AllFieldTypesTest.class); - // create some instances to query on startTx(_pm); AllFieldTypesTest test = new AllFieldTypesTest(); diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestExtents.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestExtents.java index 60b63c37f..d2e3d394e 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestExtents.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestExtents.java @@ -52,8 +52,8 @@ public class TestExtents extends BaseKernelTest { super(name); } - public void setUp() { - deleteAll(RuntimeTest1.class); + public void setUp() throws Exception { + super.setUp(RuntimeTest1.class, RuntimeTest2.class); OpenJPAEntityManager pm = getPM(); startTx(pm); @@ -101,7 +101,7 @@ public class TestExtents extends BaseKernelTest { public void testExtent4() { OpenJPAEntityManager pm = getPM(); - Extent ext = (Extent) pm.createExtent(RuntimeTest1.class, true); + Extent ext = pm.createExtent(RuntimeTest1.class, true); List all = new LinkedList(); for (Iterator i = ext.iterator(); i.hasNext();) diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestExtents2.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestExtents2.java index 98d2cef3b..30b67b0a7 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestExtents2.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestExtents2.java @@ -54,8 +54,8 @@ public class TestExtents2 extends BaseKernelTest { public TestExtents2() { } - public void setUp() { - deleteAll(RuntimeTest1.class); + public void setUp() throws Exception { + super.setUp(RuntimeTest1.class, RuntimeTest2.class, RuntimeTest3.class); RuntimeTest1 test1 = new RuntimeTest1(); test1.setIntField(1); @@ -170,7 +170,7 @@ public class TestExtents2 extends BaseKernelTest { startTx(pm); try { RuntimeTest2 test2 = - (RuntimeTest2) pm.find(RuntimeTest2.class, _oid2); + pm.find(RuntimeTest2.class, _oid2); pm.remove(test2); RuntimeTest1 test1 = new RuntimeTest1(); pm.persist(test1);