Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed May 22, 2018
2 parents 9078a94 + 688d6d5 commit 995eb06
Showing 1 changed file with 98 additions and 25 deletions.
123 changes: 98 additions & 25 deletions src/test/java/org/cactoos/scalar/AndInThreadsTest.java
Expand Up @@ -25,29 +25,34 @@

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;
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;

/**
* 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)
*/
@SuppressWarnings("PMD.TooManyMethods")
@SuppressWarnings({"PMD.TooManyMethods", "PMD.AvoidDuplicateLiterals"})
public final class AndInThreadsTest {

@Test
Expand All @@ -57,8 +62,8 @@ public void allTrue() throws Exception {
new True(),
new True(),
new True()
).value(),
Matchers.equalTo(true)
),
new ScalarHasValue<>(true)
);
}

Expand All @@ -69,8 +74,8 @@ public void oneFalse() throws Exception {
new True(),
new False(),
new True()
).value(),
Matchers.equalTo(false)
),
new ScalarHasValue<>(false)
);
}

Expand All @@ -83,22 +88,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<Scalar<Boolean>>()),
new ScalarHasValue<>(true)
);
}

@Test
public void iteratesList() {
final List<String> list = new LinkedList<>();
final List<String> list = Collections.synchronizedList(
new ArrayList<String>(2)
);
MatcherAssert.assertThat(
"Can't iterate a list with a procedure",
new AndInThreads(
Expand All @@ -107,11 +114,21 @@ public void iteratesList() {
new IterableOf<>("hello", "world")
)
),
new ScalarHasValue<>(
Matchers.allOf(
Matchers.equalTo(true),
new ScalarHasValue<>(true)
);
MatcherAssert.assertThat(
list,
new IsIterableContainingInAnyOrder<String>(
new CollectionOf<Matcher<? super String>>(
new MatcherOf<>(
text -> {
return "hello".equals(text);
}
),
new MatcherOf<>(
value -> list.size() == 2
text -> {
return "world".equals(text);
}
)
)
)
Expand All @@ -120,12 +137,14 @@ public void iteratesList() {

@Test
public void iteratesEmptyList() {
final List<String> list = new LinkedList<>();
final List<String> list = Collections.synchronizedList(
new ArrayList<String>(2)
);
MatcherAssert.assertThat(
"Can't iterate a list",
new AndInThreads(
new Mapped<String, Scalar<Boolean>>(
new FuncOf<>(list::add, () -> true), Collections.emptyList()
new FuncOf<>(list::add, () -> true), new IterableOf<>()
)
),
new ScalarHasValue<>(
Expand All @@ -143,14 +162,29 @@ public void iteratesEmptyList() {

@Test
public void worksWithProc() throws Exception {
final List<Integer> list = new LinkedList<>();
final List<Integer> list = Collections.synchronizedList(
new ArrayList<Integer>(2)
);
new AndInThreads(
(Proc<Integer>) list::add,
1, 1
).value();
MatcherAssert.assertThat(
list.size(),
Matchers.equalTo(2)
list,
new IsIterableContainingInAnyOrder<Integer>(
new CollectionOf<Matcher<? super Integer>>(
new MatcherOf<>(
value -> {
return value.equals(1);
}
),
new MatcherOf<>(
value -> {
return value.equals(1);
}
)
)
)
);
}

Expand All @@ -160,8 +194,8 @@ public void worksWithFunc() throws Exception {
new AndInThreads(
input -> input > 0,
1, -1, 0
).value(),
Matchers.equalTo(false)
),
new ScalarHasValue<>(false)
);
}

Expand All @@ -176,7 +210,20 @@ public void worksWithProcIterable() throws Exception {
).value();
MatcherAssert.assertThat(
list,
Matchers.containsInAnyOrder(1, 2)
new IsIterableContainingInAnyOrder<Integer>(
new CollectionOf<Matcher<? super Integer>>(
new MatcherOf<>(
value -> {
return value.equals(1);
}
),
new MatcherOf<>(
value -> {
return value.equals(2);
}
)
)
)
);
}

Expand Down Expand Up @@ -206,7 +253,20 @@ public void worksWithExecServiceProcValues() throws Exception {
).value();
MatcherAssert.assertThat(
list,
Matchers.containsInAnyOrder(1, 2)
new IsIterableContainingInAnyOrder<Integer>(
new CollectionOf<Matcher<? super Integer>>(
new MatcherOf<>(
value -> {
return value.equals(1);
}
),
new MatcherOf<>(
value -> {
return value.equals(2);
}
)
)
)
);
}

Expand All @@ -223,7 +283,20 @@ public void worksWithExecServiceProcIterable() throws Exception {
).value();
MatcherAssert.assertThat(
list,
Matchers.containsInAnyOrder(1, 2)
new IsIterableContainingInAnyOrder<Integer>(
new CollectionOf<Matcher<? super Integer>>(
new MatcherOf<>(
value -> {
return value.equals(1);
}
),
new MatcherOf<>(
value -> {
return value.equals(2);
}
)
)
)
);
}

Expand Down

1 comment on commit 995eb06

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 995eb06 May 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 829-59f865f8 discovered in src/test/java/org/cactoos/scalar/AndInThreadsTest.java and submitted as #898. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.