Java Code Examples for org.cactoos.list.ListOf

The following examples show how to use org.cactoos.list.ListOf. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: verano-http   Author: Vatavuk   File: HeadersOfTest.java    License: MIT License 6 votes vote down vote up
@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")
    );
}
 
Example #2
Source Project: takes   Author: yegor256   File: HttpServletRequestFakeTest.java    License: MIT License 6 votes vote down vote up
@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")
            )
        )
    );
}
 
Example #3
Source Project: cactoos   Author: yegor256   File: IteratorOfBytesTest.java    License: MIT License 6 votes vote down vote up
@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();
}
 
Example #4
Source Project: cactoos   Author: yegor256   File: PartitionedTest.java    License: MIT License 6 votes vote down vote up
@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();
}
 
Example #5
Source Project: cactoos-http   Author: yegor256   File: HtContentTypeTest.java    License: MIT License 6 votes vote down vote up
@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"))
    );
}
 
Example #6
Source Project: cactoos   Author: yegor256   File: Timed.java    License: MIT License 6 votes vote down vote up
/**
 * 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
    );
}
 
Example #7
Source Project: takes   Author: yegor256   File: RqMtFakeTest.java    License: MIT License 6 votes vote down vote up
/**
 * 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()
        )
    );
}
 
Example #8
Source Project: cactoos   Author: yegor256   File: SolidTest.java    License: MIT License 6 votes vote down vote up
@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();
}
 
Example #9
Source Project: takes   Author: yegor256   File: FbStatusTest.java    License: MIT License 6 votes vote down vote up
/**
 * 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")
    );
}
 
Example #10
Source Project: cactoos   Author: yegor256   File: SortedTest.java    License: MIT License 6 votes vote down vote up
@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();
}
 
Example #11
Source Project: cactoos   Author: yegor256   File: SortedTest.java    License: MIT License 6 votes vote down vote up
@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();
}
 
Example #12
Source Project: cactoos   Author: yegor256   File: SortedTest.java    License: MIT License 6 votes vote down vote up
@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();
}
 
Example #13
Source Project: cactoos   Author: yegor256   File: PartitionedTest.java    License: MIT License 6 votes vote down vote up
@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)
            )
        )
    );
}
 
Example #14
Source Project: cactoos   Author: yegor256   File: ReversedTest.java    License: MIT License 6 votes vote down vote up
@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();
}
 
Example #15
Source Project: takes   Author: yegor256   File: RqFromTest.java    License: MIT License 6 votes vote down vote up
@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)
    );
}
 
Example #16
Source Project: takes   Author: yegor256   File: FbStatus.java    License: MIT License 6 votes vote down vote up
/**
 * 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;
            }
        }
    );
}
 
Example #17
Source Project: cactoos   Author: yegor256   File: RangeOfTest.java    License: MIT License 6 votes vote down vote up
@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)
    );
}
 
Example #18
Source Project: cactoos-jdbc   Author: fabriciofx   File: ParamsNamed.java    License: MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param prms List of Param
 */
public ParamsNamed(final Param... prms) {
    this.prms = new Unchecked<>(
        new Sticky<>(
            () -> new ListOf<>(prms)
        )
    );
}
 
Example #19
Source Project: cactoos   Author: yegor256   File: SyncedTest.java    License: MIT License 5 votes vote down vote up
@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();
}
 
Example #20
Source Project: eo   Author: yegor256   File: XmlTypeDefTest.java    License: MIT License 5 votes vote down vote up
@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]")
        )
    );
}
 
Example #21
Source Project: cactoos   Author: yegor256   File: PartitionedTest.java    License: MIT License 5 votes vote down vote up
@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))
        )
    );
}
 
Example #22
Source Project: cactoos   Author: yegor256   File: MinOfTest.java    License: MIT License 5 votes vote down vote up
@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)
    );
}
 
Example #23
Source Project: cactoos   Author: yegor256   File: Merged.java    License: MIT License 5 votes vote down vote up
/**
 * 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())
        )
    );
}
 
Example #24
Source Project: cactoos   Author: yegor256   File: AndInThreadsTest.java    License: MIT License 5 votes vote down vote up
@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);
                    }
                )
            )
        )
    );
}
 
Example #25
Source Project: cactoos   Author: yegor256   File: Timed.java    License: MIT License 5 votes vote down vote up
/**
 * 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
    );
}
 
Example #26
Source Project: cactoos   Author: yegor256   File: FormattedText.java    License: MIT License 5 votes vote down vote up
/**
 * 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));
}
 
Example #27
Source Project: cactoos   Author: yegor256   File: FormattedText.java    License: MIT License 5 votes vote down vote up
/**
 * 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));
}
 
Example #28
Source Project: cactoos   Author: yegor256   File: LengthOfTest.java    License: MIT License 5 votes vote down vote up
@Test
public void lengthOfEmptyIterator() {
    new Assertion<>(
        "Must calculate length of empty iterator",
        new LengthOf(
            new ListOf<>()
        ).intValue(),
        new IsEqual<>(0)
    ).affirm();
}
 
Example #29
Source Project: cactoos   Author: yegor256   File: SumOfScalar.java    License: MIT License 5 votes vote down vote up
@Override
public SumOf value() {
    return new SumOf(
        new IterableOf<>(
            new ListOf<>(this.scalars)
                .stream()
                .map(
                    scalar -> new Unchecked<>(scalar).value()
                ).collect(Collectors.toList())
        )
    );
}
 
Example #30
Source Project: cactoos   Author: yegor256   File: SumOfTest.java    License: MIT License 5 votes vote down vote up
@Test
public void withIterableOfInts() {
    final Collection<Integer> ints = new ListOf<>(1, 2, 3, 4);
    MatcherAssert.assertThat(
        new SumOf(ints).intValue(),
        new IsEqual<>(10)
    );
}