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

#829 Turn AndInThreadsTest Stable #874

Merged
merged 2 commits into from
May 22, 2018
Merged
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
123 changes: 98 additions & 25 deletions src/test/java/org/cactoos/scalar/AndInThreadsTest.java
Original file line number Diff line number Diff line change
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)
);
Copy link
Contributor

Choose a reason for hiding this comment

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

@pbenety What do you think about using cactoos SyncCollection here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@paulodamaso I thought about it but SyncCollection does not implement add method.

Copy link
Contributor

Choose a reason for hiding this comment

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

@pbenety Right, it needs to be mutable for the test, so that's ok for now. Just open a @todo about removing these static methods access from here so we can think about it in the future.

Copy link
Contributor Author

@pbenety pbenety May 22, 2018

Choose a reason for hiding this comment

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

@paulodamaso @todo added.

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)
);
Copy link
Contributor

@paulodamaso paulodamaso May 17, 2018

Choose a reason for hiding this comment

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

@pbenety What do you think about using cactoos SyncCollection here too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@paulodamaso I thought about it but SyncCollection does not implement add method.

Copy link
Contributor

Choose a reason for hiding this comment

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

@pbenety Same as above.

Copy link
Contributor Author

@pbenety pbenety May 22, 2018

Choose a reason for hiding this comment

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

@paulodamaso @todo added.

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)
);
Copy link
Contributor

@paulodamaso paulodamaso May 17, 2018

Choose a reason for hiding this comment

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

@pbenety What do you think about using cactoos SyncCollection here, again?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@paulodamaso I thought about it but SyncCollection does not implement add method.

Copy link
Contributor

Choose a reason for hiding this comment

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

@pbenety Same as above.

Copy link
Contributor Author

@pbenety pbenety May 22, 2018

Choose a reason for hiding this comment

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

@paulodamaso @todo added.

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