KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > WizardDescriptorOrderTest


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 the order of calling the WizardDescriptor support methods and
38  * the methods in implemented wizard's interfaces.
39  * @see issue 46587, issue 46589
40  * @author Jiri Rechtacek, Jesse Glick
41  *
42  */

43 public class WizardDescriptorOrderTest extends NbTestCase {
44
45     
46     public WizardDescriptorOrderTest (String JavaDoc name) {
47         super(name);
48     }
49     
50     public static void main(String JavaDoc[] args) {
51         junit.textui.TestRunner.run (new NbTestSuite (WizardDescTest.class));
52         System.exit (0);
53     }
54     
55     WizardDescriptor wd;
56     TestPanel[] panels;
57     
58     // help fields to control the order of calls to panel
59
int[] readSettingsCalls;
60     int[] storeSettingsCalls;
61     
62     // help variables
63
boolean checkStoreBeforeNext;
64     boolean checkStoreOldeBeforeReadNew;
65     boolean checkPCH2FinishOption;
66
67     protected final void setUp () {
68         panels = new TestPanel[] {new TestPanel (0), new TestPanel (1)};
69         readSettingsCalls = new int[] {0, 0};
70         storeSettingsCalls = new int[] {0, 0};
71         wd = new WizardDescriptor (new TestIterator (panels[0], panels[1]));
72         wd.addPropertyChangeListener(new Listener JavaDoc());
73         java.awt.Dialog JavaDoc d = DialogDisplayer.getDefault().createDialog (wd);
74         //d.show();
75

76         checkStoreBeforeNext = false;
77         checkStoreOldeBeforeReadNew = false;
78         checkPCH2FinishOption = false;
79     }
80     
81     public void testReadSettingsOnFirstPanel () throws Exception JavaDoc {
82         log ("Wizard has been initialized.");
83         assertTrue ("WD.P.readSettings on the first panel has been called.", readSettingsCalls[0] > 0);
84     }
85
86     public void testOrderNextPanelAndStoreSettings () throws Exception JavaDoc {
87         checkStoreBeforeNext = true;
88         log ("Do click Next button.");
89         wd.doNextClick ();
90     }
91
92     public void testStoreOldBeforeReadNew () throws Exception JavaDoc {
93         checkStoreOldeBeforeReadNew = true;
94         log ("Do click Next button.");
95         wd.doNextClick ();
96     }
97
98     public void testStoreLastBeforePCH2Finish () throws Exception JavaDoc {
99         log ("Do click Next button.");
100         wd.doNextClick ();
101         checkPCH2FinishOption = true;
102         log ("Do click Finish button.");
103         wd.doFinishClick ();
104     }
105
106     public class Listener implements java.beans.PropertyChangeListener JavaDoc {
107         
108         public void propertyChange(java.beans.PropertyChangeEvent JavaDoc propertyChangeEvent) {
109             if (WizardDescriptor.PROP_VALUE.equals(propertyChangeEvent.getPropertyName ())) {
110                 log("propertyChange [time: " + System.currentTimeMillis () +
111                                     "] with PROP_VALUE: " + handleValue (wd.getValue ()));
112                 
113                 if (checkPCH2FinishOption && WizardDescriptor.FINISH_OPTION.equals (propertyChangeEvent.getNewValue ())) {
114                     assertTrue ("WD.P.storeSettings on the last panel has been called before propertyChangeEvent to FINISH_OPTION was fired.", storeSettingsCalls[1] > 0);
115                 }
116             }
117         }
118         
119     }
120     
121     private final class TestPanel implements WizardDescriptor.Panel {
122         
123         private final int x;
124         
125         public TestPanel(int x) {
126             this.x = x;
127         }
128         
129         public void readSettings(Object JavaDoc settings) {
130             readSettingsCalls[x]++;
131             log ("readSettings of panel: " + x + " [time: " + System.currentTimeMillis () +
132                     "] with PROP_VALUE: " + handleValue (wd.getValue ()));
133         }
134         
135         public void addChangeListener(ChangeListener JavaDoc l) {}
136         
137         public void storeSettings(Object JavaDoc settings) {
138             if (checkStoreOldeBeforeReadNew && (x > 0)) {
139                 assertTrue ("WD.P.storeSettings on the previous panel has been called before WD.P.readSettings on next panel.", readSettingsCalls[x - 1] > 0);
140             }
141             storeSettingsCalls[x] ++;
142             log ("storeSettings of panel: " + x + " [time: " + System.currentTimeMillis () +
143                     "] with PROP_VALUE: " + handleValue (wd.getValue ()));
144         }
145             
146         public void removeChangeListener(ChangeListener JavaDoc l) {}
147         
148         public boolean isValid() {
149             return true;
150         }
151         public HelpCtx getHelp() {
152             return null;
153         }
154         
155         public Component JavaDoc getComponent() {
156             return new JLabel JavaDoc("panel #" + x);
157         }
158     }
159     
160     private final class TestIterator implements WizardDescriptor.Iterator {
161         private final TestPanel panel1, panel2;
162         private int which = 0;
163         public TestIterator(TestPanel panel1, TestPanel panel2) {
164             this.panel1 = panel1;
165             this.panel2 = panel2;
166         }
167         public boolean hasNext () {
168             return which == 0;
169         }
170         public WizardDescriptor.Panel current() {
171             log ("current: " + name());
172             TestPanel currentPanel = which == 0 ? panel1 : panel2;
173             return currentPanel;
174         }
175         public void nextPanel() {
176             if (checkStoreBeforeNext) {
177                 assertTrue ("WD.P.storeSettings on the previous panel has been called before WD.I.nextPanel.", storeSettingsCalls[which] > 0);
178             }
179             which ++;
180         }
181         public void removeChangeListener(ChangeListener JavaDoc l) {}
182         public boolean hasPrevious() {
183             return which > 0;
184         }
185         public void previousPanel() {
186             which --;;
187         }
188         public String JavaDoc name() {
189             return which == 0 ? "First Panel" : "Second Panel";
190         }
191         public void addChangeListener(ChangeListener JavaDoc l) {}
192     }
193     
194     public String JavaDoc handleValue (Object JavaDoc val) {
195         if (val == null) return "NULL";
196         if (val instanceof String JavaDoc) return (String JavaDoc) val;
197         if (WizardDescriptor.FINISH_OPTION.equals (val)) return "FINISH_OPTION";
198         if (WizardDescriptor.CANCEL_OPTION.equals (val)) return "CANCEL_OPTION";
199         if (WizardDescriptor.CLOSED_OPTION.equals (val)) return "CLOSED_OPTION";
200         if (val instanceof JButton) {
201             JButton butt = (JButton) val;
202             ResourceBundle b = NbBundle.getBundle ("org.openide.Bundle"); // NOI18N
203
if (b.getString ("CTL_NEXT").equals (butt.getText ())) return "NEXT_OPTION";
204             if (b.getString ("CTL_PREVIOUS").equals (butt.getText ())) return "NEXT_PREVIOUS";
205             if (b.getString ("CTL_FINISH").equals (butt.getText ())) return "FINISH_OPTION";
206             if (b.getString ("CTL_CANCEL").equals (butt.getText ())) return "CANCEL_OPTION";
207         }
208         return "UNKNOWN OPTION: " + val;
209     }
210     
211 }
212
Popular Tags