From c4a0e7eb5f2bf31e6ec32eb98f281b344601e7e0 Mon Sep 17 00:00:00 2001 From: yrizhkov Date: Wed, 30 Oct 2024 12:19:19 +0200 Subject: [PATCH] FMWK-587 Return null for inapplicable URLs in driver connect --- .../java/com/aerospike/jdbc/AerospikeDriver.java | 12 ++++++++++-- .../java/com/aerospike/jdbc/ParseJdbcUrlTest.java | 9 +++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/aerospike/jdbc/AerospikeDriver.java b/src/main/java/com/aerospike/jdbc/AerospikeDriver.java index ea3a252..4a2d042 100644 --- a/src/main/java/com/aerospike/jdbc/AerospikeDriver.java +++ b/src/main/java/com/aerospike/jdbc/AerospikeDriver.java @@ -21,6 +21,7 @@ public class AerospikeDriver implements Driver { private static final Logger logger = Logger.getLogger("com.aerospike.jdbc"); + private static final String AEROSPIKE_URL_PREFIX = "jdbc:aerospike:"; static { try { @@ -33,12 +34,19 @@ public class AerospikeDriver implements Driver { Log.setCallback(asLoggerCallback); } - public Connection connect(String url, Properties info) { + public Connection connect(String url, Properties info) throws SQLException { + if (url == null) { + throw new SQLException("url is null"); + } + if (!url.startsWith(AEROSPIKE_URL_PREFIX)) { + return null; + } + return new AerospikeConnection(url, info); } public boolean acceptsURL(String url) { - return Objects.nonNull(url) && url.startsWith("jdbc:aerospike:"); + return Objects.nonNull(url) && url.startsWith(AEROSPIKE_URL_PREFIX); } public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) { diff --git a/src/test/java/com/aerospike/jdbc/ParseJdbcUrlTest.java b/src/test/java/com/aerospike/jdbc/ParseJdbcUrlTest.java index 94228a4..69b1e20 100644 --- a/src/test/java/com/aerospike/jdbc/ParseJdbcUrlTest.java +++ b/src/test/java/com/aerospike/jdbc/ParseJdbcUrlTest.java @@ -11,6 +11,7 @@ import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertNull; import static org.testng.Assert.assertTrue; public class ParseJdbcUrlTest { @@ -70,6 +71,14 @@ public void testParseUrlParameters() throws Exception { assertEquals(config.getDriverPolicy().getRecordSetTimeoutMs(), 7000); } + @Test + public void testInapplicableUrl() throws Exception { + AerospikeDriver driver = (AerospikeDriver) Class.forName("com.aerospike.jdbc.AerospikeDriver") + .newInstance(); + String url = "jdbc:postgresql://localhost:5432/sample"; + assertNull(driver.connect(url, new Properties())); + } + private void assertTotalTimeoutAll(IAerospikeClient client, int timeout) { assertEquals(client.getReadPolicyDefault().totalTimeout, timeout); assertEquals(client.getWritePolicyDefault().totalTimeout, timeout);