1 19 20 package org.netbeans.core.filesystems; 21 22 import java.io.IOException ; 23 import java.io.OutputStream ; 24 import java.io.PrintStream ; 25 import java.util.Collection ; 26 import java.util.jar.Attributes ; 27 import java.util.logging.Level ; 28 import java.util.logging.Logger ; 29 import javax.swing.event.ChangeEvent ; 30 import javax.swing.event.ChangeListener ; 31 import org.netbeans.core.LoaderPoolNode; 32 import org.netbeans.core.startup.ManifestSection; 33 import org.openide.ErrorManager; 34 import org.openide.filesystems.FileObject; 35 import org.openide.filesystems.FileUtil; 36 import org.openide.filesystems.LocalFileSystem; 37 import org.openide.filesystems.MIMEResolver; 38 import org.openide.filesystems.Repository; 39 import org.openide.loaders.DataLoaderPool; 40 import org.openide.loaders.DataObject; 41 import org.openide.loaders.DataObjectExistsException; 42 import org.openide.loaders.MultiDataObject; 43 import org.openide.loaders.UniFileLoader; 44 import org.openide.util.Lookup; 45 import org.openide.util.LookupEvent; 46 import org.openide.util.LookupListener; 47 48 52 public class FileEntityResolverTest extends org.netbeans.core.LoggingTestCaseHid 53 implements LookupListener, ChangeListener { 54 private FileObject fo; 55 private Lenka loader; 56 private ManifestSection.LoaderSection ls; 57 private Lookup.Result mimeResolvers; 58 private int change; 59 private int poolChange; 60 private static ErrorManager err; 61 62 public FileEntityResolverTest(String testName) { 63 super(testName); 64 } 65 66 protected void setUp() throws Exception { 67 clearWorkDir(); 68 69 err = ErrorManager.getDefault().getInstance("TEST-" + getName()); 70 71 DataLoaderPool.getDefault().addChangeListener(this); 72 73 org.netbeans.core.startup.Main.getModuleSystem(); 74 75 Thread.sleep(2000); 76 77 assertEquals("No change in pool during initialization of module system", 0, poolChange); 78 79 LocalFileSystem lfs = new LocalFileSystem(); 80 lfs.setRootDirectory(getWorkDir()); 81 82 fo = FileUtil.createData(lfs.getRoot(), "X.lenka"); 83 84 Attributes at = new Attributes (); 85 at.putValue("OpenIDE-Module-Class", "Loader"); 86 String name = Lenka.class.getName().replace('.', '/') + ".class"; 87 ls = (ManifestSection.LoaderSection)ManifestSection.create(name, at, null); 88 LoaderPoolNode.add(ls); 89 90 loader = (Lenka) Lenka.getLoader(Lenka.class); 91 92 mimeResolvers = Lookup.getDefault().lookupResult(MIMEResolver.class); 93 mimeResolvers.addLookupListener(this); 94 } 95 96 protected void tearDown() throws Exception { 97 LoaderPoolNode.remove(loader); 98 } 99 100 public void testNewResolverShallInfluenceExistingDataObjects() throws Exception { 101 DataObject old = DataObject.find(fo); 102 if (old.getLoader() == loader) { 103 fail("The should be taken be default loader: " + old); 104 } 105 if (old.getClass() == MultiDataObject.class) { 106 fail("The should be taken be default loader: " + old); 107 } 108 109 assertEquals("No changes in lookup yet", 0, change); 110 111 err.log("starting to create the resolver"); 112 FileObject res = FileUtil.createData( 113 Repository.getDefault().getDefaultFileSystem().getRoot(), 114 "Services/MIMEResolver/Lenkaresolver.xml" 115 ); 116 err.log("file created: " + res); 117 org.openide.filesystems.FileLock l = res.lock(); 118 OutputStream os = res.getOutputStream(l); 119 err.log("stream opened"); 120 PrintStream ps = new PrintStream (os); 121 122 ps.println("<?xml version='1.0' encoding='UTF-8'?>"); 123 ps.println("<!DOCTYPE MIME-resolver PUBLIC '-//NetBeans//DTD MIME Resolver 1.0//EN' 'http://www.netbeans.org/dtds/mime-resolver-1_0.dtd'>"); 124 ps.println("<MIME-resolver>"); 125 ps.println(" <file>"); 126 ps.println(" <ext name='lenka'/>"); 127 ps.println(" <resolver mime='hodna/lenka'/>"); 128 ps.println(" </file>"); 129 ps.println("</MIME-resolver>"); 130 131 err.log("Content written"); 132 os.close(); 133 err.log("Stream closed"); 134 l.releaseLock(); 135 err.log("releaseLock"); 136 137 err.log("Let's query the resolvers"); 138 Collection isthere = mimeResolvers.allInstances(); 139 err.log("What is the result: " + isthere); 140 assertEquals("resolver found", 1, change); 141 142 err.log("Waiting till finished"); 143 LoaderPoolNode.waitFinished(); 144 err.log("Waiting done, querying the data object"); 145 146 err.log("Clear the mime type cache in org.openide.filesystems.MIMESupport: " + fo.getFileSystem().getRoot().getMIMEType()); 147 148 DataObject now = DataObject.find(fo); 149 150 err.log("Object is here: " + now); 151 assertEquals("Loader updated to lenka (mimetype: " + fo.getMIMEType() + ")", loader, now.getLoader()); 152 } 153 154 public void resultChanged(LookupEvent ev) { 155 Logger.global.log(Level.WARNING, null, new Exception ("change in lookup")); 156 change++; 157 } 158 159 public void stateChanged(ChangeEvent e) { 160 poolChange++; 161 } 162 163 public static final class Lenka extends UniFileLoader { 164 public Lenka() { 165 super(MultiDataObject.class.getName()); 166 } 167 168 protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException { 169 return new MultiDataObject(primaryFile, this); 170 } 171 172 protected void initialize() { 173 getExtensions().addMimeType("hodna/lenka"); 174 super.initialize(); 175 } 176 177 protected FileObject findPrimaryFile(FileObject fo) { 178 err.log("findPrimaryFile: " + fo + " with mime: " + fo.getMIMEType()); 179 FileObject retValue; 180 retValue = super.findPrimaryFile(fo); 181 err.log("findPrimaryFile result: " + retValue); 182 return retValue; 183 } 184 185 } 186 } 187 | Popular Tags |