KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > execution > PendingTaskTest


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
20 package org.netbeans.core.execution;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import javax.swing.AbstractAction JavaDoc;
24 import junit.framework.*;
25 import org.netbeans.api.progress.ProgressHandle;
26 import org.netbeans.api.progress.ProgressHandleFactory;
27 import org.netbeans.junit.NbTestCase;
28 import org.netbeans.progress.module.Controller;
29 import org.netbeans.progress.spi.ProgressEvent;
30 import org.netbeans.progress.spi.ProgressUIWorker;
31 import org.openide.actions.ActionManager;
32 import org.openide.util.RequestProcessor;
33
34 /**
35  *
36  * @author mkleint
37  */

38 public class PendingTaskTest extends NbTestCase {
39     
40     public PendingTaskTest(String JavaDoc testName) {
41     super(testName);
42     }
43
44     protected void setUp() throws Exception JavaDoc {
45     }
46
47     protected void tearDown() throws Exception JavaDoc {
48     }
49
50     
51     public void testActionManagersInvokeAction() throws InterruptedException JavaDoc {
52         class BlockingAction extends AbstractAction JavaDoc implements Runnable JavaDoc {
53             public synchronized void actionPerformed(ActionEvent JavaDoc e) {
54                 notifyAll();
55                 try {
56                     wait();
57                 } catch (InterruptedException JavaDoc ex) {
58                     ex.printStackTrace();
59                     fail("No InterruptedException please");
60                 }
61             }
62
63             
64             public void run() {
65                 ActionManager.getDefault().invokeAction(this, new ActionEvent JavaDoc(this, 0, ""));
66             }
67         }
68         
69         BlockingAction b = new BlockingAction();
70         
71
72         assertEquals("No tasks now", Install.getPendingTasks().size(), 0);
73         
74         RequestProcessor.Task t;
75         synchronized (b) {
76             t = RequestProcessor.getDefault().post(b);
77             b.wait();
78         }
79         
80         assertEquals("One action in progress", 1, Install.getPendingTasks().size());
81         
82         synchronized (b) {
83             b.notifyAll();
84         }
85         t.waitFinished();
86         
87         assertEquals("Action finished", Install.getPendingTasks().size(), 0);
88     }
89
90     
91     public void testProgressTasks() throws InterruptedException JavaDoc {
92         class MyWorker implements ProgressUIWorker {
93             int cnt;
94         
95             public synchronized void processProgressEvent(ProgressEvent event) {
96                 cnt++;
97                 getLog().println("processProgressEvent: " + event);
98                 notifyAll();
99             }
100             public void processSelectedProgressEvent(ProgressEvent event) {
101                 getLog().println("processSelectedProgressEvent: " + event);
102             }
103
104             public synchronized void waitForEvent() throws InterruptedException JavaDoc {
105                 int prev = cnt;
106                 getLog().println("waitForEvent before wait");
107                 wait(5000);
108                 getLog().println("waitForEvent after wait");
109                 if (prev == cnt) {
110                     fail("Time out - no event delivered");
111                 }
112             }
113         }
114         
115         MyWorker worker = new MyWorker();
116         Controller.defaultInstance = new Controller(worker);
117         
118         ProgressHandle proghandle = ProgressHandleFactory.createHandle("a1");
119         proghandle.setInitialDelay(0);
120         
121         assertEquals("None before", 0, Install.getPendingTasks().size());
122
123         synchronized (worker) {
124             getLog().println("proghandle - start");
125             proghandle.start();
126             worker.waitForEvent();
127         }
128             
129         assertEquals("One now", 1, Install.getPendingTasks().size());
130     
131         // waiting a while to overcome possible optimizations in progress api
132
// that prevent the finish event to be delivered
133
Thread.sleep(1000);
134         
135         synchronized (worker) {
136             getLog().println("proghandle - finish");
137             proghandle.finish();
138             worker.waitForEvent();
139         }
140         
141         assertEquals("None after", 0, Install.getPendingTasks().size());
142     }
143
144 }
145
Popular Tags