1 19 20 package org.openide.loaders; 21 22 import java.awt.Component ; 23 import java.io.IOException ; 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.HashSet ; 27 import java.util.LinkedHashSet ; 28 import java.util.Set ; 29 import javax.swing.JPanel ; 30 import javax.swing.event.ChangeListener ; 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 41 public class TemplateWizardTest extends NbTestCase { 42 43 public TemplateWizardTest (String name) { 44 super(name); 45 } 46 47 protected void setUp() throws Exception { 48 FileObject fo = Repository.getDefault ().getDefaultFileSystem ().getRoot (); 49 FileUtil.createFolder (fo, "Templates"); 50 } 51 52 54 public void testGetIteratorHonoursDataObjectsCookies () throws Exception { 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 { 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 instantiate() throws IOException { 79 return new LinkedHashSet (Arrays.asList(new FileObject[] { 80 d, 81 c, 82 a, 83 b, 84 })); 85 } 86 public void removeChangeListener(ChangeListener l) {} 87 public void addChangeListener(ChangeListener l) {} 88 public void uninitialize(WizardDescriptor wizard) {} 89 public void initialize(WizardDescriptor wizard) {} 90 public void previousPanel() {} 91 public void nextPanel() {} 92 public String 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 (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 (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 122 public void addChangeListener(ChangeListener 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 instantiate(TemplateWizard wiz) throws IOException { 136 throw new IOException (); 137 } 138 public String name() { 139 return ""; 140 } 141 public void nextPanel() { 142 } 143 public void previousPanel() { 144 } 145 public void removeChangeListener(ChangeListener l) { 146 } 147 public void uninitialize(TemplateWizard wiz) { 148 } 149 } 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 { 164 return new DO (fo); 165 } 166 } 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 <DataObject> instantiate (TemplateWizard wiz) throws IOException { 187 throw new IOException (); 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 l) { 209 } 210 211 public void addChangeListener (ChangeListener l) { 212 } 213 214 public void storeSettings (Object settings) { 215 } 216 217 public void readSettings (Object settings) { 218 } 219 220 public boolean isValid () { 221 return true; 222 } 223 224 public HelpCtx getHelp () { 225 return null; 226 } 227 228 public Component getComponent () { 229 return new JPanel (); 230 } 231 232 public String toString () { 233 return Integer.toString (index); 234 } 235 236 } 237 238 } 239 240 241 | Popular Tags |