org.assertj.core.api
Class AbstractCharSequenceAssert<S extends AbstractCharSequenceAssert<S,A>,A extends CharSequence>

java.lang.Object
  extended by org.assertj.core.api.AbstractAssert<S,A>
      extended by org.assertj.core.api.AbstractCharSequenceAssert<S,A>
Type Parameters:
S - the "self" type of this assertion class. Please read "Emulating 'self types' using Java Generics to simplify fluent API implementation" for more details.
A - the type of the "actual" value.
All Implemented Interfaces:
Assert<S,A>, Descriptable<S>, EnumerableAssert<S,Character>, ExtensionPoints<S,A>
Direct Known Subclasses:
CharSequenceAssert, StringAssert

public abstract class AbstractCharSequenceAssert<S extends AbstractCharSequenceAssert<S,A>,A extends CharSequence>
extends AbstractAssert<S,A>
implements EnumerableAssert<S,Character>

Base class for all implementations of assertions for CharSequences.

Author:
Yvonne Wang, David DIDIER, Alex Ruiz, Joel Costigliola, Mikhail Mazursky, Nicolas Francois

Field Summary
 
Fields inherited from class org.assertj.core.api.AbstractAssert
actual, info, myself
 
Constructor Summary
protected AbstractCharSequenceAssert(A actual, Class<?> selfType)
           
 
Method Summary
 S contains(CharSequence... values)
          Verifies that the actual CharSequence contains all the given strings.
 S containsIgnoringCase(CharSequence sequence)
          Verifies that the actual CharSequence contains the given sequence, ignoring case considerations.
 S containsOnlyOnce(CharSequence sequence)
          Verifies that the actual CharSequence contains the given sequence only once.
 S containsSequence(CharSequence... values)
          Verifies that the actual CharSequence contains all the given strings in the given order.
 S doesNotContain(CharSequence sequence)
          Verifies that the actual CharSequence does not contain the given sequence.
 S doesNotMatch(CharSequence regex)
          Verifies that the actual CharSequence does not match the given regular expression.
 S doesNotMatch(Pattern pattern)
          Verifies that the actual CharSequence does not match the given regular expression pattern.
 S endsWith(CharSequence suffix)
          Verifies that the actual CharSequence ends with the given suffix.
 S hasLineCount(int expectedLineCount)
          Verifies that the actual CharSequence has the expected line count.
 S hasSameSizeAs(CharSequence other)
          Verifies that the actual CharSequence has a length that's the same as the length of the given CharSequence.
 S hasSameSizeAs(Iterable<?> other)
          Verifies that the actual CharSequence has a length that's the same as the number of elements in the given Iterable.
 S hasSameSizeAs(Object other)
          Verifies that the actual CharSequence has a length that's the same as the number of elements in the given array.
 S hasSize(int expected)
          Verifies that the actual CharSequence has the expected length using the length() method.
 S inHexadecimal()
          Use hexadecimal object representation instead of standard representation in error messages.
 S inUnicode()
          Use unicode character representation instead of standard representation in error messages.
 void isEmpty()
          Verifies that the actual CharSequence is empty, i.e., it has a length of 0 and is not null.
 S isEqualToIgnoringCase(CharSequence expected)
          Verifies that the actual CharSequence is equal to the given one, ignoring case considerations.
 S isNotEmpty()
          Verifies that the actual CharSequence is not empty, i.e., is not null and has a length of 1 or more.
 void isNullOrEmpty()
          Verifies that the actual CharSequence is empty, i.e., it has a length of 0, or is null.
 S isXmlEqualTo(CharSequence expectedXml)
          Verifies that the actual CharSequence is equal to the given XML CharSequence after both have been formatted the same way.
 S isXmlEqualToContentOf(File xmlFile)
          Verifies that the actual CharSequence is equal to the content of the given file.
 S matches(CharSequence regex)
          Verifies that the actual CharSequence matches the given regular expression.
 S matches(Pattern pattern)
          Verifies that the actual CharSequence matches the given regular expression pattern.
 S startsWith(CharSequence prefix)
          Verifies that the actual CharSequence starts with the given prefix.
 S usingComparator(Comparator<? super A> customComparator)
          Use given custom comparator instead of relying on actual type A equals method for incoming assertion checks.
 S usingDefaultComparator()
          Revert to standard comparison for incoming assertion checks.
 S usingDefaultElementComparator()
          Deprecated. Custom element Comparator is not supported for CharSequence comparison.
 S usingElementComparator(Comparator<? super Character> customComparator)
          Deprecated. Custom element Comparator is not supported for CharSequence comparison.
 
