Search in sources :

Example 16 with Condition

use of org.assertj.core.api.Condition in project robozonky by RoboZonky.

the class ListenerServiceLoaderTest method correctLoading.

@Test
void correctLoading() {
    final RoboZonkyStartingEvent e = new RoboZonkyStartingEvent();
    final EventListener<RoboZonkyStartingEvent> l = mock(EventListener.class);
    final ListenerService s1 = mock(ListenerService.class);
    final EventListenerSupplier<RoboZonkyStartingEvent> returned = () -> Optional.of(l);
    doReturn(returned).when(s1).findListener(eq(e.getClass()));
    final ListenerService s2 = mock(ListenerService.class);
    doReturn((EventListenerSupplier<RoboZonkyStartingEvent>) Optional::empty).when(s2).findListener(eq(e.getClass()));
    final Iterable<ListenerService> s = () -> Arrays.asList(s1, s2).iterator();
    final List<EventListenerSupplier<RoboZonkyStartingEvent>> r = ListenerServiceLoader.load(RoboZonkyStartingEvent.class, s);
    assertThat(r).hasSize(2);
    assertThat(r).first().has(new Condition<>(result -> result.get().isPresent() && result.get().get() == l, "Exists"));
    assertThat(r).last().has(new Condition<>(result -> !result.get().isPresent(), "Does not exist"));
}
Also used : ListenerService(com.github.robozonky.api.notifications.ListenerService) Test(org.junit.jupiter.api.Test) EventListenerSupplier(com.github.robozonky.api.notifications.EventListenerSupplier) Mockito(org.mockito.Mockito) Arrays(java.util.Arrays) List(java.util.List) RoboZonkyStartingEvent(com.github.robozonky.api.notifications.RoboZonkyStartingEvent) EventListener(com.github.robozonky.api.notifications.EventListener) Condition(org.assertj.core.api.Condition) Optional(java.util.Optional) Assertions(org.assertj.core.api.Assertions) RoboZonkyTestingEvent(com.github.robozonky.api.notifications.RoboZonkyTestingEvent) ListenerService(com.github.robozonky.api.notifications.ListenerService) RoboZonkyStartingEvent(com.github.robozonky.api.notifications.RoboZonkyStartingEvent) EventListenerSupplier(com.github.robozonky.api.notifications.EventListenerSupplier) Test(org.junit.jupiter.api.Test)

Example 17 with Condition

use of org.assertj.core.api.Condition in project sandbox by irof.

the class ParallelStreamSample method sampleInForkJoinWorkerThread.

@Test
public void sampleInForkJoinWorkerThread() throws Exception {
    RecursiveTask<List<String>> task = new RecursiveTask<List<String>>() {

        @Override
        protected List<String> compute() {
            // ForkJoinPoolのThreadの中でparallelに実行します。
            return IntStream.rangeClosed(1, 5).parallel().mapToObj(i -> Thread.currentThread().getName()).collect(toList());
        }
    };
    ForkJoinPool forkJoinPool = new ForkJoinPool();
    List<String> collected = forkJoinPool.invoke(task);
    // ForkJoinPoolの中で実行されているので、すべてForkJoinPoolのスレッド名になっているはずです。
    assertThat(collected).are(new Condition<>(str -> str.startsWith("ForkJoinPool-"), "start with 'ForkJoinPool-'"));
    forkJoinPool.shutdown();
}
Also used : IntStream(java.util.stream.IntStream) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) RecursiveTask(java.util.concurrent.RecursiveTask) ForkJoinPool(java.util.concurrent.ForkJoinPool) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Condition(org.assertj.core.api.Condition) Test(org.junit.Test) RecursiveTask(java.util.concurrent.RecursiveTask) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ForkJoinPool(java.util.concurrent.ForkJoinPool) Test(org.junit.Test)

Example 18 with Condition

use of org.assertj.core.api.Condition in project TestFX by TestFX.

the class Adapter method adaptMatcher.

