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 (#1749) #4908

Merged
merged 1 commit into from
Sep 23, 2022
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 @@ -29,7 +29,7 @@
import org.junit.jupiter.api.Test;

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

public class IgnoreElementsTest {
@Test
Expand All @@ -39,7 +39,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 @@ -49,7 +49,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 @@ -20,8 +20,8 @@

import org.junit.jupiter.api.Test;

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

public class MultiDefaultIfEmptyTest {

Expand Down Expand Up @@ -128,13 +128,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 @@ -146,13 +146,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 @@ -32,10 +32,11 @@
import org.junit.jupiter.api.Test;

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;

public class MultiFlatMapPublisherTest {
Expand Down Expand Up @@ -153,7 +154,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 @@ -169,7 +170,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 @@ -256,7 +257,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 @@ -274,7 +275,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 @@ -306,12 +307,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 @@ -23,9 +23,11 @@

import org.junit.jupiter.api.Test;

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

public class MultiRetryTest {

Expand All @@ -46,7 +48,7 @@ public void predicate() {
.subscribe(ts);

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

@Test
Expand All @@ -58,7 +60,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 @@ -72,7 +74,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 @@ -86,7 +88,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 @@ -120,7 +122,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 @@ -145,7 +147,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 @@ -165,7 +167,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 @@ -16,15 +16,14 @@

package io.helidon.common.reactive;

import java.util.Collections;
import java.util.Iterator;

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 +66,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 @@ -20,8 +20,8 @@

import org.junit.jupiter.api.Test;

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

public class MultiSwitchIfEmptyTest {

Expand Down Expand Up @@ -92,13 +92,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 @@ -110,13 +110,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 @@ -17,15 +17,14 @@
package io.helidon.common.reactive;

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

import org.junit.jupiter.api.Test;

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 @@ -53,7 +52,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