Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public JdbcDriver() {}

@Override
public Connection connect(String url, Properties info) throws SQLException {
if (url != null && url.startsWith("jdbc:cloudspanner")) {
if (url != null && (url.startsWith("jdbc:cloudspanner") || url.startsWith("jdbc:spanner"))) {
try {
Matcher matcher = URL_PATTERN.matcher(url);
Matcher matcherExternalHost = EXTERNAL_HOST_URL_PATTERN.matcher(url);
Expand Down
45 changes: 33 additions & 12 deletions src/test/java/com/google/cloud/spanner/jdbc/JdbcDriverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import java.util.Objects;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
Expand Down Expand Up @@ -135,13 +136,14 @@ public void testRegister() throws SQLException {

@Test
public void testConnect() throws SQLException {
try (Connection connection =
DriverManager.getConnection(
String.format(
"jdbc:cloudspanner://localhost:%d/projects/some-company.com:test-project/instances/static-test-instance/databases/test-database;usePlainText=true;credentials=%s",
server.getPort(), TEST_KEY_PATH))) {
assertThat(connection.isClosed()).isFalse();
}
for (String prefix : new String[] {"cloudspanner", "spanner"})
try (Connection connection =
DriverManager.getConnection(
String.format(
"jdbc:%s://localhost:%d/projects/some-company.com:test-project/instances/static-test-instance/databases/test-database;usePlainText=true;credentials=%s",
prefix, server.getPort(), TEST_KEY_PATH))) {
assertThat(connection.isClosed()).isFalse();
}
}

@Test(expected = SQLException.class)
Expand Down Expand Up @@ -215,31 +217,50 @@ public void testLenient() throws SQLException {
}
}

@Test
public void testAcceptsURL() throws SQLException {
JdbcDriver driver = JdbcDriver.getRegisteredDriver();
assertTrue(
driver.acceptsURL(
"jdbc:cloudspanner:/projects/my-project/instances/my-instance/databases/my-database"));
assertTrue(
driver.acceptsURL(
"jdbc:spanner:/projects/my-project/instances/my-instance/databases/my-database"));
}

@Test
public void testJdbcExternalHostFormat() {
Matcher matcher =
Pattern.compile("jdbc:\\(\\?:([a-z]+):.*").matcher(EXTERNAL_HOST_URL_PATTERN.pattern());
String prefix = matcher.matches() ? matcher.group(1) : null;
Matcher matcherWithoutInstance =
EXTERNAL_HOST_URL_PATTERN.matcher("jdbc:cloudspanner://localhost:15000/databases/test-db");
EXTERNAL_HOST_URL_PATTERN.matcher(
String.format("jdbc:%s://localhost:15000/databases/test-db", prefix));
assertTrue(matcherWithoutInstance.matches());
assertEquals("test-db", matcherWithoutInstance.group("DATABASEGROUP"));
Matcher matcherWithProperty =
EXTERNAL_HOST_URL_PATTERN.matcher(
"jdbc:cloudspanner://localhost:15000/instances/default/databases/singers-db?usePlainText=true");
String.format(
"jdbc:%s://localhost:15000/instances/default/databases/singers-db?usePlainText=true",
prefix));
assertTrue(matcherWithProperty.matches());
assertEquals("default", matcherWithProperty.group("INSTANCEGROUP"));
assertEquals("singers-db", matcherWithProperty.group("DATABASEGROUP"));
Matcher matcherWithoutPort =
EXTERNAL_HOST_URL_PATTERN.matcher(
"jdbc:cloudspanner://localhost/instances/default/databases/test-db");
String.format("jdbc:%s://localhost/instances/default/databases/test-db", prefix));
assertTrue(matcherWithoutPort.matches());
assertEquals("default", matcherWithoutPort.group("INSTANCEGROUP"));
assertEquals("test-db", matcherWithoutPort.group("DATABASEGROUP"));
Matcher matcherWithProject =
EXTERNAL_HOST_URL_PATTERN.matcher(
"jdbc:cloudspanner://localhost:15000/projects/default/instances/default/databases/singers-db");
String.format(
"jdbc:%s://localhost:15000/projects/default/instances/default/databases/singers-db",
prefix));
assertFalse(matcherWithProject.matches());
Matcher matcherWithoutHost =
EXTERNAL_HOST_URL_PATTERN.matcher(
"jdbc:cloudspanner:/instances/default/databases/singers-db");
String.format("jdbc:%s:/instances/default/databases/singers-db", prefix));
assertFalse(matcherWithoutHost.matches());
}
}
Loading