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:
042: package org.openide.loaders;
043:
044: import java.awt.Component;
045: import java.io.IOException;
046: import java.util.ArrayList;
047: import java.util.Arrays;
048: import java.util.HashSet;
049: import java.util.LinkedHashSet;
050: import java.util.Set;
051: import javax.swing.JPanel;
052: import javax.swing.event.ChangeListener;
053: import org.netbeans.junit.NbTestCase;
054: import org.openide.WizardDescriptor;
055: import org.openide.filesystems.FileObject;
056: import org.openide.filesystems.FileSystem;
057: import org.openide.filesystems.FileUtil;
058: import org.openide.filesystems.Repository;
059: import org.openide.util.HelpCtx;
060:
061: /** Checks the testable behaviour of TemplateWizard
062: * @author Jaroslav Tulach, Jiri Rechtacek
063: */
064: public class TemplateWizardTest extends NbTestCase {
065:
066: public TemplateWizardTest(String name) {
067: super (name);
068: }
069:
070: protected void setUp() throws Exception {
071: FileObject fo = Repository.getDefault().getDefaultFileSystem()
072: .getRoot();
073: FileUtil.createFolder(fo, "Templates");
074: }
075:
076: /** Does getIterator honours DataObject's cookies?
077: */
078: public void testGetIteratorHonoursDataObjectsCookies()
079: throws Exception {
080: FileSystem fs = FileUtil.createMemoryFileSystem();
081: DataObject obj;
082: Loader l = Loader.findObject(Loader.class, true);
083: try {
084: AddLoaderManuallyHid.addRemoveLoader(l, true);
085: obj = DataObject.find(fs.getRoot());
086: } finally {
087: AddLoaderManuallyHid.addRemoveLoader(l, false);
088: }
089:
090: TemplateWizard.Iterator it = TemplateWizard.getIterator(obj);
091:
092: assertEquals("Iterator obtained from the object's cookie", obj,
093: it);
094: }
095:
096: public void testIteratorBridge() throws Exception {
097: FileSystem fs = FileUtil.createMemoryFileSystem();
098: FileObject fo = fs.getRoot().createData("x");
099: final FileObject a = fs.getRoot().createData("a");
100: final FileObject b = fs.getRoot().createData("b");
101: final FileObject c = fs.getRoot().createData("c");
102: final FileObject d = fs.getRoot().createData("d");
103: fo.setAttribute("instantiatingIterator",
104: new WizardDescriptor.InstantiatingIterator() {
105: public Set instantiate() throws IOException {
106: return new LinkedHashSet(
107: Arrays.asList(new FileObject[] { d, c,
108: a, b, }));
109: }
110:
111: public void removeChangeListener(ChangeListener l) {
112: }
113:
114: public void addChangeListener(ChangeListener l) {
115: }
116:
117: public void uninitialize(WizardDescriptor wizard) {
118: }
119:
120: public void initialize(WizardDescriptor wizard) {
121: }
122:
123: public void previousPanel() {
124: }
125:
126: public void nextPanel() {
127: }
128:
129: public String name() {
130: return null;
131: }
132:
133: public boolean hasPrevious() {
134: return false;
135: }
136:
137: public boolean hasNext() {
138: return false;
139: }
140:
141: public WizardDescriptor.Panel current() {
142: return null;
143: }
144: });
145: System.out.println("natural order:"
146: + new HashSet(Arrays.asList(new DataObject[] {
147: DataObject.find(d), DataObject.find(c),
148: DataObject.find(a), DataObject.find(b), })));
149: assertEquals("order preserved (#64760)", Arrays
150: .asList(new DataObject[] { DataObject.find(d),
151: DataObject.find(c), DataObject.find(a),
152: DataObject.find(b), }), new ArrayList(
153: TemplateWizard.getIterator(DataObject.find(fo))
154: .instantiate(new TemplateWizard())));
155: }
156:
157: private static class DO extends DataFolder implements
158: TemplateWizard.Iterator {
159: public DO(FileObject fo) throws DataObjectExistsException {
160: super (fo);
161:
162: getCookieSet().add(this );
163: }
164:
165: //
166: // Dummy implementation of wizard iterator
167: //
168:
169: public void addChangeListener(ChangeListener l) {
170: }
171:
172: public WizardDescriptor.Panel<WizardDescriptor> current() {
173: return null;
174: }
175:
176: public boolean hasNext() {
177: return false;
178: }
179:
180: public boolean hasPrevious() {
181: return false;
182: }
183:
184: public void initialize(TemplateWizard wiz) {
185: }
186:
187: public Set instantiate(TemplateWizard wiz) throws IOException {
188: throw new IOException();
189: }
190:
191: public String name() {
192: return "";
193: }
194:
195: public void nextPanel() {
196: }
197:
198: public void previousPanel() {
199: }
200:
201: public void removeChangeListener(ChangeListener l) {
202: }
203:
204: public void uninitialize(TemplateWizard wiz) {
205: }
206: } // end of DO
207:
208: private static class Loader extends UniFileLoader {
209: public Loader() {
210: super (DO.class.getName());
211: }
212:
213: protected FileObject findPrimaryFile(FileObject fo) {
214: if (fo.isFolder()) {
215: return fo;
216: } else {
217: return null;
218: }
219: }
220:
221: protected MultiDataObject createMultiObject(FileObject fo)
222: throws IOException {
223: return new DO(fo);
224: }
225: } // end of Loader
226:
227: public void testNextOnIterImpl() {
228: doNextOnIterImpl(false);
229: }
230:
231: public void testNextOnIterImplWithNotification() {
232: doNextOnIterImpl(true);
233: }
234:
235: private void doNextOnIterImpl(boolean notify) {
236: TemplateWizard wizard = new TemplateWizard();
237: wizard.initialize();
238: TemplateWizardIterImpl iter = wizard.getIterImpl();
239: assertEquals("IterImpl returns template chooser.", wizard
240: .templateChooser(), iter.current());
241: final WizardDescriptor.Panel[] arr = { new P(1), new P(2) };
242: class I extends
243: WizardDescriptor.ArrayIterator<WizardDescriptor>
244: implements TemplateWizard.Iterator {
245: public I() {
246: super (arr);
247: }
248:
249: public Set<DataObject> instantiate(TemplateWizard wiz)
250: throws IOException {
251: throw new IOException();
252: }
253:
254: public void initialize(TemplateWizard wiz) {
255: }
256:
257: public void uninitialize(TemplateWizard wiz) {
258: }
259: }
260:
261: I newIter = new I();
262: WizardDescriptor.Panel oldPanel = iter.current();
263: iter.setIterator(newIter, notify);
264: iter.nextPanel();
265: assertEquals(
266: "IterImpl returns the first panel of newly delegated iterator, ",
267: arr[0], iter.current());
268: iter.previousPanel();
269: assertEquals(
270: "IterImpl returns the first panel of old iterator on previous, ",
271: oldPanel, iter.current());
272: }
273:
274: public static class P implements WizardDescriptor.Panel {
275: int index;
276:
277: public P(int i) {
278: index = i;
279: }
280:
281: public void removeChangeListener(ChangeListener l) {
282: }
283:
284: public void addChangeListener(ChangeListener l) {
285: }
286:
287: public void storeSettings(Object settings) {
288: }
289:
290: public void readSettings(Object settings) {
291: }
292:
293: public boolean isValid() {
294: return true;
295: }
296:
297: public HelpCtx getHelp() {
298: return null;
299: }
300:
301: public Component getComponent() {
302: return new JPanel();
303: }
304:
305: public String toString() {
306: return Integer.toString(index);
307: }
308:
309: }
310:
311: }
|