/
AbstractObjectArrayAssert.java
2269 lines (2183 loc) · 98.9 KB
/
AbstractObjectArrayAssert.java
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2017 the original author or authors.
*/
package org.assertj.core.api;
import static org.assertj.core.api.filter.Filters.filter;
import static org.assertj.core.description.Description.mostRelevantDescription;
import static org.assertj.core.extractor.Extractors.byName;
import static org.assertj.core.extractor.Extractors.extractedDescriptionOf;
import static org.assertj.core.extractor.Extractors.extractedDescriptionOfMethod;
import static org.assertj.core.extractor.Extractors.resultOf;
import static org.assertj.core.internal.CommonValidations.checkSequenceIsNotNull;
import static org.assertj.core.internal.CommonValidations.checkSubsequenceIsNotNull;
import static org.assertj.core.util.Arrays.isArray;
import static org.assertj.core.util.IterableUtil.toArray;
import static org.assertj.core.util.Lists.newArrayList;
import static org.assertj.core.util.Preconditions.checkNotNull;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import org.assertj.core.api.filter.FilterOperator;
import org.assertj.core.api.filter.Filters;
import org.assertj.core.api.iterable.Extractor;
import org.assertj.core.condition.Not;
import org.assertj.core.data.Index;
import org.assertj.core.description.Description;
import org.assertj.core.groups.FieldsOrPropertiesExtractor;
import org.assertj.core.groups.Tuple;
import org.assertj.core.internal.CommonErrors;
import org.assertj.core.internal.ComparatorBasedComparisonStrategy;
import org.assertj.core.internal.FieldByFieldComparator;
import org.assertj.core.internal.IgnoringFieldsComparator;
import org.assertj.core.internal.ObjectArrayElementComparisonStrategy;
import org.assertj.core.internal.ObjectArrays;
import org.assertj.core.internal.Objects;
import org.assertj.core.internal.OnFieldsComparator;
import org.assertj.core.internal.RecursiveFieldByFieldComparator;
import org.assertj.core.internal.TypeComparators;
import org.assertj.core.util.CheckReturnValue;
import org.assertj.core.util.IterableUtil;
import org.assertj.core.util.VisibleForTesting;
import org.assertj.core.util.introspection.IntrospectionError;
/**
* Assertion methods for arrays of objects.
* <p>
* To create an instance of this class, invoke <code>{@link Assertions#assertThat(Object[])}</code>.
* </p>
*
* @param <ELEMENT> the type of elements of the "actual" value.
*
* @author Yvonne Wang
* @author Alex Ruiz
* @author Joel Costigliola
* @author Nicolas François
* @author Mikhail Mazursky
* @author Mateusz Haligowski
* @author Lovro Pandzic
*/
public abstract class AbstractObjectArrayAssert<SELF extends AbstractObjectArrayAssert<SELF, ELEMENT>, ELEMENT> extends
AbstractAssert<SELF, ELEMENT[]> implements IndexedObjectEnumerableAssert<AbstractObjectArrayAssert<SELF, ELEMENT>, ELEMENT>,
ArraySortedAssert<AbstractObjectArrayAssert<SELF, ELEMENT>, ELEMENT> {
@VisibleForTesting
ObjectArrays arrays = ObjectArrays.instance();
private Map<String, Comparator<?>> comparatorsForElementPropertyOrFieldNames = new HashMap<>();
private TypeComparators comparatorsForElementPropertyOrFieldTypes = new TypeComparators();
public AbstractObjectArrayAssert(ELEMENT[] actual, Class<?> selfType) {
super(actual, selfType);
}
@Override
public SELF as(Description description) {
return super.as(description);
}
@Override
public SELF as(String description, Object... args) {
return super.as(description, args);
}
/**
* {@inheritDoc}
*
* @throws AssertionError {@inheritDoc}
*/
@Override
public void isNullOrEmpty() {
arrays.assertNullOrEmpty(info, actual);
}
/**
* {@inheritDoc}
*
* @throws AssertionError {@inheritDoc}
*/
@Override
public void isEmpty() {
arrays.assertEmpty(info, actual);
}
/**
* {@inheritDoc}
*
* @throws AssertionError {@inheritDoc}
*/
@Override
public SELF isNotEmpty() {
arrays.assertNotEmpty(info, actual);
return myself;
}
/**
* {@inheritDoc}
*
* @throws AssertionError {@inheritDoc}
*/
@Override
public SELF hasSize(int expected) {
arrays.assertHasSize(info, actual, expected);
return myself;
}
/**
* Verifies that the actual array has the same size as the given array.
* <p>
* Parameter is declared as Object to accept both {@code Object[]} and primitive arrays (e.g. {@code int[]}).
* <p>
* Example:
* <pre><code class='java'> int[] oneTwoThree = {1, 2, 3};
* int[] fourFiveSix = {4, 5, 6};
* int[] sevenEight = {7, 8};
*
* // assertion will pass
* assertThat(oneTwoThree).hasSameSizeAs(fourFiveSix);
*
* // assertion will fail
* assertThat(oneTwoThree).hasSameSizeAs(sevenEight);</code></pre>
*
* @param other the array to compare size with actual group.
* @return {@code this} assertion object.
* @throws AssertionError if the actual group is {@code null}.
* @throws AssertionError if the array parameter is {@code null} or is not a true array.
* @throws AssertionError if actual group and given array don't have the same size.
*/
@Override
public SELF hasSameSizeAs(Object other) {
// same implementation as in AbstractArrayAssert, but can't inherit from it due to generics problem ...
arrays.assertHasSameSizeAs(info, actual, other);
return myself;
}
/**
* Verifies that the actual group has the same size as the given {@link Iterable}.
* <p>
* Example:
* <pre><code class='java'> int[] oneTwoThree = {1, 2, 3};
* Iterable<Ring> elvesRings = newArrayList(vilya, nenya, narya);
*
* // assertion will pass
* assertThat(oneTwoThree).hasSameSizeAs(elvesRings);
*
* // assertion will fail
* assertThat(oneTwoThree).hasSameSizeAs(Arrays.asList("a", "b"));</code></pre>
*
* @param other the {@code Iterable} to compare size with actual group.
* @return {@code this} assertion object.
* @throws AssertionError if the actual group is {@code null}.
* @throws AssertionError if the other {@code Iterable} is {@code null}.
* @throws AssertionError if actual group and given {@code Iterable} don't have the same size.
*/
@Override
public SELF hasSameSizeAs(Iterable<?> other) {
arrays.assertHasSameSizeAs(info, actual, other);
return myself;
}
/**
* Verifies that the actual group contains the given values, in any order.
* <p>
* Example :
* <pre><code class='java'> String[] abc = {"a", "b", "c"};
*
* // assertions will pass
* assertThat(abc).contains("b", "a");
* assertThat(abc).contains("b", "a", "b");
*
* // assertions will fail
* assertThat(abc).contains("d");
* assertThat(abc).contains("c", "d");</code></pre>
*
* @param values the given values.
* @return {@code this} assertion object.
* @throws NullPointerException if the given argument is {@code null}.
* @throws IllegalArgumentException if the given argument is an empty array.
* @throws AssertionError if the actual group is {@code null}.
* @throws AssertionError if the actual group does not contain the given values.
*/
@Override
public SELF contains(@SuppressWarnings("unchecked") ELEMENT... values) {
arrays.assertContains(info, actual, values);
return myself;
}
/**
* Verifies that the actual group contains only the given values and nothing else, <b>in any order</b>.
* <p>
* Example :
* <pre><code class='java'> String[] abc = {"a", "b", "c"};
*
* // assertions will pass
* assertThat(abc).containsOnly("c", "b", "a");
* assertThat(abc).containsOnly("a", "a", "b", "c", "c");
*
* // assertion will fail because "c" is missing
* assertThat(abc).containsOnly("a", "b");</code></pre>
*
* @param values the given values.
* @return {@code this} assertion object.
* @throws NullPointerException if the given argument is {@code null}.
* @throws IllegalArgumentException if the given argument is an empty array.
* @throws AssertionError if the actual group is {@code null}.
* @throws AssertionError if the actual group does not contain the given values, i.e. the actual group contains some
* or none of the given values, or the actual group contains more values than the given ones.
*/
@Override
public SELF containsOnly(@SuppressWarnings("unchecked") ELEMENT... values) {
arrays.assertContainsOnly(info, actual, values);
return myself;
}
/**
* Same semantic as {@link #containsOnly(Object[])} : verifies that actual contains all elements of the given
* {@code Iterable} and nothing else, <b>in any order</b>.
* <p>
* Example :
* <pre><code class='java'> Ring[] rings = {nenya, vilya};
*
* // assertions will pass
* assertThat(rings).containsOnlyElementsOf(newArrayList(nenya, vilya));
* assertThat(rings).containsOnlyElementsOf(newArrayList(nenya, nenya, vilya, vilya));
*
* // assertion will fail as actual does not contain narya
* assertThat(rings).containsOnlyElementsOf(newArrayList(nenya, vilya, narya));
* // assertion will fail as actual contains nenya
* assertThat(rings).containsOnlyElementsOf(newArrayList(vilya));</code></pre>
*
* @param iterable the given {@code Iterable} we will get elements from.
*/
@Override
public SELF containsOnlyElementsOf(Iterable<? extends ELEMENT> iterable) {
return containsOnly(toArray(iterable));
}
/**
* An alias of {@link #containsOnlyElementsOf(Iterable)} : verifies that actual contains all elements of the
* given {@code Iterable} and nothing else, <b>in any order</b>.
* </p>
* Example:
* <pre><code class='java'> Ring[] elvesRings = {vilya, nenya, narya};
*
* // assertions will pass:
* assertThat(elvesRings).hasSameElementsAs(newArrayList(nenya, narya, vilya));
* assertThat(elvesRings).hasSameElementsAs(newArrayList(nenya, narya, vilya, nenya));
*
* // assertions will fail:
* assertThat(elvesRings).hasSameElementsAs(newArrayList(nenya, narya));
* assertThat(elvesRings).hasSameElementsAs(newArrayList(nenya, narya, vilya, oneRing));</code></pre>
*
* @param iterable the {@code Iterable} whose elements we expect to be present
* @return this assertion object
* @throws AssertionError if the actual group is {@code null}
* @throws NullPointerException if the given {@code Iterable} is {@code null}
* @throws AssertionError if the actual {@code Iterable} does not have the same elements, in any order, as the given
* {@code Iterable}
*/
@Override
public SELF hasSameElementsAs(Iterable<? extends ELEMENT> iterable) {
return containsOnlyElementsOf(iterable);
}
/**
* Verifies that the actual group contains the given values only once.
* <p>
* Examples :
* <pre><code class='java'> // array is a factory method to create arrays.
*
* // assertions will pass
* assertThat(array("winter", "is", "coming")).containsOnlyOnce("winter");
* assertThat(array("winter", "is", "coming")).containsOnlyOnce("coming", "winter");
*
* // assertions will fail
* assertThat(array("winter", "is", "coming")).containsOnlyOnce("Lannister");
* assertThat(array("Arya", "Stark", "daughter", "of", "Ned", "Stark")).containsOnlyOnce("Stark");
* assertThat(array("Arya", "Stark", "daughter", "of", "Ned", "Stark")).containsOnlyOnce("Stark", "Lannister", "Arya");</code></pre>
*
* @param values the given values.
* @return {@code this} assertion object.
* @throws NullPointerException if the given argument is {@code null}.
* @throws IllegalArgumentException if the given argument is an empty array.
* @throws AssertionError if the actual group is {@code null}.
* @throws AssertionError if the actual group does not contain the given values, i.e. the actual group contains some
* or none of the given values, or the actual group contains more than once these values.
*/
@Override
public SELF containsOnlyOnce(@SuppressWarnings("unchecked") ELEMENT... values) {
arrays.assertContainsOnlyOnce(info, actual, values);
return myself;
}
/**
* Verifies that the actual array contains only the given values and nothing else, <b>in order</b>.<br>
* <p>
* Example :
* <pre><code class='java'> Ring[] elvesRings = {vilya, nenya, narya};
*
* // assertion will pass
* assertThat(elvesRings).containsExactly(vilya, nenya, narya);
*
* // assertion will fail as actual and expected order differ
* assertThat(elvesRings).containsExactly(nenya, vilya, narya);</code></pre>
*
* @param values the given values.
* @return {@code this} assertion object.
* @throws NullPointerException if the given argument is {@code null}.
* @throws AssertionError if the actual group is {@code null}.
* @throws AssertionError if the actual group does not contain the given values with same order, i.e. the actual group
* contains some or none of the given values, or the actual group contains more values than the given ones
* or values are the same but the order is not.
*/
@Override
public SELF containsExactly(@SuppressWarnings("unchecked") ELEMENT... values) {
arrays.assertContainsExactly(info, actual, values);
return myself;
}
/** {@inheritDoc} */
@Override
public SELF containsExactlyInAnyOrder(@SuppressWarnings("unchecked") ELEMENT... values) {
arrays.assertContainsExactlyInAnyOrder(info, actual, values);
return myself;
}
/**
* Same as {@link #containsExactly(Object...)} but handles the {@link Iterable} to array conversion : verifies that
* actual contains all elements of the given {@code Iterable} and nothing else <b>in the same order</b>.
* <p>
* Example :
* <pre><code class='java'> Ring[] elvesRings = {vilya, nenya, narya};
*
* // assertion will pass
* assertThat(elvesRings).containsExactlyElementsOf(newLinkedList(vilya, nenya, narya));
*
* // assertion will fail as actual and expected order differ
* assertThat(elvesRings).containsExactlyElementsOf(newLinkedList(nenya, vilya, narya));</code></pre>
*
* @param iterable the given {@code Iterable} we will get elements from.
*/
@Override
public SELF containsExactlyElementsOf(Iterable<? extends ELEMENT> iterable) {
return containsExactly(toArray(iterable));
}
/**
* Verifies that the actual array contains the given sequence in the correct order and <b>without extra value between the sequence values</b>.
* <p>
* Use {@link #containsSubsequence(Object...)} to allow values between the expected sequence values.
* <p>
* Example:
* <pre><code class='java'> Ring[] elvesRings = {vilya, nenya, narya};
*
* // assertion will pass
* assertThat(elvesRings).containsSequence(vilya, nenya);
* assertThat(elvesRings).containsSequence(nenya, narya);
*
* // assertions will fail, the elements order is correct but there is a value between them (nenya)
* assertThat(elvesRings).containsSequence(vilya, narya);
* assertThat(elvesRings).containsSequence(nenya, vilya);</code></pre>
*
* @param sequence the sequence of objects to look for.
* @return this assertion object.
* @throws AssertionError if the actual group is {@code null}.
* @throws AssertionError if the given array is {@code null}.
* @throws AssertionError if the actual group does not contain the given sequence.
*/
@Override
public SELF containsSequence(@SuppressWarnings("unchecked") ELEMENT... sequence) {
arrays.assertContainsSequence(info, actual, sequence);
return myself;
}
/**
* Verifies that the actual array contains the given sequence in the correct order and <b>without extra value between the sequence values</b>.
* <p>
* Use {@link #containsSubsequence(List)} to allow values between the expected sequence values.
* <p>
* Example:
* <pre><code class='java'> Ring[] elvesRings = {vilya, nenya, narya};
*
* // assertion will pass
* assertThat(elvesRings).containsSequence(newArrayList(vilya, nenya));
* assertThat(elvesRings).containsSequence(newArrayList(nenya, narya));
*
* // assertions will fail, the elements order is correct but there is a value between them (nenya)
* assertThat(elvesRings).containsSequence(newArrayList(vilya, narya));
* assertThat(elvesRings).containsSequence(newArrayList(nenya, vilya));</code></pre>
*
* @param sequence the sequence of objects to look for.
* @return this assertion object.
* @throws AssertionError if the actual group is {@code null}.
* @throws AssertionError if the given array is {@code null}.
* @throws AssertionError if the actual group does not contain the given sequence.
*/
@Override
public SELF containsSequence(Iterable<? extends ELEMENT> sequence) {
checkSequenceIsNotNull(sequence);
arrays.assertContainsSequence(info, actual, toArray(sequence));
return myself;
}
/**
* Verifies that the actual array does not contain the given sequence in the given order and <b>without extra values between the sequence values</b>.
* <p>
* Use {@link #doesNotContainSubsequence(Object...)} to also ensure the sequence does not exist with values between the expected sequence values.
* <p>
* Example:
* <pre><code class='java'> Ring[] elvesRings = {vilya, nenya, narya};
*
* // assertion will pass, the elements order is correct but there is a value between them (nenya)
* assertThat(elvesRings).containsSequence(vilya, narya);
* assertThat(elvesRings).containsSequence(nenya, vilya);
*
* // assertions will fail
* assertThat(elvesRings).containsSequence(vilya, nenya);
* assertThat(elvesRings).containsSequence(nenya, narya);</code></pre>
*
* @param sequence the sequence of objects to look for.
* @return this assertion object.
* @throws AssertionError if the actual group is {@code null}.
* @throws AssertionError if the given array is {@code null}.
* @throws AssertionError if the actual group does not contain the given sequence.
*/
@Override
public SELF doesNotContainSequence(@SuppressWarnings("unchecked") ELEMENT... sequence) {
arrays.assertDoesNotContainSequence(info, actual, sequence);
return myself;
}
/**
* Verifies that the actual array does not contain the given sequence in the given order and <b>without extra values between the sequence values</b>.
* <p>
* Use {@link #doesNotContainSubsequence(Object...)} to also ensure the sequence does not exist with values between the expected sequence values.
* <p>
* Example:
* <pre><code class='java'> Ring[] elvesRings = {vilya, nenya, narya};
*
* // assertion will pass, the elements order is correct but there is a value between them (nenya)
* assertThat(elvesRings).containsSequence(newArrayList(vilya, narya));
* assertThat(elvesRings).containsSequence(newArrayList(nenya, vilya));
*
* // assertions will fail
* assertThat(elvesRings).containsSequence(newArrayList(vilya, nenya));
* assertThat(elvesRings).containsSequence(newArrayList(nenya, narya));</code></pre>
*
* @param sequence the sequence of objects to look for.
* @return this assertion object.
* @throws AssertionError if the actual group is {@code null}.
* @throws AssertionError if the given array is {@code null}.
* @throws AssertionError if the actual group does not contain the given sequence.
*/
@Override
public SELF doesNotContainSequence(Iterable<? extends ELEMENT> sequence) {
checkSequenceIsNotNull(sequence);
arrays.assertDoesNotContainSequence(info, actual, toArray(sequence));
return myself;
}
/**
* Verifies that the actual array contains the given subsequence in the correct order (possibly with other values between them).
* <p>
* Example:
* <pre><code class='java'> Ring[] elvesRings = {vilya, nenya, narya};
*
* // assertions will pass
* assertThat(elvesRings).containsSubsequence(vilya, nenya);
* assertThat(elvesRings).containsSubsequence(vilya, narya);
*
* // assertion will fail
* assertThat(elvesRings).containsSubsequence(nenya, vilya);</code></pre>
*
* @param subsequence the subsequence of objects to look for.
* @return this assertion object.
* @throws AssertionError if the actual group is {@code null}.
* @throws AssertionError if the given array is {@code null}.
* @throws AssertionError if the actual group does not contain the given subsequence.
*/
@Override
public SELF containsSubsequence(@SuppressWarnings("unchecked") ELEMENT... subsequence) {
arrays.assertContainsSubsequence(info, actual, subsequence);
return myself;
}
/**
* Verifies that the actual array contains the given subsequence in the correct order (possibly with other values between them).
* <p>
* Example:
* <pre><code class='java'> Ring[] elvesRings = {vilya, nenya, narya};
*
* // assertions will pass
* assertThat(elvesRings).containsSubsequence(newArrayList(vilya, nenya));
* assertThat(elvesRings).containsSubsequence(newArrayList(vilya, narya));
*
* // assertion will fail
* assertThat(elvesRings).containsSubsequence(newArrayList(nenya, vilya));</code></pre>
*
* @param subsequence the subsequence of objects to look for.
* @return this assertion object.
* @throws AssertionError if the actual group is {@code null}.
* @throws AssertionError if the given array is {@code null}.
* @throws AssertionError if the actual group does not contain the given subsequence.
*/
@Override
public SELF containsSubsequence(Iterable<? extends ELEMENT> subsequence) {
checkSubsequenceIsNotNull(subsequence);
arrays.assertContainsSubsequence(info, actual, toArray(subsequence));
return myself;
}
/**
* Verifies that the actual array does not contain the given subsequence in the correct order (possibly with other
* values between them).
* <p>
* Example:
* <pre><code class='java'> Ring[] elvesRings = {vilya, nenya, narya};
*
* // assertions will pass
* assertThat(elvesRings).doesNotContainSubsequence(nenya, vilya);
*
* // assertion will fail
* assertThat(elvesRings).doesNotContainSubsequence(vilya, nenya);
* assertThat(elvesRings).doesNotContainSubsequence(vilya, narya);</code></pre>
*
* @param subsequence the subsequence of objects to look for.
* @return this assertion object.
* @throws AssertionError if the actual group is {@code null}.
* @throws AssertionError if the given array is {@code null}.
* @throws AssertionError if the actual group contains the given subsequence.
*/
@Override
public SELF doesNotContainSubsequence(@SuppressWarnings("unchecked") ELEMENT... subsequence) {
arrays.assertDoesNotContainSubsequence(info, actual, subsequence);
return myself;
}
/**
* Verifies that the actual array does not contain the given subsequence in the correct order (possibly with other
* values between them).
* <p>
* Example:
* <pre><code class='java'> Ring[] elvesRings = {vilya, nenya, narya};
*
* // assertions will pass
* assertThat(elvesRings).doesNotContainSubsequence(newArrayList(nenya, vilya));
*
* // assertion will fail
* assertThat(elvesRings).doesNotContainSubsequence(newArrayList(vilya, nenya));
* assertThat(elvesRings).doesNotContainSubsequence(newArrayList(vilya, narya));</code></pre>
*
* @param subsequence the subsequence of objects to look for.
* @return this assertion object.
* @throws AssertionError if the actual group is {@code null}.
* @throws AssertionError if the given array is {@code null}.
* @throws AssertionError if the actual group contains the given subsequence.
*/
@Override
public SELF doesNotContainSubsequence(Iterable<? extends ELEMENT> subsequence) {
checkSubsequenceIsNotNull(subsequence);
arrays.assertDoesNotContainSubsequence(info, actual, toArray(subsequence));
return myself;
}
/**
* Verifies that the actual array contains the given object at the given index.
* <p>
* Example:
* <pre><code class='java'> Ring[] elvesRings = {vilya, nenya, narya};
*
* // assertions will pass
* assertThat(elvesRings).contains(vilya, atIndex(0));
* assertThat(elvesRings).contains(nenya, atIndex(1));
* assertThat(elvesRings).contains(narya, atIndex(2));
*
* // assertions will fail
* assertThat(elvesRings).contains(vilya, atIndex(1));
* assertThat(elvesRings).contains(nenya, atIndex(2));
* assertThat(elvesRings).contains(narya, atIndex(0));</code></pre>
*
* @param value the object to look for.
* @param index the index where the object should be stored in the actual group.
* @return this assertion object.
* @throws AssertionError if the actual group is {@code null} or empty.
* @throws NullPointerException if the given {@code Index} is {@code null}.
* @throws IndexOutOfBoundsException if the value of the given {@code Index} is equal to or greater than the size of the actual
* group.
* @throws AssertionError if the actual group does not contain the given object at the given index.
*/
@Override
public SELF contains(ELEMENT value, Index index) {
arrays.assertContains(info, actual, value, index);
return myself;
}
/**
* Verifies that all elements of the actual group are instances of given classes or interfaces.
* <p>
* Example :
* <pre><code class='java'> Object[] objects = { "foo", new StringBuilder() };
*
* // assertions will pass
* assertThat(objects).hasOnlyElementsOfTypes(CharSequence.class);
* assertThat(objects).hasOnlyElementsOfTypes(String.class, StringBuilder.class);
*
* // assertions will fail
* assertThat(objects).hasOnlyElementsOfTypes(Number.class);
* assertThat(objects).hasOnlyElementsOfTypes(String.class, Number.class);
* assertThat(objects).hasOnlyElementsOfTypes(String.class);</code></pre>
*
* @param types the expected classes and interfaces
* @return {@code this} assertion object.
* @throws NullPointerException if the given argument is {@code null}.
* @throws AssertionError if the actual group is {@code null}.
* @throws AssertionError if not all elements of the actual group are instances of one of the given types
* @since 2.7.0 / 3.7.0
*/
@Override
public SELF hasOnlyElementsOfTypes(Class<?>... types) {
arrays.assertHasOnlyElementsOfTypes(info, actual, types);
return myself;
}
/**
* Verifies that the actual array does not contain the given object at the given index.
* <p>
* Example:
* <pre><code class='java'> Ring[] elvesRings = {vilya, nenya, narya};
*
* // assertions will pass
* assertThat(elvesRings).doesNotContain(vilya, atIndex(1));
* assertThat(elvesRings).doesNotContain(nenya, atIndex(2));
* assertThat(elvesRings).doesNotContain(narya, atIndex(0));
*
* // assertions will fail
* assertThat(elvesRings).doesNotContain(vilya, atIndex(0));
* assertThat(elvesRings).doesNotContain(nenya, atIndex(1));
* assertThat(elvesRings).doesNotContain(narya, atIndex(2));</code></pre>
*
* @param value the object to look for.
* @param index the index where the object should not be stored in the actual group.
* @return this assertion object.
* @throws AssertionError if the actual group is {@code null}.
* @throws NullPointerException if the given {@code Index} is {@code null}.
* @throws AssertionError if the actual group contains the given object at the given index.
*/
@Override
public SELF doesNotContain(ELEMENT value, Index index) {
arrays.assertDoesNotContain(info, actual, value, index);
return myself;
}
/**
* Verifies that the actual array does not contain the given values.
* <p>
* Example :
* <pre><code class='java'> String[] abc = {"a", "b", "c"};
*
* // assertion will pass
* assertThat(abc).doesNotContain("d", "e");
*
* // assertions will fail
* assertThat(abc).doesNotContain("a");
* assertThat(abc).doesNotContain("a", "b", "c");
* assertThat(abc).doesNotContain("a", "x");</code></pre>
*
* @param values the given values.
* @return {@code this} assertion object.
* @throws NullPointerException if the given argument is {@code null}.
* @throws IllegalArgumentException if the given argument is an empty array.
* @throws AssertionError if the actual group is {@code null}.
* @throws AssertionError if the actual group contains any of the given values.
*/
@Override
public SELF doesNotContain(@SuppressWarnings("unchecked") ELEMENT... values) {
arrays.assertDoesNotContain(info, actual, values);
return myself;
}
/**
* Verifies that the actual array does not contain any elements of the given {@link Iterable} (i.e. none).
* <p>
* Example:
* <pre><code class='java'> String[] abc = {"a", "b", "c"};
*
* // assertion will pass
* assertThat(actual).doesNotContainAnyElementsOf(newArrayList("d", "e"));
*
* // assertions will fail
* assertThat(actual).doesNotContainAnyElementsOf(newArrayList("a", "b"));
* assertThat(actual).doesNotContainAnyElementsOf(newArrayList("d", "e", "a"));</code></pre>
*
* @param iterable the {@link Iterable} whose elements must not be in the actual group.
* @return {@code this} assertion object.
* @throws NullPointerException if the given argument is {@code null}.
* @throws IllegalArgumentException if the given argument is an empty iterable.
* @throws AssertionError if the actual group is {@code null}.
* @throws AssertionError if the actual group contains some elements of the given {@link Iterable}.
*/
@Override
public SELF doesNotContainAnyElementsOf(Iterable<? extends ELEMENT> iterable) {
arrays.assertDoesNotContainAnyElementsOf(info, actual, iterable);
return myself;
}
/**
* Verifies that the actual array does not contain duplicates.
* <p>
* Example :
* <pre><code class='java'> String[] abc = {"a", "b", "c"};
* String[] lotsOfAs = {"a", "a", "a"};
*
* // assertion will pass
* assertThat(abc).doesNotHaveDuplicates();
*
* // assertion will fail
* assertThat(lotsOfAs).doesNotHaveDuplicates();</code></pre>
*
* @return {@code this} assertion object.
* @throws AssertionError if the actual group is {@code null}.
* @throws AssertionError if the actual group contains duplicates.
*/
@Override
public SELF doesNotHaveDuplicates() {
arrays.assertDoesNotHaveDuplicates(info, actual);
return myself;
}
/**
* Verifies that the actual array starts with the given sequence of objects, without any other objects between them.
* Similar to <code>{@link #containsSequence(Object...)}</code>, but it also verifies that the first element in the
* sequence is also the first element of the actual array.
* <p>
* Example :
* <pre><code class='java'> String[] abc = {"a", "b", "c"};
*
* // assertion will pass
* assertThat(abc).startsWith("a", "b");
*
* // assertion will fail
* assertThat(abc).startsWith("c");</code></pre>
*
* @param sequence the sequence of objects to look for.
* @return this assertion object.
* @throws NullPointerException if the given argument is {@code null}.
* @throws IllegalArgumentException if the given argument is an empty array.
* @throws AssertionError if the actual group is {@code null}.
* @throws AssertionError if the actual group does not start with the given sequence of objects.
*/
@Override
public SELF startsWith(@SuppressWarnings("unchecked") ELEMENT... sequence) {
arrays.assertStartsWith(info, actual, sequence);
return myself;
}
/**
* Verifies that the actual array ends with the given sequence of objects, without any other objects between them.
* Similar to <code>{@link #containsSequence(Object...)}</code>, but it also verifies that the last element in the
* sequence is also last element of the actual array.
* <p>
* Example :
* <pre><code class='java'> String[] abc = {"a", "b", "c"};
*
* // assertion will pass
* assertThat(abc).endsWith("b", "c");
*
* // assertion will fail
* assertThat(abc).endsWith("a");</code></pre>
*
* @param sequence the sequence of objects to look for.
* @return this assertion object.
* @throws NullPointerException if the given argument is {@code null}.
* @throws IllegalArgumentException if the given argument is an empty array.
* @throws AssertionError if the actual group is {@code null}.
* @throws AssertionError if the actual group does not end with the given sequence of objects.
*/
@Override
public SELF endsWith(@SuppressWarnings("unchecked") ELEMENT... sequence) {
arrays.assertEndsWith(info, actual, sequence);
return myself;
}
/**
* Verifies that all elements of actual are present in the given {@code Iterable}.
* <p>
* Example:
* <pre><code class='java'> Ring[] elvesRings = {vilya, nenya, narya};
* List<Ring> ringsOfPower = newArrayList(oneRing, vilya, nenya, narya, dwarfRing, manRing);
*
* // assertion will pass:
* assertThat(elvesRings).isSubsetOf(ringsOfPower);
*
* // assertion will fail:
* assertThat(elvesRings).isSubsetOf(newArrayList(nenya, narya));</code></pre>
*
* @param values the {@code Iterable} that should contain all actual elements.
* @return this assertion object.
* @throws AssertionError if the actual {@code Iterable} is {@code null}.
* @throws NullPointerException if the given {@code Iterable} is {@code null}.
* @throws AssertionError if the actual {@code Iterable} is not subset of set {@code Iterable}.
*/
@Override
public SELF isSubsetOf(Iterable<? extends ELEMENT> values) {
arrays.assertIsSubsetOf(info, actual, values);
return myself;
}
/**
* Verifies that all elements of actual are present in the given values.
* <p>
* Example:
* <pre><code class='java'> Ring[] elvesRings = {vilya, nenya, narya};
*
* // assertions will pass:
* assertThat(elvesRings).isSubsetOf(vilya, nenya, narya);
* assertThat(elvesRings).isSubsetOf(vilya, nenya, narya, dwarfRing);
*
* // assertions will fail:
* assertThat(elvesRings).isSubsetOf(vilya, nenya);
* assertThat(elvesRings).isSubsetOf(vilya, nenya, dwarfRing);</code></pre>
*
* @param values the values that should be used for checking the elements of actual.
* @return this assertion object.
* @throws AssertionError if the actual {@code Iterable} is {@code null}.
* @throws AssertionError if the actual {@code Iterable} is not subset of the given values.
*/
@Override
public SELF isSubsetOf(@SuppressWarnings("unchecked") ELEMENT... values) {
arrays.assertIsSubsetOf(info, actual, Arrays.asList(values));
return myself;
}
/**
* Verifies that the actual array contains at least a null element.
* <p>
* Example :
* <pre><code class='java'> String[] abc = {"a", "b", "c"};
* String[] abNull = {"a", "b", null};
*
* // assertion will pass
* assertThat(abNull).containsNull();
*
* // assertion will fail
* assertThat(abc).containsNull();</code></pre>
*
* @return {@code this} assertion object.
* @throws AssertionError if the actual group is {@code null}.
* @throws AssertionError if the actual group does not contain a null element.
*/
@Override
public SELF containsNull() {
arrays.assertContainsNull(info, actual);
return myself;
}
/**
* Verifies that the actual array does not contain null elements.
* <p>
* Example :
* <pre><code class='java'> String[] abc = {"a", "b", "c"};
* String[] abNull = {"a", "b", null};
*
* // assertion will pass
* assertThat(abc).doesNotContainNull();
*
* // assertion will fail
* assertThat(abNull).doesNotContainNull();</code></pre>
*
* @return {@code this} assertion object.
* @throws AssertionError if the actual group is {@code null}.
* @throws AssertionError if the actual group contains a null element.
*/
@Override
public SELF doesNotContainNull() {
arrays.assertDoesNotContainNull(info, actual);
return myself;
}
/**
* Verifies that each element value satisfies the given condition
* <p>
* Example :
* <pre><code class='java'> String[] abc = {"a", "b", "c"};
* String[] abcc = {"a", "b", "cc"};
*
* Condition<String> singleCharacterString
* = new Condition<>(s -> s.length() == 1, "single character String");
*
* // assertion will pass
* assertThat(abc).are(singleCharacterString);
*
* // assertion will fail
* assertThat(abcc).are(singleCharacterString);</code></pre>
*
* @param condition the given condition.
* @return {@code this} object.
* @throws NullPointerException if the given condition is {@code null}.
* @throws AssertionError if an element cannot be cast to T.
* @throws AssertionError if one or more elements don't satisfy the given condition.
*/
@Override
public SELF are(Condition<? super ELEMENT> condition) {
arrays.assertAre(info, actual, condition);
return myself;
}
/**
* Verifies that each element value does not satisfy the given condition
* <p>
* Example :
* <pre><code class='java'> String[] abc = {"a", "b", "c"};
* String[] abcc = {"a", "b", "cc"};
*
* Condition<String> moreThanOneCharacter =
* = new Condition<>(s -> s.length() > 1, "more than one character");
*
* // assertion will pass
* assertThat(abc).areNot(moreThanOneCharacter);
*
* // assertion will fail
* assertThat(abcc).areNot(moreThanOneCharacter);</code></pre>
*
* @param condition the given condition.
* @return {@code this} object.
* @throws NullPointerException if the given condition is {@code null}.
* @throws AssertionError if an element cannot be cast to T.
* @throws AssertionError if one or more elements satisfy the given condition.
*/
@Override
public SELF areNot(Condition<? super ELEMENT> condition) {
arrays.assertAreNot(info, actual, condition);
return myself;
}
/**
* Verifies that all elements satisfy the given condition.
* <p>
* Example :
* <pre><code class='java'> String[] abc = {"a", "b", "c"};
* String[] abcc = {"a", "b", "cc"};
*
* Condition<String> onlyOneCharacter =
* = new Condition<>(s -> s.length() == 1, "only one character");
*
* // assertion will pass
* assertThat(abc).have(onlyOneCharacter);
*
* // assertion will fail
* assertThat(abcc).have(onlyOneCharacter);</code></pre>
*
* @param condition the given condition.
* @return {@code this} object.
* @throws NullPointerException if the given condition is {@code null}.
* @throws AssertionError if an element cannot be cast to T.
* @throws AssertionError if one or more elements do not satisfy the given condition.
*/
@Override
public SELF have(Condition<? super ELEMENT> condition) {
arrays.assertHave(info, actual, condition);
return myself;
}
/**
* Verifies that all elements don't satisfy the given condition.
* <p>