This repository was archived by the owner on Mar 26, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathIsolationLevelTest.java
More file actions
97 lines (84 loc) · 3.27 KB
/
Copy pathIsolationLevelTest.java
File metadata and controls
97 lines (84 loc) · 3.27 KB
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
/*
* Copyright 2025 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.example.spanner.jdbc;
import static com.example.spanner.jdbc.IsolationLevel.isolationLevel;
import static org.junit.Assume.assumeTrue;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.testcontainers.DockerClientFactory;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.images.PullPolicy;
import org.testcontainers.utility.DockerImageName;
@RunWith(JUnit4.class)
public class IsolationLevelTest {
private static GenericContainer<?> emulator;
private static final String PROJECT = "emulator-project";
private static final String INSTANCE = "my-instance";
private static final String DATABASE = "my-database";
private static final Properties PROPERTIES = new Properties();
@BeforeClass
public static void setupEmulator() throws Exception {
assumeTrue("This test requires Docker", DockerClientFactory.instance().isDockerAvailable());
emulator =
new GenericContainer<>(DockerImageName.parse("gcr.io/cloud-spanner-emulator/emulator"));
emulator.withImagePullPolicy(PullPolicy.alwaysPull());
emulator.addExposedPort(9010);
emulator.setWaitStrategy(Wait.forListeningPorts(9010));
emulator.start();
PROPERTIES.setProperty("endpoint",
String.format("localhost:%d", emulator.getMappedPort(9010)));
PROPERTIES.setProperty("autoConfigEmulator", "true");
String url = String.format(
"jdbc:cloudspanner:/projects/%s/instances/%s/databases/%s",
PROJECT, INSTANCE, DATABASE);
try (Connection connection = DriverManager.getConnection(url, PROPERTIES)) {
try (Statement statement = connection.createStatement()) {
statement.addBatch(
"CREATE TABLE Singers ("
+ "SingerId INT64 PRIMARY KEY, "
+ "FirstName STRING(MAX), "
+ "LastName STRING(MAX), "
+ "Active BOOL)");
statement.addBatch(
"CREATE TABLE SingerHistory ("
+ "SingerId INT64, "
+ "Active BOOL, "
+ "CreatedAt TIMESTAMP) "
+ "PRIMARY KEY (SingerId, CreatedAt)");
statement.executeBatch();
}
}
}
@AfterClass
public static void stopEmulator() {
if (emulator != null) {
emulator.stop();
}
}
@Test
public void testIsolationLevel() throws SQLException {
isolationLevel("emulator-project", "my-instance", "my-database", PROPERTIES);
}
}