KickJava   Java API By Example, From Geeks To Geeks.

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


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.openide.util;
21
22 import java.awt.AWTEvent JavaDoc;
23 import java.awt.Dialog JavaDoc;
24 import java.awt.Frame JavaDoc;
25 import java.awt.Graphics JavaDoc;
26 import java.awt.Toolkit JavaDoc;
27 import java.awt.event.AWTEventListener JavaDoc;
28 import java.awt.event.ActionEvent JavaDoc;
29 import java.awt.event.ActionListener JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.EventListener JavaDoc;
32 import javax.swing.JPanel JavaDoc;
33 import javax.swing.SwingUtilities JavaDoc;
34 import javax.swing.Timer JavaDoc;
35 import org.netbeans.junit.NbTestCase;
36
37 /**
38  *
39  * @author Dafe Simonek
40  */

41 public class InitJobTest extends NbTestCase {
42     /** testing dialog instance */
43     Dialog JavaDoc dlg;
44     /** arrays which hold calls history */
45     ArrayList JavaDoc constructCalls, finishCalls, cancelCalls;
46     /** event dispatch thread */
47     Thread JavaDoc edThread;
48     /** test component */
49     SimpleInitComp comp;
50     /** parent, main frame */
51     private Frame JavaDoc frame;
52     
53     /** Creates a new instance of UtilProgressCursorTest */
54     public InitJobTest(String JavaDoc testName) {
55         super(testName);
56     }
57     
58     /** Run tests in EQ thread, as it touches Swing */
59     protected boolean runInEQ() {
60         return true;
61     }
62     
63     /** Basic testing of Utilities.attachInitJob, if calls to AsyncGUIJob
64      * impl conforms to the API behaviour described in javadoc *
65      */

66     public void testInitJob() throws Exception JavaDoc {
67         System.out.println("Testing simple init job run");
68         initializeSimple();
69         comp = new SimpleInitComp();
70         Utilities.attachInitJob(comp, comp);
71         frame = new Frame JavaDoc();
72         frame.setSize(100, 100);
73         frame.setVisible(true);
74         dlg = new Dialog JavaDoc(frame, true);
75         dlg.setSize(50, 50);
76         dlg.add(comp);
77         dlg.setVisible(true);
78     }
79     
80     public void testCancelAbility() throws Exception JavaDoc {
81         System.out.println("Testing cancel ability of async init job");
82         initializeSimple();
83         initCancelResults();
84         CancelInitComp comp = new CancelInitComp();
85         Utilities.attachInitJob(comp, comp);
86         frame = new Frame JavaDoc();
87         frame.setSize(100, 100);
88         frame.setVisible(true);
89         dlg = new Dialog JavaDoc(frame, true);
90         dlg.setSize(50, 50);
91         dlg.add(comp);
92         dlg.setVisible(true);
93     }
94     
95     
96     /**********************************************************************/
97     
98     private void constructCalled(Thread JavaDoc thread, long time) {
99         constructCalls.add(new CallData(thread, time));
100     }
101     
102     private void finishCalled(Thread JavaDoc thread, long time) {
103         finishCalls.add(new CallData(thread, time));
104     }
105     
106     private void cancelCalled() {
107         cancelCalls.add(new CallData(Thread.currentThread(), System.currentTimeMillis()));
108     }
109     
110     private void checkSimpleResults() {
111         if (constructCalls.size() != 1) {
112             fail("AsyncGUIJob.construct was called " + constructCalls.size() +
113                     " times intead of just once.");
114         }
115         if (finishCalls.size() != 1) {
116             fail("AsyncGUIJob.finish was called " + finishCalls.size() +
117                     " times intead of just once.");
118         }
119         CallData constructCall = (CallData)constructCalls.get(0);
120         CallData finishCall = (CallData)finishCalls.get(0);
121         if (constructCall.thread.equals(edThread)) {
122             fail("AsyncGUIJob.construct *was* called from event dispatch thread, " +
123                     "which is wrong.");
124         }
125         if (!finishCall.thread.equals(edThread)) {
126             fail("AsyncGUIJob.finish *was not* called from event dispatch thread, " +
127                     "which is wrong.");
128         }
129         if (constructCall.time > finishCall.time) {
130             fail("AsyncGUIJob.finish was called before AsyncGUIJob.construct, " +
131                     "which is wrong.");
132         }
133         AWTEventListener JavaDoc[] awtListeners =
134                 Toolkit.getDefaultToolkit().getAWTEventListeners(AWTEvent.PAINT_EVENT_MASK);
135         for (int i = 0; i < awtListeners.length; i++) {
136             if (awtListeners[i].getClass().equals(AsyncInitSupport.class)) {
137                 fail("Probable memory leak: AsyncInitSupport didn't detached " +
138                         "from AWT toolkit.");
139             }
140         }
141         EventListener JavaDoc[] listeners = comp.getListeners(AsyncInitSupport.class);
142         if (listeners.length != 0) {
143             fail("Probable memory leak: AsyncInitSupport didn't detached " +
144                     "from component being inited " + comp);
145         }
146     }
147     
148     private void checkCancelResults() {
149         if (cancelCalls.size() != 1) {
150             fail("Cancellable.cancel was called " + cancelCalls.size() +
151                     " times intead of just once.");
152         }
153         if (finishCalls.size() != 0) {
154             fail("AsyncGUIJob.finish should not been called at all, but was called "
155                     + finishCalls.size() + " times.");
156         }
157     }
158     
159     private void initializeSimple() throws Exception JavaDoc {
160         constructCalls = new ArrayList JavaDoc();
161         finishCalls = new ArrayList JavaDoc();
162         if (SwingUtilities.isEventDispatchThread()) {
163             edThread = Thread.currentThread();
164         } else {
165             SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
166                 public void run() {
167                     edThread = Thread.currentThread();
168                 }
169             });
170         }
171     }
172     
173     private void initCancelResults() {
174         cancelCalls = new ArrayList JavaDoc();
175     }
176     
177     /** Structure for holding data of method call */
178     private final static class CallData {
179         Thread JavaDoc thread;
180         long time;
181         
182         public CallData(Thread JavaDoc thread, long time) {
183             this.thread = thread;
184             this.time = time;
185         }
186     }
187     
188     /** Disposer of windows */
189     private final class TimerListener implements ActionListener JavaDoc {
190         /** true for cancel test, false otherwise */
191         private boolean cancel;
192         public TimerListener(boolean cancel) {
193             this.cancel = cancel;
194         }
195         public void actionPerformed(ActionEvent JavaDoc e) {
196             dlg.dispose();
197             frame.dispose();
198             if (cancel) {
199                 checkCancelResults();
200             } else {
201                 checkSimpleResults();
202             }
203         }
204     }
205     
206     /** Testing component for asynchronous init
207      */

