Best Assertj code snippet using org.assertj.core.internal.Strings.doCommonCheckForCharSequence
Source:Strings.java
...426 * @throws AssertionError if the given {@code CharSequence} is {@code null}.427 * @throws AssertionError if the actual {@code CharSequence} does not contain the given sequence.428 */429 public void assertContains(AssertionInfo info, CharSequence actual, CharSequence... values) {430 doCommonCheckForCharSequence(info, actual, values);431 Set<CharSequence> notFound = stream(values).filter(value -> !stringContains(actual, value))432 .collect(toCollection(LinkedHashSet::new));433 if (notFound.isEmpty()) return;434 if (notFound.size() == 1 && values.length == 1) {435 throw failures.failure(info, shouldContain(actual, values[0], comparisonStrategy));436 }437 throw failures.failure(info, shouldContain(actual, values, notFound, comparisonStrategy));438 }439 public void assertContainsAnyOf(AssertionInfo info, CharSequence actual, CharSequence[] values) {440 doCommonCheckForCharSequence(info, actual, values);441 boolean found = stream(values).anyMatch(value -> stringContains(actual, value));442 if (!found) throw failures.failure(info, shouldContainAnyOf(actual, values, comparisonStrategy));443 }444 /**445 * Verifies that the given {@code CharSequence} contains only digits.446 *447 * @param info contains information about the assertion.448 * @param actual the given {@code CharSequence}.449 * @throws NullPointerException if {@code actual} is {@code null}.450 * @throws AssertionError if {@code actual} contains non-digit characters or contains no digits at all.451 */452 public void assertContainsOnlyDigits(AssertionInfo info, CharSequence actual) {453 assertNotNull(info, actual);454 if (actual.length() == 0) throw failures.failure(info, shouldContainOnlyDigits(actual));455 for (int index = 0; index < actual.length(); index++) {456 char character = actual.charAt(index);457 if (!isDigit(character)) throw failures.failure(info, shouldContainOnlyDigits(actual, character, index));458 }459 }460 private void checkIsNotNull(CharSequence... values) {461 if (values == null) throw arrayOfValuesToLookForIsNull();462 }463 private void checkIsNotEmpty(CharSequence... values) {464 if (values.length == 0) throw arrayOfValuesToLookForIsEmpty();465 }466 private boolean stringContains(CharSequence actual, CharSequence sequence) {467 return comparisonStrategy.stringContains(actual.toString(), sequence.toString());468 }469 /**470 * Verifies that the given {@code CharSequence} contains the given sequence, ignoring case considerations.471 *472 * @param info contains information about the assertion.473 * @param actual the actual {@code CharSequence}.474 * @param sequence the sequence to search for.475 * @throws NullPointerException if the given sequence is {@code null}.476 * @throws AssertionError if the given {@code CharSequence} is {@code null}.477 * @throws AssertionError if the actual {@code CharSequence} does not contain the given sequence.478 */479 public void assertContainsIgnoringCase(AssertionInfo info, CharSequence actual, CharSequence sequence) {480 checkCharSequenceIsNotNull(sequence);481 assertNotNull(info, actual);482 if (!actual.toString().toLowerCase().contains(sequence.toString().toLowerCase()))483 throw failures.failure(info, shouldContainIgnoringCase(actual, sequence));484 }485 // CS427 Issue link: https://github.com/assertj/assertj-core/issues/2060486 /**487 * Verifies the given {@code CharSequence} has the strings, ignoring newlines.488 *489 * @param info contains information about the assertion.490 * @param actual the actual {@code CharSequence}.491 * @param values the values to look for.492 * @throws NullPointerException if the given sequence is {@code null}.493 * @throws IllegalArgumentException if the given values is empty.494 * @throws AssertionError if the given {@code CharSequence} is {@code null}.495 * @throws AssertionError if actual {@code CharSequence} doesn't have sequence496 */497 public void assertContainsIgnoringNewLines(final AssertionInfo info, final CharSequence actual, final CharSequence... values) {498 doCommonCheckForCharSequence(info, actual, values);499 final String actualNoNewLines = removeNewLines(actual);500 Set<CharSequence> notFound = stream(values).filter(value -> !stringContains(actualNoNewLines, removeNewLines(value)))501 .collect(toCollection(LinkedHashSet::new));502 if (notFound.isEmpty()) return;503 throw failures.failure(info, containsIgnoringNewLines(actual, values, notFound, comparisonStrategy));504 }505 /**506 * Verifies that the given {@code CharSequence} contains the given strings, ignoring whitespaces.507 *508 * @param info contains information about the assertion.509 * @param actual the actual {@code CharSequence}.510 * @param values the values to look for.511 * @throws NullPointerException if the given sequence is {@code null}.512 * @throws IllegalArgumentException if the given values is empty.513 * @throws AssertionError if the given {@code CharSequence} is {@code null}.514 * @throws AssertionError if the actual {@code CharSequence} does not contain the given sequence.515 */516 public void assertContainsIgnoringWhitespaces(AssertionInfo info, CharSequence actual, CharSequence... values) {517 doCommonCheckForCharSequence(info, actual, values);518 String actualWithoutWhitespace = removeAllWhitespaces(actual);519 Set<CharSequence> notFound = stream(values).map(Strings::removeAllWhitespaces)520 .filter(value -> !stringContains(actualWithoutWhitespace, value))521 .collect(toCollection(LinkedHashSet::new));522 if (notFound.isEmpty()) return;523 if (values.length == 1) {524 throw failures.failure(info, shouldContainIgnoringWhitespaces(actual, values[0], comparisonStrategy));525 }526 throw failures.failure(info, shouldContainIgnoringWhitespaces(actual, values, notFound, comparisonStrategy));527 }528 /**529 * Verifies that the given {@code CharSequence} does not contain any one of the given values, ignoring case considerations.530 *531 * @param info contains information about the assertion.532 * @param actual the actual {@code CharSequence}.533 * @param values the sequences to search for.534 *535 * @throws NullPointerException if the given list of values is {@code null}.536 * @throws NullPointerException if any one of the given values is {@code null}.537 * @throws IllegalArgumentException if the list of given values is empty.538 * @throws AssertionError if the actual {@code CharSequence} is {@code null}.539 * @throws AssertionError if the actual {@code CharSequence} contains any one of the given values, ignoring case considerations.540 */541 public void assertDoesNotContainIgnoringCase(AssertionInfo info, CharSequence actual, CharSequence... values) {542 doCommonCheckForCharSequence(info, actual, values);543 String actualLowerCase = actual.toString().toLowerCase();544 Set<CharSequence> foundValues = stream(values).filter(value -> actualLowerCase.contains(value.toString().toLowerCase()))545 .collect(toCollection(LinkedHashSet::new));546 if (foundValues.isEmpty()) return;547 if (foundValues.size() == 1 && values.length == 1) {548 throw failures.failure(info, shouldNotContainIgnoringCase(actual, values[0]));549 }550 throw failures.failure(info, shouldNotContainIgnoringCase(actual, values, foundValues));551 }552 /**553 * Verifies that the given {@code CharSequence} does not contain any one of the given values.554 *555 * @param info contains information about the assertion.556 * @param actual the actual {@code CharSequence}.557 * @param values the values to search for.558 * @throws NullPointerException if the given list of values is {@code null}.559 * @throws NullPointerException if any one of the given values is {@code null}.560 * @throws IllegalArgumentException if the list of given values is empty.561 * @throws AssertionError if the actual {@code CharSequence} is {@code null}.562 * @throws AssertionError if the actual {@code CharSequence} contains any one of the given values.563 */564 public void assertDoesNotContain(AssertionInfo info, CharSequence actual, CharSequence... values) {565 doCommonCheckForCharSequence(info, actual, values);566 Set<CharSequence> found = stream(values).filter(value -> stringContains(actual, value))567 .collect(toCollection(LinkedHashSet::new));568 if (found.isEmpty()) return;569 if (found.size() == 1 && values.length == 1) {570 throw failures.failure(info, shouldNotContain(actual, values[0], comparisonStrategy));571 }572 throw failures.failure(info, shouldNotContain(actual, values, found, comparisonStrategy));573 }574 private static void checkCharSequenceIsNotNull(CharSequence sequence) {575 requireNonNull(sequence, "The char sequence to look for should not be null");576 }577 /**578 * Verifies that two {@code CharSequence}s are equal, ignoring case considerations.579 *580 * @param info contains information about the assertion.581 * @param actual the actual {@code CharSequence}.582 * @param expected the expected {@code CharSequence}.583 * @throws AssertionError if the given {@code CharSequence}s are not equal.584 */585 public void assertEqualsIgnoringCase(AssertionInfo info, CharSequence actual, CharSequence expected) {586 if (!areEqualIgnoringCase(actual, expected)) throw failures.failure(info, shouldBeEqual(actual, expected), actual, expected);587 }588 /**589 * Verifies that two {@code CharSequence}s are not equal, ignoring case considerations.590 *591 * @param info contains information about the assertion.592 * @param actual the actual {@code CharSequence}.593 * @param expected the expected {@code CharSequence}.594 * @throws AssertionError if the given {@code CharSequence}s are equal ignoring case considerations.595 */596 public void assertNotEqualsIgnoringCase(AssertionInfo info, CharSequence actual, CharSequence expected) {597 if (areEqualIgnoringCase(actual, expected))598 throw failures.failure(info, shouldNotBeEqualIgnoringCase(actual, expected));599 }600 private boolean areEqualIgnoringCase(CharSequence actual, CharSequence expected) {601 if (actual == null) return expected == null;602 if (expected == null) return false;603 return actual.toString().equalsIgnoreCase(expected.toString());604 }605 /**606 * Verifies that two {@code CharSequence}s are equal, normalizing newlines.607 *608 * @param info contains information about the assertion.609 * @param actual the actual {@code CharSequence} (newlines will be normalized).610 * @param expected the expected {@code CharSequence} (newlines will be normalized)..611 * @throws AssertionError if the given {@code CharSequence}s are equal after normalizing newlines.612 */613 public void assertIsEqualToNormalizingNewlines(AssertionInfo info, CharSequence actual, CharSequence expected) {614 String actualNormalized = normalizeNewlines(actual);615 String expectedNormalized = normalizeNewlines(expected);616 if (!actualNormalized.equals(expectedNormalized))617 throw failures.failure(info, shouldBeEqualIgnoringNewLineDifferences(actual, expected), actualNormalized,618 expectedNormalized);619 }620 private static String normalizeNewlines(CharSequence actual) {621 return actual.toString().replace("\r\n", "\n");622 }623 /**624 * Verifies that two {@code CharSequence}s are equal, ignoring any differences in whitespace.625 *626 * @param info contains information about the assertion.627 * @param actual the actual {@code CharSequence}.628 * @param expected the expected {@code CharSequence}.629 * @throws AssertionError if the given {@code CharSequence}s are not equal.630 */631 public void assertEqualsIgnoringWhitespace(AssertionInfo info, CharSequence actual, CharSequence expected) {632 if (!areEqualIgnoringWhitespace(actual, expected))633 throw failures.failure(info, shouldBeEqualIgnoringWhitespace(actual, expected), actual, expected);634 }635 /**636 * Verifies that two {@code CharSequence}s are not equal, ignoring any differences in whitespace.637 *638 * @param info contains information about the assertion.639 * @param actual the actual {@code CharSequence}.640 * @param expected the expected {@code CharSequence}.641 * @throws AssertionError if the given {@code CharSequence}s are equal.642 */643 public void assertNotEqualsIgnoringWhitespace(AssertionInfo info, CharSequence actual, CharSequence expected) {644 if (areEqualIgnoringWhitespace(actual, expected))645 throw failures.failure(info, shouldNotBeEqualIgnoringWhitespace(actual, expected));646 }647 private boolean areEqualIgnoringWhitespace(CharSequence actual, CharSequence expected) {648 if (actual == null) return expected == null;649 checkCharSequenceIsNotNull(expected);650 return removeAllWhitespaces(actual).equals(removeAllWhitespaces(expected));651 }652 private static String removeAllWhitespaces(CharSequence toBeStripped) {653 final StringBuilder result = new StringBuilder(toBeStripped.length());654 for (int i = 0; i < toBeStripped.length(); i++) {655 char c = toBeStripped.charAt(i);656 if (isWhitespace(c)) {657 continue;658 }659 result.append(c);660 }661 return result.toString();662 }663 /**664 * Verifies that two {@code CharSequence}s are equal, after the whitespace of both strings665 * has been normalized.666 *667 * @param info contains information about the assertion.668 * @param actual the actual {@code CharSequence}.669 * @param expected the expected {@code CharSequence}.670 * @throws AssertionError if the given {@code CharSequence}s are not equal.671 * @since 2.8.0 / 3.8.0672 */673 public void assertEqualsNormalizingWhitespace(AssertionInfo info, CharSequence actual, CharSequence expected) {674 if (actual != null) checkCharSequenceIsNotNull(expected);675 String normalizedActual = normalizeWhitespace(actual);676 String normalizedExpected = normalizeWhitespace(expected);677 if (!java.util.Objects.equals(normalizedActual, normalizedExpected))678 throw failures.failure(info, shouldBeEqualNormalizingWhitespace(actual, expected), normalizedActual, normalizedExpected);679 }680 /**681 * Verifies that two {@code CharSequence}s are not equal, after the whitespace of both strings682 * has been normalized.683 *684 * @param info contains information about the assertion.685 * @param actual the actual {@code CharSequence}.686 * @param expected the expected {@code CharSequence}.687 * @throws AssertionError if the given {@code CharSequence}s are equal.688 * @since 2.8.0 / 3.8.0689 */690 public void assertNotEqualsNormalizingWhitespace(AssertionInfo info, CharSequence actual, CharSequence expected) {691 if (actual != null) checkCharSequenceIsNotNull(expected);692 String normalizedActual = normalizeWhitespace(actual);693 String normalizedExpected = normalizeWhitespace(expected);694 if (java.util.Objects.equals(normalizedActual, normalizedExpected))695 throw failures.failure(info, shouldNotBeEqualNormalizingWhitespace(actual, expected));696 }697 private static String normalizeWhitespace(CharSequence toNormalize) {698 if (toNormalize == null) return null;699 final StringBuilder result = new StringBuilder(toNormalize.length());700 boolean lastWasSpace = true;701 for (int i = 0; i < toNormalize.length(); i++) {702 char c = toNormalize.charAt(i);703 if (isWhitespace(c)) {704 if (!lastWasSpace) result.append(' ');705 lastWasSpace = true;706 } else {707 result.append(c);708 lastWasSpace = false;709 }710 }711 return result.toString().trim();712 }713 /**714 * Verifies that two {@code CharSequence}s are equal, after the punctuation of both strings715 * have been normalized.716 *717 * @param info contains information about the assertion.718 * @param actual the actual {@code CharSequence}.719 * @param expected the expected {@code CharSequence}.720 * @throws AssertionError if the given {@code CharSequence}s are not equal.721 * @since 3.16.0722 */723 public void assertEqualsNormalizingPunctuationAndWhitespace(AssertionInfo info, CharSequence actual, CharSequence expected) {724 if (actual != null) checkCharSequenceIsNotNull(expected);725 String normalizedActual = normalizeWhitespaceAndPunctuation(actual);726 String normalizedExpected = normalizeWhitespaceAndPunctuation(expected);727 if (!java.util.Objects.equals(normalizedActual, normalizedExpected))728 throw failures.failure(info, shouldBeEqualNormalizingPunctuationAndWhitespace(actual, expected), normalizedActual,729 normalizedExpected);730 }731 private static String normalizeWhitespaceAndPunctuation(CharSequence toNormalize) {732 if (toNormalize == null) {733 return null;734 }735 return normalizeWhitespace(toNormalize.toString().replaceAll(PUNCTUATION_REGEX, EMPTY_STRING));736 }737 /**738 * Verifies that two {@code CharSequence}s are equal, on their canonical form relying on {@link java.text.Normalizer}.739 * Using {@link java.text.Normalizer.Form#NFC} for canonical decomposition, followed by canonical composition.740 * <p>741 * Examples:742 * <pre><code class='java'>743 * // assertion succeeds744 * assertThat("\u00C4").isEqualToNormalizingUnicode("\u0041\u0308");745 * // assertion fails746 * assertThat("\u0041\u0308").isEqualToNormalizingUnicode("\u0041\u0308"); </code></pre>747 *748 * @param info contains information about the assertion.749 * @param actual the actual {@code CharSequence}.750 * @param expected the expected {@code CharSequence}.751 * @throws AssertionError if the given {@code CharSequence}s are not equal.752 * @since 3.19.0753 */754 public void assertEqualsToNormalizingUnicode(AssertionInfo info, CharSequence actual, CharSequence expected) {755 if (actual != null) checkCharSequenceIsNotNull(expected);756 String normalizedActual = Normalizer.normalize(actual, Normalizer.Form.NFC);757 String normalizedExpected = Normalizer.normalize(expected, Normalizer.Form.NFC);758 if (!java.util.Objects.equals(normalizedActual, normalizedExpected))759 throw failures.failure(info, shouldBeEqualNormalizingUnicode(actual, expected, normalizedActual, normalizedExpected),760 normalizedActual, normalizedExpected);761 }762 /**763 * Verifies that actual {@code CharSequence}s contains only once the given sequence.764 *765 * @param info contains information about the assertion.766 * @param actual the actual {@code CharSequence}.767 * @param sequence the given {@code CharSequence}.768 * @throws NullPointerException if the given sequence is {@code null}.769 * @throws AssertionError if the given {@code CharSequence} is {@code null}.770 * @throws AssertionError if the actual {@code CharSequence} does not contains <b>only once</b> the given771 * {@code CharSequence}.772 */773 public void assertContainsOnlyOnce(AssertionInfo info, CharSequence actual, CharSequence sequence) {774 checkCharSequenceIsNotNull(sequence);775 assertNotNull(info, actual);776 int sequenceOccurrencesInActual = countOccurrences(sequence, actual);777 if (sequenceOccurrencesInActual == 1) return;778 throw failures.failure(info,779 shouldContainOnlyOnce(actual, sequence, sequenceOccurrencesInActual, comparisonStrategy));780 }781 /**782 * Count occurrences of sequenceToSearch in actual {@link CharSequence}.783 *784 * @param sequenceToSearch the sequence to search in in actual {@link CharSequence}.785 * @param actual the {@link CharSequence} to search occurrences in.786 * @return the number of occurrences of sequenceToSearch in actual {@link CharSequence}.787 */788 private int countOccurrences(CharSequence sequenceToSearch, CharSequence actual) {789 String strToSearch = sequenceToSearch.toString();790 String strActual = actual.toString();791 int occurrences = 0;792 for (int i = 0; i <= (strActual.length() - strToSearch.length()); i++) {793 if (comparisonStrategy.areEqual(strActual.substring(i, i + sequenceToSearch.length()), strToSearch)) {794 occurrences++;795 }796 }797 return occurrences;798 }799 /**800 * Verifies that the given {@code CharSequence} starts with the given prefix.801 *802 * @param info contains information about the assertion.803 * @param actual the actual {@code CharSequence}.804 * @param prefix the given prefix.805 * @throws NullPointerException if the given sequence is {@code null}.806 * @throws AssertionError if the given {@code CharSequence} is {@code null}.807 * @throws AssertionError if the actual {@code CharSequence} does not start with the given prefix.808 */809 public void assertStartsWith(AssertionInfo info, CharSequence actual, CharSequence prefix) {810 failIfPrefixIsNull(prefix);811 assertNotNull(info, actual);812 if (!comparisonStrategy.stringStartsWith(actual.toString(), prefix.toString()))813 throw failures.failure(info, shouldStartWith(actual, prefix, comparisonStrategy));814 }815 /**816 * Verifies that the given {@code CharSequence} starts with the given prefix, ignoring case considerations.817 *818 * @param info contains information about the assertion.819 * @param actual the actual {@code CharSequence}.820 * @param prefix the given prefix.821 * @throws NullPointerException if the given sequence is {@code null}.822 * @throws AssertionError if the given {@code CharSequence} is {@code null}.823 * @throws AssertionError if the actual {@code CharSequence} does not start with the given prefix, ignoring case.824 * @since 3.23.0825 */826 public void assertStartsWithIgnoringCase(AssertionInfo info, CharSequence actual, CharSequence prefix) {827 failIfPrefixIsNull(prefix);828 assertNotNull(info, actual);829 if (!comparisonStrategy.stringStartsWith(actual.toString().toLowerCase(), prefix.toString().toLowerCase()))830 throw failures.failure(info, shouldStartWithIgnoringCase(actual, prefix, comparisonStrategy));831 }832 /**833 * Verifies that the given {@code CharSequence} does not start with the given prefix.834 *835 * @param info contains information about the assertion.836 * @param actual the actual {@code CharSequence}.837 * @param prefix the given prefix.838 * @throws NullPointerException if the given sequence is {@code null}.839 * @throws AssertionError if the given {@code CharSequence} is {@code null}.840 * @throws AssertionError if the actual {@code CharSequence} starts with the given prefix.841 */842 public void assertDoesNotStartWith(AssertionInfo info, CharSequence actual, CharSequence prefix) {843 failIfPrefixIsNull(prefix);844 assertNotNull(info, actual);845 if (comparisonStrategy.stringStartsWith(actual.toString(), prefix.toString()))846 throw failures.failure(info, shouldNotStartWith(actual, prefix, comparisonStrategy));847 }848 /**849 * Verifies that the given {@code CharSequence} does not start with the given prefix, ignoring case considerations.850 *851 * @param info contains information about the assertion.852 * @param actual the actual {@code CharSequence}.853 * @param prefix the given prefix.854 * @throws NullPointerException if the given sequence is {@code null}.855 * @throws AssertionError if the given {@code CharSequence} is {@code null}.856 * @throws AssertionError if the actual {@code CharSequence} starts with the given prefix, ignoring case.857 * @since 3.23.0858 */859 public void assertDoesNotStartWithIgnoringCase(AssertionInfo info, CharSequence actual, CharSequence prefix) {860 failIfPrefixIsNull(prefix);861 assertNotNull(info, actual);862 if (comparisonStrategy.stringStartsWith(actual.toString().toLowerCase(), prefix.toString().toLowerCase()))863 throw failures.failure(info, shouldNotStartWithIgnoringCase(actual, prefix, comparisonStrategy));864 }865 private static void failIfPrefixIsNull(CharSequence prefix) {866 requireNonNull(prefix, "The given prefix should not be null");867 }868 /**869 * Verifies that the given {@code CharSequence} ends with the given suffix.870 *871 * @param info contains information about the assertion.872 * @param actual the actual {@code CharSequence}.873 * @param suffix the given suffix.874 * @throws NullPointerException if the given sequence is {@code null}.875 * @throws AssertionError if the given {@code CharSequence} is {@code null}.876 * @throws AssertionError if the actual {@code CharSequence} does not end with the given suffix.877 */878 public void assertEndsWith(AssertionInfo info, CharSequence actual, CharSequence suffix) {879 failIfSuffixIsNull(suffix);880 assertNotNull(info, actual);881 if (!comparisonStrategy.stringEndsWith(actual.toString(), suffix.toString()))882 throw failures.failure(info, shouldEndWith(actual, suffix, comparisonStrategy));883 }884 /**885 * Verifies that the given {@code CharSequence} ends with the given suffix, ignoring case considerations.886 *887 * @param info contains information about the assertion.888 * @param actual the actual {@code CharSequence}.889 * @param suffix the given suffix.890 * @throws NullPointerException if the given sequence is {@code null}.891 * @throws AssertionError if the given {@code CharSequence} is {@code null}.892 * @throws AssertionError if the actual {@code CharSequence} does not end with the given suffix, ignoring case.893 * @since 3.23.0894 */895 public void assertEndsWithIgnoringCase(AssertionInfo info, CharSequence actual, CharSequence suffix) {896 failIfSuffixIsNull(suffix);897 assertNotNull(info, actual);898 if (!comparisonStrategy.stringEndsWith(actual.toString().toLowerCase(), suffix.toString().toLowerCase()))899 throw failures.failure(info, shouldEndWithIgnoringCase(actual, suffix, comparisonStrategy));900 }901 /**902 * Verifies that the given {@code CharSequence} does not end with the given suffix.903 *904 * @param info contains information about the assertion.905 * @param actual the actual {@code CharSequence}.906 * @param suffix the given suffix.907 * @throws NullPointerException if the given sequence is {@code null}.908 * @throws AssertionError if the given {@code CharSequence} is {@code null}.909 * @throws AssertionError if the actual {@code CharSequence} ends with the given suffix.910 */911 public void assertDoesNotEndWith(AssertionInfo info, CharSequence actual, CharSequence suffix) {912 failIfSuffixIsNull(suffix);913 assertNotNull(info, actual);914 if (comparisonStrategy.stringEndsWith(actual.toString(), suffix.toString()))915 throw failures.failure(info, shouldNotEndWith(actual, suffix, comparisonStrategy));916 }917 /**918 * Verifies that the given {@code CharSequence} does not end with the given suffix, ignoring case considerations.919 *920 * @param info contains information about the assertion.921 * @param actual the actual {@code CharSequence}.922 * @param suffix the given suffix.923 * @throws NullPointerException if the given sequence is {@code null}.924 * @throws AssertionError if the given {@code CharSequence} is {@code null}.925 * @throws AssertionError if the actual {@code CharSequence} ends with the given suffix, ignoring case.926 * @since 3.23.0927 */928 public void assertDoesNotEndWithIgnoringCase(AssertionInfo info, CharSequence actual, CharSequence suffix) {929 failIfSuffixIsNull(suffix);930 assertNotNull(info, actual);931 if (comparisonStrategy.stringEndsWith(actual.toString().toLowerCase(), suffix.toString().toLowerCase()))932 throw failures.failure(info, shouldNotEndWithIgnoringCase(actual, suffix, comparisonStrategy));933 }934 private static void failIfSuffixIsNull(CharSequence suffix) {935 requireNonNull(suffix, "The given suffix should not be null");936 }937 /**938 * Verifies that the given {@code CharSequence} matches the given regular expression.939 *940 * @param info contains information about the assertion.941 * @param actual the given {@code CharSequence}.942 * @param regex the regular expression to which the actual {@code CharSequence} is to be matched.943 * @throws NullPointerException if the given pattern is {@code null}.944 * @throws PatternSyntaxException if the regular expression's syntax is invalid.945 * @throws AssertionError if the given {@code CharSequence} is {@code null}.946 * @throws AssertionError if the actual {@code CharSequence} does not match the given regular expression.947 */948 public void assertMatches(AssertionInfo info, CharSequence actual, CharSequence regex) {949 checkRegexIsNotNull(regex);950 assertNotNull(info, actual);951 if (!Pattern.matches(regex.toString(), actual)) throw failures.failure(info, shouldMatch(actual, regex));952 }953 /**954 * Verifies that the given {@code CharSequence} does not match the given regular expression.955 *956 * @param info contains information about the assertion.957 * @param actual the given {@code CharSequence}.958 * @param regex the regular expression to which the actual {@code CharSequence} is to be matched.959 * @throws NullPointerException if the given pattern is {@code null}.960 * @throws PatternSyntaxException if the regular expression's syntax is invalid.961 * @throws AssertionError if the given {@code CharSequence} is {@code null}.962 * @throws AssertionError if the actual {@code CharSequence} matches the given regular expression.963 */964 public void assertDoesNotMatch(AssertionInfo info, CharSequence actual, CharSequence regex) {965 checkRegexIsNotNull(regex);966 assertNotNull(info, actual);967 if (Pattern.matches(regex.toString(), actual)) throw failures.failure(info, shouldNotMatch(actual, regex));968 }969 private void checkRegexIsNotNull(CharSequence regex) {970 if (regex == null) throw patternToMatchIsNull();971 }972 /**973 * Verifies that the given {@code CharSequence} matches the given regular expression.974 *975 * @param info contains information about the assertion.976 * @param actual the given {@code CharSequence}.977 * @param pattern the regular expression to which the actual {@code CharSequence} is to be matched.978 * @throws NullPointerException if the given pattern is {@code null}.979 * @throws AssertionError if the given {@code CharSequence} is {@code null}.980 * @throws AssertionError if the given {@code CharSequence} does not match the given regular expression.981 */982 public void assertMatches(AssertionInfo info, CharSequence actual, Pattern pattern) {983 checkIsNotNull(pattern);984 assertNotNull(info, actual);985 if (!pattern.matcher(actual).matches()) throw failures.failure(info, shouldMatch(actual, pattern.pattern()));986 }987 /**988 * Verifies that the given {@code CharSequence} does not match the given regular expression.989 *990 * @param info contains information about the assertion.991 * @param actual the given {@code CharSequence}.992 * @param pattern the regular expression to which the actual {@code CharSequence} is to be matched.993 * @throws NullPointerException if the given pattern is {@code null}.994 * @throws AssertionError if the given {@code CharSequence} matches the given regular expression.995 */996 public void assertDoesNotMatch(AssertionInfo info, CharSequence actual, Pattern pattern) {997 checkIsNotNull(pattern);998 if (!(actual == null || !pattern.matcher(actual).matches()))999 throw failures.failure(info, shouldNotMatch(actual, pattern.pattern()));1000 }1001 private void checkIsNotNull(Pattern pattern) {1002 if (pattern == null) throw patternToMatchIsNull();1003 }1004 private NullPointerException patternToMatchIsNull() {1005 return new NullPointerException("The regular expression pattern to match should not be null");1006 }1007 private void assertNotNull(AssertionInfo info, CharSequence actual) {1008 Objects.instance().assertNotNull(info, actual);1009 }1010 /**1011 * Verifies that the given charSequence contains the given sequence of charSequence, without any other charSequences between them.1012 * @param info contains information about the assertion.1013 * @param actual the given charSequence.1014 * @param sequence the sequence of charSequence to look for.1015 * @throws NullPointerException if the given sequence of charSequence is {@code null}.1016 * @throws IllegalArgumentException if the given sequence of charSequence is empty.1017 * @throws AssertionError if the given {@code CharSequence} is {@code null}.1018 * @throws AssertionError if the given {@code CharSequence} does not contain the given sequence of charSequence.1019 */1020 public void assertContainsSequence(AssertionInfo info, CharSequence actual, CharSequence[] sequence) {1021 doCommonCheckForCharSequence(info, actual, sequence);1022 Set<CharSequence> notFound = stream(sequence).filter(value -> !stringContains(actual, value))1023 .collect(toCollection(LinkedHashSet::new));1024 if (!notFound.isEmpty()) {1025 // don't bother looking for a sequence, some of the sequence elements were not found !1026 if (notFound.size() == 1 && sequence.length == 1) {1027 throw failures.failure(info, shouldContain(actual, sequence[0], comparisonStrategy));1028 }1029 throw failures.failure(info, shouldContain(actual, sequence, notFound, comparisonStrategy));1030 }1031 // we have found all the given values but were they in the expected order ?1032 if (sequence.length == 1) return; // no order check needed for a one element sequence1033 // convert all values to one char sequence to compare with the actual char sequence1034 String strActual = actual.toString();1035 String strSequence = String.join(EMPTY_STRING, sequence);1036 if (!stringContains(strActual, strSequence)) {1037 throw failures.failure(info, shouldContainSequence(actual, sequence, comparisonStrategy));1038 }1039 }1040 /**1041 * Verifies that the actual {@code CharSequence} contains all the given values <b>in the given order1042 * (possibly with other values between them)</b>.1043 *1044 * @param info contains information about the assertion.1045 * @param actual the given {@code CharSequence}.1046 * @param subsequence the Strings to look for, in order.1047 * @throws AssertionError if the given {@code CharSequence} is {@code null}.1048 * @throws NullPointerException if the given subsequence is {@code null}.1049 * @throws IllegalArgumentException if the given subsequence is empty.1050 * @throws AssertionError if the given {@code CharSequence} does not contain the given subsequence of charSequence.1051 */1052 public void assertContainsSubsequence(AssertionInfo info, CharSequence actual, CharSequence[] subsequence) {1053 doCommonCheckForCharSequence(info, actual, subsequence);1054 Set<CharSequence> notFound = stream(subsequence).filter(value -> !stringContains(actual, value))1055 .collect(toCollection(LinkedHashSet::new));1056 if (!notFound.isEmpty()) {1057 // don't bother looking for a subsequence, some of the subsequence elements were not found !1058 if (notFound.size() == 1 && subsequence.length == 1) {1059 throw failures.failure(info, shouldContain(actual, subsequence[0], comparisonStrategy));1060 }1061 throw failures.failure(info, shouldContain(actual, subsequence, notFound, comparisonStrategy));1062 }1063 // we have found all the given values but were they in the expected order ?1064 if (subsequence.length == 1) return; // no order check needed for a one element subsequence1065 // the values are in the correct order if after removing the start of actual up to the1066 // subsequence element included, we are able to find the next subsequence element, ex:1067 // "{ George Martin }" with subsequence ["George", " ", "Martin"]:1068 // - remove up to "George" in "{ George Martin }" -> " Martin }", does it contain " " ?1069 // - remove up to " " in " Martin }" -> "Martin }", does it contain "Martin" ?1070 // ...1071 String actualRest = removeUpTo(actual.toString(), subsequence[0]);1072 // check the subsequence second element since we already know the first is present1073 for (int i = 1; i < subsequence.length; i++) {1074 if (stringContains(actualRest, subsequence[i])) actualRest = removeUpTo(actualRest, subsequence[i]);1075 else throw failures.failure(info, shouldContainSubsequence(actual, subsequence, i - 1, comparisonStrategy));1076 }1077 }1078 private String removeUpTo(String string, CharSequence toRemove) {1079 // we have already checked that toRemove was not null in doCommonCheckForCharSequence and this point string is not neither1080 int index = indexOf(string, toRemove);1081 // remove the start of string up to toRemove included1082 return string.substring(index + toRemove.length());1083 }1084 private int indexOf(String string, CharSequence toFind) {1085 if (EMPTY_STRING.equals(string) && EMPTY_STRING.equals(toFind.toString())) return 0;1086 for (int i = 0; i < string.length(); i++) {1087 if (comparisonStrategy.stringStartsWith(string.substring(i), toFind.toString())) return i;1088 }1089 // should not arrive here since we this method is used from assertContainsSubsequence at a step where we know that toFind1090 // was found and we are checking whether it was at the right place/order.1091 throw new IllegalStateException(format("%s should have been found in %s, please raise an assertj-core issue", toFind,1092 string));1093 }1094 public void assertXmlEqualsTo(AssertionInfo info, CharSequence actualXml, CharSequence expectedXml) {1095 // check that actual and expected XML CharSequence are not null.1096 // we consider that null values don't make much sense when you want to compare XML document as String/CharSequence.1097 checkCharSequenceIsNotNull(expectedXml);1098 assertNotNull(info, actualXml);1099 // we only use default comparison strategy, it does not make sense to use a specific comparison strategy1100 final String formattedActualXml = xmlPrettyFormat(actualXml.toString());1101 final String formattedExpectedXml = xmlPrettyFormat(expectedXml.toString());1102 if (!comparisonStrategy.areEqual(formattedActualXml, formattedExpectedXml))1103 throw failures.failure(info, shouldBeEqual(formattedActualXml, formattedExpectedXml, comparisonStrategy,1104 info.representation()));1105 }1106 public void assertIsSubstringOf(AssertionInfo info, CharSequence actual, CharSequence sequence) {1107 assertNotNull(info, actual);1108 requireNonNull(sequence, "Expecting CharSequence not to be null");1109 if (stringContains(sequence.toString(), actual.toString())) return;1110 throw failures.failure(info, shouldBeSubstring(actual, sequence, comparisonStrategy));1111 }1112 /**1113 * Verifies that the given {@code CharSequence} contains the given regular expression.1114 *1115 * @param info contains information about the assertion.1116 * @param actual the given {@code CharSequence}.1117 * @param regex the regular expression to find in the actual {@code CharSequence}.1118 * @throws NullPointerException if the given pattern is {@code null}.1119 * @throws PatternSyntaxException if the regular expression's syntax is invalid.1120 * @throws AssertionError if the given {@code CharSequence} is {@code null}.1121 * @throws AssertionError if the actual {@code CharSequence} does not contain the given regular expression.1122 */1123 public void assertContainsPattern(AssertionInfo info, CharSequence actual, CharSequence regex) {1124 checkRegexIsNotNull(regex);1125 assertNotNull(info, actual);1126 Pattern pattern = Pattern.compile(regex.toString());1127 Matcher matcher = pattern.matcher(actual);1128 if (!matcher.find()) throw failures.failure(info, shouldContainPattern(actual, pattern.pattern()));1129 }1130 /**1131 * Verifies that the given {@code CharSequence} contains the given regular expression.1132 *1133 * @param info contains information about the assertion.1134 * @param actual the given {@code CharSequence}.1135 * @param pattern the regular expression to find in the actual {@code CharSequence}.1136 * @throws NullPointerException if the given pattern is {@code null}.1137 * @throws AssertionError if the given {@code CharSequence} is {@code null}.1138 * @throws AssertionError if the given {@code CharSequence} does not contain the given regular expression.1139 */1140 public void assertContainsPattern(AssertionInfo info, CharSequence actual, Pattern pattern) {1141 checkIsNotNull(pattern);1142 assertNotNull(info, actual);1143 Matcher matcher = pattern.matcher(actual);1144 if (!matcher.find()) throw failures.failure(info, shouldContainPattern(actual, pattern.pattern()));1145 }1146 /**1147 * Verifies that the given {@code CharSequence} does not contain the given regular expression.1148 *1149 * @param info contains information about the assertion.1150 * @param actual the given {@code CharSequence}.1151 * @param regex the regular expression to find in the actual {@code CharSequence}.1152 * @throws NullPointerException if the given pattern is {@code null}.1153 * @throws PatternSyntaxException if the regular expression's syntax is invalid.1154 * @throws AssertionError if the given {@code CharSequence} is {@code null}.1155 * @throws AssertionError if the actual {@code CharSequence} contains the given regular expression.1156 */1157 public void assertDoesNotContainPattern(AssertionInfo info, CharSequence actual, CharSequence regex) {1158 checkRegexIsNotNull(regex);1159 Pattern pattern = Pattern.compile(regex.toString());1160 assertDoesNotContainPattern(info, actual, pattern);1161 }1162 /**1163 * Verifies that the given {@code CharSequence} does not contain the given regular expression.1164 *1165 * @param info contains information about the assertion.1166 * @param actual the given {@code CharSequence}.1167 * @param pattern the regular expression to find in the actual {@code CharSequence}.1168 * @throws NullPointerException if the given pattern is {@code null}.1169 * @throws AssertionError if the given {@code CharSequence} is {@code null}.1170 * @throws AssertionError if the given {@code CharSequence} contains the given regular expression.1171 */1172 public void assertDoesNotContainPattern(AssertionInfo info, CharSequence actual, Pattern pattern) {1173 checkIsNotNull(pattern);1174 assertNotNull(info, actual);1175 Matcher matcher = pattern.matcher(actual);1176 if (matcher.find()) throw failures.failure(info, shouldNotContainPattern(actual, pattern.pattern()));1177 }1178 private void checkCharSequenceArrayDoesNotHaveNullElements(CharSequence[] values) {1179 if (values.length == 1) {1180 checkCharSequenceIsNotNull(values[0]);1181 } else {1182 for (int i = 0; i < values.length; i++) {1183 requireNonNull(values[i], "Expecting CharSequence elements not to be null but found one at index " + i);1184 }1185 }1186 }1187 /***1188 * Verifies that actual is equal to expected ignoring new lines1189 * @param info contains information about the assertion.1190 * @param actual the actual {@code CharSequence} (new lines will be ignored).1191 * @param expected the expected {@code CharSequence} (new lines will be ignored).1192 */1193 public void assertIsEqualToIgnoringNewLines(AssertionInfo info, CharSequence actual, CharSequence expected) {1194 String actualWithoutNewLines = removeNewLines(actual);1195 String expectedWithoutNewLines = removeNewLines(expected);1196 if (!actualWithoutNewLines.equals(expectedWithoutNewLines))1197 throw failures.failure(info, shouldBeEqualIgnoringNewLines(actual, expected), actual, expected);1198 }1199 public void assertLowerCase(AssertionInfo info, CharSequence actual) {1200 assertNotNull(info, actual);1201 if (!isLowerCase(actual)) throw failures.failure(info, shouldBeLowerCase(actual));1202 }1203 private boolean isLowerCase(CharSequence actual) {1204 return actual.equals(actual.toString().toLowerCase());1205 }1206 public void assertUpperCase(AssertionInfo info, CharSequence actual) {1207 assertNotNull(info, actual);1208 if (!isUpperCase(actual)) throw failures.failure(info, shouldBeUpperCase(actual));1209 }1210 private boolean isUpperCase(CharSequence actual) {1211 return actual.equals(actual.toString().toUpperCase());1212 }1213 public void assertMixedCase(AssertionInfo info, CharSequence actual) {1214 assertNotNull(info, actual);1215 if (isLowerCase(actual) != isUpperCase(actual)) throw failures.failure(info, shouldBeMixedCase(actual));1216 }1217 /***1218 * Verifies that actual is a valid Base64 encoded string.1219 *1220 * @param info contains information about the assertion.1221 * @param actual the actual {@code CharSequence} (new lines will be ignored).1222 * @throws AssertionError if {@code actual} is {@code null}.1223 * @throws AssertionError if {@code actual} is not a valid Base64 encoded string.1224 */1225 public void assertIsBase64(AssertionInfo info, String actual) {1226 assertNotNull(info, actual);1227 try {1228 Base64.getDecoder().decode(actual);1229 } catch (IllegalArgumentException e) {1230 throw failures.failure(info, shouldBeBase64(actual));1231 }1232 }1233 private static String removeNewLines(CharSequence text) {1234 String normalizedText = normalizeNewlines(text);1235 return normalizedText.replace("\n", EMPTY_STRING);1236 }1237 private void doCommonCheckForCharSequence(AssertionInfo info, CharSequence actual, CharSequence[] sequence) {1238 assertNotNull(info, actual);1239 checkIsNotNull(sequence);1240 checkIsNotEmpty(sequence);1241 checkCharSequenceArrayDoesNotHaveNullElements(sequence);1242 }1243}...
doCommonCheckForCharSequence
Using AI Code Generation
1public class Strings_assertContainsOnlyOnce_Test extends StringsBaseTest {2 public void should_fail_if_actual_does_not_contain_given_values_only() {3 String actual = "Yoda";4 String[] values = { "Luke", "Yoda", "Leia" };5 AssertionError assertionError = expectAssertionError(() -> strings.assertContainsOnlyOnce(someInfo(), actual, values));6 then(assertionError).hasMessage(shouldContainOnlyOnce(actual, values, newLinkedHashSet("Luke", "Leia"), newLinkedHashSet("Luke", "Leia")).create());7 }8}9assertThat(actual).containsOnlyOnce("Luke", "Yoda", "Leia");10public void should_pass_if_actual_contains_given_values_only_according_to_custom_comparison_strategy() {11 stringsWithCaseInsensitiveComparisonStrategy.assertContainsOnlyOnce(someInfo(), "Yoda", "YOda");12}13public void should_pass_if_actual_contains_given_values_only_according_to_custom_comparison_strategy() {14 stringsWithCaseInsensitiveComparisonStrategy.assertContainsOnlyOnce(someInfo(), "Yoda", "YOda");15}16public void should_fail_if_actual_does_not_contain_given_values_only_according_to_custom_comparison_strategy() {17 AssertionInfo info = someInfo();18 String[] expected = { "Yoda", "Luke", "Leia" };19 Throwable error = catchThrowable(() -> stringsWithCaseInsensitiveComparisonStrategy.assertContainsOnlyOnce(someInfo(), "Yoda", expected));20 assertThat(error).isInstanceOf(AssertionError.class);21 verify(failures).failure(info, shouldContainOnlyOnce("Yoda", expected, newLinkedHashSet("Luke", "Leia"), newLinkedHashSet("Luke", "Leia"), comparisonStrategy));22}23public void should_pass_if_actual_contains_given_values_only_according_to_custom_comparison_strategy() {24 stringsWithCaseInsensitiveComparisonStrategy.assertContainsOnlyOnce(someInfo(), "Yoda", "YOda");25}26public void should_pass_if_actual_contains_given_values_only_according_to_custom_comparison_strategy() {27 stringsWithCaseInsensitiveComparisonStrategy.assertContainsOnlyOnce(someInfo(), "Yoda", "YOda");28}29public void should_fail_if_actual_does_not_contain_given_values_only_according_to_custom_comparison_strategy() {30 AssertionInfo info = someInfo();
doCommonCheckForCharSequence
Using AI Code Generation
1[INFO] [org.sonar.java.checks.verifier.TestUtils] : 2: class A {2[INFO] [org.sonar.java.checks.verifier.TestUtils] : 3: void foo() {3[INFO] [org.sonar.java.checks.verifier.TestUtils] : 4: String s = "abc";4[INFO] [org.sonar.java.checks.verifier.TestUtils] : 5: if (s.equals("abc")) {5[INFO] [org.sonar.java.checks.verifier.TestUtils] : 6: System.out.println("abc");6[INFO] [org.sonar.java.checks.verifier.TestUtils] : 7: if ("abc".equals(s)) {7[INFO] [org.sonar.java.checks.verifier.TestUtils] : 8: System.out.println("abc");8[INFO] [org.sonar.java.checks.verifier.TestUtils] : 9: if (s == "abc") {9[INFO] [org.sonar.java.checks.verifier.TestUtils] : 10: System.out.println("abc");10[INFO] [org.sonar.java.checks.verifier.TestUtils] : 11: }11[INFO] [org.sonar.java.checks.verifier.TestUtils] : 12: }12[INFO] [org.sonar.java.checks.verifier.TestUtils] : 13: }13[INFO] [org.sonar.java.checks.verifier.TestUtils] : 14: }14[INFO] [org.sonar.java.checks.verifier.TestUtils] : 15: }15[INFO] [org.sonar.java.checks.verifier.TestUtils] : 19: class B {
doCommonCheckForCharSequence
Using AI Code Generation
1 assertThat(actual).isNotNull();2 strings.doCommonCheckForCharSequence(info, actual, expected, org.assertj.core.internal.ErrorMessages.shouldContainSequence, new Object[]{expected});3 if (!strings.containsSequence(actual, expected)) {4 throw failures.failure(info, org.assertj.core.internal.ErrorMessages.shouldContainSequence, actual, expected);5 }6 return myself;7}
doCommonCheckForCharSequence
Using AI Code Generation
1assertThat("abc").doCommonCheckForCharSequence(2 new BasicErrorMessageFactory("my error message"),3);4assertThat("abc").doCommonCheckForCharSequence(5 new BasicErrorMessageFactory("my error message"),6);7assertThat("abc").doCommonCheckForCharSequence(8 new BasicErrorMessageFactory("my error message"),9);10assertThat("abc").doCommonCheckForCharSequence(11 new BasicErrorMessageFactory("my error message"),12);13assertThat("abc").doCommonCheckForCharSequence(
doCommonCheckForCharSequence
Using AI Code Generation
1assertThatCode(() -> strings.doCommonCheckForCharSequence(actual, check))2 .isInstanceOf(AssertionError.class)3 .hasMessageContaining("Expecting actual not to be null or empty");4}5As you can see, there is no need to use a try-catch block. The assertThatCode() method wraps6> {code}7> assertThatExceptionOfType(MyException.class)8> .isThrownBy(() -> { throw new MyException(new RuntimeException("boom!")); })9> .withCauseInstanceOf(RuntimeException.class)10> .withCauseMessage("boom!");11> {code}12> {code}13> assertThatExceptionOfType(MyException.class)14> .isThrownBy(() -> { throw new MyException(new RuntimeException("boom!")); })15> .withMessage("boom!");16> {code}17> The implementation would be similar to the one of {{AbstractThrowableAssert#withMessage(String)}}
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!