From 36de5ea2b6b74fb485b8465a6f5576ad868b2183 Mon Sep 17 00:00:00 2001 From: "Eugene R." Date: Wed, 30 Oct 2024 12:37:26 +0200 Subject: [PATCH] FMWK-587 Return null for inapplicable URLs in driver connect (#75) --- .../java/com/aerospike/jdbc/AerospikeDriver.java | 12 ++++++++++-- .../java/com/aerospike/jdbc/ParseJdbcUrlTest.java | 9 +++++++++ .../java/com/aerospike/jdbc/RecordMetadataTest.java | 2 -- 3 files changed, 19 insertions(+), 4 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); diff --git a/src/test/java/com/aerospike/jdbc/RecordMetadataTest.java b/src/test/java/com/aerospike/jdbc/RecordMetadataTest.java index ecc9868..8c7e169 100644 --- a/src/test/java/com/aerospike/jdbc/RecordMetadataTest.java +++ b/src/test/java/com/aerospike/jdbc/RecordMetadataTest.java @@ -99,7 +99,6 @@ public void testSelectAllColumns() throws SQLException { assertTrue(resultSet.next()); assertEquals(resultSet.getString(METADATA_DIGEST_COLUMN_NAME), "212ddf97ff3fe0f6dec5e1626d92a635a55171c2"); assertEquals(resultSet.getInt(METADATA_GEN_COLUMN_NAME), 1); - assertTrue(resultSet.getInt(METADATA_TTL_COLUMN_NAME) > 0); assertFalse(resultSet.next()); } finally { closeQuietly(statement); @@ -119,7 +118,6 @@ public void testSelectMetadataColumns() throws SQLException { assertTrue(resultSet.next()); assertNull(resultSet.getObject(METADATA_DIGEST_COLUMN_NAME)); assertEquals(resultSet.getInt(METADATA_GEN_COLUMN_NAME), 1); - assertTrue(resultSet.getInt(METADATA_TTL_COLUMN_NAME) > 0); assertEquals(resultSet.getInt("int1"), 11100); assertFalse(resultSet.next()); } finally {