You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jspwiki.apache.org by ju...@apache.org on 2021/01/11 18:56:39 UTC

[jspwiki] 03/08: allow TestJDBCDataSource to connect to HsqlDb instances started on random ports

This is an automated email from the ASF dual-hosted git repository.

juanpablo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jspwiki.git

commit 2c9cfb1e948fe346301e8790f3f7dc7bf7f71c5c
Author: Juan Pablo Santos Rodríguez <ju...@gmail.com>
AuthorDate: Mon Jan 11 18:27:55 2021 +0100

    allow TestJDBCDataSource to connect to HsqlDb instances started on random ports
---
 .../java/org/apache/wiki/TestJDBCDataSource.java   | 44 +++++++++++++---------
 1 file changed, 26 insertions(+), 18 deletions(-)

diff --git a/jspwiki-main/src/test/java/org/apache/wiki/TestJDBCDataSource.java b/jspwiki-main/src/test/java/org/apache/wiki/TestJDBCDataSource.java
index 517be55..8267886 100644
--- a/jspwiki-main/src/test/java/org/apache/wiki/TestJDBCDataSource.java
+++ b/jspwiki-main/src/test/java/org/apache/wiki/TestJDBCDataSource.java
@@ -18,6 +18,7 @@
  */
 package org.apache.wiki;
 
+import javax.sql.DataSource;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.PrintWriter;
@@ -31,12 +32,10 @@ import java.sql.SQLException;
 import java.util.Properties;
 import java.util.logging.Logger;
 
-import javax.sql.DataSource;
-
 /**
  * Mock JDBC DataSource class that manages JDBC connections to a database whose
  * driver class, JDBC JAR location and connection details are specified in an
- * arbitrary propreties file. Gemerally, we pass on any exceptions encountered
+ * arbitrary properties file. Generally, we pass on any exceptions encountered
  * as unchecked, since it means that the test case that references this class is
  * failing somehow.
  */
@@ -68,11 +67,22 @@ public class TestJDBCDataSource implements DataSource
      * Constructs a new instance of this class, using a supplied properties
      * File as the source for JDBC driver properties.
      * @param file the properties file containing JDBC properties
-     * @throws Exception
+     * @throws Exception error initializing JDBC
+     */
+    public TestJDBCDataSource( final File file ) throws Exception
+    {
+        initializeJDBC( file );
+    }
+
+    /**
+     * Constructs a new instance of this class, using a supplied properties
+     * File as the source for JDBC driver properties.
+     * @param file the properties file containing JDBC properties
+     * @throws Exception error initializing JDBC
      */
-    public TestJDBCDataSource(final File file ) throws Exception
+    public TestJDBCDataSource( final File file, final String url ) throws Exception
     {
-        super();
+        m_jdbcURL = url;
         initializeJDBC( file );
     }
 
@@ -99,8 +109,7 @@ public class TestJDBCDataSource implements DataSource
         final Properties connProperties = new Properties();
         connProperties.put( "user", m_jdbcUser );
         connProperties.put( "password", m_jdbcPassword );
-        final Connection connection = m_driver.connect( m_jdbcURL, connProperties );
-        return connection;
+        return m_driver.connect( m_jdbcURL, connProperties );
     }
 
     /**
@@ -149,7 +158,9 @@ public class TestJDBCDataSource implements DataSource
 
     /**
      * Return the parent Logger of all the Loggers used by this data source. This should be 
-     * the Logger farthest from the root Logger that is still an ancestor of all of the Loggers used by this data source. Configuring this Logger will affect all of the log messages generated by the data source. In the worst case, this may be the root Logger.
+     * the Logger farthest from the root Logger that is still an ancestor of all of the Loggers used by this data source.
+     * Configuring this Logger will affect all of the log messages generated by the data source. In the worst case, this
+     * may be the root Logger.
      * @return the parent Logger for this data source
      */
     @Override
@@ -162,9 +173,9 @@ public class TestJDBCDataSource implements DataSource
      * Initialization method that reads a File, and attempts to locate and load
      * the JDBC driver from properties specified therein.
      * @param file the file containing the JDBC properties
-     * @throws SQLException
+     * @throws Exception error loading class or properties
      */
-    protected void initializeJDBC(final File file ) throws Exception
+    protected void initializeJDBC( final File file ) throws Exception
     {
         // Load the properties JDBC properties file
         final Properties properties;
@@ -172,7 +183,9 @@ public class TestJDBCDataSource implements DataSource
         final FileInputStream is = new FileInputStream( file );
         properties.load( is );
         is.close();
-        m_jdbcURL = properties.getProperty( PROPERTY_DRIVER_URL );
+        if( m_jdbcURL == null ) {
+            m_jdbcURL = properties.getProperty( PROPERTY_DRIVER_URL );
+        }
         m_jdbcUser = properties.getProperty( PROPERTY_USER_ID );
         m_jdbcPassword = properties.getProperty( PROPERTY_USER_PASSWORD );
 
@@ -185,12 +198,7 @@ public class TestJDBCDataSource implements DataSource
 
         // Load the driver using the sytem class loader
         final ClassLoader parent = ClassLoader.getSystemClassLoader();
-        final URLClassLoader loader = AccessController.doPrivileged(new PrivilegedAction<URLClassLoader>() {
-            @Override
-            public URLClassLoader run() {
-                return new URLClassLoader( new URL[] { driverURL }, parent );
-            }
-        });
+        final URLClassLoader loader = AccessController.doPrivileged( ( PrivilegedAction< URLClassLoader > ) () -> new URLClassLoader( new URL[] { driverURL }, parent ) );
         final Class< ? > driverClass = loader.loadClass( clazz );
 
         // Cache the driver