1 19 20 package org.netbeans.core.lookup; 21 22 import java.io.File ; 23 import java.util.ArrayList ; 24 import java.util.Collections ; 25 import java.util.List ; 26 import javax.print.PrintServiceLookup ; 27 import org.netbeans.Module; 28 import org.netbeans.ModuleManager; 29 import org.netbeans.core.startup.ModuleHistory; 30 import org.netbeans.junit.NbTestCase; 31 import org.openide.util.Lookup; 32 import org.openide.util.LookupEvent; 33 import org.openide.util.LookupListener; 34 import org.openide.util.Mutex; 35 import org.openide.util.MutexException; 36 37 43 public class MetaInfServicesTest extends NbTestCase { 44 45 public MetaInfServicesTest(String name) { 46 super(name); 47 } 48 49 private ModuleManager mgr; 50 private Module m1, m2; 51 protected void setUp() throws Exception { 52 clearWorkDir(); 55 mgr = org.netbeans.core.startup.Main.getModuleSystem().getManager(); 58 try { 59 mgr.mutex().writeAccess(new Mutex.ExceptionAction() { 60 public Object run() throws Exception { 61 File jar1 = InstanceDataObjectModuleTestHid.toFile(MetaInfServicesTest.class.getResource("data/services-jar-1.jar")); 62 File jar2 = InstanceDataObjectModuleTestHid.toFile(MetaInfServicesTest.class.getResource("data/services-jar-2.jar")); 63 m1 = mgr.create(jar1, new ModuleHistory(jar1.getAbsolutePath()), false, false, false); 64 m2 = mgr.create(jar2, new ModuleHistory(jar2.getAbsolutePath()), false, false, false); 65 return null; 66 } 67 }); 68 } catch (MutexException me) { 69 throw me.getException(); 70 } 71 assertEquals(Collections.EMPTY_SET, m1.getProblems()); 72 } 73 protected void tearDown() throws Exception { 74 try { 75 mgr.mutex().writeAccess(new Mutex.ExceptionAction() { 76 public Object run() throws Exception { 77 if (m2.isEnabled()) mgr.disable(m2); 78 mgr.delete(m2); 79 if (m1.isEnabled()) mgr.disable(m1); 80 mgr.delete(m1); 81 return null; 82 } 83 }); 84 } catch (MutexException me) { 85 throw me.getException(); 86 } 87 m1 = null; 88 m2 = null; 89 mgr = null; 90 } 91 protected static final int TWIDDLE_ENABLE = 0; 92 protected static final int TWIDDLE_DISABLE = 1; 93 protected void twiddle(final Module m, final int action) throws Exception { 94 try { 95 mgr.mutex().writeAccess(new Mutex.ExceptionAction() { 96 public Object run() throws Exception { 97 switch (action) { 98 case TWIDDLE_ENABLE: 99 mgr.enable(m); 100 break; 101 case TWIDDLE_DISABLE: 102 mgr.disable(m); 103 break; 104 default: 105 throw new IllegalArgumentException ("bad action: " + action); 106 } 107 return null; 108 } 109 }); 110 } catch (MutexException me) { 111 throw me.getException(); 112 } 113 } 114 115 118 public void testEverything() throws Exception { 119 twiddle(m1, TWIDDLE_ENABLE); 120 ClassLoader systemClassLoader = Lookup.getDefault().lookup(ClassLoader .class); 121 Class xface = systemClassLoader.loadClass("org.foo.Interface"); 122 Lookup.Result r = Lookup.getDefault().lookupResult(xface); 123 List instances = new ArrayList (r.allInstances()); 124 assertEquals(1, instances.size()); 126 Object instance1 = instances.get(0); 127 assertTrue(xface.isInstance(instance1)); 128 assertEquals("org.foo.impl.Implementation1", instance1.getClass().getName()); 129 LookupL l = new LookupL(); 131 r.addLookupListener(l); 132 twiddle(m2, TWIDDLE_ENABLE); 133 assertTrue("Turning on a second module with a manifest service fires a lookup change", l.gotSomething()); 134 instances = new ArrayList (r.allInstances()); 135 assertEquals(2, instances.size()); 136 assertEquals(instance1, instances.get(0)); 137 assertEquals("org.bar.Implementation2", instances.get(1).getClass().getName()); 138 l.count = 0; 140 twiddle(m2, TWIDDLE_DISABLE); 141 assertTrue(l.gotSomething()); 142 instances = new ArrayList (r.allInstances()); 143 assertEquals(1, instances.size()); 144 assertEquals(instance1, instances.get(0)); 145 l.count = 0; 147 twiddle(m1, TWIDDLE_DISABLE); 148 assertTrue(l.gotSomething()); 149 instances = new ArrayList (r.allInstances()); 150 assertEquals(0, instances.size()); 151 l.count = 0; 153 twiddle(m1, TWIDDLE_ENABLE); 154 instances = new ArrayList (r.allInstances()); 156 assertEquals(0, instances.size()); 157 systemClassLoader = Lookup.getDefault().lookup(ClassLoader .class); 158 Class xface2 = systemClassLoader.loadClass("org.foo.Interface"); 159 assertTrue(xface != xface2); 160 Lookup.Result r2 = Lookup.getDefault().lookupResult(xface2); 161 instances = new ArrayList (r2.allInstances()); 162 assertEquals(1, instances.size()); 163 PrintServiceLookup psl = Lookup.getDefault().lookup(PrintServiceLookup .class); 165 assertNotNull("Some META-INF/services/javax.print.PrintServiceLookup was found in " + Lookup.getDefault(), psl); 166 } 167 168 protected static final class LookupL implements LookupListener { 169 public int count = 0; 170 public synchronized void resultChanged(LookupEvent ev) { 171 count++; 172 notifyAll(); 173 } 174 public synchronized boolean gotSomething() throws InterruptedException { 175 if (count > 0) return true; 176 wait(9999); 177 return count > 0; 178 } 179 } 180 181 } 182 | Popular Tags |