@Test public void disableCaching_has_no_effect_if_enabledCaching_has_not_been_called() { underTest.disableCaching(); verifyNoMoreInteractions(myBatis); }
private DelegatingDbSessionSupplier buildRegularDbSessionSupplier() { return new DelegatingDbSessionSupplier(() -> { DbSession res = myBatis.openSession(false); ensureAutoCommitFalse(res); return res; }); }
@Test public void disableCaching_does_not_fail_but_logs_if_closing_MyBatis_session_close_throws_an_exception() { boolean batchOrRegular = random.nextBoolean(); IllegalStateException toBeThrown = new IllegalStateException("Faking MyBatisSession#close failing"); when(myBatis.openSession(batchOrRegular)) .thenReturn(myBatisDbSession) .thenThrow(oneCallTooMuch()); Mockito.doThrow(toBeThrown) .when(myBatisDbSession).close(); underTest.enableCaching(); underTest.openSession(batchOrRegular); underTest.disableCaching(); List<String> errorLogs = logTester.logs(LoggerLevel.ERROR); assertThat(errorLogs) .hasSize(1) .containsOnly("Failed to close " + (batchOrRegular ? "batch" : "regular") + " connection in " + Thread.currentThread()); }
@Test public void openSession_with_caching_always_returns_the_same_regular_session_when_parameter_is_false() { DbSession expected = mock(DbSession.class); when(myBatis.openSession(false)) .thenReturn(expected) .thenThrow(oneCallTooMuch()); underTest.enableCaching(); int size = 1 + abs(random.nextInt(10)); Set<DbSession> dbSessions = IntStream.range(0, size).mapToObj(ignored -> underTest.openSession(false)).collect(MoreCollectors.toSet()); assertThat(dbSessions).hasSize(size); assertThat(getWrappedDbSessions(dbSessions)) .hasSize(1) .containsOnly(expected); }
@Test public void disableCaching_does_not_open_DB_connection_if_openSession_was_never_called() { when(myBatis.openSession(anyBoolean())) .thenThrow(oneCallTooMuch()); underTest.enableCaching(); underTest.disableCaching(); verifyNoMoreInteractions(myBatis); }
@Test public void openSession_without_caching_always_returns_a_new_batch_session_when_parameter_is_true() { DbSession[] expected = {mock(DbSession.class), mock(DbSession.class), mock(DbSession.class), mock(DbSession.class)}; when(myBatis.openSession(true)) .thenReturn(expected[0]) .thenReturn(expected[1]) .thenReturn(expected[2]) .thenReturn(expected[3]) .thenThrow(oneCallTooMuch()); assertThat(Arrays.stream(expected).map(ignored -> underTest.openSession(true)).collect(MoreCollectors.toList())) .containsExactly(expected); }
boolean batchOrRegular = random.nextBoolean(); underTest.enableCaching();
List<DbSession> collector = new ArrayList<>(); Runnable runnable = () -> { underTest.enableCaching(); collector.add(underTest.openSession(batchOrRegular)); underTest.disableCaching(); }; Thread[] threads = {new Thread(runnable, "T1"), new Thread(runnable, "T2"), new Thread(runnable, "T3")};
@Test public void openSession_with_caching_always_returns_the_same_batch_session_when_parameter_is_true() { DbSession expected = mock(DbSession.class); when(myBatis.openSession(true)) .thenReturn(expected) .thenThrow(oneCallTooMuch()); underTest.enableCaching(); int size = 1 + abs(random.nextInt(10)); Set<DbSession> dbSessions = IntStream.range(0, size).mapToObj(ignored -> underTest.openSession(true)).collect(MoreCollectors.toSet()); assertThat(dbSessions).hasSize(size); assertThat(getWrappedDbSessions(dbSessions)) .hasSize(1) .containsOnly(expected); }
private void verifyDelegation(boolean batchOrRegular, boolean firstCall, BiConsumer<DbSession, DbSession> r) { when(myBatis.openSession(batchOrRegular)) .thenReturn(myBatisDbSession) .thenThrow(oneCallTooMuch()); r.accept(myBatisDbSession, underTest.openSession(batchOrRegular)); verify(myBatisDbSession, times(firstCall ? 1 : 0)).getSqlSession(); verifyNoMoreInteractions(myBatisDbSession); reset(myBatis, myBatisDbSession); }
private DbSession openSessionAndDoSeveralMutatingAndNeutralCalls() throws SQLException { boolean batchOrRegular = random.nextBoolean(); when(myBatis.openSession(batchOrRegular)) .thenReturn(myBatisDbSession) .thenThrow(oneCallTooMuch()); underTest.enableCaching(); DbSession dbSession = underTest.openSession(batchOrRegular); int dirtyingCallsCount = 1 + abs(random.nextInt(5)); int neutralCallsCount = abs(random.nextInt(5)); int[] dirtyCallIndices = IntStream.range(0, dirtyingCallsCount).map(ignored -> abs(random.nextInt(DIRTYING_CALLS.length))).toArray(); int[] neutralCallsIndices = IntStream.range(0, neutralCallsCount).map(ignored -> abs(random.nextInt(NEUTRAL_CALLS.length))).toArray(); for (int index : dirtyCallIndices) { DIRTYING_CALLS[index].consume(dbSession); } for (int index : neutralCallsIndices) { NEUTRAL_CALLS[index].consume(dbSession); } return dbSession; }
@Test public void openSession_without_caching_always_returns_a_new_regular_session_when_parameter_is_false() { DbSession[] expected = {mock(DbSession.class), mock(DbSession.class), mock(DbSession.class), mock(DbSession.class)}; when(myBatis.openSession(false)) .thenReturn(expected[0]) .thenReturn(expected[1]) .thenReturn(expected[2]) .thenReturn(expected[3]) .thenThrow(oneCallTooMuch()); assertThat(Arrays.stream(expected).map(ignored -> underTest.openSession(false)).collect(MoreCollectors.toList())) .containsExactly(expected); }
@After public void tearDown() { underTest.disableCaching(); }
private DelegatingDbSessionSupplier buildBatchDbSessionSupplier() { return new DelegatingDbSessionSupplier(() -> { DbSession res = myBatis.openSession(true); ensureAutoCommitFalse(res); return res; }); }