You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by ka...@apache.org on 2014/03/27 09:40:09 UTC

svn commit: r1582220 - in /db/derby/code/trunk/java/testing/org/apache/derbyTesting: functionTests/tests/lang/ functionTests/util/ junit/

Author: kahatlen
Date: Thu Mar 27 08:40:08 2014
New Revision: 1582220

URL: http://svn.apache.org/r1582220
Log:
DERBY-2162/DERBY-5618: Close the URLClassLoader when tests are done
with it so that file handles are freed and the jar files can be
deleted.

Modified:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DatabaseClassLoadingTest.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/FullCollationTests.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/NativeAuthenticationServiceTest.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/derby_tests.policy
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/ClasspathSetup.java

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DatabaseClassLoadingTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DatabaseClassLoadingTest.java?rev=1582220&r1=1582219&r2=1582220&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DatabaseClassLoadingTest.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DatabaseClassLoadingTest.java Thu Mar 27 08:40:08 2014
@@ -29,7 +29,6 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.net.MalformedURLException;
 import java.net.URL;
-import java.net.URLClassLoader;
 import java.security.AccessController;
 import java.sql.CallableStatement;
 import java.sql.Connection;
@@ -46,6 +45,7 @@ import junit.framework.Test;
 import junit.framework.TestSuite;
 
 import org.apache.derbyTesting.junit.BaseJDBCTestCase;
+import org.apache.derbyTesting.junit.ClasspathSetup;
 import org.apache.derbyTesting.junit.CleanDatabaseTestSetup;
 import org.apache.derbyTesting.junit.JDBC;
 import org.apache.derbyTesting.junit.JDBCDataSource;
@@ -72,7 +72,7 @@ public class DatabaseClassLoadingTest ex
      * Ordering is important here so the fixtures are added
      * explicitly.
      */
-    public static Test suite()
+    public static Test suite() throws Exception
     {
         final TestSuite suite = new TestSuite("DatabaseClassLoadingTest");
         
@@ -117,8 +117,15 @@ public class DatabaseClassLoadingTest ex
            suite.addTest(SecurityManagerSetup.noSecurityManager(
                    new DatabaseClassLoadingTest("testDatabaseInJar"))); 
 
-           suite.addTest(SecurityManagerSetup.noSecurityManager(
-                   new DatabaseClassLoadingTest("testDatabaseInClasspath")));
+            // DERBY-2162: Only run this test case on platforms that support
+            // the URLClassLoader.close() method. Otherwise, we won't be able
+            // to delete the jar file afterwards.
+            if (ClasspathSetup.supportsClose()) {
+                suite.addTest(SecurityManagerSetup.noSecurityManager(
+                    new ClasspathSetup(
+                        new DatabaseClassLoadingTest("testDatabaseInClasspath"),
+                        SupportFilesSetup.getReadOnlyURL("dclt.jar"))));
+            }
            
            // No security manager because the test uses getClass().getClassLoader()
            // in an installed jar to ensure that the class loader for
@@ -638,34 +645,11 @@ public class DatabaseClassLoadingTest ex
         readOnlyTest(ds);
     }
     
