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 19:18:04 UTC

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

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


##########
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 test is stronger than that.
   it makes sure that empty tasks are acttually runnable (can be run without throwing enything)
   assertNotNull(Task.EMPTY) does not verify this
   
   this tests checks whether things we don't want to happen (run method throwing an exception) actually don't happen.



-- 
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