1 19 20 package org.netbeans.core.startup; 21 22 import org.netbeans.Module; 24 import org.netbeans.ModuleManager; 25 import org.netbeans.core.startup.ModuleHistory; 26 import org.netbeans.core.startup.ModuleList; 27 import org.netbeans.junit.*; 28 import junit.textui.TestRunner; 29 import java.util.*; 30 import java.lang.reflect.Method ; 31 import org.openide.util.*; 32 import org.openide.modules.*; 33 import java.io.*; 34 import java.net.URI ; 35 import org.openide.filesystems.*; 36 import org.openide.util.lookup.Lookups; 37 import org.openide.util.lookup.ProxyLookup; 38 39 43 public class ModuleListTest extends SetupHid { 44 45 static { 46 System.setProperty("org.openide.util.Lookup", L.class.getName()); 47 } 48 public static final class L extends ProxyLookup { 49 public L() { 50 super(new Lookup[] { 51 Lookups.singleton(new IFL()), 52 }); 53 } 54 } 55 56 private static final File JARS = new File(URI.create(ModuleListTest.class.getResource("jars").toExternalForm())); 57 private static final String PREFIX = "wherever/"; 58 59 private static final class IFL extends InstalledFileLocator { 60 public IFL() {} 61 public File locate(String relativePath, String codeNameBase, boolean localized) { 62 if (relativePath.startsWith(PREFIX)) { 63 File f = new File(JARS, relativePath.substring(PREFIX.length()).replace('/', File.separatorChar)); 64 if (f.exists()) { 65 return f; 66 } 67 } 68 return null; 69 } 70 } 71 72 public ModuleListTest(String name) { 73 super(name); 74 } 75 76 private ModuleManager mgr; 77 private org.netbeans.core.startup.ModuleList list; 78 private FileObject modulesfolder; 79 protected void setUp() throws Exception { 80 super.setUp(); 81 clearWorkDir(); 82 FakeModuleInstaller installer = new FakeModuleInstaller(); 83 FakeEvents ev = new FakeEvents(); 84 mgr = new ModuleManager(installer, ev); 85 File dir = getWorkDir(); 86 File modulesdir = new File(dir, "Modules"); 87 if (! modulesdir.mkdir()) throw new IOException("Making " + modulesdir); 88 LocalFileSystem fs = new LocalFileSystem(); 89 fs.setRootDirectory(dir); 90 modulesfolder = fs.findResource("Modules"); 91 assertNotNull(modulesfolder); 92 list = new ModuleList(mgr, modulesfolder, ev); 93 } 94 95 private Module makeModule(String jarName) throws Exception { 96 File f = new File(JARS, jarName); 97 Module m = mgr.create(f, new ModuleHistory(PREFIX + jarName), false, false, false); 98 return m; 99 } 100 101 105 public void testScanAndTwiddle() throws Exception { 106 mgr.mutexPrivileged().enterWriteAccess(); 107 try { 108 assertEquals(Collections.EMPTY_SET, list.readInitial()); 110 Set modules = new HashSet(); 111 modules.add(makeModule("simple-module.jar")); 112 modules.add(makeModule("depends-on-simple-module.jar")); 113 list.trigger(modules); 114 assertEquals(modules, mgr.getEnabledModules()); 115 } finally { 116 mgr.mutexPrivileged().exitWriteAccess(); 117 } 118 FileObject[] xml = modulesfolder.getChildren(); 119 assertEquals(2, xml.length); 120 FileObject foo, bar; 121 if (xml[0].getPath().equals("Modules/org-foo.xml")) { 122 assertEquals("Modules/org-bar.xml", xml[1].getPath()); 123 foo = xml[0]; 124 bar = xml[1]; 125 } else { 126 assertEquals("Modules/org-bar.xml", xml[0].getPath()); 127 assertEquals("Modules/org-foo.xml", xml[1].getPath()); 128 foo = xml[1]; 129 bar = xml[0]; 130 } 131 assertFile(FileUtil.toFile(foo), new File(JARS, "org-foo.xml")); 132 assertFile(FileUtil.toFile(bar), new File(JARS, "org-bar.xml")); 133 LoggedFileListener listener = new LoggedFileListener(); 135 modulesfolder.addFileChangeListener(listener); 136 mgr.mutexPrivileged().enterWriteAccess(); 137 try { 138 Module m1 = mgr.get("org.foo"); 139 assertNotNull(m1); 140 Module m2 = mgr.get("org.bar"); 141 assertNotNull(m2); 142 mgr.disable(new HashSet(Arrays.asList(new Module[] {m1, m2}))); 143 } finally { 144 mgr.mutexPrivileged().exitWriteAccess(); 145 } 146 listener.waitForChange("Modules/org-foo.xml"); 148 listener.waitForChange("Modules/org-bar.xml"); 149 assertFile(new File(JARS, "org-foo_disabled.xml"), FileUtil.toFile(foo)); 150 assertFile(new File(JARS, "org-bar_disabled.xml"), FileUtil.toFile(bar)); 151 LoggedPCListener listener2 = new LoggedPCListener(); 153 Module m1 = mgr.get("org.foo"); 154 m1.addPropertyChangeListener(listener2); 155 copy(new File(JARS, "org-foo.xml"), foo); 156 160 listener2.waitForChange(m1, Module.PROP_ENABLED); 163 assertTrue("m1 is enabled now", m1.isEnabled()); 164 } 165 166 169 public void testAddNewModuleViaXML() throws Exception { 170 mgr.mutexPrivileged().enterWriteAccess(); 171 try { 172 assertEquals(Collections.EMPTY_SET, list.readInitial()); 173 assertEquals(Collections.EMPTY_SET, mgr.getModules()); 174 list.trigger(Collections.EMPTY_SET); 175 assertEquals(Collections.EMPTY_SET, mgr.getModules()); 176 } finally { 177 mgr.mutexPrivileged().exitWriteAccess(); 178 } 179 LoggedPCListener listener = new LoggedPCListener(); 180 mgr.addPropertyChangeListener(listener); 181 modulesfolder.getFileSystem().runAtomicAction(new FileSystem.AtomicAction() { 182 public void run() throws IOException { 183 FileObject fooxml = modulesfolder.createData("org-foo", "xml"); 185 copy(new File(JARS, "org-foo.xml"), fooxml); 186 } 187 }); 188 assertTrue("PROP_MODULES fired", listener.waitForChange(mgr, ModuleManager.PROP_MODULES)); 189 mgr.mutexPrivileged().enterReadAccess(); 190 try { 191 Set modules = mgr.getEnabledModules(); 192 assertEquals(1, modules.size()); 193 Module m = (Module)modules.iterator().next(); 194 assertEquals("org.foo", m.getCodeNameBase()); 195 assertTrue(m.isEnabled()); 196 } finally { 197 mgr.mutexPrivileged().exitReadAccess(); 198 } 199 } 200 201 203 208 } 209 | Popular Tags |