private <A> ByFieldsComparison isEqualToIgnoringGivenFields(A actual, A other, Map<String, Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType, String[] givenIgnoredFields) { Set<Field> declaredFieldsIncludingInherited = getDeclaredFieldsIncludingInherited(actual.getClass()); List<String> fieldsNames = new LinkedList<>(); List<Object> expectedValues = new LinkedList<>(); List<Object> rejectedValues = new LinkedList<>(); Set<String> ignoredFields = newLinkedHashSet(givenIgnoredFields); for (Field field : declaredFieldsIncludingInherited) { // ignore private field if user has decided not to use them in comparison String fieldName = field.getName(); if (ignoredFields.contains(fieldName) || !canReadFieldValue(field, actual)) { continue; } Object actualFieldValue = getPropertyOrFieldValue(actual, fieldName); Object otherFieldValue = getPropertyOrFieldValue(other, fieldName); if (!propertyOrFieldValuesAreEqual(actualFieldValue, otherFieldValue, fieldName, comparatorByPropertyOrField, comparatorByType)) { fieldsNames.add(fieldName); rejectedValues.add(actualFieldValue); expectedValues.add(otherFieldValue); } } return new ByFieldsComparison(fieldsNames, expectedValues, rejectedValues); }
/** * Assert that the given object is lenient equals to other object by comparing given fields value only. * * @param info contains information about the assertion. * @param actual the given object. * @param other the object to compare {@code actual} to. * @param fields accepted fields * @throws NullPointerException if the other type is {@code null}. * @throws AssertionError if actual is {@code null}. * @throws AssertionError if the actual and the given object are not lenient equals. * @throws AssertionError if the other object is not an instance of the actual type. * @throws IntrospectionError if a field does not exist in actual. */ public <A> void assertIsEqualToComparingOnlyGivenFields(AssertionInfo info, A actual, A other, String... fields) { assertNotNull(info, actual); assertOtherTypeIsCompatibleWithActualClass(info, other, actual.getClass()); ByFieldsComparison byFieldsComparison = isEqualToComparingOnlyGivenFields(actual, other, fields); if (byFieldsComparison.isFieldsNamesNotEmpty()) throw failures.failure(info, shouldBeEqualComparingOnlyGivenFields(actual, byFieldsComparison.fieldsNames, byFieldsComparison.rejectedValues, byFieldsComparison.expectedValues, newArrayList(fields))); }
/** * Assert that the given object is lenient equals to the other by comparing all fields (including inherited fields) * unless given ignored ones. * * @param info contains information about the assertion. * @param actual the given object. * @param other the object to compare {@code actual} to. * @param fields the fields to ignore in comparison * @throws NullPointerException if the other type is {@code null}. * @throws AssertionError if actual is {@code null}. * @throws AssertionError if the actual and the given object are not lenient equals. * @throws AssertionError if the other object is not an instance of the actual type. */ public <A> void assertIsEqualToIgnoringGivenFields(AssertionInfo info, A actual, A other, String... fields) { assertNotNull(info, actual); assertOtherTypeIsCompatibleWithActualClass(info, other, actual.getClass()); ByFieldsComparison byFieldsComparison = isEqualToIgnoringGivenFields(actual, other, fields); if (byFieldsComparison.isFieldsNamesNotEmpty()) throw failures.failure(info, shouldBeEqualToIgnoringGivenFields(actual, byFieldsComparison.fieldsNames, byFieldsComparison.rejectedValues, byFieldsComparison.expectedValues, newArrayList(fields))); }
private <A> ByFieldsComparison isEqualToIgnoringGivenFields(A actual, A other, String[] givenIgnoredFields) { Set<Field> declaredFieldsIncludingInherited = getDeclaredFieldsIncludingInherited(actual.getClass()); verifyIgnoredFieldsExist(actual, declaredFieldsIncludingInherited, givenIgnoredFields); List<String> fieldsNames = new LinkedList<String>(); List<Object> expectedValues = new LinkedList<Object>(); List<Object> rejectedValues = new LinkedList<Object>(); Set<String> ignoredFields = newLinkedHashSet(givenIgnoredFields); for (Field field : declaredFieldsIncludingInherited) { // ignore private field if user has decided not to use them in comparison if (ignoredFields.contains(field.getName()) || !canReadFieldValue(field, actual)) { continue; } Object actualFieldValue = getFieldOrPropertyValue(actual, field.getName()); Object otherFieldValue = getFieldOrPropertyValue(other, field.getName()); if (!org.assertj.core.util.Objects.areEqual(actualFieldValue, otherFieldValue)) { fieldsNames.add(field.getName()); rejectedValues.add(actualFieldValue); expectedValues.add(otherFieldValue); } } return new ByFieldsComparison(fieldsNames, expectedValues, rejectedValues); }
private <A> ByFieldsComparison isEqualToComparingOnlyGivenFields(A actual, A other, String[] fields) { List<String> rejectedFieldsNames = new LinkedList<String>(); List<Object> expectedValues = new LinkedList<Object>(); List<Object> rejectedValues = new LinkedList<Object>(); for (String fieldName : fields) { Object actualFieldValue = getFieldOrPropertyValue(actual, fieldName); Object otherFieldValue = getFieldOrPropertyValue(other, fieldName); if (!org.assertj.core.util.Objects.areEqual(actualFieldValue, otherFieldValue)) { rejectedFieldsNames.add(fieldName); expectedValues.add(otherFieldValue); rejectedValues.add(actualFieldValue); } } return new ByFieldsComparison(rejectedFieldsNames, expectedValues, rejectedValues); }
public boolean areEqualToComparingOnlyGivenFields(Object actual, Object other, String... fields) { return isEqualToComparingOnlyGivenFields(actual, other, fields).isFieldsNamesEmpty(); }
public boolean areEqualToIgnoringGivenFields(Object actual, Object other, String... fields) { return isEqualToIgnoringGivenFields(actual, other, fields).isFieldsNamesEmpty(); }
public boolean isFieldsNamesNotEmpty() { return !isFieldsNamesEmpty(); } }
/** * Assert that the given object is lenient equals to the other by comparing all fields (including inherited fields) * unless given ignored ones. * * @param <A> the actual type * @param info contains information about the assertion. * @param actual the given object. * @param other the object to compare {@code actual} to. * @param comparatorByPropertyOrField comparators use for specific fields * @param comparatorByType comparators use for specific types * @param fields the fields to ignore in comparison * @throws NullPointerException if the other type is {@code null}. * @throws AssertionError if actual is {@code null}. * @throws AssertionError if the actual and the given object are not lenient equals. * @throws AssertionError if the other object is not an instance of the actual type. */ public <A> void assertIsEqualToIgnoringGivenFields(AssertionInfo info, A actual, A other, Map<String, Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType, String... fields) { assertNotNull(info, actual); ByFieldsComparison byFieldsComparison = isEqualToIgnoringGivenFields(actual, other, comparatorByPropertyOrField, comparatorByType, fields); if (byFieldsComparison.isFieldsNamesNotEmpty()) throw failures.failure(info, shouldBeEqualToIgnoringGivenFields(actual, byFieldsComparison.fieldsNames, byFieldsComparison.rejectedValues, byFieldsComparison.expectedValues, newArrayList(fields))); }
comparatorByType, fields); if (byFieldsComparison.isFieldsNamesNotEmpty()) throw failures.failure(info, shouldBeEqualComparingOnlyGivenFields(actual, byFieldsComparison.fieldsNames, byFieldsComparison.rejectedValues,
private <A> ByFieldsComparison isEqualToComparingOnlyGivenFields(A actual, A other, Map<String, Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType, String[] fields) { List<String> rejectedFieldsNames = new LinkedList<>(); List<Object> expectedValues = new LinkedList<>(); List<Object> rejectedValues = new LinkedList<>(); for (String fieldName : fields) { Object actualFieldValue = getPropertyOrFieldValue(actual, fieldName); Object otherFieldValue = getPropertyOrFieldValue(other, fieldName); if (!propertyOrFieldValuesAreEqual(actualFieldValue, otherFieldValue, fieldName, comparatorByPropertyOrField, comparatorByType)) { rejectedFieldsNames.add(fieldName); expectedValues.add(otherFieldValue); rejectedValues.add(actualFieldValue); } } return new ByFieldsComparison(rejectedFieldsNames, expectedValues, rejectedValues); }
comparatorByType, fields); if (byFieldsComparison.isFieldsNamesNotEmpty()) throw failures.failure(info, shouldBeEqualComparingOnlyGivenFields(actual, byFieldsComparison.fieldsNames, byFieldsComparison.rejectedValues,
/** * Assert that the given object is lenient equals to the other by comparing all fields (including inherited fields) * unless given ignored ones. * * @param <A> the actual type * @param info contains information about the assertion. * @param actual the given object. * @param other the object to compare {@code actual} to. * @param comparatorByPropertyOrField comparators use for specific fields * @param comparatorByType comparators use for specific types * @param fields the fields to ignore in comparison * @throws NullPointerException if the other type is {@code null}. * @throws AssertionError if actual is {@code null}. * @throws AssertionError if the actual and the given object are not lenient equals. * @throws AssertionError if the other object is not an instance of the actual type. */ public <A> void assertIsEqualToIgnoringGivenFields(AssertionInfo info, A actual, A other, Map<String, Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType, String... fields) { assertNotNull(info, actual); ByFieldsComparison byFieldsComparison = isEqualToIgnoringGivenFields(actual, other, comparatorByPropertyOrField, comparatorByType, fields); if (byFieldsComparison.isFieldsNamesNotEmpty()) throw failures.failure(info, shouldBeEqualToIgnoringGivenFields(actual, byFieldsComparison.fieldsNames, byFieldsComparison.rejectedValues, byFieldsComparison.expectedValues, newArrayList(fields))); }
private <A> ByFieldsComparison isEqualToIgnoringGivenFields(A actual, A other, Map<String, Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType, String[] givenIgnoredFields) { Set<Field> declaredFieldsIncludingInherited = getDeclaredFieldsIncludingInherited(actual.getClass()); List<String> fieldsNames = new LinkedList<>(); List<Object> expectedValues = new LinkedList<>(); List<Object> rejectedValues = new LinkedList<>(); Set<String> ignoredFields = newLinkedHashSet(givenIgnoredFields); for (Field field : declaredFieldsIncludingInherited) { // ignore private field if user has decided not to use them in comparison String fieldName = field.getName(); if (ignoredFields.contains(fieldName) || !canReadFieldValue(field, actual)) { continue; } Object actualFieldValue = getPropertyOrFieldValue(actual, fieldName); Object otherFieldValue = getPropertyOrFieldValue(other, fieldName); if (!propertyOrFieldValuesAreEqual(actualFieldValue, otherFieldValue, fieldName, comparatorByPropertyOrField, comparatorByType)) { fieldsNames.add(fieldName); rejectedValues.add(actualFieldValue); expectedValues.add(otherFieldValue); } } return new ByFieldsComparison(fieldsNames, expectedValues, rejectedValues); }
private <A> ByFieldsComparison isEqualToComparingOnlyGivenFields(A actual, A other, Map<String, Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType, String[] fields) { List<String> rejectedFieldsNames = new LinkedList<>(); List<Object> expectedValues = new LinkedList<>(); List<Object> rejectedValues = new LinkedList<>(); for (String fieldName : fields) { Object actualFieldValue = getPropertyOrFieldValue(actual, fieldName); Object otherFieldValue = getPropertyOrFieldValue(other, fieldName); if (!propertyOrFieldValuesAreEqual(actualFieldValue, otherFieldValue, fieldName, comparatorByPropertyOrField, comparatorByType)) { rejectedFieldsNames.add(fieldName); expectedValues.add(otherFieldValue); rejectedValues.add(actualFieldValue); } } return new ByFieldsComparison(rejectedFieldsNames, expectedValues, rejectedValues); }
public boolean areEqualToComparingOnlyGivenFields(Object actual, Object other, Map<String, Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType, String... fields) { return isEqualToComparingOnlyGivenFields(actual, other, comparatorByPropertyOrField, comparatorByType, fields).isFieldsNamesEmpty(); }
public boolean areEqualToIgnoringGivenFields(Object actual, Object other, Map<String, Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType, String... fields) { return isEqualToIgnoringGivenFields(actual, other, comparatorByPropertyOrField, comparatorByType, fields).isFieldsNamesEmpty(); }
public boolean isFieldsNamesNotEmpty() { return !isFieldsNamesEmpty(); } }
public boolean isFieldsNamesNotEmpty() { return !isFieldsNamesEmpty(); } }
public boolean areEqualToComparingOnlyGivenFields(Object actual, Object other, Map<String, Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType, String... fields) { return isEqualToComparingOnlyGivenFields(actual, other, comparatorByPropertyOrField, comparatorByType, fields).isFieldsNamesEmpty(); }