36511
|
1 |
/*
|
|
2 |
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
* @test
|
|
26 |
* @summary Test a pool containing jimage resources and classes.
|
|
27 |
* @author Jean-Francois Denise
|
|
28 |
* @modules jdk.jlink/jdk.tools.jlink.internal
|
|
29 |
* @run build ResourcePoolTest
|
|
30 |
* @run main ResourcePoolTest
|
|
31 |
*/
|
|
32 |
|
|
33 |
import java.io.ByteArrayInputStream;
|
|
34 |
import java.nio.ByteBuffer;
|
|
35 |
import java.nio.ByteOrder;
|
|
36 |
import java.util.ArrayList;
|
|
37 |
import java.util.Collection;
|
|
38 |
import java.util.HashSet;
|
|
39 |
import java.util.List;
|
|
40 |
import java.util.Set;
|
|
41 |
|
|
42 |
import jdk.tools.jlink.internal.PoolImpl;
|
|
43 |
import jdk.tools.jlink.plugin.Pool;
|
|
44 |
import jdk.tools.jlink.plugin.Pool.Module;
|
|
45 |
import jdk.tools.jlink.plugin.Pool.ModuleData;
|
|
46 |
import jdk.tools.jlink.plugin.Pool.ModuleDataType;
|
|
47 |
import jdk.tools.jlink.plugin.Pool.Visitor;
|
|
48 |
|
|
49 |
public class ResourcePoolTest {
|
|
50 |
|
|
51 |
public static void main(String[] args) throws Exception {
|
|
52 |
new ResourcePoolTest().test();
|
|
53 |
}
|
|
54 |
|
|
55 |
public void test() throws Exception {
|
|
56 |
checkResourceAdding();
|
|
57 |
checkResourceVisitor();
|
|
58 |
checkResourcesAfterCompression();
|
|
59 |
}
|
|
60 |
|
|
61 |
private static final String SUFFIX = "END";
|
|
62 |
|
|
63 |
private void checkResourceVisitor() throws Exception {
|
|
64 |
Pool input = new PoolImpl();
|
|
65 |
for (int i = 0; i < 1000; ++i) {
|
|
66 |
String module = "/module" + (i / 10);
|
|
67 |
String resourcePath = module + "/java/package" + i;
|
|
68 |
byte[] bytes = resourcePath.getBytes();
|
|
69 |
input.add(new ModuleData(module, resourcePath,
|
|
70 |
ModuleDataType.CLASS_OR_RESOURCE,
|
|
71 |
new ByteArrayInputStream(bytes), bytes.length));
|
|
72 |
}
|
|
73 |
Pool output = new PoolImpl();
|
|
74 |
ResourceVisitor visitor = new ResourceVisitor();
|
|
75 |
input.visit(visitor, output);
|
|
76 |
if (visitor.getAmountBefore() == 0) {
|
|
77 |
throw new AssertionError("Resources not found");
|
|
78 |
}
|
|
79 |
if (visitor.getAmountBefore() != input.getContent().size()) {
|
|
80 |
throw new AssertionError("Number of visited resources. Expected: " +
|
|
81 |
visitor.getAmountBefore() + ", got: " + input.getContent().size());
|
|
82 |
}
|
|
83 |
if (visitor.getAmountAfter() != output.getContent().size()) {
|
|
84 |
throw new AssertionError("Number of added resources. Expected: " +
|
|
85 |
visitor.getAmountAfter() + ", got: " + output.getContent().size());
|
|
86 |
}
|
|
87 |
for (ModuleData outResource : output.getContent()) {
|
|
88 |
String path = outResource.getPath().replaceAll(SUFFIX + "$", "");
|
|
89 |
ModuleData inResource = input.get(path);
|
|
90 |
if (inResource == null) {
|
|
91 |
throw new AssertionError("Unknown resource: " + path);
|
|
92 |
}
|
|
93 |
}
|
|
94 |
}
|
|
95 |
|
|
96 |
private static class ResourceVisitor implements Visitor {
|
|
97 |
|
|
98 |
private int amountBefore;
|
|
99 |
private int amountAfter;
|
|
100 |
|
|
101 |
@Override
|
|
102 |
public ModuleData visit(ModuleData resource) {
|
|
103 |
int index = ++amountBefore % 3;
|
|
104 |
switch (index) {
|
|
105 |
case 0:
|
|
106 |
++amountAfter;
|
|
107 |
return new ModuleData(resource.getModule(), resource.getPath() + SUFFIX,
|
|
108 |
resource.getType(), resource.stream(), resource.getLength());
|
|
109 |
case 1:
|
|
110 |
++amountAfter;
|
|
111 |
return new ModuleData(resource.getModule(), resource.getPath(),
|
|
112 |
resource.getType(), resource.stream(), resource.getLength());
|
|
113 |
}
|
|
114 |
return null;
|
|
115 |
}
|
|
116 |
|
|
117 |
public int getAmountAfter() {
|
|
118 |
return amountAfter;
|
|
119 |
}
|
|
120 |
|
|
121 |
public int getAmountBefore() {
|
|
122 |
return amountBefore;
|
|
123 |
}
|
|
124 |
}
|
|
125 |
|
|
126 |
private void checkResourceAdding() {
|
|
127 |
List<String> samples = new ArrayList<>();
|
|
128 |
samples.add("java.base");
|
|
129 |
samples.add("java/lang/Object");
|
|
130 |
samples.add("java.base");
|
|
131 |
samples.add("java/lang/String");
|
|
132 |
samples.add("java.management");
|
|
133 |
samples.add("javax/management/ObjectName");
|
|
134 |
test(samples, (resources, module, path) -> {
|
|
135 |
try {
|
|
136 |
resources.add(new ModuleData(module, path,
|
|
137 |
ModuleDataType.CLASS_OR_RESOURCE,
|
|
138 |
new ByteArrayInputStream(new byte[0]), 0));
|
|
139 |
} catch (Exception ex) {
|
|
140 |
throw new RuntimeException(ex);
|
|
141 |
}
|
|
142 |
});
|
|
143 |
test(samples, (resources, module, path) -> {
|
|
144 |
try {
|
|
145 |
resources.add(PoolImpl.
|
|
146 |
newCompressedResource(new ModuleData(module, path,
|
|
147 |
ModuleDataType.CLASS_OR_RESOURCE,
|
|
148 |
new ByteArrayInputStream(new byte[0]), 0),
|
|
149 |
ByteBuffer.allocate(99), "bitcruncher", null,
|
|
150 |
((PoolImpl)resources).getStringTable(), ByteOrder.nativeOrder()));
|
|
151 |
} catch (Exception ex) {
|
|
152 |
throw new RuntimeException(ex);
|
|
153 |
}
|
|
154 |
});
|
|
155 |
}
|
|
156 |
|
|
157 |
private void test(List<String> samples, ResourceAdder adder) {
|
|
158 |
if (samples.isEmpty()) {
|
|
159 |
throw new AssertionError("No sample to test");
|
|
160 |
}
|
|
161 |
Pool resources = new PoolImpl();
|
|
162 |
Set<String> modules = new HashSet<>();
|
|
163 |
for (int i = 0; i < samples.size(); i++) {
|
|
164 |
String module = samples.get(i);
|
|
165 |
modules.add(module);
|
|
166 |
i++;
|
|
167 |
String clazz = samples.get(i);
|
|
168 |
String path = "/" + module + "/" + clazz + ".class";
|
|
169 |
adder.add(resources, module, path);
|
|
170 |
}
|
|
171 |
for (int i = 0; i < samples.size(); i++) {
|
|
172 |
String module = samples.get(i);
|
|
173 |
i++;
|
|
174 |
String clazz = samples.get(i);
|
|
175 |
String path = "/" + module + "/" + clazz + ".class";
|
|
176 |
ModuleData res = resources.get(path);
|
|
177 |
checkModule(resources, res);
|
|
178 |
if (res == null) {
|
|
179 |
throw new AssertionError("Resource not found " + path);
|
|
180 |
}
|
|
181 |
ModuleData res2 = resources.get(clazz);
|
|
182 |
if (res2 != null) {
|
|
183 |
throw new AssertionError("Resource found " + clazz);
|
|
184 |
}
|
|
185 |
}
|
|
186 |
if (resources.getContent().size() != samples.size() / 2) {
|
|
187 |
throw new AssertionError("Invalid number of resources");
|
|
188 |
}
|
|
189 |
}
|
|
190 |
|
|
191 |
private void checkModule(Pool resources, ModuleData res) {
|
|
192 |
Module m = resources.getModule(res.getModule());
|
|
193 |
if (m == null) {
|
|
194 |
throw new AssertionError("No module " + res.getModule());
|
|
195 |
}
|
|
196 |
if (!m.getName().equals(res.getModule())) {
|
|
197 |
throw new AssertionError("Not right module name " + res.getModule());
|
|
198 |
}
|
|
199 |
if (m.get(res.getPath()) == null) {
|
|
200 |
throw new AssertionError("resource " + res.getPath()
|
|
201 |
+ " not in module " + m.getName());
|
|
202 |
}
|
|
203 |
}
|
|
204 |
|
|
205 |
private void checkResourcesAfterCompression() throws Exception {
|
|
206 |
PoolImpl resources1 = new PoolImpl();
|
|
207 |
ModuleData res1 = new ModuleData("module1", "/module1/toto1",
|
|
208 |
ModuleDataType.CLASS_OR_RESOURCE,
|
|
209 |
new ByteArrayInputStream(new byte[0]), 0);
|
|
210 |
ModuleData res2 = new ModuleData("module2", "/module2/toto1",
|
|
211 |
ModuleDataType.CLASS_OR_RESOURCE,
|
|
212 |
new ByteArrayInputStream(new byte[0]), 0);
|
|
213 |
resources1.add(res1);
|
|
214 |
resources1.add(res2);
|
|
215 |
|
|
216 |
checkResources(resources1, res1, res2);
|
|
217 |
Pool resources2 = new PoolImpl();
|
|
218 |
ModuleData res3 = new ModuleData("module2", "/module2/toto1",
|
|
219 |
ModuleDataType.CLASS_OR_RESOURCE,
|
|
220 |
new ByteArrayInputStream(new byte[7]), 7);
|
|
221 |
resources2.add(res3);
|
|
222 |
resources2.add(PoolImpl.newCompressedResource(res1,
|
|
223 |
ByteBuffer.allocate(7), "zip", null, resources1.getStringTable(),
|
|
224 |
ByteOrder.nativeOrder()));
|
|
225 |
checkResources(resources2, res1, res2);
|
|
226 |
}
|
|
227 |
|
|
228 |
private void checkResources(Pool resources, ModuleData... expected) {
|
|
229 |
Collection<Module> ms = resources.getModules();
|
|
230 |
List<String> modules = new ArrayList();
|
|
231 |
for(Module m : ms) {
|
|
232 |
modules.add(m.getName());
|
|
233 |
}
|
|
234 |
for (ModuleData res : expected) {
|
|
235 |
if (!resources.contains(res)) {
|
|
236 |
throw new AssertionError("Resource not found: " + res);
|
|
237 |
}
|
|
238 |
|
|
239 |
if (resources.get(res.getPath()) == null) {
|
|
240 |
throw new AssertionError("Resource not found: " + res);
|
|
241 |
}
|
|
242 |
|
|
243 |
if (!modules.contains(res.getModule())) {
|
|
244 |
throw new AssertionError("Module not found: " + res.getModule());
|
|
245 |
}
|
|
246 |
|
|
247 |
if (!resources.getContent().contains(res)) {
|
|
248 |
throw new AssertionError("Resources not found: " + res);
|
|
249 |
}
|
|
250 |
|
|
251 |
try {
|
|
252 |
resources.add(res);
|
|
253 |
throw new AssertionError(res + " already present, but an exception is not thrown");
|
|
254 |
} catch (Exception ex) {
|
|
255 |
// Expected
|
|
256 |
}
|
|
257 |
}
|
|
258 |
|
|
259 |
if (resources.isReadOnly()) {
|
|
260 |
throw new AssertionError("ReadOnly resources");
|
|
261 |
}
|
|
262 |
|
|
263 |
((PoolImpl) resources).setReadOnly();
|
|
264 |
try {
|
|
265 |
resources.add(new ModuleData("module2", "/module2/toto1",
|
|
266 |
ModuleDataType.CLASS_OR_RESOURCE, new ByteArrayInputStream(new byte[0]), 0));
|
|
267 |
throw new AssertionError("Pool is read-only, but an exception is not thrown");
|
|
268 |
} catch (Exception ex) {
|
|
269 |
// Expected
|
|
270 |
}
|
|
271 |
}
|
|
272 |
|
|
273 |
interface ResourceAdder {
|
|
274 |
void add(Pool resources, String module, String path);
|
|
275 |
}
|
|
276 |
}
|