-    public void testDatabaseInClasspath() throws SQLException, MalformedURLException
+    public void testDatabaseInClasspath() throws SQLException
     {
         String dbName = "classpath:dbro";
         DataSource ds = JDBCDataSource.getDataSource(dbName);
-        
-        try {
-            ds.getConnection();
-            fail("opened database before it was on classpath");
-        } catch (SQLException e)
-        {
-           assertSQLState("XJ004", e);
-        }
-        
-        URL jarURL = SupportFilesSetup.getReadOnlyURL("dclt.jar");
-        
-        // DERBY-2179 - temp disable using the class loader
-        // (since due to DERBY-2162) it's not working anyway)
-        // need to re-write code not to assume there is no
-        // existing thread context loader.
-        //setContextClassLoader(jarURL);
-        try {
-            // Disabled due to DERBY-2162, running this opens
-            // the database thus accessing resources and means the
-            // jar file cannot be cleaned up.
-            // readOnlyTest(ds);
-        } finally {
-            //setContextClassLoader(null);
-        } 
+        readOnlyTest(ds);
     }
     
     /**
@@ -1331,17 +1315,4 @@ public class DatabaseClassLoadingTest ex
         in.close();
         zos.closeEntry();
     }
-    
-    private static void setContextClassLoader(final URL url)
-    {
-        AccessController.doPrivileged
-        (new java.security.PrivilegedAction<Void>() {
-            public Void run()  {
-                URLClassLoader cl = 
-                    url == null ? null : new URLClassLoader(new URL[] {url});
-                java.lang.Thread.currentThread().setContextClassLoader(cl);
-              return null;
-            }
-        });
-    }
 }

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/FullCollationTests.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/FullCollationTests.java?rev=1582220&r1=1582219&r2=1582220&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/FullCollationTests.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/FullCollationTests.java Thu Mar 27 08:40:08 2014
@@ -27,19 +27,13 @@ import junit.framework.TestSuite;
 import org.apache.derbyTesting.junit.Decorator;
 
 
-public class FullCollationTests extends CollationTest {
-    
-    public FullCollationTests(String name) {
-        super(name);
-       
-    }
+public class FullCollationTests extends TestSuite {
 
-    
     /**
      * Return a suite that uses a single use database with
      * a primary fixture from this test plus all tests
      */
-    public static Test suite() 
+    public static Test suite() throws Exception
     {
         TestSuite suite = new TestSuite("FullCollationTests:territory=" +"no_NO");
         suite.addTest(new CollationTest("testNorwayCollation"));

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/NativeAuthenticationServiceTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/NativeAuthenticationServiceTest.java?rev=1582220&r1=1582219&r2=1582220&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/NativeAuthenticationServiceTest.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/NativeAuthenticationServiceTest.java Thu Mar 27 08:40:08 2014
@@ -22,6 +22,7 @@
 package org.apache.derbyTesting.functionTests.tests.lang;
 
 import java.io.File;
+import java.net.URL;
 import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
@@ -351,17 +352,16 @@ public class NativeAuthenticationService
     /**
      * Construct top level suite in this JUnit test
      */
-    public static Test suite()
+    public static Test suite() throws Exception
     {
         TestSuite suite = new TestSuite();
 
         //
         // Special version of the test which uses an encrypted database for credentials.
-        // Not run on windows until DERBY-5618 is addressed.
-        // Also not run on small devices because encryption is not supported there
-        // by default.
+        // Only run on platforms where we can expect cleanup of support files
+        // to succeed (DERBY-5618).
         //
-        if ( !onWindows() && !JDBC.vmSupportsJSR169() )
+        if (enableSubprotocolTests())
         {
             suite.addTest
                 (
@@ -380,9 +380,21 @@ public class NativeAuthenticationService
         // should be OK to run in the local time zone.
         return new TimeZoneTestSetup(suite, "GMT");
     }
-    private static  boolean onWindows()
-    {
-        return getSystemProperty("os.name").startsWith("Windows");
+
+    /**
+     * Check whether or not tests for subprotocols should be enabled
+     * on this platform.
+     */
+    private static boolean enableSubprotocolTests() {
+        // DERBY-5618: On Windows, we are not able to delete the jar files
+        // used by the tests for the classpath subsubprotocol if we don't
+        // close the URLClassLoader when we're done. Closing the class loader
+        // can only be done on Java 7 and higher.
+        //
+        // Run the tests if the Java version supports URLClassLoader.close(),
+        // or if we're on a non-Windows platform (where the jar files can be
+        // deleted even if the class loader hasn't been closed).
+        return ClasspathSetup.supportsClose() || !isWindowsPlatform();
     }
 
     /**
@@ -390,7 +402,7 @@ public class NativeAuthenticationService
      * Create a suite of all test configurations.
      * </p>
      */
-    private static  Test   allConfigurations( boolean clientServer )
+    private static Test allConfigurations(boolean clientServer) throws Exception
     {
         TestSuite suite = new TestSuite();
 
@@ -442,9 +454,10 @@ public class NativeAuthenticationService
              );
         
         //
-        // For testing subprotocols. Cleanup of support files is blocked by DERBY-5618.
+        // For testing subprotocols. Only run on platforms where we can
+        // expect cleanup of support files to succeed (DERBY-5618).
         //
-        if ( !onWindows() )
+        if (enableSubprotocolTests())
         {
             //
             // NATIVE authentication with credentials in read-only databases accessed via jar subprotocol
@@ -481,7 +494,7 @@ public class NativeAuthenticationService
                   ( CLASSPATH, NATIVE, LOCAL, DONT_DISABLE_AUTH, DISABLE_JAVA_SECURITY )
                   ).decorate( clientServer )
                  );
-        }   // end if !onWindows()
+        }
 
         return suite;
     }
