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.api.progress.ProgressHandle;
044: import org.netbeans.junit.NbTestSuite;
045:
046: import java.awt.Component;
047: import java.io.IOException;
048: import java.util.*;
049: import javax.swing.*;
050: import javax.swing.JLabel;
051: import javax.swing.event.ChangeListener;
052: import org.openide.InstantiatingIteratorTest.Listener;
053: import org.openide.util.HelpCtx;
054:
055: /** Testing functional implementation calling the methods to interface <code>WizardDescriptor.ProgressInstantiatingIterator</code>
056: * from WizardDescriptor. Check if the method <code>instantiate()</code> is called outside AWT in particular.
057: * @see Issue 58889
058: */
059: public class ProgressInstantiatingIteratorTest extends
060: AsynchronousInstantiatingIteratorTest {
061:
062: public ProgressInstantiatingIteratorTest(String name) {
063: super (name);
064: }
065:
066: private Iterator iterator;
067:
068: protected void setUp() {
069: iterator = new Iterator();
070: wd = new WizardDescriptor(iterator);
071: wd.addPropertyChangeListener(new Listener());
072: java.awt.Dialog d = DialogDisplayer.getDefault().createDialog(
073: wd);
074: checkOrder = false;
075: shouldThrowException = false;
076: //d.show();
077: }
078:
079: /** Run all tests in AWT thread */
080: protected boolean runInEQ() {
081: return true;
082: }
083:
084: public class Panel implements WizardDescriptor.FinishablePanel {
085: private JLabel component;
086: private String text;
087:
088: public Panel(String text) {
089: this .text = text;
090: }
091:
092: public Component getComponent() {
093: if (component == null) {
094: component = new JLabel(text);
095: }
096: return component;
097: }
098:
099: public void addChangeListener(ChangeListener l) {
100: changeListenersInPanel.add(l);
101: }
102:
103: public HelpCtx getHelp() {
104: return null;
105: }
106:
107: public boolean isValid() {
108: return true;
109: }
110:
111: public void readSettings(Object settings) {
112: log("readSettings of panel: " + text + " [time: "
113: + System.currentTimeMillis()
114: + "] with PROP_VALUE: "
115: + handleValue(wd.getValue()));
116: }
117:
118: public void removeChangeListener(ChangeListener l) {
119: changeListenersInPanel.remove(l);
120: }
121:
122: public void storeSettings(Object settings) {
123: if (checkOrder) {
124: assertNull(
125: "WD.P.storeSettings() called before WD.I.instantiate()",
126: iterator.result);
127: // bugfix #45093, remember storeSettings could be called multiple times
128: // do check order only when the first time
129: checkOrder = false;
130: }
131: log("storeSettings of panel: " + text + " [time: "
132: + System.currentTimeMillis()
133: + "] with PROP_VALUE: "
134: + handleValue(wd.getValue()));
135: if (exceptedValue != null) {
136: assertEquals("WD.getValue() returns excepted value.",
137: exceptedValue, handleValue(wd.getValue()));
138: }
139: }
140:
141: public boolean isFinishPanel() {
142: return true;
143: }
144:
145: }
146:
147: protected Boolean getInitialized() {
148: return iterator.initialized;
149: }
150:
151: protected Set getResult() {
152: return iterator.result;
153: }
154:
155: public class Iterator implements
156: WizardDescriptor.ProgressInstantiatingIterator {
157: int index = 0;
158: WizardDescriptor.Panel panels[] = new WizardDescriptor.Panel[2];
159: java.util.Set helpSet;
160:
161: private Boolean initialized = null;
162: private Set result = null;
163:
164: public WizardDescriptor.Panel current() {
165: assertTrue("WD.current() called on initialized iterator.",
166: initialized != null && initialized.booleanValue());
167: return panels[index];
168: }
169:
170: public String name() {
171: return "Test iterator";
172: }
173:
174: public boolean hasNext() {
175: return index < 1;
176: }
177:
178: public boolean hasPrevious() {
179: return index > 0;
180: }
181:
182: public void nextPanel() {
183: if (!hasNext())
184: throw new NoSuchElementException();
185: index++;
186: }
187:
188: public void previousPanel() {
189: if (!hasPrevious())
190: throw new NoSuchElementException();
191: index--;
192: }
193:
194: public void addChangeListener(ChangeListener l) {
195: changeListenersInIterator.add(l);
196: }
197:
198: public void removeChangeListener(ChangeListener l) {
199: changeListenersInIterator.remove(l);
200: }
201:
202: public void initialize(WizardDescriptor wizard) {
203: helpSet = new HashSet();
204: panels[0] = new Panel("first panel");
205: panels[1] = new Panel("second panel");
206: initialized = Boolean.TRUE;
207: }
208:
209: public void uninitialize(WizardDescriptor wizard) {
210: helpSet.clear();
211: initialized = Boolean.FALSE;
212: panels = null;
213: }
214:
215: public Set instantiate(ProgressHandle handle)
216: throws IOException {
217: assertNotNull("ProgressHandle cannot be null", handle);
218: if (checkIfInAWT) {
219: if (SwingUtilities.isEventDispatchThread()) {
220: throw new IOException("Cannot run in AWT queue.");
221: }
222: }
223: if (shouldThrowException) {
224: throw new IOException(
225: "Test throw IOException during instantiate().");
226: }
227: if (initialized.booleanValue()) {
228: helpSet.add("member");
229: result = helpSet;
230: } else {
231: result = null;
232: }
233: return result;
234: }
235:
236: public Set instantiate() throws IOException {
237: fail("Cannot call this method iff implement ProgressInstantiatingIterator!");
238: return null;
239: }
240: }
241:
242: }
|