1343 |
15 Sep 05 |
nicklas |
1 |
/* |
1343 |
15 Sep 05 |
nicklas |
$Id$ |
1343 |
15 Sep 05 |
nicklas |
3 |
|
3675 |
16 Aug 07 |
jari |
Copyright (C) 2005 Nicklas Nordborg |
4889 |
06 Apr 09 |
nicklas |
Copyright (C) 2006 Jari Häkkinen |
1343 |
15 Sep 05 |
nicklas |
6 |
|
2304 |
22 May 06 |
jari |
This file is part of BASE - BioArray Software Environment. |
2304 |
22 May 06 |
jari |
Available at http://base.thep.lu.se/ |
1343 |
15 Sep 05 |
nicklas |
9 |
|
1343 |
15 Sep 05 |
nicklas |
BASE is free software; you can redistribute it and/or |
1343 |
15 Sep 05 |
nicklas |
modify it under the terms of the GNU General Public License |
4480 |
05 Sep 08 |
jari |
as published by the Free Software Foundation; either version 3 |
1343 |
15 Sep 05 |
nicklas |
of the License, or (at your option) any later version. |
1343 |
15 Sep 05 |
nicklas |
14 |
|
1343 |
15 Sep 05 |
nicklas |
BASE is distributed in the hope that it will be useful, |
1343 |
15 Sep 05 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
1343 |
15 Sep 05 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
1343 |
15 Sep 05 |
nicklas |
GNU General Public License for more details. |
1343 |
15 Sep 05 |
nicklas |
19 |
|
1343 |
15 Sep 05 |
nicklas |
You should have received a copy of the GNU General Public License |
4514 |
11 Sep 08 |
jari |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
1343 |
15 Sep 05 |
nicklas |
22 |
*/ |
5013 |
25 Jun 09 |
nicklas |
23 |
import net.sf.basedb.core.BaseException; |
7195 |
30 Aug 16 |
nicklas |
24 |
import net.sf.basedb.core.ItemNotFoundException; |
1343 |
15 Sep 05 |
nicklas |
25 |
import net.sf.basedb.util.JarClassLoader; |
1345 |
19 Sep 05 |
nicklas |
26 |
import java.net.URL; |
7195 |
30 Aug 16 |
nicklas |
27 |
import java.util.Enumeration; |
1345 |
19 Sep 05 |
nicklas |
28 |
import java.io.InputStreamReader; |
1345 |
19 Sep 05 |
nicklas |
29 |
import java.io.BufferedReader; |
1343 |
15 Sep 05 |
nicklas |
30 |
|
1343 |
15 Sep 05 |
nicklas |
31 |
public class TestJarClassLoader |
1343 |
15 Sep 05 |
nicklas |
32 |
{ |
1343 |
15 Sep 05 |
nicklas |
33 |
|
1343 |
15 Sep 05 |
nicklas |
34 |
static boolean ok = true; |
1343 |
15 Sep 05 |
nicklas |
35 |
public static void main(String[] args) |
1343 |
15 Sep 05 |
nicklas |
36 |
{ |
1343 |
15 Sep 05 |
nicklas |
37 |
TestUtil.checkArgs(args); |
1343 |
15 Sep 05 |
nicklas |
38 |
TestUtil.begin(); |
1343 |
15 Sep 05 |
nicklas |
39 |
ok = test_all(); |
1343 |
15 Sep 05 |
nicklas |
40 |
TestUtil.stop(); |
1343 |
15 Sep 05 |
nicklas |
41 |
} |
1343 |
15 Sep 05 |
nicklas |
42 |
|
1343 |
15 Sep 05 |
nicklas |
43 |
static boolean test_all() |
1343 |
15 Sep 05 |
nicklas |
44 |
{ |
1343 |
15 Sep 05 |
nicklas |
45 |
write("++Testing Jar class loader"); |
1343 |
15 Sep 05 |
nicklas |
46 |
|
6684 |
14 Jan 15 |
nicklas |
47 |
test_load("JarPlugin.jar", "JarPlugin", false, true, false); |
5394 |
25 Aug 10 |
nicklas |
48 |
test_resource("JarPlugin.jar", "META-INF/MANIFEST.MF", false, true); |
5013 |
25 Jun 09 |
nicklas |
49 |
|
5013 |
25 Jun 09 |
nicklas |
// Test parent delegation |
6684 |
14 Jan 15 |
nicklas |
51 |
test_load("JarPlugin.jar", "affymetrix.calvin.exception.CalvinException", false, true, false); |
5013 |
25 Jun 09 |
nicklas |
52 |
test_resource("JarPlugin.jar", "org/hibernate/hibernate-configuration-3.0.dtd", false, false); |
6684 |
14 Jan 15 |
nicklas |
53 |
test_load("JarPlugin.jar", "affymetrix.calvin.exception.CalvinException", true, false, true); |
5013 |
25 Jun 09 |
nicklas |
54 |
test_resource("JarPlugin.jar", "org/hibernate/hibernate-configuration-3.0.dtd", true, false); |
1343 |
15 Sep 05 |
nicklas |
55 |
|
7193 |
30 Aug 16 |
nicklas |
// Test side-loading of extensions |
7193 |
30 Aug 16 |
nicklas |
57 |
test_load("ExtensionThree.jar", "net.sf.basedb.test.extension.jar1.One", false, true, false); |
7195 |
30 Aug 16 |
nicklas |
58 |
test_resource("ExtensionThree.jar", "net/sf/basedb/test/extension/jar3/res.txt", false, true); |
7195 |
30 Aug 16 |
nicklas |
59 |
test_resource("ExtensionThree.jar", "net/sf/basedb/test/extension/jar1/res.txt", false, true); |
7195 |
30 Aug 16 |
nicklas |
60 |
test_resources("ExtensionThree.jar", "net/sf/basedb/test/extension/data/res.txt", false, 3); |
7193 |
30 Aug 16 |
nicklas |
61 |
|
1343 |
15 Sep 05 |
nicklas |
62 |
write("++Testing Jar class loader "+(ok ? "OK" : "Failed")+"\n"); |
1343 |
15 Sep 05 |
nicklas |
63 |
return ok; |
1343 |
15 Sep 05 |
nicklas |
64 |
} |
1343 |
15 Sep 05 |
nicklas |
65 |
|
5013 |
25 Jun 09 |
nicklas |
66 |
static void test_load(String jarPath, String className, |
6684 |
14 Jan 15 |
nicklas |
67 |
boolean delegateFirst, boolean expectLoadedByJarClassLoader, boolean useNewInstance) |
1343 |
15 Sep 05 |
nicklas |
68 |
{ |
1343 |
15 Sep 05 |
nicklas |
69 |
try |
1343 |
15 Sep 05 |
nicklas |
70 |
{ |
6684 |
14 Jan 15 |
nicklas |
71 |
JarClassLoader cl = useNewInstance ? |
6684 |
14 Jan 15 |
nicklas |
72 |
(JarClassLoader)JarClassLoader.newInstance(jarPath) : |
6684 |
14 Jan 15 |
nicklas |
73 |
(JarClassLoader)JarClassLoader.getInstance(jarPath); |
5013 |
25 Jun 09 |
nicklas |
74 |
cl.setDelegateFirst(delegateFirst); |
6875 |
20 Apr 15 |
nicklas |
75 |
Class<?> c = cl.loadClass(className); |
7517 |
02 Nov 18 |
nicklas |
76 |
Object o = c.getDeclaredConstructor().newInstance(); |
1343 |
15 Sep 05 |
nicklas |
77 |
write_item(o); |
5013 |
25 Jun 09 |
nicklas |
78 |
ClassLoader loadedBy = o.getClass().getClassLoader(); |
5013 |
25 Jun 09 |
nicklas |
79 |
boolean wasLoadedByJarClassLoader = loadedBy instanceof JarClassLoader; |
5013 |
25 Jun 09 |
nicklas |
80 |
if (wasLoadedByJarClassLoader != expectLoadedByJarClassLoader) |
5013 |
25 Jun 09 |
nicklas |
81 |
{ |
5013 |
25 Jun 09 |
nicklas |
82 |
throw new BaseException("Class loaded by incorrect class loader: " + loadedBy); |
5013 |
25 Jun 09 |
nicklas |
83 |
} |
5394 |
25 Aug 10 |
nicklas |
84 |
write("--Load class OK (" + className + ")"); |
1343 |
15 Sep 05 |
nicklas |
85 |
} |
1343 |
15 Sep 05 |
nicklas |
86 |
catch (Throwable ex) |
1343 |
15 Sep 05 |
nicklas |
87 |
{ |
5394 |
25 Aug 10 |
nicklas |
88 |
write("--Load class FAILED (" + className + ")"); |
1343 |
15 Sep 05 |
nicklas |
89 |
ex.printStackTrace(); |
1343 |
15 Sep 05 |
nicklas |
90 |
ok = false; |
1343 |
15 Sep 05 |
nicklas |
91 |
} |
1343 |
15 Sep 05 |
nicklas |
92 |
} |
1343 |
15 Sep 05 |
nicklas |
93 |
|
5013 |
25 Jun 09 |
nicklas |
94 |
static void test_resource(String jarPath, String name, boolean delegateFirst, boolean loadContent) |
1345 |
19 Sep 05 |
nicklas |
95 |
{ |
1345 |
19 Sep 05 |
nicklas |
96 |
try |
1345 |
19 Sep 05 |
nicklas |
97 |
{ |
5013 |
25 Jun 09 |
nicklas |
98 |
JarClassLoader cl = (JarClassLoader)JarClassLoader.getInstance(jarPath); |
5013 |
25 Jun 09 |
nicklas |
99 |
cl.setDelegateFirst(delegateFirst); |
1345 |
19 Sep 05 |
nicklas |
100 |
URL resource = cl.getResource(name); |
7195 |
30 Aug 16 |
nicklas |
101 |
if (resource == null) throw new ItemNotFoundException(name); |
1345 |
19 Sep 05 |
nicklas |
102 |
StringBuilder content = new StringBuilder(); |
5013 |
25 Jun 09 |
nicklas |
103 |
if (loadContent) |
1345 |
19 Sep 05 |
nicklas |
104 |
{ |
5013 |
25 Jun 09 |
nicklas |
105 |
BufferedReader in = new BufferedReader(new InputStreamReader(resource.openStream())); |
5013 |
25 Jun 09 |
nicklas |
106 |
String line = in.readLine(); |
5013 |
25 Jun 09 |
nicklas |
107 |
while (line != null) |
5013 |
25 Jun 09 |
nicklas |
108 |
{ |
5394 |
25 Aug 10 |
nicklas |
109 |
content.append(line).append("\n"); |
5013 |
25 Jun 09 |
nicklas |
110 |
line = in.readLine(); |
5013 |
25 Jun 09 |
nicklas |
111 |
} |
5013 |
25 Jun 09 |
nicklas |
112 |
in.close(); |
1345 |
19 Sep 05 |
nicklas |
113 |
} |
1345 |
19 Sep 05 |
nicklas |
114 |
|
1345 |
19 Sep 05 |
nicklas |
115 |
if (!TestUtil.getSilent()) |
1345 |
19 Sep 05 |
nicklas |
116 |
{ |
1345 |
19 Sep 05 |
nicklas |
117 |
write("Resource URL: "+resource); |
5013 |
25 Jun 09 |
nicklas |
118 |
if (loadContent) write("Resource content: "+content.toString()); |
1345 |
19 Sep 05 |
nicklas |
119 |
} |
5394 |
25 Aug 10 |
nicklas |
120 |
write("--Load resource OK (" + name + ")"); |
1345 |
19 Sep 05 |
nicklas |
121 |
} |
1345 |
19 Sep 05 |
nicklas |
122 |
catch (Throwable ex) |
1345 |
19 Sep 05 |
nicklas |
123 |
{ |
5394 |
25 Aug 10 |
nicklas |
124 |
write("--Load resource FAILED (" + name + ")"); |
1345 |
19 Sep 05 |
nicklas |
125 |
ex.printStackTrace(); |
1345 |
19 Sep 05 |
nicklas |
126 |
ok = false; |
1345 |
19 Sep 05 |
nicklas |
127 |
} |
1345 |
19 Sep 05 |
nicklas |
128 |
} |
1345 |
19 Sep 05 |
nicklas |
129 |
|
7195 |
30 Aug 16 |
nicklas |
130 |
static void test_resources(String jarPath, String name, boolean delegateFirst, int numExpected) |
7195 |
30 Aug 16 |
nicklas |
131 |
{ |
7195 |
30 Aug 16 |
nicklas |
132 |
try |
7195 |
30 Aug 16 |
nicklas |
133 |
{ |
7195 |
30 Aug 16 |
nicklas |
134 |
JarClassLoader cl = (JarClassLoader)JarClassLoader.getInstance(jarPath); |
7195 |
30 Aug 16 |
nicklas |
135 |
cl.setDelegateFirst(delegateFirst); |
7195 |
30 Aug 16 |
nicklas |
136 |
Enumeration<URL> resources = cl.getResources(name); |
7195 |
30 Aug 16 |
nicklas |
137 |
|
7195 |
30 Aug 16 |
nicklas |
138 |
int numResources = 0; |
7195 |
30 Aug 16 |
nicklas |
139 |
while (resources.hasMoreElements()) |
7195 |
30 Aug 16 |
nicklas |
140 |
{ |
7195 |
30 Aug 16 |
nicklas |
141 |
numResources++; |
7195 |
30 Aug 16 |
nicklas |
142 |
URL res = resources.nextElement(); |
7195 |
30 Aug 16 |
nicklas |
143 |
if (!TestUtil.getSilent()) |
7195 |
30 Aug 16 |
nicklas |
144 |
{ |
7195 |
30 Aug 16 |
nicklas |
145 |
write("Resource URL: "+res); |
7195 |
30 Aug 16 |
nicklas |
146 |
} |
7195 |
30 Aug 16 |
nicklas |
147 |
} |
7195 |
30 Aug 16 |
nicklas |
148 |
|
7195 |
30 Aug 16 |
nicklas |
149 |
if (numExpected > 0 && numExpected != numResources) |
7195 |
30 Aug 16 |
nicklas |
150 |
{ |
7195 |
30 Aug 16 |
nicklas |
151 |
throw new BaseException("Expected "+numExpected+" resources, not " + numResources); |
7195 |
30 Aug 16 |
nicklas |
152 |
} |
7195 |
30 Aug 16 |
nicklas |
153 |
|
7195 |
30 Aug 16 |
nicklas |
154 |
write("--Load resources OK (" + name + ")"); |
7195 |
30 Aug 16 |
nicklas |
155 |
} |
7195 |
30 Aug 16 |
nicklas |
156 |
catch (Throwable ex) |
7195 |
30 Aug 16 |
nicklas |
157 |
{ |
7195 |
30 Aug 16 |
nicklas |
158 |
write("--Load resources FAILED (" + name + ")"); |
7195 |
30 Aug 16 |
nicklas |
159 |
ex.printStackTrace(); |
7195 |
30 Aug 16 |
nicklas |
160 |
ok = false; |
7195 |
30 Aug 16 |
nicklas |
161 |
} |
7195 |
30 Aug 16 |
nicklas |
162 |
} |
7195 |
30 Aug 16 |
nicklas |
163 |
|
1345 |
19 Sep 05 |
nicklas |
164 |
|
1343 |
15 Sep 05 |
nicklas |
165 |
static void write(String message) |
1343 |
15 Sep 05 |
nicklas |
166 |
{ |
1343 |
15 Sep 05 |
nicklas |
167 |
System.out.println(message); |
1343 |
15 Sep 05 |
nicklas |
168 |
} |
1343 |
15 Sep 05 |
nicklas |
169 |
|
1343 |
15 Sep 05 |
nicklas |
170 |
static void write_item(Object o) |
1343 |
15 Sep 05 |
nicklas |
171 |
{ |
1343 |
15 Sep 05 |
nicklas |
172 |
if (!TestUtil.getSilent()) |
1343 |
15 Sep 05 |
nicklas |
173 |
{ |
6875 |
20 Apr 15 |
nicklas |
174 |
Class<?> c = o.getClass(); |
5394 |
25 Aug 10 |
nicklas |
175 |
write("Object '"+o+"' of class '"+c.getName()+"' in package '" + c.getPackage() + |
5394 |
25 Aug 10 |
nicklas |
176 |
"' loaded with class loader: " + c.getClassLoader()); |
1343 |
15 Sep 05 |
nicklas |
177 |
} |
1343 |
15 Sep 05 |
nicklas |
178 |
} |
1343 |
15 Sep 05 |
nicklas |
179 |
|
1343 |
15 Sep 05 |
nicklas |
180 |
} |
1343 |
15 Sep 05 |
nicklas |
181 |
|