Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* Contributor(s): |
28 |
* |
29 |
* The Original Software is NetBeans. The Initial Developer of the Original |
30 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2008 Sun |
31 |
* Microsystems, Inc. All Rights Reserved. |
32 |
* |
33 |
* If you wish your version of this file to be governed by only the CDDL |
34 |
* or only the GPL Version 2, indicate your decision by adding |
35 |
* "[Contributor] elects to include this software in this distribution |
36 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
37 |
* single choice of license, a recipient has the option to distribute |
38 |
* your version of this file under either the CDDL, the GPL Version 2 or |
39 |
* to extend the choice of license to its licensees as provided above. |
40 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
41 |
* Version 2 license, then the option applies only if the new code is |
42 |
* made subject to such option by the copyright holder. |
43 |
*/ |
44 |
|
45 |
package org.netbeans.core.netigso; |
46 |
|
47 |
import org.netbeans.core.startup.*; |
48 |
import java.io.File; |
49 |
import java.io.IOException; |
50 |
import java.util.Collections; |
51 |
import java.util.Locale; |
52 |
import org.netbeans.Module; |
53 |
import org.netbeans.ModuleManager; |
54 |
import org.netbeans.SetupHid; |
55 |
import org.openide.filesystems.FileObject; |
56 |
import org.openide.filesystems.FileUtil; |
57 |
import org.osgi.framework.Bundle; |
58 |
|
59 |
/** |
60 |
* How does OSGi integration deals with layer registration? Can we read |
61 |
* it without resolving the bundle? |
62 |
* |
63 |
* @author Jaroslav Tulach |
64 |
*/ |
65 |
public class ExportedIfPresentTest extends SetupHid { |
66 |
private static Module m1; |
67 |
private static ModuleManager mgr; |
68 |
|
69 |
public ExportedIfPresentTest(String name) { |
70 |
super(name); |
71 |
} |
72 |
|
73 |
protected @Override void setUp() throws Exception { |
74 |
// changes minimal start level to 10 |
75 |
Locale.setDefault(new Locale("def", "EX")); |
76 |
clearWorkDir(); |
77 |
|
78 |
|
79 |
data = new File(getDataDir(), "jars"); |
80 |
jars = new File(getWorkDir(), "jars"); |
81 |
jars.mkdirs(); |
82 |
File simpleModule = createTestJAR("simple-module", null); |
83 |
File dependsOnSimpleModule = createTestJAR("depends-on-simple-module", null, simpleModule); |
84 |
|
85 |
if (System.getProperty("netbeans.user") == null) { |
86 |
File ud = new File(getWorkDir(), "ud"); |
87 |
ud.mkdirs(); |
88 |
|
89 |
System.setProperty("netbeans.user", ud.getPath()); |
90 |
|
91 |
|
92 |
ModuleSystem ms = Main.getModuleSystem(); |
93 |
mgr = ms.getManager(); |
94 |
mgr.mutexPrivileged().enterWriteAccess(); |
95 |
try { |
96 |
m1 = mgr.create(simpleModule, null, false, false, false); |
97 |
mgr.enable(Collections.<Module>singleton(m1)); |
98 |
} finally { |
99 |
mgr.mutexPrivileged().exitWriteAccess(); |
100 |
} |
101 |
} |
102 |
|
103 |
} |
104 |
private File createTestJAR(String name, String srcdir, File... classpath) throws IOException { |
105 |
return createTestJAR(data, jars, name, srcdir, classpath); |
106 |
} |
107 |
public void testCanLoadClassFromContextClassLoaderAsNoExportPackageAvailable() throws Exception { |
108 |
FileObject fo; |
109 |
Module m2; |
110 |
try { |
111 |
mgr.mutexPrivileged().enterWriteAccess(); |
112 |
String mfBar = "Bundle-SymbolicName: org.bar\n" + |
113 |
"Bundle-Version: 1.1.0\n" + |
114 |
"Bundle-ManifestVersion: 2\n" + |
115 |
"Import-Package: org.foo\n" + |
116 |
"OpenIDE-Module-Layer: org/bar/layer.xml\n" + |
117 |
"\n\n"; |
118 |
|
119 |
File j2 = changeManifest(new File(jars, "depends-on-simple-module.jar"), mfBar); |
120 |
m2 = mgr.create(j2, null, false, false, false); |
121 |
mgr.enable(m2); |
122 |
} finally { |
123 |
mgr.mutexPrivileged().exitWriteAccess(); |
124 |
} |
125 |
try { |
126 |
mgr.mutexPrivileged().enterWriteAccess(); |
127 |
Class<?> c = mgr.getClassLoader().loadClass("org.bar.SomethingElse"); |
128 |
assertNotNull("Can load the class successfully", c); |
129 |
} finally { |
130 |
mgr.disable(m2); |
131 |
mgr.mutexPrivileged().exitWriteAccess(); |
132 |
} |
133 |
} |
134 |
public void testCannotLoadClassFromContextClassLoaderWhenSomeExportPackageIsAvailable() throws Exception { |
135 |
FileObject fo; |
136 |
Module m2; |
137 |
try { |
138 |
mgr.mutexPrivileged().enterWriteAccess(); |
139 |
String mfBar = "Bundle-SymbolicName: org.bar2\n" + |
140 |
"Bundle-Version: 1.1.0\n" + |
141 |
"Bundle-ManifestVersion: 2\n" + |
142 |
"Import-Package: org.foo\n" + |
143 |
"Export-Package: org.foo\n" + |
144 |
"\n\n"; |
145 |
|
146 |
File j2 = changeManifest(new File(jars, "depends-on-simple-module.jar"), mfBar); |
147 |
m2 = mgr.create(j2, null, false, false, false); |
148 |
mgr.enable(m2); |
149 |
} finally { |
150 |
mgr.mutexPrivileged().exitWriteAccess(); |
151 |
} |
152 |
try { |
153 |
mgr.mutexPrivileged().enterWriteAccess(); |
154 |
try { |
155 |
Class<?> c = mgr.getClassLoader().loadClass("org.bar.SomethingElse"); |
156 |
fail("The class should not have been found: " + c); |
157 |
} catch (ClassNotFoundException ex) { |
158 |
assertTrue(ex.getMessage(), ex.getMessage().contains("SomethingElse")); |
159 |
} |
160 |
} finally { |
161 |
mgr.disable(m2); |
162 |
mgr.mutexPrivileged().exitWriteAccess(); |
163 |
} |
164 |
} |
165 |
|
166 |
} |