KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > InstantiatingIteratorTest


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.openide;
20
21
22 import org.netbeans.junit.*;
23 import org.netbeans.junit.NbTestCase;
24 import org.netbeans.junit.NbTestSuite;
25
26 import java.awt.Component JavaDoc;
27 import java.beans.PropertyChangeListener JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.util.*;
30 import javax.swing.*;
31 import javax.swing.JLabel JavaDoc;
32 import javax.swing.event.ChangeListener JavaDoc;
33 import org.netbeans.junit.NbTestCase;
34 import org.openide.util.*;
35 import org.openide.util.HelpCtx;
36
37 /** Testing functional implementation calling the methods to interface <code>WizardDescriptor.InstantiatingIterator</code>
38  * from WizardDescriptor.
39  */

40 public class InstantiatingIteratorTest extends NbTestCase {
41
42     
43     public InstantiatingIteratorTest (String JavaDoc name) {
44         super(name);
45     }
46     
47     public static void main(String JavaDoc[] args) {
48         junit.textui.TestRunner.run (new NbTestSuite (InstantiatingIteratorTest.class));
49         System.exit (0);
50     }
51     
52     protected WizardDescriptor wd;
53     protected String JavaDoc exceptedValue;
54     private Iterator iterator;
55     protected int attachedInIterator = 0;
56     protected int attachedInPanel = 0;
57     protected boolean checkOrder = false;
58     protected boolean shouldThrowException = false;
59     protected boolean uninitializeShouldThrowException = false;
60     protected Set/*<ChangeListener>*/ changeListenersInIterator = new HashSet ();
61     protected Set/*<ChangeListener>*/ changeListenersInPanel = new HashSet ();
62     protected boolean checkIfInAWT;
63
64     protected void setUp () {
65         iterator = new Iterator ();
66         wd = new WizardDescriptor (iterator);
67         wd.addPropertyChangeListener(new Listener JavaDoc ());
68         java.awt.Dialog JavaDoc d = DialogDisplayer.getDefault ().createDialog (wd);
69         checkOrder = false;
70         shouldThrowException = false;
71         //d.show();
72
}
73     
74     /** Run all tests in AWT thread */
75     protected boolean runInEQ() {
76         return true;
77     }
78     
79     public void testCleanChangeListenerAfterFinish () {
80         assertEquals ("One listener is attached.", 1, changeListenersInIterator.size ());
81         wd.doNextClick ();
82         assertEquals ("Still only one listener is attached after Next.", 1, changeListenersInIterator.size ());
83         wd.doPreviousClick ();
84         assertEquals ("Still only one listener is attached after Previous.", 1, changeListenersInIterator.size ());
85         finishWizard (wd);
86         assertEquals ("No one listener is attached after Finish.", 0, changeListenersInIterator.size ());
87         assertEquals ("No one listener is attached in WD.Panel after Finish.", 0, changeListenersInPanel.size ());
88     }
89     
90     public void testCleanChangeListenerAfterCancel () {
91         assertEquals ("One listener is attached.", 1, changeListenersInIterator.size ());
92         wd.doCancelClick ();
93         assertEquals ("No one listener is attached after Cancel.", 0, changeListenersInIterator.size ());
94         assertEquals ("No one listener is attached in WD.Panel after Finish.", 0, changeListenersInPanel.size ());
95     }
96     
97     public void testInitializeIterator () throws Exception JavaDoc {
98         assertTrue ("InstantiatingIterator was initialized.", getInitialized ().booleanValue ());
99         assertNull ("InstantiatingIterator wasn't instantiated.", getResult ());
100     }
101
102     public void testUninitializeIterator () throws Exception JavaDoc {
103         assertTrue ("InstantiatingIterator was initialized at start.", getInitialized ().booleanValue ());
104         wd.doCancelClick ();
105         assertFalse ("InstantiatingIterator was uninitialized after cancel.", getInitialized ().booleanValue ());
106         assertNull ("InstantiatingIterator wasn't instantiated.", getResult ());
107     }
108
109     public void testFinishAndUninitializeIterator () throws Exception JavaDoc {
110         assertTrue ("InstantiatingIterator was initialized at start.", getInitialized ().booleanValue ());
111         wd.doNextClick ();
112         assertTrue ("InstantiatingIterator wasn't uninitialized after next.", getInitialized ().booleanValue ());
113         finishWizard (wd);
114         assertFalse ("InstantiatingIterator wasn uninitialized after finish.", getInitialized ().booleanValue ());
115         assertNotNull ("InstantiatingIterator was instantiated.", getResult ());
116     }
117
118     public void testUninitializeIteratorAndCalledCurrent () throws Exception JavaDoc {
119         assertTrue ("InstantiatingIterator was initialized at start.", getInitialized ().booleanValue ());
120         wd.doNextClick ();
121         assertTrue ("InstantiatingIterator wasn't uninitialized after next.", getInitialized ().booleanValue ());
122         finishWizard (wd);
123         assertFalse ("InstantiatingIterator was uninitialized after finish.", getInitialized ().booleanValue ());
124         assertNotNull ("InstantiatingIterator was instantiated.", getResult ());
125     }
126
127     public void testOrderStoreSettingAndInstantiate () throws Exception JavaDoc {
128         checkOrder = true;
129         wd.doNextClick ();
130         finishWizard (wd);
131         assertNotNull ("InstantiatingIterator was instantiated.", getResult ());
132     }
133
134     public void testGetInstantiatedObjects () throws Exception JavaDoc {
135         wd.doNextClick ();
136         finishWizard (wd);
137         assertNotNull ("InstantiatingIterator was instantiated.", getResult ());
138         Set newObjects = wd.getInstantiatedObjects ();
139         assertEquals ("WD returns same objects as InstantiatingIterator instantiated.", getResult (), newObjects);
140         
141     }
142     
143     public void testInstantiateInAWTQueueOrNot () {
144         checkIfInAWT = true;
145
146         wd.doNextClick ();
147         finishWizard (wd);
148         try {
149             Set newObjects = wd.getInstantiatedObjects ();
150         } catch (IllegalStateException JavaDoc ise) {
151             fail ("IllegalStateException was caught because WD.instantiate() called outside AWT queue.");
152         }
153         assertNotNull ("InstantiatingIterator was correctly instantiated.", getResult ());
154     }
155     
156     public void testFinishOptionWhenInstantiateFails () throws Exception JavaDoc {
157         shouldThrowException = true;
158
159         wd.doNextClick ();
160         Object JavaDoc state = wd.getValue();
161         finishWizard (wd);
162         
163         assertNull ("InstantiatingIterator was not correctly instantiated.", getResult ());
164         try {
165             Set newObjects = wd.getInstantiatedObjects ();
166             fail ("No IllegalStateException was caught. Should be thrown when invoked getInstantiatedObjects() on unfinished wizard.");
167         } catch (IllegalStateException JavaDoc ise) {
168             // correct behavior
169
}
170         assertEquals ("The state is same as before instantiate()", state, wd.getValue ());
171     }
172     
173     public void testFinishOptionWhenUninitializeThrowsError () throws Exception JavaDoc {
174         shouldThrowException = false;
175         uninitializeShouldThrowException = true;
176
177         wd.doFinishClick();
178         Object JavaDoc state = wd.getValue();
179         finishWizard (wd);
180         
181         assertEquals ("The state is same as before instantiate()", state, wd.getValue ());
182     }
183     
184     public class Panel implements WizardDescriptor.FinishablePanel {
185         private JLabel JavaDoc component;
186         private String JavaDoc text;
187         public Panel(String JavaDoc text) {
188             this.text = text;
189         }
190
191         public Component JavaDoc getComponent() {
192             if (component == null) {
193                 component = new JLabel JavaDoc (text);
194             }
195             return component;
196         }
197         
198         public void addChangeListener(ChangeListener JavaDoc l) {
199             changeListenersInPanel.add (l);
200         }
201         
202         public HelpCtx getHelp() {
203             return null;
204         }
205         
206         public boolean isValid() {
207             return true;
208         }
209         
210         public void readSettings(Object JavaDoc settings) {
211             log ("readSettings of panel: " + text + " [time: " + System.currentTimeMillis () +
212                     "] with PROP_VALUE: " + handleValue (wd.getValue ()));
213         }
214         
215         public void removeChangeListener(ChangeListener JavaDoc l) {
216             changeListenersInPanel.remove (l);
217         }
218         
219         public void storeSettings(Object JavaDoc settings) {
220             if (checkOrder) {
221                 assertNull ("WD.P.storeSettings() called before WD.I.instantiate()", getResult ());
222                 // bugfix #45093, remember storeSettings could be called multiple times
223
// do check order only when the first time
224
checkOrder = false;
225             }
226             log ("storeSettings of panel: " + text + " [time: " + System.currentTimeMillis () +
227                     "] with PROP_VALUE: " + handleValue (wd.getValue ()));
228             if (exceptedValue != null) {
229                 assertEquals ("WD.getValue() returns excepted value.", exceptedValue, handleValue (wd.getValue ()));
230             }
231         }
232         
233         public boolean isFinishPanel () {
234             return true;
235         }
236         
237     }
238     
239     protected Boolean JavaDoc getInitialized () {
240         return iterator.initialized;
241     }
242     
243     protected Set getResult () {
244         return iterator.result;
245     }
246     
247     public class Iterator implements WizardDescriptor.InstantiatingIterator {
248         int index = 0;
249         WizardDescriptor.Panel panels[] = new WizardDescriptor.Panel[2];
250         java.util.Set JavaDoc helpSet;
251         
252         private Boolean JavaDoc initialized = null;
253         private Set result = null;
254         
255         public WizardDescriptor.Panel current () {
256             assertTrue ("WD.current() called on initialized iterator.", initialized != null && initialized.booleanValue ());
257             return panels[index];
258         }
259         public String JavaDoc name () {
260             return "Test iterator";
261         }
262         public boolean hasNext () {
263             return index < 1;
264         }
265         public boolean hasPrevious () {
266             return index > 0;
267         }
268         public void nextPanel () {
269             if (!hasNext ()) throw new NoSuchElementException ();
270             index ++;
271         }
272         public void previousPanel () {
273             if (!hasPrevious ()) throw new NoSuchElementException ();
274             index --;
275         }
276         public void addChangeListener (ChangeListener JavaDoc l) {
277             changeListenersInIterator.add (l);
278         }
279         public void removeChangeListener (ChangeListener JavaDoc l) {
280             changeListenersInIterator.remove (l);
281         }
282         public java.util.Set JavaDoc instantiate () throws IOException JavaDoc {
283             if (checkIfInAWT) {
284                 if (! SwingUtilities.isEventDispatchThread ()) {
285                     throw new IOException JavaDoc ("Must run in AWT queue.");
286                 }
287             }
288             if (shouldThrowException) {
289                 throw new IOException JavaDoc ("Test throw IOException during instantiate().");
290             }
291             if (initialized.booleanValue ()) {
292                 helpSet.add ("member");
293                 result = helpSet;
294             } else {
295                 result = null;
296             }
297             return result;
298         }
299         public void initialize (WizardDescriptor wizard) {
300             helpSet = new HashSet ();
301             panels[0] = new Panel("first panel");
302             panels[1] = new Panel("second panel");
303             initialized = Boolean.TRUE;
304         }
305         public void uninitialize (WizardDescriptor wizard) {
306             if (uninitializeShouldThrowException) {
307                 throw new RuntimeException JavaDoc ("test");
308             }
309             helpSet.clear ();
310             initialized = Boolean.FALSE;
311             panels = null;
312         }
313     }
314     
315     public class Listener implements PropertyChangeListener JavaDoc {
316         
317         public void propertyChange(java.beans.PropertyChangeEvent JavaDoc propertyChangeEvent) {
318             if (WizardDescriptor.PROP_VALUE.equals(propertyChangeEvent.getPropertyName ())) {
319                 log("propertyChange [time: " + System.currentTimeMillis () +
320                                     "] with PROP_VALUE: " + handleValue (wd.getValue ()));
321
322             }
323         }
324         
325     }
326     
327     public String JavaDoc handleValue (Object JavaDoc val) {
328         if (val == null) return "NULL";
329         if (val instanceof String JavaDoc) return (String JavaDoc) val;
330         if (WizardDescriptor.FINISH_OPTION.equals (val)) return "FINISH_OPTION";
331         if (WizardDescriptor.CANCEL_OPTION.equals (val)) return "CANCEL_OPTION";
332         if (WizardDescriptor.CLOSED_OPTION.equals (val)) return "CLOSED_OPTION";
333         if (val instanceof JButton) {
334             JButton butt = (JButton) val;
335             ResourceBundle b = NbBundle.getBundle ("org.openide.Bundle"); // NOI18N
336
if (b.getString ("CTL_NEXT").equals (butt.getText ())) return "NEXT_OPTION";
337             if (b.getString ("CTL_PREVIOUS").equals (butt.getText ())) return "NEXT_PREVIOUS";
338             if (b.getString ("CTL_FINISH").equals (butt.getText ())) return "FINISH_OPTION";
339             if (b.getString ("CTL_CANCEL").equals (butt.getText ())) return "CANCEL_OPTION";
340         }
341         return "UNKNOWN OPTION: " + val;
342     }
343     
344     public static void finishWizard (WizardDescriptor wd) {
345         wd.doFinishClick ();
346         WizardDescriptor.ASYNCHRONOUS_JOBS_RP.post (new Runnable JavaDoc () {
347             public void run () {}
348         }).waitFinished ();
349     }
350 }
351
Popular Tags