1 19 20 package org.netbeans.core.filesystems; 21 22 import java.io.*; 23 import java.util.*; 24 import java.net.URL ; 25 import org.xml.sax.*; 26 import org.openide.cookies.InstanceCookie; 27 import org.openide.loaders.*; 28 import org.openide.filesystems.*; 29 import org.openide.filesystems.FileSystem; import org.openide.util.*; 31 import org.openide.util.lookup.*; 32 import org.openide.xml.*; 33 import org.openide.*; 34 import org.netbeans.junit.*; 36 37 public class MIMEResolverImplTest extends NbTestCase { 38 List resolvers; 39 FileObject root; 40 41 public MIMEResolverImplTest(String testName) { 42 super(testName); 43 } 44 45 protected void setUp() throws Exception { 46 50 URL u = this.getClass().getResource ("code-fs.xml"); 51 FileSystem fs = new XMLFileSystem(u); 52 53 FileObject coderoot = fs.getRoot().getFileObject("root"); 54 coderoot.refresh(); 55 56 FileObject fos[] = coderoot.getChildren(); 57 resolvers = new ArrayList(); 58 for (int i = 0; i<fos.length; i++) { 59 resolvers.add(createResolver(fos[i])); 60 } 61 62 66 u = this.getClass().getResource ("data-fs.xml"); 67 fs = new XMLFileSystem(u); 68 69 root = fs.getRoot().getFileObject("root"); 70 root.refresh(); 71 FileUtil.setMIMEType("txt2", "text/plain; charset=us-ascii"); 72 } 73 74 public static void main(java.lang.String [] args) { 75 junit.textui.TestRunner.run(suite()); 76 } 77 78 public static NbTest suite() { 79 NbTestSuite suite = new NbTestSuite(MIMEResolverImplTest.class); 80 81 return suite; 82 } 83 84 85 private static MIMEResolver createResolver(FileObject fo) throws Exception { 86 if (fo == null) throw new NullPointerException (); 87 return new MIMEResolverImpl.Impl(fo); 88 } 89 90 private String resolve(FileObject fo) { 91 Iterator it = resolvers.iterator(); 92 while (it.hasNext()) { 93 MIMEResolver r = (MIMEResolver) it.next(); 94 String s = r.findMIMEType(fo); 95 if (s != null) return s; 96 } 97 return null; 98 } 99 100 public void testDeclarativeMIME() throws Exception { 101 102 Object tl1 = new Object (); 103 Object tl2 = new Object (); 104 105 TestThread t1 = new TestThread(tl1); 106 TestThread t2 = new TestThread(tl2); 107 108 110 t1.start(); 111 t2.start(); 112 Thread.currentThread().join(100); 113 synchronized (tl1) {tl1.notify();} 114 synchronized (tl2) {tl2.notify();} 115 116 117 t1.join(5000); 118 t2.join(5000); 119 120 if (t1.fail != null) fail(t1.fail); 121 122 if (t2.fail != null) fail(t2.fail); 123 } 124 125 private class TestThread extends Thread { 126 127 Object lock; 128 String fail; 129 130 private TestThread(Object lock) { 131 this.lock = lock; 132 } 133 134 public void run() { 135 String s; 136 FileObject fo = null; 137 138 fo = root.getFileObject("test","txt2"); 139 s = resolve(fo); 140 if ("mime.xml".equals(s) == false) fail = "mime rule failure: " + fo + " => " + s; 141 142 fo = root.getFileObject("test","elf"); 143 s = resolve(fo); 144 if ("magic-mask.xml".equals(s) == false) fail = "magic-mask rule failure: " + fo + " => " + s; 145 146 fo = root.getFileObject("test","exe"); 147 s = resolve(fo); 148 if ("magic.xml".equals(s) == false) fail = "magic rule failure: " + fo + " => " + s; 149 150 fo = root.getFileObject("root","xml"); 151 s = resolve(fo); 152 if ("root.xml".equals(s) == false) fail = "root rule failure" + fo + " => " + s; 153 154 fo = root.getFileObject("ns","xml"); 155 s = resolve(fo); 156 if ("ns.xml".equals(s) == false) fail = "ns rule failure" + fo + " => " + s; 157 158 try { 159 synchronized (lock) { 160 lock.wait(5000); } 162 } catch (Exception ex) { 163 } 165 166 fo = root.getFileObject("empty","dtd"); 167 s = resolve(fo); 168 if (null != s) fail = "null rule failure" + fo + " => " + s; 169 170 fo = root.getFileObject("pid","xml"); 171 s = resolve(fo); 172 if ("pid.xml".equals(s) == false) fail = "pid rule failure" + fo + " => " + s; 173 174 } 175 } 176 177 180 public void testParseFailures() { 181 assertEquals("build1.xml recognized as Ant script", "text/x-ant+xml", resolve(root.getFileObject("build1", "xml"))); 182 assertEquals("bogus.xml not recognized as anything", null, resolve(root.getFileObject("bogus", "xml"))); 183 assertEquals("build2.xml recognized as Ant script", "text/x-ant+xml", resolve(root.getFileObject("build2", "xml"))); 184 } 185 186 } 187 | Popular Tags |