1616
1717package com .google .cloud .spanner .jdbc ;
1818
19- import static org .hamcrest .CoreMatchers .endsWith ;
20- import static org .hamcrest .CoreMatchers .equalTo ;
21- import static org .hamcrest .CoreMatchers .is ;
22- import static org .hamcrest .CoreMatchers .nullValue ;
23- import static org .hamcrest .MatcherAssert .assertThat ;
19+ import static com .google .common .truth .Truth .assertThat ;
2420import static org .junit .Assert .fail ;
2521
2622import com .google .cloud .Timestamp ;
5349import java .util .List ;
5450import org .junit .AfterClass ;
5551import org .junit .BeforeClass ;
56- import org .junit .Rule ;
5752import org .junit .Test ;
58- import org .junit .rules .ExpectedException ;
5953import org .junit .runner .RunWith ;
6054import org .junit .runners .Parameterized ;
6155import org .junit .runners .Parameterized .Parameter ;
@@ -110,8 +104,6 @@ public void retryFinished(
110104 @ Parameter (0 )
111105 public boolean retryAbortsInternally ;
112106
113- @ Rule public ExpectedException expected = ExpectedException .none ();
114-
115107 @ Parameters (name = "retryAbortsInternally = {0}" )
116108 public static Collection <Object []> data () {
117109 List <Object []> params = new ArrayList <>();
@@ -175,23 +167,25 @@ public void testAutocommitUpdateAborted() throws SQLException {
175167 try (java .sql .Connection connection = createConnection ()) {
176168 mockSpanner .abortNextStatement ();
177169 int updateCount = connection .createStatement ().executeUpdate (UPDATE_STATEMENT .getSql ());
178- assertThat (updateCount , is ( equalTo ( UPDATE_COUNT )) );
170+ assertThat (updateCount ). isEqualTo ( UPDATE_COUNT );
179171 }
180172 }
181173
182174 @ Test
183175 public void testTransactionalUpdateAborted () throws SQLException {
184176 // Updates in transactional mode are automatically retried by default, but this can be switched
185177 // off.
186- if (!retryAbortsInternally ) {
187- expected .expect (JdbcAbortedException .class );
188- }
189178 try (java .sql .Connection connection = createConnection ()) {
190179 connection .setAutoCommit (false );
191180 mockSpanner .abortNextStatement ();
192181 int updateCount = connection .createStatement ().executeUpdate (UPDATE_STATEMENT .getSql ());
193- assertThat (updateCount , is (equalTo (UPDATE_COUNT )));
194- assertThat (getRetryCount (connection ), is (equalTo (1 )));
182+ if (!retryAbortsInternally ) {
183+ fail ("missing expected exception" );
184+ }
185+ assertThat (updateCount ).isEqualTo (UPDATE_COUNT );
186+ assertThat (getRetryCount (connection )).isEqualTo (1 );
187+ } catch (JdbcAbortedException e ) {
188+ assertThat (retryAbortsInternally ).isFalse ();
195189 }
196190 }
197191
@@ -203,25 +197,27 @@ public void testAutocommitBatchUpdateAborted() throws SQLException {
203197 statement .addBatch (UPDATE_STATEMENT .getSql ());
204198 statement .addBatch (UPDATE_STATEMENT .getSql ());
205199 int [] updateCounts = statement .executeBatch ();
206- assertThat (updateCounts , is ( equalTo ( new int [] { UPDATE_COUNT , UPDATE_COUNT })) );
200+ assertThat (updateCounts ). asList (). containsExactly ( UPDATE_COUNT , UPDATE_COUNT );
207201 }
208202 }
209203 }
210204
211205 @ Test
212206 public void testTransactionalBatchUpdateAborted () throws SQLException {
213- if (!retryAbortsInternally ) {
214- expected .expect (JdbcAbortedException .class );
215- }
216207 try (java .sql .Connection connection = createConnection ()) {
217208 connection .setAutoCommit (false );
218209 mockSpanner .abortNextStatement ();
219210 try (java .sql .Statement statement = connection .createStatement ()) {
220211 statement .addBatch (UPDATE_STATEMENT .getSql ());
221212 statement .addBatch (UPDATE_STATEMENT .getSql ());
222213 int [] updateCounts = statement .executeBatch ();
223- assertThat (updateCounts , is (equalTo (new int [] {UPDATE_COUNT , UPDATE_COUNT })));
224- assertThat (getRetryCount (connection ), is (equalTo (1 )));
214+ if (!retryAbortsInternally ) {
215+ fail ("missing expected exception" );
216+ }
217+ assertThat (updateCounts ).asList ().containsExactly (UPDATE_COUNT , UPDATE_COUNT );
218+ assertThat (getRetryCount (connection )).isEqualTo (1 );
219+ } catch (JdbcAbortedException e ) {
220+ assertThat (retryAbortsInternally ).isFalse ();
225221 }
226222 }
227223 }
@@ -233,38 +229,35 @@ public void testAutocommitSelectAborted() throws SQLException {
233229 mockSpanner .abortNextStatement ();
234230 try (ResultSet rs = connection .createStatement ().executeQuery (SELECT1 .getSql ())) {
235231 while (rs .next ()) {
236- assertThat (rs .getLong (1 ), is ( equalTo ( 1L )) );
232+ assertThat (rs .getLong (1 )). isEqualTo ( 1L );
237233 }
238234 }
239235 }
240236 }
241237
242238 @ Test
243239 public void testTransactionalSelectAborted () throws SQLException {
244- if (!retryAbortsInternally ) {
245- expected .expect (JdbcAbortedException .class );
246- }
247240 try (java .sql .Connection connection = createConnection ()) {
248241 connection .setAutoCommit (false );
249242 mockSpanner .abortNextStatement ();
250243 try (ResultSet rs = connection .createStatement ().executeQuery (SELECT1 .getSql ())) {
251244 while (rs .next ()) {
252- assertThat (rs .getLong (1 ), is (equalTo (1L )));
245+ if (!retryAbortsInternally ) {
246+ fail ("missing expected exception" );
247+ }
248+ assertThat (rs .getLong (1 )).isEqualTo (1L );
253249 }
254250 }
255- assertThat (getRetryCount (connection ), is (equalTo (1 )));
251+ assertThat (getRetryCount (connection )).isEqualTo (1 );
252+ } catch (JdbcAbortedException e ) {
253+ assertThat (retryAbortsInternally ).isFalse ();
256254 }
257255 }
258256
259257 @ Test
260258 public void testTransactionalUpdateWithConcurrentModificationsAborted () throws SQLException {
261- if (retryAbortsInternally ) {
262- // As the transaction does a random select, the retry will always see different data than the
263- // original attempt.
264- expected .expect (JdbcAbortedDueToConcurrentModificationException .class );
265- } else {
266- expected .expect (JdbcAbortedException .class );
267- }
259+ // As the transaction does a random select, the retry will always see different data than the
260+ // original attempt.
268261 try (java .sql .Connection connection = createConnection ()) {
269262 connection .setAutoCommit (false );
270263 // Set a random answer.
@@ -281,14 +274,15 @@ public void testTransactionalUpdateWithConcurrentModificationsAborted() throws S
281274 // This will abort and start an internal retry.
282275 connection .createStatement ().executeUpdate (UPDATE_STATEMENT .getSql ());
283276 fail ("missing expected aborted exception" );
277+ } catch (JdbcAbortedDueToConcurrentModificationException e ) {
278+ assertThat (retryAbortsInternally ).isTrue ();
279+ } catch (JdbcAbortedException e ) {
280+ assertThat (retryAbortsInternally ).isFalse ();
284281 }
285282 }
286283
287284 @ Test
288285 public void testTransactionalUpdateWithErrorOnOriginalAndRetry () throws SQLException {
289- if (!retryAbortsInternally ) {
290- expected .expect (JdbcAbortedException .class );
291- }
292286 final String sql = "UPDATE SOMETHING SET OTHER=1" ;
293287 mockSpanner .putStatementResult (
294288 StatementResult .exception (
@@ -298,7 +292,7 @@ public void testTransactionalUpdateWithErrorOnOriginalAndRetry() throws SQLExcep
298292 connection .setAutoCommit (false );
299293 try (ResultSet rs = connection .createStatement ().executeQuery (SELECT1 .getSql ())) {
300294 while (rs .next ()) {
301- assertThat (rs .getLong (1 ), is ( equalTo ( 1L )) );
295+ assertThat (rs .getLong (1 )). isEqualTo ( 1L );
302296 }
303297 }
304298 try {
@@ -309,16 +303,16 @@ public void testTransactionalUpdateWithErrorOnOriginalAndRetry() throws SQLExcep
309303 }
310304 mockSpanner .abortNextStatement ();
311305 connection .commit ();
306+ if (!retryAbortsInternally ) {
307+ fail ("missing expected exception" );
308+ }
309+ } catch (JdbcAbortedException e ) {
310+ assertThat (retryAbortsInternally ).isFalse ();
312311 }
313312 }
314313
315314 @ Test
316315 public void testTransactionalUpdateWithErrorOnRetryAndNotOnOriginal () throws SQLException {
317- if (retryAbortsInternally ) {
318- expected .expect (JdbcAbortedDueToConcurrentModificationException .class );
319- } else {
320- expected .expect (JdbcAbortedException .class );
321- }
322316 final String sql = "UPDATE SOMETHING SET OTHER=1" ;
323317 try (java .sql .Connection connection = createConnection ()) {
324318 connection .setAutoCommit (false );
@@ -335,20 +329,17 @@ public void testTransactionalUpdateWithErrorOnRetryAndNotOnOriginal() throws SQL
335329 connection .commit ();
336330 fail ("missing expected aborted exception" );
337331 } catch (JdbcAbortedDueToConcurrentModificationException e ) {
338- assertThat (
339- e .getDatabaseErrorDuringRetry ().getErrorCode (), is (equalTo (ErrorCode .INVALID_ARGUMENT )));
340- assertThat (e .getDatabaseErrorDuringRetry ().getMessage (), endsWith ("test" ));
341- throw e ;
332+ assertThat (retryAbortsInternally ).isTrue ();
333+ assertThat (e .getDatabaseErrorDuringRetry ().getErrorCode ())
334+ .isEqualTo (ErrorCode .INVALID_ARGUMENT );
335+ assertThat (e .getDatabaseErrorDuringRetry ().getMessage ()).endsWith ("test" );
336+ } catch (JdbcAbortedException e ) {
337+ assertThat (retryAbortsInternally ).isFalse ();
342338 }
343339 }
344340
345341 @ Test
346342 public void testTransactionalUpdateWithErrorOnOriginalAndNotOnRetry () throws SQLException {
347- if (retryAbortsInternally ) {
348- expected .expect (JdbcAbortedDueToConcurrentModificationException .class );
349- } else {
350- expected .expect (JdbcAbortedException .class );
351- }
352343 final String sql = "UPDATE SOMETHING SET OTHER=1" ;
353344 mockSpanner .putStatementResult (
354345 StatementResult .exception (
@@ -358,7 +349,7 @@ public void testTransactionalUpdateWithErrorOnOriginalAndNotOnRetry() throws SQL
358349 connection .setAutoCommit (false );
359350 try (ResultSet rs = connection .createStatement ().executeQuery (SELECT1 .getSql ())) {
360351 while (rs .next ()) {
361- assertThat (rs .getLong (1 ), is ( equalTo ( 1L )) );
352+ assertThat (rs .getLong (1 )). isEqualTo ( 1L );
362353 }
363354 }
364355 try {
@@ -373,8 +364,10 @@ public void testTransactionalUpdateWithErrorOnOriginalAndNotOnRetry() throws SQL
373364 connection .commit ();
374365 fail ("missing expected aborted exception" );
375366 } catch (JdbcAbortedDueToConcurrentModificationException e ) {
376- assertThat (e .getDatabaseErrorDuringRetry (), is (nullValue ()));
377- throw e ;
367+ assertThat (retryAbortsInternally ).isTrue ();
368+ assertThat (e .getDatabaseErrorDuringRetry ()).isNull ();
369+ } catch (JdbcAbortedException e ) {
370+ assertThat (retryAbortsInternally ).isFalse ();
378371 }
379372 }
380373}
0 commit comments