From 028428530f3ad38f14cb1a48d60e5ac42caf435d Mon Sep 17 00:00:00 2001 From: Paulo Benety Date: Tue, 15 May 2018 20:41:11 -0300 Subject: [PATCH 1/2] (#829) Turn AndInThreadsTest Stable --- .../org/cactoos/scalar/AndInThreadsTest.java | 54 ++++++++++--------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/src/test/java/org/cactoos/scalar/AndInThreadsTest.java b/src/test/java/org/cactoos/scalar/AndInThreadsTest.java index 3d1e48a1e7..08ae5a4fa9 100644 --- a/src/test/java/org/cactoos/scalar/AndInThreadsTest.java +++ b/src/test/java/org/cactoos/scalar/AndInThreadsTest.java @@ -25,7 +25,6 @@ import java.util.ArrayList; import java.util.Collections; -import java.util.LinkedList; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -47,7 +46,7 @@ * @checkstyle JavadocMethodCheck (500 lines) * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) */ -@SuppressWarnings("PMD.TooManyMethods") +@SuppressWarnings({"PMD.TooManyMethods", "PMD.AvoidDuplicateLiterals"}) public final class AndInThreadsTest { @Test @@ -57,8 +56,8 @@ public void allTrue() throws Exception { new True(), new True(), new True() - ).value(), - Matchers.equalTo(true) + ), + new ScalarHasValue<>(true) ); } @@ -69,8 +68,8 @@ public void oneFalse() throws Exception { new True(), new False(), new True() - ).value(), - Matchers.equalTo(false) + ), + new ScalarHasValue<>(false) ); } @@ -83,22 +82,24 @@ public void allFalse() throws Exception { new False(), new False() ) - ).value(), - Matchers.equalTo(false) + ), + new ScalarHasValue<>(false) ); } @Test public void emptyIterator() throws Exception { MatcherAssert.assertThat( - new AndInThreads(Collections.emptyList()).value(), - Matchers.equalTo(true) + new AndInThreads(new IterableOf>()), + new ScalarHasValue<>(true) ); } @Test public void iteratesList() { - final List list = new LinkedList<>(); + final List list = Collections.synchronizedList( + new ArrayList(2) + ); MatcherAssert.assertThat( "Can't iterate a list with a procedure", new AndInThreads( @@ -107,25 +108,24 @@ public void iteratesList() { new IterableOf<>("hello", "world") ) ), - new ScalarHasValue<>( - Matchers.allOf( - Matchers.equalTo(true), - new MatcherOf<>( - value -> list.size() == 2 - ) - ) - ) + new ScalarHasValue<>(true) + ); + MatcherAssert.assertThat( + list, + Matchers.containsInAnyOrder("hello", "world") ); } @Test public void iteratesEmptyList() { - final List list = new LinkedList<>(); + final List list = Collections.synchronizedList( + new ArrayList(2) + ); MatcherAssert.assertThat( "Can't iterate a list", new AndInThreads( new Mapped>( - new FuncOf<>(list::add, () -> true), Collections.emptyList() + new FuncOf<>(list::add, () -> true), new IterableOf<>() ) ), new ScalarHasValue<>( @@ -143,14 +143,16 @@ public void iteratesEmptyList() { @Test public void worksWithProc() throws Exception { - final List list = new LinkedList<>(); + final List list = Collections.synchronizedList( + new ArrayList(2) + ); new AndInThreads( (Proc) list::add, 1, 1 ).value(); MatcherAssert.assertThat( - list.size(), - Matchers.equalTo(2) + list, + Matchers.contains(1, 1) ); } @@ -160,8 +162,8 @@ public void worksWithFunc() throws Exception { new AndInThreads( input -> input > 0, 1, -1, 0 - ).value(), - Matchers.equalTo(false) + ), + new ScalarHasValue<>(false) ); } From 688d6d512d6b123fd0784d42782ec92cdaa0f830 Mon Sep 17 00:00:00 2001 From: Paulo Benety Date: Mon, 21 May 2018 22:27:17 -0300 Subject: [PATCH 2/2] (#829) Fix PR issues --- .../org/cactoos/scalar/AndInThreadsTest.java | 81 +++++++++++++++++-- 1 file changed, 76 insertions(+), 5 deletions(-) diff --git a/src/test/java/org/cactoos/scalar/AndInThreadsTest.java b/src/test/java/org/cactoos/scalar/AndInThreadsTest.java index 08ae5a4fa9..51d2fd335c 100644 --- a/src/test/java/org/cactoos/scalar/AndInThreadsTest.java +++ b/src/test/java/org/cactoos/scalar/AndInThreadsTest.java @@ -30,12 +30,15 @@ import java.util.concurrent.Executors; import org.cactoos.Proc; import org.cactoos.Scalar; +import org.cactoos.collection.CollectionOf; import org.cactoos.func.FuncOf; import org.cactoos.iterable.IterableOf; import org.cactoos.iterable.Mapped; import org.cactoos.list.ListOf; +import org.hamcrest.Matcher; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; +import org.hamcrest.collection.IsIterableContainingInAnyOrder; import org.junit.Test; import org.llorllale.cactoos.matchers.MatcherOf; import org.llorllale.cactoos.matchers.ScalarHasValue; @@ -43,6 +46,9 @@ /** * Test case for {@link AndInThreads}. * @since 0.25 + * @todo #829:30min Remove the use of the static method + * `Collections.synchronizedList`. Replace by an object-oriented approach. + * Create a class similar to `SyncCollection` but mutable. * @checkstyle JavadocMethodCheck (500 lines) * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) */ @@ -112,7 +118,20 @@ public void iteratesList() { ); MatcherAssert.assertThat( list, - Matchers.containsInAnyOrder("hello", "world") + new IsIterableContainingInAnyOrder( + new CollectionOf>( + new MatcherOf<>( + text -> { + return "hello".equals(text); + } + ), + new MatcherOf<>( + text -> { + return "world".equals(text); + } + ) + ) + ) ); } @@ -152,7 +171,20 @@ public void worksWithProc() throws Exception { ).value(); MatcherAssert.assertThat( list, - Matchers.contains(1, 1) + new IsIterableContainingInAnyOrder( + new CollectionOf>( + new MatcherOf<>( + value -> { + return value.equals(1); + } + ), + new MatcherOf<>( + value -> { + return value.equals(1); + } + ) + ) + ) ); } @@ -178,7 +210,20 @@ public void worksWithProcIterable() throws Exception { ).value(); MatcherAssert.assertThat( list, - Matchers.containsInAnyOrder(1, 2) + new IsIterableContainingInAnyOrder( + new CollectionOf>( + new MatcherOf<>( + value -> { + return value.equals(1); + } + ), + new MatcherOf<>( + value -> { + return value.equals(2); + } + ) + ) + ) ); } @@ -208,7 +253,20 @@ public void worksWithExecServiceProcValues() throws Exception { ).value(); MatcherAssert.assertThat( list, - Matchers.containsInAnyOrder(1, 2) + new IsIterableContainingInAnyOrder( + new CollectionOf>( + new MatcherOf<>( + value -> { + return value.equals(1); + } + ), + new MatcherOf<>( + value -> { + return value.equals(2); + } + ) + ) + ) ); } @@ -225,7 +283,20 @@ public void worksWithExecServiceProcIterable() throws Exception { ).value(); MatcherAssert.assertThat( list, - Matchers.containsInAnyOrder(1, 2) + new IsIterableContainingInAnyOrder( + new CollectionOf>( + new MatcherOf<>( + value -> { + return value.equals(1); + } + ), + new MatcherOf<>( + value -> { + return value.equals(2); + } + ) + ) + ) ); }