1 19 20 package org.openide.loaders; 21 22 import java.io.IOException ; 23 import java.util.Enumeration ; 24 import java.util.logging.Level ; 25 import java.util.logging.Logger ; 26 import org.netbeans.junit.Log; 27 import org.netbeans.junit.NbTestCase; 28 import org.openide.filesystems.FileObject; 29 import org.openide.filesystems.FileUtil; 30 import org.openide.nodes.CookieSet; 31 import org.openide.util.Enumerations; 32 import org.openide.util.NbBundle; 33 import org.openide.util.io.NbMarshalledObject; 34 35 36 41 public class FightWithWrongOrderOfLoadersTest extends LoggingTestCaseHid 42 implements DataLoader.RecognizedFiles { 43 44 private CharSequence log; 45 private FileObject f0; 46 private FileObject f1; 47 private FileObject f2; 48 49 public FightWithWrongOrderOfLoadersTest(String testName) { 50 super(testName); 51 } 52 53 protected void setUp() throws Exception { 54 log = Log.enable("org.openide.loaders", Level.SEVERE); 55 56 registerIntoLookup(new Pool()); 57 58 FileObject fo = FileUtil.createFolder(FileUtil.createMemoryFileSystem().getRoot(), "test"); 59 60 f0 = FileUtil.createData(fo, "j1.java"); 61 f1 = FileUtil.createData(fo, "f.formKit"); 62 f2 = FileUtil.createData(fo, "f.java"); 63 } 64 65 protected Level logLevel() { 66 return Level.INFO; 67 } 68 69 public void testRecognizeToDefault() throws Exception { 70 DataObject obj0 = DataObject.find(f0); 71 DataObject obj1 = DataObject.find(f1); 72 DataObject obj2 = DataObject.find(f2); 73 74 assertEquals(DataLoader.getLoader(JavaDataLoader.class), obj0.getLoader()); 75 assertEquals(DataLoader.getLoader(JavaDataLoader.class), obj2.getLoader()); 76 77 78 if (obj1 == obj2) { 79 fail("They should be different: " + obj1); 80 } 81 82 assertEquals("It is default loader", obj1.getLoader(), DataLoaderPool.getDefaultFileLoader()); 83 } 84 85 public void testRecognizeJavaFirstFormKitLater() throws Exception { 86 DataObject obj0 = DataObject.find(f0); 87 DataObject obj2 = DataObject.find(f2); 88 DataObject obj1 = DataObject.find(f1); 89 90 assertEquals(DataLoader.getLoader(JavaDataLoader.class), obj0.getLoader()); 91 assertEquals(DataLoader.getLoader(JavaDataLoader.class), obj2.getLoader()); 92 93 if (obj1 == obj2) { 94 fail("They should be different: " + obj1); 95 } 96 97 assertEquals("It is default loader", obj1.getLoader(), DataLoaderPool.getDefaultFileLoader()); 98 } 99 100 101 102 public void markRecognized(FileObject fo) { 103 } 104 105 106 private static final class Pool extends DataLoaderPool { 107 private static DataLoader[] ARR = { 108 JavaDataLoader.getLoader(JavaDataLoader.class), 109 FormKitDataLoader.getLoader(FormKitDataLoader.class), 110 }; 111 112 113 protected Enumeration loaders() { 114 return Enumerations.array(ARR); 115 } 116 } 117 118 public static class JavaDataLoader extends MultiFileLoader { 119 120 public static final String JAVA_EXTENSION = "java"; 121 122 public JavaDataLoader() { 123 super(JavaDO.class.getName()); 124 } 125 126 protected JavaDataLoader(String s) { 127 super(s); 128 } 129 130 protected FileObject findPrimaryFile(FileObject fo) { 131 if (fo.hasExt(JAVA_EXTENSION)) { 132 return fo; 133 } else { 134 return null; 135 } 136 } 137 138 protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException { 139 return new JavaDO(primaryFile, this); 140 } 141 142 protected MultiDataObject.Entry createPrimaryEntry(MultiDataObject obj, FileObject primaryFile) { 143 return new FileEntry(obj, primaryFile); 144 } 145 146 protected MultiDataObject.Entry createSecondaryEntry(MultiDataObject obj, FileObject secondaryFile) { 147 throw new UnsupportedOperationException (); 148 } 149 150 static class JavaDO extends MultiDataObject { 151 public JavaDO(FileObject fo, JavaDataLoader l) throws DataObjectExistsException { 152 super(fo, l); 153 } 154 } 155 } 156 157 158 public static class FormKitDataLoader extends JavaDataLoader { 159 private static Logger LOG = Logger.getLogger(FormKitDataLoader.class.getName()); 160 161 public static final String REQUIRED_MIME = "text/x-java"; 162 public static final String FORM_EXTENSION = "formKit"; 164 private static final long serialVersionUID = 1L; 165 166 public FormKitDataLoader() 167 { 168 super(FormKitDataObject.class.getName()); 169 } 170 171 protected String defaultDisplayName() 172 { 173 return NbBundle.getMessage(FormKitDataLoader.class, "LBL_FormKit_loader_name"); 174 } 175 176 protected String actionsContext() 177 { 178 return "Loaders/" + REQUIRED_MIME + "/Actions"; 179 } 180 181 protected FileObject findPrimaryFile(FileObject fo) 182 { 183 LOG.info("FormKitDataLoader.findPrimaryFile(): " + fo.getNameExt()); 184 185 String ext = fo.getExt(); 186 if (ext.equals(FORM_EXTENSION)) 187 { 188 return FileUtil.findBrother(fo, JAVA_EXTENSION); 189 } 190 if (ext.equals(JAVA_EXTENSION) && FileUtil.findBrother(fo, FORM_EXTENSION) != null) 191 { 192 return fo; 193 } 194 return null; 195 } 196 197 protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, java.io.IOException 198 { 199 LOG.info("FormKitDataLoader.createMultiObject(): " + primaryFile.getNameExt()); 200 201 return new FormKitDataObject(FileUtil.findBrother(primaryFile, FORM_EXTENSION), 202 primaryFile, 203 this); 204 } 205 206 protected MultiDataObject.Entry createSecondaryEntry(MultiDataObject multiDataObject, FileObject fileObject) 207 { 208 if (fileObject.getExt().equals(FORM_EXTENSION)) 209 { 210 FileEntry formEntry = new FileEntry(multiDataObject, fileObject); 211 return formEntry; 212 } 213 return super.createSecondaryEntry(multiDataObject, fileObject); 214 } 215 216 public final class FormKitDataObject extends JavaDO { 217 FileEntry formEntry; 218 219 public FormKitDataObject(FileObject ffo, FileObject jfo, FormKitDataLoader loader) throws DataObjectExistsException, IOException 220 { 221 super(jfo, loader); 222 formEntry = (FileEntry)registerEntry(ffo); 223 224 CookieSet cookies = getCookieSet(); 225 } 227 228 229 } 230 } 231 232 } 233 | Popular Tags |