How to use commonCheckThatIterableAssertionSucceeds method of org.assertj.core.internal.Iterables class

Best Assertj code snippet using org.assertj.core.internal.Iterables.commonCheckThatIterableAssertionSucceeds

Source:Iterables.java Github

copy

Full Screen

...204 * @throws AssertionError if the given {@code Iterable} is {@code null}.205 * @throws AssertionError if the given {@code Iterable} does not contain the given values.206 */207 public void assertContains(AssertionInfo info, Iterable<?> actual, Object[] values) {208 if (commonCheckThatIterableAssertionSucceeds(info, actual, values)) return;209 // check for elements in values that are missing in actual.210 assertIterableContainsGivenValues(actual, values, info);211 }212 private void assertIterableContainsGivenValues(Iterable<?> actual, Object[] values, AssertionInfo info) {213 Set<Object> notFound = new LinkedHashSet<>();214 for (Object value : values) {215 if (!iterableContains(actual, value)) {216 notFound.add(value);217 }218 }219 if (notFound.isEmpty())220 return;221 throw failures.failure(info, shouldContain(actual, values, notFound, comparisonStrategy));222 }223 /**224 * Delegates to {@link ComparisonStrategy#iterableContains(Iterable, Object)}225 */226 private boolean iterableContains(Iterable<?> actual, Object value) {227 return comparisonStrategy.iterableContains(actual, value);228 }229 /**230 * Delegates to {@link ComparisonStrategy#iterablesRemoveFirst(Iterable, Object)}231 */232 private void iterablesRemoveFirst(Iterable<?> actual, Object value) {233 comparisonStrategy.iterablesRemoveFirst(actual, value);234 }235 /**236 * Delegates to {@link ComparisonStrategy#iterableRemoves(Iterable, Object)}237 */238 private void iterablesRemove(Iterable<?> actual, Object value) {239 comparisonStrategy.iterableRemoves(actual, value);240 }241 /**242 * Asserts that the given {@code Iterable} contains only the given values and nothing else, in any order.243 * 244 * @param info contains information about the assertion.245 * @param actual the given {@code Iterable}.246 * @param expectedValues the values that are expected to be in the given {@code Iterable}.247 * @throws NullPointerException if the array of values is {@code null}.248 * @throws IllegalArgumentException if the array of values is empty.249 * @throws AssertionError if the given {@code Iterable} is {@code null}.250 * @throws AssertionError if the given {@code Iterable} does not contain the given values or if the given251 * {@code Iterable} contains values that are not in the given array.252 */253 public void assertContainsOnly(AssertionInfo info, Iterable<?> actual, Object[] expectedValues) {254 if (commonCheckThatIterableAssertionSucceeds(info, actual, expectedValues)) return;255 // after the for loop, unexpected = expectedValues - actual256 List<Object> unexpectedValues = newArrayList(actual);257 // after the for loop, missing = actual - expectedValues258 List<Object> missingValues = newArrayList(expectedValues);259 for (Object expected : expectedValues) {260 if (iterableContains(actual, expected)) {261 // since expected was found in actual:262 // -- it does not belong to the missing elements263 iterablesRemove(missingValues, expected);264 // -- it does not belong to the unexpected elements265 iterablesRemove(unexpectedValues, expected);266 }267 }268 if (!unexpectedValues.isEmpty() || !missingValues.isEmpty()) {269 throw failures.failure(info, shouldContainOnly(actual, expectedValues,270 missingValues, unexpectedValues,271 comparisonStrategy));272 }273 }274 /**275 * Asserts that the given {@code Iterable} contains the given values and only once.276 * 277 * @param info contains information about the assertion.278 * @param actual the given {@code Iterable}.279 * @param values the values that are expected to be in the given {@code Iterable}.280 * @throws NullPointerException if the array of values is {@code null}.281 * @throws IllegalArgumentException if the array of values is empty.282 * @throws AssertionError if the given {@code Iterable} is {@code null}.283 * @throws AssertionError if the given {@code Iterable} does not contain the given values or if the given284 * {@code Iterable} contains values that are not in the given array.285 */286 public void assertContainsOnlyOnce(AssertionInfo info, Iterable<?> actual, Object[] values) {287 if (commonCheckThatIterableAssertionSucceeds(info, actual, values)) return;288 // check for elements in values that are missing in actual.289 Set<Object> notFound = new LinkedHashSet<>();290 Set<Object> notOnlyOnce = new LinkedHashSet<>();291 Iterable<?> actualDuplicates = comparisonStrategy.duplicatesFrom(actual);292 for (Object expectedOnlyOnce : values) {293 if (!iterableContains(actual, expectedOnlyOnce)) {294 notFound.add(expectedOnlyOnce);295 } else if (iterableContains(actualDuplicates, expectedOnlyOnce)) {296 notOnlyOnce.add(expectedOnlyOnce);297 }298 }299 if (!notFound.isEmpty() || !notOnlyOnce.isEmpty())300 throw failures.failure(info, shouldContainsOnlyOnce(actual, values, notFound, notOnlyOnce, comparisonStrategy));301 // assertion succeeded302 }303 /**304 * Asserts that the given {@code Iterable} contains only null elements and nothing else.305 *306 * @param info contains information about the assertion.307 * @param actual the given {@code Iterable}.308 * @throws AssertionError if the given {@code Iterable} is {@code null}.309 * @throws AssertionError if the given {@code Iterable} does not contain at least a null element or if the given310 * {@code Iterable} contains values that are not null elements.311 */312 public void assertContainsOnlyNulls(AssertionInfo info, Iterable<?> actual) {313 assertNotNull(info, actual);314 // empty => no null elements => failure315 if (sizeOf(actual) == 0) throw failures.failure(info, shouldContainOnlyNulls(actual));316 // look for any non null elements317 List<Object> nonNullElements = new ArrayList<>();318 for (Object element : actual) {319 if (element != null) nonNullElements.add(element);320 }321 if (nonNullElements.size() > 0) throw failures.failure(info, shouldContainOnlyNulls(actual, nonNullElements));322 }323 /**324 * Verifies that the given <code>{@link Iterable}</code> contains the given sequence of objects, without any other325 * objects between them.326 * 327 * @param info contains information about the assertion.328 * @param actual the given {@code Iterable}.329 * @param sequence the sequence of objects to look for.330 * @throws AssertionError if the given {@code Iterable} is {@code null}.331 * @throws NullPointerException if the given sequence is {@code null}.332 * @throws IllegalArgumentException if the given sequence is empty.333 * @throws AssertionError if the given {@code Iterable} does not contain the given sequence of objects.334 */335 public void assertContainsSequence(AssertionInfo info, Iterable<?> actual, Object[] sequence) {336 if (commonCheckThatIterableAssertionSucceeds(info, actual, sequence)) return;337 // check for elements in values that are missing in actual.338 List<?> actualAsList = newArrayList(actual);339 for (int i = 0; i < actualAsList.size(); i++) {340 // look for given sequence in actual starting from current index (i)341 if (containsSequenceAtGivenIndex(actualAsList, sequence, i)) return;342 }343 throw actualDoesNotContainSequence(info, actual, sequence);344 }345 /**346 * Verifies that the given <code>{@link Iterable}</code> does not contain the given sequence of objects in order.347 *348 * @param info contains information about the assertion.349 * @param actual the given {@code Iterable}.350 * @param sequence the sequence of objects to look for.351 * @throws AssertionError if the given {@code Iterable} is {@code null}.352 * @throws NullPointerException if the given sequence is {@code null}.353 * @throws IllegalArgumentException if the given sequence is empty.354 * @throws AssertionError if the given {@code Iterable} does contain the given sequence of objects.355 */356 public void assertDoesNotContainSequence(AssertionInfo info, Iterable<?> actual, Object[] sequence) {357 checkIsNotNullSequence(sequence);358 checkIsNotEmptySequence(sequence);359 assertNotNull(info, actual);360 // check for elements in values that are missing in actual.361 List<?> actualAsList = newArrayList(actual);362 for (int index = 0; index < actualAsList.size(); index++) {363 // look for given sequence in actual starting from current index (i)364 if (containsSequenceAtGivenIndex(actualAsList, sequence, index)) {365 throw actualDoesContainSequence(info, actual, sequence, index);366 }367 }368 }369 /**370 * Verifies that the given <code>{@link Iterable}</code> contains the given subsequence of objects (possibly with371 * other values between them).372 * 373 * @param info contains information about the assertion.374 * @param actual the given {@code Iterable}.375 * @param subsequence the subsequence of objects to look for.376 * @throws AssertionError if the given {@code Iterable} is {@code null}.377 * @throws NullPointerException if the given sequence is {@code null}.378 * @throws IllegalArgumentException if the given subsequence is empty.379 * @throws AssertionError if the given {@code Iterable} does not contain the given subsequence of objects.380 */381 public void assertContainsSubsequence(AssertionInfo info, Iterable<?> actual, Object[] subsequence) {382 if (commonCheckThatIterableAssertionSucceeds(info, actual, subsequence)) return;383 Iterator<?> actualIterator = actual.iterator();384 int subsequenceIndex = 0;385 while (actualIterator.hasNext() && subsequenceIndex < subsequence.length) {386 Object actualNext = actualIterator.next();387 Object subsequenceNext = subsequence[subsequenceIndex];388 if (areEqual(actualNext, subsequenceNext)) subsequenceIndex++;389 }390 if (subsequenceIndex < subsequence.length) throw actualDoesNotContainSubsequence(info, actual, subsequence);391 }392 public void assertContainsSubsequence(AssertionInfo info, Iterable<?> actual, List<?> subsequence) {393 checkIsNotNull(subsequence);394 assertContainsSubsequence(info, actual, subsequence.toArray());395 }396 /**397 * Verifies that the given <code>{@link Iterable}</code> does not contain the given subsequence of objects (possibly398 * with other values between them).399 *400 * @param info contains information about the assertion.401 * @param actual the given {@code Iterable}.402 * @param subsequence the subsequence of objects to look for.403 * @throws AssertionError if the given {@code Iterable} is {@code null}.404 * @throws NullPointerException if the given sequence is {@code null}.405 * @throws IllegalArgumentException if the given subsequence is empty.406 * @throws AssertionError if the given {@code Iterable} contains the given subsequence of objects.407 */408 public void assertDoesNotContainSubsequence(AssertionInfo info, Iterable<?> actual, Object[] subsequence) {409 checkIsNotNullSubsequence(subsequence);410 checkIsNotEmptySubsequence(subsequence);411 assertNotNull(info, actual);412 int subsequenceIndex = 0;413 int subsequenceStartIndex = 0;414 List<?> actualAsList = newArrayList(actual);415 for (int index = 0; index < actualAsList.size(); index++) {416 Object actualNext = actualAsList.get(index);417 Object subsequenceNext = subsequence[subsequenceIndex];418 if (areEqual(actualNext, subsequenceNext)) {419 if (subsequenceIndex == 0) subsequenceStartIndex = index;420 subsequenceIndex++;421 }422 if (subsequenceIndex == subsequence.length) {423 throw actualContainsSubsequence(info, actual, subsequence, subsequenceStartIndex);424 }425 }426 }427 /**428 * Verifies that the actual <code>Iterable</code> is a subset of values <code>Iterable</code>. <br>429 * Both actual and given iterable are treated as sets, therefore duplicates on either of them are ignored.430 * 431 * @param info contains information about the assertion.432 * @param actual the actual {@code Iterable}.433 * @param values the {@code Iterable} that should contain all actual elements.434 * @throws AssertionError if the actual {@code Iterable} is {@code null}.435 * @throws NullPointerException if the given Iterable is {@code null}.436 * @throws AssertionError if the actual {@code Iterable} is not subset of set <code>{@link Iterable}</code>437 */438 public void assertIsSubsetOf(AssertionInfo info, Iterable<?> actual, Iterable<?> values) {439 assertNotNull(info, actual);440 checkIterableIsNotNull(info, values);441 List<Object> extra = newArrayList();442 for (Object actualElement : actual) {443 if (!iterableContains(values, actualElement)) extra.add(actualElement);444 }445 if (extra.size() > 0) throw failures.failure(info, shouldBeSubsetOf(actual, values, extra, comparisonStrategy));446 }447 /**448 * Return true if actualAsList contains exactly the given sequence at given starting index, false otherwise.449 * 450 * @param actualAsList the list to look sequence in451 * @param sequence the sequence to look for452 * @param startingIndex the index of actual list at which we start looking for sequence.453 * @return true if actualAsList contains exactly the given sequence at given starting index, false otherwise.454 */455 private boolean containsSequenceAtGivenIndex(List<?> actualAsList, Object[] sequence, int startingIndex) {456 // check that, starting from given index, actualAsList has enough remaining elements to contain sequence457 if (actualAsList.size() - startingIndex < sequence.length) return false;458 for (int i = 0; i < sequence.length; i++) {459 if (!areEqual(actualAsList.get(startingIndex + i), sequence[i])) return false;460 }461 return true;462 }463 /**464 * Delegates to {@link ComparisonStrategy#areEqual(Object, Object)}465 */466 private boolean areEqual(Object actual, Object other) {467 return comparisonStrategy.areEqual(actual, other);468 }469 private AssertionError actualDoesNotContainSequence(AssertionInfo info, Iterable<?> actual, Object[] sequence) {470 return failures.failure(info, shouldContainSequence(actual, sequence, comparisonStrategy));471 }472 private AssertionError actualDoesContainSequence(AssertionInfo info, Iterable<?> actual, Object[] sequence,473 int index) {474 return failures.failure(info, shouldNotContainSequence(actual, sequence, index, comparisonStrategy));475 }476 private AssertionError actualDoesNotContainSubsequence(AssertionInfo info, Iterable<?> actual, Object[] subsequence) {477 return failures.failure(info, shouldContainSubsequence(actual, subsequence, comparisonStrategy));478 }479 private AssertionError actualContainsSubsequence(AssertionInfo info, Iterable<?> actual, Object[] subsequence,480 int index) {481 return failures.failure(info, shouldNotContainSubsequence(actual, subsequence, comparisonStrategy, index));482 }483 /**484 * Asserts that the given {@code Iterable} does not contain the given values.485 * 486 * @param info contains information about the assertion.487 * @param actual the given {@code Iterable}.488 * @param values the values that are expected not to be in the given {@code Iterable}.489 * @throws NullPointerException if the array of values is {@code null}.490 * @throws IllegalArgumentException if the array of values is empty.491 * @throws AssertionError if the given {@code Iterable} is {@code null}.492 * @throws AssertionError if the given {@code Iterable} contains any of given values.493 */494 public void assertDoesNotContain(AssertionInfo info, Iterable<?> actual, Object[] values) {495 checkIsNotNullAndNotEmpty(values);496 assertNotNull(info, actual);497 Set<Object> found = new LinkedHashSet<>();498 for (Object o : values) {499 if (iterableContains(actual, o)) found.add(o);500 }501 if (!found.isEmpty()) throw failures.failure(info, shouldNotContain(actual, values, found, comparisonStrategy));502 }503 /**504 * Asserts that the given {@code Iterable} does not contain the given values.505 * 506 * @param info contains information about the assertion.507 * @param actual the given {@code Iterable}.508 * @param iterable the values that are expected not to be in the given {@code Iterable}.509 * @throws NullPointerException if the array of values is {@code null}.510 * @throws IllegalArgumentException if the array of values is empty.511 * @throws AssertionError if the given {@code Iterable} is {@code null}.512 * @throws AssertionError if the given {@code Iterable} contains any of given values.513 */514 public <T> void assertDoesNotContainAnyElementsOf(AssertionInfo info, Iterable<? extends T> actual,515 Iterable<? extends T> iterable) {516 checkIsNotNullAndNotEmpty(iterable);517 List<T> values = newArrayList(iterable);518 assertDoesNotContain(info, actual, values.toArray());519 }520 /**521 * Asserts that the given {@code Iterable} does not have duplicate values.522 * 523 * @param info contains information about the assertion.524 * @param actual the given {@code Iterable}.525 * @throws NullPointerException if the array of values is {@code null}.526 * @throws IllegalArgumentException if the array of values is empty.527 * @throws AssertionError if the given {@code Iterable} is {@code null}.528 * @throws AssertionError if the given {@code Iterable} contains duplicate values.529 */530 public void assertDoesNotHaveDuplicates(AssertionInfo info, Iterable<?> actual) {531 assertNotNull(info, actual);532 Iterable<?> duplicates = comparisonStrategy.duplicatesFrom(actual);533 if (!isNullOrEmpty(duplicates))534 throw failures.failure(info, shouldNotHaveDuplicates(actual, duplicates, comparisonStrategy));535 }536 /**537 * Verifies that the given {@code Iterable} starts with the given sequence of objects, without any other objects538 * between them. Similar to <code>{@link #assertContainsSequence(AssertionInfo, Iterable, Object[])}</code>, but it539 * also verifies that the first element in the sequence is also the first element of the given {@code Iterable}.540 * 541 * @param info contains information about the assertion.542 * @param actual the given {@code Iterable}.543 * @param sequence the sequence of objects to look for.544 * @throws NullPointerException if the given argument is {@code null}.545 * @throws IllegalArgumentException if the given argument is an empty array.546 * @throws AssertionError if the given {@code Iterable} is {@code null}.547 * @throws AssertionError if the given {@code Iterable} does not start with the given sequence of objects.548 */549 public void assertStartsWith(AssertionInfo info, Iterable<?> actual, Object[] sequence) {550 if (commonCheckThatIterableAssertionSucceeds(info, actual, sequence)) return;551 int i = 0;552 for (Object actualCurrentElement : actual) {553 if (i >= sequence.length) break;554 if (areEqual(actualCurrentElement, sequence[i++])) continue;555 throw actualDoesNotStartWithSequence(info, actual, sequence);556 }557 if (sequence.length > i) {558 // sequence has more elements than actual559 throw actualDoesNotStartWithSequence(info, actual, sequence);560 }561 }562 private AssertionError actualDoesNotStartWithSequence(AssertionInfo info, Iterable<?> actual, Object[] sequence) {563 return failures.failure(info, shouldStartWith(actual, sequence, comparisonStrategy));564 }565 /**566 * Verifies that the given {@code Iterable} ends with the given sequence of objects, without any other objects between567 * them. Similar to <code>{@link #assertContainsSequence(AssertionInfo, Iterable, Object[])}</code>, but it also568 * verifies that the last element in the sequence is also the last element of the given {@code Iterable}.569 * 570 * @param info contains information about the assertion.571 * @param actual the given {@code Iterable}.572 * @param first the first element of the end sequence.573 * @param rest the optional next elements of the end sequence.574 * @throws NullPointerException if the given argument is {@code null}.575 * @throws IllegalArgumentException if the given argument is an empty array.576 * @throws AssertionError if the given {@code Iterable} is {@code null}.577 * @throws AssertionError if the given {@code Iterable} does not end with the given sequence of objects.578 */579 public void assertEndsWith(AssertionInfo info, Iterable<?> actual, Object first, Object[] rest) {580 Object[] sequence = prepend(first, rest);581 assertEndsWith(info, actual, sequence);582 }583 /**584 * Verifies that the given {@code Iterable} ends with the given sequence of objects, without any other objects between585 * them. Similar to <code>{@link #assertContainsSequence(AssertionInfo, Iterable, Object[])}</code>, but it also586 * verifies that the last element in the sequence is also the last element of the given {@code Iterable}.587 *588 * @param info contains information about the assertion.589 * @param actual the given {@code Iterable}.590 * @param sequence the sequence of objects to look for.591 * @throws NullPointerException if the given argument is {@code null}.592 * @throws IllegalArgumentException if the given argument is an empty array.593 * @throws AssertionError if the given {@code Iterable} is {@code null}.594 * @throws AssertionError if the given {@code Iterable} does not end with the given sequence of objects.595 */596 public void assertEndsWith(AssertionInfo info, Iterable<?> actual, Object[] sequence) {597 checkNotNullIterables(info, actual, sequence);598 int sizeOfActual = sizeOf(actual);599 if (sizeOfActual < sequence.length) throw actualDoesNotEndWithSequence(info, actual, sequence);600 int start = sizeOfActual - sequence.length;601 int sequenceIndex = 0, indexOfActual = 0;602 for (Object actualElement : actual) {603 if (indexOfActual++ < start) continue;604 if (areEqual(actualElement, sequence[sequenceIndex++])) continue;605 throw actualDoesNotEndWithSequence(info, actual, sequence);606 }607 }608 private boolean commonCheckThatIterableAssertionSucceeds(AssertionInfo info, Iterable<?> actual, Object[] sequence) {609 checkNotNullIterables(info, actual, sequence);610 // if both actual and values are empty, then assertion passes.611 if (!actual.iterator().hasNext() && sequence.length == 0) return true;612 failIfEmptySinceActualIsNotEmpty(sequence);613 return false;614 }615 private void checkNotNullIterables(AssertionInfo info, Iterable<?> actual, Object[] sequence) {616 checkIsNotNull(sequence);617 assertNotNull(info, actual);618 }619 /**620 * Asserts that the given {@code Iterable} contains at least a null element.621 * 622 * @param info contains information about the assertion.623 * @param actual the given {@code Iterable}.624 * @throws AssertionError if the given {@code Iterable} is {@code null}.625 * @throws AssertionError if the given {@code Iterable} does not contain at least a null element.626 */627 public void assertContainsNull(AssertionInfo info, Iterable<?> actual) {628 assertNotNull(info, actual);629 if (!iterableContains(actual, null)) throw failures.failure(info, shouldContainNull(actual));630 }631 /**632 * Asserts that the given {@code Iterable} does not contain null elements.633 * 634 * @param info contains information about the assertion.635 * @param actual the given {@code Iterable}.636 * @throws AssertionError if the given {@code Iterable} is {@code null}.637 * @throws AssertionError if the given {@code Iterable} contains a null element.638 */639 public void assertDoesNotContainNull(AssertionInfo info, Iterable<?> actual) {640 assertNotNull(info, actual);641 if (iterableContains(actual, null)) throw failures.failure(info, shouldNotContainNull(actual));642 }643 /**644 * Assert that each element of given {@code Iterable} satisfies the given condition.645 * 646 * @param info contains information about the assertion.647 * @param actual the given {@code Iterable}.648 * @param condition the given {@code Condition}.649 * @throws NullPointerException if the given condition is {@code null}.650 * @throws AssertionError if an element cannot be cast to E.651 * @throws AssertionError if one or more elements do not satisfy the given condition.652 */653 public <E> void assertAre(AssertionInfo info, Iterable<? extends E> actual, Condition<? super E> condition) {654 assertNotNull(info, actual);655 conditions.assertIsNotNull(condition);656 try {657 List<E> notSatisfiesCondition = notSatisfyingCondition(actual, condition);658 if (!notSatisfiesCondition.isEmpty())659 throw failures.failure(info, elementsShouldBe(actual, notSatisfiesCondition, condition));660 } catch (ClassCastException e) {661 throw failures.failure(info, shouldBeSameGenericBetweenIterableAndCondition(actual, condition));662 }663 }664 /**665 * Assert that each element of given {@code Iterable} not satisfies the given condition.666 * 667 * @param info contains information about the assertion.668 * @param actual the given {@code Iterable}.669 * @param condition the given {@code Condition}.670 * @throws NullPointerException if the given condition is {@code null}.671 * @throws AssertionError if an element cannot be cast to E.672 * @throws AssertionError if one or more elements satisfy the given condition.673 */674 public <E> void assertAreNot(AssertionInfo info, Iterable<? extends E> actual, Condition<? super E> condition) {675 assertNotNull(info, actual);676 conditions.assertIsNotNull(condition);677 try {678 List<E> satisfiesCondition = satisfiesCondition(actual, condition);679 if (!satisfiesCondition.isEmpty())680 throw failures.failure(info, elementsShouldNotBe(actual, satisfiesCondition, condition));681 } catch (ClassCastException e) {682 throw failures.failure(info, shouldBeSameGenericBetweenIterableAndCondition(actual, condition));683 }684 }685 /**686 * Assert that each element of given {@code Iterable} satisfies the given condition.687 * 688 * @param info contains information about the assertion.689 * @param actual the given {@code Iterable}.690 * @param condition the given {@code Condition}.691 * @throws NullPointerException if the given condition is {@code null}.692 * @throws AssertionError if an element cannot be cast to E.693 * @throws AssertionError if one or more elements do not satisfy the given condition.694 */695 public <E> void assertHave(AssertionInfo info, Iterable<? extends E> actual, Condition<? super E> condition) {696 assertNotNull(info, actual);697 conditions.assertIsNotNull(condition);698 try {699 List<E> notSatisfiesCondition = notSatisfyingCondition(actual, condition);700 if (!notSatisfiesCondition.isEmpty())701 throw failures.failure(info, elementsShouldHave(actual, notSatisfiesCondition, condition));702 } catch (ClassCastException e) {703 throw failures.failure(info, shouldBeSameGenericBetweenIterableAndCondition(actual, condition));704 }705 }706 /**707 * Assert that each element of given {@code Iterable} not satisfies the given condition.708 * 709 * @param info contains information about the assertion.710 * @param actual the given {@code Iterable}.711 * @param condition the given {@code Condition}.712 * @throws NullPointerException if the given condition is {@code null}.713 * @throws AssertionError if an element cannot be cast to E.714 * @throws AssertionError if one or more elements satisfy the given condition.715 */716 public <E> void assertDoNotHave(AssertionInfo info, Iterable<? extends E> actual, Condition<? super E> condition) {717 assertNotNull(info, actual);718 conditions.assertIsNotNull(condition);719 try {720 List<E> satisfiesCondition = satisfiesCondition(actual, condition);721 if (!satisfiesCondition.isEmpty())722 throw failures.failure(info, elementsShouldNotHave(actual, satisfiesCondition, condition));723 } catch (ClassCastException e) {724 throw failures.failure(info, shouldBeSameGenericBetweenIterableAndCondition(actual, condition));725 }726 }727 /**728 * Assert that there are <b>at least</b> <i>n</i> elements in the actual {@code Iterable} satisfying the given729 * condition.730 * 731 * @param info contains information about the assertion.732 * @param actual the given {@code Iterable}.733 * @param times the minimum number of times the condition should be verified.734 * @param condition the given {@code Condition}.735 * @throws NullPointerException if the given condition is {@code null}.736 * @throws AssertionError if an element cannot be cast to E.737 * @throws AssertionError if the number of elements satisfying the given condition is &lt; n.738 */739 public <E> void assertAreAtLeast(AssertionInfo info, Iterable<? extends E> actual, int times,740 Condition<? super E> condition) {741 assertNotNull(info, actual);742 conditions.assertIsNotNull(condition);743 try {744 if (!conditionIsSatisfiedAtLeastNTimes(actual, times, condition))745 throw failures.failure(info, elementsShouldBeAtLeast(actual, times, condition));746 } catch (ClassCastException e) {747 throw failures.failure(info, shouldBeSameGenericBetweenIterableAndCondition(actual, condition));748 }749 }750 private <E> boolean conditionIsSatisfiedAtLeastNTimes(Iterable<? extends E> actual, int n,751 Condition<? super E> condition) {752 List<E> satisfiesCondition = satisfiesCondition(actual, condition);753 return satisfiesCondition.size() >= n;754 }755 /**756 * Assert that there are <b>at most</b> <i>n</i> elements in the actual {@code Iterable} satisfying the given757 * condition.758 * 759 * @param info contains information about the assertion.760 * @param actual the given {@code Iterable}.761 * @param n the number of times the condition should be at most verified.762 * @param condition the given {@code Condition}.763 * @throws NullPointerException if the given condition is {@code null}.764 * @throws AssertionError if an element cannot be cast to E.765 * @throws AssertionError if the number of elements satisfying the given condition is &gt; n.766 */767 public <E> void assertAreAtMost(AssertionInfo info, Iterable<? extends E> actual, int n,768 Condition<? super E> condition) {769 assertNotNull(info, actual);770 conditions.assertIsNotNull(condition);771 try {772 if (!conditionIsSatisfiedAtMostNTimes(actual, condition, n))773 throw failures.failure(info, elementsShouldBeAtMost(actual, n, condition));774 } catch (ClassCastException e) {775 throw failures.failure(info, shouldBeSameGenericBetweenIterableAndCondition(actual, condition));776 }777 }778 private <E> boolean conditionIsSatisfiedAtMostNTimes(Iterable<? extends E> actual, Condition<? super E> condition,779 int n) {780 List<E> satisfiesCondition = satisfiesCondition(actual, condition);781 return satisfiesCondition.size() <= n;782 }783 /**784 * Verifies that there are <b>exactly</b> <i>n</i> elements in the actual {@code Iterable} satisfying the given785 * condition.786 * 787 * @param info contains information about the assertion.788 * @param actual the given {@code Iterable}.789 * @param times the exact number of times the condition should be verified.790 * @param condition the given {@code Condition}.791 * @throws NullPointerException if the given condition is {@code null}.792 * @throws AssertionError if an element cannot be cast to E.793 * @throws AssertionError if the number of elements satisfying the given condition is &ne; n.794 */795 public <E> void assertAreExactly(AssertionInfo info, Iterable<? extends E> actual, int times,796 Condition<? super E> condition) {797 assertNotNull(info, actual);798 conditions.assertIsNotNull(condition);799 try {800 if (!conditionIsSatisfiedNTimes(actual, condition, times))801 throw failures.failure(info, elementsShouldBeExactly(actual, times, condition));802 } catch (ClassCastException e) {803 throw failures.failure(info, shouldBeSameGenericBetweenIterableAndCondition(actual, condition));804 }805 }806 private <E> boolean conditionIsSatisfiedNTimes(Iterable<? extends E> actual, Condition<? super E> condition,807 int times) {808 List<E> satisfiesCondition = satisfiesCondition(actual, condition);809 return satisfiesCondition.size() == times;810 }811 /**812 * An alias method of {@link #assertAreAtLeast(AssertionInfo, Iterable, int, Condition)} to provide a richer fluent813 * api (same logic, only error message differs).814 */815 public <E> void assertHaveAtLeast(AssertionInfo info, Iterable<? extends E> actual, int times,816 Condition<? super E> condition) {817 assertNotNull(info, actual);818 conditions.assertIsNotNull(condition);819 try {820 if (!conditionIsSatisfiedAtLeastNTimes(actual, times, condition))821 throw failures.failure(info, elementsShouldHaveAtLeast(actual, times, condition));822 } catch (ClassCastException e) {823 throw failures.failure(info, shouldBeSameGenericBetweenIterableAndCondition(actual, condition));824 }825 }826 /**827 * An alias method of {@link #assertAreAtMost(AssertionInfo, Iterable, int, Condition)} to provide a richer fluent api828 * (same logic, only error message differs).829 */830 public <E> void assertHaveAtMost(AssertionInfo info, Iterable<? extends E> actual, int times,831 Condition<? super E> condition) {832 assertNotNull(info, actual);833 conditions.assertIsNotNull(condition);834 try {835 if (!conditionIsSatisfiedAtMostNTimes(actual, condition, times))836 throw failures.failure(info, elementsShouldHaveAtMost(actual, times, condition));837 } catch (ClassCastException e) {838 throw failures.failure(info, shouldBeSameGenericBetweenIterableAndCondition(actual, condition));839 }840 }841 /**842 * An alias method of {@link #assertAreExactly(AssertionInfo, Iterable, int, Condition)} to provide a richer fluent843 * api (same logic, only error message differs).844 */845 public <E> void assertHaveExactly(AssertionInfo info, Iterable<? extends E> actual, int times,846 Condition<? super E> condition) {847 assertNotNull(info, actual);848 conditions.assertIsNotNull(condition);849 try {850 if (!conditionIsSatisfiedNTimes(actual, condition, times))851 throw failures.failure(info, elementsShouldHaveExactly(actual, times, condition));852 } catch (ClassCastException e) {853 throw failures.failure(info, shouldBeSameGenericBetweenIterableAndCondition(actual, condition));854 }855 }856 /**857 * Asserts that the given {@code Iterable} contains all the elements of the other {@code Iterable}, in any order.858 * 859 * @param info contains information about the assertion.860 * @param actual the given {@code Iterable}.861 * @param other the other {@code Iterable}.862 * @throws NullPointerException if {@code Iterable} is {@code null}.863 * @throws AssertionError if the given {@code Iterable} is {@code null}.864 * @throws AssertionError if the given {@code Iterable} does not contain all the elements of the other865 * {@code Iterable}, in any order.866 */867 public void assertContainsAll(AssertionInfo info, Iterable<?> actual, Iterable<?> other) {868 if (other == null) throw iterableToLookForIsNull();869 assertNotNull(info, actual);870 Object[] values = newArrayList(other).toArray();871 assertIterableContainsGivenValues(actual, values, info);872 }873 /**874 * Asserts that the given {@code Iterable} contains exactly the given values and nothing else, <b>in order</b>.875 * 876 * @param info contains information about the assertion.877 * @param actual the given {@code Iterable}.878 * @param values the values that are expected to be in the given {@code Iterable} in order.879 * @throws NullPointerException if the array of values is {@code null}.880 * @throws AssertionError if the given {@code Iterable} is {@code null}.881 * @throws AssertionError if the given {@code Iterable} does not contain the given values or if the given882 * {@code Iterable} contains values that are not in the given array, in order.883 */884 public void assertContainsExactly(AssertionInfo info, Iterable<?> actual, Object[] values) {885 checkIsNotNull(values);886 assertNotNull(info, actual);887 int actualSize = sizeOf(actual);888 if (values.length != actualSize)889 throw failures.failure(info, shouldHaveSameSize(actual, values, actualSize, values.length, comparisonStrategy));890 assertHasSameSizeAs(info, actual, values); // include check that actual is not null891 List<Object> actualAsList = newArrayList(actual);892 IterableDiff diff = diff(actualAsList, asList(values), comparisonStrategy);893 if (!diff.differencesFound()) {894 // actual and values have the same elements but are they in the same order ?895 int i = 0;896 for (Object elementFromActual : actual) {897 if (!areEqual(elementFromActual, values[i])) {898 throw failures.failure(info, elementsDifferAtIndex(elementFromActual, values[i], i, comparisonStrategy));899 }900 i++;901 }902 return;903 }904 throw failures.failure(info,905 shouldContainExactly(actual, values, diff.missing, diff.unexpected, comparisonStrategy));906 }907 /**908 * Asserts that the given {@code Iterable} contains at least one of the given {@code values}.909 *910 * @param info contains information about the assertion.911 * @param actual the given {@code Iterable}.912 * @param values the values that, at least one of which is expected to be in the given {@code Iterable}.913 * @throws NullPointerException if the array of values is {@code null}.914 * @throws IllegalArgumentException if the array of values is empty and given {@code Iterable} is not empty.915 * @throws AssertionError if the given {@code Iterable} is {@code null}.916 * @throws AssertionError if the given {@code Iterable} does not contain any of given {@code values}.917 */918 public void assertContainsAnyOf(AssertionInfo info, Iterable<?> actual, Object[] values) {919 if (commonCheckThatIterableAssertionSucceeds(info, actual, values))920 return;921 Set<Object> valuesToSearchFor = newTreeSet(values);922 for (Object element : actual) {923 if (iterableContains(valuesToSearchFor, element)) return;924 }925 throw failures.failure(info, shouldContainAnyOf(actual, values, comparisonStrategy));926 }927 public void assertContainsExactlyInAnyOrder(AssertionInfo info, Iterable<?> actual, Object[] values) {928 checkIsNotNull(values);929 assertNotNull(info, actual);930 List<Object> notExpected = newArrayList(actual);931 List<Object> notFound = newArrayList(values);932 for (Object value : values) {933 if (iterableContains(notExpected, value)) {...

Full Screen

Full Screen

commonCheckThatIterableAssertionSucceeds

Using AI Code Generation

copy

Full Screen

1[INFO] --- maven-checkstyle-plugin:3.1.0:check (default) @ assertj-core ---2[ERROR] /home/travis/build/joel-costigliola/assertj-core/src/test/java/org/assertj/core/internal/iterables/Iterables_assertContainsAnyOf_Test.java:16:1: File contains tab characters (this is the first instance). [FileTabCharacter]3[ERROR] /home/travis/build/joel-costigliola/assertj-core/src/test/java/org/assertj/core/internal/iterables/Iterables_assertContainsAnyOf_Test.java:17:1: File contains tab characters (this is the first instance). [FileTabCharacter]4[ERROR] /home/travis/build/joel-costigliola/assertj-core/src/test/java/org/assertj/core/internal/iterables/Iterables_assertContainsAnyOf_Test.java:18:1: File contains tab characters (this is the first instance). [FileTabCharacter]5[ERROR] /home/travis/build/joel-costigliola/assertj-core/src/test/java/org/assertj/core/internal/iterables/Iterables_assertContainsAnyOf_Test.java:19:1: File contains tab characters (this is the first instance). [FileTabCharacter]6[ERROR] /home/travis/build/joel-costigliola/assertj-core/src/test/java/org/assertj/core/internal/iterables/Iterables_assertContainsAnyOf_Test.java:20:1: File contains tab characters (this is the first instance). [FileTabCharacter]

Full Screen

Full Screen

commonCheckThatIterableAssertionSucceeds

Using AI Code Generation

copy

Full Screen

1import static org.assertj.core.api.Assertions.assertThat;2import static org.assertj.core.api.Assertions.catchThrowable;3import static org.assertj.core.error.ShouldContainSequence.shouldContainSequence;4import static org.assertj.core.util.Lists.newArrayList;5import static org.assertj.core.util.FailureMessages.actualIsNull;6import static org.assertj.core.util.IterableUtil.toArray;7import static org.assertj.core.util.Lists.list;8import static org.assertj.core.internal.ErrorMessages.*;9import static org.assertj.core.test.ExpectedException.none;10import static org.assertj.core.test.TestData.someInfo;11import static org.assertj.core.util.Arrays.array;12import static org.assertj.core.util.FailureMessages.actualIsNull;13import static org.assertj.core.util.Lists.list;14import static org.mockito.Mockito.*;15import java.util.*;16import org.assertj.core.api.*;17import org.assertj.core.api.AssertionInfo;18import org.assertj.core.api.iterable.*;19import org.assertj.core.data.Index;20import org.assertj.core.internal.*;21import org.assertj.core.test.ExpectedException;22import org.assertj.core.util.*;23import org.junit.*;24public class Iterables_assertContainsSequence_Test extends IterablesBaseTest {25 public ExpectedException thrown = none();26 protected void initActualArray() {27 actual = array("Luke", "Yoda", "Leia");28 }29 protected void initActual() {30 actual = newArrayList("Luke", "Yoda", "Leia");31 }32 public void should_pass_if_actual_contains_given_values_exactly_in_order() {33 iterables.assertContainsSequence(someInfo(), actual, array("Luke", "Yoda", "Leia"));34 }35 public void should_pass_if_actual_contains_given_values_exactly_in_order_according_to_custom_comparison_strategy() {36 iterablesWithCaseInsensitiveComparisonStrategy.assertContainsSequence(someInfo(), actual, array("LUKE", "YODA", "LEIA"));37 }38 public void should_pass_if_actual_contains_given_values_exactly_in_order_with_duplicate_values() {39 actual = newArrayList("Luke", "Luke", "Yoda", "Yoda", "Leia");40 iterables.assertContainsSequence(someInfo(), actual, array("Luke", "Yoda", "Leia"));41 }42 public void should_pass_if_actual_contains_given_values_exactly_in_order_with_duplicate_values_according_to_custom_comparison_strategy() {43 actual = newArrayList("Luke",

Full Screen

Full Screen

commonCheckThatIterableAssertionSucceeds

Using AI Code Generation

copy

Full Screen

1[INFO] [INFO] --- maven-compiler-plugin:3.0:testCompile (default-testCompile) @ assertj-core ---2does not override abstract method getObjectsThatDoNotContainDuplicates() in org.assertj.core.internal.IterablesBaseTest3does not override abstract method getObjectsThatDoNotContainDuplicates() in org.assertj.core.internal.IterablesBaseTest4does not override abstract method getObjectsThatDoNotContainDuplicates() in org.assertj.core.internal.IterablesBaseTest5does not override abstract method getObjectsThatDoNotContainDuplicates() in org.assertj.core.internal.IterablesBaseTest6does not override abstract method getObjectsThatDoNotContainDuplicates() in org.assertj.core.internal.IterablesBaseTest

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Assertj automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in Iterables

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful