public class Objects
extends java.lang.Object
Object
s.Modifier and Type | Class and Description |
---|---|
static class |
Objects.ByFieldsComparison |
Modifier and Type | Field and Description |
---|---|
private ComparisonStrategy |
comparisonStrategy |
(package private) Failures |
failures |
private FieldSupport |
fieldSupport |
private static Objects |
INSTANCE |
(package private) PropertySupport |
propertySupport |
Constructor and Description |
---|
Objects() |
Objects(ComparisonStrategy comparisonStrategy) |
Modifier and Type | Method and Description |
---|---|
private boolean |
actualIsExactlyInstanceOfType(java.lang.Object actual,
java.lang.Class<?> expectedType,
AssertionInfo info) |
private boolean |
areEqual(java.lang.Object actual,
java.lang.Object other)
Compares actual and other with standard strategy (null safe equals check).
|
boolean |
areEqualToComparingOnlyGivenFields(java.lang.Object actual,
java.lang.Object other,
java.lang.String... fields) |
boolean |
areEqualToIgnoringGivenFields(java.lang.Object actual,
java.lang.Object other,
java.lang.String... fields) |
void |
assertDoesNotHaveSameClassAs(AssertionInfo info,
java.lang.Object actual,
java.lang.Object other)
Verifies that the actual value does not have the same class as the given object.
|
void |
assertEqual(AssertionInfo info,
java.lang.Object actual,
java.lang.Object expected)
Asserts that two objects are equal.
|
void |
assertHasSameClassAs(AssertionInfo info,
java.lang.Object actual,
java.lang.Object other)
Verifies that the actual value has the same class as the given object.
|
void |
assertHasToString(AssertionInfo info,
java.lang.Object actual,
java.lang.String expectedToString) |
<A> void |
assertIsEqualToComparingOnlyGivenFields(AssertionInfo info,
A actual,
A other,
java.lang.String... fields)
Assert that the given object is lenient equals to other object by comparing given fields value only.
|
<A> void |
assertIsEqualToIgnoringGivenFields(AssertionInfo info,
A actual,
A other,
java.lang.String... fields)
Assert that the given object is lenient equals to the other by comparing all fields (including inherited fields)
unless given ignored ones.
|
<A> void |
assertIsEqualToIgnoringNullFields(AssertionInfo info,
A actual,
A other)
Assert that the given object is lenient equals by ignoring null fields value on other object (including inherited
fields).
|
void |
assertIsExactlyInstanceOf(AssertionInfo info,
java.lang.Object actual,
java.lang.Class<?> type)
Verifies that the actual value is exactly a instance of given type.
|
void |
assertIsIn(AssertionInfo info,
java.lang.Object actual,
java.lang.Iterable<?> values)
Asserts that the given object is present in the given collection.
|
void |
assertIsIn(AssertionInfo info,
java.lang.Object actual,
java.lang.Object[] values)
Asserts that the given object is present in the given array.
|
void |
assertIsInstanceOf(AssertionInfo info,
java.lang.Object actual,
java.lang.Class<?> type)
Verifies that the given object is an instance of the given type.
|
void |
assertIsInstanceOfAny(AssertionInfo info,
java.lang.Object actual,
java.lang.Class<?>[] types)
Verifies that the given object is an instance of any of the given types.
|
void |
assertIsNotExactlyInstanceOf(AssertionInfo info,
java.lang.Object actual,
java.lang.Class<?> type)
Verifies that the actual value is not exactly a instance of given type.
|
void |
assertIsNotIn(AssertionInfo info,
java.lang.Object actual,
java.lang.Iterable<?> values)
Asserts that the given object is not present in the given collection.
|
void |
assertIsNotIn(AssertionInfo info,
java.lang.Object actual,
java.lang.Object[] values)
Asserts that the given object is not present in the given array.
|
void |
assertIsNotInstanceOf(AssertionInfo info,
java.lang.Object actual,
java.lang.Class<?> type)
Verifies that the given object is not an instance of the given type.
|
void |
assertIsNotInstanceOfAny(AssertionInfo info,
java.lang.Object actual,
java.lang.Class<?>[] types)
Verifies that the given object is not an instance of any of the given types.
|
void |
assertIsNotOfAnyClassIn(AssertionInfo info,
java.lang.Object actual,
java.lang.Class<?>[] types)
Verifies that the actual value type is not in given types.
|
void |
assertIsOfAnyClassIn(AssertionInfo info,
java.lang.Object actual,
java.lang.Class<?>[] types)
Verifies that the actual value type is in given types.
|
void |
assertNotEqual(AssertionInfo info,
java.lang.Object actual,
java.lang.Object other)
Asserts that two objects are not equal.
|
void |
assertNotNull(AssertionInfo info,
java.lang.Object actual)
Asserts that the given object is not
null . |
void |
assertNotSame(AssertionInfo info,
java.lang.Object actual,
java.lang.Object other)
Asserts that two objects do not refer to the same object.
|
void |
assertNull(AssertionInfo info,
java.lang.Object actual)
Asserts that the given object is
null . |
void |
assertSame(AssertionInfo info,
java.lang.Object actual,
java.lang.Object expected)
Asserts that two objects refer to the same object.
|
private <A> boolean |
canReadFieldValue(java.lang.reflect.Field field,
A actual) |
private void |
checkIsNotNullAndIsNotEmpty(java.lang.Class<?>[] types) |
private void |
checkIsNotNullAndNotEmpty(java.lang.Iterable<?> values) |
private void |
checkIsNotNullAndNotEmpty(java.lang.Object[] values) |
java.util.Comparator<?> |
getComparator() |
ComparisonStrategy |
getComparisonStrategy() |
private static java.util.Set<java.lang.reflect.Field> |
getDeclaredFieldsIncludingInherited(java.lang.Class<?> clazz)
Returns the declared fields of given class and its superclasses stopping at superclass in
java.lang
package whose fields are not included. |
private <A> java.lang.Object |
getPropertyOrFieldValue(A a,
java.lang.String fieldName)
Get property value first and in case of error try field value.
|
private boolean |
haveSameClass(java.lang.Object actual,
java.lang.Object other,
AssertionInfo info) |
static Objects |
instance()
Returns the singleton instance of this class based on
StandardComparisonStrategy . |
private boolean |
isActualIn(java.lang.Object actual,
java.lang.Iterable<?> values) |
private <A> Objects.ByFieldsComparison |
isEqualToComparingOnlyGivenFields(A actual,
A other,
java.lang.String[] fields) |
private <A> Objects.ByFieldsComparison |
isEqualToIgnoringGivenFields(A actual,
A other,
java.lang.String[] givenIgnoredFields) |
private boolean |
isInstanceOfClass(java.lang.Object actual,
java.lang.Class<?> clazz,
AssertionInfo info) |
private boolean |
isItemInArray(java.lang.Object item,
java.lang.Object[] arrayOfValues)
Returns
true if given item is in given array, false otherwise. |
private boolean |
isOfOneOfGivenTypes(java.lang.Object actual,
java.lang.Class<?>[] types,
AssertionInfo info) |
private boolean |
objectIsInstanceOfOneOfGivenClasses(java.lang.Object actual,
java.lang.Class<?>[] types,
AssertionInfo info) |
private <A> void |
verifyIgnoredFieldsExist(A actual,
java.util.Set<java.lang.reflect.Field> declaredFields,
java.lang.String[] ignoredFields) |
private static final Objects INSTANCE
final PropertySupport propertySupport
private final ComparisonStrategy comparisonStrategy
Failures failures
private final FieldSupport fieldSupport
Objects()
public Objects(ComparisonStrategy comparisonStrategy)
public static Objects instance()
StandardComparisonStrategy
.StandardComparisonStrategy
.public java.util.Comparator<?> getComparator()
public ComparisonStrategy getComparisonStrategy()
public void assertIsInstanceOf(AssertionInfo info, java.lang.Object actual, java.lang.Class<?> type)
info
- contains information about the assertion.actual
- the given object.type
- the type to check the given object against.java.lang.NullPointerException
- if the given type is null
.java.lang.AssertionError
- if the given object is null
.java.lang.AssertionError
- if the given object is not an instance of the given type.public void assertIsInstanceOfAny(AssertionInfo info, java.lang.Object actual, java.lang.Class<?>[] types)
info
- contains information about the assertion.actual
- the given object.types
- the types to check the given object against.java.lang.NullPointerException
- if the given array is null
.java.lang.IllegalArgumentException
- if the given array is empty.java.lang.NullPointerException
- if the given array has null
elements.java.lang.AssertionError
- if the given object is null
.java.lang.AssertionError
- if the given object is not an instance of any of the given types.private boolean objectIsInstanceOfOneOfGivenClasses(java.lang.Object actual, java.lang.Class<?>[] types, AssertionInfo info)
public void assertIsNotInstanceOf(AssertionInfo info, java.lang.Object actual, java.lang.Class<?> type)
info
- contains information about the assertion.actual
- the given object.type
- the type to check the given object against.java.lang.NullPointerException
- if the given type is null
.java.lang.AssertionError
- if the given object is null
.java.lang.AssertionError
- if the given object is an instance of the given type.private boolean isInstanceOfClass(java.lang.Object actual, java.lang.Class<?> clazz, AssertionInfo info)
public void assertIsNotInstanceOfAny(AssertionInfo info, java.lang.Object actual, java.lang.Class<?>[] types)
info
- contains information about the assertion.actual
- the given object.types
- the types to check the given object against.java.lang.NullPointerException
- if the given array is null
.java.lang.IllegalArgumentException
- if the given array is empty.java.lang.NullPointerException
- if the given array has null
elements.java.lang.AssertionError
- if the given object is null
.java.lang.AssertionError
- if the given object is an instance of any of the given types.public void assertHasSameClassAs(AssertionInfo info, java.lang.Object actual, java.lang.Object other)
info
- contains information about the assertion.actual
- the given object.java.lang.AssertionError
- if the actual has not the same type has the given object.java.lang.NullPointerException
- if the actual value is null.java.lang.NullPointerException
- if the given object is null.private boolean haveSameClass(java.lang.Object actual, java.lang.Object other, AssertionInfo info)
public void assertDoesNotHaveSameClassAs(AssertionInfo info, java.lang.Object actual, java.lang.Object other)
info
- contains information about the assertion.actual
- the given object.other
- the object to check type against.java.lang.AssertionError
- if the actual has the same type has the given object.java.lang.NullPointerException
- if the actual value is null.java.lang.NullPointerException
- if the given object is null.public void assertIsExactlyInstanceOf(AssertionInfo info, java.lang.Object actual, java.lang.Class<?> type)
info
- contains information about the assertion.actual
- the given object.type
- the type to check the actual value against.java.lang.AssertionError
- if the actual is not exactly a instance of given type.java.lang.NullPointerException
- if the actual value is null.java.lang.NullPointerException
- if the given object is null.private boolean actualIsExactlyInstanceOfType(java.lang.Object actual, java.lang.Class<?> expectedType, AssertionInfo info)
public void assertIsNotExactlyInstanceOf(AssertionInfo info, java.lang.Object actual, java.lang.Class<?> type)
info
- contains information about the assertion.actual
- the given object.type
- the type to check the actual value against.java.lang.AssertionError
- if the actual is exactly a instance of given type.java.lang.NullPointerException
- if the actual value is null.java.lang.NullPointerException
- if the given object is null.public void assertIsOfAnyClassIn(AssertionInfo info, java.lang.Object actual, java.lang.Class<?>[] types)
info
- contains information about the assertion.actual
- the given object.types
- the types to check the actual value against.java.lang.AssertionError
- if the actual value type is in given type.java.lang.NullPointerException
- if the actual value is null.java.lang.NullPointerException
- if the given types is null.private boolean isOfOneOfGivenTypes(java.lang.Object actual, java.lang.Class<?>[] types, AssertionInfo info)
public void assertIsNotOfAnyClassIn(AssertionInfo info, java.lang.Object actual, java.lang.Class<?>[] types)
info
- contains information about the assertion.actual
- the given object.types
- the types to check the actual value against.java.lang.AssertionError
- if the actual value type is in given type.java.lang.NullPointerException
- if the actual value is null.java.lang.NullPointerException
- if the given types is null.private void checkIsNotNullAndIsNotEmpty(java.lang.Class<?>[] types)
public void assertEqual(AssertionInfo info, java.lang.Object actual, java.lang.Object expected)
info
- contains information about the assertion.actual
- the "actual" object.expected
- the "expected" object.java.lang.AssertionError
- if actual
is not equal to expected
. This method will throw a
org.junit.ComparisonFailure
instead if JUnit is in the classpath and the given objects are not
equal.public void assertNotEqual(AssertionInfo info, java.lang.Object actual, java.lang.Object other)
info
- contains information about the assertion.actual
- the given object.other
- the object to compare actual
to.java.lang.AssertionError
- if actual
is equal to other
.private boolean areEqual(java.lang.Object actual, java.lang.Object other)
actual
- the object to compare to otherother
- the object to compare to actualpublic void assertNull(AssertionInfo info, java.lang.Object actual)
null
.info
- contains information about the assertion.actual
- the given object.java.lang.AssertionError
- if the given object is not null
.public void assertNotNull(AssertionInfo info, java.lang.Object actual)
null
.info
- contains information about the assertion.actual
- the given object.java.lang.AssertionError
- if the given object is null
.public void assertSame(AssertionInfo info, java.lang.Object actual, java.lang.Object expected)
info
- contains information about the assertion.actual
- the given object.expected
- the expected object.java.lang.AssertionError
- if the given objects do not refer to the same object.public void assertNotSame(AssertionInfo info, java.lang.Object actual, java.lang.Object other)
info
- contains information about the assertion.actual
- the given object.other
- the object to compare actual
to.java.lang.AssertionError
- if the given objects refer to the same object.public void assertHasToString(AssertionInfo info, java.lang.Object actual, java.lang.String expectedToString)
public void assertIsIn(AssertionInfo info, java.lang.Object actual, java.lang.Object[] values)
info
- contains information about the assertion.actual
- the given object.values
- the given array.java.lang.NullPointerException
- if the given array is null
.java.lang.IllegalArgumentException
- if the given array is empty.java.lang.AssertionError
- if the given object is not present in the given array.public void assertIsNotIn(AssertionInfo info, java.lang.Object actual, java.lang.Object[] values)
info
- contains information about the assertion.actual
- the given object.values
- the given array.java.lang.NullPointerException
- if the given array is null
.java.lang.IllegalArgumentException
- if the given array is empty.java.lang.AssertionError
- if the given object is present in the given array.private void checkIsNotNullAndNotEmpty(java.lang.Object[] values)
private boolean isItemInArray(java.lang.Object item, java.lang.Object[] arrayOfValues)
true
if given item is in given array, false
otherwise.item
- the object to look for in arrayOfValuesarrayOfValues
- the array of valuestrue
if given item is in given array, false
otherwise.public void assertIsIn(AssertionInfo info, java.lang.Object actual, java.lang.Iterable<?> values)
info
- contains information about the assertion.actual
- the given object.values
- the given iterable.java.lang.NullPointerException
- if the given collection is null
.java.lang.IllegalArgumentException
- if the given collection is empty.java.lang.AssertionError
- if the given object is not present in the given collection.public void assertIsNotIn(AssertionInfo info, java.lang.Object actual, java.lang.Iterable<?> values)
info
- contains information about the assertion.actual
- the given object.values
- the given collection.java.lang.NullPointerException
- if the given iterable is null
.java.lang.IllegalArgumentException
- if the given collection is empty.java.lang.AssertionError
- if the given object is present in the given collection.private void checkIsNotNullAndNotEmpty(java.lang.Iterable<?> values)
private boolean isActualIn(java.lang.Object actual, java.lang.Iterable<?> values)
public <A> void assertIsEqualToIgnoringNullFields(AssertionInfo info, A actual, A other)
info
- contains information about the assertion.actual
- the given object.other
- the object to compare actual
to.java.lang.NullPointerException
- if the actual type is null
.java.lang.NullPointerException
- if the other type is null
.java.lang.AssertionError
- if the actual and the given object are not lenient equals.java.lang.AssertionError
- if the other object is not an instance of the actual type.public <A> void assertIsEqualToComparingOnlyGivenFields(AssertionInfo info, A actual, A other, java.lang.String... fields)
info
- contains information about the assertion.actual
- the given object.other
- the object to compare actual
to.fields
- accepted fieldsjava.lang.NullPointerException
- if the other type is null
.java.lang.AssertionError
- if actual is null
.java.lang.AssertionError
- if the actual and the given object are not lenient equals.java.lang.AssertionError
- if the other object is not an instance of the actual type.IntrospectionError
- if a field does not exist in actual.private <A> Objects.ByFieldsComparison isEqualToComparingOnlyGivenFields(A actual, A other, java.lang.String[] fields)
public <A> void assertIsEqualToIgnoringGivenFields(AssertionInfo info, A actual, A other, java.lang.String... fields)
info
- contains information about the assertion.actual
- the given object.other
- the object to compare actual
to.fields
- the fields to ignore in comparisonjava.lang.NullPointerException
- if the other type is null
.java.lang.AssertionError
- if actual is null
.java.lang.AssertionError
- if the actual and the given object are not lenient equals.java.lang.AssertionError
- if the other object is not an instance of the actual type.private <A> Objects.ByFieldsComparison isEqualToIgnoringGivenFields(A actual, A other, java.lang.String[] givenIgnoredFields)
private <A> boolean canReadFieldValue(java.lang.reflect.Field field, A actual)
private <A> void verifyIgnoredFieldsExist(A actual, java.util.Set<java.lang.reflect.Field> declaredFields, java.lang.String[] ignoredFields)
private <A> java.lang.Object getPropertyOrFieldValue(A a, java.lang.String fieldName)
This method supports nested field/property (e.g. "address.street.number").
a
- the object to get field value fromfieldName
- Field name to read, can be nestedIntrospectionError
- is field value can't get retrieved.private static java.util.Set<java.lang.reflect.Field> getDeclaredFieldsIncludingInherited(java.lang.Class<?> clazz)
java.lang
package whose fields are not included.clazz
- the class we want the declared fields.public boolean areEqualToIgnoringGivenFields(java.lang.Object actual, java.lang.Object other, java.lang.String... fields)
public boolean areEqualToComparingOnlyGivenFields(java.lang.Object actual, java.lang.Object other, java.lang.String... fields)