Skip to content

Commit 8be2f4c

Browse files
authored
test: add tests to verify behavior of empty transactions (#1472)
1 parent aa4c0f5 commit 8be2f4c

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright 2024 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.google.cloud.spanner.jdbc;
18+
19+
import static org.junit.Assert.assertEquals;
20+
21+
import com.google.cloud.spanner.connection.AbstractMockServerTest;
22+
import com.google.cloud.spanner.connection.SpannerPool;
23+
import com.google.spanner.v1.CommitRequest;
24+
import java.sql.Connection;
25+
import java.sql.DriverManager;
26+
import java.sql.SQLException;
27+
import java.sql.Statement;
28+
import org.junit.After;
29+
import org.junit.AfterClass;
30+
import org.junit.Test;
31+
import org.junit.runner.RunWith;
32+
import org.junit.runners.JUnit4;
33+
34+
@RunWith(JUnit4.class)
35+
public class TransactionMockServerTest extends AbstractMockServerTest {
36+
37+
@AfterClass
38+
public static void closeSpannerPool() {
39+
SpannerPool.closeSpannerPool();
40+
}
41+
42+
@After
43+
public void clearRequests() {
44+
mockSpanner.clearRequests();
45+
}
46+
47+
private String createUrl() {
48+
return String.format(
49+
"jdbc:cloudspanner://localhost:%d/projects/%s/instances/%s/databases/%s?usePlainText=true;autoCommit=false",
50+
getPort(), "proj", "inst", "db");
51+
}
52+
53+
private Connection createConnection() throws SQLException {
54+
return DriverManager.getConnection(createUrl());
55+
}
56+
57+
@Test
58+
public void testCommittingEmptyTransactionIsNoOp() throws SQLException {
59+
try (Connection connection = createConnection()) {
60+
connection.commit();
61+
}
62+
63+
assertEquals(0, mockSpanner.countRequestsOfType(CommitRequest.class));
64+
}
65+
66+
@Test
67+
public void testCommittingEmptyExplicitTransactionIsNoOp() throws SQLException {
68+
try (Connection connection = createConnection()) {
69+
connection.setAutoCommit(true);
70+
try (Statement statement = connection.createStatement()) {
71+
statement.execute("begin transaction");
72+
statement.execute("commit");
73+
}
74+
}
75+
76+
assertEquals(0, mockSpanner.countRequestsOfType(CommitRequest.class));
77+
}
78+
79+
@Test
80+
public void testRollingBackEmptyTransactionIsNoOp() throws SQLException {
81+
try (Connection connection = createConnection()) {
82+
connection.rollback();
83+
}
84+
85+
assertEquals(0, mockSpanner.countRequestsOfType(CommitRequest.class));
86+
}
87+
88+
@Test
89+
public void testRollingBackEmptyExplicitTransactionIsNoOp() throws SQLException {
90+
try (Connection connection = createConnection()) {
91+
connection.setAutoCommit(true);
92+
try (Statement statement = connection.createStatement()) {
93+
statement.execute("begin transaction");
94+
statement.execute("rollback");
95+
}
96+
}
97+
98+
assertEquals(0, mockSpanner.countRequestsOfType(CommitRequest.class));
99+
}
100+
}

0 commit comments

Comments
 (0)