Reusable assertions for Objects. @author Yvonne Wang @author Alex Ruiz @author Nicolas François @author Mikhail Mazursky
| 58 | * @author Mikhail Mazursky |
| 59 | */ |
| 60 | public class Objects { |
| 61 | |
| 62 | private static final Objects INSTANCE = new Objects(); |
| 63 | @VisibleForTesting |
| 64 | final PropertySupport propertySupport = PropertySupport.instance(); |
| 65 | private final ComparisonStrategy comparisonStrategy; |
| 66 | @VisibleForTesting |
| 67 | Failures failures = Failures.instance(); |
| 68 | private final FieldSupport fieldSupport = FieldSupport.comparison(); |
| 69 | |
| 70 | /** |
| 71 | * Returns the singleton instance of this class based on {@link StandardComparisonStrategy}. |
| 72 | * |
| 73 | * @return the singleton instance of this class based on {@link StandardComparisonStrategy}. |
| 74 | */ |
| 75 | public static Objects instance() { |
| 76 | return INSTANCE; |
| 77 | } |
| 78 | |
| 79 | @VisibleForTesting |
| 80 | Objects() { |
| 81 | this(StandardComparisonStrategy.instance()); |
| 82 | } |
| 83 | |
| 84 | public Objects(ComparisonStrategy comparisonStrategy) { |
| 85 | this.comparisonStrategy = comparisonStrategy; |
| 86 | } |
| 87 | |
| 88 | @VisibleForTesting |
| 89 | public Comparator<?> getComparator() { |
| 90 | return comparisonStrategy instanceof ComparatorBasedComparisonStrategy ? |
| 91 | ((ComparatorBasedComparisonStrategy) comparisonStrategy).getComparator() : null; |
| 92 | } |
| 93 | |
| 94 | @VisibleForTesting |
| 95 | public ComparisonStrategy getComparisonStrategy() { |
| 96 | return comparisonStrategy; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Verifies that the given object is an instance of the given type. |
| 101 | * |
| 102 | * @param info contains information about the assertion. |
| 103 | * @param actual the given object. |
| 104 | * @param type the type to check the given object against. |
| 105 | * @throws NullPointerException if the given type is {@code null}. |
| 106 | * @throws AssertionError if the given object is {@code null}. |
| 107 | * @throws AssertionError if the given object is not an instance of the given type. |
| 108 | */ |
| 109 | public void assertIsInstanceOf(AssertionInfo info, Object actual, Class<?> type) { |
| 110 | if (!isInstanceOfClass(actual, type, info)) throw failures.failure(info, shouldBeInstance(actual, type)); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Verifies that the given object is an instance of any of the given types. |
| 115 | * |
| 116 | * @param info contains information about the assertion. |
| 117 | * @param actual the given object. |
nothing calls this directly
no test coverage detected