001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.openide;
042:
043: import org.netbeans.junit.NbTestSuite;
044:
045: import java.awt.Component;
046: import java.io.IOException;
047: import java.util.*;
048: import javax.swing.*;
049: import javax.swing.JLabel;
050: import javax.swing.event.ChangeListener;
051: import org.openide.InstantiatingIteratorTest.Listener;
052: import org.openide.util.HelpCtx;
053:
054: /** Testing functional implementation calling the methods to interface <code>WizardDescriptor.AsynchronousInstantiatingIterator</code>
055: * from WizardDescriptor. Check if the method <code>instantiate()</code> is called outside AWT in particular.
056: * @see Issue 62161
057: */
058: public class AsynchronousInstantiatingIteratorTest extends
059: InstantiatingIteratorTest {
060:
061: public AsynchronousInstantiatingIteratorTest(String name) {
062: super (name);
063: }
064:
065: public static void main(String[] args) {
066: junit.textui.TestRunner.run(new NbTestSuite(
067: AsynchronousInstantiatingIteratorTest.class));
068: System.exit(0);
069: }
070:
071: private Iterator iterator;
072:
073: protected void setUp() {
074: iterator = new Iterator();
075: wd = new WizardDescriptor(iterator);
076: wd.addPropertyChangeListener(new Listener());
077: java.awt.Dialog d = DialogDisplayer.getDefault().createDialog(
078: wd);
079: checkOrder = false;
080: shouldThrowException = false;
081: //d.show();
082: }
083:
084: /** Run all tests in AWT thread */
085: protected boolean runInEQ() {
086: return true;
087: }
088:
089: public void testInstantiateInAWTQueueOrNot() {
090: checkIfInAWT = true;
091:
092: wd.doNextClick();
093: finishWizard(wd);
094: try {
095: Set newObjects = wd.getInstantiatedObjects();
096: } catch (IllegalStateException ise) {
097: fail("IllegalStateException was caught because WD.instantiate() called in AWT queue.");
098: }
099: assertNotNull(
100: "InstantiatingIterator was correctly instantiated.",
101: getResult());
102: }
103:
104: public class Panel implements WizardDescriptor.FinishablePanel {
105: private JLabel component;
106: private String text;
107:
108: public Panel(String text) {
109: this .text = text;
110: }
111:
112: public Component getComponent() {
113: if (component == null) {
114: component = new JLabel(text);
115: }
116: return component;
117: }
118:
119: public void addChangeListener(ChangeListener l) {
120: changeListenersInPanel.add(l);
121: }
122:
123: public HelpCtx getHelp() {
124: return null;
125: }
126:
127: public boolean isValid() {
128: return true;
129: }
130:
131: public void readSettings(Object settings) {
132: log("readSettings of panel: " + text + " [time: "
133: + System.currentTimeMillis()
134: + "] with PROP_VALUE: "
135: + handleValue(wd.getValue()));
136: }
137:
138: public void removeChangeListener(ChangeListener l) {
139: changeListenersInPanel.remove(l);
140: }
141:
142: public void storeSettings(Object settings) {
143: if (checkOrder) {
144: assertNull(
145: "WD.P.storeSettings() called before WD.I.instantiate()",
146: iterator.result);
147: // bugfix #45093, remember storeSettings could be called multiple times
148: // do check order only when the first time
149: checkOrder = false;
150: }
151: log("storeSettings of panel: " + text + " [time: "
152: + System.currentTimeMillis()
153: + "] with PROP_VALUE: "
154: + handleValue(wd.getValue()));
155: if (exceptedValue != null) {
156: assertEquals("WD.getValue() returns excepted value.",
157: exceptedValue, handleValue(wd.getValue()));
158: }
159: }
160:
161: public boolean isFinishPanel() {
162: return true;
163: }
164:
165: }
166:
167: protected Boolean getInitialized() {
168: return iterator.initialized;
169: }
170:
171: protected Set getResult() {
172: return iterator.result;
173: }
174:
175: public class Iterator implements
176: WizardDescriptor.AsynchronousInstantiatingIterator {
177: int index = 0;
178: WizardDescriptor.Panel panels[] = new WizardDescriptor.Panel[2];
179: java.util.Set helpSet;
180:
181: private Boolean initialized = null;
182: private Set result = null;
183:
184: public WizardDescriptor.Panel current() {
185: assertTrue("WD.current() called on initialized iterator.",
186: initialized != null && initialized.booleanValue());
187: return panels[index];
188: }
189:
190: public String name() {
191: return "Test iterator";
192: }
193:
194: public boolean hasNext() {
195: return index < 1;
196: }
197:
198: public boolean hasPrevious() {
199: return index > 0;
200: }
201:
202: public void nextPanel() {
203: if (!hasNext())
204: throw new NoSuchElementException();
205: index++;
206: }
207:
208: public void previousPanel() {
209: if (!hasPrevious())
210: throw new NoSuchElementException();
211: index--;
212: }
213:
214: public void addChangeListener(ChangeListener l) {
215: changeListenersInIterator.add(l);
216: }
217:
218: public void removeChangeListener(ChangeListener l) {
219: changeListenersInIterator.remove(l);
220: }
221:
222: public java.util.Set instantiate() throws IOException {
223: if (checkIfInAWT) {
224: if (SwingUtilities.isEventDispatchThread()) {
225: throw new IOException("Cannot run in AWT queue.");
226: }
227: }
228: if (shouldThrowException) {
229: throw new IOException(
230: "Test throw IOException during instantiate().");
231: }
232: if (initialized.booleanValue()) {
233: helpSet.add("member");
234: result = helpSet;
235: } else {
236: result = null;
237: }
238: return result;
239: }
240:
241: public void initialize(WizardDescriptor wizard) {
242: helpSet = new HashSet();
243: panels[0] = new Panel("first panel");
244: panels[1] = new Panel("second panel");
245: initialized = Boolean.TRUE;
246: }
247:
248: public void uninitialize(WizardDescriptor wizard) {
249: helpSet.clear();
250: initialized = Boolean.FALSE;
251: panels = null;
252: }
253: }
254:
255: }
|