I know,it's a long post, but read it anyway :)
I have started implementing FEST-64 (http://jira.codehaus.org/browse/
FEST-64) to provide custom comparison strategy (instead of equals())
for basic assertions like,isEqualTo, isIn, ...
assertion example :
raceComparator = new RaceComparator();
assertThat(frodo).usingComparator(raceComparator).isEqualTo(sam);
I have tried different implementations (only for isEqualTo for the
time being), I would like to have your point of view of which you
prefer (and suggest a better way).
Implementation 1 : relies on Objects singleton
- add customComparator to AbstractAssert
- AbstractAssert.isEqualTo decides which strategy to use => if/else
public S isEqualTo(A expected) {
if (mustUseCustomComparator()) {
objects.assertEqualUsingComparator(info, actual, expected,
customComparator);
} else {
objects.assertEqual(info, actual, expected);
}
return myself;
}
- entails new method assertEqualUsingComparator in Objects to
propagate comparator
- add method shouldBeEqualUsingComparator to ShouldBeEqual to get an
error message mentioning the comparator usage
see full implementation here in github FEST-64 branch :
https://github.com/alexruiz/fest-assert-2.x/tree/FEST-64
Implementation 2 : use Strategy design Pattern to switch of comparison
strategy
- no customComparator in AbstractAssert, comparison strategy is hedl
by AbstractAssert.objects attribute
- for standard comparison strategy, objects is Objects singleton
- for custom comparison strategy, objects is
ObjectsWithCustomComparisonStrategy a subclass of Objects - a new
instance of ObjectsWithCustomComparisonStrategy is built for each
assertion.
- ObjectsWithCustomComparisonStrategy has a Comparator attribute.
- AbstractAssert isEqualTo method is not modified, it just swith of
comparison strategy on demand :
public S usingComparator(Comparator<?> customComparator) {
// using a specific strategy to compare actual with other objects.
this.objects = new
ObjectsWithCustomComparisonStrategy(customComparator);
return myself;
}
- Objects has been refactored to let subclass overides areEqual and
shouldBeEqual methods
here's ObjectsWithCustomComparisonStrategy one's
protected boolean areEqual(Object actual, Object other) {
return comparator.compare(actual, other) == 0;
}
protected AssertionErrorFactory shouldBeEqual(Object actual, Object
expected) {
return shouldBeEqualUsingComparator(actual, expected, comparator);
}
- a ShouldBeEqualUsingComparator has been created extending
ShouldBeEqual and overiding
methods creating error message to mention comparator usage.
code is available in FEST-64-Polymorphic-implementation branch :
https://github.com/alexruiz/fest-assert-2.x/tree/FEST-64-Polymorphic-implementation
------------------------------------------------------------------
I prefer implementation 2 based on ObjectsWithCustomComparisonStrategy
because :
- AbstractAssert is almost not changed
- comparison logic is well encapsulated (Strategy Design pattern)
- the specifics of error message building are also encapsulated
Please share your comments/idea with me.
In the meantime, I gonna continue working on FEST-64-Polymorphic-
implementation.
Cheers,
Joel
I finally ends up with using a ComparisonStrategy interface to
delegate all comparison operations to, it understands following
comparison :
- areEqual
- isGreaterThan/isLessThan
- contains (for collection, array and string)
- startsWith, endsWith for String
- removes (since removing an object from a collection is based on
equals method we must redefine it to be consistent with
ComparisonStrategy areEqual)
There is two implementation of ComparisonStrategy
- StandardComparisonStrategy that just use natural comparison (i.e.
the one currently used if client don't used a Comparator)
- ComparatorBasedComparisonStrategy that use a Comparator to perform
comparison
The code in progress can be reviewes at following branches :
- https://github.com/alexruiz/fest-assert-2.x/tree/FEST-64
- https://github.com/alexruiz/fest-util/tree/FEST-64
This is still a wip and there is a lot of work still to do but I think
it is the way to go
Joel
On Nov 27, 6:08 pm, Joel Costigliola <joel.costigli...@gmail.com>
wrote:
> code is available in FEST-64-Polymorphic-implementation branch :https://github.com/alexruiz/fest-assert-2.x/tree/FEST-64-Polymorphic-...