Skip to content

Commit

Permalink
ARROW-18296: [Java] Honor Driver#connect API contract in JDBC driver (#…
Browse files Browse the repository at this point in the history
…14617)

Authored-by: David Li <li.davidm96@gmail.com>
Signed-off-by: David Li <li.davidm96@gmail.com>
  • Loading branch information
lidavidm committed Nov 10, 2022
1 parent 0a6922d commit bbd78fd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.sql.SQLException;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;

import org.apache.arrow.driver.jdbc.utils.ArrowFlightConnectionConfigImpl.ArrowFlightConnectionProperty;
Expand Down Expand Up @@ -72,7 +73,11 @@ public ArrowFlightConnection connect(final String url, final Properties info)
properties.putAll(info);

if (url != null) {
final Map<Object, Object> propertiesFromUrl = getUrlsArgs(url);
final Optional<Map<Object, Object>> maybeProperties = getUrlsArgs(url);
if (!maybeProperties.isPresent()) {
return null;
}
final Map<Object, Object> propertiesFromUrl = maybeProperties.get();
properties.putAll(propertiesFromUrl);
}

Expand Down Expand Up @@ -199,11 +204,11 @@ public boolean acceptsURL(final String url) {
* </table>
*
* @param url The url to parse.
* @return the parsed arguments.
* @return the parsed arguments, or an empty optional if the driver does not handle this URL.
* @throws SQLException If an error occurs while trying to parse the URL.
*/
@VisibleForTesting // ArrowFlightJdbcDriverTest
Map<Object, Object> getUrlsArgs(String url)
Optional<Map<Object, Object>> getUrlsArgs(String url)
throws SQLException {

/*
Expand Down Expand Up @@ -240,8 +245,7 @@ Map<Object, Object> getUrlsArgs(String url)

if (!Objects.equals(uri.getScheme(), "arrow-flight") &&
!Objects.equals(uri.getScheme(), "arrow-flight-sql")) {
throw new SQLException("URL Scheme must be 'arrow-flight'. Expected format: " +
CONNECTION_STRING_EXPECTED);
return Optional.empty();
}

if (uri.getHost() == null) {
Expand All @@ -258,7 +262,7 @@ Map<Object, Object> getUrlsArgs(String url)
resultMap.putAll(keyValuePairs);
}

return resultMap;
return Optional.of(resultMap);
}

static Properties lowerCasePropertyKeys(final Properties properties) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@

package org.apache.arrow.driver.jdbc;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.sql.Connection;
import java.sql.Driver;
Expand Down Expand Up @@ -91,17 +93,15 @@ public void testDriverIsRegisteredInDriverManager() throws Exception {
}

/**
* Tests whether the {@link ArrowFlightJdbcDriver} fails when provided with an
* Tests whether the {@link ArrowFlightJdbcDriver} returns null when provided with an
* unsupported URL prefix.
*
* @throws SQLException If the test passes.
*/
@Test(expected = SQLException.class)
@Test
public void testShouldDeclineUrlWithUnsupportedPrefix() throws Exception {
final Driver driver = new ArrowFlightJdbcDriver();

driver.connect("jdbc:mysql://localhost:32010", dataSource.getProperties("flight", "flight123"))
.close();
assertNull(driver.connect("jdbc:mysql://localhost:32010",
dataSource.getProperties("flight", "flight123")));
}

/**
Expand Down Expand Up @@ -263,7 +263,8 @@ public void testDriverUrlParsingMechanismShouldReturnTheDesiredArgsFromUrl() thr
final ArrowFlightJdbcDriver driver = new ArrowFlightJdbcDriver();

final Map<Object, Object> parsedArgs = driver.getUrlsArgs(
"jdbc:arrow-flight-sql://localhost:2222/?key1=value1&key2=value2&a=b");
"jdbc:arrow-flight-sql://localhost:2222/?key1=value1&key2=value2&a=b")
.orElseThrow(() -> new RuntimeException("URL was rejected"));

// Check size == the amount of args provided (scheme not included)
assertEquals(5, parsedArgs.size());
Expand All @@ -284,7 +285,8 @@ public void testDriverUrlParsingMechanismShouldReturnTheDesiredArgsFromUrl() thr
public void testDriverUrlParsingMechanismShouldReturnTheDesiredArgsFromUrlWithSemicolon() throws Exception {
final ArrowFlightJdbcDriver driver = new ArrowFlightJdbcDriver();
final Map<Object, Object> parsedArgs = driver.getUrlsArgs(
"jdbc:arrow-flight-sql://localhost:2222/;key1=value1;key2=value2;a=b");
"jdbc:arrow-flight-sql://localhost:2222/;key1=value1;key2=value2;a=b")
.orElseThrow(() -> new RuntimeException("URL was rejected"));

// Check size == the amount of args provided (scheme not included)
assertEquals(5, parsedArgs.size());
Expand All @@ -305,7 +307,8 @@ public void testDriverUrlParsingMechanismShouldReturnTheDesiredArgsFromUrlWithSe
public void testDriverUrlParsingMechanismShouldReturnTheDesiredArgsFromUrlWithOneSemicolon() throws Exception {
final ArrowFlightJdbcDriver driver = new ArrowFlightJdbcDriver();
final Map<Object, Object> parsedArgs = driver.getUrlsArgs(
"jdbc:arrow-flight-sql://localhost:2222/;key1=value1");
"jdbc:arrow-flight-sql://localhost:2222/;key1=value1")
.orElseThrow(() -> new RuntimeException("URL was rejected"));

// Check size == the amount of args provided (scheme not included)
assertEquals(3, parsedArgs.size());
Expand All @@ -320,16 +323,10 @@ public void testDriverUrlParsingMechanismShouldReturnTheDesiredArgsFromUrlWithOn
assertEquals(parsedArgs.get("key1"), "value1");
}

/**
* Tests whether an exception is thrown upon attempting to connect to a
* malformed URI.
*
*/
@Test
public void testDriverUrlParsingMechanismShouldThrowExceptionUponProvidedWithMalformedUrl() {
public void testDriverUrlParsingMechanismShouldReturnEmptyOptionalForUnknownScheme() throws SQLException {
final ArrowFlightJdbcDriver driver = new ArrowFlightJdbcDriver();
assertThrows(SQLException.class, () -> driver.getUrlsArgs(
"jdbc:malformed-url-flight://localhost:2222"));
assertFalse(driver.getUrlsArgs("jdbc:malformed-url-flight://localhost:2222").isPresent());
}

/**
Expand All @@ -341,7 +338,8 @@ public void testDriverUrlParsingMechanismShouldThrowExceptionUponProvidedWithMal
@Test
public void testDriverUrlParsingMechanismShouldWorkWithIPAddress() throws Exception {
final ArrowFlightJdbcDriver driver = new ArrowFlightJdbcDriver();
final Map<Object, Object> parsedArgs = driver.getUrlsArgs("jdbc:arrow-flight-sql://0.0.0.0:2222");
final Map<Object, Object> parsedArgs = driver.getUrlsArgs("jdbc:arrow-flight-sql://0.0.0.0:2222")
.orElseThrow(() -> new RuntimeException("URL was rejected"));

// Check size == the amount of args provided (scheme not included)
assertEquals(2, parsedArgs.size());
Expand All @@ -364,7 +362,8 @@ public void testDriverUrlParsingMechanismShouldWorkWithEmbeddedEspecialCharacter
throws Exception {
final ArrowFlightJdbcDriver driver = new ArrowFlightJdbcDriver();
final Map<Object, Object> parsedArgs = driver.getUrlsArgs(
"jdbc:arrow-flight-sql://0.0.0.0:2222?test1=test1value&test2%26continue=test2value&test3=test3value");
"jdbc:arrow-flight-sql://0.0.0.0:2222?test1=test1value&test2%26continue=test2value&test3=test3value")
.orElseThrow(() -> new RuntimeException("URL was rejected"));

// Check size == the amount of args provided (scheme not included)
assertEquals(5, parsedArgs.size());
Expand Down

0 comments on commit bbd78fd

Please sign in to comment.