KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > AsynchronousInstantiatingIteratorTest


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;
20
21
22 import org.netbeans.junit.NbTestSuite;
23
24 import java.awt.Component JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.*;
27 import javax.swing.*;
28 import javax.swing.JLabel JavaDoc;
29 import javax.swing.event.ChangeListener JavaDoc;
30 import org.openide.InstantiatingIteratorTest.Listener;
31 import org.openide.util.HelpCtx;
32
33 /** Testing functional implementation calling the methods to interface <code>WizardDescriptor.AsynchronousInstantiatingIterator</code>
34  * from WizardDescriptor. Check if the method <code>instantiate()</code> is called outside AWT in particular.
35  * @see Issue 62161
36  */

37 public class AsynchronousInstantiatingIteratorTest extends InstantiatingIteratorTest {
38
39     
40     public AsynchronousInstantiatingIteratorTest (String JavaDoc name) {
41         super(name);
42     }
43     
44     public static void main(String JavaDoc[] args) {
45         junit.textui.TestRunner.run (new NbTestSuite (AsynchronousInstantiatingIteratorTest.class));
46         System.exit (0);
47     }
48     
49     private Iterator iterator;
50
51     protected void setUp () {
52         iterator = new Iterator ();
53         wd = new WizardDescriptor (iterator);
54         wd.addPropertyChangeListener(new Listener JavaDoc ());
55         java.awt.Dialog JavaDoc d = DialogDisplayer.getDefault ().createDialog (wd);
56         checkOrder = false;
57         shouldThrowException = false;
58         //d.show();
59
}
60     
61     /** Run all tests in AWT thread */
62     protected boolean runInEQ() {
63         return true;
64     }
65
66     public void testInstantiateInAWTQueueOrNot () {
67         checkIfInAWT = true;
68
69         wd.doNextClick ();
70         finishWizard (wd);
71         try {
72             Set newObjects = wd.getInstantiatedObjects ();
73         } catch (IllegalStateException JavaDoc ise) {
74             fail ("IllegalStateException was caught because WD.instantiate() called in AWT queue.");
75         }
76         assertNotNull ("InstantiatingIterator was correctly instantiated.", getResult ());
77     }
78     
79     public class Panel implements WizardDescriptor.FinishablePanel {
80         private JLabel JavaDoc component;
81         private String JavaDoc text;
82         public Panel(String JavaDoc text) {
83             this.text = text;
84         }
85
86         public Component JavaDoc getComponent() {
87             if (component == null) {
88                 component = new JLabel JavaDoc (text);
89             }
90             return component;
91         }
92         
93         public void addChangeListener(ChangeListener JavaDoc l) {
94             changeListenersInPanel.add (l);
95         }
96         
97         public HelpCtx getHelp() {
98             return null;
99         }
100         
101         public boolean isValid() {
102             return true;
103         }
104         
105         public void readSettings(Object JavaDoc settings) {
106             log ("readSettings of panel: " + text + " [time: " + System.currentTimeMillis () +
107                     "] with PROP_VALUE: " + handleValue (wd.getValue ()));
108         }
109         
110         public void removeChangeListener(ChangeListener JavaDoc l) {
111             changeListenersInPanel.remove (l);
112         }
113         
114         public void storeSettings(Object JavaDoc settings) {
115             if (checkOrder) {
116                 assertNull ("WD.P.storeSettings() called before WD.I.instantiate()", iterator.result);
117                 // bugfix #45093, remember storeSettings could be called multiple times
118
// do check order only when the first time
119
checkOrder = false;
120             }
121             log ("storeSettings of panel: " + text + " [time: " + System.currentTimeMillis () +
122                     "] with PROP_VALUE: " + handleValue (wd.getValue ()));
123             if (exceptedValue != null) {
124                 assertEquals ("WD.getValue() returns excepted value.", exceptedValue, handleValue (wd.getValue ()));
125             }
126         }
127         
128         public boolean isFinishPanel () {
129             return true;
130         }
131         
132     }
133     
134     protected Boolean JavaDoc getInitialized () {
135         return iterator.initialized;
136     }
137     
138     protected Set getResult () {
139         return iterator.result;
140     }
141     
142     public class Iterator implements WizardDescriptor.AsynchronousInstantiatingIterator {
143         int index = 0;
144         WizardDescriptor.Panel panels[] = new WizardDescriptor.Panel[2];
145         java.util.Set JavaDoc helpSet;
146         
147         private Boolean JavaDoc initialized = null;
148         private Set result = null;
149         
150         public WizardDescriptor.Panel current () {
151             assertTrue ("WD.current() called on initialized iterator.", initialized != null && initialized.booleanValue ());
152             return panels[index];
153         }
154         public String JavaDoc name () {
155             return "Test iterator";
156         }
157         public boolean hasNext () {
158             return index < 1;
159         }
160         public boolean hasPrevious () {
161             return index > 0;
162         }
163         public void nextPanel () {
164             if (!hasNext ()) throw new NoSuchElementException ();
165             index ++;
166         }
167         public void previousPanel () {
168             if (!hasPrevious ()) throw new NoSuchElementException ();
169             index --;
170         }
171         public void addChangeListener (ChangeListener JavaDoc l) {
172             changeListenersInIterator.add (l);
173         }
174         public void removeChangeListener (ChangeListener JavaDoc l) {
175             changeListenersInIterator.remove (l);
176         }
177         public java.util.Set JavaDoc instantiate () throws IOException JavaDoc {
178             if (checkIfInAWT) {
179                 if (SwingUtilities.isEventDispatchThread ()) {
180                     throw new IOException JavaDoc ("Cannot run in AWT queue.");
181                 }
182             }
183             if (shouldThrowException) {
184                 throw new IOException JavaDoc ("Test throw IOException during instantiate().");
185             }
186             if (initialized.booleanValue ()) {
187                 helpSet.add ("member");
188                 result = helpSet;
189             } else {
190                 result = null;
191             }
192             return result;
193         }
194         public void initialize (WizardDescriptor wizard) {
195             helpSet = new HashSet ();
196             panels[0] = new Panel("first panel");
197             panels[1] = new Panel("second panel");
198             initialized = Boolean.TRUE;
199         }
200         public void uninitialize (WizardDescriptor wizard) {
201             helpSet.clear ();
202             initialized = Boolean.FALSE;
203             panels = null;
204         }
205     }
206     
207 }
208
Popular Tags