Skip to content

Commit

Permalink
Fix up leaks in test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
jduo committed Nov 6, 2023
1 parent 67d5fee commit 528b39c
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 146 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,21 @@ public void testGetEncryptedClientWithBadMTlsCertPath() {
final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(
userTest, passTest);

assertThrows(SQLException.class, () -> new ArrowFlightSqlClientHandler.Builder()
.withHost(FLIGHT_SERVER_TEST_RULE.getHost())
.withPort(FLIGHT_SERVER_TEST_RULE.getPort())
.withUsername(credentials.getUserName())
.withPassword(credentials.getPassword())
.withTlsRootCertificates(tlsRootCertsPath)
.withClientCertificate(badClientMTlsCertPath)
.withClientKey(clientMTlsKeyPath)
.withBufferAllocator(allocator)
.withEncryption(true)
.build());
assertThrows(SQLException.class, () -> {
try (ArrowFlightSqlClientHandler handler = new ArrowFlightSqlClientHandler.Builder()
.withHost(FLIGHT_SERVER_TEST_RULE.getHost())
.withPort(FLIGHT_SERVER_TEST_RULE.getPort())
.withUsername(credentials.getUserName())
.withPassword(credentials.getPassword())
.withTlsRootCertificates(tlsRootCertsPath)
.withClientCertificate(badClientMTlsCertPath)
.withClientKey(clientMTlsKeyPath)
.withBufferAllocator(allocator)
.withEncryption(true)
.build()) {
Assert.fail();
}
});
}

