/** * Compares actual and other with standard strategy (null safe equals check). * * @param actual the object to compare to other * @param other the object to compare to actual * @return true if actual and other are equal (null safe equals check), false otherwise. */ private boolean areEqual(Object actual, Object other) { return comparisonStrategy.areEqual(actual, other); }
/** * Delegates to {@link ComparisonStrategy#arrayContains(Object, Object)} */ private boolean arrayContains(Object array, Object value) { return comparisonStrategy.arrayContains(array, value); }
/** * Returns true if actual is after other according to underlying {@link #comparisonStrategy}, false otherwise. * @param actual the {@link Date} to compare to other * @param other the {@link Date} to compare to actual * @return true if actual is after other according to underlying {@link #comparisonStrategy}, false otherwise. */ private boolean isAfter(Date actual, Date other) { return comparisonStrategy.isGreaterThan(actual, other); }
void assertContainsOnlyOnce(AssertionInfo info, Failures failures, Object actual, Object values) { if (commonChecks(info, actual, values)) return; Iterable<?> actualDuplicates = comparisonStrategy.duplicatesFrom(asList(actual)); Set<Object> notFound = new LinkedHashSet<>(); Set<Object> notOnlyOnce = new LinkedHashSet<>(); for (Object expectedElement : asList(values)) { if (!arrayContains(actual, expectedElement)) { notFound.add(expectedElement); } else if (iterableContains(actualDuplicates, expectedElement)) { notOnlyOnce.add(expectedElement); } } if (!notFound.isEmpty() || !notOnlyOnce.isEmpty()) throw failures.failure(info, shouldContainsOnlyOnce(actual, values, notFound, notOnlyOnce, comparisonStrategy)); // assertion succeeded }
/** * Verifies that the actual {@code Class} has the exactly the {@code fields} and nothing more. <b>in any order</b>. * * @param info contains information about the assertion. * @param actual the "actual" {@code Class}. * @param expectedFields all the fields that are expected to be in the class. * @throws AssertionError if {@code actual} is {@code null}. * @throws AssertionError if fields are not all the fields of the actual {@code Class}. */ public void assertHasOnlyDeclaredFields(AssertionInfo info, Class<?> actual, String... expectedFields) { assertNotNull(info, actual); Set<String> actualFieldNames = fieldsToName(filterSyntheticMembers(actual.getDeclaredFields())); List<String> notExpected = newArrayList(actualFieldNames); List<String> notFound = newArrayList(expectedFields); if (expectedFields.length == 0) { if (actualFieldNames.isEmpty()) return; throw failures.failure(info, shouldHaveNoDeclaredFields(actual, actualFieldNames)); } for (String field : expectedFields) { if (comparisonStrategy.iterableContains(notExpected, field)) { comparisonStrategy.iterablesRemoveFirst(notExpected, field); comparisonStrategy.iterablesRemoveFirst(notFound, field); } } if (notExpected.isEmpty() && notFound.isEmpty()) return; throw failures.failure(info, shouldOnlyHaveDeclaredFields(actual, newArrayList(expectedFields), notFound, notExpected)); }
/** * Delegates to {@link ComparisonStrategy#iterableContains(Iterable, Object)} */ private boolean iterableContains(Iterable<?> actual, Object value) { return comparisonStrategy.iterableContains(actual, value); }
protected <T extends Comparable<? super T>> void checkBoundsValidity(T start, T end, boolean inclusiveStart, boolean inclusiveEnd) { // don't use isLessThanOrEqualTo or isGreaterThanOrEqualTo to avoid equal comparison which makes BigDecimal // to fail when start = end with different precision, ex: [10.0, 10.00]. boolean inclusiveBoundsCheck = inclusiveEnd && inclusiveStart && !isGreaterThan(start, end); boolean strictBoundsCheck = !inclusiveEnd && !inclusiveStart && isLessThan(start, end); String operator = inclusiveEnd && inclusiveStart ? "less than" : "less than or equal to"; String boundsCheckErrorMessage = format("The end value <%s> must not be %s the start value <%s>%s!", end, operator, start, (comparisonStrategy.isStandard() ? "" : " (using " + comparisonStrategy + ")")); checkArgument(inclusiveBoundsCheck || strictBoundsCheck, boundsCheckErrorMessage); } }
/** * Delegates to {@link ComparisonStrategy#iterableRemoves(Iterable, Object)} */ private void iterablesRemove(Iterable<?> actual, Object value) { comparisonStrategy.iterableRemoves(actual, value); }
/** * Returns true if actual is before other according to underlying {@link #comparisonStrategy}, false otherwise. * @param actual the {@link Date} to compare to other * @param other the {@link Date} to compare to actual * @return true if actual is before other according to underlying {@link #comparisonStrategy}, false otherwise. */ private boolean isBefore(Date actual, Date other) { return comparisonStrategy.isLessThan(actual, other); }
/** * Returns <code>true</code> if the actual {@code Date} is before or equal to the given one according to underlying * {@link #comparisonStrategy}, false otherwise. * @param actual the actual date - must not be null. * @param other the given Date. * @return <code>true</code> if the actual {@code Date} is before or equal to the given one according to underlying * {@link #comparisonStrategy}, false otherwise. * @throws NullPointerException if {@code actual} is {@code null}. */ private boolean isBeforeOrEqualTo(Date actual, Date other) { return comparisonStrategy.isLessThanOrEqualTo(actual, other); }
/** * Returns <code>true</code> if the actual {@code Date} is after or equal to the given one according to underlying * {@link #comparisonStrategy}, false otherwise. * @param actual the actual date - must not be null. * @param other the given Date. * @return <code>true</code> if the actual {@code Date} is after or equal to the given one according to underlying * {@link #comparisonStrategy}, false otherwise. * @throws NullPointerException if {@code actual} is {@code null}. */ private boolean isAfterOrEqualTo(Date actual, Date other) { return comparisonStrategy.isGreaterThanOrEqualTo(actual, other); }
/** * Asserts that the given {@code Iterable} contains the given values and only once. * * @param info contains information about the assertion. * @param actual the given {@code Iterable}. * @param values the values that are expected to be in the given {@code Iterable}. * @throws NullPointerException if the array of values is {@code null}. * @throws IllegalArgumentException if the array of values is empty. * @throws AssertionError if the given {@code Iterable} is {@code null}. * @throws AssertionError if the given {@code Iterable} does not contain the given values or if the given * {@code Iterable} contains values that are not in the given array. */ public void assertContainsOnlyOnce(AssertionInfo info, Iterable<?> actual, Object[] values) { if (commonCheckThatIterableAssertionSucceeds(info, actual, values)) return; // check for elements in values that are missing in actual. Set<Object> notFound = new LinkedHashSet<>(); Set<Object> notOnlyOnce = new LinkedHashSet<>(); Iterable<?> actualDuplicates = comparisonStrategy.duplicatesFrom(actual); for (Object expectedOnlyOnce : values) { if (!iterableContains(actual, expectedOnlyOnce)) { notFound.add(expectedOnlyOnce); } else if (iterableContains(actualDuplicates, expectedOnlyOnce)) { notOnlyOnce.add(expectedOnlyOnce); } } if (!notFound.isEmpty() || !notOnlyOnce.isEmpty()) throw failures.failure(info, shouldContainsOnlyOnce(actual, values, notFound, notOnlyOnce, comparisonStrategy)); // assertion succeeded }
/** * Verifies that the actual {@code Class} has only the {@code fields} and nothing more. <b>in any order</b>. * * @param info contains information about the assertion. * @param actual the "actual" {@code Class}. * @param expectedFields all the fields that are expected to be in the class. * @throws AssertionError if {@code actual} is {@code null}. * @throws AssertionError if fields are not all the fields of the actual {@code Class}. */ public void assertHasOnlyPublicFields(AssertionInfo info, Class<?> actual, String... expectedFields) { assertNotNull(info, actual); Set<String> actualFieldNames = fieldsToName(filterSyntheticMembers(actual.getFields())); List<String> notExpected = newArrayList(actualFieldNames); List<String> notFound = newArrayList(expectedFields); if (expectedFields.length == 0) { if (actualFieldNames.isEmpty()) return; throw failures.failure(info, shouldHaveNoPublicFields(actual, actualFieldNames)); } for (String field : expectedFields) { if (comparisonStrategy.iterableContains(notExpected, field)) { comparisonStrategy.iterablesRemoveFirst(notExpected, field); comparisonStrategy.iterablesRemoveFirst(notFound, field); } } if (notExpected.isEmpty() && notFound.isEmpty()) return; throw failures.failure(info, shouldOnlyHaveFields(actual, newArrayList(expectedFields), notFound, notExpected)); }
/** * Delegates to {@link ComparisonStrategy#iterableContains(Iterable, Object)} */ private boolean iterableContains(Iterable<?> actual, Object value) { return comparisonStrategy.iterableContains(actual, value); }
protected <T extends Comparable<? super T>> void checkBoundsValidity(T start, T end, boolean inclusiveStart, boolean inclusiveEnd) { // don't use isLessThanOrEqualTo or isGreaterThanOrEqualTo to avoid equal comparison which makes BigDecimal // to fail when start = end with different precision, ex: [10.0, 10.00]. boolean inclusiveBoundsCheck = inclusiveEnd && inclusiveStart && !isGreaterThan(start, end); boolean strictBoundsCheck = !inclusiveEnd && !inclusiveStart && isLessThan(start, end); String operator = inclusiveEnd && inclusiveStart ? "less than" : "less than or equal to"; String boundsCheckErrorMessage = format("The end value <%s> must not be %s the start value <%s>%s!", end, operator, start, (comparisonStrategy.isStandard() ? "" : " (using " + comparisonStrategy + ")")); checkArgument(inclusiveBoundsCheck || strictBoundsCheck, boundsCheckErrorMessage); } }
/** * Delegates to {@link ComparisonStrategy#iterableRemoves(Iterable, Object)} */ private void iterableRemoves(Collection<?> actual, Object value) { comparisonStrategy.iterableRemoves(actual, value); }
private boolean isLessThan(Object actual, Object other) { return comparisonStrategy.isLessThan(actual, other); }
/** * Returns <code>true</code> if the actual {@code Date} is before or equal to the given one according to underlying * {@link #comparisonStrategy}, false otherwise. * @param actual the actual date - must not be null. * @param other the given Date. * @return <code>true</code> if the actual {@code Date} is before or equal to the given one according to underlying * {@link #comparisonStrategy}, false otherwise. * @throws NullPointerException if {@code actual} is {@code null}. */ private boolean isBeforeOrEqualTo(Date actual, Date other) { return comparisonStrategy.isLessThanOrEqualTo(actual, other); }
/** * Returns <code>true</code> if the actual {@code Date} is after or equal to the given one according to underlying * {@link #comparisonStrategy}, false otherwise. * @param actual the actual date - must not be null. * @param other the given Date. * @return <code>true</code> if the actual {@code Date} is after or equal to the given one according to underlying * {@link #comparisonStrategy}, false otherwise. * @throws NullPointerException if {@code actual} is {@code null}. */ private boolean isAfterOrEqualTo(Date actual, Date other) { return comparisonStrategy.isGreaterThanOrEqualTo(actual, other); }
/** * Returns true if the actual {@code Date} is equal to the given one according to underlying {@link #comparisonStrategy}, false * otherwise. * @param actual the actual date - must not be null. * @param other the given Date. * @return <code>true</code> if the actual {@code Date} is equal to the given one according to underlying * {@link #comparisonStrategy}, false otherwise. */ private boolean areEqual(Date actual, Date other) { return comparisonStrategy.areEqual(actual, other); }