/** * Asserts the given <code>{@link Condition}</code> is not null. * @param condition the given {@code Condition}. * @throws NullPointerException if the given {@code Condition} is {@code null}. */ public void assertIsNotNull(Condition<?> condition) { assertIsNotNull(condition, "The condition to evaluate should not be null"); }
/** * Asserts the given <code>{@link Condition}</code> is not null. * @param condition the given {@code Condition}. * @throws NullPointerException if the given {@code Condition} is {@code null}. */ public void assertIsNotNull(Condition<?> condition) { assertIsNotNull(condition, "The condition to evaluate should not be null"); }
private void assertNotNull(Condition<?> condition) { Conditions.instance().assertIsNotNull(condition); }
/** * Asserts that the actual value does not satisfy the given <code>{@link Condition}</code>. * @param <T> the type of the actual value and the type of values that given {@code Condition} takes. * @param info contains information about the assertion. * @param actual the actual value. * @param condition the given {@code Condition}. * @throws NullPointerException if the given {@code Condition} is {@code null}. * @throws AssertionError if the actual value satisfies the given {@code Condition}. */ public <T> void assertIsNot(AssertionInfo info, T actual, Condition<? super T> condition) { assertIsNotNull(condition); if (condition.matches(actual)) throw failures.failure(info, shouldNotBe(actual, condition)); }
/** * Asserts that the actual value satisfies the given <code>{@link Condition}</code>. * @param <T> the type of the actual value and the type of values that given {@code Condition} takes. * @param info contains information about the assertion. * @param actual the actual value. * @param condition the given {@code Condition}. * @throws NullPointerException if the given {@code Condition} is {@code null}. * @throws AssertionError if the actual value does not satisfy the given {@code Condition}. */ public <T> void assertHas(AssertionInfo info, T actual, Condition<? super T> condition) { assertIsNotNull(condition); if (!condition.matches(actual)) throw failures.failure(info, shouldHave(actual, condition)); }
/** * Asserts that the actual value does not satisfy the given <code>{@link Condition}</code>. * @param <T> the type of the actual value and the type of values that given {@code Condition} takes. * @param info contains information about the assertion. * @param actual the actual value. * @param condition the given {@code Condition}. * @throws NullPointerException if the given {@code Condition} is {@code null}. * @throws AssertionError if the actual value satisfies the given {@code Condition}. */ public <T> void assertDoesNotHave(AssertionInfo info, T actual, Condition<? super T> condition) { assertIsNotNull(condition); if (condition.matches(actual)) throw failures.failure(info, shouldNotHave(actual, condition)); }
/** * Asserts that the actual value satisfies the given <code>{@link Condition}</code>. * @param <T> the type of the actual value and the type of values that given {@code Condition} takes. * @param info contains information about the assertion. * @param actual the actual value. * @param condition the given {@code Condition}. * @throws NullPointerException if the given {@code Condition} is {@code null}. * @throws AssertionError if the actual value does not satisfy the given {@code Condition}. */ public <T> void assertIs(AssertionInfo info, T actual, Condition<? super T> condition) { assertIsNotNull(condition); if (!condition.matches(actual)) throw failures.failure(info, shouldBe(actual, condition)); }
public <T> void assertSatisfies(AssertionInfo info, T actual, Condition<? super T> condition) { assertIsNotNull(condition); if (!condition.matches(actual)) throw failures.failure(info, shouldSatisfy(actual, condition)); }
@SuppressWarnings("unchecked") private <E> List<E> filterElements(AssertionInfo info, Failures failures, Conditions conditions, Object array, Condition<E> condition, boolean negateCondition) throws AssertionError { assertNotNull(info, array); conditions.assertIsNotNull(condition); try { List<E> filteredElements = new LinkedList<>(); int arraySize = sizeOf(array); for (int i = 0; i < arraySize; i++) { E element = (E) Array.get(array, i); if (negateCondition ? !condition.matches(element) : condition.matches(element)) filteredElements.add(element); } return filteredElements; } catch (ClassCastException e) { throw failures.failure(info, shouldBeSameGenericBetweenIterableAndCondition(array, condition)); } }
/** * Asserts that the actual value satisfies the given <code>{@link Condition}</code>. * @param <T> the type of the actual value and the type of values that given {@code Condition} takes. * @param info contains information about the assertion. * @param actual the actual value. * @param condition the given {@code Condition}. * @throws NullPointerException if the given {@code Condition} is {@code null}. * @throws AssertionError if the actual value does not satisfy the given {@code Condition}. */ public <T> void assertIs(AssertionInfo info, T actual, Condition<? super T> condition) { assertIsNotNull(condition); if (!condition.matches(actual)) throw failures.failure(info, shouldBe(actual, condition)); }
/** * Verifies that the given {@code Map} contains the value for given {@code key} that satisfy given {@code valueCondition}. * * @param <K> key type * @param <V> value type * @param info contains information about the assertion. * @param actual the given {@code Map}. * @param key he given key to check. * @param valueCondition the given condition for check value. * @throws NullPointerException if the given values is {@code null}. * @throws AssertionError if the actual map is {@code null}. * @throws AssertionError if the actual map not contains the given {@code key}. * @throws AssertionError if the actual map contains the given key, but value not match the given {@code valueCondition}. * @since 2.6.0 / 3.6.0 */ @SuppressWarnings("unchecked") public <K, V> void assertHasEntrySatisfying(AssertionInfo info, Map<K, V> actual, K key, Condition<? super V> valueCondition) { assertContainsKeys(info, actual, key); conditions.assertIsNotNull(valueCondition); V value = actual.get(key); if (!valueCondition.matches(value)) throw failures.failure(info, elementsShouldBe(actual, value, valueCondition)); }
/** * Asserts that the actual value satisfies the given <code>{@link Condition}</code>. * @param <T> the type of the actual value and the type of values that given {@code Condition} takes. * @param info contains information about the assertion. * @param actual the actual value. * @param condition the given {@code Condition}. * @throws NullPointerException if the given {@code Condition} is {@code null}. * @throws AssertionError if the actual value does not satisfy the given {@code Condition}. */ public <T> void assertHas(AssertionInfo info, T actual, Condition<? super T> condition) { assertIsNotNull(condition); if (!condition.matches(actual)) throw failures.failure(info, shouldHave(actual, condition)); }
/** * Verifies that the given {@code Map} contains an entry with key satisfying {@code keyCondition}. * * @param <K> key type * @param info contains information about the assertion. * @param actual the given {@code Map}. * @param keyCondition the condition for key search. * @throws NullPointerException if the given condition is {@code null}. * @throws AssertionError if the actual map is {@code null}. * @throws AssertionError if there is no key matching given {@code keyCondition}. * @since 2.7.0 / 3.7.0 */ public <K> void assertHasKeySatisfying(AssertionInfo info, Map<K, ?> actual, Condition<? super K> keyCondition) { assertNotNull(info, actual); conditions.assertIsNotNull(keyCondition); for (K key : actual.keySet()) { if (keyCondition.matches(key)) return; } throw failures.failure(info, shouldContainKey(actual, keyCondition)); }
/** * Verifies that the given {@code Map} contains an entry with value satisfying {@code valueCondition}. * * @param <V> value type * @param info contains information about the assertion. * @param actual the given {@code Map}. * @param valueCondition the condition for value search. * @throws NullPointerException if the given condition is {@code null}. * @throws AssertionError if the actual map is {@code null}. * @throws AssertionError if there is no value matching given {@code valueCondition}. * @since 2.7.0 / 3.7.0 */ public <V> void assertHasValueSatisfying(AssertionInfo info, Map<?, V> actual, Condition<? super V> valueCondition) { assertNotNull(info, actual); conditions.assertIsNotNull(valueCondition); for (V value : actual.values()) { if (valueCondition.matches(value)) return; } throw failures.failure(info, shouldContainValue(actual, valueCondition)); }
/** * Asserts that the actual value does not satisfy the given <code>{@link Condition}</code>. * @param <T> the type of the actual value and the type of values that given {@code Condition} takes. * @param info contains information about the assertion. * @param actual the actual value. * @param condition the given {@code Condition}. * @throws NullPointerException if the given {@code Condition} is {@code null}. * @throws AssertionError if the actual value satisfies the given {@code Condition}. */ public <T> void assertDoesNotHave(AssertionInfo info, T actual, Condition<? super T> condition) { assertIsNotNull(condition); if (condition.matches(actual)) throw failures.failure(info, shouldNotHave(actual, condition)); }
/** * Assert that each element of given {@code Iterable} satisfies the given condition. * * @param <E> the type of actual elements * @param info contains information about the assertion. * @param actual the given {@code Iterable}. * @param condition the given {@code Condition}. * @throws NullPointerException if the given condition is {@code null}. * @throws AssertionError if an element cannot be cast to E. * @throws AssertionError if one or more elements do not satisfy the given condition. */ public <E> void assertHave(AssertionInfo info, Iterable<? extends E> actual, Condition<? super E> condition) { assertNotNull(info, actual); conditions.assertIsNotNull(condition); try { List<E> notSatisfiesCondition = notSatisfyingCondition(actual, condition); if (!notSatisfiesCondition.isEmpty()) throw failures.failure(info, elementsShouldHave(actual, notSatisfiesCondition, condition)); } catch (ClassCastException e) { throw failures.failure(info, shouldBeSameGenericBetweenIterableAndCondition(actual, condition)); } }
/** * Assert that each element of given {@code Iterable} not satisfies the given condition. * * @param <E> the type of actual elements * @param info contains information about the assertion. * @param actual the given {@code Iterable}. * @param condition the given {@code Condition}. * @throws NullPointerException if the given condition is {@code null}. * @throws AssertionError if an element cannot be cast to E. * @throws AssertionError if one or more elements satisfy the given condition. */ public <E> void assertDoNotHave(AssertionInfo info, Iterable<? extends E> actual, Condition<? super E> condition) { assertNotNull(info, actual); conditions.assertIsNotNull(condition); try { List<E> satisfiesCondition = satisfiesCondition(actual, condition); if (!satisfiesCondition.isEmpty()) throw failures.failure(info, elementsShouldNotHave(actual, satisfiesCondition, condition)); } catch (ClassCastException e) { throw failures.failure(info, shouldBeSameGenericBetweenIterableAndCondition(actual, condition)); } }
/** * Verifies that there are <b>exactly</b> <i>n</i> elements in the actual {@code Iterable} satisfying the given * condition. * * @param <E> the type of actual elements * @param info contains information about the assertion. * @param actual the given {@code Iterable}. * @param times the exact number of times the condition should be verified. * @param condition the given {@code Condition}. * @throws NullPointerException if the given condition is {@code null}. * @throws AssertionError if an element cannot be cast to E. * @throws AssertionError if the number of elements satisfying the given condition is ≠ n. */ public <E> void assertAreExactly(AssertionInfo info, Iterable<? extends E> actual, int times, Condition<? super E> condition) { assertNotNull(info, actual); conditions.assertIsNotNull(condition); try { if (!conditionIsSatisfiedNTimes(actual, condition, times)) throw failures.failure(info, elementsShouldBeExactly(actual, times, condition)); } catch (ClassCastException e) { throw failures.failure(info, shouldBeSameGenericBetweenIterableAndCondition(actual, condition)); } }
/** * An alias method of {@link #assertAreAtMost(AssertionInfo, Iterable, int, Condition)} to provide a richer fluent api * (same logic, only error message differs). * * @param <E> the type of actual elements * @param info contains information about the assertion. * @param actual the given {@code Iterable}. * @param times the number of times the condition should be at most verified. * @param condition the given {@code Condition}. * @throws NullPointerException if the given condition is {@code null}. * @throws AssertionError if an element cannot be cast to E. * @throws AssertionError if the number of elements satisfying the given condition is > n. */ public <E> void assertHaveAtMost(AssertionInfo info, Iterable<? extends E> actual, int times, Condition<? super E> condition) { assertNotNull(info, actual); conditions.assertIsNotNull(condition); try { if (!conditionIsSatisfiedAtMostNTimes(actual, condition, times)) throw failures.failure(info, elementsShouldHaveAtMost(actual, times, condition)); } catch (ClassCastException e) { throw failures.failure(info, shouldBeSameGenericBetweenIterableAndCondition(actual, condition)); } }
/** * Assert that there are <b>at most</b> <i>n</i> elements in the actual {@code Iterable} satisfying the given * condition. * * @param <E> the type of actual elements * @param info contains information about the assertion. * @param actual the given {@code Iterable}. * @param n the number of times the condition should be at most verified. * @param condition the given {@code Condition}. * @throws NullPointerException if the given condition is {@code null}. * @throws AssertionError if an element cannot be cast to E. * @throws AssertionError if the number of elements satisfying the given condition is > n. */ public <E> void assertAreAtMost(AssertionInfo info, Iterable<? extends E> actual, int n, Condition<? super E> condition) { assertNotNull(info, actual); conditions.assertIsNotNull(condition); try { if (!conditionIsSatisfiedAtMostNTimes(actual, condition, n)) throw failures.failure(info, elementsShouldBeAtMost(actual, n, condition)); } catch (ClassCastException e) { throw failures.failure(info, shouldBeSameGenericBetweenIterableAndCondition(actual, condition)); } }