org.cactoos.list.ListOf Java Examples

The following examples show how to use org.cactoos.list.ListOf. 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 File: HeadersOfTest.java    From verano-http with 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 File: FbStatusTest.java    From takes with 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 #3
Source File: SolidTest.java    From cactoos with 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 #4
Source File: SortedTest.java    From cactoos with 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 #5
Source File: SortedTest.java    From cactoos with 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 #6
Source File: SortedTest.java    From cactoos with 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 #7
Source File: RqMtFakeTest.java    From takes with 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 File: RqFromTest.java    From takes with 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 #9
Source File: FbStatus.java    From takes with 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 #10
Source File: PartitionedTest.java    From cactoos with 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 #11
Source File: Timed.java    From cactoos with 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 #12
Source File: HtContentTypeTest.java    From cactoos-http with 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 #13
Source File: RangeOfTest.java    From cactoos with 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 #14
Source File: PartitionedTest.java    From cactoos with 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 #15
Source File: IteratorOfBytesTest.java    From cactoos with 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 #16
Source File: HttpServletRequestFakeTest.java    From takes with 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 #17
Source File: ReversedTest.java    From cactoos with 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 #18
Source File: ImmutableTest.java    From cactoos with MIT License 5 votes vote down vote up
@Test
public void toArray() {
    new Assertion<>(
        "toArray() must be equals to original",
        new Immutable<>(
            new ListOf<>(1, 2)
        ).toArray(),
        new IsEqual<>(
            new ListOf<>(1, 2).toArray()
        )
    ).affirm();
}
 
Example #19
Source File: MappedTest.java    From cactoos with MIT License 5 votes vote down vote up
@Test
public void string() {
    MatcherAssert.assertThat(
        "Can't convert to string",
        new Mapped<Integer, Integer>(
            x -> x * 2,
            new ListOf<>(1, 2, 3)
        ).toString(),
        Matchers.equalTo("2, 4, 6")
    );
}
 
Example #20
Source File: RqFromTest.java    From takes with MIT License 5 votes vote down vote up
@Test
public void containsHostHeaderInHeader() throws IOException {
    final String method = "GET /one-more-test";
    final String header = "Host: www.thesite.com";
    MatcherAssert.assertThat(
        "Can't set a host in a servlet request",
        new RqPrint(
            new RqFrom(
                new HttpServletRequestFake(
                    new RqFake(
                        new ListOf<>(
                            method,
                            header
                        ),
                        ""
                    )
                )
            )
        ).printHead(),
        new StringStartsWith(
            new Joined(
                RqFromTest.EOL,
                method,
                header,
                RqFromTest.LOCAL_ADDRESS,
                RqFromTest.REMOTE_ADDRESS
            ).asString()
        )
    );
}
 
Example #21
Source File: ImmutableTest.java    From cactoos with MIT License 5 votes vote down vote up
@Test
public void testToString() {
    new Assertion<>(
        "toString() must be equals to original",
        new Immutable<>(
            new ListOf<>(1, 2, 3)
        ).toString(),
        new IsEqual<>(
            new ListOf<>(1, 2, 3).toString()
        )
    ).affirm();
}
 
Example #22
Source File: ImmutableTest.java    From cactoos with MIT License 5 votes vote down vote up
@Test
public void contains() {
    new Assertion<>(
        "contains() must be equals to original",
        new Immutable<>(
            new ListOf<>(1, 2)
        ).contains(1),
        new IsEqual<>(
            new ListOf<>(1, 2).contains(1)
        )
    ).affirm();
}
 
Example #23
Source File: ImmutableTest.java    From cactoos with MIT License 5 votes vote down vote up
@Test
public void retainAll() {
    new Assertion<>(
        "retainAll() must throw exception",
        () -> new Immutable<>(
            new ListOf<>(1, 2, 3)
        ).retainAll(new ListOf<>(1)),
        new Throws<>(
            new MatcherOf<>(
                (String msg) -> msg.equals("#retainAll(): the collection is read-only")
            ),
            UnsupportedOperationException.class
        )
    ).affirm();
}
 
