KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > platform > wizard > PlatformInstallIteratorTest


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
20 package org.netbeans.modules.java.platform.wizard;
21
22 import java.awt.Component JavaDoc;
23 import javax.swing.JFileChooser JavaDoc;
24 import javax.swing.JPanel JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.Set JavaDoc;
28 import javax.swing.event.ChangeListener JavaDoc;
29 import org.netbeans.junit.NbTestCase;
30 import org.netbeans.modules.java.platform.InstallerRegistry;
31 import org.netbeans.modules.java.platform.InstallerRegistryAccessor;
32 import org.netbeans.spi.java.platform.CustomPlatformInstall;
33 import org.netbeans.spi.java.platform.GeneralPlatformInstall;
34 import org.netbeans.spi.java.platform.PlatformInstall;
35 import org.openide.filesystems.FileObject;
36 import org.openide.util.HelpCtx;
37 import org.openide.WizardDescriptor;
38
39 /**
40  *
41  * @author Tomas Zezula
42  */

43 public class PlatformInstallIteratorTest extends NbTestCase {
44     
45     public PlatformInstallIteratorTest(String JavaDoc testName) {
46         super(testName);
47     }
48
49     public void testSinglePlatformInstall () throws IOException JavaDoc {
50         InstallerRegistry regs = InstallerRegistryAccessor.prepareForUnitTest(new GeneralPlatformInstall[] {
51             new FileBasedPlatformInstall ("FileBased1", new WizardDescriptor.Panel[] {
52                 new Panel JavaDoc ("FileBased1_panel1")
53             })
54         });
55         PlatformInstallIterator iterator = PlatformInstallIterator.create();
56         WizardDescriptor wd = new WizardDescriptor (iterator);
57         iterator.initialize(wd);
58         assertEquals("Invalid state", 1, iterator.getPanelIndex());
59         WizardDescriptor.Panel panel = iterator.current();
60         assertTrue ("Invalid panel",panel instanceof LocationChooser.Panel);
61         ((JFileChooser JavaDoc)panel.getComponent()).setSelectedFile(this.getWorkDir()); //Select some folder
62
assertTrue ("LocationChooser is not valid after folder was selected",panel.isValid());
63         assertTrue ("Should have next panel",iterator.hasNext());
64         assertFalse ("Should not have previous panel", iterator.hasPrevious());
65         iterator.nextPanel();
66         assertEquals("Invalid state", 2, iterator.getPanelIndex());
67         panel = iterator.current();
68         assertEquals("Invalid panel","FileBased1_panel1",panel.getComponent().getName());
69         assertFalse ("Should not have next panel",iterator.hasNext());
70         assertTrue ("Should have previous panel", iterator.hasPrevious());
71     }
72     
73     public void testSingleCustomInstall () throws IOException JavaDoc {
74         InstallerRegistry regs = InstallerRegistryAccessor.prepareForUnitTest(new GeneralPlatformInstall[] {
75             new OtherPlatformInstall ("Custom1", new WizardDescriptor.Panel[] {
76                 new Panel JavaDoc ("Custom1_panel1")
77             })
78         });
79         PlatformInstallIterator iterator = PlatformInstallIterator.create();
80         WizardDescriptor wd = new WizardDescriptor (iterator);
81         iterator.initialize(wd);
82         assertEquals("Invalid state", 3, iterator.getPanelIndex());
83         WizardDescriptor.Panel panel = iterator.current();
84         assertEquals("Invalid panel","Custom1_panel1",panel.getComponent().getName());
85         assertFalse ("Should not have next panel",iterator.hasNext());
86         assertFalse ("Should not have previous panel", iterator.hasPrevious());
87     }
88     
89     public void testMultipleGenralPlatformInstalls () throws IOException JavaDoc {
90         GeneralPlatformInstall[] installers = new GeneralPlatformInstall[] {
91             new FileBasedPlatformInstall ("FileBased1", new WizardDescriptor.Panel[] {
92                 new Panel JavaDoc ("FileBased1_panel1")
93             }),
94             new OtherPlatformInstall ("Custom1", new WizardDescriptor.Panel[] {
95                 new Panel JavaDoc ("Custom1_panel1")
96             })
97         };
98         InstallerRegistry regs = InstallerRegistryAccessor.prepareForUnitTest (installers);
99         PlatformInstallIterator iterator = PlatformInstallIterator.create();
100         WizardDescriptor wd = new WizardDescriptor (iterator);
101         iterator.initialize(wd);
102         assertEquals("Invalid state", 0, iterator.getPanelIndex());
103         WizardDescriptor.Panel panel = iterator.current();
104         assertTrue ("Invalid panel",panel instanceof SelectorPanel.Panel);
105         assertTrue ("Installer was not found",((SelectorPanel)panel.getComponent()).selectInstaller(installers[0]));
106         assertTrue ("SelectorPanel should be valid",panel.isValid());
107         assertTrue ("Should have next panel",iterator.hasNext());
108         assertFalse ("Should not have previous panel", iterator.hasPrevious());
109         iterator.nextPanel ();
110         assertEquals("Invalid state", 1, iterator.getPanelIndex());
111         panel = iterator.current();
112         assertTrue ("Invalid panel",panel instanceof LocationChooser.Panel);
113         assertTrue ("Should have previous panel", iterator.hasPrevious());
114         iterator.previousPanel();
115         assertEquals("Invalid state", 0, iterator.getPanelIndex());
116         panel = iterator.current();
117         assertTrue ("Invalid panel",panel instanceof SelectorPanel.Panel);
118         assertTrue ("Installer was not found",((SelectorPanel)panel.getComponent()).selectInstaller(installers[1]));
119         assertTrue ("SelectorPanel should be valid",panel.isValid());
120         assertTrue ("Should have next panel",iterator.hasNext());
121         assertFalse ("Should not have previous panel", iterator.hasPrevious());
122         iterator.nextPanel ();
123         assertEquals("Invalid state", 3, iterator.getPanelIndex());
124         panel = iterator.current();
125         assertEquals("Invalid panel","Custom1_panel1",panel.getComponent().getName());
126         assertFalse ("Should not have next panel",iterator.hasNext());
127         assertTrue ("Should have previous panel", iterator.hasPrevious());
128     }
129     
130     public void testMultipleFileBasedPlatformInstalls () throws IOException JavaDoc {
131         GeneralPlatformInstall[] installers = new GeneralPlatformInstall[] {
132             new FileBasedPlatformInstall ("FileBased1", new WizardDescriptor.Panel[] {
133                 new Panel JavaDoc ("FileBased1_panel1")
134             }),
135             new FileBasedPlatformInstall ("FileBased2", new WizardDescriptor.Panel[] {
136                 new Panel JavaDoc ("FileBased2_panel2")
137             }),
138         };
139         InstallerRegistry regs = InstallerRegistryAccessor.prepareForUnitTest (installers);
140         PlatformInstallIterator iterator = PlatformInstallIterator.create();
141         WizardDescriptor wd = new WizardDescriptor (iterator);
142         iterator.initialize(wd);
143         assertEquals("Invalid state", 0, iterator.getPanelIndex());
144         WizardDescriptor.Panel panel = iterator.current();
145         assertTrue ("Invalid panel",panel instanceof SelectorPanel.Panel);
146         assertTrue ("Installer was not found",((SelectorPanel)panel.getComponent()).selectInstaller(installers[0]));
147         iterator.nextPanel ();
148         assertEquals("Invalid state", 1, iterator.getPanelIndex());
149         panel = iterator.current();
150         assertTrue ("Invalid panel",panel instanceof LocationChooser.Panel);
151         PlatformInstall platformInstall = ((LocationChooser.Panel)panel).getPlatformInstall();
152         assertEquals ("Invalid PlatformInstall",installers[0],platformInstall);
153         iterator.previousPanel();
154         assertEquals("Invalid state", 0, iterator.getPanelIndex());
155         panel = iterator.current();
156         assertTrue ("Invalid panel",panel instanceof SelectorPanel.Panel);
157         assertTrue ("Installer was not found",((SelectorPanel)panel.getComponent()).selectInstaller(installers[1]));
158         iterator.nextPanel ();
159         assertEquals("Invalid state", 1, iterator.getPanelIndex());
160         panel = iterator.current();
161         assertTrue ("Invalid panel",panel instanceof LocationChooser.Panel);
162         platformInstall = ((LocationChooser.Panel)panel).getPlatformInstall();
163         assertEquals ("Invalid PlatformInstall",installers[1],platformInstall);
164     }
165     
166     public void testIteratorWithMorePanels () throws IOException JavaDoc {
167         InstallerRegistry regs = InstallerRegistryAccessor.prepareForUnitTest(new GeneralPlatformInstall[] {
168             new OtherPlatformInstall ("Custom1", new WizardDescriptor.Panel[] {
169                 new Panel JavaDoc ("Custom1_panel1"),
170                 new Panel JavaDoc ("Custom1_panel2"),
171             })
172         });
173         PlatformInstallIterator iterator = PlatformInstallIterator.create();
174         WizardDescriptor wd = new WizardDescriptor (iterator);
175         iterator.initialize(wd);
176         assertEquals("Invalid state", 3, iterator.getPanelIndex());
177         WizardDescriptor.Panel panel = iterator.current();
178         assertEquals("Invalid panel","Custom1_panel1",panel.getComponent().getName());
179         assertTrue ("Should have next panel",iterator.hasNext());
180         assertFalse ("Should not have previous panel", iterator.hasPrevious());
181         iterator.nextPanel();
182         panel = iterator.current();
183         assertEquals("Invalid panel","Custom1_panel2",panel.getComponent().getName());
184         assertFalse ("Should not have next panel",iterator.hasNext());
185         assertTrue ("Should have previous panel", iterator.hasPrevious());
186     }
187     
188     private static class FileBasedPlatformInstall extends PlatformInstall {
189         
190         private String JavaDoc name;
191         private WizardDescriptor.InstantiatingIterator iterator;
192         
193         public FileBasedPlatformInstall (String JavaDoc name, WizardDescriptor.Panel[] panels) {
194             this.name = name;
195             this.iterator = new Iterator (panels);
196         }
197
198         public WizardDescriptor.InstantiatingIterator createIterator(FileObject baseFolder) {
199             return this.iterator;
200         }
201
202         public boolean accept(FileObject baseFolder) {
203             return true;
204         }
205
206         public String JavaDoc getDisplayName() {
207             return this.name;
208         }
209     }
210     
211     private static class OtherPlatformInstall extends CustomPlatformInstall {
212         
213         private String JavaDoc name;
214         private WizardDescriptor.InstantiatingIterator iterator;
215         
216         public OtherPlatformInstall (String JavaDoc name, WizardDescriptor.Panel[] panels) {
217             this.name = name;
218             this.iterator = new Iterator (panels);
219         }
220
221         public String JavaDoc getDisplayName() {
222             return this.name;
223         }
224
225         public WizardDescriptor.InstantiatingIterator createIterator () {
226             return this.iterator;
227         }
228                         
229     }
230     
231     private static class Iterator implements WizardDescriptor.InstantiatingIterator {
232         
233         private WizardDescriptor.Panel[] panels;
234         private int index;
235         
236         public Iterator (WizardDescriptor.Panel[] panels) {
237             this.panels = panels;
238         }
239         
240         public void removeChangeListener(ChangeListener JavaDoc l) {
241         }
242
243         public void addChangeListener(ChangeListener JavaDoc l) {
244         }
245
246         public void uninitialize(WizardDescriptor wizard) {
247         }
248
249         public void initialize(WizardDescriptor wizard) {
250             this.index = 0;
251         }
252
253         public void previousPanel() {
254             this.index--;
255         }
256
257         public void nextPanel() {
258             this.index++;
259         }
260
261         public String JavaDoc name() {
262             return "Test"; //NOI18N
263
}
264
265         public Set JavaDoc instantiate() throws IOException JavaDoc {
266             return Collections.EMPTY_SET;
267         }
268
269         public boolean hasPrevious() {
270             return this.index > 0;
271         }
272
273         public boolean hasNext() {
274             return this.index < (this.panels.length - 1);
275         }
276
277         public WizardDescriptor.Panel current() {
278             return this.panels[this.index];
279         }
280         
281     }
282     
283     private static class Panel implements WizardDescriptor.Panel {
284         
285         private JPanel JavaDoc p;
286         private String JavaDoc name;
287         
288         public Panel (String JavaDoc name) {
289             this.name = name;
290         }
291         
292         public void removeChangeListener(ChangeListener JavaDoc l) {
293         }
294
295         public void addChangeListener(ChangeListener JavaDoc l) {
296         }
297
298         public void storeSettings(Object JavaDoc settings) {
299         }
300
301         public void readSettings(Object JavaDoc settings) {
302         }
303
304         public boolean isValid() {
305             return true;
306         }
307
308         public HelpCtx getHelp() {
309             return HelpCtx.DEFAULT_HELP;
310         }
311
312         public Component JavaDoc getComponent() {
313             if (this.p == null) {
314                 p = new JPanel JavaDoc ();
315                 p.setName(this.name);
316             }
317             return p;
318         }
319         
320     }
321     
322 }
323
Popular Tags