KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > util > TaskTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.openide.util;
20
21 import junit.textui.TestRunner;
22
23
24 import org.netbeans.junit.*;
25
26 /**
27  *
28  * @author Jaroslav Tulach
29  */

30 public class TaskTest extends NbTestCase {
31     /** Creates a new instance of UtilProgressCursorTest */
32     public TaskTest(String JavaDoc testName) {
33         super(testName);
34     }
35
36     public static void main(java.lang.String JavaDoc[] args) {
37         TestRunner.run(new NbTestSuite(TaskTest.class));
38     }
39
40
41     //
42
// tests
43
//
44
public void testPlainTaskWaitsForBeingExecuted () throws Exception JavaDoc {
45         R run = new R ();
46         Task t = new Task (run);
47         
48         Thread JavaDoc thread = new Thread JavaDoc (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 JavaDoc {
64         assertTrue (Task.EMPTY.isFinished ());
65     }
66     
67     public void testWaitFinishedOnEMPTYTaskReturnsImmediatelly () throws Exception JavaDoc {
68         Task.EMPTY.waitFinished ();
69     }
70
71     public void testWaitWithTimeOutReturnsImmediatellyOnFinishedTasks () throws Exception JavaDoc {
72         assertTrue ("Was successfully finished", Task.EMPTY.waitFinished (0));
73     }
74     
75     public void testWaitWithTimeOutReturnsAfterTimeOutWhenTheTaskIsNotComputedAtAll () throws Exception JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc 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 JavaDoc {
130         public synchronized void run () {
131             notify ();
132             try {
133                 wait ();
134             } catch (InterruptedException JavaDoc ex) {
135                 ex.printStackTrace();
136             }
137         }
138     }
139
140 }
141
Popular Tags