KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > WizardDescTest


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.junit.*;
23 import junit.framework.Test;
24 import junit.framework.TestSuite;
25 import org.netbeans.junit.NbTestCase;
26 import org.netbeans.junit.NbTestSuite;
27
28 import java.awt.Component JavaDoc;
29 import java.util.*;
30 import javax.swing.*;
31 import javax.swing.JLabel JavaDoc;
32 import javax.swing.event.ChangeListener JavaDoc;
33 import org.netbeans.junit.NbTestCase;
34 import org.openide.util.*;
35 import org.openide.util.HelpCtx;
36
37 /** Testing behaviour of WizardDescription in order to fix bug 35266
38  ** @see issue 35266
39  */

40 public class WizardDescTest extends NbTestCase {
41
42     
43     public WizardDescTest (String JavaDoc name) {
44         super(name);
45     }
46     
47     public static void main(String JavaDoc[] args) {
48         junit.textui.TestRunner.run (new NbTestSuite (WizardDescTest.class));
49         System.exit (0);
50     }
51     
52     WizardDescriptor wd;
53     String JavaDoc exceptedValue;
54
55     protected final void setUp () {
56         WizardDescriptor.Panel panels[] = new WizardDescriptor.Panel[2];
57         panels[0] = new Panel("first panel");
58         panels[1] = new Panel("second panel");
59         wd = new WizardDescriptor(panels);
60         wd.addPropertyChangeListener(new Listener JavaDoc());
61         java.awt.Dialog JavaDoc d = DialogDisplayer.getDefault().createDialog (wd);
62         //d.show();
63
}
64     
65     public boolean runInEQ () {
66         return true;
67     }
68     
69     public void testNextOption () throws Exception JavaDoc {
70         exceptedValue = "NEXT_OPTION";
71         log ("Do click Next button.");
72         wd.doNextClick ();
73
74         assertEquals ("Closed with next option.", WizardDescriptor.NEXT_OPTION, wd.getValue ());
75     }
76
77     public void testPreviousOption () throws Exception JavaDoc {
78         exceptedValue = "NEXT_OPTION";
79         log ("Do click Next button.");
80         wd.doNextClick ();
81
82         exceptedValue = "PREVIOUS_OPTION";
83         log ("Do click Previous button.");
84         wd.doPreviousClick ();
85
86         // failed because PREVIOUS_OPTION is replaced with NEXT_OPTION by WD.updateState()
87
assertEquals ("Closed with previous option. \n (failed because PREVIOUS_OPTION is replaced with NEXT_OPTION by WD.updateState())", WizardDescriptor.PREVIOUS_OPTION, wd.getValue ());
88     }
89
90     public void testFinishOption () throws Exception JavaDoc {
91         exceptedValue = "NEXT_OPTION";
92         log ("Do click Next button.");
93         wd.doNextClick ();
94
95         exceptedValue = "FINISH_OPTION";
96         log ("Do click Finish button.");
97         wd.doFinishClick ();
98
99         assertEquals ("Closed with finish option.", WizardDescriptor.FINISH_OPTION, wd.getValue ());
100     }
101
102     public void testCancelOption () throws Exception JavaDoc {
103         exceptedValue = "NEXT_OPTION";
104         log ("Do click Next button.");
105         wd.doNextClick ();
106
107         exceptedValue = "CANCEL_OPTION";
108         log ("Do click Cancel button.");
109         wd.doCancelClick ();
110
111         assertEquals ("Closed with cancel option.", WizardDescriptor.CANCEL_OPTION, wd.getValue ());
112     }
113
114     public void testNextOptionWhenLazyValidationFails () throws Exception JavaDoc {
115         Panel panels[] = new Panel[3];
116         
117         class MyPanel extends Panel implements WizardDescriptor.ValidatingPanel {
118             public String JavaDoc validateMsg;
119             public String JavaDoc failedMsg;
120             
121             public MyPanel () {
122                 super ("enhanced panel");
123             }
124             
125             public void validate () throws WizardValidationException {
126                 if (validateMsg != null) {
127                     failedMsg = validateMsg;
128                     throw new WizardValidationException (null, "MyPanel.validate() failed.", validateMsg);
129                 }
130                 return;
131             }
132         }
133         
134         class MyFinishPanel extends MyPanel implements WizardDescriptor.FinishablePanel {
135             public boolean isFinishPanel () {
136                 return true;
137             }
138         }
139         
140         MyPanel mp = new MyPanel ();
141         MyFinishPanel mfp = new MyFinishPanel ();
142         panels[0] = mp;
143         panels[1] = mfp;
144         panels[2] = new Panel ("Last one");
145         wd = new WizardDescriptor(panels);
146         
147         assertNull ("Component has not been yet initialized", panels[1].component);
148         mp.failedMsg = null;
149         mp.validateMsg = "xtest-fail-without-msg";
150         wd.doNextClick ();
151         assertEquals ("The lazy validation failed on Next.", mp.validateMsg, mp.failedMsg);
152         assertNull ("The lazy validation failed, still no initialiaation", panels[1].component);
153         assertNull ("The lazy validation failed, still no initialiaation", panels[2].component);
154         mp.failedMsg = null;
155         mp.validateMsg = null;
156         wd.doNextClick ();
157         assertNull ("Validation on Next passes", mp.failedMsg);
158         assertNotNull ("Now we switched to another panel", panels[1].component);
159         assertNull ("The lazy validation failed, still no initialiaation", panels[2].component);
160         
161         // remember previous state
162
Object JavaDoc state = wd.getValue();
163         mfp.validateMsg = "xtest-fail-without-msg";
164         mfp.failedMsg = null;
165         wd.doFinishClick();
166         assertEquals ("The lazy validation failed on Finish.", mfp.validateMsg, mfp.failedMsg);
167         assertNull ("The validation failed, still no initialiaation", panels[2].component);
168         assertEquals ("State has not changed", state, wd.getValue ());
169         
170         mfp.validateMsg = null;
171         mfp.failedMsg = null;
172         wd.doFinishClick ();
173         assertNull ("Validation on Finish passes", mfp.failedMsg);
174         assertNull ("Finish was clicked, no initialization either", panels[2].component);
175         assertEquals ("The state is finish", WizardDescriptor.FINISH_OPTION, wd.getValue ());
176     }
177     
178     public void testDynamicallyEnabledFinish () throws Exception JavaDoc {
179         WizardDescriptor.Panel panels[] = new WizardDescriptor.Panel [2];
180         
181         class MaybeFinishPanel implements WizardDescriptor.Panel, WizardDescriptor.FinishablePanel {
182             private JLabel JavaDoc component;
183             private String JavaDoc text;
184             
185             public boolean isValid = true;
186             public boolean isFinishPanel = true;
187             
188             public MaybeFinishPanel () {
189                 text = "maybe finish panel";
190             }
191             
192             public boolean isFinishPanel () {
193                 return isFinishPanel;
194             }
195             
196             public boolean isValid () {
197                 return isValid;
198             }
199             
200             public MaybeFinishPanel (String JavaDoc text) {
201                 this.text = text;
202             }
203
204             public Component JavaDoc getComponent() {
205                 if (component == null) {
206                     component = new JLabel JavaDoc (text);
207                 }
208                 return component;
209             }
210
211             public void addChangeListener(ChangeListener JavaDoc l) {
212             }
213
214             public HelpCtx getHelp() {
215                 return null;
216             }
217
218             public void readSettings(Object JavaDoc settings) {
219             }
220
221             public void removeChangeListener(ChangeListener JavaDoc l) {
222             }
223
224             public void storeSettings(Object JavaDoc settings) {
225             }
226         }
227         
228         MaybeFinishPanel firstPanel = new MaybeFinishPanel ();
229         Panel normalPanel = new Panel ("normal panel");
230         panels[0] = firstPanel;
231         panels[1] = normalPanel;
232         wd = new WizardDescriptor(panels);
233         
234         // if 1. panel is not valid then both button are disabled
235
firstPanel.isValid = false;
236         firstPanel.isFinishPanel = false;
237         wd.updateState ();
238         assertFalse ("Panel is not valid and Next button is disabled.", wd.isNextEnabled ());
239         assertFalse ("Panel is not valid and Finish button is disabled as well.", wd.isFinishEnabled ());
240         
241         // now will be panel valid => next will be enabled and finish
242
// button disabled because this panel doesn't implement WD.FinishPanel
243
firstPanel.isValid = true;
244         wd.updateState ();
245         assertTrue ("Panel is valid then Next button is enabled.", wd.isNextEnabled ());
246         assertFalse ("Panel doesn't implement WD.FinishPanel.", panels[0] instanceof WizardDescriptor.FinishPanel);
247         assertFalse ("Panel is valid but Finish button is disabled because not FinishPanel.", wd.isFinishEnabled ());
248
249         // panel is valid and finish is enabled => next will be enabled and finish
250
// button enabled too because this panel implements WD.FinishablePanel
251
// isFinishEnabled() returns true despite doesn't implement WD.FinishPanel
252
firstPanel.isValid = true;
253         firstPanel.isFinishPanel = true;
254         wd.updateState ();
255         assertTrue ("Panel is valid then Next button is enabled.", wd.isNextEnabled ());
256         assertFalse ("Panel doesn't implement WD.FinishPanel.", panels[0] instanceof WizardDescriptor.FinishPanel);
257         assertTrue ("Panel implements WD.FinishablePanel.", panels[0] instanceof WizardDescriptor.FinishablePanel);
258         assertTrue ("Panel is enabled because implements FinishablePanel.", wd.isFinishEnabled ());
259     }
260     
261     public void testGetInstantiatedObjectsWhenFinished () {
262         boolean exceptionCaught = false;
263         try {
264             wd.getInstantiatedObjects ();
265         } catch (IllegalStateException JavaDoc ise) {
266             exceptionCaught = true;
267         }
268         if (!exceptionCaught) {
269             fail ("Call getInstantiatedObjects() only on finished wizard, not on start.");
270             exceptionCaught = false;
271         }
272         
273         log ("Do click Next button.");
274         wd.doNextClick ();
275         try {
276             wd.getInstantiatedObjects ();
277         } catch (IllegalStateException JavaDoc ise) {
278             exceptionCaught = true;
279         }
280         if (!exceptionCaught) {
281             fail ("Call getInstantiatedObjects() only on finished wizard, not on next.");
282             exceptionCaught = false;
283         }
284         
285
286         log ("Do click Cancel button.");
287         wd.doFinishClick ();
288         try {
289             wd.getInstantiatedObjects ();
290         } catch (IllegalStateException JavaDoc ise) {
291             fail ("Called getInstantiatedObjects() on finished wizard.");
292         }
293     }
294     
295     public void testGetInstantiatedObjectsWhenCanceled () {
296         boolean exceptionCaught = false;
297         try {
298             wd.getInstantiatedObjects ();
299         } catch (IllegalStateException JavaDoc ise) {
300             exceptionCaught = true;
301         }
302         if (!exceptionCaught) {
303             fail ("Call getInstantiatedObjects() only on finished wizard, not on start.");
304             exceptionCaught = false;
305         }
306         
307         log ("Do click Next button.");
308         wd.doNextClick ();
309         try {
310             wd.getInstantiatedObjects ();
311         } catch (IllegalStateException JavaDoc ise) {
312             exceptionCaught = true;
313         }
314         if (!exceptionCaught) {
315             fail ("Call getInstantiatedObjects() only on finished wizard, not on next.");
316             exceptionCaught = false;
317         }
318         
319
320         log ("Do click Cancel button.");
321         wd.doCancelClick ();
322         try {
323             wd.getInstantiatedObjects ();
324         } catch (IllegalStateException JavaDoc ise) {
325             exceptionCaught = true;
326         }
327         if (!exceptionCaught) {
328             fail ("Call getInstantiatedObjects() only on finished wizard, not when wizard canceled.");
329             exceptionCaught = false;
330         }
331     }
332     
333     public class Panel implements WizardDescriptor.Panel, WizardDescriptor.FinishPanel {
334         private JLabel JavaDoc component;
335         private String JavaDoc text;
336         public Panel(String JavaDoc text) {
337             this.text = text;
338         }
339         
340         public Component JavaDoc getComponent() {
341             if (component == null) {
342                 component = new JLabel JavaDoc (text);
343             }
344             return component;
345         }
346         
347         public void addChangeListener(ChangeListener JavaDoc l) {
348         }
349         
350         public HelpCtx getHelp() {
351             return null;
352         }
353         
354         public boolean isValid() {
355             return true;
356         }
357         
358         public void readSettings(Object JavaDoc settings) {
359             log ("readSettings of panel: " + text + " [time: " + System.currentTimeMillis () +
360                     "] with PROP_VALUE: " + handleValue (wd.getValue ()));
361         }
362         
363         public void removeChangeListener(ChangeListener JavaDoc l) {
364         }
365         
366         public void storeSettings(Object JavaDoc settings) {
367             log ("storeSettings of panel: " + text + " [time: " + System.currentTimeMillis () +
368                     "] with PROP_VALUE: " + handleValue (wd.getValue ()));
369             if (exceptedValue != null) {
370                 assertEquals ("WD.getValue() returns excepted value.", exceptedValue, handleValue (wd.getValue ()));
371             }
372         }
373         
374     }
375     
376     public class Listener implements java.beans.PropertyChangeListener JavaDoc {
377         
378         public void propertyChange(java.beans.PropertyChangeEvent JavaDoc propertyChangeEvent) {
379             if (WizardDescriptor.PROP_VALUE.equals(propertyChangeEvent.getPropertyName ())) {
380                 log("propertyChange [time: " + System.currentTimeMillis () +
381                                     "] with PROP_VALUE: " + handleValue (wd.getValue ()));
382
383             }
384         }
385         
386     }
387     
388     public String JavaDoc handleValue (Object JavaDoc val) {
389         if (val == null) return "NULL";
390         if (val instanceof String JavaDoc) return (String JavaDoc) val;
391         if (WizardDescriptor.FINISH_OPTION.equals (val)) return "FINISH_OPTION";
392         if (WizardDescriptor.CANCEL_OPTION.equals (val)) return "CANCEL_OPTION";
393         if (WizardDescriptor.CLOSED_OPTION.equals (val)) return "CLOSED_OPTION";
394         if (val instanceof JButton) {
395             JButton butt = (JButton) val;
396             ResourceBundle b = NbBundle.getBundle ("org.openide.Bundle"); // NOI18N
397
if (b.getString ("CTL_NEXT").equals (butt.getText ())) return "NEXT_OPTION";
398             if (b.getString ("CTL_PREVIOUS").equals (butt.getText ())) return "NEXT_PREVIOUS";
399             if (b.getString ("CTL_FINISH").equals (butt.getText ())) return "FINISH_OPTION";
400             if (b.getString ("CTL_CANCEL").equals (butt.getText ())) return "CANCEL_OPTION";
401         }
402         return "UNKNOWN OPTION: " + val;
403     }
404 }
405
Popular Tags