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_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 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); }
@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 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()); }
List<DbSession> collector = new ArrayList<>(); Runnable runnable = () -> { underTest.enableCaching(); collector.add(underTest.openSession(batchOrRegular)); underTest.disableCaching();
boolean batchOrRegular = random.nextBoolean(); underTest.enableCaching();