| 1 | |
package net.sf.cotta.test.assertion; |
| 2 | |
|
| 3 | |
import org.hamcrest.BaseMatcher; |
| 4 | |
import org.hamcrest.Description; |
| 5 | |
import org.hamcrest.Matcher; |
| 6 | |
import org.hamcrest.Matchers; |
| 7 | |
import org.hamcrest.core.IsEqual; |
| 8 | |
import org.hamcrest.core.IsNot; |
| 9 | |
import org.hamcrest.core.IsNull; |
| 10 | |
import org.junit.Assert; |
| 11 | |
import org.junit.matchers.JUnitMatchers; |
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
abstract public class BaseAssert<T, A> extends Assert { |
| 21 | 21 | private String description = ""; |
| 22 | |
private T value; |
| 23 | |
|
| 24 | 21 | public BaseAssert(T value) { |
| 25 | 21 | this.value = value; |
| 26 | 21 | } |
| 27 | |
|
| 28 | |
@SuppressWarnings({"unchecked"}) |
| 29 | |
protected A self() { |
| 30 | 10 | return (A) this; |
| 31 | |
} |
| 32 | |
|
| 33 | |
public A describedAs(String description) { |
| 34 | 1 | this.description = description; |
| 35 | 1 | return self(); |
| 36 | |
} |
| 37 | |
|
| 38 | |
public T value() { |
| 39 | 19 | return this.value; |
| 40 | |
} |
| 41 | |
|
| 42 | |
@SuppressWarnings({"EqualsWhichDoesntCheckParameterClass"}) |
| 43 | |
public boolean equals(Object obj) { |
| 44 | 1 | throw new UnsupportedOperationException("equals method is not supported by assertion, you probably wanted to use eq method"); |
| 45 | |
} |
| 46 | |
|
| 47 | |
public A eq(T expected) { |
| 48 | 0 | matches(IsEqual.equalTo(expected)); |
| 49 | 0 | return self(); |
| 50 | |
} |
| 51 | |
|
| 52 | |
public A matches(Matcher<T> matcher) { |
| 53 | 12 | assertThat(description, value(), matcher); |
| 54 | 9 | return self(); |
| 55 | |
} |
| 56 | |
|
| 57 | |
public A sameAs(T expected) { |
| 58 | 1 | matches(Matchers.sameInstance(expected)); |
| 59 | 0 | return self(); |
| 60 | |
} |
| 61 | |
|
| 62 | |
public A notNull() { |
| 63 | 0 | matches(IsNot.not(new IsNull<T>())); |
| 64 | 0 | return self(); |
| 65 | |
} |
| 66 | |
|
| 67 | |
public A not(Matcher<T> matcher) { |
| 68 | 0 | matches(Matchers.not(matcher)); |
| 69 | 0 | return self(); |
| 70 | |
} |
| 71 | |
|
| 72 | |
public A isNull() { |
| 73 | 0 | matches(Matchers.<T>nullValue()); |
| 74 | 0 | return self(); |
| 75 | |
} |
| 76 | |
|
| 77 | |
@SuppressWarnings({"unchecked"}) |
| 78 | |
public <K> K isA(Class<K> expectedClass) { |
| 79 | 0 | matches(instanceOf(expectedClass)); |
| 80 | 0 | return (K) expectedClass; |
| 81 | |
} |
| 82 | |
|
| 83 | |
private <K> Matcher<T> instanceOf(final Class<K> expectedClass) { |
| 84 | 0 | return new BaseMatcher<T>() { |
| 85 | |
public boolean matches(Object item) { |
| 86 | 0 | return expectedClass.isInstance(item); |
| 87 | |
} |
| 88 | |
|
| 89 | |
public void describeTo(Description description) { |
| 90 | 0 | description.appendText("an instance of ").appendText(expectedClass.getName()); |
| 91 | 0 | } |
| 92 | |
}; |
| 93 | |
} |
| 94 | |
|
| 95 | |
public A eqWithHash(T expected) { |
| 96 | 0 | matches(JUnitMatchers.both(IsEqual.equalTo(expected)).and(hashEq(expected))); |
| 97 | 0 | return self(); |
| 98 | |
} |
| 99 | |
|
| 100 | |
private Matcher<T> hashEq(final T expected) { |
| 101 | 0 | return new BaseMatcher<T>() { |
| 102 | |
public boolean matches(Object o) { |
| 103 | 0 | return value().hashCode() == expected.hashCode(); |
| 104 | |
} |
| 105 | |
|
| 106 | |
public void describeTo(Description description) { |
| 107 | 0 | description.appendText("same hash code"); |
| 108 | 0 | } |
| 109 | |
}; |
| 110 | |
} |
| 111 | |
|
| 112 | |
public A notSameAs(T expected) { |
| 113 | 0 | return not(Matchers.sameInstance(expected)); |
| 114 | |
} |
| 115 | |
} |