/**
Expand All @@ -162,17 +166,21 @@ public void testGetEncryptedClientWithBadMTlsKeyPath() {
final UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(
userTest, passTest);

assertThrows(SQLException.class, () -> new ArrowFlightSqlClientHandler.Builder()
.withHost(FLIGHT_SERVER_TEST_RULE.getHost())
.withPort(FLIGHT_SERVER_TEST_RULE.getPort())
.withUsername(credentials.getUserName())
.withPassword(credentials.getPassword())
.withTlsRootCertificates(tlsRootCertsPath)
.withClientCertificate(clientMTlsCertPath)
.withClientKey(badClientMTlsKeyPath)
.withBufferAllocator(allocator)
.withEncryption(true)
.build());
assertThrows(SQLException.class, () -> {
try (ArrowFlightSqlClientHandler handler = new ArrowFlightSqlClientHandler.Builder()
.withHost(FLIGHT_SERVER_TEST_RULE.getHost())
.withPort(FLIGHT_SERVER_TEST_RULE.getPort())
.withUsername(credentials.getUserName())
.withPassword(credentials.getPassword())
.withTlsRootCertificates(tlsRootCertsPath)
.withClientCertificate(clientMTlsCertPath)
.withClientKey(badClientMTlsKeyPath)
.withBufferAllocator(allocator)
.withEncryption(true)
.build()) {
Assert.fail();
}
});
}

/**
Expand Down Expand Up @@ -222,7 +230,7 @@ public void testGetEncryptedConnectionWithValidCredentialsAndTlsRootsPath() thro
final ArrowFlightJdbcDataSource dataSource =
ArrowFlightJdbcDataSource.createNewDataSource(properties);
try (final Connection connection = dataSource.getConnection()) {
assert connection.isValid(300);
Assert.assertTrue(connection.isValid(300));
}
}

Expand All @@ -245,7 +253,7 @@ public void testGetNonAuthenticatedEncryptedConnection() throws Exception {

final ArrowFlightJdbcDataSource dataSource = ArrowFlightJdbcDataSource.createNewDataSource(properties);
try (final Connection connection = dataSource.getConnection()) {
assert connection.isValid(300);
Assert.assertTrue(connection.isValid(300));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.net.URISyntaxException;
import java.sql.Connection;
Expand Down Expand Up @@ -100,7 +101,7 @@ public void testUnencryptedConnectionShouldOpenSuccessfullyWhenProvidedValidCred
try (Connection connection = DriverManager.getConnection(
"jdbc:arrow-flight-sql://" + FLIGHT_SERVER_TEST_RULE.getHost() + ":" +
FLIGHT_SERVER_TEST_RULE.getPort(), properties)) {
assert connection.isValid(300);
Assert.assertTrue(connection.isValid(300));
}
}

Expand All @@ -122,11 +123,13 @@ public void testTokenOverridesUsernameAndPasswordAuth() {
properties.put(ArrowFlightConnectionProperty.TOKEN.camelName(), "token");
properties.put("useEncryption", false);

SQLException e = assertThrows(SQLException.class, () ->
DriverManager.getConnection(
"jdbc:arrow-flight-sql://" + FLIGHT_SERVER_TEST_RULE.getHost() + ":" +
FLIGHT_SERVER_TEST_RULE.getPort(),
properties));
SQLException e = assertThrows(SQLException.class, () -> {
try (Connection conn = DriverManager.getConnection(
"jdbc:arrow-flight-sql://" + FLIGHT_SERVER_TEST_RULE.getHost() + ":" + FLIGHT_SERVER_TEST_RULE.getPort(),
properties)) {
Assert.fail();
}
});
assertTrue(e.getMessage().contains("UNAUTHENTICATED"));
}

Expand All @@ -145,7 +148,9 @@ public void testUnencryptedConnectionWithEmptyHost()
properties.put("password", passTest);
final String invalidUrl = "jdbc:arrow-flight-sql://";

DriverManager.getConnection(invalidUrl, properties);
try (Connection conn = DriverManager.getConnection(invalidUrl, properties)) {
Assert.fail("Expected SQLException.");
}
}

/**
Expand Down Expand Up @@ -192,7 +197,9 @@ public void testUnencryptedConnectionProvidingInvalidPort()
final String invalidUrl = "jdbc:arrow-flight-sql://" + FLIGHT_SERVER_TEST_RULE.getHost() +
":" + 65537;

DriverManager.getConnection(invalidUrl, properties);
try (Connection conn = DriverManager.getConnection(invalidUrl, properties)) {
fail("Expected SQLException");
}
}

/**
Expand All @@ -207,6 +214,7 @@ public void testGetBasicClientNoAuthShouldOpenConnection() throws Exception {
new ArrowFlightSqlClientHandler.Builder()
.withHost(FLIGHT_SERVER_TEST_RULE.getHost())
.withBufferAllocator(allocator)
.withEncryption(false)
.build()) {
assertNotNull(client);
}
Expand All @@ -229,7 +237,7 @@ public void testUnencryptedConnectionShouldOpenSuccessfullyWithoutAuthentication
false);
try (Connection connection = DriverManager
.getConnection("jdbc:arrow-flight-sql://localhost:32010", properties)) {
assert connection.isValid(300);
Assert.assertTrue(connection.isValid(300));
}
}

Expand Down Expand Up @@ -272,14 +280,14 @@ public void testTLSConnectionPropertyFalseCorrectCastUrlWithDriverManager() thro
final Driver driver = new ArrowFlightJdbcDriver();
DriverManager.registerDriver(driver);

Connection connection = DriverManager.getConnection(
try (Connection connection = DriverManager.getConnection(
String.format(
"jdbc:arrow-flight-sql://localhost:%s?user=%s&password=%s&useEncryption=false",
FLIGHT_SERVER_TEST_RULE.getPort(),
userTest,
passTest));
Assert.assertTrue(connection.isValid(0));
connection.close();
passTest))) {
Assert.assertTrue(connection.isValid(0));
}
}

/**
Expand All @@ -302,13 +310,13 @@ public void testTLSConnectionPropertyFalseCorrectCastUrlAndPropertiesUsingSetPro
passTest);
properties.setProperty(ArrowFlightConnectionProperty.USE_ENCRYPTION.camelName(), "false");

Connection connection = DriverManager.getConnection(
try (Connection connection = DriverManager.getConnection(
String.format(
"jdbc:arrow-flight-sql://localhost:%s",
FLIGHT_SERVER_TEST_RULE.getPort()),
properties);
Assert.assertTrue(connection.isValid(0));
connection.close();
properties)) {
Assert.assertTrue(connection.isValid(0));
}
}

/**
Expand All @@ -330,13 +338,13 @@ public void testTLSConnectionPropertyFalseCorrectCastUrlAndPropertiesUsingPutWit
passTest);
properties.put(ArrowFlightConnectionProperty.USE_ENCRYPTION.camelName(), false);

Connection connection = DriverManager.getConnection(
try (Connection connection = DriverManager.getConnection(
String.format(
"jdbc:arrow-flight-sql://localhost:%s",
FLIGHT_SERVER_TEST_RULE.getPort()),
properties);
Assert.assertTrue(connection.isValid(0));
connection.close();
properties)) {
Assert.assertTrue(connection.isValid(0));
}
}

/**
Expand All @@ -351,14 +359,14 @@ public void testTLSConnectionPropertyFalseIntegerCorrectCastUrlWithDriverManager
final Driver driver = new ArrowFlightJdbcDriver();
DriverManager.registerDriver(driver);

Connection connection = DriverManager.getConnection(
try (Connection connection = DriverManager.getConnection(
String.format(
"jdbc:arrow-flight-sql://localhost:%s?user=%s&password=%s&useEncryption=0",
FLIGHT_SERVER_TEST_RULE.getPort(),
userTest,
passTest));
Assert.assertTrue(connection.isValid(0));
connection.close();
passTest))) {
Assert.assertTrue(connection.isValid(0));
}
}

/**
Expand All @@ -381,13 +389,13 @@ public void testTLSConnectionPropertyFalseIntegerCorrectCastUrlAndPropertiesUsin
passTest);
properties.setProperty(ArrowFlightConnectionProperty.USE_ENCRYPTION.camelName(), "0");

Connection connection = DriverManager.getConnection(
try (Connection connection = DriverManager.getConnection(
String.format(
"jdbc:arrow-flight-sql://localhost:%s",
FLIGHT_SERVER_TEST_RULE.getPort()),
properties);
Assert.assertTrue(connection.isValid(0));
connection.close();
properties)) {
Assert.assertTrue(connection.isValid(0));
}
}

/**
Expand All @@ -410,13 +418,13 @@ public void testTLSConnectionPropertyFalseIntegerCorrectCastUrlAndPropertiesUsin
passTest);
properties.put(ArrowFlightConnectionProperty.USE_ENCRYPTION.camelName(), 0);

Connection connection = DriverManager.getConnection(
try (Connection connection = DriverManager.getConnection(
String.format(
"jdbc:arrow-flight-sql://localhost:%s",
FLIGHT_SERVER_TEST_RULE.getPort()),
properties);
Assert.assertTrue(connection.isValid(0));
connection.close();
properties)) {
Assert.assertTrue(connection.isValid(0));
}
}

/**
Expand All @@ -431,15 +439,15 @@ public void testThreadPoolSizeConnectionPropertyCorrectCastUrlWithDriverManager(
final Driver driver = new ArrowFlightJdbcDriver();
DriverManager.registerDriver(driver);

Connection connection = DriverManager.getConnection(
try (Connection connection = DriverManager.getConnection(
String.format(
"jdbc:arrow-flight-sql://localhost:%s?user=%s&password=%s&threadPoolSize=1&useEncryption=%s",
FLIGHT_SERVER_TEST_RULE.getPort(),
userTest,
passTest,
false));
Assert.assertTrue(connection.isValid(0));
connection.close();
false))) {
Assert.assertTrue(connection.isValid(0));
}
}

/**
Expand All @@ -463,13 +471,13 @@ public void testThreadPoolSizeConnectionPropertyCorrectCastUrlAndPropertiesUsing
properties.setProperty(ArrowFlightConnectionProperty.THREAD_POOL_SIZE.camelName(), "1");
properties.put("useEncryption", false);

Connection connection = DriverManager.getConnection(
try (Connection connection = DriverManager.getConnection(
String.format(
"jdbc:arrow-flight-sql://localhost:%s",
FLIGHT_SERVER_TEST_RULE.getPort()),
properties);
Assert.assertTrue(connection.isValid(0));
connection.close();
properties)) {
Assert.assertTrue(connection.isValid(0));
}
}

/**
Expand All @@ -493,13 +501,13 @@ public void testThreadPoolSizeConnectionPropertyCorrectCastUrlAndPropertiesUsing
properties.put(ArrowFlightConnectionProperty.THREAD_POOL_SIZE.camelName(), 1);
properties.put("useEncryption", false);

Connection connection = DriverManager.getConnection(
try (Connection connection = DriverManager.getConnection(
String.format(
"jdbc:arrow-flight-sql://localhost:%s",
FLIGHT_SERVER_TEST_RULE.getPort()),
properties);
Assert.assertTrue(connection.isValid(0));
connection.close();
properties)) {
Assert.assertTrue(connection.isValid(0));
}
}

/**
Expand All @@ -514,15 +522,15 @@ public void testPasswordConnectionPropertyIntegerCorrectCastUrlWithDriverManager
final Driver driver = new ArrowFlightJdbcDriver();
DriverManager.registerDriver(driver);

Connection connection = DriverManager.getConnection(
try (Connection connection = DriverManager.getConnection(
String.format(
"jdbc:arrow-flight-sql://localhost:%s?user=%s&password=%s&useEncryption=%s",
FLIGHT_SERVER_TEST_RULE.getPort(),
userTest,
passTest,
false));
Assert.assertTrue(connection.isValid(0));
connection.close();
false))) {
Assert.assertTrue(connection.isValid(0));
}
}

/**
Expand All @@ -545,13 +553,13 @@ public void testPasswordConnectionPropertyIntegerCorrectCastUrlAndPropertiesUsin
passTest);
properties.put("useEncryption", false);

Connection connection = DriverManager.getConnection(
try (Connection connection = DriverManager.getConnection(
String.format(
"jdbc:arrow-flight-sql://localhost:%s",
FLIGHT_SERVER_TEST_RULE.getPort()),
properties);
Assert.assertTrue(connection.isValid(0));
connection.close();
properties)) {
Assert.assertTrue(connection.isValid(0));
}
}

/**
Expand All @@ -574,12 +582,12 @@ public void testPasswordConnectionPropertyIntegerCorrectCastUrlAndPropertiesUsin
passTest);
properties.put("useEncryption", false);

Connection connection = DriverManager.getConnection(
try (Connection connection = DriverManager.getConnection(
String.format(
"jdbc:arrow-flight-sql://localhost:%s",
FLIGHT_SERVER_TEST_RULE.getPort()),
properties);
Assert.assertTrue(connection.isValid(0));
connection.close();
properties)) {
Assert.assertTrue(connection.isValid(0));
}
}
}
Loading

0 comments on commit 528b39c

Please sign in to comment.