1 19 20 package org.openide.filesystems; 21 22 import java.io.IOException ; 23 import java.lang.ref.WeakReference ; 24 import org.netbeans.junit.NbTestCase; 25 import org.openide.util.Lookup; 26 import org.openide.util.lookup.AbstractLookup; 27 import org.openide.util.lookup.InstanceContent; 28 import org.openide.util.lookup.ProxyLookup; 29 30 34 public class MIMESupportTest extends NbTestCase { 35 private TestLookup lookup; 36 public MIMESupportTest(String testName) { 37 super(testName); 38 } 39 40 static { 41 System.setProperty("org.openide.util.Lookup", MIMESupportTest.TestLookup.class.getName()); 42 assertEquals(MIMESupportTest.TestLookup.class, Lookup.getDefault().getClass()); 43 44 } 45 46 protected void setUp() throws Exception { 47 lookup = (MIMESupportTest.TestLookup)Lookup.getDefault(); 48 lookup.init(); 49 } 50 51 public void testFindMIMETypeCanBeGarbageCollected() throws IOException { 52 FileObject fo = FileUtil.createData(FileUtil.createMemoryFileSystem().getRoot(), "Ahoj.bla"); 53 54 String expResult = "content/unknown"; 55 String result = FileUtil.getMIMEType(fo); 56 assertEquals("some content found", expResult, result); 57 58 WeakReference <FileObject> r = new WeakReference <FileObject>(fo); 59 fo = null; 60 assertGC("Can be GCed", r); 61 } 62 63 public void testBehaviourWhemLookupResultIsChanging() throws Exception { 64 MIMESupportTest.TestResolver testR = new MIMESupportTest.TestResolver("a/a"); 65 assertTrue(Lookup.getDefault().lookupAll(MIMEResolver.class).isEmpty()); 66 67 FileObject fo = FileUtil.createData(FileUtil.createMemoryFileSystem().getRoot(), "mysterious.lenka"); 68 69 assertEquals("content/unknown",fo.getMIMEType()); 70 71 lookup.setLookups(testR); 72 assertTrue(Lookup.getDefault().lookupAll(MIMEResolver.class).contains(testR)); 73 assertEquals(testR.getMime(),fo.getMIMEType()); 74 75 testR = new MIMESupportTest.TestResolver("b/b"); 76 lookup.setLookups(testR); 77 assertTrue(Lookup.getDefault().lookupAll(MIMEResolver.class).contains(testR)); 78 assertEquals(testR.getMime(),fo.getMIMEType()); 79 } 80 81 private class TestResolver extends MIMEResolver { 82 private String mime; 83 private TestResolver(String mime) { 84 this.mime = mime; 85 } 86 87 public String findMIMEType(FileObject fo) { 88 return mime; 89 } 90 91 private String getMime() { 92 return mime; 93 } 94 } 95 96 public static class TestLookup extends ProxyLookup { 97 public TestLookup() { 98 super(); 99 init(); 100 } 101 102 private void init() { 103 setLookups(new Lookup[] {}); 104 } 105 106 private void setLookups(Object instance) { 107 setLookups(new Lookup[] {getInstanceLookup(instance)}); 108 } 109 110 private Lookup getInstanceLookup(final Object instance) { 111 InstanceContent instanceContent = new InstanceContent(); 112 instanceContent.add(instance); 113 Lookup instanceLookup = new AbstractLookup(instanceContent); 114 return instanceLookup; 115 } 116 } 117 118 } 119 | Popular Tags |