Search in sources :

Example 6 with Assertion

use of org.llorllale.cactoos.matchers.Assertion in project cactoos by yegor256.

the class AsyncTest method runsInBackgroundWithThreadFactory.

@Test
void runsInBackgroundWithThreadFactory() {
    final String name = "secret name for thread factory";
    final ThreadFactory factory = r -> new Thread(r, name);
    final CountDownLatch latch = new CountDownLatch(1);
    new Assertion<>("Must run in the background with specific thread factory", new Async<>(new FuncOf<>(input -> {
        if (!input.equals(Thread.currentThread().getName())) {
            throw new IllegalStateException("Another thread factory was used");
        }
        latch.countDown();
    }, true), factory), new IsApplicable<>(name, new Satisfies<>(future -> {
        future.get();
        return latch.getCount() == 0;
    }))).affirm();
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) IsApplicable(org.llorllale.cactoos.matchers.IsApplicable) Assertion(org.llorllale.cactoos.matchers.Assertion) IsEqual(org.hamcrest.core.IsEqual) ThreadFactory(java.util.concurrent.ThreadFactory) Executors(java.util.concurrent.Executors) Satisfies(org.llorllale.cactoos.matchers.Satisfies) ThreadFactory(java.util.concurrent.ThreadFactory) IsApplicable(org.llorllale.cactoos.matchers.IsApplicable) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 7 with Assertion

use of org.llorllale.cactoos.matchers.Assertion in project cactoos by yegor256.

the class IoCheckedFuncTest method rethrowsIoException.

@Test
void rethrowsIoException() {
    final IOException exception = new IOException("intended");
    new Assertion<>("Must rethrow original IOException", () -> new IoCheckedFunc<>(i -> {
        throw exception;
    }).apply(1), new Throws<>(exception.getMessage(), exception.getClass())).affirm();
}
Also used : Test(org.junit.jupiter.api.Test) Assertion(org.llorllale.cactoos.matchers.Assertion) IOException(java.io.IOException) Throws(org.llorllale.cactoos.matchers.Throws) IOException(java.io.IOException) Throws(org.llorllale.cactoos.matchers.Throws) Test(org.junit.jupiter.api.Test)

Example 8 with Assertion

use of org.llorllale.cactoos.matchers.Assertion in project cactoos by yegor256.

the class TimedFuncTest method futureTaskIsCancelled.

@Test
@SuppressWarnings("PMD.AvoidCatchingGenericException")
public void futureTaskIsCancelled() {
    final long period = 50L;
    final long time = 2000L;
    final Future<Boolean> future = Executors.newSingleThreadExecutor().submit(() -> {
        Thread.sleep(time);
        return true;
    });
    try {
        new Timed<Boolean, Boolean>(period, input -> future).apply(true);
    // @checkstyle IllegalCatchCheck (1 line)
    } catch (final Exception exp) {
        new Assertion<>("Must be canceled after 1 sec", future.isCancelled(), new IsTrue()).affirm();
    }
}
Also used : IsTrue(org.llorllale.cactoos.matchers.IsTrue) Future(java.util.concurrent.Future) Endless(org.cactoos.iterable.Endless) And(org.cactoos.scalar.And) Assertion(org.llorllale.cactoos.matchers.Assertion) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test) Executors(java.util.concurrent.Executors) IsTrue(org.llorllale.cactoos.matchers.IsTrue) Assertion(org.llorllale.cactoos.matchers.Assertion) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 9 with Assertion

use of org.llorllale.cactoos.matchers.Assertion in project cactoos by yegor256.

the class IoCheckedBiFuncTest method rethrowsIoException.

@Test
public void rethrowsIoException() {
    final IOException exception = new IOException("intended");
    new Assertion<>("Must rethrow IOException", () -> new IoCheckedBiFunc<>((fst, scd) -> {
        throw exception;
    }).apply(1, 2), new Throws<>(exception.getMessage(), exception.getClass())).affirm();
}
Also used : Assertion(org.llorllale.cactoos.matchers.Assertion) IOException(java.io.IOException) Test(org.junit.Test) Throws(org.llorllale.cactoos.matchers.Throws) IOException(java.io.IOException) Throws(org.llorllale.cactoos.matchers.Throws) Test(org.junit.Test)

Example 10 with Assertion

use of org.llorllale.cactoos.matchers.Assertion in project cactoos by yegor256.

the class CheckedBiProcTest method noExceptionThrown.

@Test
public void noExceptionThrown() throws Exception {
    final AtomicInteger counter = new AtomicInteger();
    new CheckedBiProc<>((first, second) -> counter.incrementAndGet(), exp -> exp).exec(true, true);
    new Assertion<>("Must not throw an exception", counter.get(), new IsEqual<>(1)).affirm();
}
Also used : IsNull(org.hamcrest.core.IsNull) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assertion(org.llorllale.cactoos.matchers.Assertion) IsEqual(org.hamcrest.core.IsEqual) IOException(java.io.IOException) Test(org.junit.Test) EOFException(java.io.EOFException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IsEqual(org.hamcrest.core.IsEqual) Test(org.junit.Test)

Aggregations

Assertion (org.llorllale.cactoos.matchers.Assertion)12 IOException (java.io.IOException)7 IsEqual (org.hamcrest.core.IsEqual)7 Test (org.junit.jupiter.api.Test)7 Test (org.junit.Test)5 Throws (org.llorllale.cactoos.matchers.Throws)5 Executors (java.util.concurrent.Executors)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 ThreadFactory (java.util.concurrent.ThreadFactory)3 TimeUnit (java.util.concurrent.TimeUnit)3 IsApplicable (org.llorllale.cactoos.matchers.IsApplicable)3 Satisfies (org.llorllale.cactoos.matchers.Satisfies)3 EOFException (java.io.EOFException)2 IsNull (org.hamcrest.core.IsNull)2 FileNotFoundException (java.io.FileNotFoundException)1 AcceptPendingException (java.nio.channels.AcceptPendingException)1 Future (java.util.concurrent.Future)1 TimeoutException (java.util.concurrent.TimeoutException)1 Proc (org.cactoos.Proc)1