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