Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 1997-2010 Sun Microsystems, Inc. All rights reserved. |
5 |
* |
6 |
* The contents of this file are subject to the terms of either the GNU |
7 |
* General Public License Version 2 only ("GPL") or the Common |
8 |
* Development and Distribution License("CDDL") (collectively, the |
9 |
* "License"). You may not use this file except in compliance with the |
10 |
* License. You can obtain a copy of the License at |
11 |
* http://www.netbeans.org/cddl-gplv2.html |
12 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
13 |
* specific language governing permissions and limitations under the |
14 |
* License. When distributing the software, include this License Header |
15 |
* Notice in each file and include the License file at |
16 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this |
17 |
* particular file as subject to the "Classpath" exception as provided |
18 |
* by Sun in the GPL Version 2 section of the License file that |
19 |
* accompanied this code. If applicable, add the following below the |
20 |
* License Header, with the fields enclosed by brackets [] replaced by |
21 |
* your own identifying information: |
22 |
* "Portions Copyrighted [year] [name of copyright owner]" |
23 |
* |
24 |
* If you wish your version of this file to be governed by only the CDDL |
25 |
* or only the GPL Version 2, indicate your decision by adding |
26 |
* "[Contributor] elects to include this software in this distribution |
27 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
28 |
* single choice of license, a recipient has the option to distribute |
29 |
* your version of this file under either the CDDL, the GPL Version 2 or |
30 |
* to extend the choice of license to its licensees as provided above. |
31 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
32 |
* Version 2 license, then the option applies only if the new code is |
33 |
* made subject to such option by the copyright holder. |
34 |
* |
35 |
* Contributor(s): |
36 |
* |
37 |
* Portions Copyrighted 2010 Sun Microsystems, Inc. |
38 |
*/ |
39 |
|
40 |
package org.netbeans; |
41 |
|
42 |
import java.io.File; |
43 |
import java.io.IOException; |
44 |
import java.util.HashMap; |
45 |
import java.util.Map; |
46 |
import java.util.StringTokenizer; |
47 |
import org.netbeans.junit.NbTestCase; |
48 |
import org.netbeans.junit.MockServices; |
49 |
import org.openide.modules.InstalledFileLocator; |
50 |
import org.openide.util.lookup.ServiceProvider; |
51 |
|
52 |
/** |
53 |
* A test covering native library lookup mechanism. |
54 |
* @see http://bits.netbeans.org/dev/javadoc/org-openide-modules/org/openide/modules/doc-files/api.html#jni |
55 |
* |
56 |
* @author Petr Nejedly <pnejedly@netbeans.org> |
57 |
*/ |
58 |
public class NativeLibraryTest extends NbTestCase { |
59 |
private File userdir; |
60 |
private File ide; |
61 |
private File platform; |
62 |
private File install; |
63 |
|
64 |
private File jar; |
65 |
private Module module; |
66 |
|
67 |
File fullLib; |
68 |
File archLib; |
69 |
File commonLib; |
70 |
|
71 |
public NativeLibraryTest(String testName) { |
72 |
super(testName); |
73 |
} |
74 |
|
75 |
@Override |
76 |
protected void setUp() throws Exception { |
77 |
// MockServices.setServices(TestLocator.class); |
78 |
|
79 |
clearWorkDir(); |
80 |
|
81 |
String arch = System.getProperty("os.arch"); // NOI18N |
82 |
String system = System.getProperty("os.name").toLowerCase(); // NOI18N |
83 |
|
84 |
install = new File(getWorkDir(), "install"); |
85 |
platform = new File(install, "platform"); |
86 |
ide = new File(install, "ide"); |
87 |
userdir = new File(getWorkDir(), "tmp"); |
88 |
|
89 |
System.setProperty("netbeans.home", platform.getPath()); |
90 |
System.setProperty("netbeans.dirs", ide.getPath()); |
91 |
System.setProperty("netbeans.user", userdir.getPath()); |
92 |
|
93 |
jar = createModule("org.openide.sample", platform, |
94 |
"OpenIDE-Module-Public-Packages", "-", |
95 |
"OpenIDE-Module", "org.openide.sample" |
96 |
); |
97 |
|
98 |
root = jar.getParentFile().getParentFile(); |
99 |
|
100 |
// now create 3 libraries, full, arch and common |
101 |
fullLib = createFakeLibrary(jar, arch, system, System.mapLibraryName("full")); |
102 |
archLib = createFakeLibrary(jar, arch, System.mapLibraryName("arch")); |
103 |
commonLib = createFakeLibrary(jar, arch, System.mapLibraryName("common")); |
104 |
|
105 |
MockModuleInstaller installer = new MockModuleInstaller(); |
106 |
MockEvents ev = new MockEvents(); |
107 |
ModuleManager mgr = new ModuleManager(installer, ev); |
108 |
mgr.mutexPrivileged().enterWriteAccess(); |
109 |
module = mgr.create(jar, null, false, false, false); |
110 |
mgr.enable(module); |
111 |
mgr.mutexPrivileged().exitWriteAccess(); |
112 |
|
113 |
} |
114 |
|
115 |
public void testLocateLibrary() { |
116 |
// testing |
117 |
StandardModule.OneModuleClassLoader ldr = (StandardModule.OneModuleClassLoader)module.getClassLoader(); |
118 |
assertEquals(fullLib.getAbsolutePath(), ldr.findLibrary("full")); |
119 |
assertEquals(archLib.getAbsolutePath(), ldr.findLibrary("arch")); |
120 |
assertEquals(commonLib.getAbsolutePath(), ldr.findLibrary("common")); |
121 |
assertNull(ldr.findLibrary("none")); |
122 |
} |
123 |
|
124 |
private File createFakeLibrary(File jar, String... path) throws IOException { |
125 |
File out = new File(jar.getParentFile(), "lib"); |
126 |
for(String component : path) out = new File(out, component); |
127 |
|
128 |
// prepare the path |
129 |
out.getParentFile().mkdirs(); |
130 |
|
131 |
out.createNewFile(); |
132 |
return out; |
133 |
} |
134 |
|
135 |
|
136 |
private File createModule(String cnb, File cluster, String... attr) throws IOException { |
137 |
String dashes = cnb.replace('.', '-'); |
138 |
|
139 |
File tmp = new File(new File(cluster, "modules"), dashes + ".jar"); |
140 |
|
141 |
Map<String,String> attribs = new HashMap<String, String>(); |
142 |
for (int i = 0; i < attr.length; i += 2) { |
143 |
attribs.put(attr[i], attr[i + 1]); |
144 |
} |
145 |
|
146 |
Map<String,String> files = new HashMap<String, String>(); |
147 |
files.put("fake/" + cnb, cnb); |
148 |
|
149 |
tmp.getParentFile().mkdirs(); |
150 |
SetupHid.createJar(tmp, files, attribs); |
151 |
|
152 |
return tmp; |
153 |
} |
154 |
|
155 |
static File root; |
156 |
|
157 |
@ServiceProvider(service=InstalledFileLocator.class) |
158 |
public static final class TestLocator extends InstalledFileLocator { |
159 |
|
160 |
public TestLocator() { |
161 |
} |
162 |
|
163 |
public File locate(String relativePath, String codeNameBase, boolean localized) { |
164 |
StringTokenizer tk = new StringTokenizer(relativePath,"/"); |
165 |
File f = root; |
166 |
while (tk.hasMoreTokens()) { |
167 |
String part = tk.nextToken(); |
168 |
f = new File (f,part); |
169 |
if (!f.exists()) { |
170 |
return null; |
171 |
} |
172 |
} |
173 |
return f; |
174 |
} |
175 |
} |
176 |
|
177 |
} |