1 20 21 package org.openide.loaders; 22 23 import java.awt.Dialog ; 24 import java.io.IOException ; 25 import java.util.Arrays ; 26 import java.util.Collections ; 27 import java.util.Enumeration ; 28 import java.util.Map ; 29 import java.util.Set ; 30 import javax.swing.JButton ; 31 import javax.swing.JDialog ; 32 import org.netbeans.junit.MockServices; 33 import org.netbeans.junit.NbTestCase; 34 import org.openide.DialogDescriptor; 35 import org.openide.DialogDisplayer; 36 import org.openide.NotifyDescriptor; 37 import org.openide.WizardDescriptor; 38 import org.openide.filesystems.FileObject; 39 import org.openide.filesystems.FileUtil; 40 import org.openide.util.Enumerations; 41 42 46 public class CreateFromTemplateHandlerTest extends NbTestCase { 47 48 public CreateFromTemplateHandlerTest(String testName) { 49 super(testName); 50 } 51 52 protected boolean runInEQ() { 53 return true; 54 } 55 56 protected void setUp() throws Exception { 57 Hand.acceptObject = null; 58 Hand.fileObject = null; 59 Hand.origObject = null; 60 Hand.name = null; 61 Hand.parameters = null; 62 63 MockServices.setServices(Hand.class, Attr.class, DD.class, Pool.class); 64 } 65 66 protected void tearDown() throws Exception { 67 super.tearDown(); 68 } 69 70 public void testCreateFromTemplate() throws Exception { 71 FileObject root = FileUtil.createMemoryFileSystem().getRoot(); 72 FileObject fo = FileUtil.createData(root, "simpleObject.txt"); 73 74 DataObject obj = DataObject.find(fo); 75 76 DataFolder folder = DataFolder.findFolder(FileUtil.createFolder(root, "target")); 77 78 Map <String ,String > parameters = Collections.singletonMap("type", "empty"); 79 DataObject n = obj.createFromTemplate(folder, "complex", parameters); 80 81 assertEquals("Created in right place", folder, n.getFolder()); 82 assertEquals("Created with right name", "complex.txt", n.getName()); 83 84 assertEquals("The right source", fo, Hand.origObject); 85 assertEquals("The right source in query", fo, Hand.acceptObject); 86 assertEquals("The right destiny folder", folder.getPrimaryFile(), Hand.fileObject); 87 assertEquals("The right name", "complex", Hand.name); 88 if (Hand.parameters.size() < 2) { 89 fail("As least two: " + Hand.parameters + " but was " + Hand.parameters.size()); 90 } 91 assertEquals("empty", Hand.parameters.get("type")); 92 assertEquals("complex", Hand.parameters.get("name")); 93 try { 94 Hand.parameters.put("kuk", "buk"); 95 } catch (UnsupportedOperationException ex) { 96 return; 98 } 99 fail("Modifications shall be unsupported"); 100 } 101 102 public void testTemplateWizardCopiesItsPropertiesToMap() throws Exception { 103 doTemplateWizardCopiesItsPropertiesToMap("simpleObject.txt"); 104 } 105 106 public void testTemplateWizardCopiesItsPropertiesToMapForOverridenEntry() throws Exception { 107 DataObject obj = doTemplateWizardCopiesItsPropertiesToMap("simpleObject.prima"); 108 assertEquals("The right loader", SimpleLoader.class, obj.getLoader().getClass()); 109 } 110 111 private DataObject doTemplateWizardCopiesItsPropertiesToMap(String fileName) throws Exception { 112 FileObject root = FileUtil.createMemoryFileSystem().getRoot(); 113 FileObject fo = FileUtil.createData(root, fileName); 114 115 DataObject obj = DataObject.find(fo); 116 117 DataFolder folder = DataFolder.findFolder(FileUtil.createFolder(root, "target")); 118 119 TemplateWizard t = new TemplateWizard(); 120 t.putProperty("type", "empty"); 121 Set <DataObject> created = t.instantiate(obj, folder); 122 assertNotNull(created); 123 assertEquals("One is created: " + created, 1, created.size()); 124 125 DataObject n = created.iterator().next(); 126 127 assertEquals("Created in right place", folder, n.getFolder()); 128 assertEquals("Created with right name", fileName, n.getName()); 129 130 assertEquals("The right source", fo, Hand.origObject); 131 assertEquals("The right source in query", fo, Hand.acceptObject); 132 assertEquals("The right destiny folder", folder.getPrimaryFile(), Hand.fileObject); 133 assertEquals("The right name", "simpleObject", Hand.name); 134 assertTrue("At least two elements: " + Hand.parameters, 2 <= Hand.parameters.size()); 135 assertEquals("empty", Hand.parameters.get("type")); 136 assertEquals("There was no name, just default", null, Hand.parameters.get("name")); 137 assertTrue("the argument is there", Hand.parameters.containsKey("name")); 138 try { 139 Hand.parameters.put("kuk", "buk"); 140 } catch (UnsupportedOperationException ex) { 141 return obj; 143 } 144 fail("Modifications shall be unsupported"); 145 throw new NullPointerException (); 146 } 147 148 public static final class Hand extends CreateFromTemplateHandler { 149 public static FileObject fileObject, origObject, acceptObject; 150 public static String name; 151 public static Map <String , Object > parameters; 152 153 protected boolean accept(FileObject fo) { 154 assertNull(acceptObject); 155 acceptObject = fo; 156 return true; 157 } 158 159 protected FileObject createFromTemplate( 160 FileObject orig, FileObject f, String n, 161 Map <String , Object > p 162 ) throws IOException { 163 assertNull("Not yet filled", fileObject); 164 165 origObject = orig; 166 fileObject = f; 167 name = n; 168 parameters = p; 169 170 return FileUtil.copyFile(orig, fileObject, name); 171 } 172 } 173 174 public static final class Attr extends CreateFromTemplateAttributesProvider { 175 176 177 protected Map <String , ? extends Object > attributesFor( 178 DataObject template, 179 DataFolder target, 180 String name 181 ) { 182 return Collections.singletonMap("name", name); 183 } 184 } 185 186 public static final class DD extends DialogDisplayer { 187 public Object notify(NotifyDescriptor descriptor) { 188 throw new UnsupportedOperationException ("Not supported yet."); 189 } 190 191 public Dialog createDialog(final DialogDescriptor descriptor) { 192 return new JDialog () { 193 @Deprecated 194 public void show() { 195 for (Object object : descriptor.getOptions()) { 196 if (object instanceof JButton ) { 197 JButton b = (JButton )object; 198 if (b.getText().equals("Finish")) { 199 descriptor.setValue(WizardDescriptor.FINISH_OPTION); 200 b.doClick(); 201 return; 202 } 203 } 204 } 205 fail("Cannot find Finish button: " + Arrays.asList(descriptor.getOptions())); 206 } 207 }; 208 } 209 } 210 211 public static final class Pool extends DataLoaderPool { 212 protected Enumeration <DataLoader> loaders() { 213 return Enumerations.<DataLoader>singleton(SimpleLoader.getLoader(SimpleLoader.class)); 214 } 215 } 216 217 public static final class SimpleLoader extends MultiFileLoader { 218 public SimpleLoader() { 219 super(SimpleObject.class.getName()); 220 } 221 protected String displayName() { 222 return "SimpleLoader"; 223 } 224 protected FileObject findPrimaryFile(FileObject fo) { 225 if (fo.hasExt("prima")) { 226 return fo; 227 } 228 return null; 229 } 230 protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException { 231 return new SimpleObject(this, primaryFile); 232 } 233 protected MultiDataObject.Entry createPrimaryEntry(MultiDataObject obj, FileObject primaryFile) { 234 return new FE(obj, primaryFile); 235 } 236 protected MultiDataObject.Entry createSecondaryEntry(MultiDataObject obj, FileObject secondaryFile) { 237 return new FileEntry(obj, secondaryFile); 238 } 239 } 240 241 private static final class FE extends FileEntry { 242 public FE(MultiDataObject mo, FileObject fo) { 243 super(mo, fo); 244 } 245 246 @Override 247 public FileObject createFromTemplate(FileObject f, String name) throws IOException { 248 fail("I do not want to be called"); 249 return null; 250 } 251 252 253 254 } 255 256 public static final class SimpleObject extends MultiDataObject { 257 public SimpleObject(SimpleLoader l, FileObject fo) throws DataObjectExistsException { 258 super(fo, l); 259 } 260 261 public String getName() { 262 return getPrimaryFile().getNameExt(); 263 } 264 } 265 266 } 267 | Popular Tags |