Best Assertj code snippet using org.assertj.core.api.recursive.comparison.FieldComparators.isEmpty
Source:RecursiveComparisonConfiguration.java
...89 public boolean hasComparatorForType(Class<?> keyType) {90 return typeComparators.hasComparatorForType(keyType);91 }92 public boolean hasCustomComparators() {93 return !typeComparators.isEmpty() || !fieldComparators.isEmpty();94 }95 public Comparator<?> getComparatorForType(Class<?> fieldType) {96 return typeComparators.get(fieldType);97 }98 public TypeComparators getTypeComparators() {99 return typeComparators;100 }101 Stream<Entry<Class<?>, Comparator<?>>> comparatorByTypes() {102 return typeComparators.comparatorByTypes();103 }104 @VisibleForTesting105 boolean getIgnoreAllActualNullFields() {106 return ignoreAllActualNullFields;107 }108 @VisibleForTesting109 boolean getIgnoreAllExpectedNullFields() {110 return ignoreAllExpectedNullFields;111 }112 @VisibleForTesting113 boolean getIgnoreAllOverriddenEquals() {114 return ignoreAllOverriddenEquals;115 }116 /**117 * Sets whether actual empty optional fields are ignored in the recursive comparison.118 * <p>119 * See {@link RecursiveComparisonAssert#ignoringActualNullFields()} for code examples.120 *121 * @param ignoringAllActualEmptyOptionalFields whether to ignore actual empty optional fields in the recursive comparison122 */123 public void setIgnoreAllActualEmptyOptionalFields(boolean ignoringAllActualEmptyOptionalFields) {124 this.ignoreAllActualEmptyOptionalFields = ignoringAllActualEmptyOptionalFields;125 }126 @VisibleForTesting127 boolean getIgnoreAllActualEmptyOptionalFields() {128 return ignoreAllActualEmptyOptionalFields;129 }130 /**131 * Sets whether actual null fields are ignored in the recursive comparison.132 * <p>133 * See {@link RecursiveComparisonAssert#ignoringActualNullFields()} for code examples.134 *135 * @param ignoreAllActualNullFields whether to ignore actual null fields in the recursive comparison136 */137 public void setIgnoreAllActualNullFields(boolean ignoreAllActualNullFields) {138 this.ignoreAllActualNullFields = ignoreAllActualNullFields;139 }140 /**141 * Sets whether expected null fields are ignored in the recursive comparison.142 * <p>143 * See {@link RecursiveComparisonAssert#ignoringExpectedNullFields()} for code examples.144 *145 * @param ignoreAllExpectedNullFields whether to ignore expected null fields in the recursive comparison146 */147 public void setIgnoreAllExpectedNullFields(boolean ignoreAllExpectedNullFields) {148 this.ignoreAllExpectedNullFields = ignoreAllExpectedNullFields;149 }150 /**151 * Adds the given fields to the list of the object under test fields to ignore in the recursive comparison.152 * <p>153 * The field are ignored by name, not by value.154 * <p>155 * See {@link RecursiveComparisonAssert#ignoringFields(String...) RecursiveComparisonAssert#ignoringFields(String...)} for examples.156 *157 * @param fieldsToIgnore the fields of the object under test to ignore in the comparison.158 */159 public void ignoreFields(String... fieldsToIgnore) {160 List<String> fieldLocations = list(fieldsToIgnore);161 ignoredFields.addAll(fieldLocations);162 }163 /**164 * Allows to ignore in the recursive comparison the object under test fields matching the given regexes. The given regexes are added to the already registered ones.165 * <p>166 * See {@link RecursiveComparisonAssert#ignoringFieldsMatchingRegexes(String...) RecursiveComparisonAssert#ignoringFieldsMatchingRegexes(String...)} for examples.167 *168 * @param regexes regexes used to ignore fields in the comparison.169 */170 public void ignoreFieldsMatchingRegexes(String... regexes) {171 ignoredFieldsRegexes.addAll(Stream.of(regexes)172 .map(Pattern::compile)173 .collect(toList()));174 }175 /**176 * Adds the given types to the list of the object under test fields types to ignore in the recursive comparison.177 * The fields are ignored if their types exactly match one of the ignored types, if a field is a subtype of an ignored type it won't be ignored.178 * <p>179 * Note that if some object under test fields are null, they are not ignored by this method as their type can't be evaluated.180 * <p>181 * See {@link RecursiveComparisonAssert#ignoringFields(String...) RecursiveComparisonAssert#ignoringFieldsOfTypes(Class...)} for examples.182 *183 * @param types the types of the object under test to ignore in the comparison.184 */185 public void ignoreFieldsOfTypes(Class<?>... types) {186 stream(types).map(RecursiveComparisonConfiguration::asWrapperIfPrimitiveType).forEach(ignoredTypes::add);187 }188 private static Class<?> asWrapperIfPrimitiveType(Class<?> type) {189 if (!type.isPrimitive()) return type;190 if (type.equals(boolean.class)) return Boolean.class;191 if (type.equals(byte.class)) return Byte.class;192 if (type.equals(int.class)) return Integer.class;193 if (type.equals(short.class)) return Short.class;194 if (type.equals(char.class)) return Character.class;195 if (type.equals(float.class)) return Float.class;196 if (type.equals(double.class)) return Double.class;197 // should not arrive here since we have tested primitive types first198 return type;199 }200 /**201 * Returns the list of the object under test fields to ignore in the recursive comparison.202 *203 * @return the list of the object under test fields to ignore in the recursive comparison.204 */205 public Set<String> getIgnoredFields() {206 return ignoredFields;207 }208 /**209 * Returns the set of the object under test fields types to ignore in the recursive comparison.210 *211 * @return the set of the object under test fields types to ignore in the recursive comparison.212 */213 public Set<Class<?>> getIgnoredTypes() {214 return ignoredTypes;215 }216 /**217 * Force a recursive comparison on all fields (except java types).218 * <p>219 * See {@link RecursiveComparisonAssert#ignoringAllOverriddenEquals()} for examples.220 */221 public void ignoreAllOverriddenEquals() {222 ignoreAllOverriddenEquals = true;223 }224 /**225 * Force a recursive comparison on all fields (except java types).226 * <p>227 * See {@link RecursiveComparisonAssert#usingOverriddenEquals()} for examples.228 */229 public void useOverriddenEquals() {230 ignoreAllOverriddenEquals = false;231 }232 /**233 * Adds the given fields to the list of fields to force a recursive comparison on.234 * <p>235 * See {@link RecursiveComparisonAssert#ignoringOverriddenEqualsForFields(String...) RecursiveComparisonAssert#ignoringOverriddenEqualsForFields(String...)} for examples.236 *237 * @param fields the fields to force a recursive comparison on.238 */239 public void ignoreOverriddenEqualsForFields(String... fields) {240 List<String> fieldLocations = list(fields);241 ignoredOverriddenEqualsForFields.addAll(fieldLocations);242 }243 /**244 * Adds the given regexes to the list of regexes used find the fields to force a recursive comparison on.245 * <p>246 * See {@link RecursiveComparisonAssert#ignoringOverriddenEqualsForFieldsMatchingRegexes(String...) RecursiveComparisonAssert#ignoringOverriddenEqualsForFieldsMatchingRegexes(String...)} for examples.247 *248 * @param regexes regexes used to specify the fields we want to force a recursive comparison on.249 */250 public void ignoreOverriddenEqualsForFieldsMatchingRegexes(String... regexes) {251 ignoredOverriddenEqualsForFieldsMatchingRegexes.addAll(Stream.of(regexes)252 .map(Pattern::compile)253 .collect(toList()));254 }255 /**256 * Adds the given types to the list of types to force a recursive comparison on.257 * <p>258 * See {@link RecursiveComparisonAssert#ignoringOverriddenEqualsForTypes(Class...) RecursiveComparisonAssert#ignoringOverriddenEqualsForTypes(Class...)} for examples.259 *260 * @param types the types to the list of types to force a recursive comparison on.261 */262 public void ignoreOverriddenEqualsForTypes(Class<?>... types) {263 ignoredOverriddenEqualsForTypes.addAll(list(types));264 }265 @VisibleForTesting266 boolean getIgnoreCollectionOrder() {267 return ignoreCollectionOrder;268 }269 /**270 * Sets whether to ignore collection order in the comparison.271 * <p>272 * See {@link RecursiveComparisonAssert#ignoringCollectionOrder()} for code examples.273 *274 * @param ignoreCollectionOrder whether to ignore collection order in the comparison.275 */276 public void ignoreCollectionOrder(boolean ignoreCollectionOrder) {277 this.ignoreCollectionOrder = ignoreCollectionOrder;278 }279 /**280 * Adds the given fields to the list of the object under test fields to ignore collection order in the recursive comparison.281 * <p>282 * See {@link RecursiveComparisonAssert#ignoringCollectionOrderInFields(String...) RecursiveComparisonAssert#ignoringCollectionOrderInFields(String...)} for examples.283 *284 * @param fieldsToIgnoreCollectionOrder the fields of the object under test to ignore collection order in the comparison.285 */286 public void ignoreCollectionOrderInFields(String... fieldsToIgnoreCollectionOrder) {287 List<String> fieldLocations = list(fieldsToIgnoreCollectionOrder);288 ignoredCollectionOrderInFields.addAll(fieldLocations);289 }290 /**291 * Returns the list of the object under test fields to ignore collection order in the recursive comparison.292 *293 * @return the list of the object under test fields to ignore collection order in the recursive comparison.294 */295 public Set<String> getIgnoredCollectionOrderInFields() {296 return ignoredCollectionOrderInFields;297 }298 /**299 * Adds the given regexes to the list of regexes used to find the object under test fields to ignore collection order in the recursive comparison.300 * <p>301 * See {@link RecursiveComparisonAssert#ignoringCollectionOrderInFieldsMatchingRegexes(String...) RecursiveComparisonAssert#ignoringCollectionOrderInFieldsMatchingRegexes(String...)} for examples.302 *303 * @param regexes regexes used to find the object under test fields to ignore collection order in in the comparison.304 */305 public void ignoreCollectionOrderInFieldsMatchingRegexes(String... regexes) {306 ignoredCollectionOrderInFieldsMatchingRegexes.addAll(Stream.of(regexes)307 .map(Pattern::compile)308 .collect(toList()));309 }310 /**311 * Returns the list of regexes used to find the object under test fields to ignore collection order in the recursive comparison.312 *313 * @return the list of regexes used to find the object under test fields to ignore collection order in the recursive comparison.314 */315 public List<Pattern> getIgnoredCollectionOrderInFieldsMatchingRegexes() {316 return ignoredCollectionOrderInFieldsMatchingRegexes;317 }318 /**319 * Registers the given {@link Comparator} to compare the fields with the given type.320 * <p>321 * Comparators registered with this method have less precedence than comparators registered with {@link #registerComparatorForFields(Comparator, String...)}.322 * <p>323 * Note that registering a {@link Comparator} for a given type will override the previously registered BiPredicate/Comparator (if any).324 * <p>325 * See {@link RecursiveComparisonAssert#withComparatorForType(Comparator, Class)} for examples.326 *327 * @param <T> the class type to register a comparator for328 * @param comparator the {@link java.util.Comparator Comparator} to use to compare the given type329 * @param type the type to be compared with the given comparator.330 * @throws NullPointerException if the given comparator is null.331 */332 public <T> void registerComparatorForType(Comparator<? super T> comparator, Class<T> type) {333 requireNonNull(comparator, "Expecting a non null Comparator");334 typeComparators.put(type, comparator);335 }336 /**337 * Registers the given {@link BiPredicate} to compare the fields with the given type.338 * <p>339 * BiPredicates specified with this method have less precedence than the ones registered with {@link #registerEqualsForFields(BiPredicate, String...)}340 * or comparators registered with {@link #registerComparatorForFields(Comparator, String...)}.341 * <p>342 * Note that registering a {@link BiPredicate} for a given type will override the previously registered BiPredicate/Comparator (if any).343 * <p>344 * See {@link RecursiveComparisonAssert#withEqualsForType(BiPredicate, Class)} for examples.345 *346 * @param <T> the class type to register a comparator for347 * @param equals the equals implementation to compare the given type348 * @param type the type to be compared with the given equals implementation .349 * @throws NullPointerException if the given BiPredicate is null.350 * @since 3.17.0351 */352 @SuppressWarnings("unchecked")353 public <T> void registerEqualsForType(BiPredicate<? super T, ? super T> equals, Class<T> type) {354 registerComparatorForType(toComparator(equals), type);355 }356 /**357 * Registers the given {@link Comparator} to compare the fields at the given locations.358 * <p>359 * The fields must be specified from the root object, for example if {@code Foo} has a {@code Bar} field and both have an {@code id} field,360 * one can register a comparator for Foo and Bar's {@code id} by calling:361 * <pre><code class='java'> registerComparatorForFields(idComparator, "foo.id", "foo.bar.id")</code></pre>362 * <p>363 * Comparators registered with this method have precedence over comparators registered with {@link #registerComparatorForType(Comparator, Class)}.364 * <p>365 * Note that registering a {@link Comparator} for a given field will override the previously registered BiPredicate/Comparator (if any).366 * <p>367 * See {@link RecursiveComparisonAssert#withComparatorForFields(Comparator, String...) RecursiveComparisonAssert#withComparatorForFields(Comparator, String...)} for examples.368 *369 * @param comparator the {@link java.util.Comparator Comparator} to use to compare the given field370 * @param fieldLocations the locations from the root object of the fields the comparator should be used for371 * @throws NullPointerException if the given comparator is null.372 */373 public void registerComparatorForFields(Comparator<?> comparator, String... fieldLocations) {374 requireNonNull(comparator, "Expecting a non null Comparator");375 Stream.of(fieldLocations).forEach(fieldLocation -> fieldComparators.registerComparator(fieldLocation, comparator));376 }377 /**378 * Registers the given {@link BiPredicate} to compare the fields at the given locations.379 * <p>380 * The fields must be specified from the root object, for example if {@code Foo} has a {@code Bar} field and both have an {@code id} field,381 * one can register a BiPredicate for Foo and Bar's {@code id} by calling:382 * <pre><code class='java'> registerEqualsForFields(idBiPredicate, "foo.id", "foo.bar.id")</code></pre>383 * <p>384 * BiPredicates registered with this method have precedence over the ones registered with {@link #registerEqualsForType(BiPredicate, Class)}385 * or the comparators registered with {@link #registerComparatorForType(Comparator, Class)}.386 * <p>387 * Note that registering a {@link BiPredicate} for a given field will override the previously registered BiPredicate/Comparator (if any).388 * <p>389 * See {@link RecursiveComparisonAssert#withEqualsForFields(BiPredicate, String...) RecursiveComparisonAssert#withEqualsForFields(BiPredicate, String...)} for examples.390 *391 * @param equals the equals implementation to compare the given fields.392 * @param fieldLocations the locations from the root object of the fields the comparator should be used for393 * @throws NullPointerException if the given BiPredicate is null.394 * @since 3.17.0395 */396 public void registerEqualsForFields(BiPredicate<?, ?> equals, String... fieldLocations) {397 registerComparatorForFields(toComparator(equals), fieldLocations);398 }399 /**400 * Sets whether the recursive comparison will check that actual's type is compatible with expected's type (the same applies for each field).401 * Compatible means that the expected's type is the same or a subclass of actual's type.402 * <p>403 * See {@link RecursiveComparisonAssert#withStrictTypeChecking()} for code examples.404 *405 * @param strictTypeChecking whether the recursive comparison will check that actual's type is compatible with expected's type.406 */407 public void strictTypeChecking(boolean strictTypeChecking) {408 this.strictTypeChecking = strictTypeChecking;409 }410 public boolean isInStrictTypeCheckingMode() {411 return strictTypeChecking;412 }413 public List<Pattern> getIgnoredFieldsRegexes() {414 return ignoredFieldsRegexes;415 }416 public List<Class<?>> getIgnoredOverriddenEqualsForTypes() {417 return ignoredOverriddenEqualsForTypes;418 }419 public List<String> getIgnoredOverriddenEqualsForFields() {420 return ignoredOverriddenEqualsForFields;421 }422 public List<Pattern> getIgnoredOverriddenEqualsForFieldsMatchingRegexes() {423 return ignoredOverriddenEqualsForFieldsMatchingRegexes;424 }425 public Stream<Entry<String, Comparator<?>>> comparatorByFields() {426 return fieldComparators.comparatorByFields();427 }428 @Override429 public String toString() {430 return multiLineDescription(CONFIGURATION_PROVIDER.representation());431 }432 @Override433 public int hashCode() {434 return java.util.Objects.hash(fieldComparators, ignoreAllActualEmptyOptionalFields, ignoreAllActualNullFields,435 ignoreAllExpectedNullFields, ignoreAllOverriddenEquals, ignoreCollectionOrder,436 ignoredCollectionOrderInFields, ignoredCollectionOrderInFieldsMatchingRegexes, ignoredFields,437 ignoredFieldsRegexes, ignoredOverriddenEqualsForFields, ignoredOverriddenEqualsForTypes,438 ignoredOverriddenEqualsForFieldsMatchingRegexes, ignoredTypes, strictTypeChecking,439 typeComparators);440 }441 @Override442 public boolean equals(Object obj) {443 if (this == obj) return true;444 if (obj == null) return false;445 if (getClass() != obj.getClass()) return false;446 RecursiveComparisonConfiguration other = (RecursiveComparisonConfiguration) obj;447 return java.util.Objects.equals(fieldComparators, other.fieldComparators)448 && ignoreAllActualEmptyOptionalFields == other.ignoreAllActualEmptyOptionalFields449 && ignoreAllActualNullFields == other.ignoreAllActualNullFields450 && ignoreAllExpectedNullFields == other.ignoreAllExpectedNullFields451 && ignoreAllOverriddenEquals == other.ignoreAllOverriddenEquals452 && ignoreCollectionOrder == other.ignoreCollectionOrder453 && java.util.Objects.equals(ignoredCollectionOrderInFields, other.ignoredCollectionOrderInFields)454 && java.util.Objects.equals(ignoredFields, other.ignoredFields)455 && java.util.Objects.equals(ignoredFieldsRegexes, other.ignoredFieldsRegexes)456 && java.util.Objects.equals(ignoredOverriddenEqualsForFields, other.ignoredOverriddenEqualsForFields)457 && java.util.Objects.equals(ignoredOverriddenEqualsForTypes, other.ignoredOverriddenEqualsForTypes)458 && java.util.Objects.equals(ignoredOverriddenEqualsForFieldsMatchingRegexes,459 other.ignoredOverriddenEqualsForFieldsMatchingRegexes)460 && java.util.Objects.equals(ignoredTypes, other.ignoredTypes) && strictTypeChecking == other.strictTypeChecking461 && java.util.Objects.equals(typeComparators, other.typeComparators)462 && java.util.Objects.equals(ignoredCollectionOrderInFieldsMatchingRegexes,463 other.ignoredCollectionOrderInFieldsMatchingRegexes);464 }465 public String multiLineDescription(Representation representation) {466 StringBuilder description = new StringBuilder();467 describeIgnoreAllActualNullFields(description);468 describeIgnoreAllActualEmptyOptionalFields(description);469 describeIgnoreAllExpectedNullFields(description);470 describeIgnoredFields(description);471 describeIgnoredFieldsRegexes(description);472 describeIgnoredFieldsForTypes(description);473 describeOverriddenEqualsMethodsUsage(description, representation);474 describeIgnoreCollectionOrder(description);475 describeIgnoredCollectionOrderInFields(description);476 describeIgnoredCollectionOrderInFieldsMatchingRegexes(description);477 describeRegisteredComparatorByTypes(description);478 describeRegisteredComparatorForFields(description);479 describeTypeCheckingStrictness(description);480 return description.toString();481 }482 boolean shouldIgnore(DualValue dualValue) {483 FieldLocation fieldLocation = dualValue.fieldLocation;484 return matchesAnIgnoredField(fieldLocation)485 || matchesAnIgnoredFieldRegex(fieldLocation)486 || shouldIgnoreFieldBasedOnFieldValue(dualValue);487 }488 Set<String> getNonIgnoredActualFieldNames(DualValue dualValue) {489 Set<String> actualFieldsNames = Objects.getFieldsNames(dualValue.actual.getClass());490 // we are doing the same as shouldIgnore(DualValue dualValue) but in two steps for performance reasons:491 // - we filter first ignored field by names that don't need building DualValues492 // - then we filter field DualValues with the remaining criteria that need to get the field value493 // DualValues are built introspecting fields which is expensive.494 return actualFieldsNames.stream()495 // evaluate field name ignoring criteria on dualValue field location + field name496 .filter(fieldName -> !shouldIgnoreFieldBasedOnFieldLocation(dualValue.fieldLocation.field(fieldName)))497 .map(fieldName -> dualValueForField(dualValue, fieldName))498 // evaluate field value ignoring criteria499 .filter(fieldDualValue -> !shouldIgnoreFieldBasedOnFieldValue(fieldDualValue))500 // back to field name501 .map(DualValue::getFieldName)502 .filter(fieldName -> !fieldName.isEmpty())503 .collect(toSet());504 }505 // non accessible stuff506 private boolean shouldIgnoreFieldBasedOnFieldValue(DualValue dualValue) {507 return matchesAnIgnoredNullField(dualValue)508 || matchesAnIgnoredFieldType(dualValue)509 || matchesAnIgnoredEmptyOptionalField(dualValue);510 }511 private boolean shouldIgnoreFieldBasedOnFieldLocation(FieldLocation fieldLocation) {512 return matchesAnIgnoredField(fieldLocation) || matchesAnIgnoredFieldRegex(fieldLocation);513 }514 private static DualValue dualValueForField(DualValue parentDualValue, String fieldName) {515 Object actualFieldValue = COMPARISON.getSimpleValue(fieldName, parentDualValue.actual);516 // no guarantees we have a field in expected named as fieldName517 Object expectedFieldValue;518 try {519 expectedFieldValue = COMPARISON.getSimpleValue(fieldName, parentDualValue.expected);520 } catch (@SuppressWarnings("unused") Exception e) {521 // set the field to null to express it is absent, this not 100% accurate as the value could be null522 // but it works to evaluate if dualValue should be ignored with matchesAnIgnoredFieldType523 expectedFieldValue = null;524 }525 FieldLocation fieldLocation = parentDualValue.fieldLocation.field(fieldName);526 return new DualValue(fieldLocation, actualFieldValue, expectedFieldValue);527 }528 boolean hasCustomComparator(DualValue dualValue) {529 String fieldName = dualValue.getConcatenatedPath();530 if (hasComparatorForField(fieldName)) return true;531 if (dualValue.actual == null && dualValue.expected == null) return false;532 // best effort assuming actual and expected have the same type (not 100% true as we can compare object of differennt types)533 Class<?> valueType = dualValue.actual != null ? dualValue.actual.getClass() : dualValue.expected.getClass();534 return hasComparatorForType(valueType);535 }536 boolean shouldIgnoreOverriddenEqualsOf(DualValue dualValue) {537 // we must compare java basic types otherwise the recursive comparison loops infinitely!538 if (dualValue.isActualJavaType()) return false;539 // enums don't have fields, comparing them field by field has no sense, we need to use equals which is overridden and final540 if (dualValue.isActualAnEnum()) return false;541 return ignoreAllOverriddenEquals542 || matchesAnIgnoredOverriddenEqualsField(dualValue.fieldLocation)543 || (dualValue.actual != null && shouldIgnoreOverriddenEqualsOf(dualValue.actual.getClass()));544 }545 @VisibleForTesting546 boolean shouldIgnoreOverriddenEqualsOf(Class<? extends Object> clazz) {547 return matchesAnIgnoredOverriddenEqualsRegex(clazz) || matchesAnIgnoredOverriddenEqualsType(clazz);548 }549 boolean shouldIgnoreCollectionOrder(FieldLocation fieldLocation) {550 return ignoreCollectionOrder551 || matchesAnIgnoredCollectionOrderInField(fieldLocation)552 || matchesAnIgnoredCollectionOrderInFieldRegex(fieldLocation);553 }554 private void describeIgnoredFieldsRegexes(StringBuilder description) {555 if (!ignoredFieldsRegexes.isEmpty())556 description.append(format("- the fields matching the following regexes were ignored in the comparison: %s%n",557 describeRegexes(ignoredFieldsRegexes)));558 }559 private void describeIgnoredFields(StringBuilder description) {560 if (!ignoredFields.isEmpty())561 description.append(format("- the following fields were ignored in the comparison: %s%n", describeIgnoredFields()));562 }563 private void describeIgnoredFieldsForTypes(StringBuilder description) {564 if (!ignoredTypes.isEmpty())565 description.append(format("- the following types were ignored in the comparison: %s%n", describeIgnoredTypes()));566 }567 private void describeIgnoreAllActualNullFields(StringBuilder description) {568 if (ignoreAllActualNullFields) description.append(format("- all actual null fields were ignored in the comparison%n"));569 }570 private void describeIgnoreAllActualEmptyOptionalFields(StringBuilder description) {571 if (getIgnoreAllActualEmptyOptionalFields())572 description.append(format("- all actual empty optional fields were ignored in the comparison (including Optional, OptionalInt, OptionalLong and OptionalDouble)%n"));573 }574 private void describeIgnoreAllExpectedNullFields(StringBuilder description) {575 if (ignoreAllExpectedNullFields) description.append(format("- all expected null fields were ignored in the comparison%n"));576 }577 private void describeOverriddenEqualsMethodsUsage(StringBuilder description, Representation representation) {578 String header = ignoreAllOverriddenEquals579 ? "- no overridden equals methods were used in the comparison (except for java types)"580 : "- overridden equals methods were used in the comparison";581 description.append(header);582 if (isConfiguredToIgnoreSomeButNotAllOverriddenEqualsMethods()) {583 description.append(format(" except for:%n"));584 describeIgnoredOverriddenEqualsMethods(description, representation);585 } else {586 description.append(format("%n"));587 }588 }589 private void describeIgnoredOverriddenEqualsMethods(StringBuilder description, Representation representation) {590 if (!ignoredOverriddenEqualsForFields.isEmpty())591 description.append(format("%s the following fields: %s%n", INDENT_LEVEL_2,592 describeIgnoredOverriddenEqualsForFields()));593 if (!ignoredOverriddenEqualsForTypes.isEmpty())594 description.append(format("%s the following types: %s%n", INDENT_LEVEL_2,595 describeIgnoredOverriddenEqualsForTypes(representation)));596 if (!ignoredOverriddenEqualsForFieldsMatchingRegexes.isEmpty())597 description.append(format("%s the types matching the following regexes: %s%n", INDENT_LEVEL_2,598 describeRegexes(ignoredOverriddenEqualsForFieldsMatchingRegexes)));599 }600 private String describeIgnoredOverriddenEqualsForTypes(Representation representation) {601 List<String> fieldsDescription = ignoredOverriddenEqualsForTypes.stream()602 .map(representation::toStringOf)603 .collect(toList());604 return join(fieldsDescription).with(", ");605 }606 private String describeIgnoredOverriddenEqualsForFields() {607 return join(ignoredOverriddenEqualsForFields).with(", ");608 }609 private void describeIgnoreCollectionOrder(StringBuilder description) {610 if (ignoreCollectionOrder) description.append(format("- collection order was ignored in all fields in the comparison%n"));611 }612 private void describeIgnoredCollectionOrderInFields(StringBuilder description) {613 if (!ignoredCollectionOrderInFields.isEmpty())614 description.append(format("- collection order was ignored in the following fields in the comparison: %s%n",615 describeIgnoredCollectionOrderInFields()));616 }617 private void describeIgnoredCollectionOrderInFieldsMatchingRegexes(StringBuilder description) {618 if (!ignoredCollectionOrderInFieldsMatchingRegexes.isEmpty())619 description.append(format("- collection order was ignored in the fields matching the following regexes in the comparison: %s%n",620 describeRegexes(ignoredCollectionOrderInFieldsMatchingRegexes)));621 }622 private boolean matchesAnIgnoredOverriddenEqualsRegex(Class<?> clazz) {623 if (ignoredOverriddenEqualsForFieldsMatchingRegexes.isEmpty()) return false; // shortcut624 String canonicalName = clazz.getCanonicalName();625 return ignoredOverriddenEqualsForFieldsMatchingRegexes.stream()626 .anyMatch(regex -> regex.matcher(canonicalName).matches());627 }628 private boolean matchesAnIgnoredOverriddenEqualsType(Class<?> clazz) {629 return ignoredOverriddenEqualsForTypes.contains(clazz);630 }631 private boolean matchesAnIgnoredOverriddenEqualsField(FieldLocation fieldLocation) {632 return ignoredOverriddenEqualsForFields.stream().anyMatch(fieldLocation::matches);633 }634 private boolean matchesAnIgnoredNullField(DualValue dualValue) {635 return (ignoreAllActualNullFields && dualValue.actual == null)636 || (ignoreAllExpectedNullFields && dualValue.expected == null);637 }638 private boolean matchesAnIgnoredEmptyOptionalField(DualValue dualValue) {639 return ignoreAllActualEmptyOptionalFields640 && dualValue.isActualFieldAnEmptyOptionalOfAnyType();641 }642 private boolean matchesAnIgnoredFieldRegex(FieldLocation fieldLocation) {643 return ignoredFieldsRegexes.stream()644 .anyMatch(regex -> regex.matcher(fieldLocation.getPathToUseInRules()).matches());645 }646 private boolean matchesAnIgnoredFieldType(DualValue dualValue) {647 Object actual = dualValue.actual;648 if (actual != null) return ignoredTypes.contains(actual.getClass());649 Object expected = dualValue.expected;650 // actual is null => we can't evaluate its type, we can only reliably check dualValue.expected's type if651 // strictTypeChecking is enabled which guarantees expected is of the same type.652 if (strictTypeChecking && expected != null) return ignoredTypes.contains(expected.getClass());653 // if strictTypeChecking is disabled, we can't safely ignore the field (if we did, we would ignore all null fields!).654 return false;655 }656 private boolean matchesAnIgnoredField(FieldLocation fieldLocation) {657 return ignoredFields.stream().anyMatch(fieldLocation::matches);658 }659 private boolean matchesAnIgnoredCollectionOrderInField(FieldLocation fieldLocation) {660 return ignoredCollectionOrderInFields.stream().anyMatch(fieldLocation::matches);661 }662 private boolean matchesAnIgnoredCollectionOrderInFieldRegex(FieldLocation fieldLocation) {663 String pathToUseInRules = fieldLocation.getPathToUseInRules();664 return ignoredCollectionOrderInFieldsMatchingRegexes.stream().anyMatch(regex -> regex.matcher(pathToUseInRules).matches());665 }666 private String describeIgnoredFields() {667 return join(ignoredFields).with(", ");668 }669 private String describeIgnoredTypes() {670 List<String> typesDescription = ignoredTypes.stream()671 .map(Class::getName)672 .collect(toList());673 return join(typesDescription).with(", ");674 }675 private String describeIgnoredCollectionOrderInFields() {676 return join(ignoredCollectionOrderInFields).with(", ");677 }678 private String describeRegexes(List<Pattern> regexes) {679 List<String> fieldsDescription = regexes.stream()680 .map(Pattern::pattern)681 .collect(toList());682 return join(fieldsDescription).with(", ");683 }684 private boolean isConfiguredToIgnoreSomeButNotAllOverriddenEqualsMethods() {685 boolean ignoreSomeOverriddenEqualsMethods = !ignoredOverriddenEqualsForFieldsMatchingRegexes.isEmpty()686 || !ignoredOverriddenEqualsForTypes.isEmpty()687 || !ignoredOverriddenEqualsForFields.isEmpty();688 return !ignoreAllOverriddenEquals && ignoreSomeOverriddenEqualsMethods;689 }690 private void describeRegisteredComparatorByTypes(StringBuilder description) {691 if (!typeComparators.isEmpty()) {692 description.append(format("- these types were compared with the following comparators:%n"));693 describeComparatorForTypes(description);694 }695 }696 private void describeComparatorForTypes(StringBuilder description) {697 typeComparators.comparatorByTypes()698 .map(this::formatRegisteredComparatorByType)699 .forEach(description::append);700 }701 private String formatRegisteredComparatorByType(Entry<Class<?>, Comparator<?>> next) {702 return format("%s %s -> %s%n", INDENT_LEVEL_2, next.getKey().getName(), next.getValue());703 }704 private void describeRegisteredComparatorForFields(StringBuilder description) {705 if (!fieldComparators.isEmpty()) {706 description.append(format("- these fields were compared with the following comparators:%n"));707 describeComparatorForFields(description);708 if (!typeComparators.isEmpty()) {709 description.append(format("- field comparators take precedence over type comparators.%n"));710 }711 }712 }713 private void describeComparatorForFields(StringBuilder description) {714 fieldComparators.comparatorByFields()715 .map(this::formatRegisteredComparatorForField)716 .forEach(description::append);717 }718 private String formatRegisteredComparatorForField(Entry<String, Comparator<?>> comparatorForField) {719 return format("%s %s -> %s%n", INDENT_LEVEL_2, comparatorForField.getKey(), comparatorForField.getValue());720 }721 private void describeTypeCheckingStrictness(StringBuilder description) {722 String str = strictTypeChecking...
Source:RecursiveComparisonAssert_fluent_API_Test.java
...43 // THEN44 assertThat(recursiveComparisonConfiguration.isInStrictTypeCheckingMode()).isFalse();45 List<Entry<Class<?>, Comparator<?>>> defaultComparators = defaultTypeComparators().comparatorByTypes().collect(toList());46 assertThat(recursiveComparisonConfiguration.comparatorByTypes()).containsExactlyElementsOf(defaultComparators);47 assertThat(recursiveComparisonConfiguration.comparatorByFields()).isEmpty();48 assertThat(recursiveComparisonConfiguration.getIgnoreAllActualNullFields()).isFalse();49 assertThat(recursiveComparisonConfiguration.getIgnoredFields()).isEmpty();50 assertThat(recursiveComparisonConfiguration.getIgnoredTypes()).isEmpty();51 assertThat(recursiveComparisonConfiguration.getIgnoredFieldsRegexes()).isEmpty();52 assertThat(recursiveComparisonConfiguration.getIgnoredOverriddenEqualsForFields()).isEmpty();53 assertThat(recursiveComparisonConfiguration.getIgnoredOverriddenEqualsForTypes()).isEmpty();54 assertThat(recursiveComparisonConfiguration.getIgnoredOverriddenEqualsForFieldsMatchingRegexes()).isEmpty();55 assertThat(recursiveComparisonConfiguration.hasCustomComparators()).isTrue();56 }57 @Test58 void should_allow_to_enable_strict_mode_comparison() {59 // GIVEN60 RecursiveComparisonConfiguration recursiveComparisonConfiguration = new RecursiveComparisonConfiguration();61 // WHEN62 RecursiveComparisonConfiguration configuration = assertThat(ACTUAL).usingRecursiveComparison(recursiveComparisonConfiguration)63 .withStrictTypeChecking()64 .getRecursiveComparisonConfiguration();65 // THEN66 assertThat(configuration.isInStrictTypeCheckingMode()).isTrue();67 }68 @Test...
Source:FieldComparators.java
...42 }43 /**44 * @return {@code true} is there are registered comparators, {@code false} otherwise45 */46 public boolean isEmpty() {47 return fieldComparators.isEmpty();48 }49 @Override50 public int hashCode() {51 return fieldComparators.hashCode();52 }53 @Override54 public boolean equals(Object obj) {55 return obj instanceof FieldComparators && Objects.equals(fieldComparators, ((FieldComparators) obj).fieldComparators);56 }57 @Override58 public String toString() {59 List<String> registeredComparatorsDescription = new ArrayList<>();60 for (Entry<FieldLocation, Comparator<?>> fieldComparator : this.fieldComparators.entrySet()) {61 registeredComparatorsDescription.add(formatRegisteredComparator(fieldComparator));...
isEmpty
Using AI Code Generation
1import org.assertj.core.api.recursive.comparison.FieldComparators;2public class 1 {3 public static void main(String[] args) {4 FieldComparators comparators = new FieldComparators();5 System.out.println(comparators.isEmpty());6 }7}
isEmpty
Using AI Code Generation
1import org.assertj.core.api.recursive.comparison.FieldComparators;2import org.assertj.core.api.recursive.comparison.FieldLocation;3public class Test {4 public static void main(String[] args) {5 FieldComparators comparators = new FieldComparators();6 FieldLocation location = new FieldLocation("field");7 System.out.println(comparators.isEmpty(location, null));8 }9}10public boolean isEmpty(FieldLocation location, Object value) {11 return value == null || value instanceof Collection && ((Collection<?>) value).isEmpty();12}13public boolean isEmpty(FieldLocation location, Object value) {14 return value == null || value instanceof Collection && ((Collection<?>) value).isEmpty();15}16public boolean isEmpty(FieldLocation location, Object value) {17 return value == null || value instanceof Collection && ((Collection<?>) value).isEmpty();18}19public boolean isEmpty(FieldLocation location, Object value) {20 return value == null || value instanceof Collection && ((Collection<?>) value).isEmpty();21}22public boolean isEmpty(FieldLocation location, Object value) {23 return value == null || value instanceof Collection && ((Collection<?>) value).isEmpty();24}25public boolean isEmpty(FieldLocation location, Object value) {26 return value == null || value instanceof Collection && ((Collection<?>) value).isEmpty();27}28public boolean isEmpty(FieldLocation location, Object value) {29 return value == null || value instanceof Collection && ((Collection<?>) value).isEmpty();30}31public boolean isEmpty(FieldLocation location, Object value) {32 return value == null || value instanceof Collection && ((Collection<?>) value).isEmpty();33}34public boolean isEmpty(FieldLocation location, Object value) {35 return value == null || value instanceof Collection && ((Collection<?>) value).isEmpty();36}
isEmpty
Using AI Code Generation
1import static org.assertj.core.api.Assertions.*;2import org.assertj.core.api.recursive.comparison.FieldComparators;3import org.assertj.core.api.recursive.comparison.FieldComparisonResult;4import org.assertj.core.api.recursive.comparison.FieldLocation;5import org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration;6public class Test {7 public static void main(String[] args) {8 RecursiveComparisonConfiguration config = RecursiveComparisonConfiguration.builder()9 .withComparatorForFields(FieldComparators::isEmpty, "list")10 .build();11 FieldComparisonResult result = config.fieldComparators().compare(new FieldLocation("list"), new FieldLocation("list"));12 System.out.println(result);13 }14}15public static boolean isEmpty(FieldLocation fieldLocation) {16 return fieldLocation.value() instanceof Collection && ((Collection<?>) fieldLocation.value()).isEmpty();17}18public static boolean isNullOrEmpty(FieldLocation fieldLocation) {19 return isEmpty(fieldLocation) || isNull(fieldLocation);20}
isEmpty
Using AI Code Generation
1package com.mycompany.app;2import org.assertj.core.api.recursive.comparison.FieldComparators;3public class App {4 public static void main(String[] args) {5 FieldComparators comparators = new FieldComparators();6 System.out.println(comparators.isEmpty());7 }8}9package com.mycompany.app;10import org.assertj.core.api.recursive.comparison.FieldComparators;11public class App {12 public static void main(String[] args) {13 FieldComparators comparators = new FieldComparators();14 System.out.println(comparators.isEmpty());15 }16}17package com.mycompany.app;18import org.assertj.core.api.recursive.comparison.FieldComparators;19public class App {20 public static void main(String[] args) {21 FieldComparators comparators = new FieldComparators();22 System.out.println(comparators.isEmpty());23 }24}25package com.mycompany.app;26import org.assertj.core.api.recursive.comparison.FieldComparators;27public class App {28 public static void main(String[] args) {29 FieldComparators comparators = new FieldComparators();30 System.out.println(comparators.isEmpty());31 }32}
isEmpty
Using AI Code Generation
1import org.assertj.core.api.recursive.comparison.FieldComparators;2import org.assertj.core.api.recursive.comparison.FieldLocation;3import org.assertj.core.api.recursive.comparison.FieldLocationFactory;4public class Test {5 public static void main(String[] args) {6 FieldLocation fieldLocation = FieldLocationFactory.createFieldLocation("field");7 FieldComparators comparators = new FieldComparators();8 System.out.println(comparators.isEmpty(fieldLocation));9 }10}11import org.assertj.core.internal.FieldComparators;12import org.assertj.core.internal.FieldLocation;13import org.assertj.core.internal.FieldLocationFactory;14public class Test {15 public static void main(String[] args) {16 FieldLocation fieldLocation = FieldLocationFactory.createFieldLocation("field");17 FieldComparators comparators = new FieldComparators();18 System.out.println(comparators.isEmpty(fieldLocation));19 }20}21import org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration;22public class Test {23 public static void main(String[] args) {24 RecursiveComparisonConfiguration configuration = RecursiveComparisonConfiguration.builder()25 .withIgnoredFields("listField")26 .build();27 assertThat(object1).usingRecursiveComparison(configuration).isEqualTo(object2);28 }29}30 <TestObject(name=null, listField=[TestObject(name=null, listField=[]), TestObject(name=null, listField=[])])>
isEmpty
Using AI Code Generation
1import org.assertj.core.api.recursive.comparison.FieldComparators;2import org.assertj.core.api.recursive.comparison.FieldLocation;3import org.assertj.core.api.recursive.comparison.FieldLocationContext;4public class 1 {5 public static void main(String[] args) {6 FieldLocationContext context = new FieldLocationContext("field");7 FieldLocation fieldLocation = new FieldLocation(context, "field");8 System.out.println(FieldComparators.isEmpty(fieldLocation));9 }10}11import org.assertj.core.api.recursive.comparison.FieldComparators;12import org.assertj.core.api.recursive.comparison.FieldLocation;13import org.assertj.core.api.recursive.comparison.FieldLocationContext;14public class 2 {15 public static void main(String[] args) {16 FieldLocationContext context = new FieldLocationContext("field");17 FieldLocation fieldLocation = new FieldLocation(context, "field");18 System.out.println(FieldComparators.isEmpty(fieldLocation));19 }20}21import org.assertj.core.api.recursive.comparison.FieldComparators;22import org.assertj.core.api.recursive.comparison.FieldLocation;23import org.assertj.core.api.recursive.comparison.FieldLocationContext;24public class 3 {25 public static void main(String[] args) {26 FieldLocationContext context = new FieldLocationContext("field");27 FieldLocation fieldLocation = new FieldLocation(context, "field");28 System.out.println(FieldComparators.isEmpty(fieldLocation));29 }30}31import org.assertj.core.api.recursive.comparison.FieldComparators;32import org.assertj.core.api.recursive.comparison.FieldLocation;33import org.assertj.core.api.recursive.comparison.FieldLocationContext;34public class 4 {35 public static void main(String[] args) {
isEmpty
Using AI Code Generation
1import org.assertj.core.api.recursive.comparison.FieldComparators;2import org.junit.Test;3public class 1 {4 public void test1() {5 FieldComparators comparators = new FieldComparators();6 comparators.isEmpty();7 }8}9import org.assertj.core.api.recursive.comparison.FieldComparators;10import org.junit.Test;11public class 2 {12 public void test1() {13 FieldComparators comparators = new FieldComparators();14 comparators.isEmpty();15 }16}17import org.assertj.core.api.recursive.comparison.FieldComparators;18import org.junit.Test;19public class 3 {20 public void test1() {21 FieldComparators comparators = new FieldComparators();22 comparators.isEmpty();23 }24}25import org.assertj.core.api.recursive.comparison.FieldComparators;26import org.junit.Test;27public class 4 {28 public void test1() {29 FieldComparators comparators = new FieldComparators();30 comparators.isEmpty();31 }32}33import org.assertj.core.api.recursive.comparison.FieldComparators;34import org.junit.Test;35public class 5 {36 public void test1() {37 FieldComparators comparators = new FieldComparators();38 comparators.isEmpty();39 }40}41import org.assertj.core.api.recursive.comparison.FieldComparators;42import org.junit.Test;43public class 6 {44 public void test1() {45 FieldComparators comparators = new FieldComparators();46 comparators.isEmpty();47 }48}49import org.assertj.core.api.recursive.comparison.FieldComparators
isEmpty
Using AI Code Generation
1import org.assertj.core.api.recursive.comparison.FieldComparators;2public class 1 {3 public static void main(String[] args) {4 FieldComparators fieldComparators = new FieldComparators();5 boolean result = fieldComparators.isEmpty();6 System.out.println("Is the FieldComparators object empty? : " + result);7 }8}
isEmpty
Using AI Code Generation
1package org.assertj.core.api.recursive.comparison;2import static org.assertj.core.api.Assertions.assertThat;3import java.util.ArrayList;4import java.util.List;5import org.assertj.core.api.recursive.comparison.FieldComparators;6import org.junit.Test;7public class FieldComparatorsTest {8 public void testIsEmptyMethod() {9 List<String> list1 = new ArrayList<String>();10 List<String> list2 = new ArrayList<String>();11 list1.add("one");12 assertThat(list1).usingComparatorForFields(FieldComparators.isEmpty(), "list").isEqualTo(list2);13 }14}15at org.assertj.core.api.recursive.comparison.FieldComparatorsTest.testIsEmptyMethod(FieldComparatorsTest.java:20)16FieldComparators#isNotEmpty() method17FieldComparators#isNullOrEmpty() method18FieldComparators#isNotNullOrEmpty() method19FieldComparators#isNullOrEmptyString() method20FieldComparators#isNotNullOrEmptyString() method21FieldComparators#isNullOrEmptyCollection() method22FieldComparators#isNotNullOrEmptyCollection() method23FieldComparators#isNullOrEmptyMap() method24FieldComparators#isNotNullOrEmptyMap() method25FieldComparators#isNullOrEmptyArray() method26FieldComparators#isNotNullOrEmptyArray() method27FieldComparators#isNullOrEmptyStringInArray() method28FieldComparators#isNotNullOrEmptyStringInArray() method29FieldComparators#isNullOrEmptyStringInCollection() method30FieldComparators#isNotNullOrEmptyStringInCollection() method31FieldComparators#isNullOrEmptyStringInMap() method32FieldComparators#isNotNullOrEmptyStringInMap() method33FieldComparators#isNullOrEmptyStringInIterable() method34FieldComparators#isNotNullOrEmptyStringInIterable() method35FieldComparators#isNullOrEmptyStringInCharSequence() method36FieldComparators#isNotNullOrEmptyStringInCharSequence() method37FieldComparators#isNullOrEmptyStringInString() method38FieldComparators#isNotNullOrEmptyStringInString() method
isEmpty
Using AI Code Generation
1public class 1 {2 public static void main(String[] args) {3 HashMap<String, String> map1 = new HashMap<>();4 HashMap<String, String> map2 = new HashMap<>();5 map1.put("1", "1");6 map2.put("1", "1");7 assertThat(map1).usingComparatorForFields(new FieldComparators().isEmpty(), "1").isEqualTo(map2);8 }9}10public class 2 {11 public static void main(String[] args) {12 ArrayList<String> list1 = new ArrayList<>();13 ArrayList<String> list2 = new ArrayList<>();14 list1.add("1");15 list2.add("1");16 assertThat(list1).usingComparatorForFields(new FieldComparators().isEmpty(), "1").isEqualTo(list2);17 }18}19public class 3 {20 public static void main(String[] args) {21 HashSet<String> set1 = new HashSet<>();22 HashSet<String> set2 = new HashSet<>();23 set1.add("1");24 set2.add("1");25 assertThat(set1).usingComparatorForFields(new FieldComparators().isEmpty(), "1").isEqualTo(set2);26 }27}28public class 4 {29 public static void main(String[] args) {30 LinkedList<String> list1 = new LinkedList<>();31 LinkedList<String> list2 = new LinkedList<>();32 list1.add("1");33 list2.add("1");34 assertThat(list1).usingComparatorForFields(new FieldComparators().isEmpty(), "1").isEqualTo(list2);35 }36}37public class 5 {38 public static void main(String[] args) {
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!