private static <T> Condition<? super T> adaptMatcher(Matcher<? extends T> matcher, boolean invert) {
    return new Condition<T>() {

        @Override
        public boolean matches(T t) {
            boolean matches = matcher.matches(t);
            if (invert) {
                matches = !matches;
            }
            if (!matches) {
                StringDescription reasonDescription = new StringDescription();
                matcher.describeTo(reasonDescription);
                // at each "assertThat" call site.
                throw new AssertionError(getErrorMessage(matcher, t, invert));
            }
            return true;
        }
    };
}
Also used : Condition(org.assertj.core.api.Condition) StringDescription(org.hamcrest.StringDescription)

Example 19 with Condition

use of org.assertj.core.api.Condition in project reactor-core by reactor.

the class WorkQueueProcessorTest method testCustomRequestTaskThreadNameShare.

@Test
public void testCustomRequestTaskThreadNameShare() {
    String expectedName = "workQueueProcessorRequestTaskShare";
    // NOTE: the below single executor should not be used usually as requestTask assumes it immediately gets executed
    ExecutorService customTaskExecutor = Executors.newSingleThreadExecutor(r -> new Thread(r, expectedName));
    WorkQueueProcessor<Object> processor = WorkQueueProcessor.builder().executor(Executors.newCachedThreadPool()).requestTaskExecutor(customTaskExecutor).bufferSize(8).waitStrategy(WaitStrategy.liteBlocking()).autoCancel(true).build();
    processor.requestTask(Operators.cancelledSubscription());
    processor.subscribe();
    Thread[] threads = new Thread[Thread.activeCount()];
    Thread.enumerate(threads);
    // cleanup to avoid visibility in other tests
    customTaskExecutor.shutdownNow();
    processor.forceShutdown();
    Condition<Thread> customRequestTaskThread = new Condition<>(thread -> expectedName.equals(thread.getName()), "a thread named \"%s\"", expectedName);
    Assertions.assertThat(threads).haveExactly(1, customRequestTaskThread);
}
Also used : Condition(org.assertj.core.api.Condition) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Example 20 with Condition

use of org.assertj.core.api.Condition in project reactor-core by reactor.

the class WorkQueueProcessorTest method testCustomRequestTaskThreadNameCreate.

@Test
public void testCustomRequestTaskThreadNameCreate() {
    String expectedName = "workQueueProcessorRequestTaskCreate";
    // NOTE: the below single executor should not be used usually as requestTask assumes it immediately gets executed
    ExecutorService customTaskExecutor = Executors.newSingleThreadExecutor(r -> new Thread(r, expectedName));
    WorkQueueProcessor<Object> processor = WorkQueueProcessor.builder().executor(Executors.newCachedThreadPool()).requestTaskExecutor(customTaskExecutor).bufferSize(8).waitStrategy(WaitStrategy.liteBlocking()).autoCancel(true).build();
    processor.requestTask(Operators.cancelledSubscription());
    processor.subscribe();
    Thread[] threads = new Thread[Thread.activeCount()];
    Thread.enumerate(threads);
    // cleanup to avoid visibility in other tests
    customTaskExecutor.shutdownNow();
    processor.forceShutdown();
    Condition<Thread> customRequestTaskThread = new Condition<>(thread -> expectedName.equals(thread.getName()), "a thread named \"%s\"", expectedName);
    Assertions.assertThat(threads).haveExactly(1, customRequestTaskThread);
}
Also used : Condition(org.assertj.core.api.Condition) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Aggregations

Condition (org.assertj.core.api.Condition)33 Test (org.junit.Test)19 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)16 List (java.util.List)9 Before (org.junit.Before)7 File (java.io.File)5 IOException (java.io.IOException)5 Arrays (java.util.Arrays)5 ExecutorService (java.util.concurrent.ExecutorService)4 Assertions (org.assertj.core.api.Assertions)4 Rule (org.junit.Rule)4 Mock (org.mockito.Mock)4 When (io.cucumber.java.en.When)3 SupportPage (io.syndesis.qe.pages.SupportPage)3 InetAddress (java.net.InetAddress)3 ArrayList (java.util.ArrayList)3 Collections (java.util.Collections)3 Enumeration (java.util.Enumeration)3 TimeUnit (java.util.concurrent.TimeUnit)3 ZipEntry (java.util.zip.ZipEntry)3