Migrate common/util to JUnit5
[yangtools.git] / common / util / src / test / java / org / opendaylight / yangtools / util / concurrent / AsyncNotifyingListeningExecutorServiceTest.java
index d973cc972b2d6d6a84be40fb309d54c5bacaa3fe..6e91fc8532bc47f1f111816ad171aaae22a9a587 100644 (file)
@@ -7,9 +7,9 @@
  */
 package org.opendaylight.yangtools.util.concurrent;
 
-import static org.junit.Assert.assertSame;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.fail;
 import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
@@ -24,14 +24,13 @@ import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.MoreExecutors;
 import com.google.common.util.concurrent.ThreadFactoryBuilder;
 import java.util.ArrayList;
-import java.util.List;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicReference;
-import org.junit.After;
-import org.junit.Test;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
 import org.opendaylight.yangtools.util.concurrent.CommonTestUtils.Invoker;
 
 /**
@@ -39,13 +38,13 @@ import org.opendaylight.yangtools.util.concurrent.CommonTestUtils.Invoker;
  *
  * @author Thomas Pantelis
  */
-public class AsyncNotifyingListeningExecutorServiceTest {
+class AsyncNotifyingListeningExecutorServiceTest {
 
     private ExecutorService listenerExecutor;
     private AsyncNotifyingListeningExecutorService testExecutor;
 
-    @After
-    public void tearDown() {
+    @AfterEach
+    void tearDown() {
         if (listenerExecutor != null) {
             listenerExecutor.shutdownNow();
         }
@@ -56,9 +55,9 @@ public class AsyncNotifyingListeningExecutorServiceTest {
     }
 
     @Test
-    public void testListenerCallbackWithExecutor() throws InterruptedException {
+    void testListenerCallbackWithExecutor() throws InterruptedException {
 
-        String listenerThreadPrefix = "ListenerThread";
+        final var listenerThreadPrefix = "ListenerThread";
         listenerExecutor = Executors.newFixedThreadPool(3,
                 new ThreadFactoryBuilder().setNameFormat(listenerThreadPrefix + "-%d").build());
 
@@ -73,9 +72,9 @@ public class AsyncNotifyingListeningExecutorServiceTest {
     }
 
     @Test
-    public void testListenerCallbackWithNoExecutor() throws InterruptedException {
+    void testListenerCallbackWithNoExecutor() throws InterruptedException {
 
-        String listenerThreadPrefix = "SingleThread";
+        final var listenerThreadPrefix = "SingleThread";
         testExecutor = new AsyncNotifyingListeningExecutorService(
                 Executors.newSingleThreadExecutor(
                         new ThreadFactoryBuilder().setNameFormat(listenerThreadPrefix).build()),
@@ -89,9 +88,9 @@ public class AsyncNotifyingListeningExecutorServiceTest {
     static void testListenerCallback(final AsyncNotifyingListeningExecutorService executor,
             final Invoker invoker, final String expListenerThreadPrefix) throws InterruptedException {
 
-        AtomicReference<AssertionError> assertError = new AtomicReference<>();
-        CountDownLatch futureNotifiedLatch = new CountDownLatch(1);
-        CountDownLatch blockTaskLatch = new CountDownLatch(1);
+        final var assertError = new AtomicReference<AssertionError>();
+        var futureNotifiedLatch = new CountDownLatch(1);
+        final var blockTaskLatch = new CountDownLatch(1);
 
         // The blockTaskLatch is used to block the task from completing until we've added
         // our listener to the Future. Otherwise, if the task completes quickly and the Future is
@@ -99,15 +98,15 @@ public class AsyncNotifyingListeningExecutorServiceTest {
         // will immediately notify synchronously on this thread as Futures#addCallback defaults to
         // a same thread executor. This would erroneously fail the test.
 
-        ListenableFuture<?> future = invoker.invokeExecutor(executor, blockTaskLatch);
+        final var future = invoker.invokeExecutor(executor, blockTaskLatch);
         addCallback(future, futureNotifiedLatch, expListenerThreadPrefix, assertError);
 
         // Now that we've added our listener, signal the latch to let the task complete.
 
         blockTaskLatch.countDown();
 
-        assertTrue("ListenableFuture callback was not notified of onSuccess",
-                    futureNotifiedLatch.await(5, TimeUnit.SECONDS));
+        assertTrue(futureNotifiedLatch.await(5, TimeUnit.SECONDS),
+                    "ListenableFuture callback was not notified of onSuccess");
 
         if (assertError.get() != null) {
             throw assertError.get();
@@ -119,8 +118,8 @@ public class AsyncNotifyingListeningExecutorServiceTest {
         futureNotifiedLatch = new CountDownLatch(1);
         addCallback(future, futureNotifiedLatch, Thread.currentThread().getName(), assertError);
 
-        assertTrue("ListenableFuture callback was not notified of onSuccess",
-                    futureNotifiedLatch.await(5, TimeUnit.SECONDS));
+        assertTrue(futureNotifiedLatch.await(5, TimeUnit.SECONDS),
+                    "ListenableFuture callback was not notified of onSuccess");
 
         if (assertError.get() != null) {
             throw assertError.get();
@@ -134,11 +133,11 @@ public class AsyncNotifyingListeningExecutorServiceTest {
             @Override
             public void onSuccess(final Object result) {
                 try {
-                    String theadName = Thread.currentThread().getName();
-                    assertTrue("ListenableFuture callback was not notified on the listener executor."
+                    final var theadName = Thread.currentThread().getName();
+                    assertTrue(theadName.startsWith(expListenerThreadPrefix),
+                            "ListenableFuture callback was not notified on the listener executor."
                         + " Expected thread name prefix \"" + expListenerThreadPrefix
-                        + "\". Actual thread name \"" + theadName + "\"",
-                            theadName.startsWith(expListenerThreadPrefix));
+                        + "\". Actual thread name \"" + theadName + "\"");
                 } catch (AssertionError e) {
                     assertError.set(e);
                 } finally {
@@ -148,21 +147,21 @@ public class AsyncNotifyingListeningExecutorServiceTest {
 
             @Override
             @SuppressWarnings("checkstyle:parameterName")
-            public void onFailure(final Throwable t) {
+            public void onFailure(final Throwable cause) {
                 // Shouldn't happen
-                fail("Unexpected failure " + t);
+                fail("Unexpected failure " + cause);
             }
         }, MoreExecutors.directExecutor());
     }
 
     @Test
-    public void testDelegatedMethods() throws InterruptedException {
+    void testDelegatedMethods() throws InterruptedException {
 
-        Runnable task = () -> { };
+        final var task = (Runnable) () -> { };
 
-        List<Runnable> taskList = new ArrayList<>();
+        final var taskList = new ArrayList<Runnable>();
 
-        ExecutorService mockDelegate = mock(ExecutorService.class);
+        final var mockDelegate = mock(ExecutorService.class);
         doNothing().when(mockDelegate).execute(task);
         doNothing().when(mockDelegate).shutdown();
         doReturn(taskList).when(mockDelegate).shutdownNow();
@@ -170,15 +169,15 @@ public class AsyncNotifyingListeningExecutorServiceTest {
         doReturn(Boolean.TRUE).when(mockDelegate).isShutdown();
         doReturn(Boolean.TRUE).when(mockDelegate).isTerminated();
 
-        AsyncNotifyingListeningExecutorService executor = new AsyncNotifyingListeningExecutorService(
+        final var executor = new AsyncNotifyingListeningExecutorService(
                                                                    mockDelegate, null);
 
         executor.execute(task);
         executor.shutdown();
-        assertTrue("awaitTermination", executor.awaitTermination(3, TimeUnit.SECONDS));
-        assertSame("shutdownNow", taskList, executor.shutdownNow());
-        assertTrue("isShutdown", executor.isShutdown());
-        assertTrue("isTerminated", executor.isTerminated());
+        assertTrue(executor.awaitTermination(3, TimeUnit.SECONDS), "awaitTermination");
+        assertSame(taskList, executor.shutdownNow(), "shutdownNow");
+        assertTrue(executor.isShutdown(), "isShutdown");
+        assertTrue(executor.isTerminated(), "isTerminated");
 
         verify(mockDelegate).execute(task);
         verify(mockDelegate).shutdown();