Skip to content

Commit

Permalink
test: use Junit5 in api-common (#2779)
Browse files Browse the repository at this point in the history
Fixes #2727  ☕️
  • Loading branch information
alicejli committed May 17, 2024
1 parent f7a5161 commit 1ade786
Show file tree
Hide file tree
Showing 8 changed files with 315 additions and 258 deletions.
18 changes: 15 additions & 3 deletions api-common-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,21 @@

<!-- Test Dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;
import org.junit.jupiter.api.Test;

public class ApiFuturesTest {
class ApiFuturesTest {

@Test
public void testAddCallback() throws Exception {
void testAddCallback() {
final AtomicInteger flag = new AtomicInteger();
SettableApiFuture<Integer> future = SettableApiFuture.<Integer>create();
ApiFutures.addCallback(
Expand All @@ -65,7 +65,7 @@ public void onFailure(Throwable t) {
}

@Test
public void testCatch() throws Exception {
void testCatch() throws Exception {
SettableApiFuture<Integer> future = SettableApiFuture.<Integer>create();
ApiFuture<Integer> fallback =
ApiFutures.catching(
Expand All @@ -83,7 +83,7 @@ public Integer apply(Exception ex) {
}

@Test
public void testCatchAsync() throws Exception {
void testCatchAsync() throws Exception {
SettableApiFuture<Integer> future = SettableApiFuture.<Integer>create();
ApiFuture<Integer> fallback =
ApiFutures.catchingAsync(
Expand All @@ -101,7 +101,7 @@ public ApiFuture<Integer> apply(Exception ex) {
}

@Test
public void testTransform() throws Exception {
void testTransform() throws Exception {
SettableApiFuture<Integer> inputFuture = SettableApiFuture.<Integer>create();
ApiFuture<String> transformedFuture =
ApiFutures.transform(
Expand All @@ -118,7 +118,7 @@ public String apply(Integer input) {
}

@Test
public void testTransformWithExecutor() throws Exception {
void testTransformWithExecutor() throws Exception {
SettableApiFuture<Integer> inputFuture = SettableApiFuture.<Integer>create();
final AtomicInteger counter = new AtomicInteger(0);
ApiFuture<String> transformedFuture =
Expand All @@ -143,7 +143,7 @@ public void execute(Runnable command) {
}

@Test
public void testAllAsList() throws Exception {
void testAllAsList() throws Exception {
SettableApiFuture<Integer> inputFuture1 = SettableApiFuture.<Integer>create();
SettableApiFuture<Integer> inputFuture2 = SettableApiFuture.<Integer>create();
ApiFuture<List<Integer>> listFuture =
Expand All @@ -154,7 +154,7 @@ public void testAllAsList() throws Exception {
}

@Test
public void successfulAllAsList() throws Exception {
void successfulAllAsList() throws Exception {
SettableApiFuture<Integer> inputFuture1 = SettableApiFuture.<Integer>create();
SettableApiFuture<Integer> inputFuture2 = SettableApiFuture.<Integer>create();
ApiFuture<List<Integer>> listFuture =
Expand All @@ -165,7 +165,7 @@ public void successfulAllAsList() throws Exception {
}

@Test
public void testTransformAsync() throws Exception {
void testTransformAsync() throws Exception {
ApiFuture<Integer> inputFuture = ApiFutures.immediateFuture(0);
ApiFuture<Integer> outputFuture =
ApiFutures.transformAsync(
Expand All @@ -181,31 +181,24 @@ public ApiFuture<Integer> apply(Integer input) {
}

@Test
public void testTransformAsyncWithExecutor() throws Exception {
void testTransformAsyncWithExecutor() throws Exception {
ApiFuture<Integer> inputFuture = ApiFutures.immediateFuture(0);
final AtomicInteger counter = new AtomicInteger(0);
ApiFuture<Integer> outputFuture =
ApiFutures.transformAsync(
inputFuture,
new ApiAsyncFunction<Integer, Integer>() {
@Override
public ApiFuture<Integer> apply(Integer input) {
return ApiFutures.immediateFuture(input + 1);
}
},
new Executor() {
@Override
public void execute(Runnable command) {
counter.incrementAndGet();
command.run();
}
});
(ApiAsyncFunction<Integer, Integer>) input -> ApiFutures.immediateFuture(input + 1),
(Executor)
command -> {
counter.incrementAndGet();
command.run();
});
assertThat(outputFuture.get()).isEqualTo(1);
assertThat(counter.get()).isEqualTo(1);
}

@Test
public void testImmediateFailedFuture() throws InterruptedException {
void testImmediateFailedFuture() throws InterruptedException {
ApiFuture<String> future =
ApiFutures.immediateFailedFuture(new IllegalArgumentException("The message"));
IllegalArgumentException exception = null;
Expand All @@ -219,7 +212,7 @@ public void testImmediateFailedFuture() throws InterruptedException {
}

@Test
public void testImmediateCancelledFuture() throws InterruptedException, ExecutionException {
void testImmediateCancelledFuture() throws InterruptedException, ExecutionException {
ApiFuture<String> future = ApiFutures.immediateCancelledFuture();
CancellationException exception = null;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@

import com.google.common.util.concurrent.MoreExecutors;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class ApiServiceTest {
class ApiServiceTest {
@Test
public void testNoopService() {
void testNoopService() {
ApiService service =
new AbstractApiService() {
@Override
Expand All @@ -50,12 +50,12 @@ protected void doStart() {
}
};
service.startAsync().awaitRunning();
Assert.assertTrue(service.isRunning());
Assertions.assertTrue(service.isRunning());
service.stopAsync().awaitTerminated();
}

@Test
public void testFailingService() {
void testFailingService() {
final AtomicReference<Throwable> savedFailure = new AtomicReference<>();
ApiService service =
new AbstractApiService() {
Expand Down Expand Up @@ -86,8 +86,8 @@ public void failed(ApiService.State from, Throwable failure) {
// Expected
}

Assert.assertEquals(service.state(), ApiService.State.FAILED);
Assert.assertEquals(savedFailure.get().getMessage(), "this service always fails");
Assert.assertEquals(service.failureCause().getMessage(), "this service always fails");
Assertions.assertEquals(service.state(), ApiService.State.FAILED);
Assertions.assertEquals(savedFailure.get().getMessage(), "this service always fails");
Assertions.assertEquals(service.failureCause().getMessage(), "this service always fails");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@

import com.google.common.truth.Truth;
import com.google.common.util.concurrent.SettableFuture;
import org.junit.Test;
import org.junit.jupiter.api.Test;

public class ListenableFutureToApiFutureTest {
class ListenableFutureToApiFutureTest {

@Test
public void testGet() throws Exception {
void testGet() throws Exception {
SettableFuture<Integer> future = SettableFuture.create();
ListenableFutureToApiFuture<Integer> apiFuture = new ListenableFutureToApiFuture<>(future);
future.set(3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class SettableApiFutureTest {
class SettableApiFutureTest {
@Test
public void testSet() throws Exception {
void testSet() throws Exception {
SettableApiFuture<Integer> future = SettableApiFuture.<Integer>create();
Truth.assertThat(future.isDone()).isFalse();
future.set(42);
Expand All @@ -48,7 +49,7 @@ public void testSet() throws Exception {
}

@Test
public void testCancel() throws Exception {
void testCancel() {
SettableApiFuture<Integer> future = SettableApiFuture.<Integer>create();
Truth.assertThat(future.isDone()).isFalse();
Truth.assertThat(future.isCancelled()).isFalse();
Expand All @@ -57,15 +58,19 @@ public void testCancel() throws Exception {
Truth.assertThat(future.isCancelled()).isTrue();
}

@Test(expected = ExecutionException.class)
public void testException() throws Exception {
SettableApiFuture<Integer> future = SettableApiFuture.<Integer>create();
future.setException(new Exception());
future.get();
@Test
void testException() {
Assertions.assertThrows(
ExecutionException.class,
() -> {
SettableApiFuture<Integer> future = SettableApiFuture.<Integer>create();
future.setException(new Exception());
future.get();
});
}

@Test
public void testListener() throws Exception {
void testListener() {
final AtomicInteger flag = new AtomicInteger();
SettableApiFuture<Integer> future = SettableApiFuture.<Integer>create();
future.addListener(
Expand Down

0 comments on commit 1ade786

Please sign in to comment.