forked from googleapis/java-spanner
/
JdbcDriverTest.java
104 lines (94 loc) · 3.75 KB
/
JdbcDriverTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.spanner.jdbc;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
import com.google.cloud.spanner.MockSpannerServiceImpl;
import io.grpc.Server;
import io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class JdbcDriverTest {
/**
* Make sure the JDBC driver class is loaded. This is needed when running the test using Maven.
*/
static {
try {
Class.forName("com.google.cloud.spanner.jdbc.JdbcDriver");
} catch (ClassNotFoundException e) {
throw new IllegalStateException(
"JdbcDataSource failed to load com.google.cloud.spanner.jdbc.JdbcDriver", e);
}
}
private static MockSpannerServiceImpl mockSpanner;
private static Server server;
private static InetSocketAddress address;
private static final String TEST_KEY_PATH =
ConnectionOptionsTest.class.getResource("test-key.json").getFile();
@BeforeClass
public static void startStaticServer() throws IOException {
mockSpanner = new MockSpannerServiceImpl();
address = new InetSocketAddress("localhost", 0);
server = NettyServerBuilder.forAddress(address).addService(mockSpanner).build().start();
}
@AfterClass
public static void stopServer() throws Exception {
SpannerPool.closeSpannerPool();
server.shutdown();
server.awaitTermination();
}
@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();
}
}
@Test(expected = SQLException.class)
public void testInvalidConnect() 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;credentialsUrl=%s",
server.getPort(), TEST_KEY_PATH))) {
assertThat(connection.isClosed()).isFalse();
}
}
@Test
public void testConnectWithCredentialsAndOAuthToken() throws SQLException {
try (Connection connection =
DriverManager.getConnection(
String.format(
"jdbc:cloudspanner://localhost:%d/projects/test-project/instances/static-test-instance/databases/test-database;usePlainText=true;credentials=%s;OAuthToken=%s",
server.getPort(), TEST_KEY_PATH, "some-token"))) {
fail("missing expected exception");
} catch (SQLException e) {
assertThat(e.getMessage()).contains("Cannot specify both credentials and an OAuth token");
}
}
}