Refine search
public <E> void assertHasAtLeastOneElementOfType(AssertionInfo info, E[] actual, Class<?> type) { Objects.instance().assertNotNull(info, actual); boolean found = false; for (Object o : actual) { if (!type.isInstance(o)) continue; found = true; break; } if (!found) throw failures.failure(info, shouldHaveAtLeastOneElementOfType(actual, type)); }
public void assertHasToString(AssertionInfo info, Object actual, String expectedToString) { assertNotNull(info, actual); if (!actual.toString().equals(expectedToString)) throw failures.failure(info, shouldHaveToString(actual, expectedToString)); }
public <E> void assertHasOnlyElementsOfType(AssertionInfo info, E[] actual, Class<?> type) { Objects.instance().assertNotNull(info, actual); for (Object o : actual) { if (!type.isInstance(o)) throw failures.failure(info, shouldHaveOnlyElementsOfType(actual, type, o.getClass())); } }
public void assertHasToString(AssertionInfo info, Object actual, String expectedToString) { assertNotNull(info, actual); if (!actual.toString().equals(expectedToString)) throw failures.failure(info, shouldHaveToString(actual, expectedToString)); }
public <E> void assertDoesNotHaveAnyElementsOfTypes(AssertionInfo info, E[] actual, Class<?>... unexpectedTypes) { Objects.instance().assertNotNull(info, actual); Map<Class<?>, List<Object>> nonMatchingElementsByType = new LinkedHashMap<>(); for (E element : actual) { for (Class<?> type : unexpectedTypes) { if (type.isInstance(element)) { if (!nonMatchingElementsByType.containsKey(type)) { nonMatchingElementsByType.put(type, new ArrayList<>()); } nonMatchingElementsByType.get(type).add(element); } } } if (!nonMatchingElementsByType.isEmpty()) { throw failures.failure(info, shouldNotHaveAnyElementsOfTypes(actual, unexpectedTypes, nonMatchingElementsByType)); } }
public <A> void assertHasFieldOrProperty(AssertionInfo info, A actual, String name) { assertNotNull(info, actual); try { extractPropertyOrField(actual, name); } catch (IntrospectionError error) { throw failures.failure(info, shouldHavePropertyOrField(actual, name)); } }
public <E> void assertHasOnlyElementsOfType(AssertionInfo info, E[] actual, Class<?> type) { Objects.instance().assertNotNull(info, actual); for (Object element : actual) { if (!type.isInstance(element)) { throw failures.failure(info, shouldHaveOnlyElementsOfType(actual, type, element == null ? null : element.getClass())); } } }
/** * Asserts that the actual object has the same hashCode as the given object. * * @param <A> the actual type * @param info contains information about the assertion. * @param actual the given object. * @param other the object to check hashCode against. * * @throws AssertionError if the actual object is null. * @throws AssertionError if the given object is null. * @throws AssertionError if the actual object has not the same hashCode as the given object. */ public <A> void assertHasSameHashCodeAs(AssertionInfo info, A actual, Object other) { assertNotNull(info, actual); checkNotNull(other, "The object used to compare actual's hash code with should not be null"); if (actual.hashCode() != other.hashCode()) throw failures.failure(info, shouldHaveSameHashCode(actual, other)); }
public <E> void assertHasAtLeastOneElementOfType(AssertionInfo info, E[] actual, Class<?> type) { Objects.instance().assertNotNull(info, actual); boolean found = false; for (Object o : actual) { if (!type.isInstance(o)) continue; found = true; break; } if (!found) throw failures.failure(info, shouldHaveAtLeastOneElementOfType(actual, type)); }
public <A> void assertHasFieldOrProperty(AssertionInfo info, A actual, String name) { assertNotNull(info, actual); try { extractPropertyOrField(actual, name); } catch (IntrospectionError error) { throw failures.failure(info, shouldHavePropertyOrField(actual, name)); } }
public <E> void assertDoesNotHaveAnyElementsOfTypes(AssertionInfo info, E[] actual, Class<?>... unexpectedTypes) { Objects.instance().assertNotNull(info, actual); Map<Class<?>, List<Object>> nonMatchingElementsByType = new LinkedHashMap<>(); for (E element : actual) { for (Class<?> type : unexpectedTypes) { if (type.isInstance(element)) { if (!nonMatchingElementsByType.containsKey(type)) { nonMatchingElementsByType.put(type, new ArrayList<>()); } nonMatchingElementsByType.get(type).add(element); } } } if (!nonMatchingElementsByType.isEmpty()) { throw failures.failure(info, shouldNotHaveAnyElementsOfTypes(actual, unexpectedTypes, nonMatchingElementsByType)); } }
/** * Asserts that the actual object has the same hashCode as the given object. * * @param <A> the actual type * @param info contains information about the assertion. * @param actual the given object. * @param other the object to check hashCode against. * * @throws AssertionError if the actual object is null. * @throws AssertionError if the given object is null. * @throws AssertionError if the actual object has not the same hashCode as the given object. */ public <A> void assertHasSameHashCodeAs(AssertionInfo info, A actual, Object other) { assertNotNull(info, actual); checkNotNull(other, "The object used to compare actual's hash code with should not be null"); if (actual.hashCode() != other.hashCode()) throw failures.failure(info, shouldHaveSameHashCode(actual, other)); }
/** * Verifies that the actual {@code LocalDate} is today, that is matching current year, month and day. * <p> * Example: * <pre><code class='java'> // assertion will pass * assertThat(LocalDate.now()).isToday(); * * // assertion will fail * assertThat(theFellowshipOfTheRing.getReleaseDate()).isToday();</code></pre> * * @return this assertion object. * @throws AssertionError if the actual {@code LocalDate} is {@code null}. * @throws AssertionError if the actual {@code LocalDate} is not today. */ public SELF isToday() { Objects.instance().assertNotNull(info, actual); if (!actual.isEqual(LocalDate.now())) throw Failures.instance().failure(info, shouldBeToday(actual)); return myself; }
/** * Assert that the given object is "deeply" equals to other by comparing all fields recursively. * * @param <A> the actual type * @param info contains information about the assertion. * @param actual the given object. * @param comparatorByPropertyOrField comparators use for specific fields * @param comparatorByType comparators use for specific types * @param other the object to compare {@code actual} to. * @throws AssertionError if actual is {@code null}. * @throws AssertionError if the actual and the given object are not "deeply" equal. */ public <A> void assertIsEqualToComparingFieldByFieldRecursively(AssertionInfo info, Object actual, Object other, Map<String, Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType) { assertNotNull(info, actual); List<Difference> differences = determineDifferences(actual, other, comparatorByPropertyOrField, comparatorByType); if (!differences.isEmpty()) { throw failures.failure(info, shouldBeEqualByComparingFieldByFieldRecursive(actual, other, differences, info.representation())); } }
/** * Verifies that the actual {@code LocalDate} is today, that is matching current year, month and day. * <p> * Example: * <pre><code class='java'> // assertion will pass * assertThat(LocalDate.now()).isToday(); * * // assertion will fail * assertThat(theFellowshipOfTheRing.getReleaseDate()).isToday();</code></pre> * * @return this assertion object. * @throws AssertionError if the actual {@code LocalDate} is {@code null}. * @throws AssertionError if the actual {@code LocalDate} is not today. */ public SELF isToday() { Objects.instance().assertNotNull(info, actual); if (!actual.isEqual(LocalDate.now())) throw Failures.instance().failure(info, shouldBeToday(actual)); return myself; }
Map<String, Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType) { assertNotNull(info, actual); List<String> fieldsNames = new LinkedList<>(); List<Object> rejectedValues = new LinkedList<>(); throw failures.failure(info, shouldBeEqualToIgnoringGivenFields(actual, fieldsNames, rejectedValues, expectedValues, nullFields));
/** * Asserts that a sequence of the message of the actual {@code Throwable} matches with the given regular expression (see {@link java.util.regex.Matcher#find()}). * The Pattern used under the hood enables the {@link Pattern#DOTALL} mode. * * @param info contains information about the assertion. * @param actual the given {@code Throwable}. * @param regex the regular expression expected to be found in the actual {@code Throwable}'s message. * @throws AssertionError if the actual {@code Throwable} is {@code null}. * @throws AssertionError if the message of the actual {@code Throwable} doesn't contain any sequence matching with the given regular expression * @throws NullPointerException if the regex is null */ public void assertHasMessageFindingMatch(AssertionInfo info, Throwable actual, String regex) { checkNotNull(regex, "regex must not be null"); assertNotNull(info, actual); Objects.instance().assertNotNull(info, actual.getMessage(), "exception message of actual"); if (Pattern.compile(regex, Pattern.DOTALL).asPredicate().test(actual.getMessage())) return; throw failures.failure(info, shouldHaveMessageFindingMatchRegex(actual, regex)); }
/** * Assert that the given object has no null fields except the given ones. * * @param <A> the actual type * @param info contains information about the assertion. * @param actual the given object. * @param propertiesOrFieldsToIgnore the fields to ignore in comparison * @throws AssertionError if actual is {@code null}. * @throws AssertionError if some of the fields of the actual object are null. */ public <A> void assertHasNoNullFieldsOrPropertiesExcept(AssertionInfo info, A actual, String... propertiesOrFieldsToIgnore) { assertNotNull(info, actual); Set<Field> declaredFieldsIncludingInherited = getDeclaredFieldsIncludingInherited(actual.getClass()); List<String> nullFieldNames = new LinkedList<>(); Set<String> ignoredFields = newLinkedHashSet(propertiesOrFieldsToIgnore); 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); if (actualFieldValue == null) nullFieldNames.add(fieldName); } if (!nullFieldNames.isEmpty()) throw failures.failure(info, shouldHaveNoNullFieldsExcept(actual, nullFieldNames, newArrayList(propertiesOrFieldsToIgnore))); }
/** * Verifies that the actual {@code LocalDate} is <b>strictly</b> before the given one. * <p> * Example : * <pre><code class='java'> assertThat(parse("2000-01-01")).isBefore(parse("2000-01-02"));</code></pre> * * @param other the given {@link LocalDate}. * @return this assertion object. * @throws AssertionError if the actual {@code LocalDate} is {@code null}. * @throws IllegalArgumentException if other {@code LocalDate} is {@code null}. * @throws AssertionError if the actual {@code LocalDate} is not strictly before the given one. */ public SELF isBefore(LocalDate other) { Objects.instance().assertNotNull(info, actual); assertLocalDateParameterIsNotNull(other); if (!actual.isBefore(other)) throw Failures.instance().failure(info, shouldBeBefore(actual, other)); return myself; }
/** * 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))); }