Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Hamcrest assertions instead of JUnit in common/reactive - backport 2.x (#1749) #4946

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates.
* Copyright (c) 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,7 +27,7 @@
import java.util.concurrent.atomic.AtomicInteger;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.hamcrest.CoreMatchers.is;

import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
Expand All @@ -40,7 +40,7 @@ void multiIgnoreTriggerSubscription() throws InterruptedException {
.peek(i -> latch.countDown())
.ignoreElements(); // should trigger subscription on its own

assertTrue(latch.await(200, TimeUnit.MILLISECONDS));
assertThat(latch.await(200, TimeUnit.MILLISECONDS), is(true));
}

@Test
Expand All @@ -50,7 +50,7 @@ void singleIgnoreTriggerSubscription() throws InterruptedException {
.peek(i -> latch.countDown())
.ignoreElement(); // should trigger subscription on its own

assertTrue(latch.await(200, TimeUnit.MILLISECONDS));
assertThat(latch.await(200, TimeUnit.MILLISECONDS), is(true));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2021 Oracle and/or its affiliates.
* Copyright (c) 2020, 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,11 +19,11 @@
import java.io.IOException;
import java.util.concurrent.SubmissionPublisher;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.is;

public class MultiDefaultIfEmptyTest {

@Test
Expand Down Expand Up @@ -129,13 +129,13 @@ public void cancel() {
.defaultIfEmpty(2)
.subscribe(ts);

assertTrue(sp.hasSubscribers());
assertThat(sp.hasSubscribers(), is(true));

ts.assertEmpty();

ts.cancel();

assertFalse(sp.hasSubscribers());
assertThat(sp.hasSubscribers(), is(false));
}

@Test
Expand All @@ -147,13 +147,13 @@ public void cancelWithSupplier() {
.defaultIfEmpty(() -> 2)
.subscribe(ts);

assertTrue(sp.hasSubscribers());
assertThat(sp.hasSubscribers(), is(true));

ts.assertEmpty();

ts.cancel();

assertFalse(sp.hasSubscribers());
assertThat(sp.hasSubscribers(), is(false));
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2021 Oracle and/or its affiliates.
* Copyright (c) 2020, 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,10 +30,11 @@
import java.util.stream.IntStream;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

import org.junit.jupiter.api.RepeatedTest;
Expand Down Expand Up @@ -154,7 +155,7 @@ public void delayError() {

ts.requestMax();

assertEquals(ts.getItems(), Arrays.asList(6));
assertThat(ts.getItems(), contains(6));
assertThat(ts.getLastError(), instanceOf(ArithmeticException.class));
assertThat(ts.isComplete(), is(false));
}
Expand All @@ -170,7 +171,7 @@ public void delayErrorInner() {

ts.requestMax();

assertEquals(ts.getItems(), Arrays.asList(6, -6));
assertThat(ts.getItems(), contains(6, -6));
assertThat(ts.getLastError(), instanceOf(ArithmeticException.class));
assertThat(ts.isComplete(), is(false));
}
Expand Down Expand Up @@ -257,7 +258,7 @@ public void justJust() {

ts.request1();

assertEquals(ts.getItems(), Collections.singletonList(1));
assertThat(ts.getItems(), contains(1));
assertThat(ts.getLastError(), is(nullValue()));
assertThat(ts.isComplete(), is(true));
}
Expand All @@ -275,7 +276,7 @@ public void onSubscribe(Flow.Subscription subscription) {
.flatMap(Single::just)
.subscribe(ts);

assertEquals(ts.getItems(), Collections.singletonList(1));
assertThat(ts.getItems(), contains(1));
assertThat(ts.getLastError(), is(nullValue()));
assertThat(ts.isComplete(), is(true));
}
Expand Down Expand Up @@ -307,12 +308,11 @@ public void flatMapCompletionStage() {

@RepeatedTest(500)
public void multi() throws ExecutionException, InterruptedException {
assertEquals(EXPECTED_EMISSION_COUNT, Multi.create(TEST_DATA)
assertThat(Multi.create(TEST_DATA)
.flatMap(MultiFlatMapPublisherTest::asyncFlowPublisher, MAX_CONCURRENCY, false, PREFETCH)
.distinct()
.collectList()
.await(800, TimeUnit.MILLISECONDS)
.size());
.await(800, TimeUnit.MILLISECONDS), hasSize(EXPECTED_EMISSION_COUNT));
}

private static Flow.Publisher<? extends String> asyncFlowPublisher(Integer i) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2021 Oracle and/or its affiliates.
* Copyright (c) 2020, 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,12 +22,14 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

import static org.hamcrest.Matchers.emptyArray;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.MatcherAssert.assertThat;

public class MultiRetryTest {

@Test
Expand All @@ -47,7 +49,7 @@ public void predicate() {
.subscribe(ts);

ts.assertFailure(IllegalArgumentException.class);
assertEquals(count.get(), 5);
assertThat(count.get(), is(5));
}

@Test
Expand All @@ -59,7 +61,7 @@ public void whenFunctionNull() {
.subscribe(ts);

ts.assertFailure(NullPointerException.class);
assertTrue(ts.getLastError().getSuppressed()[0] instanceof IOException, "" + ts.getLastError());
assertThat("" + ts.getLastError(), ts.getLastError().getSuppressed()[0], instanceOf(IOException.class));
}

@Test
Expand All @@ -73,7 +75,7 @@ public void whenFunctionCrash() {
.subscribe(ts);

ts.assertFailure(IllegalArgumentException.class);
assertTrue(ts.getLastError().getSuppressed()[0] instanceof IOException, "" + ts.getLastError());
assertThat("" + ts.getLastError(), ts.getLastError().getSuppressed()[0], instanceOf(IOException.class));
}

@Test
Expand All @@ -87,7 +89,7 @@ public void whenFunctionNoSelfSuppression() {
.subscribe(ts);

ts.assertFailure(IllegalArgumentException.class);
assertEquals(ts.getLastError().getSuppressed().length, 0, "" + ts.getLastError());
assertThat("" + ts.getLastError(), ts.getLastError().getSuppressed(), emptyArray());
}

@Test
Expand Down Expand Up @@ -121,7 +123,7 @@ public void whenFunctionError() {
.subscribe(ts);

ts.assertFailure(IllegalArgumentException.class);
assertEquals(ts.getLastError().getSuppressed().length, 0, "" + ts.getLastError());
assertThat("" + ts.getLastError(), ts.getLastError().getSuppressed(), emptyArray());
}

@Test
Expand All @@ -146,7 +148,7 @@ public void whenFunctionErrorHidden() {
.subscribe(ts);

ts.assertFailure(IllegalArgumentException.class);
assertEquals(ts.getLastError().getSuppressed().length, 0, "" + ts.getLastError());
assertThat("" + ts.getLastError(), ts.getLastError().getSuppressed(), emptyArray());
}

@Test
Expand All @@ -166,7 +168,7 @@ public void timer() {
ts.awaitDone(5, TimeUnit.SECONDS)
.assertResult(1);

assertEquals(count.get(), 5);
assertThat(count.get(), is(5));
} finally {
executor.shutdown();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2021 Oracle and/or its affiliates.
* Copyright (c) 2020, 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,14 +17,14 @@

package io.helidon.common.reactive;

import org.junit.jupiter.api.Test;
import java.util.Iterator;

import java.util.*;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class MultiSkipPublisherTest {

Expand Down Expand Up @@ -67,7 +67,7 @@ public Object next() {

ts.requestMax();

assertEquals(ts.getItems(), Collections.singletonList(3));
assertThat(ts.getItems(), contains(3));
assertThat(ts.getLastError(), instanceOf(IllegalArgumentException.class));
assertThat(ts.isComplete(), is(false));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2021 Oracle and/or its affiliates.
* Copyright (c) 2020, 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,11 +19,11 @@
import java.io.IOException;
import java.util.concurrent.SubmissionPublisher;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class MultiSwitchIfEmptyTest {

@Test
Expand Down Expand Up @@ -93,13 +93,13 @@ public void cancel() {
.switchIfEmpty(Multi.singleton(2))
.subscribe(ts);

assertTrue(sp.hasSubscribers());
assertThat(sp.hasSubscribers(), is(true));

ts.assertEmpty();

ts.cancel();

assertFalse(sp.hasSubscribers());
assertThat(sp.hasSubscribers(), is(false));
}

@Test
Expand All @@ -111,13 +111,13 @@ public void cancelFallback() {
.switchIfEmpty(Multi.create(sp))
.subscribe(ts);

assertTrue(sp.hasSubscribers());
assertThat(sp.hasSubscribers(), is(true));

ts.assertEmpty();

ts.cancel();

assertFalse(sp.hasSubscribers());
assertThat(sp.hasSubscribers(), is(false));
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2021 Oracle and/or its affiliates.
* Copyright (c) 2020, 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,13 +20,12 @@
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.Arrays;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.core.IsNull.nullValue;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class MultiTakeWhilePublisherTest {

Expand Down Expand Up @@ -54,7 +53,7 @@ public void limited() {

ts.requestMax();

assertEquals(ts.getItems(), Arrays.asList(1, 2, 3));
assertThat(ts.getItems(), contains(1, 2, 3));
assertThat(ts.getLastError(), is(nullValue()));
assertThat(ts.isComplete(), is(true));
}
Expand Down
Loading