1 19 20 package org.openide.loaders; 21 22 23 import java.io.IOException ; 24 import java.util.Collections ; 25 import java.util.Enumeration ; 26 import org.netbeans.junit.MockServices; 27 import org.netbeans.junit.NbTestCase; 28 import org.openide.filesystems.FileObject; 29 import org.openide.filesystems.Repository; 30 import org.openide.util.Enumerations; 31 import org.openide.util.MapFormat; 32 33 37 public class CreateFromTemplateTest extends NbTestCase { 38 39 public CreateFromTemplateTest (String name) { 40 super(name); 41 } 42 43 protected void setUp() { 44 MockServices.setServices(Pool.class); 45 } 46 47 public void testCreateExecutorFromTemplate () throws Exception { 48 String folderName = "/Templates/Services/Executor"; 49 FileObject data = org.openide.filesystems.FileUtil.createData ( 50 Repository.getDefault ().getDefaultFileSystem ().getRoot (), 51 folderName + "/" + "X.xml" 52 ); 53 data.setAttribute ("template", Boolean.TRUE); 54 FileObject fo = data.getParent (); 55 assertNotNull ("FileObject " + folderName + " found on DefaultFileSystem.", fo); 56 DataFolder f = DataFolder.findFolder (fo); 57 assertNotNull ("Folder " + folderName + " found on DefaultFileSystem.", f); 58 DataObject[] executors = f.getChildren (); 59 assertTrue ("Templates for Executor found.", executors.length > 0); 60 DataObject executor = executors[0]; 61 assertNotNull ("Executor found.", executor); 67 String newExecutorName = "NewExecutor" + Double.toString (Math.random ()); 68 executor.createFromTemplate (f, newExecutorName); 69 executors = f.getChildren (); 70 boolean found = false; 71 for (int i = 0; i < executors.length && !found; i++) { 73 found = newExecutorName.equals (executors[i].getName ()); 75 } 76 assertTrue (newExecutorName + " was created on right place.", found); 78 } 79 80 public void testNoTemplateFlagUnset() throws Exception { 81 String folderName = "/Templates/"; 82 FileObject data = org.openide.filesystems.FileUtil.createData ( 83 Repository.getDefault ().getDefaultFileSystem ().getRoot (), 84 folderName + "/" + "X.prima" 85 ); 86 data.setAttribute ("template", Boolean.TRUE); 87 FileObject fo = data.getParent (); 88 assertNotNull ("FileObject " + folderName + " found on DefaultFileSystem.", fo); 89 DataFolder f = DataFolder.findFolder (fo); 90 DataObject templ = DataObject.find(data); 91 92 DataObject res = templ.createFromTemplate(f); 93 94 assertFalse("Not marked as template", res.isTemplate()); 95 assertEquals(SimpleLoader.class, res.getLoader().getClass()); 96 } 97 98 public static final class Pool extends DataLoaderPool { 99 protected Enumeration <DataLoader> loaders() { 100 return Enumerations.<DataLoader>singleton(SimpleLoader.getLoader(SimpleLoader.class)); 101 } 102 } 103 104 public static final class SimpleLoader extends MultiFileLoader { 105 public SimpleLoader() { 106 super(SimpleObject.class.getName()); 107 } 108 protected String displayName() { 109 return "SimpleLoader"; 110 } 111 protected FileObject findPrimaryFile(FileObject fo) { 112 if (fo.hasExt("prima")) { 113 return fo; 114 } 115 return null; 116 } 117 protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException { 118 return new SimpleObject(this, primaryFile); 119 } 120 protected MultiDataObject.Entry createPrimaryEntry(MultiDataObject obj, FileObject primaryFile) { 121 return new FE(obj, primaryFile); 122 } 123 protected MultiDataObject.Entry createSecondaryEntry(MultiDataObject obj, FileObject secondaryFile) { 124 return new FileEntry(obj, secondaryFile); 125 } 126 } 127 128 private static final class FE extends FileEntry.Format { 129 public FE(MultiDataObject mo, FileObject fo) { 130 super(mo, fo); 131 } 132 133 protected java.text.Format createFormat(FileObject target, String n, String e) { 134 return new MapFormat(Collections.emptyMap()); 135 } 136 } 137 138 public static final class SimpleObject extends MultiDataObject { 139 public SimpleObject(SimpleLoader l, FileObject fo) throws DataObjectExistsException { 140 super(fo, l); 141 } 142 143 public String getName() { 144 return getPrimaryFile().getNameExt(); 145 } 146 } 147 148 } 149 150 151 | Popular Tags |