From 5113654485807a7884796562b9c62d56a2ec7999 Mon Sep 17 00:00:00 2001 From: Donald Woods Date: Thu, 10 Sep 2009 16:40:37 +0000 Subject: [PATCH] add common setTestsDisabled()/isTestsDisabled() in AbstractPersistenceTestCase which is used by runTest() to automatically skip all tests. added new setSupportedDatabases()/setUnsupportedDatabases() methods in SingleEMFTestCase to set the testsDisabled flag in AbstractPTC, along with moving a getLog() method from some testcases into the base SingleEMFTC class for everyone to use. git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@813506 13f79535-47bb-0310-9956-ffa450edef68 --- .../validation/TestValidatingLEM.java | 8 -- .../openjpa/conf/TestQueryProperties.java | 8 -- .../persistence/embed/TestEmbeddable.java | 8 -- .../TestMultipleSchemaNames.java | 20 +---- .../persistence/query/TestQueryTimeout.java | 11 +-- .../test/AbstractPersistenceTestCase.java | 32 +++++++- .../persistence/test/SingleEMFTestCase.java | 52 +++++++++++++ .../validation/TestValidationFactory.java | 9 --- .../validation/TestValidationMode.java | 8 -- .../query/TestXMLCustomerOrder.java | 73 ++----------------- 10 files changed, 95 insertions(+), 134 deletions(-) diff --git a/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestValidatingLEM.java b/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestValidatingLEM.java index b1877346b..110a60aad 100644 --- a/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestValidatingLEM.java +++ b/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestValidatingLEM.java @@ -244,12 +244,4 @@ public class TestValidatingLEM extends SingleEMFTestCase { closeEMF(emf); } - /** - * Internal convenience method for getting the OpenJPA logger - * - * @return - */ - private Log getLog() { - return emf.getConfiguration().getLog("Tests"); - } } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/conf/TestQueryProperties.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/conf/TestQueryProperties.java index a991b86a7..953e88084 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/conf/TestQueryProperties.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/conf/TestQueryProperties.java @@ -311,12 +311,4 @@ public class TestQueryProperties extends SingleEMFTestCase { } } - /** - * Internal convenience method for getting the OpenJPA logger - * - * @return - */ - private Log getLog() { - return emf.getConfiguration().getLog("Tests"); - } } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/TestEmbeddable.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/TestEmbeddable.java index 6997ae320..2404dc7f3 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/TestEmbeddable.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/TestEmbeddable.java @@ -2807,12 +2807,4 @@ public class TestEmbeddable extends SQLListenerTestCase { } } - /** - * Internal convenience method for getting the OpenJPA logger - * - * @return - */ - private Log getLog() { - return emf.getConfiguration().getLog("Tests"); - } } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/TestMultipleSchemaNames.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/TestMultipleSchemaNames.java index e322efc94..0fc403b76 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/TestMultipleSchemaNames.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/TestMultipleSchemaNames.java @@ -39,12 +39,11 @@ import org.apache.openjpa.persistence.test.SingleEMFTestCase; public class TestMultipleSchemaNames extends SingleEMFTestCase { static private DBDictionary dict = null; - static private Boolean skipTests = null; public void setUp() { // Need to skip tests on MySQL, Oracle and MS SQL Server // See createSchemas() comment at the bottom - if ((dict == null) || (skipTests == null)) { + if (dict == null) { OpenJPAEntityManagerFactorySPI emf = createEMF(); OpenJPAEntityManagerSPI em = emf.createEntityManager(); JDBCConfiguration conf = (JDBCConfiguration) em.getConfiguration(); @@ -53,17 +52,15 @@ public class TestMultipleSchemaNames extends SingleEMFTestCase { if ((dict instanceof MySQLDictionary) || (dict instanceof OracleDictionary) || (dict instanceof SQLServerDictionary)) { - skipTests = Boolean.TRUE; + setTestsDisabled(true); // do some logging emf.getConfiguration().getLog("Tests").trace( "TestMultipleSchemaNames() - Skipping all tests - Not supported on this DB"); - } else { - skipTests = Boolean.FALSE; - } + } closeEMF(emf); } - if (skipTests) { + if (isTestsDisabled()) { return; } @@ -163,9 +160,6 @@ public class TestMultipleSchemaNames extends SingleEMFTestCase { } public void testGeneratedAUTO() { - if (skipTests) - return; - EntityManager em = emf.createEntityManager(); OpenJPAEntityManager kem = OpenJPAPersistence.cast(em); em.getTransaction().begin(); @@ -243,9 +237,6 @@ public class TestMultipleSchemaNames extends SingleEMFTestCase { } public void testGeneratedTABLE() { - if (skipTests) - return; - EntityManager em = emf.createEntityManager(); OpenJPAEntityManager kem = OpenJPAPersistence.cast(em); em.getTransaction().begin(); @@ -391,9 +382,6 @@ public class TestMultipleSchemaNames extends SingleEMFTestCase { } public void testGeneratedIDENTITY() { - if (skipTests) - return; - EntityManager em = emf.createEntityManager(); OpenJPAEntityManager kem = OpenJPAPersistence.cast(em); diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestQueryTimeout.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestQueryTimeout.java index 16515fd9c..9ecf476c6 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestQueryTimeout.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/query/TestQueryTimeout.java @@ -133,6 +133,7 @@ public class TestQueryTimeout extends SQLListenerTestCase { } else { // unknown db, so skip all timeout tests skipTests = skipExceptionTests = noSelectTimeouts = true; + setTestsDisabled(true); getLog().info("TestQueryTimeout tests are being skipped, due " + "to " + dict.platform + " not supporting Query Timeouts."); } @@ -140,6 +141,7 @@ public class TestQueryTimeout extends SQLListenerTestCase { getLog().info("TestQueryTimeout tests are being skipped, " + "due to " + dict.platform + " not supporting Query Timeouts."); skipTests = skipExceptionTests = true; + setTestsDisabled(true); } } @@ -1042,15 +1044,6 @@ public class TestQueryTimeout extends SQLListenerTestCase { } } - /** - * Internal convenience method for getting the OpenJPA logger - * - * @return - */ - private Log getLog() { - return emf.getConfiguration().getLog("Tests"); - } - /** * Internal convenience method for checking that the given Exception matches * the expected type for a given DB platform. diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/AbstractPersistenceTestCase.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/AbstractPersistenceTestCase.java index e00c4ba9e..64887ae6c 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/AbstractPersistenceTestCase.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/AbstractPersistenceTestCase.java @@ -54,6 +54,7 @@ public abstract class AbstractPersistenceTestCase extends TestCase { public static final String RETAIN_DATA = "Retain data after test run"; private boolean retainDataOnTearDown; protected boolean _fresh = false; + private Boolean testsDisabled = Boolean.FALSE; public static final String ALLOW_FAILURE_LOG = "log"; public static final String ALLOW_FAILURE_IGNORE = "ignore"; @@ -463,8 +464,10 @@ public abstract class AbstractPersistenceTestCase extends TestCase { } /** - * Overrides to allow tests annotated with @AllowFailure to fail. If the test is in error then the normal pathway is - * executed. + * Overrides to allow tests annotated with @AllowFailure to fail. + * If @DatabasePlatform value matches the current JDBC driver or + * tests have been disabled, then the test will not be run. + * If the test is in error then the normal pathway is executed. */ @Override public void runBare() throws Throwable { @@ -499,6 +502,18 @@ public abstract class AbstractPersistenceTestCase extends TestCase { } } + /** + * Override to run the test and assert its state. + * @exception Throwable if any exception is thrown + */ + @Override + protected void runTest() throws Throwable { + if (isTestsDisabled()) { + return; + } + super.runTest(); + } + /** * Affirms if the test case or the test method is annotated with * @@ -587,4 +602,17 @@ public abstract class AbstractPersistenceTestCase extends TestCase { assertFalse(String.format("Expected %s:%s not to exist in cache", clss, id), cache.contains(clss, id)); } } + + protected void setTestsDisabled(boolean disable) { + synchronized (testsDisabled) { + testsDisabled = new Boolean(disable); + } + } + + protected boolean isTestsDisabled() { + synchronized (testsDisabled) { + return testsDisabled.booleanValue(); + } + } + } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/SingleEMFTestCase.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/SingleEMFTestCase.java index f9f3a2922..7c1c269f5 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/SingleEMFTestCase.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/SingleEMFTestCase.java @@ -22,8 +22,12 @@ import java.util.List; import javax.persistence.EntityManager; +import org.apache.openjpa.jdbc.conf.JDBCConfiguration; import org.apache.openjpa.jdbc.meta.ClassMapping; +import org.apache.openjpa.jdbc.sql.DBDictionary; +import org.apache.openjpa.lib.log.Log; import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI; +import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI; /** * Base class for OpenJPA-specific Test Case. @@ -156,4 +160,52 @@ public abstract class SingleEMFTestCase protected ClassMapping [] getMappings() { return (ClassMapping [] ) emf.getConfiguration().getMetaDataRepositoryInstance().getMetaDatas(); } + + protected void setUnsupportedDatabases(Class ... dbs) { + OpenJPAEntityManagerFactorySPI tempEMF = emf; + if (tempEMF == null) { + tempEMF = createEMF(); + } + OpenJPAEntityManagerSPI em = tempEMF.createEntityManager(); + JDBCConfiguration conf = (JDBCConfiguration) em.getConfiguration(); + DBDictionary dict = conf.getDBDictionaryInstance(); + for (Class db : dbs) { + if (dict.getClass().getCanonicalName().equalsIgnoreCase(db.getCanonicalName())) { + setTestsDisabled(true); + break; + } + } + if (emf == null) { + closeEMF(tempEMF); + } else { + em.close(); + } + } + + protected void setSupportedDatabases(Class ... dbs) { + OpenJPAEntityManagerFactorySPI tempEMF = emf; + if (tempEMF == null) { + tempEMF = createEMF(); + } + OpenJPAEntityManagerSPI em = tempEMF.createEntityManager(); + JDBCConfiguration conf = (JDBCConfiguration) em.getConfiguration(); + DBDictionary dict = conf.getDBDictionaryInstance(); + boolean supportedDB = false; + for (Class db : dbs) { + if (dict.getClass().getCanonicalName().equalsIgnoreCase(db.getCanonicalName())) { + supportedDB = true; + break; + } + } + setTestsDisabled(!supportedDB); + if (emf == null) { + closeEMF(tempEMF); + } else { + em.close(); + } + } + + protected Log getLog() { + return emf.getConfiguration().getLog("Tests"); + } } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/validation/TestValidationFactory.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/validation/TestValidationFactory.java index 3e03df5bd..422b49062 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/validation/TestValidationFactory.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/validation/TestValidationFactory.java @@ -161,13 +161,4 @@ public class TestValidationFactory extends SingleEMFTestCase { } } - - /** - * Internal convenience method for getting the OpenJPA logger - * - * @return - */ - private Log getLog() { - return emf.getConfiguration().getLog("Tests"); - } } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/validation/TestValidationMode.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/validation/TestValidationMode.java index 2d99225ac..227a07a77 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/validation/TestValidationMode.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/validation/TestValidationMode.java @@ -256,12 +256,4 @@ public class TestValidationMode extends SingleEMFTestCase { closeEMF(emf); } - /** - * Internal convenience method for getting the OpenJPA logger - * - * @return - */ - private Log getLog() { - return emf.getConfiguration().getLog("Tests"); - } } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/xmlmapping/query/TestXMLCustomerOrder.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/xmlmapping/query/TestXMLCustomerOrder.java index b59a8b897..fea11c4b7 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/xmlmapping/query/TestXMLCustomerOrder.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/xmlmapping/query/TestXMLCustomerOrder.java @@ -28,6 +28,7 @@ import junit.textui.TestRunner; import org.apache.openjpa.jdbc.conf.JDBCConfiguration; import org.apache.openjpa.jdbc.sql.DBDictionary; +import org.apache.openjpa.lib.log.Log; import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI; import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI; import org.apache.openjpa.persistence.test.SingleEMFTestCase; @@ -51,8 +52,6 @@ import org.apache.openjpa.persistence.xmlmapping.xmlbindings.myaddress.USAAddres */ public class TestXMLCustomerOrder extends SingleEMFTestCase { - - private static Boolean skipTests = null; private static final int ORDER_1_OID = 10; private static final double ORDER_1_AMOUNT = 850; @@ -63,26 +62,16 @@ public class TestXMLCustomerOrder private static final boolean ORDER_2_DELIVERED = false; public void setUp() { - // skip test if dictionary has no support for XML column type - if (skipTests == null) { - if (!dictionarySupportsXMLColumn()) { - skipTests = Boolean.TRUE; - // do some logging - OpenJPAEntityManagerFactorySPI emf = createEMF(); - emf.getConfiguration().getLog("Tests").trace( - "TestXMLCustomerOrder() - Skipping all tests - No XML Column support"); - closeEMF(emf); - } else { - skipTests = Boolean.FALSE; - } - } + setUp(Customer.class, Customer.CustomerKey.class, Order.class, + EAddress.class, CLEAR_TABLES); - if (skipTests) { + // skip test if dictionary has no support for XML column type + setTestsDisabled(!dictionarySupportsXMLColumn()); + if (isTestsDisabled()) { + getLog().trace("TestXMLCustomerOrder() - Skipping all tests - No XML Column support"); return; } - setUp(Customer.class, Customer.CustomerKey.class, Order.class, - EAddress.class, CLEAR_TABLES); EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); persistEntities(em); @@ -92,10 +81,6 @@ public class TestXMLCustomerOrder @SuppressWarnings("unchecked") public void testXMLFieldProjection() { - if (skipTests) { - return; - } - EntityManager em = emf.createEntityManager(); List
addrs = em.createQuery( "select o.shipAddress from Order o order by o.oid") @@ -116,10 +101,6 @@ public class TestXMLCustomerOrder @SuppressWarnings("unchecked") public void testXMLFieldInEntity() { - if (skipTests) { - return; - } - EntityManager em = emf.createEntityManager(); List orders = em.createQuery( "select o from Order o order by o.oid") @@ -142,10 +123,6 @@ public class TestXMLCustomerOrder @SuppressWarnings("unchecked") public void testXMLStringToXMLStringComparison() { - if (skipTests) { - return; - } - EntityManager em = emf.createEntityManager(); List orders = em.createQuery( "select o, o2 from Order o, Order o2 where o.shipAddress.city " + @@ -171,10 +148,6 @@ public class TestXMLCustomerOrder @SuppressWarnings("unchecked") public void testXMLStringToEmbeddedStringComparison() { - if (skipTests) { - return; - } - EntityManager em = emf.createEntityManager(); List orders = em.createQuery( "select o from Order o, Customer c where o.shipAddress.city " + @@ -190,10 +163,6 @@ public class TestXMLCustomerOrder @SuppressWarnings("unchecked") public void testXMLStringToConstantStringComparison() { - if (skipTests) { - return; - } - EntityManager em = emf.createEntityManager(); List orders = em.createQuery( "select o from Order o where o.shipAddress.city = 'San Jose'") @@ -208,10 +177,6 @@ public class TestXMLCustomerOrder @SuppressWarnings("unchecked") public void testXMLStringToParameterStringComparison() { - if (skipTests) { - return; - } - EntityManager em = emf.createEntityManager(); Query query = em.createQuery( "select o from Order o where o.shipAddress.city = :city"); @@ -227,10 +192,6 @@ public class TestXMLCustomerOrder @SuppressWarnings("unchecked") public void testParameterStringToXMLStringComparison() { - if (skipTests) { - return; - } - EntityManager em = emf.createEntityManager(); Query query = em.createQuery( "select o from Order o where :city = o.shipAddress.city"); @@ -245,10 +206,6 @@ public class TestXMLCustomerOrder } public void testUpdate() { - if (skipTests) { - return; - } - EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); @@ -273,10 +230,6 @@ public class TestXMLCustomerOrder } public void testNullify() { - if (skipTests) { - return; - } - EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); @@ -298,10 +251,6 @@ public class TestXMLCustomerOrder } public void testXMLStringToConstantIntComparison() { - if (skipTests) { - return; - } - EntityManager em = emf.createEntityManager(); try { em.createQuery( @@ -316,10 +265,6 @@ public class TestXMLCustomerOrder } public void testXMLListToConstantStringComparison() { - if (skipTests) { - return; - } - EntityManager em = emf.createEntityManager(); try { em.createQuery( @@ -334,10 +279,6 @@ public class TestXMLCustomerOrder } public void testSubclassPropertyInXMLFieldComparison() { - if (skipTests) { - return; - } - EntityManager em = emf.createEntityManager(); try { em.createQuery(