Skip to content

Commit fc31498

Browse files
authored
test: test cause of BatchUpdateException (#1974)
1 parent ea0b557 commit fc31498

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 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.google.cloud.spanner.jdbc;
18+
19+
import static com.google.cloud.spanner.jdbc.FallbackToPartitionedDMLMockServerTest.createTransactionMutationLimitExceededException;
20+
import static org.junit.Assert.assertArrayEquals;
21+
import static org.junit.Assert.assertEquals;
22+
import static org.junit.Assert.assertNotNull;
23+
import static org.junit.Assert.assertThrows;
24+
25+
import com.google.cloud.spanner.MockSpannerServiceImpl.StatementResult;
26+
import com.google.cloud.spanner.SpannerBatchUpdateException;
27+
import com.google.cloud.spanner.TransactionMutationLimitExceededException;
28+
import com.google.cloud.spanner.connection.AbstractMockServerTest;
29+
import com.google.spanner.v1.ExecuteBatchDmlRequest;
30+
import com.google.spanner.v1.ExecuteSqlRequest;
31+
import java.sql.BatchUpdateException;
32+
import java.sql.Connection;
33+
import java.sql.SQLException;
34+
import java.sql.Statement;
35+
import org.junit.After;
36+
import org.junit.BeforeClass;
37+
import org.junit.Test;
38+
import org.junit.runner.RunWith;
39+
import org.junit.runners.JUnit4;
40+
41+
@RunWith(JUnit4.class)
42+
public class BatchMockServerTest extends AbstractMockServerTest {
43+
private static final String NON_PARAMETERIZED_INSERT =
44+
"insert into foo (id, value) values (1, 'One')";
45+
private static final String NON_PARAMETERIZED_UPDATE = "update foo set value='Zero' where id=0";
46+
private static final String LARGE_UPDATE = "update foo set value='Zero' where true";
47+
48+
@BeforeClass
49+
public static void setup() {
50+
mockSpanner.putStatementResult(
51+
StatementResult.update(
52+
com.google.cloud.spanner.Statement.of(NON_PARAMETERIZED_INSERT), 1L));
53+
mockSpanner.putStatementResult(
54+
StatementResult.update(
55+
com.google.cloud.spanner.Statement.of(NON_PARAMETERIZED_UPDATE), 1L));
56+
mockSpanner.putStatementResult(
57+
StatementResult.exception(
58+
com.google.cloud.spanner.Statement.of(LARGE_UPDATE),
59+
createTransactionMutationLimitExceededException()));
60+
}
61+
62+
@After
63+
public void clearRequests() {
64+
mockSpanner.clearRequests();
65+
}
66+
67+
@Test
68+
public void testStatementBatch() throws SQLException {
69+
try (Connection connection = createJdbcConnection()) {
70+
try (Statement statement = connection.createStatement()) {
71+
statement.addBatch(NON_PARAMETERIZED_INSERT);
72+
statement.addBatch(NON_PARAMETERIZED_UPDATE);
73+
assertArrayEquals(new int[] {1, 1}, statement.executeBatch());
74+
}
75+
}
76+
assertEquals(1, mockSpanner.countRequestsOfType(ExecuteBatchDmlRequest.class));
77+
assertEquals(0, mockSpanner.countRequestsOfType(ExecuteSqlRequest.class));
78+
}
79+
80+
@Test
81+
public void testStatementBatchFailsDueToMutationLimit() throws SQLException {
82+
try (Connection connection = createJdbcConnection()) {
83+
try (Statement statement = connection.createStatement()) {
84+
statement.addBatch(NON_PARAMETERIZED_INSERT);
85+
statement.addBatch(LARGE_UPDATE);
86+
87+
BatchUpdateException batchUpdateException =
88+
assertThrows(BatchUpdateException.class, statement::executeBatch);
89+
assertNotNull(batchUpdateException.getCause());
90+
assertEquals(SpannerBatchUpdateException.class, batchUpdateException.getCause().getClass());
91+
assertNotNull(batchUpdateException.getCause().getCause());
92+
assertEquals(
93+
TransactionMutationLimitExceededException.class,
94+
batchUpdateException.getCause().getCause().getClass());
95+
}
96+
}
97+
assertEquals(1, mockSpanner.countRequestsOfType(ExecuteBatchDmlRequest.class));
98+
assertEquals(0, mockSpanner.countRequestsOfType(ExecuteSqlRequest.class));
99+
}
100+
}

0 commit comments

Comments
 (0)