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 org.netbeans.junit.NbTestCase; |
47 |
|
48 |
/** |
49 |
* A test covering native library lookup mechanism. |
50 |
* @see http://bits.netbeans.org/dev/javadoc/org-openide-modules/org/openide/modules/doc-files/api.html#jni |
51 |
* |
52 |
* @author Petr Nejedly <pnejedly@netbeans.org> |
53 |
*/ |
54 |
public class NativeLibraryTest extends NbTestCase { |
55 |
private File userdir; |
56 |
private File ide; |
57 |
private File platform; |
58 |
private File install; |
59 |
|
60 |
private File jar; |
61 |
private Module module; |
62 |
|
63 |
File fullLib; |
64 |
File archLib; |
65 |
File commonLib; |
66 |
|
67 |
public NativeLibraryTest(String testName) { |
68 |
super(testName); |
69 |
} |
70 |
|
71 |
@Override |
72 |
protected void setUp() throws Exception { |
73 |
clearWorkDir(); |
74 |
|
75 |
String arch = System.getProperty("os.arch"); // NOI18N |
76 |
String system = System.getProperty("os.name").toLowerCase(); // NOI18N |
77 |
|
78 |
install = new File(getWorkDir(), "install"); |
79 |
platform = new File(install, "platform"); |
80 |
ide = new File(install, "ide"); |
81 |
userdir = new File(getWorkDir(), "tmp"); |
82 |
|
83 |
System.setProperty("netbeans.home", platform.getPath()); |
84 |
System.setProperty("netbeans.dirs", ide.getPath()); |
85 |
System.setProperty("netbeans.user", userdir.getPath()); |
86 |
|
87 |
jar = createModule("org.openide.sample", platform, |
88 |
"OpenIDE-Module-Public-Packages", "-", |
89 |
"OpenIDE-Module", "org.openide.sample" |
90 |
); |
91 |
|
92 |
// now create 3 libraries, full, arch and common |
93 |
fullLib = createFakeLibrary(jar, arch, system, System.mapLibraryName("full")); |
94 |
archLib = createFakeLibrary(jar, arch, System.mapLibraryName("arch")); |
95 |
commonLib = createFakeLibrary(jar, arch, System.mapLibraryName("common")); |
96 |
|
97 |
MockModuleInstaller installer = new MockModuleInstaller(); |
98 |
MockEvents ev = new MockEvents(); |
99 |
ModuleManager mgr = new ModuleManager(installer, ev); |
100 |
mgr.mutexPrivileged().enterWriteAccess(); |
101 |
module = mgr.create(jar, null, false, false, false); |
102 |
mgr.enable(module); |
103 |
mgr.mutexPrivileged().exitWriteAccess(); |
104 |
|
105 |
} |
106 |
|
107 |
public void testLocateLibrary() { |
108 |
// testing |
109 |
StandardModule.OneModuleClassLoader ldr = (StandardModule.OneModuleClassLoader)module.getClassLoader(); |
110 |
assertEquals(fullLib.getAbsolutePath(), ldr.findLibrary("full")); |
111 |
assertEquals(archLib.getAbsolutePath(), ldr.findLibrary("arch")); |
112 |
assertEquals(commonLib.getAbsolutePath(), ldr.findLibrary("common")); |
113 |
assertNull(ldr.findLibrary("none")); |
114 |
} |
115 |
|
116 |
private File createFakeLibrary(File jar, String... path) throws IOException { |
117 |
File out = new File(jar.getParentFile(), "lib"); |
118 |
for(String component : path) out = new File(out, component); |
119 |
|
120 |
// prepare the path |
121 |
out.getParentFile().mkdirs(); |
122 |
|
123 |
out.createNewFile(); |
124 |
return out; |
125 |
} |
126 |
|
127 |
|
128 |
private File createModule(String cnb, File cluster, String... attr) throws IOException { |
129 |
String dashes = cnb.replace('.', '-'); |
130 |
|
131 |
File tmp = new File(new File(cluster, "modules"), dashes + ".jar"); |
132 |
|
133 |
Map<String,String> attribs = new HashMap<String, String>(); |
134 |
for (int i = 0; i < attr.length; i += 2) { |
135 |
attribs.put(attr[i], attr[i + 1]); |
136 |
} |
137 |
|
138 |
Map<String,String> files = new HashMap<String, String>(); |
139 |
files.put("fake/" + cnb, cnb); |
140 |
|
141 |
tmp.getParentFile().mkdirs(); |
142 |
SetupHid.createJar(tmp, files, attribs); |
143 |
|
144 |
return tmp; |
145 |
} |
146 |
|
147 |
} |