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;
|
38320
|
40 |
import java.util.Optional;
|
36511
|
41 |
import java.util.Set;
|
38320
|
42 |
import java.util.function.Function;
|
|
43 |
import jdk.tools.jlink.internal.ModulePoolImpl;
|
|
44 |
import jdk.tools.jlink.plugin.ModulePool;
|
|
45 |
import jdk.tools.jlink.plugin.LinkModule;
|
|
46 |
import jdk.tools.jlink.plugin.ModuleEntry;
|
|
47 |
import jdk.tools.jlink.plugin.ModulePool;
|
36511
|
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 {
|
38320
|
64 |
ModulePool input = new ModulePoolImpl();
|
36511
|
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();
|
38320
|
69 |
input.add(ModuleEntry.create(module, resourcePath,
|
|
70 |
ModuleEntry.Type.CLASS_OR_RESOURCE,
|
36511
|
71 |
new ByteArrayInputStream(bytes), bytes.length));
|
|
72 |
}
|
38320
|
73 |
ModulePool output = new ModulePoolImpl();
|
36511
|
74 |
ResourceVisitor visitor = new ResourceVisitor();
|
38320
|
75 |
input.transformAndCopy(visitor, output);
|
36511
|
76 |
if (visitor.getAmountBefore() == 0) {
|
|
77 |
throw new AssertionError("Resources not found");
|
|
78 |
}
|
38320
|
79 |
if (visitor.getAmountBefore() != input.getEntryCount()) {
|
36511
|
80 |
throw new AssertionError("Number of visited resources. Expected: " +
|
38320
|
81 |
visitor.getAmountBefore() + ", got: " + input.getEntryCount());
|
36511
|
82 |
}
|
38320
|
83 |
if (visitor.getAmountAfter() != output.getEntryCount()) {
|
36511
|
84 |
throw new AssertionError("Number of added resources. Expected: " +
|
38320
|
85 |
visitor.getAmountAfter() + ", got: " + output.getEntryCount());
|
36511
|
86 |
}
|
38320
|
87 |
output.entries().forEach(outResource -> {
|
36511
|
88 |
String path = outResource.getPath().replaceAll(SUFFIX + "$", "");
|
38320
|
89 |
if (!input.findEntry(path).isPresent()) {
|
36511
|
90 |
throw new AssertionError("Unknown resource: " + path);
|
|
91 |
}
|
38320
|
92 |
});
|
36511
|
93 |
}
|
|
94 |
|
38320
|
95 |
private static class ResourceVisitor implements Function<ModuleEntry, ModuleEntry> {
|
36511
|
96 |
|
|
97 |
private int amountBefore;
|
|
98 |
private int amountAfter;
|
|
99 |
|
|
100 |
@Override
|
38320
|
101 |
public ModuleEntry apply(ModuleEntry resource) {
|
36511
|
102 |
int index = ++amountBefore % 3;
|
|
103 |
switch (index) {
|
|
104 |
case 0:
|
|
105 |
++amountAfter;
|
38320
|
106 |
return ModuleEntry.create(resource.getModule(), resource.getPath() + SUFFIX,
|
36511
|
107 |
resource.getType(), resource.stream(), resource.getLength());
|
|
108 |
case 1:
|
|
109 |
++amountAfter;
|
38320
|
110 |
return ModuleEntry.create(resource.getModule(), resource.getPath(),
|
36511
|
111 |
resource.getType(), resource.stream(), resource.getLength());
|
|
112 |
}
|
|
113 |
return null;
|
|
114 |
}
|
|
115 |
|
|
116 |
public int getAmountAfter() {
|
|
117 |
return amountAfter;
|
|
118 |
}
|
|
119 |
|
|
120 |
public int getAmountBefore() {
|
|
121 |
return amountBefore;
|
|
122 |
}
|
|
123 |
}
|
|
124 |
|
|
125 |
private void checkResourceAdding() {
|
|
126 |
List<String> samples = new ArrayList<>();
|
|
127 |
samples.add("java.base");
|
|
128 |
samples.add("java/lang/Object");
|
|
129 |
samples.add("java.base");
|
|
130 |
samples.add("java/lang/String");
|
|
131 |
samples.add("java.management");
|
|
132 |
samples.add("javax/management/ObjectName");
|
|
133 |
test(samples, (resources, module, path) -> {
|
|
134 |
try {
|
38320
|
135 |
resources.add(ModuleEntry.create(module, path,
|
|
136 |
ModuleEntry.Type.CLASS_OR_RESOURCE,
|
36511
|
137 |
new ByteArrayInputStream(new byte[0]), 0));
|
|
138 |
} catch (Exception ex) {
|
|
139 |
throw new RuntimeException(ex);
|
|
140 |
}
|
|
141 |
});
|
|
142 |
test(samples, (resources, module, path) -> {
|
|
143 |
try {
|
38320
|
144 |
resources.add(ModulePoolImpl.
|
|
145 |
newCompressedResource(ModuleEntry.create(module, path,
|
|
146 |
ModuleEntry.Type.CLASS_OR_RESOURCE,
|
36511
|
147 |
new ByteArrayInputStream(new byte[0]), 0),
|
|
148 |
ByteBuffer.allocate(99), "bitcruncher", null,
|
38320
|
149 |
((ModulePoolImpl)resources).getStringTable(), ByteOrder.nativeOrder()));
|
36511
|
150 |
} catch (Exception ex) {
|
|
151 |
throw new RuntimeException(ex);
|
|
152 |
}
|
|
153 |
});
|
|
154 |
}
|
|
155 |
|
|
156 |
private void test(List<String> samples, ResourceAdder adder) {
|
|
157 |
if (samples.isEmpty()) {
|
|
158 |
throw new AssertionError("No sample to test");
|
|
159 |
}
|
38320
|
160 |
ModulePool resources = new ModulePoolImpl();
|
36511
|
161 |
Set<String> modules = new HashSet<>();
|
|
162 |
for (int i = 0; i < samples.size(); i++) {
|
|
163 |
String module = samples.get(i);
|
|
164 |
modules.add(module);
|
|
165 |
i++;
|
|
166 |
String clazz = samples.get(i);
|
|
167 |
String path = "/" + module + "/" + clazz + ".class";
|
|
168 |
adder.add(resources, module, path);
|
|
169 |
}
|
|
170 |
for (int i = 0; i < samples.size(); i++) {
|
|
171 |
String module = samples.get(i);
|
|
172 |
i++;
|
|
173 |
String clazz = samples.get(i);
|
|
174 |
String path = "/" + module + "/" + clazz + ".class";
|
38320
|
175 |
Optional<ModuleEntry> res = resources.findEntry(path);
|
|
176 |
if (!res.isPresent()) {
|
36511
|
177 |
throw new AssertionError("Resource not found " + path);
|
|
178 |
}
|
38320
|
179 |
checkModule(resources, res.get());
|
|
180 |
if (resources.findEntry(clazz).isPresent()) {
|
36511
|
181 |
throw new AssertionError("Resource found " + clazz);
|
|
182 |
}
|
|
183 |
}
|
38320
|
184 |
if (resources.getEntryCount() != samples.size() / 2) {
|
36511
|
185 |
throw new AssertionError("Invalid number of resources");
|
|
186 |
}
|
|
187 |
}
|
|
188 |
|
38320
|
189 |
private void checkModule(ModulePool resources, ModuleEntry res) {
|
|
190 |
Optional<LinkModule> optMod = resources.findModule(res.getModule());
|
|
191 |
if (!optMod.isPresent()) {
|
36511
|
192 |
throw new AssertionError("No module " + res.getModule());
|
|
193 |
}
|
38320
|
194 |
LinkModule m = optMod.get();
|
36511
|
195 |
if (!m.getName().equals(res.getModule())) {
|
|
196 |
throw new AssertionError("Not right module name " + res.getModule());
|
|
197 |
}
|
38320
|
198 |
if (!m.findEntry(res.getPath()).isPresent()) {
|
36511
|
199 |
throw new AssertionError("resource " + res.getPath()
|
|
200 |
+ " not in module " + m.getName());
|
|
201 |
}
|
|
202 |
}
|
|
203 |
|
|
204 |
private void checkResourcesAfterCompression() throws Exception {
|
38320
|
205 |
ModulePoolImpl resources1 = new ModulePoolImpl();
|
|
206 |
ModuleEntry res1 = ModuleEntry.create("module1", "/module1/toto1",
|
|
207 |
ModuleEntry.Type.CLASS_OR_RESOURCE,
|
36511
|
208 |
new ByteArrayInputStream(new byte[0]), 0);
|
38320
|
209 |
ModuleEntry res2 = ModuleEntry.create("module2", "/module2/toto1",
|
|
210 |
ModuleEntry.Type.CLASS_OR_RESOURCE,
|
36511
|
211 |
new ByteArrayInputStream(new byte[0]), 0);
|
|
212 |
resources1.add(res1);
|
|
213 |
resources1.add(res2);
|
|
214 |
|
|
215 |
checkResources(resources1, res1, res2);
|
38320
|
216 |
ModulePool resources2 = new ModulePoolImpl();
|
|
217 |
ModuleEntry res3 = ModuleEntry.create("module2", "/module2/toto1",
|
|
218 |
ModuleEntry.Type.CLASS_OR_RESOURCE,
|
36511
|
219 |
new ByteArrayInputStream(new byte[7]), 7);
|
|
220 |
resources2.add(res3);
|
38320
|
221 |
resources2.add(ModulePoolImpl.newCompressedResource(res1,
|
36511
|
222 |
ByteBuffer.allocate(7), "zip", null, resources1.getStringTable(),
|
|
223 |
ByteOrder.nativeOrder()));
|
|
224 |
checkResources(resources2, res1, res2);
|
|
225 |
}
|
|
226 |
|
38320
|
227 |
private void checkResources(ModulePool resources, ModuleEntry... expected) {
|
36511
|
228 |
List<String> modules = new ArrayList();
|
38320
|
229 |
resources.modules().forEach(m -> {
|
36511
|
230 |
modules.add(m.getName());
|
38320
|
231 |
});
|
|
232 |
for (ModuleEntry res : expected) {
|
36511
|
233 |
if (!resources.contains(res)) {
|
|
234 |
throw new AssertionError("Resource not found: " + res);
|
|
235 |
}
|
|
236 |
|
38320
|
237 |
if (!resources.findEntry(res.getPath()).isPresent()) {
|
36511
|
238 |
throw new AssertionError("Resource not found: " + res);
|
|
239 |
}
|
|
240 |
|
|
241 |
if (!modules.contains(res.getModule())) {
|
|
242 |
throw new AssertionError("Module not found: " + res.getModule());
|
|
243 |
}
|
|
244 |
|
38320
|
245 |
if (!resources.contains(res)) {
|
36511
|
246 |
throw new AssertionError("Resources not found: " + res);
|
|
247 |
}
|
|
248 |
|
|
249 |
try {
|
|
250 |
resources.add(res);
|
|
251 |
throw new AssertionError(res + " already present, but an exception is not thrown");
|
|
252 |
} catch (Exception ex) {
|
|
253 |
// Expected
|
|
254 |
}
|
|
255 |
}
|
|
256 |
|
|
257 |
if (resources.isReadOnly()) {
|
|
258 |
throw new AssertionError("ReadOnly resources");
|
|
259 |
}
|
|
260 |
|
38320
|
261 |
((ModulePoolImpl) resources).setReadOnly();
|
36511
|
262 |
try {
|
38320
|
263 |
resources.add(ModuleEntry.create("module2", "/module2/toto1",
|
|
264 |
ModuleEntry.Type.CLASS_OR_RESOURCE, new ByteArrayInputStream(new byte[0]), 0));
|
|
265 |
throw new AssertionError("ModulePool is read-only, but an exception is not thrown");
|
36511
|
266 |
} catch (Exception ex) {
|
|
267 |
// Expected
|
|
268 |
}
|
|
269 |
}
|
|
270 |
|
|
271 |
interface ResourceAdder {
|
38320
|
272 |
void add(ModulePool resources, String module, String path);
|
36511
|
273 |
}
|
|
274 |
}
|