KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > loaders > TemplateWizardTest


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.openide.loaders;
21
22 import java.awt.Component JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.LinkedHashSet JavaDoc;
28 import java.util.Set JavaDoc;
29 import javax.swing.JPanel JavaDoc;
30 import javax.swing.event.ChangeListener JavaDoc;
31 import org.netbeans.junit.NbTestCase;
32 import org.openide.WizardDescriptor;
33 import org.openide.filesystems.FileObject;
34 import org.openide.filesystems.FileSystem;
35 import org.openide.filesystems.FileUtil;
36 import org.openide.filesystems.Repository;
37 import org.openide.util.HelpCtx;
38 /** Checks the testable behaviour of TemplateWizard
39  * @author Jaroslav Tulach, Jiri Rechtacek
40  */

41 public class TemplateWizardTest extends NbTestCase {
42     
43     public TemplateWizardTest (String JavaDoc name) {
44         super(name);
45     }
46     
47     protected void setUp() throws Exception JavaDoc {
48          FileObject fo = Repository.getDefault ().getDefaultFileSystem ().getRoot ();
49          FileUtil.createFolder (fo, "Templates");
50     }
51
52     /** Does getIterator honours DataObject's cookies?
53      */

54     public void testGetIteratorHonoursDataObjectsCookies () throws Exception JavaDoc {
55         FileSystem fs = FileUtil.createMemoryFileSystem();
56         DataObject obj;
57         Loader l = Loader.findObject (Loader.class, true);
58         try {
59             AddLoaderManuallyHid.addRemoveLoader (l, true);
60             obj = DataObject.find (fs.getRoot ());
61         } finally {
62             AddLoaderManuallyHid.addRemoveLoader (l, false);
63         }
64         
65         TemplateWizard.Iterator it = TemplateWizard.getIterator (obj);
66         
67         assertEquals ("Iterator obtained from the object's cookie", obj, it);
68     }
69     
70     public void testIteratorBridge() throws Exception JavaDoc {
71         FileSystem fs = FileUtil.createMemoryFileSystem();
72         FileObject fo = fs.getRoot().createData("x");
73         final FileObject a = fs.getRoot().createData("a");
74         final FileObject b = fs.getRoot().createData("b");
75         final FileObject c = fs.getRoot().createData("c");
76         final FileObject d = fs.getRoot().createData("d");
77         fo.setAttribute("instantiatingIterator", new WizardDescriptor.InstantiatingIterator() {
78             public Set JavaDoc instantiate() throws IOException JavaDoc {
79                 return new LinkedHashSet JavaDoc(Arrays.asList(new FileObject[] {
80                     d,
81                     c,
82                     a,
83                     b,
84                 }));
85             }
86             public void removeChangeListener(ChangeListener JavaDoc l) {}
87             public void addChangeListener(ChangeListener JavaDoc l) {}
88             public void uninitialize(WizardDescriptor wizard) {}
89             public void initialize(WizardDescriptor wizard) {}
90             public void previousPanel() {}
91             public void nextPanel() {}
92             public String JavaDoc name() {return null;}
93             public boolean hasPrevious() {return false;}
94             public boolean hasNext() {return false;}
95             public WizardDescriptor.Panel current() {return null;}
96         });
97         System.out.println("natural order:" + new HashSet JavaDoc(Arrays.asList(new DataObject[] {
98             DataObject.find(d),
99             DataObject.find(c),
100             DataObject.find(a),
101             DataObject.find(b),
102         })));
103         assertEquals("order preserved (#64760)", Arrays.asList(new DataObject[] {
104             DataObject.find(d),
105             DataObject.find(c),
106             DataObject.find(a),
107             DataObject.find(b),
108         }), new ArrayList JavaDoc(TemplateWizard.getIterator(DataObject.find(fo)).instantiate(new TemplateWizard())));
109     }
110     
111     private static class DO extends DataFolder implements TemplateWizard.Iterator {
112         public DO (FileObject fo) throws DataObjectExistsException {
113             super (fo);
114
115             getCookieSet ().add (this);
116         }
117
118         //
119
// Dummy implementation of wizard iterator
120
//
121

122         public void addChangeListener(ChangeListener JavaDoc l) {
123         }
124         public WizardDescriptor.Panel<WizardDescriptor> current() {
125             return null;
126         }
127         public boolean hasNext() {
128             return false;
129         }
130         public boolean hasPrevious() {
131             return false;
132         }
133         public void initialize(TemplateWizard wiz) {
134         }
135         public Set JavaDoc instantiate(TemplateWizard wiz) throws IOException JavaDoc {
136             throw new IOException JavaDoc ();
137         }
138         public String JavaDoc name() {
139             return "";
140         }
141         public void nextPanel() {
142         }
143         public void previousPanel() {
144         }
145         public void removeChangeListener(ChangeListener JavaDoc l) {
146         }
147         public void uninitialize(TemplateWizard wiz) {
148         }
149     } // end of DO
150
private static class Loader extends UniFileLoader {
151         public Loader () {
152             super (DO.class.getName ());
153         }
154
155         protected FileObject findPrimaryFile (FileObject fo) {
156             if (fo.isFolder ()) {
157                 return fo;
158             } else {
159                 return null;
160             }
161         }
162
163         protected MultiDataObject createMultiObject (FileObject fo) throws IOException JavaDoc {
164             return new DO (fo);
165         }
166     } // end of Loader
167

168     public void testNextOnIterImpl () {
169         doNextOnIterImpl (false);
170     }
171     
172     public void testNextOnIterImplWithNotification () {
173         doNextOnIterImpl (true);
174     }
175     
176     private void doNextOnIterImpl (boolean notify) {
177         TemplateWizard wizard = new TemplateWizard ();
178         wizard.initialize ();
179         TemplateWizardIterImpl iter = wizard.getIterImpl ();
180         assertEquals ("IterImpl returns template chooser.", wizard.templateChooser (), iter.current ());
181         final WizardDescriptor.Panel[] arr = {new P(1), new P(2)};
182         class I extends WizardDescriptor.ArrayIterator<WizardDescriptor> implements TemplateWizard.Iterator {
183             public I () {
184                 super (arr);
185             }
186             public Set JavaDoc<DataObject> instantiate (TemplateWizard wiz) throws IOException JavaDoc {
187                 throw new IOException JavaDoc ();
188             }
189             public void initialize(TemplateWizard wiz) {}
190             public void uninitialize(TemplateWizard wiz) {}
191         }
192         
193         I newIter = new I ();
194         WizardDescriptor.Panel oldPanel = iter.current ();
195         iter.setIterator (newIter, notify);
196         iter.nextPanel ();
197         assertEquals ("IterImpl returns the first panel of newly delegated iterator, ", arr[0], iter.current ());
198         iter.previousPanel ();
199         assertEquals ("IterImpl returns the first panel of old iterator on previous, ", oldPanel, iter.current ());
200     }
201     
202     public static class P implements WizardDescriptor.Panel {
203         int index;
204         public P (int i) {
205             index = i;
206         }
207         
208         public void removeChangeListener (ChangeListener JavaDoc l) {
209         }
210
211         public void addChangeListener (ChangeListener JavaDoc l) {
212         }
213
214         public void storeSettings (Object JavaDoc settings) {
215         }
216
217         public void readSettings (Object JavaDoc settings) {
218         }
219
220         public boolean isValid () {
221             return true;
222         }
223
224         public HelpCtx getHelp () {
225             return null;
226         }
227
228         public Component JavaDoc getComponent () {
229             return new JPanel JavaDoc ();
230         }
231         
232         public String JavaDoc toString () {
233             return Integer.toString (index);
234         }
235         
236     }
237
238 }
239
240
241
Popular Tags