KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > ProgressInstantiatingIteratorTest


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

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