@@ -142,8 +142,7 @@ void readOnlyTxnAllowedInReadOnlyEnv() {
142142 .open (file )) {
143143 try (Txn <ByteBuffer > readTxn = roEnv .txnRead ()) {
144144 assertThat (readTxn ).isNotNull ();
145- assertThat (readTxn .isReadOnly ()).isTrue ();
146- assertThat (readTxn .isWritable ()).isFalse ();
145+ assertReadOnly (readTxn );
147146 }
148147 }
149148 }
@@ -227,20 +226,19 @@ void testGetId() {
227226 @ Test
228227 void txCanCommitThenCloseWithoutError () {
229228 try (Txn <ByteBuffer > txn = env .txnRead ()) {
230- assertThat (txn . getState ()). isEqualTo ( READY );
229+ assertState (txn , READY );
231230 txn .commit ();
232- assertThat (txn . getState ()). isEqualTo ( DONE );
231+ assertState (txn , DONE );
233232 }
234233 }
235234
236235 @ Test
237236 void txCannotAbortIfAlreadyCommitted () {
238237
239238 try (Txn <ByteBuffer > txn = env .txnRead ()) {
240- assertThat (txn . getState ()). isEqualTo ( READY );
239+ assertState (txn , READY );
241240 txn .commit ();
242- assertThat (txn .getState ()).isEqualTo (DONE );
243-
241+ assertState (txn , DONE );
244242 assertThatThrownBy (txn ::abort ).isInstanceOf (NotReadyException .class );
245243 }
246244 }
@@ -368,34 +366,29 @@ void txParentRWChildROIncompatible() {
368366 void txReadOnly () {
369367 try (Txn <ByteBuffer > txn = env .txnRead ()) {
370368 assertThat (txn .getParent ()).isNull ();
371- assertThat (txn .getState ()).isEqualTo (READY );
372- assertThat (txn .isReadOnly ()).isTrue ();
373- txn .checkReady ();
374- txn .checkReadOnly ();
369+ assertState (txn , READY );
370+ assertReadOnly (txn );
375371 txn .reset ();
376- assertThat (txn . getState ()). isEqualTo ( RESET );
372+ assertState (txn , RESET );
377373 txn .renew ();
378- assertThat (txn . getState ()). isEqualTo ( READY );
374+ assertState (txn , READY );
379375 txn .commit ();
380- assertThat (txn . getState ()). isEqualTo ( DONE );
376+ assertState (txn , DONE );
381377 txn .close ();
382- assertThat (txn . getState ()). isEqualTo ( RELEASED );
378+ assertState (txn , RELEASED );
383379 }
384380 }
385381
386382 @ Test
387383 void txReadWrite () {
388384 final Txn <ByteBuffer > txn = env .txnWrite ();
389385 assertThat (txn .getParent ()).isNull ();
390- assertThat (txn .getState ()).isEqualTo (READY );
391- assertThat (txn .isReadOnly ()).isFalse ();
392- assertThat (txn .isWritable ()).isTrue ();
393- txn .checkReady ();
394- txn .checkWritesAllowed ();
386+ assertState (txn , READY );
387+ assertWritable (txn );
395388 txn .commit ();
396- assertThat (txn . getState ()). isEqualTo ( DONE );
389+ assertState (txn , DONE );
397390 txn .close ();
398- assertThat (txn . getState ()). isEqualTo ( RELEASED );
391+ assertState (txn , RELEASED );
399392 }
400393
401394 @ Test
@@ -451,4 +444,30 @@ void zeroByteKeysRejected() {
451444 })
452445 .isInstanceOf (BadValueSizeException .class );
453446 }
447+
448+ private void assertState (final Txn <?> txn , final Txn .State expectedState ) {
449+ assertThat (txn .getState ()).isEqualTo (expectedState );
450+ if (expectedState == READY ) {
451+ assertThat (txn .isReady ()).isTrue ();
452+ txn .checkReady ();
453+ } else {
454+ assertThat (txn .isReady ()).isFalse ();
455+ assertThatThrownBy (txn ::checkReady ).isInstanceOf (NotReadyException .class );
456+ }
457+ }
458+
459+ private void assertReadOnly (final Txn <?> txn ) {
460+ assertThat (txn .isReadOnly ()).isTrue ();
461+ assertThat (txn .isWritable ()).isFalse ();
462+ txn .checkReadOnly ();
463+ assertThatThrownBy (txn ::checkWritesAllowed ).isInstanceOf (ReadWriteRequiredException .class );
464+ }
465+
466+ private void assertWritable (final Txn <?> txn ) {
467+ assertThat (txn .isReadOnly ()).isFalse ();
468+ assertThat (txn .isWritable ()).isTrue ();
469+ assertThatThrownBy (txn ::checkReadOnly ).isInstanceOf (ReadOnlyRequiredException .class );
470+ // Should not throw in a writable state
471+ txn .checkWritesAllowed ();
472+ }
454473}
0 commit comments