KickJava   Java API By Example, From Geeks To Geeks.

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


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 Nokia. Portions Copyright 2005 Nokia. All Rights Reserved.
17  */

18 package org.netbeans.core.startup;
19
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.net.URLClassLoader JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.jar.JarFile JavaDoc;
30 import java.util.jar.Manifest JavaDoc;
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 /**
43  * These tests verify that the module manager behaves basically the
44  * same way with ModuleFactory as without it.
45  * @author David Strupl
46  */

47 public class ModuleFactoryTest extends ModuleManagerTest {
48
49     // #88772: MockServices does not work here, probably because MainLookup ignores CCL.
50
static {
51         System.setProperty("org.openide.util.Lookup", L JavaDoc.class.getName());
52         assertTrue(Lookup.getDefault() instanceof L JavaDoc);
53     }
54     public static final class L extends ProxyLookup {
55         public L() {
56             super(new Lookup[] {
57                 Lookups.fixed(new Object JavaDoc[] {
58                     new MyModuleFactory()
59                 }),
60             });
61         }
62     }
63
64     public ModuleFactoryTest(String JavaDoc 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 JavaDoc {
74         // clear the counters before the test!
75
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 JavaDoc j1 = new File JavaDoc(jars, "simple-module.jar");
84             File JavaDoc j2 = new File JavaDoc(jars, "depends-on-simple-module.jar");
85             File JavaDoc j3 = new File JavaDoc(jars, "dep-on-two-modules.jar");
86             URLClassLoader JavaDoc l = new URLClassLoader JavaDoc(new URL JavaDoc[] {j1.toURL(), j2.toURL()});
87             Manifest JavaDoc mani1, mani2;
88             JarFile JavaDoc j = new JarFile JavaDoc(j1);
89             try {
90                 mani1 = j.getManifest();
91             } finally {
92                 j.close();
93             }
94             j = new JarFile JavaDoc(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 JavaDoc<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 JavaDoc {
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 JavaDoc j1 = new File JavaDoc(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 JavaDoc 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 JavaDoc {
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 JavaDoc j1 = new File JavaDoc(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 JavaDoc 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 JavaDoc jar, Object JavaDoc history, boolean reloadable, boolean autoload, boolean eager, ModuleManager mgr, Events ev) throws IOException JavaDoc {
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 JavaDoc mani, Object JavaDoc history, ClassLoader JavaDoc 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 JavaDoc getClasspathDelegateClassLoader(ModuleManager mgr, ClassLoader JavaDoc 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 JavaDoc history, boolean reloadable, boolean autoload, boolean eager) throws IOException JavaDoc {
194             super(mgr, ev, history, reloadable, autoload, eager);
195             manifest = new Manifest JavaDoc();
196             manifest.getMainAttributes().putValue("OpenIDE-Module", "boom");
197             parseManifest();
198         }
199         @Override JavaDoc
200         public List JavaDoc<File JavaDoc> getAllJars() {
201             return Collections.emptyList();
202         }
203         @Override JavaDoc
204         public void setReloadable(boolean r) {
205         }
206         @Override JavaDoc
207         public void reload() throws IOException JavaDoc {
208         }
209         @Override JavaDoc
210         protected void classLoaderUp(Set JavaDoc parents) throws IOException JavaDoc {
211             classloader = new JarClassLoader(Collections.<Union2<File JavaDoc,JarFile JavaDoc>>emptyList(), new ClassLoader JavaDoc[] {new NoOpClassLoader()});
212         }
213         @Override JavaDoc
214         protected void classLoaderDown() {
215         }
216         @Override JavaDoc
217         protected void cleanup() {
218         }
219         @Override JavaDoc
220         protected void destroy() {
221         }
222         @Override JavaDoc
223         public boolean isFixed() {
224             return true;
225         }
226         @Override JavaDoc
227         public Object JavaDoc getLocalizedAttribute(String JavaDoc attr) {
228             return null;
229         }
230     }
231     
232     private static final class NoOpClassLoader extends ClassLoader JavaDoc {
233         protected Class JavaDoc loadClass(String JavaDoc name, boolean resolve) throws ClassNotFoundException JavaDoc {
234             if ("java.lang.String".equals(name)) {
235                 throw new ClassNotFoundException JavaDoc("NoOpClassLoader cannot load " + name);
236             }
237             return super.loadClass(name, resolve);
238         }
239     }
240 }
241
Popular Tags