org.llorllale.cactoos.matchers.Throws Java Examples

The following examples show how to use org.llorllale.cactoos.matchers.Throws. 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: NoNullsTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void nthThrowsErrorIfNull() {
    new Assertion<>(
        "Must throw exception",
        () -> new TailOf<>(
            1,
            new NoNulls<>(
                new IteratorOf<>("a", "b", null, "c")
            )
        ).next(),
        new Throws<>(
            new MatcherOf<>(
                (String msg) -> msg.matches("^Item #2 of .*? is NULL")
            ),
            IllegalStateException.class
        )
    ).affirm();
}
 
Example #2
Source File: TimedTest.java    From cactoos with MIT License 6 votes vote down vote up
/**
 * Execute 1 task within executor service and ensure that we'll get the
 *  expected exception type.
 */
@Test
public void failsDueToException() {
    new Assertion<>(
        "Wraps error into CompletionException",
        () -> new Timed<String>(
            Executors.newSingleThreadExecutor(),
            1L,
            TimeUnit.SECONDS,
            new ScalarOf<String>(
                () -> {
                    // @checkstyle LineLengthCheck (1 line)
                    throw new IllegalStateException("Something went wrong");
                }
            )
        ).iterator().next(),
        new Throws<>(
            // @checkstyle LineLengthCheck (1 line)
            "java.io.IOException: java.util.concurrent.ExecutionException: java.lang.IllegalStateException: Something went wrong",
            UncheckedIOException.class
        )
    ).affirm();
}
 
Example #3
Source File: ThreadsTest.java    From cactoos with MIT License 6 votes vote down vote up
/**
 * Execute 1 task within executor service and ensure that we'll get the
 *  expected exception type.
 */
@Test
public void failsDueToException() {
    new Assertion<>(
        "wraps error into CompletionException",
        () -> new Threads<String>(
            Executors.newSingleThreadExecutor(),
            new ScalarOf<String>(
                () -> {
                    // @checkstyle LineLengthCheck (1 line)
                    throw new IllegalStateException("Something went wrong");
                }
            )
        ).iterator().next(),
        new Throws<>(
            // @checkstyle LineLengthCheck (1 line)
            "java.io.IOException: java.util.concurrent.ExecutionException: java.lang.IllegalStateException: Something went wrong",
            UncheckedIOException.class
        )
    ).affirm();
}
 
Example #4
Source File: ImmutableTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test()
public void subListReturnsListIteratorWithUnsupportedSet() {
    new Assertion<>(
        "subList.listIterator().set() must throw exception",
        () -> {
            final ListIterator<String> iterator = new Immutable<>(
                new ListOf<>("one", "two", "three")
            ).subList(0, 2).listIterator(0);
            iterator.next();
            iterator.set("zero");
            return new Object();
        },
        new Throws<>(
            "List Iterator is read-only and doesn't allow rewriting items",
            UnsupportedOperationException.class
        )
    ).affirm();
}
 
Example #5
Source File: MapEnvelopeTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void clearThrowsException() {
    new Assertion<>(
        "clear method must throw exception",
        () -> {
            new NoNulls<>(
                new MapOf<Integer, Integer>(
                    new MapEntry<>(0, -1)
                )
            ).clear();
            return 0;
        },
        new Throws<>(
            "#clear() is not supported, it's a read-only map",
            UnsupportedOperationException.class
        )
    ).affirm();
}
 
Example #6
Source File: MainTest.java    From jpeek with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings({"unchecked", "rawtypes"})
public void crashesIfOverwriteAndSourceEqualsToTarget(@TempDir final Path source) {
    new Assertion(
        "Must throw an exception",
        (Scalar<Boolean>) () -> {
            Main.main(
                "--sources", source.toString(),
                "--target", source.toString(),
                "--overwrite"
            );
            return true;
        },
        new Throws(
            "Invalid paths - can't be equal if overwrite option is set.",
            IllegalArgumentException.class
        )
    ).affirm();
}
 
Example #7
Source File: MainTest.java    From jpeek with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings({"unchecked", "rawtypes"})
public void crashesIfMetricsHaveInvalidNames(@TempDir final Path target) {
    new Assertion(
        "Must throw an exception",
        (Scalar<Boolean>) () -> {
            Main.main(
                "--sources", Paths.get(".").toString(),
                "--target", target.toString(),
                "--metrics", "#%$!"
            );
            return true;
        },
        new Throws(
            "Invalid metric name: '#%$!'",
            IllegalArgumentException.class
        )
    ).affirm();
}
 
Example #8
Source File: ImmutableTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void clear() {
    new Assertion<>(
        "clear() must throw exception",
        () -> {
            new Immutable<>(
                new ListOf<>(1, 2, 3)
            ).clear();
            return new Object();
        },
        new Throws<>(
            new MatcherOf<>(
                (String msg) -> msg.equals("#clear(): the list is read-only")
            ),
            UnsupportedOperationException.class
        )
    ).affirm();
}
 