Methods inherited from class org.assertj.core.api.AbstractAssert
as, as, describedAs, describedAs, descriptionText, doesNotHave, doesNotHaveSameClassAs, equals, failWithMessage, getWritableAssertionInfo, has, hashCode, hasSameClassAs, inBinary, is, isEqualTo, isExactlyInstanceOf, isIn, isIn, isInstanceOf, isInstanceOfAny, isNot, isNotEqualTo, isNotExactlyInstanceOf, isNotIn, isNotIn, isNotInstanceOf, isNotInstanceOfAny, isNotNull, isNotOfAnyClassIn, isNotSameAs, isNull, isOfAnyClassIn, isSameAs, overridingErrorMessage
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractCharSequenceAssert

protected AbstractCharSequenceAssert(A actual,
                                     Class<?> selfType)
Method Detail

isNullOrEmpty

public void isNullOrEmpty()
Verifies that the actual CharSequence is empty, i.e., it has a length of 0, or is null.

If you do not want to accept a null value, use isEmpty() instead.

Both of these assertions will succeed:

 String emptyString = ""
 assertThat(emptyString).isNullOrEmpty();
 
 String nullString = null;
 assertThat(nullString).isNullOrEmpty();
 

Specified by:
isNullOrEmpty in interface EnumerableAssert<S extends AbstractCharSequenceAssert<S,A>,Character>
Throws:
AssertionError - if the actual CharSequence has a non-zero length.

isEmpty

public void isEmpty()
Verifies that the actual CharSequence is empty, i.e., it has a length of 0 and is not null.

If you want to accept a null value as well as a 0 length, use isNullOrEmpty() instead.

This assertion will succeed:

 String emptyString = ""
 assertThat(emptyString).isEmpty();
 
Whereas this assertion will fail:
 String nullString = null;
 assertThat(nullString).isEmpty();
 

Specified by:
isEmpty in interface EnumerableAssert<S extends AbstractCharSequenceAssert<S,A>,Character>
Throws:
AssertionError - if the actual CharSequence has a non-zero length or is null.

isNotEmpty

public S isNotEmpty()
Verifies that the actual CharSequence is not empty, i.e., is not null and has a length of 1 or more.

This assertion will succeed:

 String bookName = "A Game of Thrones"
 assertThat(bookName).isNotEmpty();
 
Whereas this assertion will fail:
 String emptyString = ""
 assertThat(emptyString).isNotEmpty();
 

Specified by:
isNotEmpty in interface EnumerableAssert<S extends AbstractCharSequenceAssert<S,A>,Character>
Returns:
this assertion object.
Throws:
AssertionError - if the actual CharSequence is empty (has a length of 0).

hasSize

public S hasSize(int expected)
Verifies that the actual CharSequence has the expected length using the length() method.

This assertion will succeed:

 String bookName = "A Game of Thrones"
 assertThat(bookName).hasSize(17);
 
Whereas this assertion will fail:
 String bookName = "A Clash of Kings"
 assertThat(bookName).hasSize(4);
 

Specified by:
hasSize in interface EnumerableAssert<S extends AbstractCharSequenceAssert<S,A>,Character>
Parameters:
expected - the expected length of the actual CharSequence.
Returns:
this assertion object.
Throws:
AssertionError - if the actual length is not equal to the expected length.

hasLineCount

public S hasLineCount(int expectedLineCount)
Verifies that the actual CharSequence has the expected line count.

A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed (see LineNumberReader).

This assertion will succeed:

 String multiLine = "First line\n" +
                    "Last line";
 assertThat(multiLine).hasLineCount(2);
 
