openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ppod...@apache.org
Subject svn commit: r747969 - in /openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa: lib/conf/TestEquivalentConfiguration.java persistence/test/DatabasePlatform.java persistence/test/PersistenceTestCase.java
Date Wed, 25 Feb 2009 23:42:44 GMT
Author: ppoddar
Date: Wed Feb 25 23:42:44 2009
New Revision: 747969

URL: http://svn.apache.org/viewvc?rev=747969&view=rev
Log:
OPENJPA-942: Allow Tests to specify target database platform

Added:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/DatabasePlatform.java
Modified:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/lib/conf/TestEquivalentConfiguration.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/PersistenceTestCase.java

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/lib/conf/TestEquivalentConfiguration.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/lib/conf/TestEquivalentConfiguration.java?rev=747969&r1=747968&r2=747969&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/lib/conf/TestEquivalentConfiguration.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/lib/conf/TestEquivalentConfiguration.java
Wed Feb 25 23:42:44 2009
@@ -31,6 +31,7 @@
 import org.apache.openjpa.persistence.OpenJPAPersistence;
 import org.apache.openjpa.persistence.common.utils.AbstractTestCase;
 import org.apache.openjpa.persistence.test.AllowFailure;
+import org.apache.openjpa.persistence.test.DatabasePlatform;
 import org.apache.openjpa.persistence.test.SingleEMFTestCase;
 
 import junit.framework.TestCase;
@@ -47,6 +48,11 @@
  * @author Pinaki Poddar
  * 
  */
+
+// Specifies that the test runs only if the driver is available in the 
+// execution classpath
+@DatabasePlatform("org.apache.derby.jdbc.EmbeddedDriver")
+
 public class TestEquivalentConfiguration extends SingleEMFTestCase {
     private EntityManagerFactory emf;
 

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/DatabasePlatform.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/DatabasePlatform.java?rev=747969&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/DatabasePlatform.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/DatabasePlatform.java
Wed Feb 25 23:42:44 2009
@@ -0,0 +1,41 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+package org.apache.openjpa.persistence.test;
+
+import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+/**
+ * Annotates a test to run only if the specified driver classes are available in
+ * the current classpath.
+ * 
+ * Specify comma-separated list of fully-qualified JDBC driver class names.
+ *  
+ * @author Pinaki Poddar
+ *
+ */
+
+@Target({TYPE})
+@Retention(RUNTIME)
+public @interface DatabasePlatform {
+    String value();
+}

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/PersistenceTestCase.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/PersistenceTestCase.java?rev=747969&r1=747968&r2=747969&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/PersistenceTestCase.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/PersistenceTestCase.java
Wed Feb 25 23:42:44 2009
@@ -27,6 +27,7 @@
 import java.lang.reflect.Modifier;
 import java.sql.SQLException;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
@@ -446,6 +447,8 @@
      */
     @Override
     public void runBare() throws Throwable {
+        if (!isRunsOnCurrentPlatform())
+            return;
         try {
             super.runBare();
         } catch (Throwable t) {
@@ -484,6 +487,37 @@
     	return null;
     }
     
+    /**
+     * Affirms if either this test has been annotated with @DatabasePlatform and 
+     * at least one of the specified driver is available in the classpath,
+     * or no such annotation is used.
+     *   
+     */
+    protected boolean isRunsOnCurrentPlatform() {
+        DatabasePlatform anno = getClass().getAnnotation(DatabasePlatform.class);
+        if (anno == null)
+            return true;
+        if (anno != null) {
+            String value = anno.value();
+            if (value == null || value.trim().length() == 0)
+                return true;
+            String[] drivers = value.split("\\,");
+            for (String driver : drivers) {
+                try {
+                    Class.forName(driver.trim(), false, 
+                        Thread.currentThread().getContextClassLoader());
+                    return true;
+                } catch (Throwable t) {
+                    // swallow;
+                }
+            }
+            System.err.println("WARN: " + getClass().getName() + " not run " +
+                "in the current platform because none of the specified " + 
+                " driver(s) " + Arrays.toString(drivers) + " can be loaded");
+        }
+        return false;
+    }
+    
     private static class FixedMap extends LinkedHashMap<EMFKey, OpenJPAEntityManagerFactorySPI>
{
         public boolean removeEldestEntry(Map.Entry<EMFKey, OpenJPAEntityManagerFactorySPI>
entry) {
             return this.size() > 2;



Mime
View raw message