1 package org.synchronoss.cpo.jdbc.adapter;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import static org.testng.Assert.*;
26
27 import java.math.BigInteger;
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.stream.Stream;
31 import org.synchronoss.cpo.core.CpoAdapter;
32 import org.synchronoss.cpo.core.CpoAdapterFactoryManager;
33 import org.synchronoss.cpo.core.CpoOrderBy;
34 import org.synchronoss.cpo.core.CpoQuery;
35 import org.synchronoss.cpo.core.CpoWhere;
36 import org.synchronoss.cpo.core.enums.Comparison;
37 import org.synchronoss.cpo.jdbc.ValueObject;
38 import org.testng.annotations.AfterClass;
39 import org.testng.annotations.BeforeClass;
40 import org.testng.annotations.Test;
41
42
43
44
45
46
47 public class WhereTest {
48
49
50 private static final int IDB = 2400000;
51
52 private CpoAdapter cpoAdapter = null;
53 private ArrayList<ValueObject> al = new ArrayList<>();
54
55 public WhereTest() {}
56
57
58
59
60
61 @BeforeClass
62 public void setUp() {
63 String method = "setUp:";
64
65 try {
66 cpoAdapter = CpoAdapterFactoryManager.getCpoAdapter(JdbcStatics.ADAPTER_CONTEXT_JDBC);
67 assertNotNull(cpoAdapter, method + "cpoAdapter is null");
68 } catch (Exception e) {
69 fail(method + e.getMessage());
70 }
71 ValueObject vo = ValueObjectFactory.createValueObject(IDB + 1);
72 vo.setAttrVarChar("Test");
73 vo.setAttrSmallInt((short) 1);
74 vo.setAttrInteger(IDB + 1);
75 vo.setAttrBigInt(BigInteger.valueOf(IDB + 1));
76 vo.setAttrDate(new java.sql.Date(new java.util.Date().getTime()));
77 al.add(vo);
78 al.add(ValueObjectFactory.createValueObject(IDB + 2));
79 al.add(ValueObjectFactory.createValueObject(IDB + 3));
80 al.add(ValueObjectFactory.createValueObject(IDB + 4));
81 al.add(ValueObjectFactory.createValueObject(IDB + 5));
82 al.add(ValueObjectFactory.createValueObject(-(IDB + 6)));
83 try {
84 cpoAdapter.insertBeans(ValueObject.FG_CREATE_TESTORDERBYINSERT, al);
85 } catch (Exception e) {
86 fail(method + e.getMessage());
87 }
88 }
89
90
91 @AfterClass
92 public void tearDown() {
93 String method = "tearDown:";
94 try {
95 cpoAdapter.deleteBeans(ValueObject.FG_DELETE_TESTORDERBYDELETE, al);
96
97 } catch (Exception e) {
98 fail(method + e.getMessage());
99 }
100 cpoAdapter = null;
101 }
102
103
104 @Test
105 public void testStaticWhere() {
106 String method = "testStaticWhere:";
107 Collection<ValueObject> col;
108 CpoWhere cw;
109
110 try {
111 ValueObject valObj = ValueObjectFactory.createValueObject();
112 cw =
113 cpoAdapter
114 .whereBuilder()
115 .where(ValueObject.ATTR_ID, Comparison.GT, null)
116 .staticValue(String.valueOf(IDB + 3))
117 .build();
118 ArrayList<CpoWhere> wheres = new ArrayList<>();
119 wheres.add(cw);
120 try (Stream<ValueObject> beans =
121 cpoAdapter.retrieveBeans(
122 CpoQuery.group(ValueObject.FG_LIST_TESTWHERERETRIEVE).wheres(wheres), valObj)) {
123 long count =
124 beans
125 .filter(b -> Math.abs(b.getId()) >= IDB && Math.abs(b.getId()) < IDB + 100000)
126 .count();
127 assertEquals(count, 2, "Number of beans is " + count);
128 }
129 } catch (Exception e) {
130 fail(method + e.getMessage());
131 }
132 }
133
134 @Test
135 public void testSetObjectWhere() {
136 String method = "testSetObjectWhere:";
137 Collection<ValueObject> col;
138 CpoWhere cw;
139
140 try {
141 BigInteger bigInt = BigInteger.valueOf(IDB + 1);
142 cw =
143 cpoAdapter
144 .whereBuilder()
145 .where(ValueObject.ATTR_ATTRBIGINT, Comparison.EQ, bigInt)
146 .build();
147
148 ArrayList<CpoWhere> wheres = new ArrayList<>();
149 wheres.add(cw);
150 try (Stream<ValueObject> beans =
151 cpoAdapter.retrieveBeans(
152 CpoQuery.group(ValueObject.FG_LIST_TESTWHERERETRIEVE).wheres(wheres),
153 ValueObjectFactory.createValueObject())) {
154 long count =
155 beans
156 .filter(b -> Math.abs(b.getId()) >= IDB && Math.abs(b.getId()) < IDB + 100000)
157 .count();
158 assertEquals(count, 1, "Number of beans is " + count);
159 }
160 } catch (Exception e) {
161 fail(method + e.getMessage());
162 }
163 }
164
165 @Test
166 public void testValueWhere() {
167 String method = "testValueWhere:";
168 Collection<ValueObject> col;
169 CpoWhere cw;
170
171 try {
172 ValueObject valObj = ValueObjectFactory.createValueObject(IDB + 3);
173 cw = cpoAdapter.whereBuilder().where(ValueObject.ATTR_ID, Comparison.GT, valObj).build();
174
175 ArrayList<CpoWhere> wheres = new ArrayList<>();
176 wheres.add(cw);
177 try (Stream<ValueObject> beans =
178 cpoAdapter.retrieveBeans(
179 CpoQuery.group(ValueObject.FG_LIST_TESTWHERERETRIEVE).wheres(wheres), valObj)) {
180 long count =
181 beans
182 .filter(b -> Math.abs(b.getId()) >= IDB && Math.abs(b.getId()) < IDB + 100000)
183 .count();
184 assertEquals(count, 2, "Number of beans is " + count);
185 }
186 } catch (Exception e) {
187 fail(method + e.getMessage());
188 }
189 }
190
191 @Test
192 public void testNoMarkerWhere() {
193 String method = "testNoMarkerWhere:";
194 Collection<ValueObject> col;
195 CpoWhere cw;
196
197 try {
198 ValueObject valObj = ValueObjectFactory.createValueObject(IDB + 3);
199 cw = cpoAdapter.whereBuilder().where(ValueObject.ATTR_ID, Comparison.GT, valObj).build();
200
201 ArrayList<CpoWhere> wheres = new ArrayList<>();
202 wheres.add(cw);
203 try (Stream<ValueObject> beans =
204 cpoAdapter.retrieveBeans(
205 CpoQuery.group(ValueObject.FG_LIST_NULL).wheres(wheres), valObj)) {
206 long count =
207 beans
208 .filter(b -> Math.abs(b.getId()) >= IDB && Math.abs(b.getId()) < IDB + 100000)
209 .count();
210 assertEquals(count, 2, "Number of beans is " + count);
211 }
212 } catch (Exception e) {
213 fail(method + e.getMessage());
214 }
215 }
216
217
218
219
220
221 @Test
222 public void testNoWhere() {
223 String method = "testNoWhere:";
224 Collection<ValueObject> col = null;
225
226 ValueObject valObj = ValueObjectFactory.createValueObject(IDB + 3);
227 try (Stream<ValueObject> beans =
228 cpoAdapter.retrieveBeans(ValueObject.FG_LIST_TESTWHERERETRIEVE, valObj)) {
229 long count =
230 beans
231 .filter(b -> Math.abs(b.getId()) >= IDB && Math.abs(b.getId()) < IDB + 100000)
232 .count();
233 assertEquals(count, 6, "Number of beans is " + count);
234 } catch (Exception e) {
235 fail(method + e.getMessage());
236 }
237 }
238
239 @Test
240 public void testNestedWhere() {
241 String method = "testNestedWhere:";
242 Collection<ValueObject> col;
243 CpoWhere cw;
244 ArrayList<CpoWhere> wheres = new ArrayList<>();
245
246 try {
247 ValueObject valObj = ValueObjectFactory.createValueObject(-(IDB + 6));
248 ValueObject valObjOr1 = ValueObjectFactory.createValueObject(IDB + 2);
249 ValueObject valObjOr2 = ValueObjectFactory.createValueObject(IDB + 3);
250 cw =
251 cpoAdapter
252 .whereBuilder()
253 .where(ValueObject.ATTR_ID, Comparison.EQ, valObj)
254 .or(
255 g ->
256 g.where(ValueObject.ATTR_ID, Comparison.EQ, valObjOr1)
257 .or(ValueObject.ATTR_ID, Comparison.EQ, valObjOr2))
258 .build();
259
260 wheres.add(cw);
261 valObj = valObjOr2;
262 try (Stream<ValueObject> beans =
263 cpoAdapter.retrieveBeans(
264 CpoQuery.group(ValueObject.FG_LIST_TESTWHERERETRIEVE).wheres(wheres), valObj)) {
265 long count =
266 beans
267 .filter(b -> Math.abs(b.getId()) >= IDB && Math.abs(b.getId()) < IDB + 100000)
268 .count();
269 assertEquals(count, 3, "Number of beans is " + count);
270 }
271 } catch (Exception e) {
272 fail(method + e.getMessage());
273 }
274 }
275
276 @Test
277 public void testIsNullWhere() {
278 String method = "testIsNullWhere:";
279 Collection<ValueObject> col;
280 CpoWhere cw;
281
282 try {
283 ValueObject valObj = ValueObjectFactory.createValueObject(IDB + 3);
284 cw =
285 cpoAdapter
286 .whereBuilder()
287 .where(ValueObject.ATTR_ATTRCHAR, Comparison.ISNULL, null)
288 .build();
289
290 ArrayList<CpoWhere> wheres = new ArrayList<>();
291 wheres.add(cw);
292 try (Stream<ValueObject> beans =
293 cpoAdapter.retrieveBeans(
294 CpoQuery.group(ValueObject.FG_LIST_TESTWHERERETRIEVE).wheres(wheres), valObj)) {
295 long count =
296 beans
297 .filter(b -> Math.abs(b.getId()) >= IDB && Math.abs(b.getId()) < IDB + 100000)
298 .count();
299 assertEquals(count, 6, "Number of beans is " + count);
300 }
301 } catch (Exception e) {
302 fail(method + e.getMessage());
303 }
304 }
305
306 @Test
307 public void testAttributeFunction() {
308 String method = "testAttributeFunction:";
309 Collection<ValueObject> col;
310 CpoWhere cw;
311
312 try {
313 ValueObject valObj = ValueObjectFactory.createValueObject(IDB + 6);
314 cw =
315 cpoAdapter
316 .whereBuilder()
317 .where(ValueObject.ATTR_ID, Comparison.EQ, valObj)
318 .attributeFunction("ABS(id)")
319 .build();
320
321 ArrayList<CpoWhere> wheres = new ArrayList<>();
322 wheres.add(cw);
323 try (Stream<ValueObject> beans =
324 cpoAdapter.retrieveBeans(
325 CpoQuery.group(ValueObject.FG_LIST_TESTWHERERETRIEVE).wheres(wheres), valObj)) {
326 var list =
327 beans
328 .filter(b -> Math.abs(b.getId()) >= IDB && Math.abs(b.getId()) < IDB + 100000)
329 .toList();
330 assertEquals(list.size(), 1, "Number of beans is " + list.size());
331 var rvo = list.getFirst();
332 assertEquals(rvo.getId(), -(IDB + 6), "-6 != " + rvo.getId());
333 }
334 } catch (Exception e) {
335 fail(method + e.getMessage());
336 }
337 }
338
339 @Test
340 public void testValueFunction() {
341 String method = "testValueFunction:";
342 Collection<ValueObject> col;
343 CpoWhere cw;
344
345 try {
346 ValueObject valObj = ValueObjectFactory.createValueObject(-(IDB + 1));
347 cw =
348 cpoAdapter
349 .whereBuilder()
350 .where(ValueObject.ATTR_ID, Comparison.EQ, valObj, false)
351 .valueFunction("abs(id)")
352 .build();
353
354 ArrayList<CpoWhere> wheres = new ArrayList<>();
355 wheres.add(cw);
356 try (Stream<ValueObject> beans =
357 cpoAdapter.retrieveBeans(
358 CpoQuery.group(ValueObject.FG_LIST_TESTWHERERETRIEVE).wheres(wheres), valObj)) {
359 var list =
360 beans
361 .filter(b -> Math.abs(b.getId()) >= IDB && Math.abs(b.getId()) < IDB + 100000)
362 .toList();
363 assertEquals(list.size(), 1, "Number of beans is " + list.size());
364 var rvo = list.getFirst();
365 assertEquals(rvo.getId(), IDB + 1, "1 != " + rvo.getId());
366 }
367 } catch (Exception e) {
368 fail(method + e.getMessage());
369 }
370 }
371
372 @Test
373 public void testAndWhere() {
374 String method = "testAndWhere:";
375
376 try {
377 ValueObject valObj = ValueObjectFactory.createValueObject(IDB + 3);
378 CpoWhere cw =
379 cpoAdapter
380 .whereBuilder()
381 .where(ValueObject.ATTR_ATTRCHAR, Comparison.ISNULL, null)
382 .and(ValueObject.ATTR_ATTRCHAR, Comparison.ISNULL, null, true)
383 .build();
384
385 ArrayList<CpoWhere> wheres = new ArrayList<>();
386 wheres.add(cw);
387 try (Stream<ValueObject> beans =
388 cpoAdapter.retrieveBeans(
389 CpoQuery.group(ValueObject.FG_LIST_TESTWHERERETRIEVE).wheres(wheres), valObj)) {
390 long count =
391 beans
392 .filter(b -> Math.abs(b.getId()) >= IDB && Math.abs(b.getId()) < IDB + 100000)
393 .count();
394 assertEquals(count, 0, "Number of beans is " + count);
395 }
396
397 cw =
398 cpoAdapter
399 .whereBuilder()
400 .where(ValueObject.ATTR_ATTRCHAR, Comparison.ISNULL, null)
401 .and(ValueObject.ATTR_ID, Comparison.EQ, valObj)
402 .build();
403
404 wheres.clear();
405 wheres.add(cw);
406 try (Stream<ValueObject> beans =
407 cpoAdapter.retrieveBeans(
408 CpoQuery.group(ValueObject.FG_LIST_TESTWHERERETRIEVE).wheres(wheres), valObj)) {
409 long count =
410 beans
411 .filter(b -> Math.abs(b.getId()) >= IDB && Math.abs(b.getId()) < IDB + 100000)
412 .count();
413 assertEquals(count, 1, "Number of beans is " + count);
414 }
415 } catch (Exception e) {
416 fail(method + e.getMessage());
417 }
418 }
419
420 @Test
421 public void testOrWhere() {
422 String method = "testOrWhere:";
423
424 try {
425 ValueObject valObj = ValueObjectFactory.createValueObject(IDB + 3);
426 CpoWhere cw =
427 cpoAdapter
428 .whereBuilder()
429 .where(ValueObject.ATTR_ID, Comparison.EQ, null)
430 .staticValue(String.valueOf(IDB + 2))
431 .or(ValueObject.ATTR_ID, Comparison.EQ, valObj)
432 .build();
433
434 ArrayList<CpoWhere> wheres = new ArrayList<>();
435 wheres.add(cw);
436 try (Stream<ValueObject> beans =
437 cpoAdapter.retrieveBeans(
438 CpoQuery.group(ValueObject.FG_LIST_TESTWHERERETRIEVE).wheres(wheres), valObj)) {
439 long count =
440 beans
441 .filter(b -> Math.abs(b.getId()) >= IDB && Math.abs(b.getId()) < IDB + 100000)
442 .count();
443 assertEquals(count, 2, "Number of beans is " + count);
444 }
445
446 cw =
447 cpoAdapter
448 .whereBuilder()
449 .where(ValueObject.ATTR_ID, Comparison.EQ, null)
450 .staticValue(String.valueOf(IDB + 3))
451 .or(ValueObject.ATTR_ID, Comparison.EQ, valObj, true)
452 .build();
453
454 wheres.clear();
455 wheres.add(cw);
456 try (Stream<ValueObject> beans =
457 cpoAdapter.retrieveBeans(
458 CpoQuery.group(ValueObject.FG_LIST_TESTWHERERETRIEVE).wheres(wheres), valObj)) {
459 long count =
460 beans
461 .filter(b -> Math.abs(b.getId()) >= IDB && Math.abs(b.getId()) < IDB + 100000)
462 .count();
463 assertEquals(count, 6, "Number of beans is " + count);
464 }
465 } catch (Exception e) {
466 fail(method + e.getMessage());
467 }
468 }
469
470 @Test
471 public void testRightAttributeFunction() {
472 String method = "testRightAttributeFunction:";
473
474 try {
475 ValueObject valObj = ValueObjectFactory.createValueObject(-(IDB + 1));
476 CpoWhere cw =
477 cpoAdapter
478 .whereBuilder()
479 .where("id", Comparison.EQ, null)
480 .compareToAttribute("attrBigInt")
481 .attributeFunction("ABS(id)")
482 .rightAttributeFunction("ABS(attrBigInt)")
483 .build();
484
485 ArrayList<CpoWhere> wheres = new ArrayList<>();
486 wheres.add(cw);
487 try (Stream<ValueObject> beans =
488 cpoAdapter.retrieveBeans(
489 CpoQuery.group(ValueObject.FG_LIST_TESTWHERERETRIEVE).wheres(wheres), valObj)) {
490 var list =
491 beans
492 .filter(b -> Math.abs(b.getId()) >= IDB && Math.abs(b.getId()) < IDB + 100000)
493 .toList();
494 assertEquals(list.size(), 1, "Number of beans is " + list.size());
495 var rvo = list.getFirst();
496 assertEquals(rvo.getId(), IDB + 1, "1 != " + rvo.getId());
497 }
498 } catch (Exception e) {
499 fail(method + e.getMessage());
500 }
501 }
502
503 @Test
504 public void testRightAttribute() {
505 String method = "testRightAttribute:";
506
507 try {
508 ValueObject valObj = ValueObjectFactory.createValueObject(-(IDB + 1));
509 CpoWhere cw =
510 cpoAdapter
511 .whereBuilder()
512 .where("id", Comparison.EQ, null)
513 .compareToAttribute("attrBigInt")
514 .build();
515
516 try (Stream<ValueObject> beans =
517 cpoAdapter.retrieveBeans(
518 CpoQuery.group(ValueObject.FG_LIST_TESTWHERERETRIEVE).where(cw), valObj, valObj)) {
519 var list =
520 beans
521 .filter(b -> Math.abs(b.getId()) >= IDB && Math.abs(b.getId()) < IDB + 100000)
522 .toList();
523 assertEquals(list.size(), 1, "Number of beans is " + list.size());
524 var rvo = list.getFirst();
525 assertEquals(rvo.getId(), IDB + 1, "1 != " + rvo.getId());
526 }
527 } catch (Exception e) {
528 fail(method + e.getMessage());
529 }
530 }
531
532 @Test
533 public void testMultipleBindWhere() {
534 String method = "testMultipleBindWhere:";
535
536 try {
537 ValueObject valObj = ValueObjectFactory.createValueObject(IDB + 1);
538 valObj.setAttrVarChar("Test");
539
540 CpoWhere cw =
541 cpoAdapter
542 .whereBuilder()
543 .where(ValueObject.ATTR_ATTRVARCHAR, Comparison.EQ, valObj)
544 .and(ValueObject.ATTR_ID, Comparison.EQ, valObj)
545 .build();
546
547 ArrayList<CpoWhere> wheres = new ArrayList<>();
548 wheres.add(cw);
549 try (Stream<ValueObject> beans =
550 cpoAdapter.retrieveBeans(
551 CpoQuery.group(ValueObject.FG_LIST_TESTWHERERETRIEVE).wheres(wheres), valObj)) {
552 long count =
553 beans
554 .filter(b -> Math.abs(b.getId()) >= IDB && Math.abs(b.getId()) < IDB + 100000)
555 .count();
556 assertEquals(count, 1, "Number of beans is " + count);
557 }
558 } catch (Exception e) {
559 fail(method + e.getMessage());
560 }
561 }
562
563 @Test
564 public void testLikeWhere() {
565 String method = "testLikeWhere:";
566
567 try {
568 ValueObject valObj = ValueObjectFactory.createValueObject(IDB + 1);
569 valObj.setAttrVarChar("T%");
570
571 CpoWhere cw =
572 cpoAdapter
573 .whereBuilder()
574 .where(ValueObject.ATTR_ATTRVARCHAR, Comparison.LIKE, valObj)
575 .and(ValueObject.ATTR_ID, Comparison.EQ, valObj)
576 .build();
577
578 ArrayList<CpoWhere> wheres = new ArrayList<>();
579 wheres.add(cw);
580 try (Stream<ValueObject> beans =
581 cpoAdapter.retrieveBeans(
582 CpoQuery.group(ValueObject.FG_LIST_TESTWHERERETRIEVE).wheres(wheres), valObj)) {
583 long count =
584 beans
585 .filter(b -> Math.abs(b.getId()) >= IDB && Math.abs(b.getId()) < IDB + 100000)
586 .count();
587 assertEquals(count, 1, "Number of beans is " + count);
588 }
589 } catch (Exception e) {
590 fail(method + e.getMessage());
591 }
592 }
593
594 @Test
595 public void testLikeWhereStrings() {
596 String method = "testLikeWhereStrings:";
597
598 try {
599 ValueObject valObj = ValueObjectFactory.createValueObject(IDB + 1);
600 valObj.setAttrVarChar("T%");
601
602 CpoWhere cw =
603 cpoAdapter
604 .whereBuilder()
605 .where(ValueObject.ATTR_ATTRVARCHAR, Comparison.LIKE, "T%")
606 .and(ValueObject.ATTR_ID, Comparison.EQ, IDB + 1)
607 .build();
608
609 ArrayList<CpoWhere> wheres = new ArrayList<>();
610 wheres.add(cw);
611 try (Stream<ValueObject> beans =
612 cpoAdapter.retrieveBeans(
613 CpoQuery.group(ValueObject.FG_LIST_TESTWHERERETRIEVE).wheres(wheres), valObj)) {
614 long count =
615 beans
616 .filter(b -> Math.abs(b.getId()) >= IDB && Math.abs(b.getId()) < IDB + 100000)
617 .count();
618 assertEquals(count, 1, "Number of beans is " + count);
619 }
620 } catch (Exception e) {
621 fail(method + e.getMessage());
622 }
623 }
624
625 @Test
626 public void testNonAttributeWhere() {
627 String method = "testNonAttributeWhere:";
628
629 try {
630 ValueObject valObj = ValueObjectFactory.createValueObject(IDB + 1);
631
632 CpoWhere cw =
633 cpoAdapter.whereBuilder().where("value_object.id", Comparison.LT, IDB + 1).build();
634
635 ArrayList<CpoWhere> wheres = new ArrayList<>();
636 wheres.add(cw);
637 try (Stream<ValueObject> beans =
638 cpoAdapter.retrieveBeans(
639 CpoQuery.group(ValueObject.FG_LIST_TESTWHERERETRIEVE).wheres(wheres), valObj)) {
640 long count =
641 beans
642 .filter(b -> Math.abs(b.getId()) >= IDB && Math.abs(b.getId()) < IDB + 100000)
643 .count();
644 assertEquals(count, 1, "Number of beans is " + count);
645 }
646 } catch (Exception e) {
647 fail(method + e.getMessage());
648 }
649 }
650
651 @Test
652 public void testInWhereStaticValue() {
653 String method = "testInWhereStaticValue:";
654
655 try {
656 ValueObject valObj = ValueObjectFactory.createValueObject(IDB + 1);
657
658 CpoWhere cw =
659 cpoAdapter
660 .whereBuilder()
661 .where(ValueObject.ATTR_ID, Comparison.IN, null)
662 .staticValue("(" + (IDB + 1) + "," + (IDB + 3) + "," + (IDB + 5) + ")")
663 .build();
664
665 ArrayList<CpoWhere> wheres = new ArrayList<>();
666 wheres.add(cw);
667 try (Stream<ValueObject> beans =
668 cpoAdapter.retrieveBeans(
669 CpoQuery.group(ValueObject.FG_LIST_TESTWHERERETRIEVE).wheres(wheres), valObj)) {
670 long count =
671 beans
672 .filter(b -> Math.abs(b.getId()) >= IDB && Math.abs(b.getId()) < IDB + 100000)
673 .count();
674 assertEquals(count, 3, "Number of beans is " + count);
675 }
676 } catch (Exception e) {
677 fail(method + e.getMessage());
678 }
679 }
680
681 @Test
682 public void testInWhereCollection() {
683 String method = "testInWhereCollection:";
684
685 try {
686 ValueObject valObj = ValueObjectFactory.createValueObject(IDB + 1);
687 Collection<Integer> inColl = new ArrayList<>();
688 inColl.add(IDB + 1);
689 inColl.add(IDB + 3);
690 inColl.add(IDB + 5);
691
692 CpoWhere cw =
693 cpoAdapter.whereBuilder().where(ValueObject.ATTR_ID, Comparison.IN, inColl).build();
694
695 ArrayList<CpoWhere> wheres = new ArrayList<>();
696 wheres.add(cw);
697 try (Stream<ValueObject> beans =
698 cpoAdapter.retrieveBeans(
699 CpoQuery.group(ValueObject.FG_LIST_TESTWHERERETRIEVE).wheres(wheres), valObj)) {
700 long count =
701 beans
702 .filter(b -> Math.abs(b.getId()) >= IDB && Math.abs(b.getId()) < IDB + 100000)
703 .count();
704 assertEquals(count, 3, "Number of beans is " + count);
705 }
706 } catch (Exception e) {
707 fail(method + e.getMessage());
708 }
709 }
710
711 @Test
712 public void testMultiInWhereCollection() {
713 String method = "testMultiInWhereCollection:";
714
715 try {
716 ValueObject valObj = ValueObjectFactory.createValueObject(IDB + 1);
717 Collection<Integer> inColl1 = new ArrayList<>();
718 inColl1.add(IDB + 1);
719 inColl1.add(IDB + 2);
720
721 Collection<Integer> inColl2 = new ArrayList<>();
722 inColl2.add(IDB + 3);
723 inColl2.add(IDB + 4);
724
725 Collection<Integer> inColl3 = new ArrayList<>();
726 inColl3.add(IDB + 5);
727 inColl3.add(-(IDB + 6));
728
729 CpoWhere cw =
730 cpoAdapter
731 .whereBuilder()
732 .where(ValueObject.ATTR_ID, Comparison.IN, inColl1)
733 .or(ValueObject.ATTR_ID, Comparison.IN, inColl2)
734 .or(ValueObject.ATTR_ID, Comparison.IN, inColl3)
735 .build();
736
737 ArrayList<CpoWhere> wheres = new ArrayList<>();
738 wheres.add(cw);
739 try (Stream<ValueObject> beans =
740 cpoAdapter.retrieveBeans(
741 CpoQuery.group(ValueObject.FG_LIST_TESTORDERBYRETRIEVE).wheres(wheres), valObj)) {
742 long count =
743 beans
744 .filter(b -> Math.abs(b.getId()) >= IDB && Math.abs(b.getId()) < IDB + 100000)
745 .count();
746 assertEquals(count, 6, "Number of beans is " + count);
747 }
748 } catch (Exception e) {
749 fail(method + e.getMessage());
750 }
751 }
752
753 @Test
754 public void testNonAttributeInWhereCollection() {
755 String method = "testNonAttributeInWhereCollection:";
756
757 try {
758 ValueObject valObj = ValueObjectFactory.createValueObject(IDB + 1);
759 Collection<Integer> inColl = new ArrayList<>();
760 inColl.add(IDB + 1);
761 inColl.add(IDB + 3);
762 inColl.add(IDB + 5);
763
764 CpoWhere cw =
765 cpoAdapter.whereBuilder().where("value_object.id", Comparison.IN, inColl).build();
766
767 ArrayList<CpoWhere> wheres = new ArrayList<>();
768 wheres.add(cw);
769 try (Stream<ValueObject> beans =
770 cpoAdapter.retrieveBeans(
771 CpoQuery.group(ValueObject.FG_LIST_TESTWHERERETRIEVE).wheres(wheres), valObj)) {
772 long count =
773 beans
774 .filter(b -> Math.abs(b.getId()) >= IDB && Math.abs(b.getId()) < IDB + 100000)
775 .count();
776 assertEquals(count, 3, "Number of beans is " + count);
777 }
778 } catch (Exception e) {
779 fail(method + e.getMessage());
780 }
781 }
782
783 @Test
784 public void testWhereParens() {
785 String method = "testWhereParens:";
786 Collection<ValueObject> col = null;
787
788 try {
789 ValueObject valObj = ValueObjectFactory.createValueObject(IDB + 1);
790
791
792
793 CpoWhere cw1 =
794 cpoAdapter
795 .whereBuilder()
796 .and(
797 g ->
798 g.where(ValueObject.ATTR_ID, Comparison.EQ, IDB + 1)
799 .or(ValueObject.ATTR_ID, Comparison.EQ, IDB + 3))
800 .build();
801
802 ArrayList<CpoWhere> wheres = new ArrayList<>();
803 ArrayList<CpoOrderBy> orderBys = new ArrayList<>();
804 wheres.add(cw1);
805
806 valObj =
807 cpoAdapter.retrieveBean(
808 CpoQuery.group(ValueObject.FG_RETRIEVE_NULL).wheres(wheres).orderBys(orderBys),
809 valObj);
810
811 assertNotNull(valObj, "Value Object should not be null");
812 assertEquals(valObj.getId(), IDB + 1, "Id should equal 1");
813 } catch (Exception e) {
814 fail(method + e.getMessage());
815 }
816 }
817
818 @Test
819 public void testWhereOrderBy() {
820 String method = "testWhereOrderBy:";
821 Collection<ValueObject> col = null;
822
823 try {
824 ValueObject valObj = ValueObjectFactory.createValueObject(IDB + 1);
825
826
827
828 CpoWhere cw1 =
829 cpoAdapter
830 .whereBuilder()
831 .and(
832 g ->
833 g.where(ValueObject.ATTR_ID, Comparison.EQ, IDB + 1)
834 .or(ValueObject.ATTR_ID, Comparison.EQ, IDB + 3))
835 .build();
836
837 ArrayList<CpoWhere> wheres = new ArrayList<>();
838 ArrayList<CpoOrderBy> orderBys = new ArrayList<>();
839 wheres.add(cw1);
840
841 valObj =
842 cpoAdapter.retrieveBean(
843 CpoQuery.group(ValueObject.FG_RETRIEVE_NULL).wheres(wheres).orderBys(orderBys),
844 valObj,
845 valObj);
846
847 assertNotNull(valObj, "Value Object should not be null");
848 assertEquals(valObj.getId(), IDB + 1, "Id should equal 1");
849 } catch (Exception e) {
850 fail(method + e.getMessage());
851 }
852 }
853 }