|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.spanner.jdbc; |
| 18 | + |
| 19 | +import static com.example.spanner.jdbc.IsolationLevel.isolationLevel; |
| 20 | +import static org.junit.Assume.assumeTrue; |
| 21 | + |
| 22 | +import java.sql.Connection; |
| 23 | +import java.sql.DriverManager; |
| 24 | +import java.sql.SQLException; |
| 25 | +import java.sql.Statement; |
| 26 | +import java.util.Properties; |
| 27 | +import org.junit.AfterClass; |
| 28 | +import org.junit.BeforeClass; |
| 29 | +import org.junit.Test; |
| 30 | +import org.junit.runner.RunWith; |
| 31 | +import org.junit.runners.JUnit4; |
| 32 | +import org.testcontainers.DockerClientFactory; |
| 33 | +import org.testcontainers.containers.GenericContainer; |
| 34 | +import org.testcontainers.containers.wait.strategy.Wait; |
| 35 | +import org.testcontainers.images.PullPolicy; |
| 36 | +import org.testcontainers.utility.DockerImageName; |
| 37 | + |
| 38 | +@RunWith(JUnit4.class) |
| 39 | +public class IsolationLevelTest { |
| 40 | + |
| 41 | + private static GenericContainer<?> emulator; |
| 42 | + |
| 43 | + private static final String PROJECT = "emulator-project"; |
| 44 | + private static final String INSTANCE = "my-instance"; |
| 45 | + private static final String DATABASE = "my-database"; |
| 46 | + private static final Properties PROPERTIES = new Properties(); |
| 47 | + |
| 48 | + @BeforeClass |
| 49 | + public static void setupEmulator() throws Exception { |
| 50 | + assumeTrue("This test requires Docker", DockerClientFactory.instance().isDockerAvailable()); |
| 51 | + |
| 52 | + emulator = |
| 53 | + new GenericContainer<>(DockerImageName.parse("gcr.io/cloud-spanner-emulator/emulator")); |
| 54 | + emulator.withImagePullPolicy(PullPolicy.alwaysPull()); |
| 55 | + emulator.addExposedPort(9010); |
| 56 | + emulator.setWaitStrategy(Wait.forListeningPorts(9010)); |
| 57 | + emulator.start(); |
| 58 | + |
| 59 | + PROPERTIES.setProperty("endpoint", |
| 60 | + String.format("localhost:%d", emulator.getMappedPort(9010))); |
| 61 | + PROPERTIES.setProperty("autoConfigEmulator", "true"); |
| 62 | + |
| 63 | + String url = String.format( |
| 64 | + "jdbc:cloudspanner:/projects/%s/instances/%s/databases/%s", |
| 65 | + PROJECT, INSTANCE, DATABASE); |
| 66 | + try (Connection connection = DriverManager.getConnection(url, PROPERTIES)) { |
| 67 | + try (Statement statement = connection.createStatement()) { |
| 68 | + statement.addBatch( |
| 69 | + "CREATE TABLE Singers (" |
| 70 | + + "SingerId INT64 PRIMARY KEY, " |
| 71 | + + "FirstName STRING(MAX), " |
| 72 | + + "LastName STRING(MAX), " |
| 73 | + + "Active BOOL)"); |
| 74 | + statement.addBatch( |
| 75 | + "CREATE TABLE SingerHistory (" |
| 76 | + + "SingerId INT64, " |
| 77 | + + "Active BOOL, " |
| 78 | + + "CreatedAt TIMESTAMP) " |
| 79 | + + "PRIMARY KEY (SingerId, CreatedAt)"); |
| 80 | + statement.executeBatch(); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @AfterClass |
| 86 | + public static void stopEmulator() { |
| 87 | + if (emulator != null) { |
| 88 | + emulator.stop(); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testIsolationLevel() throws SQLException { |
| 94 | + isolationLevel("emulator-project", "my-instance", "my-database", PROPERTIES); |
| 95 | + } |
| 96 | + |
| 97 | +} |
0 commit comments