Path: blob/master/test/jdk/tools/jlink/ResourcePoolTest.java
41144 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @summary Test a pool containing jimage resources and classes.26* @author Jean-Francois Denise27* @modules jdk.jlink/jdk.tools.jlink.internal28* jdk.jlink/jdk.tools.jlink.plugin29* @run build ResourcePoolTest30* @run main ResourcePoolTest31*/3233import java.io.ByteArrayInputStream;34import java.nio.ByteBuffer;35import java.nio.ByteOrder;36import java.util.ArrayList;37import java.util.Collection;38import java.util.HashSet;39import java.util.List;40import java.util.Optional;41import java.util.Set;42import java.util.function.Function;43import jdk.tools.jlink.internal.ResourcePoolManager;44import jdk.tools.jlink.plugin.ResourcePool;45import jdk.tools.jlink.plugin.ResourcePoolModule;46import jdk.tools.jlink.plugin.ResourcePool;47import jdk.tools.jlink.plugin.ResourcePoolEntry;4849public class ResourcePoolTest {5051public static void main(String[] args) throws Exception {52new ResourcePoolTest().test();53}5455public void test() throws Exception {56checkResourceAdding();57checkResourceVisitor();58checkResourcesAfterCompression();59}6061private static final String SUFFIX = "END";6263private void checkResourceVisitor() throws Exception {64ResourcePoolManager input = new ResourcePoolManager();65for (int i = 0; i < 1000; ++i) {66String module = "/module" + (i / 10);67String resourcePath = module + "/java/package" + i;68byte[] bytes = resourcePath.getBytes();69input.add(ResourcePoolEntry.create(resourcePath, bytes));70}71ResourcePoolManager output = new ResourcePoolManager();72ResourceVisitor visitor = new ResourceVisitor();73input.resourcePool().transformAndCopy(visitor, output.resourcePoolBuilder());74if (visitor.getAmountBefore() == 0) {75throw new AssertionError("Resources not found");76}77if (visitor.getAmountBefore() != input.entryCount()) {78throw new AssertionError("Number of visited resources. Expected: " +79visitor.getAmountBefore() + ", got: " + input.entryCount());80}81if (visitor.getAmountAfter() != output.entryCount()) {82throw new AssertionError("Number of added resources. Expected: " +83visitor.getAmountAfter() + ", got: " + output.entryCount());84}85output.entries().forEach(outResource -> {86String path = outResource.path().replaceAll(SUFFIX + "$", "");87if (!input.findEntry(path).isPresent()) {88throw new AssertionError("Unknown resource: " + path);89}90});91}9293private static class ResourceVisitor implements Function<ResourcePoolEntry, ResourcePoolEntry> {9495private int amountBefore;96private int amountAfter;9798@Override99public ResourcePoolEntry apply(ResourcePoolEntry resource) {100int index = ++amountBefore % 3;101switch (index) {102case 0:103++amountAfter;104return ResourcePoolEntry.create(resource.path() + SUFFIX,105resource.type(), resource.contentBytes());106case 1:107++amountAfter;108return resource.copyWithContent(resource.contentBytes());109}110return null;111}112113public int getAmountAfter() {114return amountAfter;115}116117public int getAmountBefore() {118return amountBefore;119}120}121122private void checkResourceAdding() {123List<String> samples = new ArrayList<>();124samples.add("java.base");125samples.add("java/lang/Object");126samples.add("java.base");127samples.add("java/lang/String");128samples.add("java.management");129samples.add("javax/management/ObjectName");130test(samples, (resources, module, path) -> {131try {132resources.add(ResourcePoolEntry.create(path, new byte[0]));133} catch (Exception ex) {134throw new RuntimeException(ex);135}136});137test(samples, (resources, module, path) -> {138try {139resources.add(ResourcePoolManager.140newCompressedResource(ResourcePoolEntry.create(path, new byte[0]),141ByteBuffer.allocate(99), "bitcruncher", null,142((ResourcePoolManager)resources).getStringTable(), ByteOrder.nativeOrder()));143} catch (Exception ex) {144throw new RuntimeException(ex);145}146});147}148149private void test(List<String> samples, ResourceAdder adder) {150if (samples.isEmpty()) {151throw new AssertionError("No sample to test");152}153ResourcePoolManager resources = new ResourcePoolManager();154Set<String> modules = new HashSet<>();155for (int i = 0; i < samples.size(); i++) {156String module = samples.get(i);157modules.add(module);158i++;159String clazz = samples.get(i);160String path = "/" + module + "/" + clazz + ".class";161adder.add(resources, module, path);162}163for (int i = 0; i < samples.size(); i++) {164String module = samples.get(i);165i++;166String clazz = samples.get(i);167String path = "/" + module + "/" + clazz + ".class";168Optional<ResourcePoolEntry> res = resources.findEntry(path);169if (!res.isPresent()) {170throw new AssertionError("Resource not found " + path);171}172checkModule(resources.resourcePool(), res.get());173if (resources.findEntry(clazz).isPresent()) {174throw new AssertionError("Resource found " + clazz);175}176}177if (resources.entryCount() != samples.size() / 2) {178throw new AssertionError("Invalid number of resources");179}180}181182private void checkModule(ResourcePool resources, ResourcePoolEntry res) {183Optional<ResourcePoolModule> optMod = resources.moduleView().findModule(res.moduleName());184if (!optMod.isPresent()) {185throw new AssertionError("No module " + res.moduleName());186}187ResourcePoolModule m = optMod.get();188if (!m.name().equals(res.moduleName())) {189throw new AssertionError("Not right module name " + res.moduleName());190}191if (!m.findEntry(res.path()).isPresent()) {192throw new AssertionError("resource " + res.path()193+ " not in module " + m.name());194}195}196197private void checkResourcesAfterCompression() throws Exception {198ResourcePoolManager resources1 = new ResourcePoolManager();199ResourcePoolEntry res1 = ResourcePoolEntry.create("/module1/toto1", new byte[0]);200ResourcePoolEntry res2 = ResourcePoolEntry.create("/module2/toto1", new byte[0]);201resources1.add(res1);202resources1.add(res2);203204checkResources(resources1, res1, res2);205ResourcePoolManager resources2 = new ResourcePoolManager();206ResourcePoolEntry res3 = ResourcePoolEntry.create("/module2/toto1", new byte[7]);207resources2.add(res3);208resources2.add(ResourcePoolManager.newCompressedResource(res1,209ByteBuffer.allocate(7), "zip", null, resources1.getStringTable(),210ByteOrder.nativeOrder()));211checkResources(resources2, res1, res2);212}213214private void checkResources(ResourcePoolManager resources, ResourcePoolEntry... expected) {215List<String> modules = new ArrayList();216resources.modules().forEach(m -> {217modules.add(m.name());218});219for (ResourcePoolEntry res : expected) {220if (!resources.contains(res)) {221throw new AssertionError("Resource not found: " + res);222}223224if (!resources.findEntry(res.path()).isPresent()) {225throw new AssertionError("Resource not found: " + res);226}227228if (!modules.contains(res.moduleName())) {229throw new AssertionError("Module not found: " + res.moduleName());230}231232if (!resources.contains(res)) {233throw new AssertionError("Resources not found: " + res);234}235236try {237resources.add(res);238throw new AssertionError(res + " already present, but an exception is not thrown");239} catch (Exception ex) {240// Expected241}242}243244try {245resources.add(ResourcePoolEntry.create("/module2/toto1", new byte[0]));246throw new AssertionError("ResourcePool is read-only, but an exception is not thrown");247} catch (Exception ex) {248// Expected249}250}251252interface ResourceAdder {253void add(ResourcePoolManager resources, String module, String path);254}255}256257258