Reusable assertions for Comparable s. @author Alex Ruiz @author Joel Costigliola
| 32 | * @author Joel Costigliola |
| 33 | */ |
| 34 | public class Comparables { |
| 35 | |
| 36 | private static final Comparables INSTANCE = new Comparables(); |
| 37 | |
| 38 | /** |
| 39 | * Returns the singleton instance of this class based on {@link StandardComparisonStrategy}. |
| 40 | * |
| 41 | * @return the singleton instance of this class based on {@link StandardComparisonStrategy}. |
| 42 | */ |
| 43 | public static Comparables instance() { |
| 44 | return INSTANCE; |
| 45 | } |
| 46 | |
| 47 | @VisibleForTesting |
| 48 | Failures failures = Failures.instance(); |
| 49 | final ComparisonStrategy comparisonStrategy; |
| 50 | |
| 51 | @VisibleForTesting |
| 52 | Comparables() { |
| 53 | this(StandardComparisonStrategy.instance()); |
| 54 | } |
| 55 | |
| 56 | public Comparables(ComparisonStrategy comparisonStrategy) { |
| 57 | this.comparisonStrategy = comparisonStrategy; |
| 58 | } |
| 59 | |
| 60 | @VisibleForTesting |
| 61 | public Comparator<?> getComparator() { |
| 62 | if (comparisonStrategy instanceof ComparatorBasedComparisonStrategy) { |
| 63 | return ((ComparatorBasedComparisonStrategy) comparisonStrategy).getComparator(); |
| 64 | } |
| 65 | return null; |
| 66 | } |
| 67 | |
| 68 | @VisibleForTesting |
| 69 | void setFailures(Failures failures) { |
| 70 | this.failures = failures; |
| 71 | } |
| 72 | |
| 73 | @VisibleForTesting |
| 74 | void resetFailures() { |
| 75 | this.failures = Failures.instance(); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Asserts that two T instances are equal. |
| 80 | * |
| 81 | * @param info contains information about the assertion. |
| 82 | * @param actual the actual value. |
| 83 | * @param expected the expected value. |
| 84 | * @throws AssertionError if the actual value is {@code null}. |
| 85 | * @throws AssertionError if the actual value is not equal to the expected one. This method will throw a |
| 86 | * {@code org.junit.ComparisonFailure} instead if JUnit is in the classpath and the expected and actual |
| 87 | * values are not equal. |
| 88 | */ |
| 89 | public <T> void assertEqual(AssertionInfo info, T actual, T expected) { |
| 90 | assertNotNull(info, actual); |
| 91 | if (areEqual(actual, expected)) |