Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2016 the original author or authors.
*/
package com.fitbur.assertj.internal;
import static java.util.Arrays.asList;
import static java.util.stream.StreamSupport.stream;
import static com.fitbur.assertj.error.ConditionAndGroupGenericParameterTypeShouldBeTheSame.shouldBeSameGenericBetweenIterableAndCondition;
import static com.fitbur.assertj.error.ElementsShouldBe.elementsShouldBe;
import static com.fitbur.assertj.error.ElementsShouldBeAtLeast.elementsShouldBeAtLeast;
import static com.fitbur.assertj.error.ElementsShouldBeAtMost.elementsShouldBeAtMost;
import static com.fitbur.assertj.error.ElementsShouldBeExactly.elementsShouldBeExactly;
import static com.fitbur.assertj.error.ElementsShouldHave.elementsShouldHave;
import static com.fitbur.assertj.error.ElementsShouldHaveAtLeast.elementsShouldHaveAtLeast;
import static com.fitbur.assertj.error.ElementsShouldHaveAtMost.elementsShouldHaveAtMost;
import static com.fitbur.assertj.error.ElementsShouldHaveExactly.elementsShouldHaveExactly;
import static com.fitbur.assertj.error.ElementsShouldMatch.elementsShouldMatch;
import static com.fitbur.assertj.error.ElementsShouldNotBe.elementsShouldNotBe;
import static com.fitbur.assertj.error.ElementsShouldNotHave.elementsShouldNotHave;
import static com.fitbur.assertj.error.ShouldBeEmpty.shouldBeEmpty;
import static com.fitbur.assertj.error.ShouldBeNullOrEmpty.shouldBeNullOrEmpty;
import static com.fitbur.assertj.error.ShouldBeSubsetOf.shouldBeSubsetOf;
import static com.fitbur.assertj.error.ShouldContain.shouldContain;
import static com.fitbur.assertj.error.ShouldContainExactly.elementsDifferAtIndex;
import static com.fitbur.assertj.error.ShouldContainExactly.shouldContainExactly;
import static com.fitbur.assertj.error.ShouldContainExactly.shouldHaveSameSize;
import static com.fitbur.assertj.error.ShouldContainExactlyInAnyOrder.*;
import static com.fitbur.assertj.error.ShouldContainNull.shouldContainNull;
import static com.fitbur.assertj.error.ShouldContainOnly.shouldContainOnly;
import static com.fitbur.assertj.error.ShouldContainSequence.shouldContainSequence;
import static com.fitbur.assertj.error.ShouldContainSubsequence.shouldContainSubsequence;
import static com.fitbur.assertj.error.ShouldContainsOnlyOnce.shouldContainsOnlyOnce;
import static com.fitbur.assertj.error.ShouldEndWith.shouldEndWith;
import static com.fitbur.assertj.error.ShouldNotBeEmpty.shouldNotBeEmpty;
import static com.fitbur.assertj.error.ShouldNotContain.shouldNotContain;
import static com.fitbur.assertj.error.ShouldNotContainNull.shouldNotContainNull;
import static com.fitbur.assertj.error.ShouldNotHaveDuplicates.shouldNotHaveDuplicates;
import static com.fitbur.assertj.error.ShouldStartWith.shouldStartWith;
import static com.fitbur.assertj.internal.Arrays.assertIsArray;
import static com.fitbur.assertj.internal.CommonValidations.checkIsNotNull;
import static com.fitbur.assertj.internal.CommonValidations.checkIsNotNullAndNotEmpty;
import static com.fitbur.assertj.internal.CommonValidations.checkIterableIsNotNull;
import static com.fitbur.assertj.internal.CommonValidations.checkSizes;
import static com.fitbur.assertj.internal.CommonValidations.failIfEmptySinceActualIsNotEmpty;
import static com.fitbur.assertj.internal.CommonValidations.hasSameSizeAsCheck;
import static com.fitbur.assertj.util.IterableUtil.isNullOrEmpty;
import static com.fitbur.assertj.util.IterableUtil.sizeOf;
import static com.fitbur.assertj.util.Lists.newArrayList;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;
import com.fitbur.assertj.api.AssertionInfo;
import com.fitbur.assertj.api.Condition;
import com.fitbur.assertj.util.VisibleForTesting;
/**
* Reusable assertions for {@link Iterable}s.
*
* @author Alex Ruiz
* @author Yvonne Wang
* @author Maciej Jaskowski
* @author Nicolas François
* @author Joel Costigliola
*/
public class Iterables {
private static final Iterables INSTANCE = new Iterables();
private final ComparisonStrategy comparisonStrategy;
@VisibleForTesting
Failures failures = Failures.instance();
@VisibleForTesting
Conditions conditions = Conditions.instance();
@VisibleForTesting
Predicates predicates = Predicates.instance();
/**
* Returns the singleton instance of this class based on {@link StandardComparisonStrategy}.
*
* @return the singleton instance of this class based on {@link StandardComparisonStrategy}.
*/
public static Iterables instance() {
return INSTANCE;
}
@VisibleForTesting
Iterables() {
this(StandardComparisonStrategy.instance());
}
public Iterables(ComparisonStrategy comparisonStrategy) {
this.comparisonStrategy = comparisonStrategy;
}
@VisibleForTesting
public Comparator> getComparator() {
if (comparisonStrategy instanceof ComparatorBasedComparisonStrategy) {
return ((ComparatorBasedComparisonStrategy) comparisonStrategy).getComparator();
}
return null;
}
@VisibleForTesting
public ComparisonStrategy getComparisonStrategy() {
return comparisonStrategy;
}
/**
* Asserts that the given {@link Iterable} is {@code null} or empty.
*
* @param info contains information about the assertion.
* @param actual the given {@code Iterable}.
* @throws AssertionError if the given {@code Iterable} is not {@code null} *and* contains one or more elements.
*/
public void assertNullOrEmpty(AssertionInfo info, Iterable> actual) {
if (!isNullOrEmpty(actual)) throw failures.failure(info, shouldBeNullOrEmpty(actual));
}
/**
* Asserts that the given {@code Iterable} is empty.
*
* @param info contains information about the assertion.
* @param actual the given {@code Iterable}.
* @throws AssertionError if the given {@code Iterable} is {@code null}.
* @throws AssertionError if the given {@code Iterable} is not empty.
*/
public void assertEmpty(AssertionInfo info, Iterable> actual) {
assertNotNull(info, actual);
if (!isNullOrEmpty(actual)) throw failures.failure(info, shouldBeEmpty(actual));
}
/**
* Asserts that the given {@code Iterable} is not empty.
*
* @param info contains information about the assertion.
* @param actual the given {@code Iterable}.
* @throws AssertionError if the given {@code Iterable} is {@code null}.
* @throws AssertionError if the given {@code Iterable} is empty.
*/
public void assertNotEmpty(AssertionInfo info, Iterable> actual) {
assertNotNull(info, actual);
if (isNullOrEmpty(actual)) throw failures.failure(info, shouldNotBeEmpty());
}
/**
* Asserts that the number of elements in the given {@code Iterable} is equal to the expected one.
*
* @param info contains information about the assertion.
* @param actual the given {@code Iterable}.
* @param expectedSize the expected size of {@code actual}.
* @throws AssertionError if the given {@code Iterable} is {@code null}.
* @throws AssertionError if the number of elements in the given {@code Iterable} is different than the expected one.
*/
public void assertHasSize(AssertionInfo info, Iterable> actual, int expectedSize) {
assertNotNull(info, actual);
checkSizes(actual, sizeOf(actual), expectedSize, info);
}
/**
* Assert that the actual {@code Iterable} has the same size as the other array.
*
* @param info contains information about the assertion.
* @param actual the given {@code Iterable}.
* @param other the given array to compare.
* @throws AssertionError if the actual group is {@code null}.
* @throws AssertionError if the other group is {@code null}.
* @throws AssertionError if actual {@code Iterable} and other array don't have the same size.
*/
public void assertHasSameSizeAs(AssertionInfo info, Iterable> actual, Object other) {
assertNotNull(info, actual);
assertIsArray(info, other);
hasSameSizeAsCheck(info, actual, other, sizeOf(actual));
}
/**
* Assert that the actual {@code Iterable} has the same size as the other {@code Iterable}.
*
* @param info contains information about the assertion.
* @param actual the given {@code Iterable}.
* @param other the given {@code Iterable}.
* @throws AssertionError if the actual group is {@code null}.
* @throws AssertionError if the other group is {@code null}.
* @throws AssertionError if actual and other {@code Iterable} don't have the same size.
*/
public void assertHasSameSizeAs(AssertionInfo info, Iterable> actual, Iterable> other) {
assertNotNull(info, actual);
hasSameSizeAsCheck(info, actual, other, sizeOf(actual));
}
/**
* Asserts that the given {@code Iterable} contains the given values, in any order.
*
* @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.
*/
public void assertContains(AssertionInfo info, Iterable> actual, Object[] values) {
if (commonCheckThatIterableAssertionSucceeds(info, actual, values))
return;
// check for elements in values that are missing in actual.
assertIterableContainsGivenValues(actual, values, info);
}
private void assertIterableContainsGivenValues(Iterable> actual, Object[] values, AssertionInfo info) {
Set