< prev index next >

test/jdk/tools/jlink/ResourcePoolTest.java

Print this page

  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 }

  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 }
< prev index next >