Whereas this assertion will fail:
 String bookName = "A Clash of Kings";
 assertThat(bookName).hasLineCount(3);
 

Parameters:
expectedLineCount - the expected line count of the actual CharSequence.
Returns:
this assertion object.
Throws:
AssertionError - if the actual line count is not equal to the expected one.

hasSameSizeAs

public S hasSameSizeAs(CharSequence other)
Verifies that the actual CharSequence has a length that's the same as the length of the given CharSequence.

Examples :

 // assertion will pass
 assertThat("C-3PO").hasSameSizeAs("R2-D2");
 
 // assertion will fail as actual and expected sizes differ
 assertThat("C-3PO").hasSameSizeAs("B1 battle droid");
 

Parameters:
other - the given CharSequence to be used for size comparison.
Returns:
this assertion object.
Throws:
AssertionError - if the actual CharSequence has a length that's different from the length of the given CharSequence.
NullPointerException - if the given CharSequence is null.

hasSameSizeAs

public S hasSameSizeAs(Object other)
Verifies that the actual CharSequence has a length that's the same as the number of elements in the given array.

Specified by:
hasSameSizeAs in interface EnumerableAssert<S extends AbstractCharSequenceAssert<S,A>,Character>
Parameters:
other - the given array to be used for size comparison.
Returns:
this assertion object.
Throws:
AssertionError - if the actual CharSequence has a length that's different from the number of elements in the array.
NullPointerException - if the given array is null.

hasSameSizeAs

public S hasSameSizeAs(Iterable<?> other)
Verifies that the actual CharSequence has a length that's the same as the number of elements in the given Iterable.

Specified by:
hasSameSizeAs in interface EnumerableAssert<S extends AbstractCharSequenceAssert<S,A>,Character>
Parameters:
other - the given Iterable to be used for size comparison.
Returns:
this assertion object.
Throws:
AssertionError - if the actual CharSequence has a length that's different from the number of elements in the Iterable.
NullPointerException - if the given Iterable is null.

isEqualToIgnoringCase

public S isEqualToIgnoringCase(CharSequence expected)
Verifies that the actual CharSequence is equal to the given one, ignoring case considerations.

Example :

 // assertion will pass
 assertThat("Gandalf the grey").isEqualToIgnoringCase("GaNdAlF tHe GREY");
 assertThat("Gandalf the grey").isEqualToIgnoringCase("Gandalf the grey");
 
 // assertion will fail
 assertThat("Gandalf the grey").isEqualToIgnoringCase("Gandalf the white");
 

Parameters:
expected - the given CharSequence to compare the actual CharSequence to.
Returns:
this assertion object.
Throws:
AssertionError - if the actual CharSequence is not equal to the given one.

containsOnlyOnce

public S containsOnlyOnce(CharSequence sequence)
Verifies that the actual CharSequence contains the given sequence only once.

Example :

 // assertion will pass
 assertThat("Frodo").containsOnlyOnce("do");
 
 // assertion will fail
 assertThat("Frodo").containsOnlyOnce("o");
 

Parameters:
sequence - the sequence to search for.
Returns:
this assertion object.
Throws:
AssertionError - if the actual CharSequence either does not contain the given one at all, or contains it more than once.

contains

public S contains(CharSequence... values)
Verifies that the actual CharSequence contains all the given strings.

You can use one or several strings as in this example:

 assertThat("Gandalf the grey").contains("alf");
 assertThat("Gandalf the grey").contains("alf", "grey");
 

Parameters:
values - the Strings to look for.
Returns:
this assertion object.
Throws:
NullPointerException - if the given list of values is null.
IllegalArgumentException - if the list of given values is empty.
AssertionError - if the actual CharSequence is null.
AssertionError - if the actual CharSequence does not contain all the given strings.

containsSequence

public S containsSequence(CharSequence... values)
Verifies that the actual CharSequence contains all the given strings in the given order.