Example #9
Source File: ImmutableTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void testAdd() {
    new Assertion<>(
        "add(int, T) must throw exception",
        () -> {
            new Immutable<>(
                new ListOf<>("a", "b")
            ).add(3, "c");
            return new Object();
        },
        new Throws<>(
            new MatcherOf<>(
                (String msg) -> msg.equals("#add(int, T): the list is read-only")
            ),
            UnsupportedOperationException.class
        )
    ).affirm();
}
 
Example #10
Source File: ListEnvelopeTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test()
public void subListReturnsListIteratorWithSupportedSet() {
    new Assertion<>(
        "subList.listIterator().set() must throw exception",
        () -> {
            final ListIterator<String> iterator = new StringList("one", "two", "three")
                .subList(0, 2)
                .listIterator(0);
            iterator.next();
            iterator.set("zero");
            return new Object();
        },
        new Throws<>(
            new MatcherOf<>(
                (String msg) -> msg.equals(
                    "List Iterator is read-only and doesn't allow rewriting items"
                )
            ),
            UnsupportedOperationException.class
        )
    ).affirm();
}
 
Example #11
Source File: ListIteratorNoNullsTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void mustRemoveFromListIterator() {
    new Assertion<>(
        "must remove element from list iterator",
        () -> {
            final List<Integer> list = new ArrayList<>(2);
            list.add(1);
            list.add(2);
            final ListIterator<Integer> iterator = new ListIteratorNoNulls<>(
                list.listIterator()
            );
            iterator.next();
            iterator.remove();
            return iterator.previous();
        },
        new Throws<>(
            NoSuchElementException.class
        )
    ).affirm();
}
 
Example #12
Source File: NoNullsTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void getThrowsErrorIfListIteratorPreviousValueIsNullValue() {
    final List<Integer> list = new ArrayList<>(2);
    list.add(1);
    list.add(2);
    final ListIterator<Integer> listiterator = new NoNulls<>(
        list
    ).listIterator();
    listiterator.next();
    list.set(0, null);
    new Assertion<>(
        "must throw error if previous value in iterator is null",
        () -> listiterator.previous(),
        new Throws<>(
            "Previous item is NULL",
            IllegalStateException.class
        )
    ).affirm();
}
 
Example #13
Source File: ImmutableListIteratorTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void mustRaiseErrorOnListIteratorRemove() {
    new Assertion<>(
        "Must throw error if modified with remove operation",
        () -> {
            new ImmutableListIterator<>(
                new ListOf<>(1, 2).listIterator()
            ).remove();
            return 0;
        },
        new Throws<>(
            "List Iterator is read-only and doesn't allow removing items",
            UnsupportedOperationException.class
        )
    ).affirm();
}
 
Example #14
Source File: FirstOfTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void throwsFallbackIfNothingMatches() {
    new Assertion<>(
        "Fallback was not thrown",
        new FirstOf<>(
            num -> num.equals(0),
            // @checkstyle MagicNumber (10 lines)
            new IterableOf<>(
                1, 2, 3, 4, 5
            ),
            () -> {
                throw new IllegalArgumentException(
                    String.format("Unable to found element with id %d", 0)
                );
            }
        ),
        new Throws<>(IllegalArgumentException.class)
    ).affirm();
}
 
Example #15
Source File: HeadOfTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void removeNotSupported() throws Exception {
    new Assertion<>(
        "Remove should not be supported",
        () -> {
            new HeadOf<>(
                1,
                new IteratorOf<>(
                    "one", "two", "three", "four"
                )
            ).remove();
            return "Should have thrown exception";
        },
        new Throws<>(UnsupportedOperationException.class)
    ).affirm();
}
 
Example #16
Source File: ImmutableTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void clear() {
    new Assertion<>(
        "clear() must throw exception",
        () -> {
            new Immutable<>(
                new ListOf<>(1, 2, 3)
            ).clear();
            return new Object();
        },
        new Throws<>(
            new MatcherOf<>(
                (String msg) -> msg.equals("#clear(): the collection is read-only")
            ),
            UnsupportedOperationException.class
        )
    ).affirm();
}
 
Example #17
Source File: AppendToTest.java    From cactoos with MIT License 6 votes vote down vote up
/**
 * Ensures that AppendTo is failing on a negative predicate result.
 */
@Test
public void failsIfFileDoesNotExist() {
    final File source = new File(
        new Randomized(
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'
        ).asString()
    );
    new Assertion<>(
        "Can't throw exception with proper message",
        () -> new AppendTo(source).stream(),
        new Throws<>(
            source.getPath(),
            NoSuchFileException.class
        )
    ).affirm();
}
 
Example #18
Source File: IoCheckedBiProcTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void wrapsExceptions() {
    final IoCheckedBiProc<Object, Object> proc = new IoCheckedBiProc<>(
        (first, second) -> {
            throw new IOException();
        }
    );
    new Assertion<>(
        "Must wrap with IOException",
        () -> {
            proc.exec(true, true);
            return true;
        },
        new Throws<>(
            "java.io.IOException",
            IOException.class
        )
    ).affirm();
}
 
