org.fest.assertions.api
Class ListAssert

java.lang.Object
  extended by org.fest.assertions.api.AbstractAssert<S,A>
      extended by org.fest.assertions.api.AbstractIterableAssert<ListAssert,List<?>>
          extended by org.fest.assertions.api.ListAssert
All Implemented Interfaces:
Assert<ListAssert,List<?>>, Descriptable<ListAssert>, EnumerableAssert<ListAssert>, ExtensionPoints<ListAssert,List<?>>, IndexedObjectEnumerableAssert, ObjectEnumerableAssert<ListAssert>

public class ListAssert
extends AbstractIterableAssert<ListAssert,List<?>>
implements IndexedObjectEnumerableAssert

Assertion methods for Lists.

To create an instance of this class, invoke Assertions.assertThat(List).

Author:
Yvonne Wang, Alex Ruiz, Joel Costigliola

Field Summary
 
Fields inherited from class org.fest.assertions.api.AbstractAssert
actual, myself
 
Constructor Summary
protected ListAssert(List<?> actual)
           
 
Method Summary
 ListAssert contains(Object value, Index index)
          Verifies that the actual group contains the given object at the given index.
 ListAssert doesNotContain(Object value, Index index)
          Verifies that the actual group does not contain the given object at the given index.
 ListAssert isSorted()
          Verifies that the actual list is sorted into ascending order according to the natural ordering of its elements.
 ListAssert isSortedAccordingTo(Comparator<? extends Object> comparator)
          Verifies that the actual list is sorted according to the given comparator.
 ListAssert usingComparator(Comparator<?> customComparator)
          Use given custom comparator instead of relying on actual type A equals method for incoming assertion checks.
 ListAssert usingDefaultComparator()
          Revert to standard comparison for incoming assertion checks.
 
Methods inherited from class org.fest.assertions.api.AbstractIterableAssert
are, areAtLeast, areAtMost, areExactly, areNot, areNotAtLeast, areNotAtMost, areNotExactly, contains, containsNull, containsOnly, containsSequence, doesNotContain, doesNotContainNull, doesNotHaveDuplicates, doNotHave, doNotHaveAtLeast, doNotHaveAtMost, doNotHaveExactly, endsWith, hasSize, have, haveAtLeast, haveAtMost, haveExactly, isEmpty, isNotEmpty, isNullOrEmpty, isSubsetOf, startsWith
 
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

ListAssert

protected ListAssert(List<?> actual)
Method Detail

contains

public ListAssert contains(Object value,
                           Index index)
Verifies that the actual group contains the given object at the given index.

Specified by:
contains in interface IndexedObjectEnumerableAssert
Parameters:
value - the object to look for.
index - the index where the object should be stored in the actual group.
Returns:
this assertion object.

doesNotContain

public ListAssert doesNotContain(Object value,
                                 Index index)
Verifies that the actual group does not contain the given object at the given index.

Specified by:
doesNotContain in interface IndexedObjectEnumerableAssert
Parameters:
value - the object to look for.
index - the index where the object should be stored in the actual group.
Returns:
this assertion object.

isSorted

public ListAssert isSorted()
Verifies that the actual list is sorted into ascending order according to the natural ordering of its elements.

All list elements must implement the Comparable interface and must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the list), examples :

Empty lists are considered sorted.
Unique element lists are considered sorted unless the element type is not Comparable.

Returns:
this assertion object.
Throws:
AssertionError - if the actual list is not sorted into ascending order according to the natural ordering of its elements.
AssertionError - if the actual list is null.
AssertionError - if the actual list element type does not implement Comparable.
AssertionError - if the actual list elements are not mutually Comparable.

isSortedAccordingTo

public ListAssert isSortedAccordingTo(Comparator<? extends Object> comparator)
Verifies that the actual list is sorted according to the given comparator.
Empty lists are considered sorted whatever the comparator is.
One element lists are considered sorted if element is compatible with comparator.

Parameters:
comparator - the Comparator used to compare list elements
Returns:
this assertion object.
Throws:
AssertionError - if the actual list is not sorted according to the given comparator.
AssertionError - if the actual list is null.
NullPointerException - if the given comparator is null.
AssertionError - if the actual list elements are not mutually comparabe according to given Comparator.

usingComparator

public ListAssert 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<ListAssert,List<?>>
Overrides:
usingComparator in class AbstractIterableAssert<ListAssert,List<?>>
Parameters:
customComparator - the comparator to use for incoming assertion checks.
Returns:
this assertion object.

usingDefaultComparator

public ListAssert 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<ListAssert,List<?>>
Overrides:
usingDefaultComparator in class AbstractIterableAssert<ListAssert,List<?>>
Returns:
this assertion object.


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