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.*;
044: import junit.framework.Test;
045: import junit.framework.TestSuite;
046: import org.netbeans.junit.NbTestCase;
047: import org.netbeans.junit.NbTestSuite;
048:
049: import java.awt.Component;
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 behaviour of WizardDescription in order to fix bug 35266
059: ** @see issue 35266
060: */
061: public class WizardDescTest extends NbTestCase {
062:
063: public WizardDescTest(String name) {
064: super (name);
065: }
066:
067: public static void main(String[] args) {
068: junit.textui.TestRunner.run(new NbTestSuite(
069: WizardDescTest.class));
070: System.exit(0);
071: }
072:
073: WizardDescriptor wd;
074: String exceptedValue;
075:
076: protected final void setUp() {
077: WizardDescriptor.Panel panels[] = new WizardDescriptor.Panel[2];
078: panels[0] = new Panel("first panel");
079: panels[1] = new Panel("second panel");
080: wd = new WizardDescriptor(panels);
081: wd.addPropertyChangeListener(new Listener());
082: java.awt.Dialog d = DialogDisplayer.getDefault().createDialog(
083: wd);
084: //d.show();
085: }
086:
087: public boolean runInEQ() {
088: return true;
089: }
090:
091: public void testNextOption() throws Exception {
092: exceptedValue = "NEXT_OPTION";
093: log("Do click Next button.");
094: wd.doNextClick();
095:
096: assertEquals("Closed with next option.",
097: WizardDescriptor.NEXT_OPTION, wd.getValue());
098: }
099:
100: public void testPreviousOption() throws Exception {
101: exceptedValue = "NEXT_OPTION";
102: log("Do click Next button.");
103: wd.doNextClick();
104:
105: exceptedValue = "PREVIOUS_OPTION";
106: log("Do click Previous button.");
107: wd.doPreviousClick();
108:
109: // failed because PREVIOUS_OPTION is replaced with NEXT_OPTION by WD.updateState()
110: assertEquals(
111: "Closed with previous option. \n (failed because PREVIOUS_OPTION is replaced with NEXT_OPTION by WD.updateState())",
112: WizardDescriptor.PREVIOUS_OPTION, wd.getValue());
113: }
114:
115: public void testFinishOption() throws Exception {
116: exceptedValue = "NEXT_OPTION";
117: log("Do click Next button.");
118: wd.doNextClick();
119:
120: exceptedValue = "FINISH_OPTION";
121: log("Do click Finish button.");
122: wd.doFinishClick();
123:
124: assertEquals("Closed with finish option.",
125: WizardDescriptor.FINISH_OPTION, wd.getValue());
126: }
127:
128: public void testCancelOption() throws Exception {
129: exceptedValue = "NEXT_OPTION";
130: log("Do click Next button.");
131: wd.doNextClick();
132:
133: exceptedValue = "CANCEL_OPTION";
134: log("Do click Cancel button.");
135: wd.doCancelClick();
136:
137: assertEquals("Closed with cancel option.",
138: WizardDescriptor.CANCEL_OPTION, wd.getValue());
139: }
140:
141: public void testNextOptionWhenLazyValidationFails()
142: throws Exception {
143: Panel panels[] = new Panel[3];
144:
145: class MyPanel extends Panel implements
146: WizardDescriptor.ValidatingPanel {
147: public String validateMsg;
148: public String failedMsg;
149:
150: public MyPanel() {
151: super ("enhanced panel");
152: }
153:
154: public void validate() throws WizardValidationException {
155: if (validateMsg != null) {
156: failedMsg = validateMsg;
157: throw new WizardValidationException(null,
158: "MyPanel.validate() failed.", validateMsg);
159: }
160: return;
161: }
162: }
163:
164: class MyFinishPanel extends MyPanel implements
165: WizardDescriptor.FinishablePanel {
166: public boolean isFinishPanel() {
167: return true;
168: }
169: }
170:
171: MyPanel mp = new MyPanel();
172: MyFinishPanel mfp = new MyFinishPanel();
173: panels[0] = mp;
174: panels[1] = mfp;
175: panels[2] = new Panel("Last one");
176: wd = new WizardDescriptor(panels);
177:
178: assertNull("Component has not been yet initialized",
179: panels[1].component);
180: mp.failedMsg = null;
181: mp.validateMsg = "xtest-fail-without-msg";
182: wd.doNextClick();
183: assertEquals("The lazy validation failed on Next.",
184: mp.validateMsg, mp.failedMsg);
185: assertNull(
186: "The lazy validation failed, still no initialiaation",
187: panels[1].component);
188: assertNull(
189: "The lazy validation failed, still no initialiaation",
190: panels[2].component);
191: mp.failedMsg = null;
192: mp.validateMsg = null;
193: wd.doNextClick();
194: assertNull("Validation on Next passes", mp.failedMsg);
195: assertNotNull("Now we switched to another panel",
196: panels[1].component);
197: assertNull(
198: "The lazy validation failed, still no initialiaation",
199: panels[2].component);
200:
201: // remember previous state
202: Object state = wd.getValue();
203: mfp.validateMsg = "xtest-fail-without-msg";
204: mfp.failedMsg = null;
205: wd.doFinishClick();
206: assertEquals("The lazy validation failed on Finish.",
207: mfp.validateMsg, mfp.failedMsg);
208: assertNull("The validation failed, still no initialiaation",
209: panels[2].component);
210: assertEquals("State has not changed", state, wd.getValue());
211:
212: mfp.validateMsg = null;
213: mfp.failedMsg = null;
214: wd.doFinishClick();
215: assertNull("Validation on Finish passes", mfp.failedMsg);
216: assertNull("Finish was clicked, no initialization either",
217: panels[2].component);
218: assertEquals("The state is finish",
219: WizardDescriptor.FINISH_OPTION, wd.getValue());
220: }
221:
222: public void testDynamicallyEnabledFinish() throws Exception {
223: WizardDescriptor.Panel panels[] = new WizardDescriptor.Panel[2];
224:
225: class MaybeFinishPanel implements WizardDescriptor.Panel,
226: WizardDescriptor.FinishablePanel {
227: private JLabel component;
228: private String text;
229:
230: public boolean isValid = true;
231: public boolean isFinishPanel = true;
232:
233: public MaybeFinishPanel() {
234: text = "maybe finish panel";
235: }
236:
237: public boolean isFinishPanel() {
238: return isFinishPanel;
239: }
240:
241: public boolean isValid() {
242: return isValid;
243: }
244:
245: public MaybeFinishPanel(String text) {
246: this .text = text;
247: }
248:
249: public Component getComponent() {
250: if (component == null) {
251: component = new JLabel(text);
252: }
253: return component;
254: }
255:
256: public void addChangeListener(ChangeListener l) {
257: }
258:
259: public HelpCtx getHelp() {
260: return null;
261: }
262:
263: public void readSettings(Object settings) {
264: }
265:
266: public void removeChangeListener(ChangeListener l) {
267: }
268:
269: public void storeSettings(Object settings) {
270: }
271: }
272:
273: MaybeFinishPanel firstPanel = new MaybeFinishPanel();
274: Panel normalPanel = new Panel("normal panel");
275: panels[0] = firstPanel;
276: panels[1] = normalPanel;
277: wd = new WizardDescriptor(panels);
278:
279: // if 1. panel is not valid then both button are disabled
280: firstPanel.isValid = false;
281: firstPanel.isFinishPanel = false;
282: wd.updateState();
283: assertFalse("Panel is not valid and Next button is disabled.",
284: wd.isNextEnabled());
285: assertFalse(
286: "Panel is not valid and Finish button is disabled as well.",
287: wd.isFinishEnabled());
288:
289: // now will be panel valid => next will be enabled and finish
290: // button disabled because this panel doesn't implement WD.FinishPanel
291: firstPanel.isValid = true;
292: wd.updateState();
293: assertTrue("Panel is valid then Next button is enabled.", wd
294: .isNextEnabled());
295: assertFalse("Panel doesn't implement WD.FinishPanel.",
296: panels[0] instanceof WizardDescriptor.FinishPanel);
297: assertFalse(
298: "Panel is valid but Finish button is disabled because not FinishPanel.",
299: wd.isFinishEnabled());
300:
301: // panel is valid and finish is enabled => next will be enabled and finish
302: // button enabled too because this panel implements WD.FinishablePanel
303: // isFinishEnabled() returns true despite doesn't implement WD.FinishPanel
304: firstPanel.isValid = true;
305: firstPanel.isFinishPanel = true;
306: wd.updateState();
307: assertTrue("Panel is valid then Next button is enabled.", wd
308: .isNextEnabled());
309: assertFalse("Panel doesn't implement WD.FinishPanel.",
310: panels[0] instanceof WizardDescriptor.FinishPanel);
311: assertTrue("Panel implements WD.FinishablePanel.",
312: panels[0] instanceof WizardDescriptor.FinishablePanel);
313: assertTrue(
314: "Panel is enabled because implements FinishablePanel.",
315: wd.isFinishEnabled());
316: }
317:
318: public void testGetInstantiatedObjectsWhenFinished() {
319: boolean exceptionCaught = false;
320: try {
321: wd.getInstantiatedObjects();
322: } catch (IllegalStateException ise) {
323: exceptionCaught = true;
324: }
325: if (!exceptionCaught) {
326: fail("Call getInstantiatedObjects() only on finished wizard, not on start.");
327: exceptionCaught = false;
328: }
329:
330: log("Do click Next button.");
331: wd.doNextClick();
332: try {
333: wd.getInstantiatedObjects();
334: } catch (IllegalStateException ise) {
335: exceptionCaught = true;
336: }
337: if (!exceptionCaught) {
338: fail("Call getInstantiatedObjects() only on finished wizard, not on next.");
339: exceptionCaught = false;
340: }
341:
342: log("Do click Cancel button.");
343: wd.doFinishClick();
344: try {
345: wd.getInstantiatedObjects();
346: } catch (IllegalStateException ise) {
347: fail("Called getInstantiatedObjects() on finished wizard.");
348: }
349: }
350:
351: public void testGetInstantiatedObjectsWhenCanceled() {
352: boolean exceptionCaught = false;
353: try {
354: wd.getInstantiatedObjects();
355: } catch (IllegalStateException ise) {
356: exceptionCaught = true;
357: }
358: if (!exceptionCaught) {
359: fail("Call getInstantiatedObjects() only on finished wizard, not on start.");
360: exceptionCaught = false;
361: }
362:
363: log("Do click Next button.");
364: wd.doNextClick();
365: try {
366: wd.getInstantiatedObjects();
367: } catch (IllegalStateException ise) {
368: exceptionCaught = true;
369: }
370: if (!exceptionCaught) {
371: fail("Call getInstantiatedObjects() only on finished wizard, not on next.");
372: exceptionCaught = false;
373: }
374:
375: log("Do click Cancel button.");
376: wd.doCancelClick();
377: try {
378: wd.getInstantiatedObjects();
379: } catch (IllegalStateException ise) {
380: exceptionCaught = true;
381: }
382: if (!exceptionCaught) {
383: fail("Call getInstantiatedObjects() only on finished wizard, not when wizard canceled.");
384: exceptionCaught = false;
385: }
386: }
387:
388: public class Panel implements WizardDescriptor.Panel,
389: WizardDescriptor.FinishPanel {
390: private JLabel component;
391: private String text;
392:
393: public Panel(String text) {
394: this .text = text;
395: }
396:
397: public Component getComponent() {
398: if (component == null) {
399: component = new JLabel(text);
400: }
401: return component;
402: }
403:
404: public void addChangeListener(ChangeListener l) {
405: }
406:
407: public HelpCtx getHelp() {
408: return null;
409: }
410:
411: public boolean isValid() {
412: return true;
413: }
414:
415: public void readSettings(Object settings) {
416: log("readSettings of panel: " + text + " [time: "
417: + System.currentTimeMillis()
418: + "] with PROP_VALUE: "
419: + handleValue(wd.getValue()));
420: }
421:
422: public void removeChangeListener(ChangeListener l) {
423: }
424:
425: public void storeSettings(Object settings) {
426: log("storeSettings of panel: " + text + " [time: "
427: + System.currentTimeMillis()
428: + "] with PROP_VALUE: "
429: + handleValue(wd.getValue()));
430: if (exceptedValue != null) {
431: assertEquals("WD.getValue() returns excepted value.",
432: exceptedValue, handleValue(wd.getValue()));
433: }
434: }
435:
436: }
437:
438: public class Listener implements java.beans.PropertyChangeListener {
439:
440: public void propertyChange(
441: java.beans.PropertyChangeEvent propertyChangeEvent) {
442: if (WizardDescriptor.PROP_VALUE.equals(propertyChangeEvent
443: .getPropertyName())) {
444: log("propertyChange [time: "
445: + System.currentTimeMillis()
446: + "] with PROP_VALUE: "
447: + handleValue(wd.getValue()));
448:
449: }
450: }
451:
452: }
453:
454: public String handleValue(Object val) {
455: if (val == null)
456: return "NULL";
457: if (val instanceof String)
458: return (String) val;
459: if (WizardDescriptor.FINISH_OPTION.equals(val))
460: return "FINISH_OPTION";
461: if (WizardDescriptor.CANCEL_OPTION.equals(val))
462: return "CANCEL_OPTION";
463: if (WizardDescriptor.CLOSED_OPTION.equals(val))
464: return "CLOSED_OPTION";
465: if (val instanceof JButton) {
466: JButton butt = (JButton) val;
467: ResourceBundle b = NbBundle.getBundle("org.openide.Bundle"); // NOI18N
468: if (b.getString("CTL_NEXT").equals(butt.getText()))
469: return "NEXT_OPTION";
470: if (b.getString("CTL_PREVIOUS").equals(butt.getText()))
471: return "NEXT_PREVIOUS";
472: if (b.getString("CTL_FINISH").equals(butt.getText()))
473: return "FINISH_OPTION";
474: if (b.getString("CTL_CANCEL").equals(butt.getText()))
475: return "CANCEL_OPTION";
476: }
477: return "UNKNOWN OPTION: " + val;
478: }
479: }
|