下面列出了org.hamcrest.collection.IsEmptyCollection#org.llorllale.cactoos.matchers.HasValues 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Execute the tasks concurrently using {@link Threads} when
* {@link ExecutorService} was initiated by {@link Threads} itself.
*/
@Test
public void containsValuesWithInlineExecutorService() {
this.repeat(
arg -> new Assertion<>(
// @checkstyle LineLength (1 line)
"contains results from the callables when using the inline executor service",
new Threads<String>(
3,
() -> {
this.sleep();
return "txt 1";
},
() -> {
this.sleep();
return "txt 2";
},
() -> {
this.sleep();
return "txt 3";
}
),
new HasValues<>("txt 1", "txt 2", "txt 3")
).affirm()
);
}
@Test
public void shuffleIterable() throws Exception {
new Assertion<>(
"Must shuffle elements in iterator",
new IterableOf<>(
new Shuffled<>(
new IteratorOf<>(
"a", "b"
)
)
),
new HasValues<>(
"a", "b"
)
).affirm();
}
@Test
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public void sortsIterable() throws Exception {
new Assertion<>(
"Must sort elements in iterator",
new IterableOf<>(
new Sorted<>(
new IteratorOf<>(
"one", "two", "three", "four"
)
)
),
new HasValues<>(
"four", "one", "three", "two"
)
).affirm();
}
@Test
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public void skipIterator() {
new Assertion<>(
"Must skip elements in iterator",
new IterableOf<>(
new Skipped<>(
2,
new IteratorOf<>(
"one", "two", "three", "four"
)
)
),
new HasValues<>(
"three",
"four"
)
).affirm();
}
@Test
public void canBeConstructedFromString() throws Exception {
final Iterator<Byte> itr = new IteratorOfBytes(
"F"
);
new Assertion<>(
"Must have 1 element",
new ListOf<>(
itr.next(),
itr.hasNext()
),
new HasValues<Object>(
(byte) 'F', false
)
).affirm();
}
@Test
public void canBeConstructedFromText() throws Exception {
final Iterator<Byte> itr = new IteratorOfBytes(
new TextOf("ABC")
);
new Assertion<>(
"Must have 3 elements",
new ListOf<>(
itr.next(),
itr.next(),
itr.next(),
itr.hasNext()
),
new HasValues<Object>(
(byte) 'A', (byte) 'B', (byte) 'C', false
)
).affirm();
}
@Test
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public void headIterator() throws Exception {
new Assertion<>(
"Must skip elements in iterator",
new IterableOf<>(
new HeadOf<>(
2,
new IteratorOf<>(
"one", "two", "three", "four"
)
)
),
new HasValues<>(
"one",
"two"
)
).affirm();
}
@Test
public void iteratesForEachRemaining() throws Exception {
final List<String> lst = new ArrayList<>(2);
new HeadOf<>(
2,
new IteratorOf<>(
"one", "two", "three", "four"
)
).forEachRemaining(
lst::add
);
new Assertion<>(
"Should iterate over 2 head elements",
lst,
new HasValues<>(
"one",
"two"
)
).affirm();
}
/**
* Execute the tasks concurrently using {@link Threads} when
* {@link ExecutorService} was initiated by someone else.
*/
@Test
public void containsResults() {
this.repeat(
arg -> {
final ExecutorService extor = Executors.newFixedThreadPool(3);
try {
new Assertion<>(
"contains results from callables",
new Threads<String>(
extor,
() -> {
this.sleep();
return "txt 1";
},
() -> {
this.sleep();
return "txt 2";
},
() -> {
this.sleep();
return "txt 3";
}
),
new HasValues<>("txt 1", "txt 2", "txt 3")
).affirm();
} finally {
extor.shutdown();
if (!extor.awaitTermination(1L, TimeUnit.SECONDS)) {
extor.shutdownNow();
}
}
}
);
}
/**
* Execute the tasks concurrently using {@link Timed} when
* {@link ExecutorService} was initiated by {@link Timed} itself.
*/
@Test
public void containsValuesWithInlineExecutorService() {
this.repeat(
arg -> new Assertion<>(
"Contains results from the callables when using the inline executor service",
new Timed<String>(
TimedTest.THREADS,
1L,
TimeUnit.SECONDS,
() -> {
this.sleep();
return TimedTest.FIRST_TEXT;
},
() -> {
this.sleep();
return TimedTest.SECOND_TEXT;
},
() -> {
this.sleep();
return TimedTest.THIRD_TEXT;
}
),
new HasValues<>(TimedTest.FIRST_TEXT, TimedTest.SECOND_TEXT, TimedTest.THIRD_TEXT)
).affirm()
);
}
@Test
public void shufflesList() throws Exception {
MatcherAssert.assertThat(
"Can't shuffle elements in list",
new Shuffled<>(
new ListOf<>(1, 0, -1, -1, 2)
),
new HasValues<>(-1)
);
}
@Test
public void iterator() {
new Assertion<>(
"iterator() is equal to original",
() -> new Immutable<>(
new ListOf<>(1, 2)
).iterator(),
new HasValues<>(1, 2)
).affirm();
}
@Test
public void listIterator() {
new Assertion<>(
"listIterator() is equal to original",
() -> new Immutable<>(
new ListOf<>("a", "b", "c", "b")
).listIterator(),
new HasValues<>("a", "b", "c", "b")
).affirm();
}
@Test
public void testListIterator() {
new Assertion<>(
"listIterator(int) is equal to original",
() -> new Immutable<>(
new ListOf<>("a", "b", "c", "b")
).listIterator(2),
new HasValues<>("c", "b")
).affirm();
}
@Test
public void iterator() {
new Assertion<>(
"iterator() is equal to original",
() -> new Immutable<>(
new ListOf<>(1, 2)
).iterator(),
new HasValues<>(1, 2)
).affirm();
}
@Test
public void shuffleArray() throws Exception {
new Assertion<>(
"Must shuffle an iterable",
new Shuffled<>(
new IterableOf<>(
6, 2, 5
)
),
new HasValues<>(2, 5, 6)
).affirm();
}
@Test
public void shuffleCollection() {
new Assertion<>(
"Must shuffle elements in collection",
new Shuffled<>(new ListOf<>(1, 2, 0, -1)),
new HasValues<>(1, 2, 0, -1)
).affirm();
}
@Test
public void shufflesIterable() {
new Assertion<>(
"Must shuffle elements in iterable",
new Shuffled<>(new IterableOf<>(1, 2, 0, -1)),
new HasValues<>(1, 2, 0, -1)
).affirm();
}
@Test
public void convertsCharactersToIterable() {
new Assertion<>(
"Must create Iterable from Text",
new IterableOfChars(new TextOf("txt")),
new HasValues<>('t', 'x', 't')
).affirm();
}
@Test
public void convertsTextToIterableOfBytes() {
new Assertion<>(
"Must create Iterable from Text",
new IterableOfBytes(
new TextOf("ABC")
),
new HasValues<>(
(byte) 'A', (byte) 'B', (byte) 'C'
)
).affirm();
}
@Test
public void convertsBytesToIterable() {
final byte[] bytes = "txt".getBytes();
new Assertion<>(
"Must create Iterable from bytes",
new IterableOfBytes(bytes),
new HasValues<>(bytes[0], bytes[1], bytes[2])
).affirm();
}
@Test
public void canBeConstructedFromText() {
new Assertion<>(
"Iterator must contain all characters of the string",
new IterableOf<>(
new IteratorOfChars(
new TextOf("abc")
)
),
new HasValues<>('a', 'b', 'c')
).affirm();
}
@Test
public void convertStringsToIterator() {
new Assertion<>(
"Must create an iterator of strings",
new IterableOf<>(
new IteratorOf<>(
"a", "b", "c"
)
),
new HasValues<>(
"a", "b", "c"
)
).affirm();
}
@Test
public void reportsParams() throws Exception {
final String name = "name";
final Map<String, Object> sample = ReportDataTest.args();
final ReportData data = new ReportData(name, sample);
new Assertion<>(
"Must returns args",
data.params().entrySet(),
new HasValues<>(sample.entrySet())
).affirm();
}
@Test
void buildsConnections() {
final Map<String, Node> byname = new MapOf<>(
Node::name,
node -> node,
new XmlGraph(
new Skeleton(new FakeBase(XmlGraphTest.CLASS_NAME))
).nodes()
);
final Node one = byname.get(XmlGraphTest.METHOD_ONE);
final Node two = byname.get(XmlGraphTest.METHOD_TWO);
final Node three = byname.get(XmlGraphTest.METHOD_THREE);
final Node four = byname.get(XmlGraphTest.METHOD_FOUR);
final Node five = byname.get(XmlGraphTest.METHOD_FIVE);
new Assertion<>(
"Must build nodes connections when called",
one.connections(),
new HasValues<>(two)
).affirm();
new Assertion<>(
"Must build nodes connections when called or calling",
two.connections(),
new HasValues<>(one, four)
).affirm();
new Assertion<>(
"Must build nodes connections when neither called nor calling",
three.connections(),
new IsEmptyCollection<>()
).affirm();
new Assertion<>(
"Must build nodes connections when calling",
four.connections(),
new HasValues<>(two)
).affirm();
new Assertion<>(
"Must build nodes connections when throwing",
five.connections(),
new IsEmptyCollection<>()
).affirm();
}
@Test
public void status() {
final HttpServletResponse sresp = new HttpServletResponseFake(
new RsEmpty()
);
// @checkstyle MagicNumber (1 line)
sresp.setStatus(502);
new Assertion<>(
"Can't set a status in servlet response",
sresp.getHeaders(HttpServletResponseFakeTest.VERSION),
new HasValues<>(
HttpServletResponseFakeTest.ERROR
)
).affirm();
}
@Test
public void sendError() throws IOException {
final HttpServletResponse sresp = new HttpServletResponseFake(
new RsEmpty()
);
// @checkstyle MagicNumber (1 line)
sresp.sendError(101, "Switching Protocol");
new Assertion<>(
"Can't send a error in servlet response",
sresp.getHeaders(HttpServletResponseFakeTest.VERSION),
new HasValues<>(
HttpServletResponseFakeTest.INFO
)
).affirm();
}
/**
* Execute the tasks concurrently using {@link Timed} when
* {@link ExecutorService} was initiated by someone else.
*/
@Test
public void containsResults() {
this.repeat(
arg -> {
final ExecutorService extor = Executors.newFixedThreadPool(TimedTest.THREADS);
try {
new Assertion<>(
"Contains results from callables",
new Timed<String>(
extor,
1L,
TimeUnit.SECONDS,
() -> {
this.sleep();
return TimedTest.FIRST_TEXT;
},
() -> {
this.sleep();
return TimedTest.SECOND_TEXT;
},
() -> {
this.sleep();
return TimedTest.THIRD_TEXT;
}
),
new HasValues<>(
TimedTest.FIRST_TEXT,
TimedTest.SECOND_TEXT,
TimedTest.THIRD_TEXT
)
).affirm();
} finally {
extor.shutdown();
if (!extor.awaitTermination(1L, TimeUnit.SECONDS)) {
extor.shutdownNow();
}
}
}
);
}