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-2007 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.*;
044: import org.netbeans.junit.NbTestCase;
045: import org.netbeans.junit.NbTestSuite;
046:
047: import java.awt.Component;
048: import java.beans.PropertyChangeListener;
049: import java.io.IOException;
050: import java.util.*;
051: import javax.swing.*;
052: import javax.swing.JLabel;
053: import javax.swing.event.ChangeListener;
054: import org.netbeans.junit.NbTestCase;
055: import org.openide.util.*;
056: import org.openide.util.HelpCtx;
057:
058: /** Testing functional implementation calling the methods to interface <code>WizardDescriptor.InstantiatingIterator</code>
059: * from WizardDescriptor.
060: */
061: public class InstantiatingIteratorTest extends NbTestCase {
062:
063: public InstantiatingIteratorTest(String name) {
064: super (name);
065: }
066:
067: public static void main(String[] args) {
068: junit.textui.TestRunner.run(new NbTestSuite(
069: InstantiatingIteratorTest.class));
070: System.exit(0);
071: }
072:
073: protected WizardDescriptor wd;
074: protected String exceptedValue;
075: private Iterator iterator;
076: protected int attachedInIterator = 0;
077: protected int attachedInPanel = 0;
078: protected boolean checkOrder = false;
079: protected boolean shouldThrowException = false;
080: protected boolean uninitializeShouldThrowException = false;
081: protected Set/*<ChangeListener>*/changeListenersInIterator = new HashSet();
082: protected Set/*<ChangeListener>*/changeListenersInPanel = new HashSet();
083: protected boolean checkIfInAWT;
084:
085: protected void setUp() {
086: iterator = new Iterator();
087: wd = new WizardDescriptor(iterator);
088: wd.addPropertyChangeListener(new Listener());
089: java.awt.Dialog d = DialogDisplayer.getDefault().createDialog(
090: wd);
091: checkOrder = false;
092: shouldThrowException = false;
093: //d.show();
094: }
095:
096: /** Run all tests in AWT thread */
097: protected boolean runInEQ() {
098: return true;
099: }
100:
101: public void testCleanChangeListenerAfterFinish() {
102: assertEquals("One listener is attached.", 1,
103: changeListenersInIterator.size());
104: wd.doNextClick();
105: assertEquals("Still only one listener is attached after Next.",
106: 1, changeListenersInIterator.size());
107: wd.doPreviousClick();
108: assertEquals(
109: "Still only one listener is attached after Previous.",
110: 1, changeListenersInIterator.size());
111: finishWizard(wd);
112: assertEquals("No one listener is attached after Finish.", 0,
113: changeListenersInIterator.size());
114: assertEquals(
115: "No one listener is attached in WD.Panel after Finish.",
116: 0, changeListenersInPanel.size());
117: }
118:
119: public void testCleanChangeListenerAfterCancel() {
120: assertEquals("One listener is attached.", 1,
121: changeListenersInIterator.size());
122: wd.doCancelClick();
123: assertEquals("No one listener is attached after Cancel.", 0,
124: changeListenersInIterator.size());
125: assertEquals(
126: "No one listener is attached in WD.Panel after Finish.",
127: 0, changeListenersInPanel.size());
128: }
129:
130: public void testInitializeIterator() throws Exception {
131: assertTrue("InstantiatingIterator was initialized.",
132: getInitialized().booleanValue());
133: assertNull("InstantiatingIterator wasn't instantiated.",
134: getResult());
135: }
136:
137: public void testUninitializeIterator() throws Exception {
138: assertTrue("InstantiatingIterator was initialized at start.",
139: getInitialized().booleanValue());
140: wd.doCancelClick();
141: assertFalse(
142: "InstantiatingIterator was uninitialized after cancel.",
143: getInitialized().booleanValue());
144: assertNull("InstantiatingIterator wasn't instantiated.",
145: getResult());
146: }
147:
148: public void testFinishAndUninitializeIterator() throws Exception {
149: assertTrue("InstantiatingIterator was initialized at start.",
150: getInitialized().booleanValue());
151: wd.doNextClick();
152: assertTrue(
153: "InstantiatingIterator wasn't uninitialized after next.",
154: getInitialized().booleanValue());
155: finishWizard(wd);
156: assertFalse(
157: "InstantiatingIterator wasn uninitialized after finish.",
158: getInitialized().booleanValue());
159: assertNotNull("InstantiatingIterator was instantiated.",
160: getResult());
161: }
162:
163: public void testUninitializeIteratorAndCalledCurrent()
164: throws Exception {
165: assertTrue("InstantiatingIterator was initialized at start.",
166: getInitialized().booleanValue());
167: wd.doNextClick();
168: assertTrue(
169: "InstantiatingIterator wasn't uninitialized after next.",
170: getInitialized().booleanValue());
171: finishWizard(wd);
172: assertFalse(
173: "InstantiatingIterator was uninitialized after finish.",
174: getInitialized().booleanValue());
175: assertNotNull("InstantiatingIterator was instantiated.",
176: getResult());
177: }
178:
179: public void testOrderStoreSettingAndInstantiate() throws Exception {
180: checkOrder = true;
181: wd.doNextClick();
182: finishWizard(wd);
183: assertNotNull("InstantiatingIterator was instantiated.",
184: getResult());
185: }
186:
187: public void testGetInstantiatedObjects() throws Exception {
188: wd.doNextClick();
189: finishWizard(wd);
190: assertNotNull("InstantiatingIterator was instantiated.",
191: getResult());
192: Set newObjects = wd.getInstantiatedObjects();
193: assertEquals(
194: "WD returns same objects as InstantiatingIterator instantiated.",
195: getResult(), newObjects);
196:
197: }
198:
199: public void testInstantiateInAWTQueueOrNot() {
200: checkIfInAWT = true;
201:
202: wd.doNextClick();
203: finishWizard(wd);
204: try {
205: Set newObjects = wd.getInstantiatedObjects();
206: } catch (IllegalStateException ise) {
207: fail("IllegalStateException was caught because WD.instantiate() called outside AWT queue.");
208: }
209: assertNotNull(
210: "InstantiatingIterator was correctly instantiated.",
211: getResult());
212: }
213:
214: public void testFinishOptionWhenInstantiateFails() throws Exception {
215: shouldThrowException = true;
216:
217: wd.doNextClick();
218: Object state = wd.getValue();
219: finishWizard(wd);
220:
221: assertNull(
222: "InstantiatingIterator was not correctly instantiated.",
223: getResult());
224: try {
225: Set newObjects = wd.getInstantiatedObjects();
226: fail("No IllegalStateException was caught. Should be thrown when invoked getInstantiatedObjects() on unfinished wizard.");
227: } catch (IllegalStateException ise) {
228: // correct behavior
229: }
230: assertEquals("The state is same as before instantiate()",
231: state, wd.getValue());
232: }
233:
234: public void testFinishOptionWhenUninitializeThrowsError()
235: throws Exception {
236: shouldThrowException = false;
237: uninitializeShouldThrowException = true;
238:
239: wd.doFinishClick();
240: Object state = wd.getValue();
241: finishWizard(wd);
242:
243: assertEquals("The state is same as before instantiate()",
244: state, wd.getValue());
245: }
246:
247: public class Panel implements WizardDescriptor.FinishablePanel {
248: private JLabel component;
249: private String text;
250:
251: public Panel(String text) {
252: this .text = text;
253: }
254:
255: public Component getComponent() {
256: if (component == null) {
257: component = new JLabel(text);
258: }
259: return component;
260: }
261:
262: public void addChangeListener(ChangeListener l) {
263: changeListenersInPanel.add(l);
264: }
265:
266: public HelpCtx getHelp() {
267: return null;
268: }
269:
270: public boolean isValid() {
271: return true;
272: }
273:
274: public void readSettings(Object settings) {
275: log("readSettings of panel: " + text + " [time: "
276: + System.currentTimeMillis()
277: + "] with PROP_VALUE: "
278: + handleValue(wd.getValue()));
279: }
280:
281: public void removeChangeListener(ChangeListener l) {
282: changeListenersInPanel.remove(l);
283: }
284:
285: public void storeSettings(Object settings) {
286: if (checkOrder) {
287: assertNull(
288: "WD.P.storeSettings() called before WD.I.instantiate()",
289: getResult());
290: // bugfix #45093, remember storeSettings could be called multiple times
291: // do check order only when the first time
292: checkOrder = false;
293: }
294: log("storeSettings of panel: " + text + " [time: "
295: + System.currentTimeMillis()
296: + "] with PROP_VALUE: "
297: + handleValue(wd.getValue()));
298: if (exceptedValue != null) {
299: assertEquals("WD.getValue() returns excepted value.",
300: exceptedValue, handleValue(wd.getValue()));
301: }
302: }
303:
304: public boolean isFinishPanel() {
305: return true;
306: }
307:
308: }
309:
310: protected Boolean getInitialized() {
311: return iterator.initialized;
312: }
313:
314: protected Set getResult() {
315: return iterator.result;
316: }
317:
318: public class Iterator implements
319: WizardDescriptor.InstantiatingIterator {
320: int index = 0;
321: WizardDescriptor.Panel panels[] = new WizardDescriptor.Panel[2];
322: java.util.Set helpSet;
323:
324: private Boolean initialized = null;
325: private Set result = null;
326:
327: public WizardDescriptor.Panel current() {
328: assertTrue("WD.current() called on initialized iterator.",
329: initialized != null && initialized.booleanValue());
330: return panels[index];
331: }
332:
333: public String name() {
334: return "Test iterator";
335: }
336:
337: public boolean hasNext() {
338: return index < 1;
339: }
340:
341: public boolean hasPrevious() {
342: return index > 0;
343: }
344:
345: public void nextPanel() {
346: if (!hasNext())
347: throw new NoSuchElementException();
348: index++;
349: }
350:
351: public void previousPanel() {
352: if (!hasPrevious())
353: throw new NoSuchElementException();
354: index--;
355: }
356:
357: public void addChangeListener(ChangeListener l) {
358: changeListenersInIterator.add(l);
359: }
360:
361: public void removeChangeListener(ChangeListener l) {
362: changeListenersInIterator.remove(l);
363: }
364:
365: public java.util.Set instantiate() throws IOException {
366: if (checkIfInAWT) {
367: if (!SwingUtilities.isEventDispatchThread()) {
368: throw new IOException("Must run in AWT queue.");
369: }
370: }
371: if (shouldThrowException) {
372: throw new IOException(
373: "Test throw IOException during instantiate().");
374: }
375: if (initialized.booleanValue()) {
376: helpSet.add("member");
377: result = helpSet;
378: } else {
379: result = null;
380: }
381: return result;
382: }
383:
384: public void initialize(WizardDescriptor wizard) {
385: helpSet = new HashSet();
386: panels[0] = new Panel("first panel");
387: panels[1] = new Panel("second panel");
388: initialized = Boolean.TRUE;
389: }
390:
391: public void uninitialize(WizardDescriptor wizard) {
392: if (uninitializeShouldThrowException) {
393: throw new RuntimeException("test");
394: }
395: helpSet.clear();
396: initialized = Boolean.FALSE;
397: panels = null;
398: }
399: }
400:
401: public class Listener implements PropertyChangeListener {
402:
403: public void propertyChange(
404: java.beans.PropertyChangeEvent propertyChangeEvent) {
405: if (WizardDescriptor.PROP_VALUE.equals(propertyChangeEvent
406: .getPropertyName())) {
407: log("propertyChange [time: "
408: + System.currentTimeMillis()
409: + "] with PROP_VALUE: "
410: + handleValue(wd.getValue()));
411:
412: }
413: }
414:
415: }
416:
417: public String handleValue(Object val) {
418: if (val == null)
419: return "NULL";
420: if (val instanceof String)
421: return (String) val;
422: if (WizardDescriptor.FINISH_OPTION.equals(val))
423: return "FINISH_OPTION";
424: if (WizardDescriptor.CANCEL_OPTION.equals(val))
425: return "CANCEL_OPTION";
426: if (WizardDescriptor.CLOSED_OPTION.equals(val))
427: return "CLOSED_OPTION";
428: if (val instanceof JButton) {
429: JButton butt = (JButton) val;
430: ResourceBundle b = NbBundle.getBundle("org.openide.Bundle"); // NOI18N
431: if (b.getString("CTL_NEXT").equals(butt.getText()))
432: return "NEXT_OPTION";
433: if (b.getString("CTL_PREVIOUS").equals(butt.getText()))
434: return "NEXT_PREVIOUS";
435: if (b.getString("CTL_FINISH").equals(butt.getText()))
436: return "FINISH_OPTION";
437: if (b.getString("CTL_CANCEL").equals(butt.getText()))
438: return "CANCEL_OPTION";
439: }
440: return "UNKNOWN OPTION: " + val;
441: }
442:
443: public static void finishWizard(WizardDescriptor wd) {
444: wd.doFinishClick();
445: WizardDescriptor.ASYNCHRONOUS_JOBS_RP.post(new Runnable() {
446: public void run() {
447: }
448: }).waitFinished();
449: }
450: }
|