@@ -493,7 +506,7 @@ public class NativeAuthenticationService
      * stored properties that can't be removed at tearDown time.
      * </p>
      */
-    private Test    decorate( boolean clientServer )
+    private Test decorate(boolean clientServer) throws Exception
     {
         String      credentialsDBPhysicalName = TestConfiguration.generateUniqueDatabaseName();
         
@@ -534,24 +547,17 @@ public class NativeAuthenticationService
         //    };
         
         //
-        // For testing subprotocols. Cleanup of support files is blocked by DERBY-5618.
+        // For testing subprotocols. Only run on platforms where we can
+        // expect cleanup of support files to succeed (DERBY-5618).
         //
-        if ( !onWindows() )
+        if (enableSubprotocolTests())
         {
-            // Add the jar files needed for testing jar and classpath subprotocols.
-            result = new SupportFilesSetup( result, SUPPORT_FILES_SOURCE, null, SUPPORT_FILES_TARGET, null );
-
-            //
             // Add a jar file to the classpath so that we can test the classpath subprotocol.
-            //
-            File        currentDirectory = currentDirectory();
-            File        readOnlyDirectory = new File( currentDirectory, SupportFilesSetup.EXTIN );
-            File        nast2Jar = new File( readOnlyDirectory, NAST2_JAR_FILE );
+            URL nast2Jar = SupportFilesSetup.getReadOnlyURL(NAST2_JAR_FILE);
+            result = new ClasspathSetup(result, nast2Jar);
 
-            try {
-                result = new ClasspathSetup( result, nast2Jar.toURI().toURL() );
-            }
-            catch (Exception e) { printStackTrace( e ); }
+            // Add the jar files needed for testing jar and classpath subprotocols.
+            result = new SupportFilesSetup( result, SUPPORT_FILES_SOURCE, null, SUPPORT_FILES_TARGET, null );
         }
         
         //
@@ -866,12 +872,11 @@ public class NativeAuthenticationService
 
         ///////////////////////////////////////////////////////////////////////////////////
         //
-        // Vet databases accessed via subprotocols. Not run on Windows machines
-        // because of DERBY-5618.
+        // Vet databases accessed via subprotocols.
         //
         ///////////////////////////////////////////////////////////////////////////////////
 
-        if ( !onWindows() )
+        if (enableSubprotocolTests())
         {
             // database accessed via jar subprotocol
             vetProtocol( jarDBName( _credentialsDBLocation ) );

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java?rev=1582220&r1=1582219&r2=1582220&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java Thu Mar 27 08:40:08 2014
@@ -49,7 +49,7 @@ public class _Suite extends BaseTestCase
 		super(name);
 	}
 
-	public static Test suite() {
+    public static Test suite() throws Exception {
 
 		TestSuite suite = new TestSuite("lang");
         

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/derby_tests.policy
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/derby_tests.policy?rev=1582220&r1=1582219&r2=1582220&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/derby_tests.policy (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/derby_tests.policy Thu Mar 27 08:40:08 2014
@@ -294,6 +294,9 @@ grant codeBase "${derbyTesting.testjar}d
 
   // Needed by NetworkServerTestSetup when probing ports.
   permission java.net.SocketPermission "localhost", "listen";
+
+  // Needed by ClasspathSetup for freeing resources.
+  permission java.lang.RuntimePermission "closeClassLoader";
 };
 
 //
@@ -378,6 +381,9 @@ grant codeBase "${derbyTesting.codeclass
   // This permission is needed to call DriverManager.deregisterDriver()
   // on Java SE 8 and later.
   permission java.sql.SQLPermission "deregisterDriver";
+
+  // Needed by ClasspathSetup for freeing resources.
+  permission java.lang.RuntimePermission "closeClassLoader";
 };
 
 // JUnit jar file tries to read junit.properties in the user's

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/ClasspathSetup.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/ClasspathSetup.java?rev=1582220&r1=1582219&r2=1582220&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/ClasspathSetup.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/ClasspathSetup.java Thu Mar 27 08:40:08 2014
@@ -19,10 +19,13 @@
  */
 package org.apache.derbyTesting.junit;
 
+import java.io.Closeable;
+import java.io.IOException;
 import java.net.URL;
 import java.net.URLClassLoader;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
+import java.security.PrivilegedExceptionAction;
 
 import junit.extensions.TestSetup;
 import junit.framework.Test;
@@ -49,6 +52,7 @@ public class ClasspathSetup extends Test
 
     private URL             _resource;
     private ClassLoader _originalClassLoader;
+    private URLClassLoader _newClassLoader;
 
     ///////////////////////////////////////////////////////////////////////////////////
     //
@@ -61,7 +65,7 @@ public class ClasspathSetup extends Test
      * Add the indicated URL to the classpath.
      * </p>
      */
-    public  ClasspathSetup( Test test, URL resource )  throws Exception
+    public ClasspathSetup(Test test, URL resource)
     {
         super( test );
         
@@ -84,9 +88,9 @@ public class ClasspathSetup extends Test
                  { 
                      _originalClassLoader = Thread.currentThread().getContextClassLoader();
 
-                     URLClassLoader newClassLoader = new URLClassLoader( new URL[] { _resource }, _originalClassLoader );
+                     _newClassLoader = new URLClassLoader( new URL[] { _resource }, _originalClassLoader );
 
-                     Thread.currentThread().setContextClassLoader( newClassLoader );
+                     Thread.currentThread().setContextClassLoader( _newClassLoader );
                      
                      return null;
                  }
@@ -94,22 +98,42 @@ public class ClasspathSetup extends Test
              );
     }
     
-    protected void tearDown()
+    protected void tearDown() throws Exception
     {
         AccessController.doPrivileged
             (
-             new PrivilegedAction<Void>()
+             new PrivilegedExceptionAction<Void>()
              {
-                 public Void run()
+                 public Void run() throws IOException
                  { 
                      Thread.currentThread().setContextClassLoader( _originalClassLoader );
-                     
+
+                     // On Java 7 and higher, URLClassLoader implements the
+                     // Closable interface and has a close() method. Use that
+                     // method, if it's available, to free all resources
+                     // associated with the class loader. DERBY-2162.
+                     if (_newClassLoader instanceof Closeable) {
+                        ((Closeable) _newClassLoader).close();
+                     }
+
                      return null;
                  }
              }
              );
-    }
 
-}
+        _originalClassLoader = null;
+        _newClassLoader = null;
+        _resource = null;
+    }
 
+    /**
+     * Check whether this platform supports closing a {@code URLClassLoader}.
+     *
+     * @return {@code true} if {@code URLClassLoader} has a {@code close()}
+     * method (Java 7 and higher), or {@code false} otherwise
+     */
+    public static boolean supportsClose() {
+        return Closeable.class.isAssignableFrom(URLClassLoader.class);
+    }
 
+}