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