Example #24
Source File: ParamsNamed.java    From cactoos-jdbc with 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 #25
Source File: ImmutableTest.java    From cactoos with MIT License 5 votes vote down vote up
@Test
public void add() {
    new Assertion<>(
        "add() must throw exception",
        () -> new Immutable<>(
            new ListOf<>(1, 2)
        ).add(3),
        new Throws<>(
            new MatcherOf<>(
                (String msg) -> msg.equals("#add(): the collection is read-only")
            ),
            UnsupportedOperationException.class
        )
    ).affirm();
}
 
Example #26
Source File: ImmutableTest.java    From cactoos with MIT License 5 votes vote down vote up
@Test
public void removeAll() {
    new Assertion<>(
        "removeAll() must throw exception",
        () -> new Immutable<>(
            new ListOf<>(1, 2, 3)
        ).removeAll(new ListOf<>(2, 3)),
        new Throws<>(
            new MatcherOf<>(
                (String msg) -> msg.equals("#removeAll(): the collection is read-only")
            ),
            UnsupportedOperationException.class
        )
    ).affirm();
}
 
Example #27
Source File: ImmutableTest.java    From cactoos with MIT License 5 votes vote down vote up
@Test
public void testToArray() {
    new Assertion<>(
        "toArray(T[]) must be equals to original",
        new Immutable<>(
            new ListOf<>(1, 2, 3)
        ).toArray(new Integer[3]),
        new IsEqual<>(
            new ListOf<>(1, 2, 3).toArray(new Integer[3])
        )
    ).affirm();
}
 
Example #28
Source File: ImmutableTest.java    From cactoos with MIT License 5 votes vote down vote up
@Test
public void addAll() {
    new Assertion<>(
        "addAll() must throw exception",
        () -> new Immutable<>(
            new ListOf<>(1, 2)
        ).addAll(new ListOf<>(3, 4)),
        new Throws<>(
            new MatcherOf<>(
                (String msg) -> msg.equals("#addAll(): the collection is read-only")
            ),
            UnsupportedOperationException.class
        )
    ).affirm();
}
 
Example #29
Source File: ImmutableTest.java    From cactoos with MIT License 5 votes vote down vote up
@Test
public void containsAll() {
    final ListOf<Integer> another = new ListOf<>(3, 4, 5);
    new Assertion<>(
        "containsAll() must be equals to original",
        new Immutable<>(
            new ListOf<>(1, 2, 3)
        ).containsAll(another),
        new IsEqual<>(
            new ListOf<>(1, 2, 3).containsAll(another)
        )
    ).affirm();
}
 
Example #30
Source File: DisjointTest.java    From jpeek with MIT License 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void calculatesDisjointSets() throws Exception {
    final Node one = new Node.Simple("one");
    final Node two = new Node.Simple("two");
    final Node three = new Node.Simple("three");
    final Node four = new Node.Simple("four");
    final Node five = new Node.Simple("five");
    final Node six = new Node.Simple("six");
    one.connections().add(two);
    two.connections().addAll(new ListOf<>(one, three));
    three.connections().add(two);
    four.connections().add(five);
    five.connections().add(four);
    final Graph graph = new FakeGraph(
        new ListOf<>(one, two, three, four, five, six)
    );
    // @checkstyle MagicNumberCheck (50 lines)
    new Assertion<>(
        "Must build disjoint sets correctly",
        new Disjoint(graph).value(),
        new AllOf<Iterable<Set<Node>>>(
            new ListOf<>(
                new HasValuesMatching<>(
                    set -> set.contains(one) && set.contains(two) && set.contains(three)
                        && set.size() == 3
                ),
                new HasValuesMatching<>(
                    set -> set.contains(five) && set.contains(four) && set.size() == 2
                ),
                new HasValuesMatching<>(
                    set -> set.contains(six) && set.size() == 1
                )
            )
        )
    ).affirm();
}