Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecated unwanted classes instead of deleting them #238

Merged
merged 5 commits into from
Dec 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package org.hamcrest.collection;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import static org.hamcrest.core.IsEqual.equalTo;

/**
* @deprecated As of release 2.1, replaced by {@link ArrayMatching}.
*/
@Deprecated
public class IsArrayContainingInAnyOrder<E> extends TypeSafeMatcher<E[]> {
private final IsIterableContainingInAnyOrder<E> iterableMatcher;
private final Collection<Matcher<? super E>> matchers;

public IsArrayContainingInAnyOrder(Collection<Matcher<? super E>> matchers) {
this.iterableMatcher = new IsIterableContainingInAnyOrder<E>(matchers);
this.matchers = matchers;
}

@Override
public boolean matchesSafely(E[] item) {
return iterableMatcher.matches(Arrays.asList(item));
}

@Override
public void describeMismatchSafely(E[] item, Description mismatchDescription) {
iterableMatcher.describeMismatch(Arrays.asList(item), mismatchDescription);
};

@Override
public void describeTo(Description description) {
description.appendList("[", ", ", "]", matchers)
.appendText(" in any order");
}

/**
* Creates an order agnostic matcher for arrays that matches when each item in the
* examined array satisfies one matcher anywhere in the specified matchers.
* For a positive match, the examined array must be of the same length as the number of
* specified matchers.
* <p>
* N.B. each of the specified matchers will only be used once during a given examination, so be
* careful when specifying matchers that may be satisfied by more than one entry in an examined
* array.
* <p>
* For example:
* <pre>assertThat(new String[]{"foo", "bar"}, arrayContainingInAnyOrder(equalTo("bar"), equalTo("foo")))</pre>
*
* @deprecated As of version 2.1, use {@link ArrayMatching#arrayContainingInAnyOrder(Matcher[])}.
*
* @param itemMatchers
* a list of matchers, each of which must be satisfied by an entry in an examined array
*/
public static <E> Matcher<E[]> arrayContainingInAnyOrder(Matcher<? super E>... itemMatchers) {
return arrayContainingInAnyOrder(Arrays.asList(itemMatchers));
}

/**
* Creates an order agnostic matcher for arrays that matches when each item in the
* examined array satisfies one matcher anywhere in the specified collection of matchers.
* For a positive match, the examined array must be of the same length as the specified collection
* of matchers.
* <p>
* N.B. each matcher in the specified collection will only be used once during a given
* examination, so be careful when specifying matchers that may be satisfied by more than
* one entry in an examined array.
* <p>
* For example:
* <pre>assertThat(new String[]{"foo", "bar"}, arrayContainingInAnyOrder(Arrays.asList(equalTo("bar"), equalTo("foo"))))</pre>
*
* @deprecated As of version 2.1, use {@link ArrayMatching#arrayContainingInAnyOrder(Collection)}.
*
* @param itemMatchers
* a list of matchers, each of which must be satisfied by an item provided by an examined array
*/
public static <E> Matcher<E[]> arrayContainingInAnyOrder(Collection<Matcher<? super E>> itemMatchers) {
return new IsArrayContainingInAnyOrder<E>(itemMatchers);
}

/**
* Creates an order agnostic matcher for arrays that matches when each item in the
* examined array is logically equal to one item anywhere in the specified items.
* For a positive match, the examined array must be of the same length as the number of
* specified items.
* <p>
* N.B. each of the specified items will only be used once during a given examination, so be
* careful when specifying items that may be equal to more than one entry in an examined
* array.
* <p>
* For example:
* <pre>assertThat(new String[]{"foo", "bar"}, containsInAnyOrder("bar", "foo"))</pre>
*
* @deprecated As of version 2.1, use {@link ArrayMatching#arrayContainingInAnyOrder(Object[])}.
*
* @param items
* the items that must equal the entries of an examined array, in any order
*/
public static <E> Matcher<E[]> arrayContainingInAnyOrder(E... items) {
List<Matcher<? super E>> matchers = new ArrayList<Matcher<? super E>>();
for (E item : items) {
matchers.add(equalTo(item));
}
return new IsArrayContainingInAnyOrder<E>(matchers);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package org.hamcrest.collection;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static java.util.Arrays.asList;
import static org.hamcrest.core.IsEqual.equalTo;

/**
* @deprecated As of release 2.1, replaced by {@link ArrayMatching}.
*/
public class IsArrayContainingInOrder<E> extends TypeSafeMatcher<E[]> {
private final Collection<Matcher<? super E>> matchers;
private final IsIterableContainingInOrder<E> iterableMatcher;

public IsArrayContainingInOrder(List<Matcher<? super E>> matchers) {
this.iterableMatcher = new IsIterableContainingInOrder<E>(matchers);
this.matchers = matchers;
}

@Override
public boolean matchesSafely(E[] item) {
return iterableMatcher.matches(asList(item));
}

@Override
public void describeMismatchSafely(E[] item, Description mismatchDescription) {
iterableMatcher.describeMismatch(asList(item), mismatchDescription);
}

@Override
public void describeTo(Description description) {
description.appendList("[", ", ", "]", matchers);
}

/**
* Creates a matcher for arrays that matcheswhen each item in the examined array is
* logically equal to the corresponding item in the specified items. For a positive match,
* the examined array must be of the same length as the number of specified items.
* <p>
* For example:
* <pre>assertThat(new String[]{"foo", "bar"}, contains("foo", "bar"))</pre>
*
* @deprecated As of version 2.1, use {@link ArrayMatching#arrayContaining(Object[])}.
*
* @param items
* the items that must equal the items within an examined array
*/
public static <E> Matcher<E[]> arrayContaining(E... items) {
List<Matcher<? super E>> matchers = new ArrayList<Matcher<? super E>>();
for (E item : items) {
matchers.add(equalTo(item));
}
return arrayContaining(matchers);
}

/**
* Creates a matcher for arrays that matches when each item in the examined array satisfies the
* corresponding matcher in the specified matchers. For a positive match, the examined array
* must be of the same length as the number of specified matchers.
* <p>
* For example:
* <pre>assertThat(new String[]{"foo", "bar"}, contains(equalTo("foo"), equalTo("bar")))</pre>
*
* @deprecated As of version 2.1, use {@link ArrayMatching#arrayContaining(Matcher[])}.
*
* @param itemMatchers
* the matchers that must be satisfied by the items in the examined array
*/
public static <E> Matcher<E[]> arrayContaining(Matcher<? super E>... itemMatchers) {
return arrayContaining(asList(itemMatchers));
}

/**
* Creates a matcher for arrays that matches when each item in the examined array satisfies the
* corresponding matcher in the specified list of matchers. For a positive match, the examined array
* must be of the same length as the specified list of matchers.
* <p>
* For example:
* <pre>assertThat(new String[]{"foo", "bar"}, contains(Arrays.asList(equalTo("foo"), equalTo("bar"))))</pre>
*
* @deprecated As of version 2.1, use {@link ArrayMatching#arrayContaining(List)}.
*
* @param itemMatchers
* a list of matchers, each of which must be satisfied by the corresponding item in an examined array
*/
public static <E> Matcher<E[]> arrayContaining(List<Matcher<? super E>> itemMatchers) {
return new IsArrayContainingInOrder<E>(itemMatchers);
}
}
100 changes: 100 additions & 0 deletions hamcrest/src/main/java/org/hamcrest/core/IsCollectionContaining.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package org.hamcrest.core;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeDiagnosingMatcher;

/**
* @deprecated As of release 2.1, replaced by {@link IsIterableContaining}.
*/
@Deprecated
public class IsCollectionContaining<T> extends TypeSafeDiagnosingMatcher<Iterable<? super T>> {
private final IsIterableContaining<T> delegate;

public IsCollectionContaining(Matcher<? super T> elementMatcher) {
this.delegate = new IsIterableContaining<>(elementMatcher);
}

@Override
protected boolean matchesSafely(Iterable<? super T> collection, Description mismatchDescription) {
return delegate.matchesSafely(collection, mismatchDescription);
}

@Override
public void describeTo(Description description) {
delegate.describeTo(description);
}


/**
* Creates a matcher for {@link Iterable}s that only matches when a single pass over the
* examined {@link Iterable} yields at least one item that is matched by the specified
* <code>itemMatcher</code>. Whilst matching, the traversal of the examined {@link Iterable}
* will stop as soon as a matching item is found.
* For example:
* <pre>assertThat(Arrays.asList("foo", "bar"), hasItem(startsWith("ba")))</pre>
*
* @deprecated As of version 2.1, use {@link IsIterableContaining#hasItem(Matcher)}.
*
* @param itemMatcher
* the matcher to apply to items provided by the examined {@link Iterable}
*/
public static <T> Matcher<Iterable<? super T>> hasItem(Matcher<? super T> itemMatcher) {
return IsIterableContaining.hasItem(itemMatcher);
}

/**
* Creates a matcher for {@link Iterable}s that only matches when a single pass over the
* examined {@link Iterable} yields at least one item that is equal to the specified
* <code>item</code>. Whilst matching, the traversal of the examined {@link Iterable}
* will stop as soon as a matching item is found.
* For example:
* <pre>assertThat(Arrays.asList("foo", "bar"), hasItem("bar"))</pre>
*
* @deprecated As of version 2.1, use {@link IsIterableContaining#hasItem(Object)}.
*
* @param item
* the item to compare against the items provided by the examined {@link Iterable}
*/
public static <T> Matcher<Iterable<? super T>> hasItem(T item) {
// Doesn't forward to hasItem() method so compiler can sort out generics.
return IsIterableContaining.hasItem(item);
}

/**
* Creates a matcher for {@link Iterable}s that matches when consecutive passes over the
* examined {@link Iterable} yield at least one item that is matched by the corresponding
* matcher from the specified <code>itemMatchers</code>. Whilst matching, each traversal of
* the examined {@link Iterable} will stop as soon as a matching item is found.
* For example:
* <pre>assertThat(Arrays.asList("foo", "bar", "baz"), hasItems(endsWith("z"), endsWith("o")))</pre>
*
* @deprecated As of version 2.1, use {@link IsIterableContaining#hasItems(Matcher[])}}.
*
* @param itemMatchers
* the matchers to apply to items provided by the examined {@link Iterable}
*/
@SafeVarargs
public static <T> Matcher<Iterable<T>> hasItems(Matcher<? super T>... itemMatchers) {
return IsIterableContaining.hasItems(itemMatchers);
}

/**
* Creates a matcher for {@link Iterable}s that matches when consecutive passes over the
* examined {@link Iterable} yield at least one item that is equal to the corresponding
* item from the specified <code>items</code>. Whilst matching, each traversal of the
* examined {@link Iterable} will stop as soon as a matching item is found.
* For example:
* <pre>assertThat(Arrays.asList("foo", "bar", "baz"), hasItems("baz", "foo"))</pre>
*
* @deprecated As of version 2.1, use {@link IsIterableContaining#hasItems(Object[])}}.
*
* @param items
* the items to compare against the items provided by the examined {@link Iterable}
*/
@SafeVarargs
public static <T> Matcher<Iterable<T>> hasItems(T... items) {
return IsIterableContaining.hasItems(items);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.hamcrest.collection;

import org.hamcrest.AbstractMatcherTest;
import org.hamcrest.Matcher;

import static org.hamcrest.collection.ArrayMatching.arrayContainingInAnyOrder;
import static org.hamcrest.core.IsEqual.equalTo;

public class ArrayMatchingInAnyOrderTest extends AbstractMatcherTest {

@SuppressWarnings("unchecked")
@Override
protected Matcher<?> createMatcher() {
return ArrayMatching.arrayContainingInAnyOrder(equalTo(1), equalTo(2));
}

@SuppressWarnings("unchecked")
public void testHasAReadableDescription() {
assertDescription("[<1>, <2>] in any order", ArrayMatching.arrayContainingInAnyOrder(equalTo(1), equalTo(2)));
assertDescription("[<1>, <2>] in any order", ArrayMatching.arrayContainingInAnyOrder(1, 2));
}

public void testMatchesItemsInAnyOrder() {
assertMatches("in order", ArrayMatching.arrayContainingInAnyOrder(1, 2, 3), new Integer[] {1, 2, 3});
assertMatches("out of order", ArrayMatching.arrayContainingInAnyOrder(1, 2, 3), new Integer[] {3, 2, 1});
assertMatches("single", ArrayMatching.arrayContainingInAnyOrder(1), new Integer[] {1});
}

@SuppressWarnings("unchecked")
public void testAppliesMatchersInAnyOrder() {
assertMatches("in order", ArrayMatching.arrayContainingInAnyOrder(equalTo(1), equalTo(2), equalTo(3)), new Integer[] {1, 2, 3});
assertMatches("out of order", ArrayMatching.arrayContainingInAnyOrder(equalTo(1), equalTo(2), equalTo(3)), new Integer[] {3, 2, 1});
assertMatches("single", ArrayMatching.arrayContainingInAnyOrder(equalTo(1)), new Integer[] {1});
}

public void testMismatchesItemsInAnyOrder() {
Matcher<Integer[]> matcher = ArrayMatching.arrayContainingInAnyOrder(1, 2, 3);
assertMismatchDescription("was null", matcher, null);
assertMismatchDescription("no item matches: <1>, <2>, <3> in []", matcher, new Integer[] {});
assertMismatchDescription("no item matches: <2>, <3> in [<1>]", matcher, new Integer[] {1});
assertMismatchDescription("not matched: <4>", matcher, new Integer[] {4,3,2,1});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.hamcrest.collection;

import org.hamcrest.AbstractMatcherTest;
import org.hamcrest.Matcher;

import static org.hamcrest.collection.ArrayMatching.arrayContaining;
import static org.hamcrest.core.IsEqual.equalTo;

public class ArrayMatchingInOrderTest extends AbstractMatcherTest {

@SuppressWarnings("unchecked")
@Override
protected Matcher<?> createMatcher() {
return arrayContaining(equalTo(1), equalTo(2));
}

@SuppressWarnings("unchecked")
public void testHasAReadableDescription() {
assertDescription("[<1>, <2>]", arrayContaining(equalTo(1), equalTo(2)));
}

public void testMatchesItemsInOrder() {
assertMatches("in order", arrayContaining(1, 2, 3), new Integer[] {1, 2, 3});
assertMatches("single", arrayContaining(1), new Integer[] {1});
}

@SuppressWarnings("unchecked")
public void testAppliesMatchersInOrder() {
assertMatches("in order", arrayContaining(equalTo(1), equalTo(2), equalTo(3)), new Integer[] {1, 2, 3});
assertMatches("single", arrayContaining(equalTo(1)), new Integer[] {1});
}

public void testMismatchesItemsInOrder() {
Matcher<Integer[]> matcher = arrayContaining(1, 2, 3);
assertMismatchDescription("was null", matcher, null);
assertMismatchDescription("no item was <1>", matcher, new Integer[] {});
assertMismatchDescription("no item was <2>", matcher, new Integer[] {1});
assertMismatchDescription("item 0: was <4>", matcher, new Integer[] {4,3,2,1});
assertMismatchDescription("item 2: was <4>", matcher, new Integer[] {1,2, 4});
}

public void testCanHandleNullValuesInAnArray() {
assertMatches("with nulls", arrayContaining(null, null), new Object[]{null, null});
}
}
Loading