You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@netbeans.apache.org by mb...@apache.org on 2022/12/08 16:38:16 UTC
[netbeans] branch master updated: moved TaskTest to JUnit4 Added test new tests to cover uncovered functionality.
This is an automated email from the ASF dual-hosted git repository.
mbien pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/netbeans.git
The following commit(s) were added to refs/heads/master by this push:
new 47b064fb7c moved TaskTest to JUnit4 Added test new tests to cover uncovered functionality.
new daefbd50ad Merge pull request #4112 from lbownik/openide.util.task
47b064fb7c is described below
commit 47b064fb7c6a0e0d9865d564f88fbb4763a3b0d7
Author: Lukasz Bownik <lu...@gmail.com>
AuthorDate: Thu May 12 23:34:10 2022 +0200
moved TaskTest to JUnit4
Added test new tests to cover uncovered functionality.
---
.../test/unit/src/org/openide/util/TaskTest.java | 196 +++++++++++++++++++--
1 file changed, 181 insertions(+), 15 deletions(-)
diff --git a/platform/openide.util/test/unit/src/org/openide/util/TaskTest.java b/platform/openide.util/test/unit/src/org/openide/util/TaskTest.java
index c45953edb3..cc5a9d1398 100644
--- a/platform/openide.util/test/unit/src/org/openide/util/TaskTest.java
+++ b/platform/openide.util/test/unit/src/org/openide/util/TaskTest.java
@@ -21,25 +21,186 @@ package org.openide.util;
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.nanoTime;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import org.junit.Ignore;
-public class TaskTest extends NbTestCase {
- private Logger LOG;
- public TaskTest(String testName) {
- super(testName);
+public class TaskTest {
+ private final static long tenMiliseconds = 10000000; // in nanosecods
+
+ 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 = nanoTime();
+ task.waitFinished();
+ final long duration = nanoTime() - begin;
+
+ assertTrue("The Task.waitFinished() took longer than 10 miliseconds. "
+ + "This is not neseserily a bug.", duration < tenMiliseconds);
}
- @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 = nanoTime();
+ task.waitFinished(0);
+ final long duration = nanoTime()- begin;
+
+ assertTrue("The Task.waitFinished(long) took longer than milisecond. "
+ + "This is not neseserily a bug.", duration < tenMiliseconds);
+ }
+
+ //--------------------------------------------------------------------------
+ @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);
+ assertSame(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;
+ });
+
+ assertSame(task, this.executedListenerTask);
+ assertTrue(task.isFinished()); // still finished
+ }
+ //--------------------------------------------------------------------------
+ @Ignore("Current implementation allows null listener but then Task.run throws NPE :(")
+ @Test
+ public void addTaskListener_throwsNullPointer_whenGivenNullArgument() {
+
+ Task task = new Task(() -> {
+ });
+ try {
+ task.addTaskListener(null);
+ fail();
+ } catch (NullPointerException e) {
+ //good
+ }
+ }
+
+ //--------------------------------------------------------------------------
+ @Test
+ public void removeTaskListener_doesNothing_whenGivenNullArgument() {
+
+ this.executedListenerTask = null;
+
+ TaskListener listener = (t) -> {
+ this.executedListenerTask = t;
+ };
+
+ Task task = new Task(() -> {
+ });
+ task.addTaskListener(listener);
+ task.removeTaskListener(null);
+ task.run();
+
+ assertSame(task, this.executedListenerTask);
+ }
+ @Test
public void testPlainTaskWaitsForBeingExecuted () throws Exception {
R run = new R ();
Task t = new Task (run);
@@ -59,18 +220,22 @@ public class TaskTest extends NbTestCase {
assertTrue ("Finished", t.isFinished ());
}
+ // this test is covered by emptyTask_isImmediatelyFinished_andNeverWaits and will be removed in next PR
+ @Test
public void testTaskEMPTYIsFinished () throws Exception {
assertTrue (Task.EMPTY.isFinished ());
}
-
+ // this test is covered by emptyTask_isImmediatelyFinished_andNeverWaits and will be removed in next PR
+ @Test
public void testWaitFinishedOnEMPTYTaskReturnsImmediatelly () throws Exception {
Task.EMPTY.waitFinished ();
}
-
+ // this test is covered by emptyTask_isImmediatelyFinished_andNeverWaits and will be removed in next PR
+ @Test
public void testWaitWithTimeOutReturnsImmediatellyOnFinishedTasks () throws Exception {
assertTrue ("Was successfully finished", Task.EMPTY.waitFinished (0));
}
-
+ @Test
public void testWaitWithTimeOutReturnsAfterTimeOutWhenTheTaskIsNotComputedAtAll () throws Exception {
if (!canWait1s()) {
LOG.warning("Skipping testWaitWithTimeOutReturnsAfterTimeOutWhenTheTaskIsNotComputedAtAll, as the computer is not able to wait 1s!");
@@ -95,7 +260,7 @@ public class TaskTest extends NbTestCase {
fail ("Something wrong happened the task should wait for 1000ms but it took: " + time + "\n" + log);
}
-
+ @Test
public void testWaitOnStrangeTaskThatStartsItsExecutionInOverridenWaitFinishedMethodLikeFolderInstancesDo () throws Exception {
class MyTask extends Task {
private int values;
@@ -116,7 +281,7 @@ public class TaskTest extends NbTestCase {
assertTrue ("Ok, even with timeout we got the result", my.waitFinished (1000));
assertEquals ("But the old waitFinished is called", 1, my.values);
}
-
+ @Test
public void testWaitOnStrangeTaskThatTakesReallyLongTime () throws Exception {
class MyTask extends Task {
public MyTask () {
@@ -151,6 +316,7 @@ public class TaskTest extends NbTestCase {
/*
* see issue #130265
*/
+ @Test
public void testWaitFinished0WaitsUntilFinished() throws Exception {
Task task = new Task(new Runnable() {
@Override
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@netbeans.apache.org
For additional commands, e-mail: commits-help@netbeans.apache.org
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists