KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > util > actions > AsynchronousTest


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.actions;
21
22 import java.awt.Image JavaDoc;
23 import java.awt.Toolkit JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.awt.image.BufferedImage JavaDoc;
26 import java.awt.image.ImageObserver JavaDoc;
27 import java.awt.image.PixelGrabber JavaDoc;
28 import java.util.Date JavaDoc;
29 import java.util.logging.Level JavaDoc;
30 import javax.swing.Icon JavaDoc;
31 import javax.swing.JButton JavaDoc;
32 import junit.textui.TestRunner;
33 import org.netbeans.junit.Log;
34 import org.netbeans.junit.NbTestCase;
35 import org.openide.ErrorManager;
36 import org.openide.util.HelpCtx;
37 import org.openide.util.Lookup;
38 import org.openide.util.actions.CallableSystemAction;
39 import org.openide.util.lookup.AbstractLookup;
40 import org.openide.util.lookup.InstanceContent;
41
42 /** Test general aspects of system actions.
43  * Currently, just the icon.
44  * @author Jesse Glick
45  */

46 public class AsynchronousTest extends NbTestCase {
47
48     private CharSequence JavaDoc err;
49     
50     public AsynchronousTest(String JavaDoc name) {
51         super(name);
52     }
53     
54     protected boolean runInEQ() {
55         return true;
56     }
57
58     protected void setUp() {
59         err = Log.enable("", Level.ALL);
60     }
61     
62     public void testExecutionOfActionsThatDoesNotOverrideAsynchronousIsAsynchronousButWarningIsPrinted() throws Exception JavaDoc {
63         DoesNotOverride action = (DoesNotOverride)DoesNotOverride.get(DoesNotOverride.class);
64         
65         synchronized (action) {
66             action.actionPerformed(new ActionEvent JavaDoc(this, 0, ""));
67             Thread.sleep(500);
68             assertFalse("Not yet finished", action.finished);
69             action.wait();
70             assertTrue("The asynchronous action is finished", action.finished);
71         }
72         
73         if (err.toString().indexOf(DoesNotOverride.class.getName() + " should override") < 0) {
74             fail("There should be warning about not overriding asynchronous: " + err);
75         }
76     }
77     
78     public void testExecutionCanBeAsynchronous() throws Exception JavaDoc {
79         DoesOverrideAndReturnsTrue action = (DoesOverrideAndReturnsTrue)DoesOverrideAndReturnsTrue.get(DoesOverrideAndReturnsTrue.class);
80         
81         synchronized (action) {
82             action.actionPerformed(new ActionEvent JavaDoc(this, 0, ""));
83             Thread.sleep(500);
84             assertFalse("Not yet finished", action.finished);
85             action.wait();
86             assertTrue("The asynchronous action is finished", action.finished);
87         }
88         
89         if (err.toString().indexOf(DoesOverrideAndReturnsTrue.class.getName()) >= 0) {
90             fail("No warning about the class: " + err);
91         }
92     }
93     
94     public void testExecutionCanBeSynchronous() throws Exception JavaDoc {
95         DoesOverrideAndReturnsFalse action = (DoesOverrideAndReturnsFalse)DoesOverrideAndReturnsFalse.get(DoesOverrideAndReturnsFalse.class);
96         
97         synchronized (action) {
98             action.actionPerformed(new ActionEvent JavaDoc(this, 0, ""));
99             assertTrue("The synchronous action is finished immediatelly", action.finished);
100         }
101         
102         if (err.toString().indexOf(DoesOverrideAndReturnsTrue.class.getName()) >= 0) {
103             fail("No warning about the class: " + err);
104         }
105     }
106     
107     public void testExecutionCanBeForcedToBeSynchronous() throws Exception JavaDoc {
108         DoesOverrideAndReturnsTrue action = (DoesOverrideAndReturnsTrue)DoesOverrideAndReturnsTrue.get(DoesOverrideAndReturnsTrue.class);
109         
110         synchronized (action) {
111             action.actionPerformed(new ActionEvent JavaDoc(this, 0, "waitFinished"));
112             assertTrue("When asked for synchronous the action is finished immediatelly", action.finished);
113         }
114         
115         if (err.toString().indexOf(DoesOverrideAndReturnsTrue.class.getName()) >= 0) {
116             fail("No warning about the class: " + err);
117         }
118     }
119     
120     public static class DoesNotOverride extends CallableSystemAction {
121         boolean finished;
122         
123         public HelpCtx getHelpCtx() {
124             return HelpCtx.DEFAULT_HELP;
125         }
126         
127         public String JavaDoc getName() {
128             return "Should warn action";
129         }
130         
131         public synchronized void performAction() {
132             notifyAll();
133             finished = true;
134         }
135         
136     }
137     
138     public static class DoesOverrideAndReturnsTrue extends DoesNotOverride {
139         public boolean asynchronous() {
140             return true;
141         }
142     }
143     
144     public static final class DoesOverrideAndReturnsFalse extends DoesOverrideAndReturnsTrue {
145         public boolean asynchronous() {
146             return false;
147         }
148     }
149 }
150
Popular Tags