1 19 20 package org.netbeans.modules.j2ee.persistence.unit; 21 22 import java.net.URI ; 23 import java.util.Enumeration ; 24 import org.netbeans.api.project.Project; 25 import org.netbeans.junit.NbTestCase; 26 import org.netbeans.spi.project.FileOwnerQueryImplementation; 27 import org.openide.filesystems.FileObject; 28 import org.openide.filesystems.MIMEResolver; 29 import org.openide.loaders.DataLoaderPool; 30 import org.openide.util.Enumerations; 31 import org.openide.util.Lookup; 32 import org.openide.util.lookup.Lookups; 33 import org.openide.util.lookup.ProxyLookup; 34 35 39 public abstract class PUDataObjectTestBase extends NbTestCase { 40 41 static { 42 System.setProperty("org.openide.util.Lookup", Lkp.class.getName()); 43 ((Lkp)Lookup.getDefault()).setLookups(new Object [] { new PUMimeResolver(), new Pool(), new PUFOQI() }); 44 45 assertEquals("Unable to set the default lookup!", Lkp.class, Lookup.getDefault().getClass()); 46 assertEquals("The default MIMEResolver is not our resolver!", PUMimeResolver.class, Lookup.getDefault().lookup(MIMEResolver.class).getClass()); 47 assertEquals("The default DataLoaderPool is not our pool!", Pool.class, Lookup.getDefault().lookup(DataLoaderPool.class).getClass()); 48 } 49 50 public PUDataObjectTestBase(String name) { 51 super(name); 52 } 53 54 57 public static final class Lkp extends ProxyLookup { 58 59 public Lkp() { 60 setLookups(new Object [0]); 61 } 62 63 public void setLookups(Object [] instances) { 64 ClassLoader l = PersistenceEditorTestBase.class.getClassLoader(); 65 setLookups(new Lookup[] { 66 Lookups.fixed(instances), 67 Lookups.metaInfServices(l), 68 Lookups.singleton(l) 69 }); 70 } 71 } 72 73 77 public static final class Pool extends DataLoaderPool { 78 79 public Enumeration loaders() { 80 return Enumerations.singleton(new PUDataLoader()); 81 } 82 } 83 84 87 public static final class PUMimeResolver extends MIMEResolver { 88 89 public String findMIMEType(FileObject fo) { 90 if (fo.getName().startsWith("persistence")){ 91 return PUDataLoader.REQUIRED_MIME; 92 } 93 return null; 94 } 95 } 96 97 101 private static final class PUFOQI implements FileOwnerQueryImplementation { 102 103 private final Project dummyProject = new Project() { 104 public Lookup getLookup() { return Lookup.EMPTY; } 105 public FileObject getProjectDirectory() { return null; } 106 }; 107 108 public Project getOwner(URI file) { 109 return dummyProject; 110 } 111 112 public Project getOwner(FileObject file) { 113 return dummyProject; 114 } 115 } 116 } 117 | Popular Tags |