org.fest.assertions.api
Class AbstractAssert<S,A>

java.lang.Object
  extended by org.fest.assertions.api.AbstractAssert<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>, ExtensionPoints<S,A>
Direct Known Subclasses:
AbstractComparableAssert, AbstractIterableAssert, BooleanArrayAssert, BooleanAssert, ByteArrayAssert, CharArrayAssert, DateAssert, DoubleArrayAssert, FileAssert, FloatArrayAssert, ImageAssert, InputStreamAssert, IntArrayAssert, LongArrayAssert, MapAssert, ObjectArrayAssert, ObjectAssert, ShortArrayAssert, StringAssert, ThrowableAssert

public abstract class AbstractAssert<S,A>
extends Object
implements Assert<S,A>

Base class for all assertions.

Author:
Alex Ruiz, Joel Costigliola

Field Summary
protected  A actual
           
protected  S myself
           
 
Constructor Summary
protected AbstractAssert(A actual, Class<S> selfType)
           
 
Method Summary
 S as(Description description)
          Sets the description of this object.
 S as(String description)
          Sets the description of this object.
 S describedAs(Description description)
          Alias for Descriptable.as(String) since "as" is a keyword in Groovy.
 S describedAs(String description)
          Alias for Descriptable.as(String) since "as" is a keyword in Groovy.
 String descriptionText()
          The description of this assertion set with describedAs(String) or describedAs(Description).
 S doesNotHave(Condition<A> condition)
          Verifies that the actual value does not satisfy the given condition.
 boolean equals(Object obj)
          Throws UnsupportedOperationException if called.
 S has(Condition<A> condition)
          Verifies that the actual value satisfies the given condition.
 int hashCode()
          Always returns 1.
 S is(Condition<A> condition)
          Verifies that the actual value satisfies the given condition.
 S isEqualTo(A expected)
          Verifies that the actual value is equal to the given one.
 S isIn(A... values)
          Verifies that the actual value is present in the given array of values.
 S isIn(Collection<?> values)
          Verifies that the actual value is present in the given collection of values.
 S isNot(Condition<A> condition)
          Verifies that the actual value does not satisfy the given condition.
 S isNotEqualTo(A other)
          Verifies that the actual value is not equal to the given one.
 S isNotIn(A... values)
          Verifies that the actual value is not present in the given array of values.
 S isNotIn(Collection<?> values)
          Verifies that the actual value is not present in the given collection of values.
 S isNotNull()
          Verifies that the actual value is not null.
 S isNotSameAs(A other)
          Verifies that the actual value is not the same as the given one.
 void isNull()
          Verifies that the actual value is null.
 S isSameAs(A expected)
          Verifies that the actual value is the same as the given one.
 S usingComparator(Comparator<?> 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.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

actual

protected final A actual

myself

protected final S myself
Constructor Detail

AbstractAssert

protected AbstractAssert(A actual,
                         Class<S> selfType)
Method Detail

as

public final S as(String description)
Sets the description of this object.

Specified by:
as in interface Descriptable<S>
Parameters:
description - the new description to set.
Returns:
this object.
See Also:
Descriptable.describedAs(String)

as

public final S as(Description description)
Sets the description of this object. To remove or clear the description, pass a EmptyTextDescription as argument.

This overloaded version of "describedAs" offers more flexibility than the one taking a String by allowing users to pass their own implementation of a description. For example, a description that creates its value lazily, only when an assertion failure occurs.

Specified by:
as in interface Descriptable<S>
Parameters:
description - the new description to set.
Returns:
this object.
See Also:
Descriptable.describedAs(Description)

describedAs

public final S describedAs(String description)
Alias for Descriptable.as(String) since "as" is a keyword in Groovy.

Specified by:
describedAs in interface Descriptable<S>
Parameters:
description - the new description to set.
Returns:
this object.

describedAs

public final S describedAs(Description description)
Alias for Descriptable.as(String) since "as" is a keyword in Groovy. To remove or clear the description, pass a EmptyTextDescription as argument.

This overloaded version of "describedAs" offers more flexibility than the one taking a String by allowing users to pass their own implementation of a description. For example, a description that creates its value lazily, only when an assertion failure occurs.

Specified by:
describedAs in interface Descriptable<S>
Parameters:
description - the new description to set.
Returns:
this object.

isEqualTo

public S isEqualTo(A expected)
Verifies that the actual value is equal to the given one.

Specified by:
isEqualTo in interface Assert<S,A>
Parameters:
expected - the given value to compare the actual value to.
Returns:
this assertion object.

isNotEqualTo

public S isNotEqualTo(A other)
Verifies that the actual value is not equal to the given one.

Specified by:
isNotEqualTo in interface Assert<S,A>
Parameters:
other - the given value to compare the actual value to.
Returns:
this assertion object.

isNull

public final void isNull()
Verifies that the actual value is null.

Specified by:
isNull in interface Assert<S,A>

isNotNull

public final S isNotNull()
Verifies that the actual value is not null.

Specified by:
isNotNull in interface Assert<S,A>
Returns:
this assertion object.

isSameAs

public final S isSameAs(A expected)
Verifies that the actual value is the same as the given one.

Specified by:
isSameAs in interface Assert<S,A>
Parameters:
expected - the given value to compare the actual value to.
Returns:
this assertion object.

isNotSameAs

public final S isNotSameAs(A other)
Verifies that the actual value is not the same as the given one.

Specified by:
isNotSameAs in interface Assert<S,A>
Parameters:
other - the given value to compare the actual value to.
Returns:
this assertion object.

isIn

public final S isIn(A... values)
Verifies that the actual value is present in the given array of values.

Specified by:
isIn in interface Assert<S,A>
Parameters:
values - the given array to search the actual value in.
Returns:
this assertion object.

isNotIn

public final S isNotIn(A... values)
Verifies that the actual value is not present in the given array of values.

Specified by:
isNotIn in interface Assert<S,A>
Parameters:
values - the given array to search the actual value in.
Returns:
this assertion object.

isIn

public final S isIn(Collection<?> values)
Verifies that the actual value is present in the given collection of values.

Specified by:
isIn in interface Assert<S,A>
Parameters:
values - the given collection to search the actual value in.
Returns:
this assertion object.

isNotIn

public final S isNotIn(Collection<?> values)
Verifies that the actual value is not present in the given collection of values.

Specified by:
isNotIn in interface Assert<S,A>
Parameters:
values - the given collection to search the actual value in.
Returns:
this assertion object.

is

public final S is(Condition<A> condition)
Verifies that the actual value satisfies the given condition. This method is an alias for ExtensionPoints.has(Condition).

Specified by:
is in interface ExtensionPoints<S,A>
Parameters:
condition - the given condition.
Returns:
this ExtensionPoints object.
See Also:
ExtensionPoints.is(Condition)

isNot

public final S isNot(Condition<A> condition)
Verifies that the actual value does not satisfy the given condition. This method is an alias for ExtensionPoints.doesNotHave(Condition).

Specified by:
isNot in interface ExtensionPoints<S,A>
Parameters:
condition - the given condition.
Returns:
this ExtensionPoints object.
See Also:
ExtensionPoints.isNot(Condition)

has

public final S has(Condition<A> condition)
Verifies that the actual value satisfies the given condition. This method is an alias for ExtensionPoints.is(Condition).

Specified by:
has in interface ExtensionPoints<S,A>
Parameters:
condition - the given condition.
Returns:
this ExtensionPoints object.
See Also:
ExtensionPoints.is(Condition)

doesNotHave

public final S doesNotHave(Condition<A> condition)
Verifies that the actual value does not satisfy the given condition. This method is an alias for ExtensionPoints.isNot(Condition).

Specified by:
doesNotHave in interface ExtensionPoints<S,A>
Parameters:
condition - the given condition.
Returns:
this ExtensionPoints object.
See Also:
ExtensionPoints.isNot(Condition)

descriptionText

public final String descriptionText()
The description of this assertion set with describedAs(String) or describedAs(Description).

Returns:
the description String representation of this assertion.

usingComparator

public S usingComparator(Comparator<?> customComparator)
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<S,A>
Parameters:
customComparator - the comparator to use for incoming assertion checks.
Returns:
this assertion object.

usingDefaultComparator

public S usingDefaultComparator()
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,A>
Returns:
this assertion object.

equals

public final boolean equals(Object obj)
Throws UnsupportedOperationException if called. It is easy to accidentally call Assert.equals(Object) instead of isEqualTo.

Specified by:
equals in interface Assert<S,A>
Overrides:
equals in class Object

hashCode

public final int hashCode()
Always returns 1.

Overrides:
hashCode in class Object
Returns:
1.


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