Example #19
Source File: RetryTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void processInterruptExceptionOnWait() {
    final Thread main = Thread.currentThread();
    new Thread(
        () -> {
            while (true) {
                if (Thread.State.TIMED_WAITING == main.getState()) {
                    main.interrupt();
                    return;
                }
            }
        }).start();
    new Assertion<>(
        "Must be interrupted on wait",
        () -> new Retry<>(
            input -> {
                throw new IllegalArgumentException("Happens");
            },
            attempts -> false,
            Duration.ofMillis(Long.MAX_VALUE)
        ).apply(true),
        new Throws<>("sleep interrupted", InterruptedException.class)
    ).affirm();
}
 
Example #20
Source File: ReplacedTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void invalidRegex() {
    final String regex = "invalid_regex{0,";
    new Assertion<>(
        "Doesn't throw proper exception",
        new Replaced(
            new TextOf("text"),
            regex,
            "error"
        )::asString,
        new Throws<>(
            new PatternSyntaxException(
                "Unclosed counted closure",
                regex,
                // @checkstyle MagicNumberCheck (1 line)
                16
            ).getMessage(),
            PatternSyntaxException.class
        )
    ).affirm();
}
 
Example #21
Source File: LoggingInputStreamTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void reThrowsException() {
    final String message = "Some read exception.";
    final LoggingInputStream stream = new LoggingInputStream(
        new InputStream() {
            @Override
            public int read() throws IOException {
                throw new IOException(message);
            }
        },
        this.getClass().getSimpleName()
    );
    new Assertion<>(
        "Read doesn't throw an the exception.",
        () -> stream.read(),
        new Throws<>(message, IOException.class)
    ).affirm();
}
 
Example #22
Source File: ScalarTest.java    From cactoos with MIT License 5 votes vote down vote up
@Test
public void failForNullArgument() {
    new Assertion<>(
        "Must fail for null argument",
        () -> new NoNulls<>(null).value(),
        new Throws<>(
            "NULL instead of a valid scalar",
            IllegalArgumentException.class
        )
    ).affirm();
}
 
Example #23
Source File: MatchedTest.java    From cactoos with MIT License 5 votes vote down vote up
@Test
public void noCorrelationWithSmallerSecondIterable() throws IllegalStateException {
    new Assertion<>(
        "All elements have correlation function as `endsWith`",
        () -> new Matched<>(
            (fst, snd) -> fst.endsWith("elem") && snd.endsWith("elem"),
            new IterableOf<>("1st elem", "2nd elem", "3rd elem"),
            new IterableOf<>("`A` elem", "`B` elem")
        ).iterator(),
        new Throws<>(IllegalStateException.class)
    ).affirm();
}
 
Example #24
Source File: NoNullsTest.java    From cactoos with MIT License 5 votes vote down vote up
@Test
public void throwsErrorIfNullInToArray() {
    new Assertion<>(
        "Must throw exception",
        () -> new NoNulls<>(
            new ListOf<>(1, null, 3)
        ).toArray(),
        new Throws<>(
            "Item #1 of #toArray() is NULL",
            IllegalStateException.class
        )
    ).affirm();
}
 
Example #25
Source File: MainTest.java    From jpeek with MIT License 5 votes vote down vote up
@Test
public void crashesIfInvalidInput() {
    new Assertion<>(
        "Must throw an exception if parameter is invalid",
        () -> {
            Main.main("hello");
            return "";
        }, new Throws<>(ParameterException.class)
    ).affirm();
}
 
Example #26
Source File: MapEnvelopeTest.java    From cactoos with MIT License 5 votes vote down vote up
@Test
public void putThrowsException() {
    new Assertion<>(
        "put method must throw exception",
        () -> new NoNulls<>(
            new MapOf<Integer, Integer>(
                new MapEntry<>(0, -1)
            )
        ).put(2, 2),
        new Throws<>(
            "#put() is not supported, it's a read-only map",
            UnsupportedOperationException.class
        )
    ).affirm();
}
 
Example #27
Source File: NoNullsTest.java    From cactoos with MIT License 5 votes vote down vote up
@Test
public void throwsErrorIfNullInAddAll() {
    final NoNulls<Integer> nonulls = new NoNulls<>(new ArrayList<>(0));
    new Assertion<>(
        "Must throw exception for nullable #addAll() parameter collection",
        () -> nonulls.addAll(new ListOf<>(1, 2, null)),
        new Throws<>(
            "Item #2 of #toArray() is NULL",
            IllegalStateException.class
        )
    ).affirm();
}
 
Example #28
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 #29
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 #30
Source File: MapEnvelopeTest.java    From cactoos with MIT License 5 votes vote down vote up
@Test
public void removeThrowsException() {
    new Assertion<>(
        "remove method did not throw exception",
        () -> new NoNulls<>(
            new MapOf<Integer, Integer>(
                new MapEntry<>(0, -1)
            )
        ).remove(0),
        new Throws<>(
            "#remove() is not supported, it's a read-only map",
            UnsupportedOperationException.class
        )
    ).affirm();
}