1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
|
/*
* SonarQube
* Copyright (C) 2009-2025 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.db;
import com.google.common.base.Throwables;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.mockito.Mockito;
import org.slf4j.event.Level;
import org.sonar.api.testfixtures.log.LogTesterJUnit5;
import static java.lang.Math.abs;
import static org.apache.commons.lang3.RandomStringUtils.secure;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
class DBSessionsImplTest {
@RegisterExtension
private final LogTesterJUnit5 logTester = new LogTesterJUnit5();
private final MyBatis myBatis = mock(MyBatis.class);
private final DbSession myBatisDbSession = mock(DbSession.class);
private final Random random = new Random();
private final DBSessionsImpl underTest = new DBSessionsImpl(myBatis);
@AfterEach
void tearDown() {
underTest.disableCaching();
}
@Test
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)).toList())
.containsExactly(expected);
}
@Test
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)).toList())
.containsExactly(expected);
}
@Test
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(Collectors.toSet());
assertThat(dbSessions).hasSize(size);
assertThat(getWrappedDbSessions(dbSessions))
.hasSize(1)
.containsOnly(expected);
}
@Test
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(Collectors.toSet());
assertThat(dbSessions).hasSize(size);
assertThat(getWrappedDbSessions(dbSessions))
.hasSize(1)
.containsOnly(expected);
}
@Test
void openSession_with_caching_returns_a_session_per_thread() {
boolean batchOrRegular = random.nextBoolean();
DbSession[] expected = {mock(DbSession.class), mock(DbSession.class), mock(DbSession.class), mock(DbSession.class)};
when(myBatis.openSession(batchOrRegular))
.thenReturn(expected[0])
.thenReturn(expected[1])
.thenReturn(expected[2])
.thenReturn(expected[3])
.thenThrow(oneCallTooMuch());
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")};
executeThreadsAndCurrent(runnable, threads);
// verify each DbSession was closed and then reset mock for next verification
Arrays.stream(expected).forEach(s -> {
verify(s).close();
reset(s);
});
// verify whether each thread got the expected DbSession from MyBatis
// by verifying to which each returned DbSession delegates to
DbSession[] dbSessions = collector.toArray(new DbSession[0]);
for (int i = 0; i < expected.length; i++) {
dbSessions[i].rollback();
verify(expected[i]).rollback();
List<DbSession> sub = new ArrayList<>(Arrays.asList(expected));
sub.remove(expected[i]);
sub.forEach(Mockito::verifyNoMoreInteractions);
reset(expected);
}
}
private static void executeThreadsAndCurrent(Runnable runnable, Thread[] threads) {
Arrays.stream(threads).forEach(thread -> {
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
Throwables.propagate(e);
}
});
runnable.run();
}
@Test
void openSession_with_caching_returns_wrapper_of_MyBatis_DbSession_which_delegates_all_methods_but_close() {
boolean batchOrRegular = random.nextBoolean();
underTest.enableCaching();
verifyFirstDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
dbSession.rollback();
verify(myBatisDbSession).rollback();
});
verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
boolean flag = random.nextBoolean();
dbSession.rollback(flag);
verify(myBatisDbSession).rollback(flag);
});
verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
dbSession.commit();
verify(myBatisDbSession).commit();
});
verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
boolean flag = random.nextBoolean();
dbSession.commit(flag);
verify(myBatisDbSession).commit(flag);
});
verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
String str = secure().nextAlphabetic(10);
dbSession.selectOne(str);
verify(myBatisDbSession).selectOne(str);
});
verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
String str = secure().nextAlphabetic(10);
Object object = new Object();
dbSession.selectOne(str, object);
verify(myBatisDbSession).selectOne(str, object);
});
verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
String str = secure().nextAlphabetic(10);
dbSession.selectList(str);
verify(myBatisDbSession).selectList(str);
});
verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
String str = secure().nextAlphabetic(10);
Object object = new Object();
dbSession.selectList(str, object);
verify(myBatisDbSession).selectList(str, object);
});
verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
String str = secure().nextAlphabetic(10);
Object parameter = new Object();
RowBounds rowBounds = new RowBounds();
dbSession.selectList(str, parameter, rowBounds);
verify(myBatisDbSession).selectList(str, parameter, rowBounds);
});
verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
String str = secure().nextAlphabetic(10);
String mapKey = secure().nextAlphabetic(10);
dbSession.selectMap(str, mapKey);
verify(myBatisDbSession).selectMap(str, mapKey);
});
verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
String str = secure().nextAlphabetic(10);
Object parameter = new Object();
String mapKey = secure().nextAlphabetic(10);
dbSession.selectMap(str, parameter, mapKey);
verify(myBatisDbSession).selectMap(str, parameter, mapKey);
});
verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
String str = secure().nextAlphabetic(10);
Object parameter = new Object();
String mapKey = secure().nextAlphabetic(10);
RowBounds rowBounds = new RowBounds();
dbSession.selectMap(str, parameter, mapKey, rowBounds);
verify(myBatisDbSession).selectMap(str, parameter, mapKey, rowBounds);
});
verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
String str = secure().nextAlphabetic(10);
ResultHandler handler = mock(ResultHandler.class);
dbSession.select(str, handler);
verify(myBatisDbSession).select(str, handler);
});
verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
String str = secure().nextAlphabetic(10);
Object parameter = new Object();
ResultHandler handler = mock(ResultHandler.class);
dbSession.select(str, parameter, handler);
verify(myBatisDbSession).select(str, parameter, handler);
});
verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
String str = secure().nextAlphabetic(10);
Object parameter = new Object();
ResultHandler handler = mock(ResultHandler.class);
RowBounds rowBounds = new RowBounds();
dbSession.select(str, parameter, rowBounds, handler);
verify(myBatisDbSession).select(str, parameter, rowBounds, handler);
});
verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
String str = secure().nextAlphabetic(10);
dbSession.insert(str);
verify(myBatisDbSession).insert(str);
});
verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
String str = secure().nextAlphabetic(10);
Object object = new Object();
dbSession.insert(str, object);
verify(myBatisDbSession).insert(str, object);
});
verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
String str = secure().nextAlphabetic(10);
dbSession.update(str);
verify(myBatisDbSession).update(str);
});
verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
String str = secure().nextAlphabetic(10);
Object object = new Object();
dbSession.update(str, object);
verify(myBatisDbSession).update(str, object);
});
verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
String str = secure().nextAlphabetic(10);
dbSession.delete(str);
verify(myBatisDbSession).delete(str);
});
verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
String str = secure().nextAlphabetic(10);
Object object = new Object();
dbSession.delete(str, object);
verify(myBatisDbSession).delete(str, object);
});
verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
dbSession.flushStatements();
verify(myBatisDbSession).flushStatements();
});
verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
dbSession.clearCache();
verify(myBatisDbSession).clearCache();
});
verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
Configuration expected = mock(Configuration.class);
when(myBatisDbSession.getConfiguration()).thenReturn(expected);
assertThat(dbSession.getConfiguration()).isSameAs(expected);
verify(myBatisDbSession).getConfiguration();
});
verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
Class<Object> clazz = Object.class;
Object expected = new Object();
when(myBatisDbSession.getMapper(clazz)).thenReturn(expected);
assertThat(dbSession.getMapper(clazz)).isSameAs(expected);
verify(myBatisDbSession).getMapper(clazz);
});
verifyDelegation(batchOrRegular, (myBatisDbSession, dbSession) -> {
Connection connection = mock(Connection.class);
when(myBatisDbSession.getConnection()).thenReturn(connection);
assertThat(dbSession.getConnection()).isSameAs(connection);
verify(myBatisDbSession).getConnection();
});
}
@Test
void openSession_with_caching_returns_DbSession_that_rolls_back_on_close_if_any_mutation_call_was_not_followed_by_commit_nor_rollback() throws SQLException {
DbSession dbSession = openSessionAndDoSeveralMutatingAndNeutralCalls();
dbSession.close();
verify(myBatisDbSession).rollback();
}
@Test
void openSession_with_caching_returns_DbSession_that_does_not_roll_back_on_close_if_any_mutation_call_was_followed_by_commit() throws SQLException {
DbSession dbSession = openSessionAndDoSeveralMutatingAndNeutralCalls();
COMMIT_CALLS[random.nextBoolean() ? 0 : 1].consume(dbSession);
dbSession.close();
verify(myBatisDbSession, times(0)).rollback();
}
@Test
void openSession_with_caching_returns_DbSession_that_does_not_roll_back_on_close_if_any_mutation_call_was_followed_by_rollback_without_parameters() throws SQLException {
DbSession dbSession = openSessionAndDoSeveralMutatingAndNeutralCalls();
dbSession.rollback();
dbSession.close();
verify(myBatisDbSession, times(1)).rollback();
}
@Test
void openSession_with_caching_returns_DbSession_that_does_not_roll_back_on_close_if_any_mutation_call_was_followed_by_rollback_with_parameters() throws SQLException {
boolean force = random.nextBoolean();
DbSession dbSession = openSessionAndDoSeveralMutatingAndNeutralCalls();
dbSession.rollback(force);
dbSession.close();
verify(myBatisDbSession, times(1)).rollback(force);
verify(myBatisDbSession, times(0)).rollback();
}
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;
}
private static final DbSessionCaller[] DIRTYING_CALLS = {
session -> session.insert(secure().nextAlphabetic(3)),
session -> session.insert(secure().nextAlphabetic(2), new Object()),
session -> session.update(secure().nextAlphabetic(3)),
session -> session.update(secure().nextAlphabetic(3), new Object()),
session -> session.delete(secure().nextAlphabetic(3)),
session -> session.delete(secure().nextAlphabetic(3), new Object()),
};
private static final DbSessionCaller[] COMMIT_CALLS = {SqlSession::commit,
session -> session.commit(new Random().nextBoolean()),
};
private static final DbSessionCaller[] NEUTRAL_CALLS = {
session -> session.selectOne(secure().nextAlphabetic(3)),
session -> session.selectOne(secure().nextAlphabetic(3), new Object()),
session -> session.select(secure().nextAlphabetic(3), mock(ResultHandler.class)),
session -> session.select(secure().nextAlphabetic(3), new Object(), mock(ResultHandler.class)),
session -> session.select(secure().nextAlphabetic(3), new Object(), new RowBounds(), mock(ResultHandler.class)),
session -> session.selectList(secure().nextAlphabetic(3)),
session -> session.selectList(secure().nextAlphabetic(3), new Object()),
session -> session.selectList(secure().nextAlphabetic(3), new Object(), new RowBounds()),
session -> session.selectMap(secure().nextAlphabetic(3), secure().nextAlphabetic(3)),
session -> session.selectMap(secure().nextAlphabetic(3), new Object(), secure().nextAlphabetic(3)),
session -> session.selectMap(secure().nextAlphabetic(3), new Object(), secure().nextAlphabetic(3), new RowBounds()),
session -> session.getMapper(Object.class), SqlSession::getConfiguration, SqlSession::getConnection, SqlSession::clearCache, SqlSession::flushStatements
};
private interface DbSessionCaller {
void consume(DbSession t) throws SQLException;
}
@Test
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
void disableCaching_has_no_effect_if_enabledCaching_has_not_been_called() {
underTest.disableCaching();
verifyNoMoreInteractions(myBatis);
}
@Test
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(Level.ERROR);
assertThat(errorLogs)
.hasSize(1)
.containsOnly("Failed to close " + (batchOrRegular ? "batch" : "regular") + " connection in " + Thread.currentThread());
}
private void verifyFirstDelegation(boolean batchOrRegular, BiConsumer<DbSession, DbSession> r) {
verifyDelegation(batchOrRegular, true, r);
}
private void verifyDelegation(boolean batchOrRegular, BiConsumer<DbSession, DbSession> r) {
verifyDelegation(batchOrRegular, false, r);
}
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 static IllegalStateException oneCallTooMuch() {
return new IllegalStateException("one call too much");
}
private Set<DbSession> getWrappedDbSessions(Set<DbSession> dbSessions) {
return dbSessions.stream()
.filter(NonClosingDbSession.class::isInstance)
.map(NonClosingDbSession.class::cast)
.map(NonClosingDbSession::getDelegate)
.collect(Collectors.toSet());
}
}
|