Skip to content

Commit

Permalink
(#1082) Replace forbidden API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
vladhss committed Apr 24, 2019
1 parent dc9c4b4 commit 622165a
Show file tree
Hide file tree
Showing 16 changed files with 458 additions and 343 deletions.
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ The MIT License (MIT)
<signaturesFile>./src/test/resources/forbidden-apis.txt</signaturesFile>
</signaturesFiles>
<!--
@todo #951:30min In the continuation of #588, all the calls
@todo #1082:30min In the continuation of #588, all the calls
to Matchers should be replaced with their OO counterparts.
This todo should be updated with a new one until everything is
done. The newly covered classes should be added to the include
Expand All @@ -172,6 +172,7 @@ The MIT License (MIT)
<include>org/cactoos/bytes/*.class</include>
<include>org/cactoos/map/*.class</include>
<include>org/cactoos/time/*.class</include>
<include>org/cactoos/collection/*.class</include>
</includes>
</configuration>
<executions>
Expand Down
69 changes: 48 additions & 21 deletions src/test/java/org/cactoos/collection/BehavesAsCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,21 @@
import java.util.Collection;
import org.cactoos.list.ListOf;
import org.hamcrest.Description;
import org.hamcrest.MatcherAssert;
import org.hamcrest.TypeSafeMatcher;
import org.hamcrest.collection.IsCollectionWithSize;
import org.hamcrest.collection.IsEmptyCollection;
import org.hamcrest.core.IsCollectionContaining;
import org.hamcrest.core.IsEqual;
import org.hamcrest.core.IsNot;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.MatcherOf;

/**
* Matcher for collection.
* @param <E> Type of source item
* @since 0.23
* @checkstyle JavadocMethodCheck (500 lines)
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
*/
public final class BehavesAsCollection<E> extends
TypeSafeMatcher<Collection<E>> {
Expand All @@ -61,28 +62,54 @@ public BehavesAsCollection(final E item) {
@Override
@SuppressWarnings({ "unchecked", "PMD.ClassCastExceptionWithToArray" })
public boolean matchesSafely(final Collection<E> col) {
MatcherAssert.assertThat(
col, new IsCollectionContaining<>(new IsEqual<>(this.sample))
);
MatcherAssert.assertThat(
col, new IsNot<>(new IsEmptyCollection<>())
);
MatcherAssert.assertThat(
col, new IsCollectionWithSize<>(new MatcherOf<>(s -> s > 0))
);
MatcherAssert.assertThat(
new ListOf<>((E[]) col.toArray()),
new IsCollectionContaining<>(new IsEqual<>(this.sample))
);
new Assertion<>(
"Must contain item",
() -> col,
new IsCollectionContaining<>(
new IsEqual<>(
this.sample
)
)
).affirm();
new Assertion<>(
"Must not be empty",
() -> col,
new IsNot<>(
new IsEmptyCollection<>()
)
).affirm();
new Assertion<>(
"Size must be more than 0",
() -> col,
new IsCollectionWithSize<>(
new MatcherOf<>(s -> s > 0)
)
).affirm();
new Assertion<>(
"Array must contain item",
() -> new ListOf<>(
(E[]) col.toArray()
),
new IsCollectionContaining<>(
new IsEqual<>(this.sample)
)
).affirm();
final E[] array = (E[]) new Object[col.size()];
col.toArray(array);
MatcherAssert.assertThat(
new ListOf<>(array),
new IsCollectionContaining<>(new IsEqual<>(this.sample))
);
MatcherAssert.assertThat(
col.containsAll(new ListOf<>(this.sample)), new IsEqual<>(true)
);
new Assertion<>(
"Array from collection must contain item",
() -> new ListOf<>(array),
new IsCollectionContaining<>(
new IsEqual<>(this.sample)
)
).affirm();
new Assertion<>(
"Must contain list with the item",
() -> col.containsAll(
new ListOf<>(this.sample)
),
new IsEqual<>(true)
).affirm();
return true;
}

Expand Down
61 changes: 30 additions & 31 deletions src/test/java/org/cactoos/collection/CollectionEnvelopeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.util.LinkedList;
import java.util.List;
import org.cactoos.list.ListOf;
import org.hamcrest.MatcherAssert;
import org.hamcrest.core.IsEqual;
import org.hamcrest.core.IsNot;
import org.junit.Test;
Expand Down Expand Up @@ -60,29 +59,29 @@ public void returnsIteratorWithUnsupportedRemove() {

@Test
public void notEqualToObjectOfAnotherType() {
MatcherAssert.assertThat(
new Assertion<>(
"Collection is equal to object of different type",
new CollectionOf<>(),
() -> new CollectionOf<>(),
new IsNot<>(new IsEqual<>("a"))
);
).affirm();
}

@Test
public void notEqualToCollectionOfDifferentSize() {
MatcherAssert.assertThat(
new Assertion<>(
"Collection is equal to a collection of different size",
new CollectionOf<>(),
() -> new CollectionOf<>(),
new IsNot<>(new IsEqual<>(new CollectionOf<>("b")))
);
).affirm();
}

@Test
public void notEqualToCollectionOfDifferentElements() {
MatcherAssert.assertThat(
new Assertion<>(
"Collection is equal to a collection with different content",
new CollectionOf<>("a", "b"),
() -> new CollectionOf<>("a", "b"),
new IsNot<>(new IsEqual<>(new CollectionOf<>("a", "c")))
);
).affirm();
}

@Test
Expand All @@ -97,65 +96,65 @@ public void equalToItself() {

@Test
public void equalToCollectionWithIdenticalContent() {
MatcherAssert.assertThat(
new Assertion<>(
"Collection is not equal to a collection with identical content",
new CollectionOf<>("val1", "val2"),
() -> new CollectionOf<>("val1", "val2"),
new IsEqual<>(new CollectionOf<>("val1", "val2"))
);
).affirm();
}

@Test
public void equalToListWithIdenticalContent() {
MatcherAssert.assertThat(
new Assertion<>(
"Collection not equal to a list with identical content",
new CollectionOf<>("a"),
() -> new CollectionOf<>("a"),
new IsEqual<>(new ListOf<>("a"))
);
).affirm();
}

@Test
public void equalToDerivedCollection() {
MatcherAssert.assertThat(
new Assertion<>(
"Collection not equal to derived collection with identical content",
new CollectionOf<>("a"),
() -> new CollectionOf<>("a"),
new IsEqual<>(new CollectionEnvelopeTest.CustomCollection("a"))
);
).affirm();
}

@Test
public void equalToEmptyCollection() {
MatcherAssert.assertThat(
new Assertion<>(
"Empty collection not equal with empty collection",
new CollectionOf<>(),
() -> new CollectionOf<>(),
new IsEqual<>(new CollectionOf<>())
);
).affirm();
}

@Test
public void notEqualToNull() {
MatcherAssert.assertThat(
new Assertion<>(
"Empty collection equal to null",
new CollectionOf<>(),
() -> new CollectionOf<>(),
new IsNot<>(new IsEqual<>(null))
);
).affirm();
}

@Test
public void hashCodeEqual() {
MatcherAssert.assertThat(
new Assertion<>(
"HashCode returns different results for same entries",
new CollectionOf<>("a", "b").hashCode(),
() -> new CollectionOf<>("a", "b").hashCode(),
new IsEqual<>(new CollectionOf<>("a", "b").hashCode())
);
).affirm();
}

@Test
public void differentHashCode() {
MatcherAssert.assertThat(
new Assertion<>(
"HashCode returns identical results for different entries",
new CollectionOf<>("a", "b").hashCode(),
() -> new CollectionOf<>("a", "b").hashCode(),
new IsNot<>(new IsEqual<>(new CollectionOf<>("b", "a").hashCode()))
);
).affirm();
}

@Test
Expand Down
40 changes: 23 additions & 17 deletions src/test/java/org/cactoos/collection/CollectionOfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@

import org.cactoos.iterable.IterableOf;
import org.cactoos.list.ListOf;
import org.hamcrest.MatcherAssert;
import org.hamcrest.core.IsEqual;
import org.junit.Test;
import org.llorllale.cactoos.matchers.Assertion;

/**
* Test Case for {@link CollectionOf}.
Expand All @@ -38,39 +38,45 @@
public final class CollectionOfTest {

@Test
public void behavesAsCollection() throws Exception {
MatcherAssert.assertThat(
public void behavesAsCollection() {
new Assertion<>(
"Can't behave as a collection",
new CollectionOf<>(1, 2, 0, -1),
() -> new CollectionOf<>(1, 2, 0, -1),
new BehavesAsCollection<>(-1)
);
).affirm();
}

@Test
public void buildsCollectionFromIterable() throws Exception {
MatcherAssert.assertThat(
public void buildsCollectionFromIterable() {
new Assertion<>(
"Can't build a collection from iterable",
new CollectionOf<>(new ListOf<>(new IterableOf<>(1, 2, 0, -1))),
() -> new CollectionOf<>(
new ListOf<>(
new IterableOf<>(1, 2, 0, -1)
)
),
new BehavesAsCollection<>(-1)
);
).affirm();
}

@Test
public void testToString() throws Exception {
MatcherAssert.assertThat(
public void testToString() {
new Assertion<>(
"Wrong toString output. Expected \"[1, 2, 0, -1]\".",
new CollectionOf<>(new ListOf<>(1, 2, 0, -1)).toString(),
() -> new CollectionOf<>(
new ListOf<>(1, 2, 0, -1)
).toString(),
new IsEqual<>("[1, 2, 0, -1]")
);
).affirm();
}

@Test
public void testToStringEmpty() throws Exception {
MatcherAssert.assertThat(
public void testToStringEmpty() {
new Assertion<>(
"Wrong toString output. Expected \"[]\".",
new CollectionOf<>(new ListOf<>()).toString(),
() -> new CollectionOf<>(new ListOf<>()).toString(),
new IsEqual<>("[]")
);
).affirm();
}

}
Loading

0 comments on commit 622165a

Please sign in to comment.