22
22
import static org .mockito .Mockito .*;
23
23
import static org .springframework .data .relational .core .sql .SqlIdentifier .*;
24
24
25
+ import java .util .Iterator ;
26
+ import java .util .List ;
27
+ import java .util .stream .Stream ;
28
+ import org .apache .ibatis .cursor .Cursor ;
25
29
import org .apache .ibatis .session .SqlSession ;
30
+ import org .jetbrains .annotations .NotNull ;
26
31
import org .junit .jupiter .api .BeforeEach ;
27
32
import org .junit .jupiter .api .Test ;
28
33
import org .mockito .ArgumentCaptor ;
43
48
* @author Mark Paluch
44
49
* @author Tyler Van Gorder
45
50
* @author Chirag Tailor
51
+ * @author Sergey Korotaev
46
52
*/
47
53
public class MyBatisDataAccessStrategyUnitTests {
48
54
@@ -241,6 +247,36 @@ public void findAll() {
241
247
);
242
248
}
243
249
250
+ @ Test
251
+ public void streamAll () {
252
+
253
+ String value = "some answer" ;
254
+
255
+ Cursor <String > cursor = getCursor (value );
256
+
257
+ when (session .selectCursor (anyString (), any ())).then (answer -> cursor );
258
+
259
+ Stream <String > streamable = accessStrategy .streamAll (String .class );
260
+
261
+ verify (session ).selectCursor (eq ("java.lang.StringMapper.streamAll" ), captor .capture ());
262
+
263
+ assertThat (streamable ).isNotNull ().containsExactly (value );
264
+
265
+ assertThat (captor .getValue ()) //
266
+ .isNotNull () //
267
+ .extracting ( //
268
+ MyBatisContext ::getInstance , //
269
+ MyBatisContext ::getId , //
270
+ MyBatisContext ::getDomainType , //
271
+ c -> c .get ("key" ) //
272
+ ).containsExactly ( //
273
+ null , //
274
+ null , //
275
+ String .class , //
276
+ null //
277
+ );
278
+ }
279
+
244
280
@ Test // DATAJDBC-123
245
281
public void findAllById () {
246
282
@@ -263,6 +299,33 @@ public void findAllById() {
263
299
);
264
300
}
265
301
302
+ @ Test
303
+ public void streamAllByIds () {
304
+
305
+ String value = "some answer 2" ;
306
+ Cursor <String > cursor = getCursor (value );
307
+
308
+ when (session .selectCursor (anyString (), any ())).then (answer -> cursor );
309
+
310
+ accessStrategy .streamAllByIds (asList ("id1" , "id2" ), String .class );
311
+
312
+ verify (session ).selectCursor (eq ("java.lang.StringMapper.streamAllByIds" ), captor .capture ());
313
+
314
+ assertThat (captor .getValue ()) //
315
+ .isNotNull () //
316
+ .extracting ( //
317
+ MyBatisContext ::getInstance , //
318
+ MyBatisContext ::getId , //
319
+ MyBatisContext ::getDomainType , //
320
+ c -> c .get ("key" ) //
321
+ ).containsExactly ( //
322
+ null , //
323
+ asList ("id1" , "id2" ), //
324
+ String .class , //
325
+ null //
326
+ );
327
+ }
328
+
266
329
@ SuppressWarnings ("unchecked" )
267
330
@ Test // DATAJDBC-384
268
331
public void findAllByPath () {
@@ -367,6 +430,33 @@ public void findAllSorted() {
367
430
);
368
431
}
369
432
433
+ @ Test
434
+ public void streamAllSorted () {
435
+
436
+ String value = "some answer 3" ;
437
+ Cursor <String > cursor = getCursor (value );
438
+
439
+ when (session .selectCursor (anyString (), any ())).then (answer -> cursor );
440
+
441
+ accessStrategy .streamAll (String .class , Sort .by ("length" ));
442
+
443
+ verify (session ).selectCursor (eq ("java.lang.StringMapper.streamAllSorted" ), captor .capture ());
444
+
445
+ assertThat (captor .getValue ()) //
446
+ .isNotNull () //
447
+ .extracting ( //
448
+ MyBatisContext ::getInstance , //
449
+ MyBatisContext ::getId , //
450
+ MyBatisContext ::getDomainType , //
451
+ c -> c .get ("sort" ) //
452
+ ).containsExactly ( //
453
+ null , //
454
+ null , //
455
+ String .class , //
456
+ Sort .by ("length" ) //
457
+ );
458
+ }
459
+
370
460
@ Test // DATAJDBC-101
371
461
public void findAllPaged () {
372
462
@@ -399,5 +489,36 @@ private static class ChildOne {
399
489
ChildTwo two ;
400
490
}
401
491
402
- private static class ChildTwo {}
492
+ private static class ChildTwo {
493
+ }
494
+
495
+ private Cursor <String > getCursor (String value ) {
496
+ return new Cursor <>() {
497
+ @ Override
498
+ public boolean isOpen () {
499
+ return false ;
500
+ }
501
+
502
+ @ Override
503
+ public boolean isConsumed () {
504
+ return false ;
505
+ }
506
+
507
+ @ Override
508
+ public int getCurrentIndex () {
509
+ return 0 ;
510
+ }
511
+
512
+ @ Override
513
+ public void close () {
514
+
515
+ }
516
+
517
+ @ NotNull
518
+ @ Override
519
+ public Iterator <String > iterator () {
520
+ return List .of (value ).iterator ();
521
+ }
522
+ };
523
+ }
403
524
}
0 commit comments