1 18 package org.netbeans.core.startup; 19 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.net.URL ; 23 import java.net.URLClassLoader ; 24 import java.util.Arrays ; 25 import java.util.Collections ; 26 import java.util.HashSet ; 27 import java.util.List ; 28 import java.util.Set ; 29 import java.util.jar.JarFile ; 30 import java.util.jar.Manifest ; 31 import org.netbeans.Events; 32 import org.netbeans.InvalidException; 33 import org.netbeans.JarClassLoader; 34 import org.netbeans.Module; 35 import org.netbeans.ModuleFactory; 36 import org.netbeans.ModuleManager; 37 import org.openide.util.Lookup; 38 import org.openide.util.Union2; 39 import org.openide.util.lookup.Lookups; 40 import org.openide.util.lookup.ProxyLookup; 41 42 47 public class ModuleFactoryTest extends ModuleManagerTest { 48 49 static { 51 System.setProperty("org.openide.util.Lookup", L .class.getName()); 52 assertTrue(Lookup.getDefault() instanceof L ); 53 } 54 public static final class L extends ProxyLookup { 55 public L() { 56 super(new Lookup[] { 57 Lookups.fixed(new Object [] { 58 new MyModuleFactory() 59 }), 60 }); 61 } 62 } 63 64 public ModuleFactoryTest(String name) { 65 super(name); 66 } 67 68 public static int numberOfStandard = 0; 69 public static int numberOfFixed = 0; 70 public static boolean testingParentClassloaders = false; 71 public static boolean testingDummyModule = false; 72 73 public void testFactoryCreatesOurModules() throws Exception { 74 numberOfStandard = 0; 76 numberOfFixed = 0; 77 78 FakeModuleInstaller installer = new FakeModuleInstaller(); 79 FakeEvents ev = new FakeEvents(); 80 ModuleManager mgr = new ModuleManager(installer, ev); 81 mgr.mutexPrivileged().enterWriteAccess(); 82 try { 83 File j1 = new File (jars, "simple-module.jar"); 84 File j2 = new File (jars, "depends-on-simple-module.jar"); 85 File j3 = new File (jars, "dep-on-two-modules.jar"); 86 URLClassLoader l = new URLClassLoader (new URL [] {j1.toURL(), j2.toURL()}); 87 Manifest mani1, mani2; 88 JarFile j = new JarFile (j1); 89 try { 90 mani1 = j.getManifest(); 91 } finally { 92 j.close(); 93 } 94 j = new JarFile (j2); 95 try { 96 mani2 = j.getManifest(); 97 } finally { 98 j.close(); 99 } 100 Module m1 = mgr.createFixed(mani1, null, l); 101 Module m2 = mgr.createFixed(mani2, null, l); 102 Module m3 = mgr.create(j3, null, false, false, false); 103 mgr.enable(new HashSet <Module>(Arrays.asList(m1, m2, m3))); 104 } finally { 105 mgr.mutexPrivileged().exitWriteAccess(); 106 } 107 assertEquals("Number of standard modules created by our factory ", 1, numberOfStandard); 108 assertEquals("Number of fixed modules created by our factory ", 2, numberOfFixed); 109 } 110 111 public void testDummyModule() throws Exception { 112 ModuleManager mgr = null; 113 try { 114 testingDummyModule = true; 115 FakeModuleInstaller installer = new FakeModuleInstaller(); 116 FakeEvents ev = new FakeEvents(); 117 mgr = new ModuleManager(installer, ev); 118 mgr.mutexPrivileged().enterWriteAccess(); 119 120 File j1 = new File (jars, "simple-module.jar"); 121 Module m1 = mgr.create(j1, null, false, false, false); 122 mgr.enable(Collections.singleton(m1)); 123 boolean res = false; 124 try { 125 m1.getClassLoader().loadClass("java.lang.String"); 126 } catch (ClassNotFoundException x) { 127 res = true; 128 } 129 assertTrue("dummy module with no-op classloader should not find any classes", res); 130 } finally { 131 testingDummyModule = false; 132 if (mgr != null) { 133 mgr.mutexPrivileged().exitWriteAccess(); 134 } 135 } 136 } 137 138 public void testRemoveBaseClassLoader() throws Exception { 139 ModuleManager mgr = null; 140 try { 141 testingParentClassloaders = true; 142 FakeModuleInstaller installer = new FakeModuleInstaller(); 143 FakeEvents ev = new FakeEvents(); 144 mgr = new ModuleManager(installer, ev); 145 mgr.mutexPrivileged().enterWriteAccess(); 146 File j1 = new File (jars, "simple-module.jar"); 147 Module m1 = mgr.create(j1, null, false, false, false); 148 mgr.enable(Collections.singleton(m1)); 149 boolean res = false; 150 try { 151 mgr.getClassLoader().loadClass("java.lang.String"); 152 } catch (ClassNotFoundException x) { 153 res = true; 154 } 155 assertTrue("When removing the base classloader not even JDK classes should be found", res); 156 } finally { 157 testingParentClassloaders = false; 158 if (mgr != null) { 159 mgr.mutexPrivileged().exitWriteAccess(); 160 } 161 } 162 } 163 164 public static final class MyModuleFactory extends ModuleFactory { 165 public Module create(File jar, Object history, boolean reloadable, boolean autoload, boolean eager, ModuleManager mgr, Events ev) throws IOException { 166 if (testingDummyModule || testingParentClassloaders) { 167 return new DummyModule(mgr, ev, history, reloadable, autoload, eager); 168 } 169 numberOfStandard++; 170 return super.create(jar, history, reloadable, autoload, eager, mgr, ev); 171 } 172 173 public Module createFixed(Manifest mani, Object history, ClassLoader loader, ModuleManager mgr, Events ev) throws InvalidException { 174 numberOfFixed++; 175 return super.createFixed(mani, history, loader, mgr, ev); 176 } 177 178 public boolean removeBaseClassLoader() { 179 if (testingParentClassloaders) { 180 return true; 181 } 182 return super.removeBaseClassLoader(); 183 } 184 public ClassLoader getClasspathDelegateClassLoader(ModuleManager mgr, ClassLoader del) { 185 if (testingParentClassloaders) { 186 return new NoOpClassLoader(); 187 } 188 return del; 189 } 190 } 191 192 private static final class DummyModule extends Module { 193 public DummyModule(ModuleManager mgr, Events ev, Object history, boolean reloadable, boolean autoload, boolean eager) throws IOException { 194 super(mgr, ev, history, reloadable, autoload, eager); 195 manifest = new Manifest (); 196 manifest.getMainAttributes().putValue("OpenIDE-Module", "boom"); 197 parseManifest(); 198 } 199 @Override 200 public List <File > getAllJars() { 201 return Collections.emptyList(); 202 } 203 @Override 204 public void setReloadable(boolean r) { 205 } 206 @Override 207 public void reload() throws IOException { 208 } 209 @Override 210 protected void classLoaderUp(Set parents) throws IOException { 211 classloader = new JarClassLoader(Collections.<Union2<File ,JarFile >>emptyList(), new ClassLoader [] {new NoOpClassLoader()}); 212 } 213 @Override 214 protected void classLoaderDown() { 215 } 216 @Override 217 protected void cleanup() { 218 } 219 @Override 220 protected void destroy() { 221 } 222 @Override 223 public boolean isFixed() { 224 return true; 225 } 226 @Override 227 public Object getLocalizedAttribute(String attr) { 228 return null; 229 } 230 } 231 232 private static final class NoOpClassLoader extends ClassLoader { 233 protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException { 234 if ("java.lang.String".equals(name)) { 235 throw new ClassNotFoundException ("NoOpClassLoader cannot load " + name); 236 } 237 return super.loadClass(name, resolve); 238 } 239 } 240 } 241 | Popular Tags |