diff --git a/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerDriver.java b/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerDriver.java index bf0d4a6..fb496fe 100644 --- a/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerDriver.java +++ b/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerDriver.java @@ -135,6 +135,8 @@ protected AWSSecretsManagerDriver(SecretCache cache) { setProperties(); AWSSecretsManagerDriver.register(this); + + getWrappedDriver(); } /** diff --git a/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMSSQLServerDriver.java b/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMSSQLServerDriver.java index a53e715..16f8e96 100644 --- a/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMSSQLServerDriver.java +++ b/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMSSQLServerDriver.java @@ -46,6 +46,11 @@ public final class AWSSecretsManagerMSSQLServerDriver extends AWSSecretsManagerD */ public static final String SUBPREFIX = "sqlserver"; + /** + * Default driver class to use. + */ + public static final String DEFAULT_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; + static { AWSSecretsManagerDriver.register(new AWSSecretsManagerMSSQLServerDriver()); } @@ -127,7 +132,7 @@ public String constructUrlFromEndpointPortDatabase(String endpoint, String port, @Override public String getDefaultDriverClass() { - return "com.microsoft.sqlserver.jdbc.SQLServerDriver"; + return DEFAULT_DRIVER; } } diff --git a/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMariaDBDriver.java b/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMariaDBDriver.java index 954c872..d5f49e2 100644 --- a/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMariaDBDriver.java +++ b/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMariaDBDriver.java @@ -46,6 +46,11 @@ public final class AWSSecretsManagerMariaDBDriver extends AWSSecretsManagerDrive */ public static final String SUBPREFIX = "mariadb"; + /** + * Default driver class to use. + */ + public static final String DEFAULT_DRIVER = "org.mariadb.jdbc.Driver"; + static { AWSSecretsManagerDriver.register(new AWSSecretsManagerMariaDBDriver()); } @@ -122,6 +127,6 @@ public String constructUrlFromEndpointPortDatabase(String endpoint, String port, @Override public String getDefaultDriverClass() { - return "org.mariadb.jdbc.Driver"; + return DEFAULT_DRIVER; } } diff --git a/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMySQLDriver.java b/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMySQLDriver.java index 51b111d..16b43b2 100644 --- a/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMySQLDriver.java +++ b/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMySQLDriver.java @@ -122,8 +122,12 @@ public String constructUrlFromEndpointPortDatabase(String endpoint, String port, @Override public String getDefaultDriverClass() { + return getStaticDefaultDriverClass(); + } + + public static String getStaticDefaultDriverClass() { try { - Class.forName("com.mysql.cj.jdbc.Driver", false, this.getClass().getClassLoader()); + Class.forName("com.mysql.cj.jdbc.Driver", false, AWSSecretsManagerMySQLDriver.class.getClassLoader()); return "com.mysql.cj.jdbc.Driver"; } catch (ClassNotFoundException e) { return "com.mysql.jdbc.Driver"; diff --git a/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerOracleDriver.java b/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerOracleDriver.java index 834fe27..b44b9e1 100644 --- a/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerOracleDriver.java +++ b/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerOracleDriver.java @@ -61,6 +61,11 @@ public final class AWSSecretsManagerOracleDriver extends AWSSecretsManagerDriver */ public static final String SUBPREFIX = "oracle"; + /** + * Default driver class to use. + */ + public static final String DEFAULT_DRIVER = "oracle.jdbc.OracleDriver"; + static { AWSSecretsManagerDriver.register(new AWSSecretsManagerOracleDriver()); } @@ -144,7 +149,7 @@ public String constructUrlFromEndpointPortDatabase(String endpoint, String port, @Override public String getDefaultDriverClass() { - return "oracle.jdbc.OracleDriver"; + return DEFAULT_DRIVER; } } diff --git a/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerPostgreSQLDriver.java b/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerPostgreSQLDriver.java index bfd2d6d..9f05691 100644 --- a/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerPostgreSQLDriver.java +++ b/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerPostgreSQLDriver.java @@ -43,6 +43,11 @@ public final class AWSSecretsManagerPostgreSQLDriver extends AWSSecretsManagerDr */ public static final String SUBPREFIX = "postgresql"; + /** + * Default driver class to use. + */ + public static final String DEFAULT_DRIVER = "org.postgresql.Driver"; + static { AWSSecretsManagerDriver.register(new AWSSecretsManagerPostgreSQLDriver()); } @@ -124,7 +129,7 @@ public String constructUrlFromEndpointPortDatabase(String endpoint, String port, @Override public String getDefaultDriverClass() { - return "org.postgresql.Driver"; + return DEFAULT_DRIVER; } } diff --git a/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerRedshiftDriver.java b/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerRedshiftDriver.java index e0e8dae..64cb3bf 100644 --- a/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerRedshiftDriver.java +++ b/src/main/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerRedshiftDriver.java @@ -43,6 +43,11 @@ public final class AWSSecretsManagerRedshiftDriver extends AWSSecretsManagerDriv public static final String SUBPREFIX = "redshift"; + /** + * Default driver class to use. + */ + public static final String DEFAULT_DRIVER = "com.amazon.redshift.Driver"; + static { AWSSecretsManagerDriver.register(new AWSSecretsManagerRedshiftDriver()); } @@ -129,6 +134,6 @@ public String constructUrlFromEndpointPortDatabase(String endpoint, String port, @Override public String getDefaultDriverClass() { - return "com.amazon.redshift.Driver"; + return DEFAULT_DRIVER; } } diff --git a/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerDriverTest.java b/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerDriverTest.java index 27af579..e084075 100644 --- a/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerDriverTest.java +++ b/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerDriverTest.java @@ -139,9 +139,10 @@ public void test_init_constructor_null_params() { @Test public void test_init_works_realDriverFromConfig() { - System.setProperty("drivers.dummy.realDriverClass", "some.other.class"); + String driver = "com.amazonaws.secretsmanager.sql.EmptyDriver"; + System.setProperty("drivers.dummy.realDriverClass", driver); AWSSecretsManagerDummyDriver sut2 = new AWSSecretsManagerDummyDriver(cache); - assertEquals(getFieldFrom(sut2, "realDriverClass"), "some.other.class"); + assertEquals(getFieldFrom(sut2, "realDriverClass"), driver); } /******************************************************************************************************************* diff --git a/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMSSQLServerDriverTest.java b/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMSSQLServerDriverTest.java index e2f644a..ddd3980 100644 --- a/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMSSQLServerDriverTest.java +++ b/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMSSQLServerDriverTest.java @@ -32,7 +32,7 @@ * Tests for the MSSQL Driver. */ @RunWith(PowerMockRunner.class) -@SuppressStaticInitializationFor("com.amazonaws.secretsmanager.sql.AWSSecretsManagerMSSQLServerDriver") +@SuppressStaticInitializationFor({"com.amazonaws.secretsmanager.sql.AWSSecretsManagerMSSQLServerDriver","com.amazonaws.secretsmanager.sql.*"}) @PowerMockIgnore("jdk.internal.reflect.*") public class AWSSecretsManagerMSSQLServerDriverTest extends TestClass { @@ -49,6 +49,8 @@ public void setup() { sut = new AWSSecretsManagerMSSQLServerDriver(cache); } catch (Exception e) { throw new RuntimeException(e); + } catch (Throwable t) { + t.printStackTrace(); } } @@ -99,8 +101,7 @@ public void test_constructUrlNullDatabase() { @Test public void test_getDefaultDriverClass() { System.clearProperty("drivers.sqlserver.realDriverClass"); - AWSSecretsManagerMSSQLServerDriver sut2 = new AWSSecretsManagerMSSQLServerDriver(cache); - assertEquals(getFieldFrom(sut2, "realDriverClass"), sut2.getDefaultDriverClass()); + assertThrows(IllegalStateException.class, "Could not load real driver with name, \"" + AWSSecretsManagerMSSQLServerDriver.DEFAULT_DRIVER + "\".", () -> new AWSSecretsManagerMSSQLServerDriver(cache)); } } diff --git a/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMariaDBDriverTest.java b/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMariaDBDriverTest.java index 6baad6a..b9bb072 100644 --- a/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMariaDBDriverTest.java +++ b/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMariaDBDriverTest.java @@ -32,7 +32,7 @@ * Tests for the MariaDB Driver. */ @RunWith(PowerMockRunner.class) -@SuppressStaticInitializationFor("com.amazonaws.secretsmanager.sql.AWSSecretsManagerMariaDBDriver") +@SuppressStaticInitializationFor({"com.amazonaws.secretsmanager.sql.AWSSecretsManagerMariaDBDriver","com.amazonaws.secretsmanager.sql.*"}) @PowerMockIgnore("jdk.internal.reflect.*") public class AWSSecretsManagerMariaDBDriverTest extends TestClass { @@ -99,8 +99,7 @@ public void test_constructUrlNullDatabase() { @Test public void test_getDefaultDriverClass() { System.clearProperty("drivers.mariadb.realDriverClass"); - AWSSecretsManagerMariaDBDriver sut2 = new AWSSecretsManagerMariaDBDriver(cache); - assertEquals(getFieldFrom(sut2, "realDriverClass"), sut2.getDefaultDriverClass()); + assertThrows(IllegalStateException.class, "Could not load real driver with name, \"" + AWSSecretsManagerMariaDBDriver.DEFAULT_DRIVER + "\".", () -> new AWSSecretsManagerMariaDBDriver(cache)); } } diff --git a/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMySQLDriverTest.java b/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMySQLDriverTest.java index 55e34df..ac92ded 100644 --- a/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMySQLDriverTest.java +++ b/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerMySQLDriverTest.java @@ -32,7 +32,7 @@ * Tests for the MySQL Driver. */ @RunWith(PowerMockRunner.class) -@SuppressStaticInitializationFor("com.amazonaws.secretsmanager.sql.AWSSecretsManagerMySQLDriver") +@SuppressStaticInitializationFor({"com.amazonaws.secretsmanager.sql.AWSSecretsManagerMySQLDriver","com.amazonaws.secretsmanager.sql.*"}) @PowerMockIgnore("jdk.internal.reflect.*") public class AWSSecretsManagerMySQLDriverTest extends TestClass { @@ -99,8 +99,7 @@ public void test_constructUrlNullDatabase() { @Test public void test_getDefaultDriverClass() { System.clearProperty("drivers.mysql.realDriverClass"); - AWSSecretsManagerMySQLDriver sut2 = new AWSSecretsManagerMySQLDriver(cache); - assertEquals(getFieldFrom(sut2, "realDriverClass"), sut2.getDefaultDriverClass()); + assertThrows(IllegalStateException.class, "Could not load real driver with name, \"" + AWSSecretsManagerMySQLDriver.getStaticDefaultDriverClass() + "\".", () -> new AWSSecretsManagerMySQLDriver(cache)); } } diff --git a/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerOracleDriverTest.java b/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerOracleDriverTest.java index 510ac84..483f9a6 100644 --- a/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerOracleDriverTest.java +++ b/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerOracleDriverTest.java @@ -32,7 +32,7 @@ * Tests for the Oracle Driver. */ @RunWith(PowerMockRunner.class) -@SuppressStaticInitializationFor("com.amazonaws.secretsmanager.sql.AWSSecretsManagerOracleDriver") +@SuppressStaticInitializationFor({"com.amazonaws.secretsmanager.sql.AWSSecretsManagerOracleDriver","com.amazonaws.secretsmanager.sql.*"}) @PowerMockIgnore("jdk.internal.reflect.*") public class AWSSecretsManagerOracleDriverTest extends TestClass { @@ -104,8 +104,7 @@ public void test_constructUrlNullDatabase() { @Test public void test_getDefaultDriverClass() { System.clearProperty("drivers.oracle.realDriverClass"); - AWSSecretsManagerOracleDriver sut2 = new AWSSecretsManagerOracleDriver(cache); - assertEquals(getFieldFrom(sut2, "realDriverClass"), sut2.getDefaultDriverClass()); + assertThrows(IllegalStateException.class, "Could not load real driver with name, \"" + AWSSecretsManagerOracleDriver.DEFAULT_DRIVER + "\".", () -> new AWSSecretsManagerOracleDriver(cache)); } } diff --git a/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerPostgreSQLDriverTest.java b/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerPostgreSQLDriverTest.java index d6239dd..c9a43af 100644 --- a/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerPostgreSQLDriverTest.java +++ b/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerPostgreSQLDriverTest.java @@ -32,7 +32,7 @@ * Tests for the PostgreSQL Driver. */ @RunWith(PowerMockRunner.class) -@SuppressStaticInitializationFor("com.amazonaws.secretsmanager.sql.AWSSecretsManagerPostgreSQLDriver") +@SuppressStaticInitializationFor({"com.amazonaws.secretsmanager.sql.AWSSecretsManagerPostgreSQLDriver","com.amazonaws.secretsmanager.sql.*"}) @PowerMockIgnore("jdk.internal.reflect.*") public class AWSSecretsManagerPostgreSQLDriverTest extends TestClass { @@ -99,8 +99,7 @@ public void test_constructUrlNullDatabase() { @Test public void test_getDefaultDriverClass() { System.clearProperty("drivers.postgresql.realDriverClass"); - AWSSecretsManagerPostgreSQLDriver sut2 = new AWSSecretsManagerPostgreSQLDriver(cache); - assertEquals(getFieldFrom(sut2, "realDriverClass"), sut2.getDefaultDriverClass()); + assertThrows(IllegalStateException.class, "Could not load real driver with name, \"" + AWSSecretsManagerPostgreSQLDriver.DEFAULT_DRIVER + "\".", () -> new AWSSecretsManagerPostgreSQLDriver(cache)); } } diff --git a/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerRedshiftDriverTest.java b/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerRedshiftDriverTest.java index ac70def..0c59e6f 100644 --- a/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerRedshiftDriverTest.java +++ b/src/test/java/com/amazonaws/secretsmanager/sql/AWSSecretsManagerRedshiftDriverTest.java @@ -32,7 +32,7 @@ * Tests for the Redshift Driver. */ @RunWith(PowerMockRunner.class) -@SuppressStaticInitializationFor("com.amazonaws.secretsmanager.sql.AWSSecretsManagerRedshiftDriver") +@SuppressStaticInitializationFor({"com.amazonaws.secretsmanager.sql.AWSSecretsManagerRedshiftDriver","com.amazonaws.secretsmanager.sql.*"}) @PowerMockIgnore("jdk.internal.reflect.*") public class AWSSecretsManagerRedshiftDriverTest extends TestClass { @@ -99,7 +99,6 @@ public void test_constructUrlNullDatabase() { @Test public void test_getDefaultDriverClass() { System.clearProperty("drivers.redshift.realDriverClass"); - AWSSecretsManagerRedshiftDriver sut2 = new AWSSecretsManagerRedshiftDriver(cache); - assertEquals(getFieldFrom(sut2, "realDriverClass"), sut2.getDefaultDriverClass()); + assertThrows(IllegalStateException.class, "Could not load real driver with name, \"" + AWSSecretsManagerRedshiftDriver.DEFAULT_DRIVER + "\".", () -> new AWSSecretsManagerRedshiftDriver(cache)); } } diff --git a/src/test/java/com/amazonaws/secretsmanager/sql/EmptyDriver.java b/src/test/java/com/amazonaws/secretsmanager/sql/EmptyDriver.java new file mode 100644 index 0000000..126004b --- /dev/null +++ b/src/test/java/com/amazonaws/secretsmanager/sql/EmptyDriver.java @@ -0,0 +1,65 @@ +package com.amazonaws.secretsmanager.sql; + +import java.sql.Connection; +import java.sql.Driver; +import java.sql.DriverManager; +import java.sql.DriverPropertyInfo; +import java.sql.SQLException; +import java.sql.SQLFeatureNotSupportedException; +import java.util.Properties; +import java.util.logging.Logger; + +public class EmptyDriver implements Driver { + public static EmptyDriver instance; + + static { + instance = new EmptyDriver(); + try { + DriverManager.registerDriver(instance); + } catch (SQLException e) { + throw new RuntimeException("Driver could not be registered.", e); + } + } + + @Override + public boolean acceptsURL(String arg0) throws SQLException { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'acceptsURL'"); + } + + @Override + public Connection connect(String arg0, Properties arg1) throws SQLException { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'connect'"); + } + + @Override + public int getMajorVersion() { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'getMajorVersion'"); + } + + @Override + public int getMinorVersion() { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'getMinorVersion'"); + } + + @Override + public Logger getParentLogger() throws SQLFeatureNotSupportedException { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'getParentLogger'"); + } + + @Override + public DriverPropertyInfo[] getPropertyInfo(String arg0, Properties arg1) throws SQLException { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'getPropertyInfo'"); + } + + @Override + public boolean jdbcCompliant() { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'jdbcCompliant'"); + } +} diff --git a/src/test/java/com/amazonaws/secretsmanager/util/TestClass.java b/src/test/java/com/amazonaws/secretsmanager/util/TestClass.java index fa6cdd7..a49032d 100644 --- a/src/test/java/com/amazonaws/secretsmanager/util/TestClass.java +++ b/src/test/java/com/amazonaws/secretsmanager/util/TestClass.java @@ -176,6 +176,18 @@ public void assertThrows(Exception exception, throwingRunnable code) { } } + public void assertThrows(Class exception, String message, throwingRunnable code) { + try { + code.run(); + throw new RuntimeException("Should have thrown a " + exception.getName() + " but threw nothing."); + } catch (Exception e) { + if (!exception.isAssignableFrom(e.getClass()) && !message.equals(e.getMessage())) { + e.printStackTrace(); + throw new RuntimeException("Should have thrown a " + exception.getName() + " with message " + message + " but threw " + e.getClass().getName() + " with message " + e.getMessage()); + } + } + } + public void assertNotThrows(throwingRunnable code) { try { code.run();