208     private final class SimpleInitComp extends JPanel JavaDoc implements AsyncGUIJob {
209         
210         /** Worker method, can be called in any thread but event dispatch thread.
211          * Implement your time consuming work here.
212          * Always called and completed before {@link #finished} method.
213          *
214          */

215         public void construct() {
216             constructCalled(Thread.currentThread(), System.currentTimeMillis());
217         }
218         
219         /** Method to update UI using given data constructed in {@link #construct}
220          * method. Always called in event dispatch thread, after {@link #construct}
221          * method completed its execution.
222          *
223          */

224         public void finished() {
225             finishCalled(Thread.currentThread(), System.currentTimeMillis());
226         }
227         
228         public void paint(Graphics JavaDoc g) {
229             super.paint(g);
230             Timer JavaDoc timer = new Timer JavaDoc(1000, new TimerListener(false));
231             timer.setRepeats(false);
232             timer.start();
233         }
234         
235     } // end of SimpleInitComp
236

237     /** Testing component for cancel during asynchronous init
238      */

239     private final class CancelInitComp extends JPanel JavaDoc implements AsyncGUIJob, Cancellable {
240         
241         /** Worker method, can be called in any thread but event dispatch thread.
242          * Implement your time consuming work here.
243          * Always called and completed before {@link #finished} method.
244          *
245          */

246         public void construct() {
247             // perform loooong task
248
try {
249                 Thread.sleep(2000);
250             } catch (InterruptedException JavaDoc exc) {
251                 // continue...
252
}
253         }
254         
255         /** Method to update UI using given data constructed in {@link #construct}
256          * method. Always called in event dispatch thread, after {@link #construct}
257          * method completed its execution.
258          *
259          */

260         public void finished() {
261             finishCalled(Thread.currentThread(), System.currentTimeMillis());
262         }
263         
264         public void paint(Graphics JavaDoc g) {
265             super.paint(g);
266             Timer JavaDoc timer = new Timer JavaDoc(1000, new TimerListener(true));
267             timer.setRepeats(false);
268             timer.start();
269         }
270         
271         /** Cancel processing of the job.
272          *
273          */

274         public boolean cancel() {
275             cancelCalled();
276             return true;
277         }
278         
279     } // end of SimpleInitComp
280

281 }
282
Popular Tags