Example:

 String book = "{ 'title':'A Game of Thrones', 'author':'George Martin'}";
 
 // this assertion succeeds ...
 assertThat(book).containsSequence("{", "title", "A Game of Thrones", "}");
 
 // ... but this one fails as "author" must come after "A Game of Thrones"
 assertThat(book).containsSequence("{", "author", "A Game of Thrones", "}");
 

Parameters:
values - the Strings to look for, in order.
Returns:
this assertion object.
Throws:
NullPointerException - if the given values is null.
IllegalArgumentException - if the given values is empty.
AssertionError - if the actual CharSequence is null.
AssertionError - if the actual CharSequence does not contain all the given strings in the given order.

containsIgnoringCase

public S containsIgnoringCase(CharSequence sequence)
Verifies that the actual CharSequence contains the given sequence, ignoring case considerations.

Example :

 // assertion will pass
 assertThat("Gandalf the grey").containsIgnoringCase("gandalf");
 
 // assertion will fail
 assertThat("Gandalf the grey").containsIgnoringCase("white");
 

Parameters:
sequence - the sequence to search for.
Returns:
this assertion object.
Throws:
NullPointerException - if the given sequence is null.
AssertionError - if the actual CharSequence is null.
AssertionError - if the actual CharSequence does not contain the given one.

doesNotContain

public S doesNotContain(CharSequence sequence)
Verifies that the actual CharSequence does not contain the given sequence.

Example :

 // assertion will pass
 assertThat("Frodo").doesNotContain("fro");
 assertThat("Frodo").doesNotContain("gandalf");
 
 // assertion will fail
 assertThat("Frodo").doesNotContain("Fro");
 

Parameters:
sequence - the sequence to search for.
Returns:
this assertion object.
Throws:
NullPointerException - if the given sequence is null.
AssertionError - if the actual CharSequence is null.
AssertionError - if the actual CharSequence contains the given one.

startsWith

public S startsWith(CharSequence prefix)
Verifies that the actual CharSequence starts with the given prefix.

Example :

 // assertion will pass
 assertThat("Frodo").startsWith("Fro");
 assertThat("Gandalf the grey").startsWith("Gandalf");
 
 // assertion will fail
 assertThat("Frodo").startsWith("fro");
 assertThat("Gandalf the grey").startsWith("grey");
 

Parameters:
prefix - the given prefix.
Returns:
this assertion object.
Throws:
NullPointerException - if the given prefix is null.
AssertionError - if the actual CharSequence is null.
AssertionError - if the actual CharSequence does not start with the given prefix.

endsWith

public S endsWith(CharSequence suffix)
Verifies that the actual CharSequence ends with the given suffix.

Example :

 // assertion will pass
 assertThat("Frodo").endsWith("do");
 
 // assertion will fail
 assertThat("Frodo").endsWith("Fro");
 

Parameters:
suffix - the given suffix.
Returns:
this assertion object.
Throws:
NullPointerException - if the given suffix is null.
AssertionError - if the actual CharSequence is null.
AssertionError - if the actual CharSequence does not end with the given suffix.

matches

public S matches(CharSequence regex)
Verifies that the actual CharSequence matches the given regular expression.

Example :

 // assertion will pass
 assertThat("Frodo").matches("..o.o");
 
 // assertion will fail
 assertThat("Frodo").matches(".*d");
 

Parameters:
regex - the regular expression to which the actual CharSequence is to be matched.
Returns:
this assertion object.
Throws:
NullPointerException - if the given pattern is null.
PatternSyntaxException - if the regular expression's syntax is invalid.
AssertionError - if the actual CharSequence is null.
AssertionError - if the actual CharSequence does not match the given regular expression.

doesNotMatch

public S doesNotMatch(CharSequence regex)
Verifies that the actual CharSequence does not match the given regular expression.

Example :

 // assertion will pass
 assertThat("Frodo").doesNotMatch(".*d");
 
 // assertion will fail
 assertThat("Frodo").doesNotMatch("..o.o");
 

Parameters:
regex - the regular expression to which the actual CharSequence is to be matched.
Returns:
this assertion object.
Throws:
NullPointerException - if the given pattern is null.
PatternSyntaxException - if the regular expression's syntax is invalid.
AssertionError - if the actual CharSequence is null.
AssertionError - if the actual CharSequence matches the given regular expression.

