1 19 package org.openide.util; 20 21 import junit.textui.TestRunner; 22 23 24 import org.netbeans.junit.*; 25 26 30 public class TaskTest extends NbTestCase { 31 32 public TaskTest(String testName) { 33 super(testName); 34 } 35 36 public static void main(java.lang.String [] args) { 37 TestRunner.run(new NbTestSuite(TaskTest.class)); 38 } 39 40 41 public void testPlainTaskWaitsForBeingExecuted () throws Exception { 45 R run = new R (); 46 Task t = new Task (run); 47 48 Thread thread = new Thread (t); 49 synchronized (run) { 50 thread.start (); 51 run.wait (); 52 } 53 54 assertFalse ("Not finished", t.isFinished ()); 55 synchronized (run) { 56 run.notify (); 57 } 58 59 t.waitFinished (); 60 assertTrue ("Finished", t.isFinished ()); 61 } 62 63 public void testTaskEMPTYIsFinished () throws Exception { 64 assertTrue (Task.EMPTY.isFinished ()); 65 } 66 67 public void testWaitFinishedOnEMPTYTaskReturnsImmediatelly () throws Exception { 68 Task.EMPTY.waitFinished (); 69 } 70 71 public void testWaitWithTimeOutReturnsImmediatellyOnFinishedTasks () throws Exception { 72 assertTrue ("Was successfully finished", Task.EMPTY.waitFinished (0)); 73 } 74 75 public void testWaitWithTimeOutReturnsAfterTimeOutWhenTheTaskIsNotComputedAtAll () throws Exception { 76 long time = System.currentTimeMillis (); 77 Task t = new Task (new R ()); 78 t.waitFinished (1000); 79 time = System.currentTimeMillis () - time; 80 81 assertFalse ("Still not finished", t.isFinished ()); 82 83 if (time < 900 || time > 1100) { 84 fail ("Something wrong happened the task should wait for 1000ms but it took: " + time); 85 } 86 } 87 88 public void testWaitOnStrangeTaskThatStartsItsExecutionInOverridenWaitFinishedMethodLikeFolderInstancesDo () throws Exception { 89 class MyTask extends Task { 90 private int values; 91 92 public MyTask () { 93 notifyFinished (); 94 } 95 96 public void waitFinished () { 97 notifyRunning (); 98 values++; 99 notifyFinished (); 100 } 101 } 102 103 MyTask my = new MyTask (); 104 assertTrue ("The task thinks that he is finished", my.isFinished ()); 105 assertTrue ("Ok, even with timeout we got the result", my.waitFinished (1000)); 106 assertEquals ("But the old waitFinished is called", 1, my.values); 107 } 108 109 public void testWaitOnStrangeTaskThatTakesReallyLongTime () throws Exception { 110 class MyTask extends Task { 111 public MyTask () { 112 notifyFinished (); 113 } 114 115 public void waitFinished () { 116 try { 117 Thread.sleep (5000); 118 } catch (InterruptedException ex) { 119 fail ("Should not happen"); 120 } 121 } 122 } 123 124 MyTask my = new MyTask (); 125 assertTrue ("The task thinks that he is finished", my.isFinished ()); 126 assertFalse ("but still it get's called, but timeouts", my.waitFinished (1000)); 127 } 128 129 final class R implements Runnable { 130 public synchronized void run () { 131 notify (); 132 try { 133 wait (); 134 } catch (InterruptedException ex) { 135 ex.printStackTrace(); 136 } 137 } 138 } 139 140 } 141 | Popular Tags |