/** * {@inheritDoc} */ @Override public SELF contains(@SuppressWarnings("unchecked") ELEMENT... values) { iterables.assertContains(info, actual, values); return myself; }
private <T> boolean conditionIsMetAtIndex(AssertionInfo info, List<T> actual, Condition<? super T> condition, Index index) { assertNotNull(info, actual); assertNotNull(condition); Iterables.instance().assertNotEmpty(info, actual); checkIndexValueIsValid(index, actual.size() - 1); return condition.matches(actual.get(index.value)); }
/** * {@inheritDoc} */ @Override public SELF containsOnly(@SuppressWarnings("unchecked") ELEMENT... values) { iterables.assertContainsOnly(info, actual, values); return myself; }
/** * Verifies that the given {@code Iterable} starts with the given sequence of objects, without any other objects * between them. Similar to <code>{@link #assertContainsSequence(AssertionInfo, Iterable, Object[])}</code>, but it * also verifies that the first element in the sequence is also the first element of the given {@code Iterable}. * * @param info contains information about the assertion. * @param actual the given {@code Iterable}. * @param sequence the sequence of objects to look for. * @throws NullPointerException if the given argument is {@code null}. * @throws IllegalArgumentException if the given argument is an empty array. * @throws AssertionError if the given {@code Iterable} is {@code null}. * @throws AssertionError if the given {@code Iterable} does not start with the given sequence of objects. */ public void assertStartsWith(AssertionInfo info, Iterable<?> actual, Object[] sequence) { if (commonCheckThatIterableAssertionSucceeds(info, actual, sequence)) return; int i = 0; for (Object actualCurrentElement : actual) { if (i >= sequence.length) break; if (areEqual(actualCurrentElement, sequence[i++])) continue; throw actualDoesNotStartWithSequence(info, actual, sequence); } if (sequence.length > i) { // sequence has more elements than actual throw actualDoesNotStartWithSequence(info, actual, sequence); } }
/** * Verifies that the given {@code Iterable} ends with the given sequence of objects, without any other objects between * them. Similar to <code>{@link #assertContainsSequence(AssertionInfo, Iterable, Object[])}</code>, but it also * verifies that the last element in the sequence is also the last element of the given {@code Iterable}. * * @param info contains information about the assertion. * @param actual the given {@code Iterable}. * @param sequence the sequence of objects to look for. * @throws NullPointerException if the given argument is {@code null}. * @throws IllegalArgumentException if the given argument is an empty array. * @throws AssertionError if the given {@code Iterable} is {@code null}. * @throws AssertionError if the given {@code Iterable} does not end with the given sequence of objects. */ public void assertEndsWith(AssertionInfo info, Iterable<?> actual, Object[] sequence) { checkNotNullIterables(info, actual, sequence); int sizeOfActual = sizeOf(actual); if (sizeOfActual < sequence.length) throw actualDoesNotEndWithSequence(info, actual, sequence); int start = sizeOfActual - sequence.length; int sequenceIndex = 0, indexOfActual = 0; for (Object actualElement : actual) { if (indexOfActual++ < start) continue; if (areEqual(actualElement, sequence[sequenceIndex++])) continue; throw actualDoesNotEndWithSequence(info, actual, sequence); } }
/** * Verifies that the given <code>{@link Iterable}</code> contains the given subsequence of objects (possibly with * other values between them). * * @param info contains information about the assertion. * @param actual the given {@code Iterable}. * @param subsequence the subsequence of objects to look for. * @throws AssertionError if the given {@code Iterable} is {@code null}. * @throws NullPointerException if the given sequence is {@code null}. * @throws IllegalArgumentException if the given subsequence is empty. * @throws AssertionError if the given {@code Iterable} does not contain the given subsequence of objects. */ public void assertContainsSubsequence(AssertionInfo info, Iterable<?> actual, Object[] subsequence) { if (commonCheckThatIterableAssertionSucceeds(info, actual, subsequence)) return; Iterator<?> actualIterator = actual.iterator(); int subsequenceIndex = 0; while (actualIterator.hasNext() && subsequenceIndex < subsequence.length) { Object actualNext = actualIterator.next(); Object subsequenceNext = subsequence[subsequenceIndex]; if (areEqual(actualNext, subsequenceNext)) subsequenceIndex++; } if (subsequenceIndex < subsequence.length) throw actualDoesNotContainSubsequence(info, actual, subsequence); }
/** * Verifies that the actual Query's queryParameters contains the given QueryParameter elements. * @param queryParameters the given elements that should be contained in actual Query's queryParameters. * @return this assertion object. * @throws AssertionError if the actual Query's queryParameters does not contain all given QueryParameter elements. */ public QueryAssert hasQueryParameters(QueryParameter... queryParameters) { // check that actual Query we want to make assertions on is not null. isNotNull(); // check that given QueryParameter varargs is not null. if (queryParameters == null) throw new AssertionError("Expecting queryParameters parameter not to be null."); // check with standard error message, to set another message call: info.overridingErrorMessage("my error message"); Iterables.instance().assertContains(info, actual.getQueryParameters(), queryParameters); // return the current assertion for method chaining return this; }
/** * Verifies that the actual JobBatchRecordValue's jobKeys contains <b>only</b> the given Long elements and nothing else in whatever order. * @param jobKeys the given elements that should be contained in actual JobBatchRecordValue's jobKeys. * @return this assertion object. * @throws AssertionError if the actual JobBatchRecordValue's jobKeys does not contain all given Long elements. */ public S hasOnlyJobKeys(Long... jobKeys) { // check that actual JobBatchRecordValue we want to make assertions on is not null. isNotNull(); // check that given Long varargs is not null. if (jobKeys == null) failWithMessage("Expecting jobKeys parameter not to be null."); // check with standard error message, to set another message call: info.overridingErrorMessage("my error message"); Iterables.instance().assertContainsOnly(info, actual.getJobKeys(), jobKeys); // return the current assertion for method chaining return myself; }
/** * Verifies that the actual DeploymentRecordValue's resources does not contain the given io.zeebe.exporter.record.value.deployment.DeploymentResource elements. * * @param resources the given elements that should not be in actual DeploymentRecordValue's resources. * @return this assertion object. * @throws AssertionError if the actual DeploymentRecordValue's resources contains any given io.zeebe.exporter.record.value.deployment.DeploymentResource elements. */ public S doesNotHaveResources(io.zeebe.exporter.record.value.deployment.DeploymentResource... resources) { // check that actual DeploymentRecordValue we want to make assertions on is not null. isNotNull(); // check that given io.zeebe.exporter.record.value.deployment.DeploymentResource varargs is not null. if (resources == null) failWithMessage("Expecting resources parameter not to be null."); // check with standard error message (use overridingErrorMessage before contains to set your own message). Iterables.instance().assertDoesNotContain(info, actual.getResources(), resources); // return the current assertion for method chaining return myself; }
/** * Verifies that the given <code>{@link Iterable}</code> contains the given sequence of objects, without any other * objects between them. * * @param info contains information about the assertion. * @param actual the given {@code Iterable}. * @param sequence the sequence of objects to look for. * @throws AssertionError if the given {@code Iterable} is {@code null}. * @throws NullPointerException if the given sequence is {@code null}. * @throws IllegalArgumentException if the given sequence is empty. * @throws AssertionError if the given {@code Iterable} does not contain the given sequence of objects. */ public void assertContainsSequence(AssertionInfo info, Iterable<?> actual, Object[] sequence) { if (commonCheckThatIterableAssertionSucceeds(info, actual, sequence)) return; // check for elements in values that are missing in actual. List<?> actualAsList = newArrayList(actual); for (int i = 0; i < actualAsList.size(); i++) { // look for given sequence in actual starting from current index (i) if (containsSequenceAtGivenIndex(actualAsList, sequence, i)) return; } throw actualDoesNotContainSequence(info, actual, sequence); }
checkIsNotEmptySubsequence(subsequence); assertNotNull(info, actual); Object actualNext = actualAsList.get(index); Object subsequenceNext = subsequence[subsequenceIndex]; if (areEqual(actualNext, subsequenceNext)) { if (subsequenceIndex == 0) subsequenceStartIndex = index; subsequenceIndex++; throw actualContainsSubsequence(info, actual, subsequence, subsequenceStartIndex);
/** * Verifies that the given {@code Iterable} ends with the given sequence of objects, without any other objects between * them. Similar to <code>{@link #assertContainsSequence(AssertionInfo, Iterable, Object[])}</code>, but it also * verifies that the last element in the sequence is also the last element of the given {@code Iterable}. * * @param info contains information about the assertion. * @param actual the given {@code Iterable}. * @param sequence the sequence of objects to look for. * @throws NullPointerException if the given argument is {@code null}. * @throws IllegalArgumentException if the given argument is an empty array. * @throws AssertionError if the given {@code Iterable} is {@code null}. * @throws AssertionError if the given {@code Iterable} does not end with the given sequence of objects. */ public void assertEndsWith(AssertionInfo info, Iterable<?> actual, Object[] sequence) { if (commonCheckThatIterableAssertionSucceeds(info, actual, sequence)) return; int sizeOfActual = sizeOf(actual); if (sizeOfActual < sequence.length) { throw actualDoesNotEndWithSequence(info, actual, sequence); } int start = sizeOfActual - sequence.length; int sequenceIndex = 0, indexOfActual = 0; for (Object actualElement : actual) { if (indexOfActual++ < start) continue; if (areEqual(actualElement, sequence[sequenceIndex++])) continue; throw actualDoesNotEndWithSequence(info, actual, sequence); } }
/** * Asserts that the given {@code Iterable} contains exactly the given values and nothing else, <b>in order</b>. * * @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} in order. * @throws NullPointerException if the array of values is {@code null}. * @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, in order. */ public void assertContainsExactly(AssertionInfo info, Iterable<?> actual, Object[] values) { checkIsNotNull(values); assertHasSameSizeAs(info, actual, values); // include check that actual is not null Set<Object> notExpected = setFromIterable(actual); Set<Object> notFound = containsOnly(notExpected, values); if (notExpected.isEmpty() && notFound.isEmpty()) { // actual and values have the same elements but are they in the same order. int i = 0; for (Object elementFromActual : actual) { if (!areEqual(elementFromActual, values[i])) { throw failures.failure(info, shouldContainExactly(elementFromActual, values[i], i, comparisonStrategy)); } i++; } return; } throw failures.failure(info, shouldContainExactly(actual, values, notFound, notExpected, comparisonStrategy)); }
@Override public SELF doesNotContain(@SuppressWarnings("unchecked") ELEMENT... values) { iterables.assertDoesNotContain(info, actual, values); return myself; }
/** * {@inheritDoc} */ @Override @CheckReturnValue public SELF usingDefaultElementComparator() { this.iterables = Iterables.instance(); return usingDefaultComparator(); }
protected SELF usingComparisonStrategy(ComparisonStrategy comparisonStrategy) { iterables = new Iterables(comparisonStrategy); return myself; }
assertNotNull(info, actual); if (!areEqual(elementFromActual, values[i])) { throw failures.failure(info, elementsDifferAtIndex(elementFromActual, values[i], i, comparisonStrategy));
/** * Return true if actualAsList contains exactly the given sequence at given starting index, false otherwise. * * @param actualAsList the list to look sequence in * @param sequence the sequence to look for * @param startingIndex the index of actual list at which we start looking for sequence. * @return true if actualAsList contains exactly the given sequence at given starting index, false otherwise. */ private boolean containsSequenceAtGivenIndex(List<?> actualAsList, Object[] sequence, int startingIndex) { // check that, starting from given index, actualAsList has enough remaining elements to contain sequence if (actualAsList.size() - startingIndex < sequence.length) return false; for (int i = 0; i < sequence.length; i++) { if (!areEqual(actualAsList.get(startingIndex + i), sequence[i])) return false; } return true; }
/** * Verifies that the actual JobBatchRecordValue's jobKeys contains the given Long elements. * @param jobKeys the given elements that should be contained in actual JobBatchRecordValue's jobKeys. * @return this assertion object. * @throws AssertionError if the actual JobBatchRecordValue's jobKeys does not contain all given Long elements. */ public S hasJobKeys(Long... jobKeys) { // check that actual JobBatchRecordValue we want to make assertions on is not null. isNotNull(); // check that given Long varargs is not null. if (jobKeys == null) failWithMessage("Expecting jobKeys parameter not to be null."); // check with standard error message, to set another message call: info.overridingErrorMessage("my error message"); Iterables.instance().assertContains(info, actual.getJobKeys(), jobKeys); // return the current assertion for method chaining return myself; }
/** * Verifies that the actual JobBatchRecordValue's jobs contains <b>only</b> the given JobRecordValue elements and nothing else in whatever order. * @param jobs the given elements that should be contained in actual JobBatchRecordValue's jobs. * @return this assertion object. * @throws AssertionError if the actual JobBatchRecordValue's jobs does not contain all given JobRecordValue elements. */ public S hasOnlyJobs(JobRecordValue... jobs) { // check that actual JobBatchRecordValue we want to make assertions on is not null. isNotNull(); // check that given JobRecordValue varargs is not null. if (jobs == null) failWithMessage("Expecting jobs parameter not to be null."); // check with standard error message, to set another message call: info.overridingErrorMessage("my error message"); Iterables.instance().assertContainsOnly(info, actual.getJobs(), jobs); // return the current assertion for method chaining return myself; }