author | duke |
Wed, 05 Jul 2017 22:06:37 +0200 | |
changeset 40471 | ad45133ff99e |
parent 39834 | 53a6fb443c20 |
child 43185 | d75d9ff8d4e7 |
permissions | -rw-r--r-- |
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; |
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
43 |
import jdk.tools.jlink.internal.ResourcePoolManager; |
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
44 |
import jdk.tools.jlink.plugin.ResourcePool; |
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
45 |
import jdk.tools.jlink.plugin.ResourcePoolModule; |
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
46 |
import jdk.tools.jlink.plugin.ResourcePool; |
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
47 |
import jdk.tools.jlink.plugin.ResourcePoolEntry; |
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 { |
|
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
64 |
ResourcePoolManager input = new ResourcePoolManager(); |
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(); |
|
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
69 |
input.add(ResourcePoolEntry.create(resourcePath, bytes)); |
36511 | 70 |
} |
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
71 |
ResourcePoolManager output = new ResourcePoolManager(); |
36511 | 72 |
ResourceVisitor visitor = new ResourceVisitor(); |
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
73 |
input.resourcePool().transformAndCopy(visitor, output.resourcePoolBuilder()); |
36511 | 74 |
if (visitor.getAmountBefore() == 0) { |
75 |
throw new AssertionError("Resources not found"); |
|
76 |
} |
|
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
77 |
if (visitor.getAmountBefore() != input.entryCount()) { |
36511 | 78 |
throw new AssertionError("Number of visited resources. Expected: " + |
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
79 |
visitor.getAmountBefore() + ", got: " + input.entryCount()); |
36511 | 80 |
} |
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
81 |
if (visitor.getAmountAfter() != output.entryCount()) { |
36511 | 82 |
throw new AssertionError("Number of added resources. Expected: " + |
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
83 |
visitor.getAmountAfter() + ", got: " + output.entryCount()); |
36511 | 84 |
} |
38320 | 85 |
output.entries().forEach(outResource -> { |
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
86 |
String path = outResource.path().replaceAll(SUFFIX + "$", ""); |
38320 | 87 |
if (!input.findEntry(path).isPresent()) { |
36511 | 88 |
throw new AssertionError("Unknown resource: " + path); |
89 |
} |
|
38320 | 90 |
}); |
36511 | 91 |
} |
92 |
||
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
93 |
private static class ResourceVisitor implements Function<ResourcePoolEntry, ResourcePoolEntry> { |
36511 | 94 |
|
95 |
private int amountBefore; |
|
96 |
private int amountAfter; |
|
97 |
||
98 |
@Override |
|
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
99 |
public ResourcePoolEntry apply(ResourcePoolEntry resource) { |
36511 | 100 |
int index = ++amountBefore % 3; |
101 |
switch (index) { |
|
102 |
case 0: |
|
103 |
++amountAfter; |
|
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
104 |
return ResourcePoolEntry.create(resource.path() + SUFFIX, |
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
105 |
resource.type(), resource.contentBytes()); |
36511 | 106 |
case 1: |
107 |
++amountAfter; |
|
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
108 |
return resource.copyWithContent(resource.contentBytes()); |
36511 | 109 |
} |
110 |
return null; |
|
111 |
} |
|
112 |
||
113 |
public int getAmountAfter() { |
|
114 |
return amountAfter; |
|
115 |
} |
|
116 |
||
117 |
public int getAmountBefore() { |
|
118 |
return amountBefore; |
|
119 |
} |
|
120 |
} |
|
121 |
||
122 |
private void checkResourceAdding() { |
|
123 |
List<String> samples = new ArrayList<>(); |
|
124 |
samples.add("java.base"); |
|
125 |
samples.add("java/lang/Object"); |
|
126 |
samples.add("java.base"); |
|
127 |
samples.add("java/lang/String"); |
|
128 |
samples.add("java.management"); |
|
129 |
samples.add("javax/management/ObjectName"); |
|
130 |
test(samples, (resources, module, path) -> { |
|
131 |
try { |
|
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
132 |
resources.add(ResourcePoolEntry.create(path, new byte[0])); |
36511 | 133 |
} catch (Exception ex) { |
134 |
throw new RuntimeException(ex); |
|
135 |
} |
|
136 |
}); |
|
137 |
test(samples, (resources, module, path) -> { |
|
138 |
try { |
|
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
139 |
resources.add(ResourcePoolManager. |
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
140 |
newCompressedResource(ResourcePoolEntry.create(path, new byte[0]), |
36511 | 141 |
ByteBuffer.allocate(99), "bitcruncher", null, |
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
142 |
((ResourcePoolManager)resources).getStringTable(), ByteOrder.nativeOrder())); |
36511 | 143 |
} catch (Exception ex) { |
144 |
throw new RuntimeException(ex); |
|
145 |
} |
|
146 |
}); |
|
147 |
} |
|
148 |
||
149 |
private void test(List<String> samples, ResourceAdder adder) { |
|
150 |
if (samples.isEmpty()) { |
|
151 |
throw new AssertionError("No sample to test"); |
|
152 |
} |
|
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
153 |
ResourcePoolManager resources = new ResourcePoolManager(); |
36511 | 154 |
Set<String> modules = new HashSet<>(); |
155 |
for (int i = 0; i < samples.size(); i++) { |
|
156 |
String module = samples.get(i); |
|
157 |
modules.add(module); |
|
158 |
i++; |
|
159 |
String clazz = samples.get(i); |
|
160 |
String path = "/" + module + "/" + clazz + ".class"; |
|
161 |
adder.add(resources, module, path); |
|
162 |
} |
|
163 |
for (int i = 0; i < samples.size(); i++) { |
|
164 |
String module = samples.get(i); |
|
165 |
i++; |
|
166 |
String clazz = samples.get(i); |
|
167 |
String path = "/" + module + "/" + clazz + ".class"; |
|
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
168 |
Optional<ResourcePoolEntry> res = resources.findEntry(path); |
38320 | 169 |
if (!res.isPresent()) { |
36511 | 170 |
throw new AssertionError("Resource not found " + path); |
171 |
} |
|
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
172 |
checkModule(resources.resourcePool(), res.get()); |
38320 | 173 |
if (resources.findEntry(clazz).isPresent()) { |
36511 | 174 |
throw new AssertionError("Resource found " + clazz); |
175 |
} |
|
176 |
} |
|
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
177 |
if (resources.entryCount() != samples.size() / 2) { |
36511 | 178 |
throw new AssertionError("Invalid number of resources"); |
179 |
} |
|
180 |
} |
|
181 |
||
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
182 |
private void checkModule(ResourcePool resources, ResourcePoolEntry res) { |
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
183 |
Optional<ResourcePoolModule> optMod = resources.moduleView().findModule(res.moduleName()); |
38320 | 184 |
if (!optMod.isPresent()) { |
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
185 |
throw new AssertionError("No module " + res.moduleName()); |
36511 | 186 |
} |
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
187 |
ResourcePoolModule m = optMod.get(); |
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
188 |
if (!m.name().equals(res.moduleName())) { |
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
189 |
throw new AssertionError("Not right module name " + res.moduleName()); |
36511 | 190 |
} |
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
191 |
if (!m.findEntry(res.path()).isPresent()) { |
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
192 |
throw new AssertionError("resource " + res.path() |
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
193 |
+ " not in module " + m.name()); |
36511 | 194 |
} |
195 |
} |
|
196 |
||
197 |
private void checkResourcesAfterCompression() throws Exception { |
|
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
198 |
ResourcePoolManager resources1 = new ResourcePoolManager(); |
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
199 |
ResourcePoolEntry res1 = ResourcePoolEntry.create("/module1/toto1", new byte[0]); |
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
200 |
ResourcePoolEntry res2 = ResourcePoolEntry.create("/module2/toto1", new byte[0]); |
36511 | 201 |
resources1.add(res1); |
202 |
resources1.add(res2); |
|
203 |
||
204 |
checkResources(resources1, res1, res2); |
|
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
205 |
ResourcePoolManager resources2 = new ResourcePoolManager(); |
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
206 |
ResourcePoolEntry res3 = ResourcePoolEntry.create("/module2/toto1", new byte[7]); |
36511 | 207 |
resources2.add(res3); |
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
208 |
resources2.add(ResourcePoolManager.newCompressedResource(res1, |
36511 | 209 |
ByteBuffer.allocate(7), "zip", null, resources1.getStringTable(), |
210 |
ByteOrder.nativeOrder())); |
|
211 |
checkResources(resources2, res1, res2); |
|
212 |
} |
|
213 |
||
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
214 |
private void checkResources(ResourcePoolManager resources, ResourcePoolEntry... expected) { |
36511 | 215 |
List<String> modules = new ArrayList(); |
38320 | 216 |
resources.modules().forEach(m -> { |
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
217 |
modules.add(m.name()); |
38320 | 218 |
}); |
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
219 |
for (ResourcePoolEntry res : expected) { |
36511 | 220 |
if (!resources.contains(res)) { |
221 |
throw new AssertionError("Resource not found: " + res); |
|
222 |
} |
|
223 |
||
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
224 |
if (!resources.findEntry(res.path()).isPresent()) { |
36511 | 225 |
throw new AssertionError("Resource not found: " + res); |
226 |
} |
|
227 |
||
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
228 |
if (!modules.contains(res.moduleName())) { |
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
229 |
throw new AssertionError("Module not found: " + res.moduleName()); |
36511 | 230 |
} |
231 |
||
38320 | 232 |
if (!resources.contains(res)) { |
36511 | 233 |
throw new AssertionError("Resources not found: " + res); |
234 |
} |
|
235 |
||
236 |
try { |
|
237 |
resources.add(res); |
|
238 |
throw new AssertionError(res + " already present, but an exception is not thrown"); |
|
239 |
} catch (Exception ex) { |
|
240 |
// Expected |
|
241 |
} |
|
242 |
} |
|
243 |
||
244 |
try { |
|
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
245 |
resources.add(ResourcePoolEntry.create("/module2/toto1", new byte[0])); |
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
246 |
throw new AssertionError("ResourcePool is read-only, but an exception is not thrown"); |
36511 | 247 |
} catch (Exception ex) { |
248 |
// Expected |
|
249 |
} |
|
250 |
} |
|
251 |
||
252 |
interface ResourceAdder { |
|
39834
53a6fb443c20
8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents:
39151
diff
changeset
|
253 |
void add(ResourcePoolManager resources, String module, String path); |
36511 | 254 |
} |
255 |
} |