Class Strings

java.lang.Object
org.assertj.core.internal.Strings

public class Strings extends Object
Reusable assertions for CharSequences.
  • Field Details

  • Constructor Details

  • Method Details

    • instance

      public static Strings instance()
      Returns the singleton instance of this class based on StandardComparisonStrategy.
      Returns:
      the singleton instance of this class based on StandardComparisonStrategy.
    • getComparator

      public Comparator<?> getComparator()
    • assertNullOrEmpty

      public void assertNullOrEmpty(AssertionInfo info, CharSequence actual)
      Asserts that the given CharSequence is null or empty.
      Parameters:
      info - contains information about the assertion.
      actual - the given CharSequence.
      Throws:
      AssertionError - if the given CharSequence is not null *and* it is not empty.
    • assertEmpty

      public void assertEmpty(AssertionInfo info, CharSequence actual)
      Asserts that the given CharSequence is empty.
      Parameters:
      info - contains information about the assertion.
      actual - the given CharSequence.
      Throws:
      AssertionError - if the given CharSequence is null.
      AssertionError - if the given CharSequence is not empty.
    • assertNotEmpty

      public void assertNotEmpty(AssertionInfo info, CharSequence actual)
      Asserts that the given CharSequence is not empty.
      Parameters:
      info - contains information about the assertion.
      actual - the given CharSequence.
      Throws:
      AssertionError - if the given CharSequence is null.
      AssertionError - if the given CharSequence is empty.
    • hasContent

      private static boolean hasContent(CharSequence s)
    • assertBlank

      public void assertBlank(AssertionInfo info, CharSequence actual)
      Asserts that the given CharSequence is Null, empty or contains only whitespace.
      Parameters:
      info - contains information about the assertion.
      actual - the given CharSequence.
      Throws:
      AssertionError - if the given CharSequence is not blank.
    • assertNotBlank

      public void assertNotBlank(AssertionInfo info, CharSequence actual)
      Asserts that the given CharSequence is not Null, not empty and contains not only whitespace.
      Parameters:
      info - contains information about the assertion.
      actual - the given CharSequence.
      Throws:
      AssertionError - if the given CharSequence is blank.
    • isBlank

      private boolean isBlank(CharSequence actual)
    • assertJavaBlank

      public void assertJavaBlank(AssertionInfo info, CharSequence actual)
      Asserts that the given CharSequence is Null, empty or contains only whitespace according to Character.isWhitespace(char).
      Parameters:
      info - contains information about the assertion.
      actual - the given CharSequence.
      Throws:
      AssertionError - if the given CharSequence is not blank.
    • assertNotJavaBlank

      public void assertNotJavaBlank(AssertionInfo info, CharSequence actual)
      Asserts that the given CharSequence is not Null, not empty or contains not only whitespace according to Character.isWhitespace(char).
      Parameters:
      info - contains information about the assertion.
      actual - the given CharSequence.
      Throws:
      AssertionError - if the given CharSequence is blank.
    • isJavaBlank

      private boolean isJavaBlank(CharSequence actual)
    • assertHasSize

      public void assertHasSize(AssertionInfo info, CharSequence actual, int expectedSize)
      Asserts that the size of the given CharSequence is equal to the expected one.
      Parameters:
      info - contains information about the assertion.
      actual - the given CharSequence.
      expectedSize - the expected size of actual.
      Throws:
      AssertionError - if the given CharSequence is null.
      AssertionError - if the size of the given CharSequence is different than the expected one.
    • assertHasLineCount

      public void assertHasLineCount(AssertionInfo info, CharSequence actual, int expectedLineCount)
      Asserts that the line count of the given CharSequence is equal to the expected one.
      Parameters:
      info - contains information about the assertion.
      actual - the given CharSequence.
      expectedLineCount - the expected line count of actual.
      Throws:
      AssertionError - if the given CharSequence is null.
      AssertionError - if the line count of the given CharSequence is different than the expected one.
    • assertHasSameSizeAs

      public void assertHasSameSizeAs(AssertionInfo info, CharSequence actual, Iterable<?> other)
      Asserts that the number of entries in the given CharSequence has the same size as the other Iterable.
      Parameters:
      info - contains information about the assertion.
      actual - the given CharSequence.
      other - the group to compare
      Throws:
      AssertionError - if the given CharSequence is null.
      AssertionError - if the given Iterable is null.
      AssertionError - if the number of entries in the given CharSequence does not have the same size.
    • assertHasSameSizeAs

      public void assertHasSameSizeAs(AssertionInfo info, CharSequence actual, Object array)
      Asserts that the number of entries in the given CharSequence has the same size as the other array.
      Parameters:
      info - contains information about the assertion.
      actual - the given CharSequence.
      array - the array to compare
      Throws:
      AssertionError - if the given CharSequence is null.
      AssertionError - if the given array is null.
      AssertionError - if the number of entries in the given CharSequence does not have the same size.
    • assertHasSameSizeAs

      public void assertHasSameSizeAs(AssertionInfo info, CharSequence actual, CharSequence other)
    • assertContains

      public void assertContains(AssertionInfo info, CharSequence actual, CharSequence... values)
      Verifies that the given CharSequence contains the given strings.
      Parameters:
      info - contains information about the assertion.
      actual - the actual CharSequence.
      values - the values to look for.
      Throws:
      NullPointerException - if the given sequence is null.
      IllegalArgumentException - if the given values is empty.
      AssertionError - if the given CharSequence is null.
      AssertionError - if the actual CharSequence does not contain the given sequence.
    • assertContainsOnlyDigits

      public void assertContainsOnlyDigits(AssertionInfo info, CharSequence actual)
      Verifies that the given CharSequence contains only digits.
      Parameters:
      info - contains information about the assertion.
      actual - the given CharSequence.
      Throws:
      NullPointerException - if actual is null.
      AssertionError - if actual contains non-digit characters or contains no digits at all.
    • checkIsNotNull

      private void checkIsNotNull(CharSequence... values)
    • checkIsNotEmpty

      private void checkIsNotEmpty(CharSequence... values)
    • stringContains

      private boolean stringContains(CharSequence actual, CharSequence sequence)
    • assertContainsIgnoringCase

      public void assertContainsIgnoringCase(AssertionInfo info, CharSequence actual, CharSequence sequence)
      Verifies that the given CharSequence contains the given sequence, ignoring case considerations.
      Parameters:
      info - contains information about the assertion.
      actual - the actual CharSequence.
      sequence - the sequence to search for.
      Throws:
      NullPointerException - if the given sequence is null.
      AssertionError - if the given CharSequence is null.
      AssertionError - if the actual CharSequence does not contain the given sequence.
    • assertDoesNotContain

      public void assertDoesNotContain(AssertionInfo info, CharSequence actual, CharSequence sequence)
      Verifies that the given CharSequence does not contain the given sequence.
      Parameters:
      info - contains information about the assertion.
      actual - the actual CharSequence.
      sequence - the sequence to search for.
      Throws:
      NullPointerException - if the given sequence is null.
      AssertionError - if the given CharSequence is null.
      AssertionError - if the actual CharSequence contains the given sequence.
    • checkCharSequenceIsNotNull

      private void checkCharSequenceIsNotNull(CharSequence sequence)
    • assertEqualsIgnoringCase

      public void assertEqualsIgnoringCase(AssertionInfo info, CharSequence actual, CharSequence expected)
      Verifies that two CharSequences are equal, ignoring case considerations.
      Parameters:
      info - contains information about the assertion.
      actual - the actual CharSequence.
      expected - the expected CharSequence.
      Throws:
      AssertionError - if the given CharSequences are not equal.
    • assertNotEqualsIgnoringCase

      public void assertNotEqualsIgnoringCase(AssertionInfo info, CharSequence actual, CharSequence expected)
      Verifies that two CharSequences are not equal, ignoring case considerations.
      Parameters:
      info - contains information about the assertion.
      actual - the actual CharSequence.
      expected - the expected CharSequence.
      Throws:
      AssertionError - if the given CharSequences are equal ignoring case considerations.
    • areEqualIgnoringCase

      private boolean areEqualIgnoringCase(CharSequence actual, CharSequence expected)
    • assertIsEqualToNormalizingNewlines

      public void assertIsEqualToNormalizingNewlines(AssertionInfo info, CharSequence actual, CharSequence expected)
      Verifies that two CharSequences are not equal, normalizing newlines.
      Parameters:
      info - contains information about the assertion.
      actual - the actual CharSequence (newlines will be normalized).
      expected - the expected CharSequence (newlines will be normalized)..
      Throws:
      AssertionError - if the given CharSequences are equal after normalizing newlines.
    • normalizeNewlines

      private static String normalizeNewlines(CharSequence actual)
    • assertEqualsIgnoringWhitespace

      public void assertEqualsIgnoringWhitespace(AssertionInfo info, CharSequence actual, CharSequence expected)
      Verifies that two CharSequences are equal, ignoring any differences in whitespace.
      Parameters:
      info - contains information about the assertion.
      actual - the actual CharSequence.
      expected - the expected CharSequence.
      Throws:
      AssertionError - if the given CharSequences are not equal.
    • assertNotEqualsIgnoringWhitespace

      public void assertNotEqualsIgnoringWhitespace(AssertionInfo info, CharSequence actual, CharSequence expected)
      Verifies that two CharSequences are not equal, ignoring any differences in whitespace.
      Parameters:
      info - contains information about the assertion.
      actual - the actual CharSequence.
      expected - the expected CharSequence.
      Throws:
      AssertionError - if the given CharSequences are equal.
    • areEqualIgnoringWhitespace

      private boolean areEqualIgnoringWhitespace(CharSequence actual, CharSequence expected)
    • removeAllWhitespaces

      private String removeAllWhitespaces(CharSequence toBeStripped)
    • assertEqualsNormalizingWhitespace

      public void assertEqualsNormalizingWhitespace(AssertionInfo info, CharSequence actual, CharSequence expected)
      Verifies that two CharSequences are equal, after the whitespace of both strings has been normalized.
      Parameters:
      info - contains information about the assertion.
      actual - the actual CharSequence.
      expected - the expected CharSequence.
      Throws:
      AssertionError - if the given CharSequences are not equal.
      Since:
      2.8.0 / 3.8.0
    • assertNotEqualsNormalizingWhitespace

      public void assertNotEqualsNormalizingWhitespace(AssertionInfo info, CharSequence actual, CharSequence expected)
      Verifies that two CharSequences are not equal, after the whitespace of both strings has been normalized.
      Parameters:
      info - contains information about the assertion.
      actual - the actual CharSequence.
      expected - the expected CharSequence.
      Throws:
      AssertionError - if the given CharSequences are equal.
      Since:
      2.8.0 / 3.8.0
    • areEqualNormalizingWhitespace

      private boolean areEqualNormalizingWhitespace(CharSequence actual, CharSequence expected)
    • normalizeWhitespace

      private String normalizeWhitespace(CharSequence toNormalize)
    • assertContainsOnlyOnce

      public void assertContainsOnlyOnce(AssertionInfo info, CharSequence actual, CharSequence sequence)
      Verifies that actual CharSequences contains only once the given sequence.
      Parameters:
      info - contains information about the assertion.
      actual - the actual CharSequence.
      sequence - the given CharSequence.
      Throws:
      NullPointerException - if the given sequence is null.
      AssertionError - if the given CharSequence is null.
      AssertionError - if the actual CharSequence does not contains only once the given CharSequence.
    • countOccurrences

      private int countOccurrences(CharSequence sequenceToSearch, CharSequence actual)
      Count occurrences of sequenceToSearch in actual CharSequence.
      Parameters:
      sequenceToSearch - the sequence to search in in actual CharSequence.
      actual - the CharSequence to search occurrences in.
      Returns:
      the number of occurrences of sequenceToSearch in actual CharSequence.
    • assertStartsWith

      public void assertStartsWith(AssertionInfo info, CharSequence actual, CharSequence prefix)
      Verifies that the given CharSequence starts with the given prefix.
      Parameters:
      info - contains information about the assertion.
      actual - the actual CharSequence.
      prefix - the given prefix.
      Throws:
      NullPointerException - if the given sequence is null.
      AssertionError - if the given CharSequence is null.
      AssertionError - if the actual CharSequence does not start with the given prefix.
    • assertDoesNotStartWith

      public void assertDoesNotStartWith(AssertionInfo info, CharSequence actual, CharSequence prefix)
      Verifies that the given CharSequence does not start with the given prefix.
      Parameters:
      info - contains information about the assertion.
      actual - the actual CharSequence.
      prefix - the given prefix.
      Throws:
      NullPointerException - if the given sequence is null.
      AssertionError - if the given CharSequence is null.
      AssertionError - if the actual CharSequence starts with the given prefix.
    • failIfPrefixIsNull

      private static void failIfPrefixIsNull(CharSequence prefix)
    • assertEndsWith

      public void assertEndsWith(AssertionInfo info, CharSequence actual, CharSequence suffix)
      Verifies that the given CharSequence ends with the given suffix.
      Parameters:
      info - contains information about the assertion.
      actual - the actual CharSequence.
      suffix - the given suffix.
      Throws:
      NullPointerException - if the given sequence is null.
      AssertionError - if the given CharSequence is null.
      AssertionError - if the actual CharSequence does not end with the given suffix.
    • assertDoesNotEndWith

      public void assertDoesNotEndWith(AssertionInfo info, CharSequence actual, CharSequence suffix)
      Verifies that the given CharSequence does not end with the given suffix.
      Parameters:
      info - contains information about the assertion.
      actual - the actual CharSequence.
      suffix - the given suffix.
      Throws:
      NullPointerException - if the given sequence is null.
      AssertionError - if the given CharSequence is null.
      AssertionError - if the actual CharSequence ends with the given suffix.
    • failIfSuffixIsNull

      private static void failIfSuffixIsNull(CharSequence suffix)
    • assertMatches

      public void assertMatches(AssertionInfo info, CharSequence actual, CharSequence regex)
      Verifies that the given CharSequence matches the given regular expression.
      Parameters:
      info - contains information about the assertion.
      actual - the given CharSequence.
      regex - the regular expression to which the actual CharSequence is to be matched.
      Throws:
      NullPointerException - if the given pattern is null.
      PatternSyntaxException - if the regular expression's syntax is invalid.
      AssertionError - if the given CharSequence is null.
      AssertionError - if the actual CharSequence does not match the given regular expression.
    • assertDoesNotMatch

      public void assertDoesNotMatch(AssertionInfo info, CharSequence actual, CharSequence regex)
      Verifies that the given CharSequence does not match the given regular expression.
      Parameters:
      info - contains information about the assertion.
      actual - the given CharSequence.
      regex - the regular expression to which the actual CharSequence is to be matched.
      Throws:
      NullPointerException - if the given pattern is null.
      PatternSyntaxException - if the regular expression's syntax is invalid.
      AssertionError - if the given CharSequence is null.
      AssertionError - if the actual CharSequence matches the given regular expression.
    • checkRegexIsNotNull

      private void checkRegexIsNotNull(CharSequence regex)
    • assertMatches

      public void assertMatches(AssertionInfo info, CharSequence actual, Pattern pattern)
      Verifies that the given CharSequence matches the given regular expression.
      Parameters:
      info - contains information about the assertion.
      actual - the given CharSequence.
      pattern - the regular expression to which the actual CharSequence is to be matched.
      Throws:
      NullPointerException - if the given pattern is null.
      AssertionError - if the given CharSequence is null.
      AssertionError - if the given CharSequence does not match the given regular expression.
    • assertDoesNotMatch

      public void assertDoesNotMatch(AssertionInfo info, CharSequence actual, Pattern pattern)
      Verifies that the given CharSequence does not match the given regular expression.
      Parameters:
      info - contains information about the assertion.
      actual - the given CharSequence.
      pattern - the regular expression to which the actual CharSequence is to be matched.
      Throws:
      NullPointerException - if the given pattern is null.
      AssertionError - if the given CharSequence matches the given regular expression.
    • checkIsNotNull

      private void checkIsNotNull(Pattern pattern)
    • patternToMatchIsNull

      private NullPointerException patternToMatchIsNull()
    • assertNotNull

      private void assertNotNull(AssertionInfo info, CharSequence actual)
    • assertContainsSequence

      public void assertContainsSequence(AssertionInfo info, CharSequence actual, CharSequence[] sequence)
    • indexOf

      private int indexOf(String string, String toFind)
    • assertXmlEqualsTo

      public void assertXmlEqualsTo(AssertionInfo info, CharSequence actualXml, CharSequence expectedXml)
    • assertIsSubstringOf

      public void assertIsSubstringOf(AssertionInfo info, CharSequence actual, CharSequence sequence)
    • assertContainsPattern

      public void assertContainsPattern(AssertionInfo info, CharSequence actual, CharSequence regex)
      Verifies that the given CharSequence contains the given regular expression.
      Parameters:
      info - contains information about the assertion.
      actual - the given CharSequence.
      regex - the regular expression to find in the actual CharSequence.
      Throws:
      NullPointerException - if the given pattern is null.
      PatternSyntaxException - if the regular expression's syntax is invalid.
      AssertionError - if the given CharSequence is null.
      AssertionError - if the actual CharSequence does not contain the given regular expression.
    • assertContainsPattern

      public void assertContainsPattern(AssertionInfo info, CharSequence actual, Pattern pattern)
      Verifies that the given CharSequence contains the given regular expression.
      Parameters:
      info - contains information about the assertion.
      actual - the given CharSequence.
      pattern - the regular expression to find in the actual CharSequence.
      Throws:
      NullPointerException - if the given pattern is null.
      AssertionError - if the given CharSequence is null.
      AssertionError - if the given CharSequence does not contain the given regular expression.
    • assertDoesNotContainPattern

      public void assertDoesNotContainPattern(AssertionInfo info, CharSequence actual, CharSequence regex)
      Verifies that the given CharSequence does not contain the given regular expression.
      Parameters:
      info - contains information about the assertion.
      actual - the given CharSequence.
      regex - the regular expression to find in the actual CharSequence.
      Throws:
      NullPointerException - if the given pattern is null.
      PatternSyntaxException - if the regular expression's syntax is invalid.
      AssertionError - if the given CharSequence is null.
      AssertionError - if the actual CharSequence contains the given regular expression.
    • assertDoesNotContainPattern

      public void assertDoesNotContainPattern(AssertionInfo info, CharSequence actual, Pattern pattern)
      Verifies that the given CharSequence does not contain the given regular expression.
      Parameters:
      info - contains information about the assertion.
      actual - the given CharSequence.
      pattern - the regular expression to find in the actual CharSequence.
      Throws:
      NullPointerException - if the given pattern is null.
      AssertionError - if the given CharSequence is null.
      AssertionError - if the given CharSequence contains the given regular expression.
    • checkCharSequenceArrayDoesNotHaveNullElements

      private void checkCharSequenceArrayDoesNotHaveNullElements(CharSequence[] values)