matches

public S matches(Pattern pattern)
Verifies that the actual CharSequence matches the given regular expression pattern.

Example :

 // assertion will pass
 assertThat("Frodo").matches(Pattern.compile("..o.o"));
 
 // assertion will fail
 assertThat("Frodo").matches(Pattern.compile(".*d"));
 

Parameters:
pattern - the regular expression to which the actual CharSequence is to be matched.
Returns:
this assertion object.
Throws:
NullPointerException - if the given pattern is null.
AssertionError - if the actual CharSequence is null.
AssertionError - if the actual CharSequence does not match the given regular expression.

doesNotMatch

public S doesNotMatch(Pattern pattern)
Verifies that the actual CharSequence does not match the given regular expression pattern.

Example :

 // assertion will pass
 assertThat("Frodo").doesNotMatch(Pattern.compile(".*d"));
 
 // assertion will fail
 assertThat("Frodo").doesNotMatch(Pattern.compile("..o.o"));
 

Parameters:
pattern - the regular expression to which the actual CharSequence is to be matched.
Returns:
this assertion object.
Throws:
NullPointerException - if the given pattern is null.
AssertionError - if the actual CharSequence does not match the given regular expression.

isXmlEqualTo

public S isXmlEqualTo(CharSequence expectedXml)
Verifies that the actual CharSequence is equal to the given XML CharSequence after both have been formatted the same way.

Example :

 String expectedXml =
     "<rings>\n" +
         "  <bearer>\n" +
         "    <name>Frodo</name>\n" +
         "    <ring>\n" +
         "      <name>one ring</name>\n" +
         "      <createdBy>Sauron</createdBy>\n" +
         "    </ring>\n" +
         "  </bearer>\n" +
         "</rings>";
 
 // Whatever how formatted your xml string is, isXmlEqualTo assertion is able to compare it with another xml String.
 String oneLineXml = "<rings><bearer><name>Frodo</name><ring><name>one ring</name><createdBy>Sauron</createdBy></ring></bearer></rings>";
 assertThat(oneLineXml).isXmlEqualTo(expectedXml);
 
 String xmlWithNewLine =
     "<rings>\n" +
         "<bearer>   \n" +
         "  <name>Frodo</name>\n" +
         "  <ring>\n" +
         "    <name>one ring</name>\n" +
         "    <createdBy>Sauron</createdBy>\n" +
         "  </ring>\n" +
         "</bearer>\n" +
         "</rings>";
 assertThat(xmlWithNewLine).isXmlEqualTo(expectedXml);
 
 // You can compare it with oneLineXml
 assertThat(xmlWithNewLine).isXmlEqualTo(oneLineXml);
 
 // Tip : use isXmlEqualToContentOf assertion to compare your XML String with the content of an XML file :
 assertThat(oneLineXml).isXmlEqualToContentOf(new File("src/test/resources/formatted.xml"));
 

Parameters:
expectedXml - the XML CharSequence to which the actual CharSequence is to be compared to.
Returns:
this assertion object to chain other assertions.
Throws:
NullPointerException - if the given CharSequence is null.
AssertionError - if the actual CharSequence is null or is not the same XML as the given XML CharSequence.

isXmlEqualToContentOf

public S isXmlEqualToContentOf(File xmlFile)
Verifies that the actual CharSequence is equal to the content of the given file.

This is an handy shortcut that calls : isXmlEqualTo(contentOf(xmlFile))

Example :

 // You can easily compare your XML String to the content of an XML file, whatever how formatted thay are.
 String oneLineXml = "<rings><bearer><name>Frodo</name><ring><name>one ring</name><createdBy>Sauron</createdBy></ring></bearer></rings>";
 assertThat(oneLineXml).isXmlEqualToContentOf(new File("src/test/resources/formatted.xml"));
 

