001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.openide.util;
042:
043: import junit.textui.TestRunner;
044:
045: import org.netbeans.junit.*;
046:
047: /**
048: *
049: * @author Jaroslav Tulach
050: */
051: public class TaskTest extends NbTestCase {
052: /** Creates a new instance of UtilProgressCursorTest */
053: public TaskTest(String testName) {
054: super (testName);
055: }
056:
057: public static void main(java.lang.String[] args) {
058: TestRunner.run(new NbTestSuite(TaskTest.class));
059: }
060:
061: //
062: // tests
063: //
064: public void testPlainTaskWaitsForBeingExecuted() throws Exception {
065: R run = new R();
066: Task t = new Task(run);
067:
068: Thread thread = new Thread(t);
069: synchronized (run) {
070: thread.start();
071: run.wait();
072: }
073:
074: assertFalse("Not finished", t.isFinished());
075: synchronized (run) {
076: run.notify();
077: }
078:
079: t.waitFinished();
080: assertTrue("Finished", t.isFinished());
081: }
082:
083: public void testTaskEMPTYIsFinished() throws Exception {
084: assertTrue(Task.EMPTY.isFinished());
085: }
086:
087: public void testWaitFinishedOnEMPTYTaskReturnsImmediatelly()
088: throws Exception {
089: Task.EMPTY.waitFinished();
090: }
091:
092: public void testWaitWithTimeOutReturnsImmediatellyOnFinishedTasks()
093: throws Exception {
094: assertTrue("Was successfully finished", Task.EMPTY
095: .waitFinished(0));
096: }
097:
098: public void testWaitWithTimeOutReturnsAfterTimeOutWhenTheTaskIsNotComputedAtAll()
099: throws Exception {
100: long time = System.currentTimeMillis();
101: Task t = new Task(new R());
102: t.waitFinished(1000);
103: time = System.currentTimeMillis() - time;
104:
105: assertFalse("Still not finished", t.isFinished());
106:
107: if (time < 900 || time > 1100) {
108: fail("Something wrong happened the task should wait for 1000ms but it took: "
109: + time);
110: }
111: }
112:
113: public void testWaitOnStrangeTaskThatStartsItsExecutionInOverridenWaitFinishedMethodLikeFolderInstancesDo()
114: throws Exception {
115: class MyTask extends Task {
116: private int values;
117:
118: public MyTask() {
119: notifyFinished();
120: }
121:
122: public void waitFinished() {
123: notifyRunning();
124: values++;
125: notifyFinished();
126: }
127: }
128:
129: MyTask my = new MyTask();
130: assertTrue("The task thinks that he is finished", my
131: .isFinished());
132: assertTrue("Ok, even with timeout we got the result", my
133: .waitFinished(1000));
134: assertEquals("But the old waitFinished is called", 1, my.values);
135: }
136:
137: public void testWaitOnStrangeTaskThatTakesReallyLongTime()
138: throws Exception {
139: class MyTask extends Task {
140: public MyTask() {
141: notifyFinished();
142: }
143:
144: public void waitFinished() {
145: try {
146: Thread.sleep(5000);
147: } catch (InterruptedException ex) {
148: fail("Should not happen");
149: }
150: }
151: }
152:
153: MyTask my = new MyTask();
154: assertTrue("The task thinks that he is finished", my
155: .isFinished());
156: assertFalse("but still it get's called, but timeouts", my
157: .waitFinished(1000));
158: }
159:
160: final class R implements Runnable {
161: public synchronized void run() {
162: notify();
163: try {
164: wait();
165: } catch (InterruptedException ex) {
166: ex.printStackTrace();
167: }
168: }
169: }
170:
171: }
|