Skip to content

Commit 322133c

Browse files
committed
added unit tests for the new introduced pattern
1 parent 5464be5 commit 322133c

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/main/java/com/google/cloud/spanner/jdbc/JdbcDriver.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.google.cloud.spanner.connection.ConnectionOptionsHelper;
2626
import com.google.cloud.spanner.connection.ConnectionPropertiesHelper;
2727
import com.google.cloud.spanner.connection.ConnectionProperty;
28+
import com.google.common.annotations.VisibleForTesting;
2829
import com.google.rpc.Code;
2930
import io.opentelemetry.api.OpenTelemetry;
3031
import java.sql.Connection;
@@ -147,8 +148,9 @@ public class JdbcDriver implements Driver {
147148
private static final Pattern URL_PATTERN = Pattern.compile(JDBC_URL_FORMAT);
148149
private static final String JDBC_EXTERNAL_HOST_FORMAT =
149150
"jdbc:" + ConnectionOptions.Builder.EXTERNAL_HOST_FORMAT;
150-
private static final Pattern EXTERNAL_HOST_URL_PATTERN =
151-
Pattern.compile(JDBC_EXTERNAL_HOST_FORMAT);
151+
152+
@VisibleForTesting
153+
static final Pattern EXTERNAL_HOST_URL_PATTERN = Pattern.compile(JDBC_EXTERNAL_HOST_FORMAT);
152154

153155
@InternalApi
154156
public static String getClientLibToken() {

src/test/java/com/google/cloud/spanner/jdbc/JdbcDriverTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616

1717
package com.google.cloud.spanner.jdbc;
1818

19+
import static com.google.cloud.spanner.jdbc.JdbcDriver.EXTERNAL_HOST_URL_PATTERN;
1920
import static com.google.common.truth.Truth.assertThat;
2021
import static org.junit.Assert.assertEquals;
22+
import static org.junit.Assert.assertFalse;
2123
import static org.junit.Assert.assertNotNull;
2224
import static org.junit.Assert.assertTrue;
2325
import static org.junit.Assert.fail;
@@ -47,6 +49,7 @@
4749
import java.util.Collection;
4850
import java.util.Objects;
4951
import java.util.Properties;
52+
import java.util.regex.Matcher;
5053
import org.junit.AfterClass;
5154
import org.junit.BeforeClass;
5255
import org.junit.Test;
@@ -211,4 +214,32 @@ public void testLenient() throws SQLException {
211214
assertThat(jdbc.getCode()).isEqualTo(Code.INVALID_ARGUMENT);
212215
}
213216
}
217+
218+
@Test
219+
public void testJdbcExternalHostFormat() {
220+
Matcher matcherWithoutInstance =
221+
EXTERNAL_HOST_URL_PATTERN.matcher("jdbc:cloudspanner://localhost:15000/databases/test-db");
222+
assertTrue(matcherWithoutInstance.matches());
223+
assertEquals("test-db", matcherWithoutInstance.group("DATABASEGROUP"));
224+
Matcher matcherWithProperty =
225+
EXTERNAL_HOST_URL_PATTERN.matcher(
226+
"jdbc:cloudspanner://localhost:15000/instances/default/databases/singers-db?usePlainText=true");
227+
assertTrue(matcherWithProperty.matches());
228+
assertEquals("default", matcherWithProperty.group("INSTANCEGROUP"));
229+
assertEquals("singers-db", matcherWithProperty.group("DATABASEGROUP"));
230+
Matcher matcherWithoutPort =
231+
EXTERNAL_HOST_URL_PATTERN.matcher(
232+
"jdbc:cloudspanner://localhost/instances/default/databases/test-db");
233+
assertTrue(matcherWithoutPort.matches());
234+
assertEquals("default", matcherWithoutPort.group("INSTANCEGROUP"));
235+
assertEquals("test-db", matcherWithoutPort.group("DATABASEGROUP"));
236+
Matcher matcherWithProject =
237+
EXTERNAL_HOST_URL_PATTERN.matcher(
238+
"jdbc:cloudspanner://localhost:15000/projects/default/instances/default/databases/singers-db");
239+
assertFalse(matcherWithProject.matches());
240+
Matcher matcherWithoutHost =
241+
EXTERNAL_HOST_URL_PATTERN.matcher(
242+
"jdbc:cloudspanner:/instances/default/databases/singers-db");
243+
assertFalse(matcherWithoutHost.matches());
244+
}
214245
}

0 commit comments

Comments
 (0)