Parameters:
xmlFile - the file to read the expected XML String to compare with actual CharSequence
Returns:
this assertion object to chain other assertions.
Throws:
NullPointerException - if the given File is null.
AssertionError - if the actual CharSequence is null or is not the same XML as the content of given File.

usingElementComparator

@Deprecated
public final S usingElementComparator(Comparator<? super Character> customComparator)
Deprecated. Custom element Comparator is not supported for CharSequence comparison.

Do not use this method.

Specified by:
usingElementComparator in interface EnumerableAssert<S extends AbstractCharSequenceAssert<S,A>,Character>
Parameters:
customComparator - the comparator to use for incoming assertion checks.
Returns:
this assertion object.
Throws:
UnsupportedOperationException - if this method is called.

usingDefaultElementComparator

@Deprecated
public final S usingDefaultElementComparator()
Deprecated. Custom element Comparator is not supported for CharSequence comparison.

Do not use this method.

Specified by:
usingDefaultElementComparator in interface EnumerableAssert<S extends AbstractCharSequenceAssert<S,A>,Character>
Returns:
this assertion object.
Throws:
UnsupportedOperationException - if this method is called.

usingComparator

public S usingComparator(Comparator<? super A> customComparator)
Description copied from class: AbstractAssert
Use given custom comparator instead of relying on actual type A equals method for incoming assertion checks.

Custom comparator is bound to assertion instance, meaning that if a new assertion is created, it will use default comparison strategy.

Examples :

 // frodo and sam are instances of Character with Hobbit race (obviously :).
 // raceComparator implements Comparator<Character> 
 assertThat(frodo).usingComparator(raceComparator).isEqualTo(sam); 
 

Specified by:
usingComparator in interface Assert<S extends AbstractCharSequenceAssert<S,A>,A extends CharSequence>
Overrides:
usingComparator in class AbstractAssert<S extends AbstractCharSequenceAssert<S,A>,A extends CharSequence>
Parameters:
customComparator - the comparator to use for incoming assertion checks.
Returns:
this assertion object.

usingDefaultComparator

public S usingDefaultComparator()
Description copied from class: AbstractAssert
Revert to standard comparison for incoming assertion checks.

This method should be used to disable a custom comparison strategy set by calling Assert.usingComparator(Comparator).

Specified by:
usingDefaultComparator in interface Assert<S extends AbstractCharSequenceAssert<S,A>,A extends CharSequence>
Overrides:
usingDefaultComparator in class AbstractAssert<S extends AbstractCharSequenceAssert<S,A>,A extends CharSequence>
Returns:
this assertion object.

inHexadecimal

public S inHexadecimal()
Description copied from class: AbstractAssert
Use hexadecimal object representation instead of standard representation in error messages.

It can be useful when comparing UNICODE characters - many unicode chars have duplicate characters assigned, it is thus impossible to find differences from the standard error message:

With standard message:

 assertThat("µµµ").contains("μμμ");

 java.lang.AssertionError:
 Expecting:
   <"µµµ">
 to contain:
   <"μμμ">
 
With Hexadecimal message:
 assertThat("µµµ").inHexadecimal().contains("μμμ");

 java.lang.AssertionError:
 Expecting:
   <"['00B5', '00B5', '00B5']">
 to contain:
   <"['03BC', '03BC', '03BC']">
 

Overrides:
inHexadecimal in class AbstractAssert<S extends AbstractCharSequenceAssert<S,A>,A extends CharSequence>
Returns:
this assertion object.

inUnicode

public S inUnicode()
Use unicode character representation instead of standard representation in error messages.

It can be useful when comparing UNICODE characters - many unicode chars have duplicate characters assigned, it is thus impossible to find differences from the standard error message:

With standard message:

 assertThat("µµµ").contains("μμμ");
 
 java.lang.AssertionError:
 Expecting:
   <"µµµ">
 to contain:
   <"μμμ">
 
With Hexadecimal message:
 assertThat("µµµ").inUnicode().contains("μμμ");
 
 java.lang.AssertionError:
 Expecting:
   <µµµ>
 to contain:
   <μμμ>
 

Returns:
this assertion object.


Copyright © 2013–2015 AssertJ. All rights reserved.