You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by GitBox <gi...@apache.org> on 2022/12/07 18:52:12 UTC

[GitHub] [netbeans] sdedic commented on a diff in pull request #4112: added tests to org.openide.util.TaskTest & upgraded to JUnit4

sdedic commented on code in PR #4112:
URL: https://github.com/apache/netbeans/pull/4112#discussion_r1042564056


##########
platform/openide.util/test/unit/src/org/openide/util/TaskTest.java:
##########
@@ -21,25 +21,183 @@
 
 import java.util.logging.Level;
 import java.util.logging.Logger;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import org.junit.Test;
 import org.netbeans.junit.Log;
-import org.netbeans.junit.NbTestCase;
-import org.openide.util.Exceptions;
-import org.openide.util.Task;
+import static java.lang.System.currentTimeMillis;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertNull;
+import org.junit.Ignore;
 
-public class TaskTest extends NbTestCase {
-    private Logger LOG;
 
-    public TaskTest(String testName) {
-        super(testName);
+public class TaskTest {
+    private static final Logger LOG = Logger.getLogger("org.openide.util.TaskTest");
+
+    private volatile boolean runHasBeenExecuted = false;
+    private volatile Task executedListenerTask = null;
+
+    //--------------------------------------------------------------------------
+    private static void assertFinished(final Task task) {
+
+        assertTrue(task.isFinished());
+    }
+
+    //--------------------------------------------------------------------------
+    private static void assertWaitFinishedReturnsImmediately(final Task task) {
+
+        final long begin = currentTimeMillis();
+        task.waitFinished();
+        final long duration = currentTimeMillis() - begin;
+        // shorter than 1 milisecond
+        assertEquals("The Task.waitFinished() took longer than milisecond. "
+                + "This is not neseserily a bug.", 0, duration);
     }
     
-    @Override
-    protected void setUp() throws Exception {
-        LOG = Logger.getLogger("org.openide.util.Task." + getName());
+    //--------------------------------------------------------------------------
+    private static void assertWaitFinishedWithTimeoutReturnsImmediately(final Task task)
+            throws Exception {
+
+        final long begin = currentTimeMillis();
+        task.waitFinished(0);
+        final long duration = currentTimeMillis() - begin;
+        // shorter than 1 milisecond
+        assertEquals("The Task.waitFinished(long) took longer than milisecond. "
+                + "This is not neseserily a bug.", 0, duration);
     }
 
+    //--------------------------------------------------------------------------
+    @Test
+    public void emptyTask_isImmediatelyFinished_andNeverWaits()
+            throws Exception {
+
+        assertFinished(Task.EMPTY);
+        assertWaitFinishedReturnsImmediately(Task.EMPTY);
+        assertWaitFinishedWithTimeoutReturnsImmediately(Task.EMPTY);
+        assertEquals("task null", Task.EMPTY.toString());
+        assertEquals("null", Task.EMPTY.debug());
+
+        Task empty = new Task(null);
+        assertFinished(empty);
+        assertWaitFinishedReturnsImmediately(empty);
+        assertWaitFinishedWithTimeoutReturnsImmediately(empty);
+        assertEquals("task null", empty.toString());
+        assertEquals("null", empty.debug());
+    }
+
+    //--------------------------------------------------------------------------
+    @Test
+    public void runningEmptyTask_doesNothing() {
+
+        try {
+            Task.EMPTY.run();

Review Comment:
   this is not necessary, simple `assertNotNull(Task.EMPTY)`. Maybe the 1st test that checks that `public static final` object is not null ...



##########
platform/openide.util/test/unit/src/org/openide/util/TaskTest.java:
##########
@@ -21,25 +21,183 @@
 
 import java.util.logging.Level;
 import java.util.logging.Logger;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import org.junit.Test;
 import org.netbeans.junit.Log;
-import org.netbeans.junit.NbTestCase;
-import org.openide.util.Exceptions;
-import org.openide.util.Task;
+import static java.lang.System.currentTimeMillis;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertNull;
+import org.junit.Ignore;
 
-public class TaskTest extends NbTestCase {
-    private Logger LOG;
 
-    public TaskTest(String testName) {
-        super(testName);
+public class TaskTest {
+    private static final Logger LOG = Logger.getLogger("org.openide.util.TaskTest");
+
+    private volatile boolean runHasBeenExecuted = false;
+    private volatile Task executedListenerTask = null;
+
+    //--------------------------------------------------------------------------
+    private static void assertFinished(final Task task) {
+
+        assertTrue(task.isFinished());
+    }
+
+    //--------------------------------------------------------------------------
+    private static void assertWaitFinishedReturnsImmediately(final Task task) {
+
+        final long begin = currentTimeMillis();
+        task.waitFinished();
+        final long duration = currentTimeMillis() - begin;
+        // shorter than 1 milisecond
+        assertEquals("The Task.waitFinished() took longer than milisecond. "
+                + "This is not neseserily a bug.", 0, duration);
     }
     
-    @Override
-    protected void setUp() throws Exception {
-        LOG = Logger.getLogger("org.openide.util.Task." + getName());
+    //--------------------------------------------------------------------------
+    private static void assertWaitFinishedWithTimeoutReturnsImmediately(final Task task)
+            throws Exception {
+
+        final long begin = currentTimeMillis();
+        task.waitFinished(0);
+        final long duration = currentTimeMillis() - begin;
+        // shorter than 1 milisecond
+        assertEquals("The Task.waitFinished(long) took longer than milisecond. "
+                + "This is not neseserily a bug.", 0, duration);
     }
 
+    //--------------------------------------------------------------------------
+    @Test
+    public void emptyTask_isImmediatelyFinished_andNeverWaits()
+            throws Exception {
+
+        assertFinished(Task.EMPTY);
+        assertWaitFinishedReturnsImmediately(Task.EMPTY);
+        assertWaitFinishedWithTimeoutReturnsImmediately(Task.EMPTY);
+        assertEquals("task null", Task.EMPTY.toString());
+        assertEquals("null", Task.EMPTY.debug());
+
+        Task empty = new Task(null);
+        assertFinished(empty);
+        assertWaitFinishedReturnsImmediately(empty);
+        assertWaitFinishedWithTimeoutReturnsImmediately(empty);
+        assertEquals("task null", empty.toString());
+        assertEquals("null", empty.debug());
+    }
+
+    //--------------------------------------------------------------------------
+    @Test
+    public void runningEmptyTask_doesNothing() {
+
+        try {
+            Task.EMPTY.run();
+        } catch (final NullPointerException e) {
+            fail("NullPointerException shall never happen.");
+        }
+    }
+
+    //--------------------------------------------------------------------------
+    @Test
+    public void runningTask_executesRunnableAndListeners() {
+
+        this.runHasBeenExecuted = false;
+        this.executedListenerTask = null;
+
+        Task task = new Task(() -> {
+            this.runHasBeenExecuted = true;
+        });
+        task.addTaskListener((t) -> {
+            this.executedListenerTask = t;
+        });
+
+        assertFalse(task.isFinished());
+        assertNotEquals("null", task.debug());
+
+        task.run();
+
+        assertTrue(this.runHasBeenExecuted);
+        assertEquals(task, this.executedListenerTask);

Review Comment:
   I'd put even `assertSame` here.



##########
platform/openide.util/test/unit/src/org/openide/util/TaskTest.java:
##########
@@ -21,25 +21,183 @@
 
 import java.util.logging.Level;
 import java.util.logging.Logger;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import org.junit.Test;
 import org.netbeans.junit.Log;
-import org.netbeans.junit.NbTestCase;
-import org.openide.util.Exceptions;
-import org.openide.util.Task;
+import static java.lang.System.currentTimeMillis;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertNull;
+import org.junit.Ignore;
 
-public class TaskTest extends NbTestCase {
-    private Logger LOG;
 
-    public TaskTest(String testName) {
-        super(testName);
+public class TaskTest {
+    private static final Logger LOG = Logger.getLogger("org.openide.util.TaskTest");
+
+    private volatile boolean runHasBeenExecuted = false;
+    private volatile Task executedListenerTask = null;
+
+    //--------------------------------------------------------------------------
+    private static void assertFinished(final Task task) {
+
+        assertTrue(task.isFinished());
+    }
+
+    //--------------------------------------------------------------------------
+    private static void assertWaitFinishedReturnsImmediately(final Task task) {
+
+        final long begin = currentTimeMillis();
+        task.waitFinished();
+        final long duration = currentTimeMillis() - begin;
+        // shorter than 1 milisecond
+        assertEquals("The Task.waitFinished() took longer than milisecond. "
+                + "This is not neseserily a bug.", 0, duration);
     }
     
-    @Override
-    protected void setUp() throws Exception {
-        LOG = Logger.getLogger("org.openide.util.Task." + getName());
+    //--------------------------------------------------------------------------
+    private static void assertWaitFinishedWithTimeoutReturnsImmediately(final Task task)
+            throws Exception {
+
+        final long begin = currentTimeMillis();
+        task.waitFinished(0);
+        final long duration = currentTimeMillis() - begin;
+        // shorter than 1 milisecond
+        assertEquals("The Task.waitFinished(long) took longer than milisecond. "
+                + "This is not neseserily a bug.", 0, duration);
     }
 
+    //--------------------------------------------------------------------------
+    @Test
+    public void emptyTask_isImmediatelyFinished_andNeverWaits()
+            throws Exception {
+
+        assertFinished(Task.EMPTY);
+        assertWaitFinishedReturnsImmediately(Task.EMPTY);
+        assertWaitFinishedWithTimeoutReturnsImmediately(Task.EMPTY);
+        assertEquals("task null", Task.EMPTY.toString());
+        assertEquals("null", Task.EMPTY.debug());
+
+        Task empty = new Task(null);
+        assertFinished(empty);
+        assertWaitFinishedReturnsImmediately(empty);
+        assertWaitFinishedWithTimeoutReturnsImmediately(empty);
+        assertEquals("task null", empty.toString());
+        assertEquals("null", empty.debug());
+    }
+
+    //--------------------------------------------------------------------------
+    @Test
+    public void runningEmptyTask_doesNothing() {
+
+        try {
+            Task.EMPTY.run();
+        } catch (final NullPointerException e) {
+            fail("NullPointerException shall never happen.");
+        }
+    }
+
+    //--------------------------------------------------------------------------
+    @Test
+    public void runningTask_executesRunnableAndListeners() {
+
+        this.runHasBeenExecuted = false;
+        this.executedListenerTask = null;
+
+        Task task = new Task(() -> {
+            this.runHasBeenExecuted = true;
+        });
+        task.addTaskListener((t) -> {
+            this.executedListenerTask = t;
+        });
+
+        assertFalse(task.isFinished());
+        assertNotEquals("null", task.debug());
+
+        task.run();
+
+        assertTrue(this.runHasBeenExecuted);
+        assertEquals(task, this.executedListenerTask);
+        assertTrue(task.isFinished());
+    }
+
+    //--------------------------------------------------------------------------
+    @Test
+    public void runningTask_doesNotRunRemovedListeners() {
+
+        this.executedListenerTask = null;
+        TaskListener listener = (t) -> {
+            this.executedListenerTask = t;
+        };
+
+        Task task = new Task(() -> {
+        });
+        task.addTaskListener(listener);
+        task.removeTaskListener(listener);
+
+        task.run();
+
+        assertTrue(task.isFinished());
+        assertNull(this.executedListenerTask);
+    }
+
+    //--------------------------------------------------------------------------
+    @Test
+    public void finishedTask_executesAddedListenerImmediately() {
+
+        this.executedListenerTask = null;
+
+        Task task = new Task(() -> {
+        });
+        task.run();
+
+        assertNull(this.executedListenerTask);
+        assertTrue(task.isFinished());
+
+        task.addTaskListener((t) -> {
+            this.executedListenerTask = t;
+        });
+
+        assertEquals(task, this.executedListenerTask);

Review Comment:
   this is an important feature, thanks for the test.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists