org.fest.assertions.api
Class StringAssert

java.lang.Object
  extended by org.fest.assertions.api.AbstractAssert<StringAssert,String>
      extended by org.fest.assertions.api.StringAssert
All Implemented Interfaces:
Assert<StringAssert,String>, Descriptable<StringAssert>, EnumerableAssert<StringAssert>, ExtensionPoints<StringAssert,String>

public class StringAssert
extends AbstractAssert<StringAssert,String>
implements EnumerableAssert<StringAssert>

Assertion methods for Strings.

To create a new instance of this class, invoke Assertions.assertThat(String).

Author:
Yvonne Wang, David DIDIER, Alex Ruiz, Joel Costigliola

Field Summary
 
Fields inherited from class org.fest.assertions.api.AbstractAssert
actual, myself
 
Constructor Summary
protected StringAssert(String actual)
           
 
Method Summary
 StringAssert contains(String sequence)
          Verifies that the actual String contains the given sequence.
 StringAssert containsIgnoringCase(String sequence)
          Verifies that the actual String contains the given sequence, ignoring case considerations.
 StringAssert doesNotContain(String sequence)
          Verifies that the actual String does not contain the given sequence.
 StringAssert doesNotMatch(Pattern pattern)
          Verifies that the actual String does not match the given regular expression.
 StringAssert doesNotMatch(String regex)
          Verifies that the actual String does not match the given regular expression.
 StringAssert endsWith(String suffix)
          Verifies that the actual String ends with the given suffix.
 StringAssert hasSize(int expected)
          Verifies that the number of values in the actual group is equal to the given one.
 void isEmpty()
          Verifies that the actual group of values is empty.
 StringAssert isEqualToIgnoringCase(String expected)
          Verifies that the actual String is equal to the given one, ignoring case considerations.
 StringAssert isNotEmpty()
          Verifies that the actual group of values is not empty.
 void isNullOrEmpty()
          Verifies that the actual group of values is null or empty.
 StringAssert matches(Pattern pattern)
          Verifies that the actual String matches the given regular expression.
 StringAssert matches(String regex)
          Verifies that the actual String matches the given regular expression.
 StringAssert startsWith(String prefix)
          Verifies that the actual String starts with the given prefix.
 StringAssert usingComparator(Comparator<?> customComparator)
          Use given custom comparator instead of relying on actual type A equals method for incoming assertion checks.
 StringAssert usingDefaultComparator()
          Revert to standard comparison for incoming assertion checks.
 
Methods inherited from class org.fest.assertions.api.AbstractAssert
as, as, describedAs, describedAs, descriptionText, doesNotHave, equals, has, hashCode, is, isEqualTo, isIn, isIn, isNot, isNotEqualTo, isNotIn, isNotIn, isNotNull, isNotSameAs, isNull, isSameAs
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StringAssert

protected StringAssert(String actual)
Method Detail

isNullOrEmpty

public void isNullOrEmpty()
Verifies that the actual group of values is null or empty.

Specified by:
isNullOrEmpty in interface EnumerableAssert<StringAssert>

isEmpty

public void isEmpty()
Verifies that the actual group of values is empty.

Specified by:
isEmpty in interface EnumerableAssert<StringAssert>

isNotEmpty

public StringAssert isNotEmpty()
Verifies that the actual group of values is not empty.

Specified by:
isNotEmpty in interface EnumerableAssert<StringAssert>
Returns:
this assertion object.

hasSize

public StringAssert hasSize(int expected)
Verifies that the number of values in the actual group is equal to the given one.

Specified by:
hasSize in interface EnumerableAssert<StringAssert>
Parameters:
expected - the expected number of values in the actual group.
Returns:
this assertion object.

isEqualToIgnoringCase

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

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

contains

public StringAssert contains(String sequence)
Verifies that the actual String contains the given sequence.

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

containsIgnoringCase

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

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

doesNotContain

public StringAssert doesNotContain(String sequence)
Verifies that the actual String does not contain the given sequence.

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

startsWith

public StringAssert startsWith(String prefix)
Verifies that the actual String starts with the given prefix.

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

endsWith

public StringAssert endsWith(String suffix)
Verifies that the actual String ends with the given suffix.

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

matches

public StringAssert matches(String regex)
Verifies that the actual String matches the given regular expression.

Parameters:
regex - the regular expression to which the actual String 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 String is null.
AssertionError - if the actual String does not match the given regular expression.

doesNotMatch

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

Parameters:
regex - the regular expression to which the actual String 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 String is null.
AssertionError - if the actual String matches the given regular expression.

matches

public StringAssert matches(Pattern pattern)
Verifies that the actual String matches the given regular expression.

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

doesNotMatch

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

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

usingComparator

public StringAssert usingComparator(Comparator<?> 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.

Example :
 // compares invoices by payee 
 assertThat(invoiceList).usingComparator(invoicePayeeComparator).isEqualTo(expectedInvoiceList).
 
 // compares invoices by date, doesNotHaveDuplicates and contains both use the given invoice date comparator
 assertThat(invoiceList).usingComparator(invoiceDateComparator).doesNotHaveDuplicates().contains(may2010Invoice)
 
 // as assertThat(invoiceList) creates a new assertion, it uses standard comparison strategy (Invoice's equal method) to compare invoiceList elements to lowestInvoice.                                                      
 assertThat(invoiceList).contains(lowestInvoice).
 
Custom comparator is not parameterized with actual type A (ie. Comparator<A>) because if it was, we could not write the following code :
 // frodo and sam are instances of Character (a Character having a Race)
 // raceComparator implements Comparator<Character> 
 // assertThat(frodo) returns an ObjectAssert and not a custom CharacterAssert implementing Assert<CharacterAssert, Character>  
 assertThat(frodo).usingComparator(raceComparator).isEqualTo(sam); // won't compile !
 
 The code does not compile because assertThat(frodo) returns an ObjectAssert, thus usingComparator expects a Comparator<Object> 
 and Comparator<Character> is not a Comparator<Object> as generics are not reified.
 
 Note that, it would have worked if assertThat(frodo) returned a CharacterAssert implementing Assert<CharacterAssert, Character>. 
 

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

usingDefaultComparator

public StringAssert 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<StringAssert,String>
Overrides:
usingDefaultComparator in class AbstractAssert<StringAssert,String>
Returns:
this assertion object.


Copyright © 2007-2012 FEST (Fixtures for Easy Software Testing). All Rights Reserved.