Java源码示例:org.cactoos.list.ListOf
示例1
@Test
public void extractHeadersFromDict() {
final List<Kvp> kvps = new ListOf<>(
new Headers.Of(
new HashDict(
new KvpOf("test", "test"),
new KvpOf("h.kfirst", "first"),
new KvpOf("h.ksecond", "second")
)
)
);
MatcherAssert.assertThat(
kvps.size(),
new IsEqual<>(2)
);
MatcherAssert.assertThat(
kvps.get(0).key(),
new IsEqual<>("kfirst")
);
MatcherAssert.assertThat(
kvps.get(1).key(),
new IsEqual<>("ksecond")
);
}
示例2
@SuppressWarnings("unchecked")
@Test
public void containsAllHeadersNames() {
MatcherAssert.assertThat(
"Can't get the header names",
Collections.list(
new HttpServletRequestFake(
new RqWithHeaders(
new RqFake(),
"AnyHeader: anyValue",
"CrazyHeader: crazyValue"
)
).getHeaderNames()
),
new IsIterableContainingInAnyOrder<>(
new ListOf<>(
new IsEqual<>("host"),
new IsEqual<>("crazyheader"),
new IsEqual<>("anyheader")
)
)
);
}
示例3
@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();
}
示例4
@Test
@SuppressWarnings("unchecked")
public void partitionedLastPartitionSmaller() {
new Assertion<>(
"Can't generate a Partitioned of size 2 last partition smaller.",
new ListOf<>(
new Partitioned<>(2, new ListOf<>(1, 2, 3).iterator())
),
new IsEqual<>(
new ListOf<>(
new ListOf<>(1, 2),
new ListOf<>(3)
)
)
).affirm();
}
示例5
@Test
public void takesDefaultContentType() {
MatcherAssert.assertThat(
new HtContentType(
new HtHead(
new InputOf(
new Joined(
"\r\n",
"HTTP/1.1 200 OK",
"",
"Hello, dude!"
)
)
)
).value(),
new IsEqual<>(new ListOf<>("application/octet-stream"))
);
}
示例6
/**
* Ctor.
* @param threads The quantity of threads which will be used within the
* {@link ExecutorService}.
* @param tasks The tasks to be executed concurrently.
* @param timeout The maximum time to wait.
* @param unit The time unit of the timeout argument.
* @checkstyle IndentationCheck (20 lines)
* @checkstyle ParameterNumberCheck (5 lines)
*/
public Timed(
final int threads,
final Iterable<Scalar<T>> tasks,
final long timeout,
final TimeUnit unit
) {
this(
todo -> {
final ExecutorService executor = Executors.newFixedThreadPool(
threads
);
try {
return executor.invokeAll(new ListOf<>(todo), timeout, unit);
} finally {
executor.shutdown();
}
},
tasks
);
}
示例7
/**
* Tests the bug described in #577.
*
* @throws Exception If there is some error inside
*/
@Test
@Ignore("See puzzle above")
public void contentDispositionShouldBeRecognized() throws Exception {
new RqMtFake(
new RqFake(),
new RqWithHeader(
new RqFake(new ListOf<>(""), ""),
new FormattedText(
RqMtFakeTest.CONTENT_DISP, "name=\"field1\""
).asString()
),
new RqWithHeader(
new RqFake("", "", "field2Value"),
new FormattedText(
RqMtFakeTest.CONTENT_DISP, "name=\"field2\""
).asString()
)
);
}
示例8
@Test
public void worksInThreads() {
new Assertion<>(
"must work well in multiple threads",
scalar -> {
new Assertion<>(
"must compute value once",
scalar,
new ScalarHasValue<>(scalar.value())
).affirm();
return true;
},
new RunsInThreads<>(
new Unchecked<>(
new Solid<>(() -> new ListOf<>(1, 2))
)
)
).affirm();
}
示例9
/**
* FbStatus can react to Condition.
* @throws Exception If some problem inside
*/
@Test
public void reactsToCondition() throws Exception {
final RqFallback req = new RqFallback.Fake(
HttpURLConnection.HTTP_MOVED_PERM
);
MatcherAssert.assertThat(
new RsPrint(
new FbStatus(
new Filtered<>(
status -> {
return status == HttpURLConnection.HTTP_MOVED_PERM
|| status == HttpURLConnection.HTTP_MOVED_TEMP;
},
new ListOf<>(
HttpURLConnection.HTTP_MOVED_PERM,
HttpURLConnection.HTTP_MOVED_TEMP
)
),
new FbFixed(new RsText("response text"))
).route(req).get()
).printBody(),
Matchers.startsWith("response")
);
}
示例10
@Test
public void mustSortIntegerArrayAsSetInAscendingOrder() {
new Assertion<>(
"Must keep unique integer numbers sorted",
new Sorted<>(
Integer::compareTo,
2, 1, 3, 2, 1
),
new IsIterableContainingInOrder<>(
new ListOf<Matcher<? super Integer>>(
new IsEqual<>(1),
new IsEqual<>(2),
new IsEqual<>(3)
)
)
).affirm();
}
示例11
@Test
public void mustSortTextIterableAsSetUsingCustomCOmparator() {
new Assertion<>(
"Must keep unique integer numbers sorted in descending order",
new Sorted<>(
(first, second) -> {
final String left = new UncheckedText(first).asString();
final String right = new UncheckedText(second).asString();
return left.compareTo(right);
},
new TextOf("cd"),
new TextOf("ab"),
new TextOf("gh"),
new TextOf("ef")
),
new IsIterableContainingInOrder<>(
new ListOf<Matcher<? super Text>>(
new IsEqual<>(new TextOf("ab")),
new IsEqual<>(new TextOf("cd")),
new IsEqual<>(new TextOf("ef")),
new IsEqual<>(new TextOf("gh"))
)
)
).affirm();
}
示例12
@Test
public void mustNotBeEqualToSortedSet() {
new Assertion<>(
"Sorted set must not be equal to the tested collection",
new Sorted<>(
Integer::compareTo,
2, 1, 3, 2, 1
),
new IsNot<>(
new IsIterableContainingInOrder<>(
new ListOf<Matcher<? super Integer>>(
new IsEqual<>(1),
new IsEqual<>(3),
new IsEqual<>(2)
)
))
).affirm();
}
示例13
@Test
@SuppressWarnings("unchecked")
public void partitionedOne() {
MatcherAssert.assertThat(
"Can't generate a Partitioned of partition size 1.",
new ArrayList<>(
new ListOf<>(
new Partitioned<>(1, new ListOf<>(1, 2, 3).iterator())
)
),
Matchers.equalTo(
new ListOf<>(
Collections.singletonList(1), Collections.singletonList(2),
Collections.singletonList(3)
)
)
);
}
示例14
@Test
public void reversesIterator() {
new Assertion<>(
"Must reverse the iterator",
new ListOf<>(
new Reversed<>(
new IteratorOf<>("c", "a", "c", "t", "o", "o", "s")
)
),
new IsEqual<>(
new ListOf<>(
new IteratorOf<>("s", "o", "o", "t", "c", "a", "c")
)
)
).affirm();
}
示例15
@Test
public void containsContentInRequestBody() throws IOException {
final String content = "My name is neo!";
MatcherAssert.assertThat(
"Can't add a body to servlet request",
new RqPrint(
new RqFrom(
new HttpServletRequestFake(
new RqFake(
new ListOf<>(RqFromTest.EOL),
content
)
)
)
).printBody(),
new StringContains(content)
);
}
示例16
/**
* Ctor.
* @param check Check
* @param fallback Fallback
*/
@SuppressWarnings
(
{
"PMD.CallSuperInConstructor",
"PMD.ConstructorOnlyInitializesOrCallOtherConstructors"
}
)
public FbStatus(final Iterable<Integer> check,
final Scalar<Fallback> fallback) {
super(
new Fallback() {
@Override
public Opt<Response> route(final RqFallback req)
throws Exception {
Opt<Response> rsp = new Opt.Empty<>();
if (new ListOf<>(check).contains(req.code())) {
rsp = fallback.get().route(req);
}
return rsp;
}
}
);
}
示例17
@Test
public final void testIntegerFibonacciRange() {
MatcherAssert.assertThat(
"Can't generate a range of fibonacci integers",
new ListOf<>(
new RangeOf<>(
1,
100,
new Func<Integer, Integer>() {
private int last;
@Override
public Integer apply(
final Integer input) throws Exception {
final int next = this.last + input;
this.last = input;
return next;
}
}
)
),
Matchers.contains(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89)
);
}
示例18
/**
* Ctor.
* @param prms List of Param
*/
public ParamsNamed(final Param... prms) {
this.prms = new Unchecked<>(
new Sticky<>(
() -> new ListOf<>(prms)
)
);
}
示例19
@Test
public void syncIteratorReturnsCorrectValuesWithInternalLock() {
new Assertion<>(
"Unexpected value found.",
new ListOf<>(
new Synced<>(
new IteratorOf<>("a", "b")
)
).toArray(),
new IsEqual<>(new Object[]{"a", "b"})
).affirm();
}
示例20
@Test
public void compileToXml() throws Exception {
MatcherAssert.assertThat(
XhtmlMatchers.xhtml(
new Xembler(
new XmlTypeDef(
"Book",
new ListOf<>(
"Text"
),
new ListOf<>(
new Method(
"page",
Collections.emptyList(),
"Page"
)
)
)
).xml()
),
Matchers.allOf(
XhtmlMatchers.hasXPath("/type[@name = 'Book']"),
XhtmlMatchers.hasXPath("/type/super/type[@name = 'Text']"),
XhtmlMatchers.hasXPath("/type/methods[count(method) = 1]")
)
);
}
示例21
@Test
@SuppressWarnings("unchecked")
public void partitionedEqualSize() {
MatcherAssert.assertThat(
"Can't generate a Partitioned of partition size 2.",
new ArrayList<>(
new ListOf<>(
new Partitioned<>(2, new ListOf<>(1, 2, 3, 4).iterator())
)
),
Matchers.equalTo(
new ListOf<>(new ListOf<>(1, 2), new ListOf<>(3, 4))
)
);
}
示例22
@Test
public void withDoubleCollection() {
MatcherAssert.assertThat(
new MinOf(
new ListOf<>(1.0d, 2.0d, 3.0d, 4.0d).toArray(new Double[4])
).intValue(),
Matchers.equalTo(1)
);
MatcherAssert.assertThat(
new MinOf(
new ListOf<>(1.0d, 2.0d, 3.0d, 4.0d).toArray(new Double[4])
).longValue(),
Matchers.equalTo(1L)
);
MatcherAssert.assertThat(
new MinOf(
new ListOf<>(1.0d, 2.0d, 3.0d, 4.0d).toArray(new Double[4])
).doubleValue(),
Matchers.equalTo(1.0d)
);
MatcherAssert.assertThat(
new MinOf(
new ListOf<>(1.0d, 2.0d, 3.0d, 4.0d).toArray(new Double[4])
).floatValue(),
Matchers.equalTo(1.0f)
);
}
示例23
/**
* Ctor.
* @param maps Maps to merge.
*/
@SafeVarargs
public Merged(final Map<K, V>... maps) {
super(() -> new MapOf<>(
new ListOf<>(maps)
.stream()
.flatMap(map -> map.entrySet().stream())
.collect(Collectors.toList())
)
);
}
示例24
@Test
public void worksWithExecServiceProcValues() throws Exception {
final List<Integer> list = new Synced<>(new ListOf<>());
final ExecutorService service = Executors.newSingleThreadExecutor();
new AndInThreads(
service,
new ProcNoNulls<Integer>(list::add),
1, 2
).value();
MatcherAssert.assertThat(
list,
new IsIterableContainingInAnyOrder<Integer>(
new ListOf<Matcher<? super Integer>>(
new MatcherOf<>(
value -> {
return value.equals(1);
}
),
new MatcherOf<>(
value -> {
return value.equals(2);
}
)
)
)
);
}
示例25
/**
* Ctor.
* @param exc The executor.
* @param tasks The tasks to be executed concurrently.
* @param timeout The maximum time to wait.
* @param unit The time unit of the timeout argument.
* @checkstyle ParameterNumberCheck (5 lines)
*/
public Timed(
final ExecutorService exc,
final Iterable<Scalar<T>> tasks,
final long timeout,
final TimeUnit unit
) {
this(
input -> exc.invokeAll(new ListOf<>(input), timeout, unit),
tasks
);
}
示例26
/**
* New formatted string with specified locale.
*
* @param ptn Pattern
* @param locale Format locale
* @param arguments Arguments
*/
public FormattedText(
final String ptn,
final Locale locale,
final Object... arguments
) {
this(ptn, locale, new ListOf<>(arguments));
}
示例27
/**
* New formatted string with specified locale.
*
* @param ptn Pattern
* @param locale Format locale
* @param arguments Arguments
*/
public FormattedText(
final Text ptn,
final Locale locale,
final Object... arguments
) {
this(ptn, locale, new ListOf<>(arguments));
}
示例28
@Test
public void lengthOfEmptyIterator() {
new Assertion<>(
"Must calculate length of empty iterator",
new LengthOf(
new ListOf<>()
).intValue(),
new IsEqual<>(0)
).affirm();
}
示例29
@Override
public SumOf value() {
return new SumOf(
new IterableOf<>(
new ListOf<>(this.scalars)
.stream()
.map(
scalar -> new Unchecked<>(scalar).value()
).collect(Collectors.toList())
)
);
}
示例30
@Test
public void withIterableOfInts() {
final Collection<Integer> ints = new ListOf<>(1, 2, 3, 4);
MatcherAssert.assertThat(
new SumOf(ints).intValue(),
new IsEqual<>(10)
);
}