KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > startup > ModuleListTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.startup;
21
22 //import junit.framework.*;
23
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 JavaDoc;
31 import org.openide.util.*;
32 import org.openide.modules.*;
33 import java.io.*;
34 import java.net.URI JavaDoc;
35 import org.openide.filesystems.*;
36 import org.openide.util.lookup.Lookups;
37 import org.openide.util.lookup.ProxyLookup;
38
39 /** Test the functions of the module list, i.e. finding modules on
40  * disk and installing them, and writing out state as needed.
41  * @author Jesse Glick
42  */

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 JavaDoc PREFIX = "wherever/";
58     
59     private static final class IFL extends InstalledFileLocator {
60         public IFL() {}
61         public File locate(String JavaDoc relativePath, String JavaDoc 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 JavaDoc 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 JavaDoc {
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 JavaDoc jarName) throws Exception JavaDoc {
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     /** Load simple-module and depends-on-simple-module.
102      * Make sure they can be installed and in a sane order.
103      * Make sure a class from one can depend on a class from another.
104      */

105     public void testScanAndTwiddle() throws Exception JavaDoc {
106         mgr.mutexPrivileged().enterWriteAccess();
107         try {
108             // XXX try to read an actual initial list too...
109
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         // Checking that changes in memory will rewrite XML:
134
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         // We expect it to have marked both as disabled now:
147
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         // Check that changes in disk are parsed and applied (#13921)
152
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         /* Does not seem to refresh reliably enough:
157         copy(new File(JARS, "org-foo.xml"), FileUtil.toFile(foo));
158         foo.refresh();
159          */

160         // The change ought to be noticed by filesystems, picked up by
161
// ModuleList, parsed, and result in org.foo being turned back on.
162
listener2.waitForChange(m1, Module.PROP_ENABLED);
163         assertTrue("m1 is enabled now", m1.isEnabled());
164     }
165     
166     /** Check that adding a new module via XML, as Auto Update does, works.
167      * Written to help test #27106.
168      */

169     public void testAddNewModuleViaXML() throws Exception JavaDoc {
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                 // XXX this will require that there be an appropriate InstalledFileLocator in Lookup
184
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     // XXX try to read a nonempty initial list
202

203     // XXX would be nice to also have test which uses a layer
204
// to install and remove the Modules/ entries in a MFS
205
// and check that layer-driven events are enough to cause
206
// complex installations & uninstallations
207

208 }
209
Popular Tags