Path: blob/master/test/jdk/tools/jlink/ImageFilePoolTest.java
41145 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 external files.26* @author Andrei Eremeev27* @modules jdk.jlink/jdk.tools.jlink.internal28* jdk.jlink/jdk.tools.jlink.plugin29* @run build ImageFilePoolTest30* @run main ImageFilePoolTest31*/3233import java.io.ByteArrayInputStream;34import java.util.Optional;35import java.util.function.Function;36import jdk.tools.jlink.internal.ResourcePoolEntryFactory;37import jdk.tools.jlink.internal.ResourcePoolManager;38import jdk.tools.jlink.plugin.ResourcePoolEntry;39import jdk.tools.jlink.plugin.ResourcePool;4041public class ImageFilePoolTest {42public static void main(String[] args) throws Exception {43new ImageFilePoolTest().test();44}4546public void test() throws Exception {47checkNegative();48checkVisitor();49}5051private static final String SUFFIX = "END";5253private void checkVisitor() throws Exception {54ResourcePoolManager input = new ResourcePoolManager();55for (int i = 0; i < 1000; ++i) {56String module = "module" + (i / 100);57input.add(newInMemoryImageFile("/" + module + "/java/class" + i,58ResourcePoolEntry.Type.CONFIG, "class" + i));59}60if (input.entryCount() != 1000) {61throw new AssertionError();62}63ResourcePoolManager output = new ResourcePoolManager();64ResourceVisitor visitor = new ResourceVisitor();65input.resourcePool().transformAndCopy(visitor, output.resourcePoolBuilder());66if (visitor.getAmountBefore() == 0) {67throw new AssertionError("Resources not found");68}69if (visitor.getAmountBefore() != input.entryCount()) {70throw new AssertionError("Number of visited resources. Expected: " +71visitor.getAmountBefore() + ", got: " + input.entryCount());72}73if (visitor.getAmountAfter() != output.entryCount()) {74throw new AssertionError("Number of added resources. Expected: " +75visitor.getAmountAfter() + ", got: " + output.entryCount());76}77output.entries().forEach(outFile -> {78String path = outFile.path().replaceAll(SUFFIX + "$", "");79Optional<ResourcePoolEntry> inFile = input.findEntry(path);80if (!inFile.isPresent()) {81throw new AssertionError("Unknown resource: " + path);82}83});84}8586private static class ResourceVisitor implements Function<ResourcePoolEntry, ResourcePoolEntry> {8788private int amountBefore;89private int amountAfter;9091@Override92public ResourcePoolEntry apply(ResourcePoolEntry file) {93int index = ++amountBefore % 3;94switch (index) {95case 0:96++amountAfter;97return newInMemoryImageFile(file.path() + SUFFIX,98file.type(), file.path());99case 1:100++amountAfter;101return newInMemoryImageFile(file.path(),102file.type(), file.path());103}104return null;105}106107public int getAmountAfter() {108return amountAfter;109}110111public int getAmountBefore() {112return amountBefore;113}114}115116private void checkNegative() throws Exception {117ResourcePoolManager input = new ResourcePoolManager();118try {119input.add(null);120throw new AssertionError("NullPointerException is not thrown");121} catch (NullPointerException e) {122// expected123}124try {125input.contains(null);126throw new AssertionError("NullPointerException is not thrown");127} catch (NullPointerException e) {128// expected129}130if (input.findEntry("unknown").isPresent()) {131throw new AssertionError("ImageFileResourcePool does not return null for unknown file");132}133if (input.contains(newInMemoryImageFile("/unknown/foo", ResourcePoolEntry.Type.CONFIG, "unknown"))) {134throw new AssertionError("'contain' returns true for /unknown/foo file");135}136input.add(newInMemoryImageFile("/aaa/bbb", ResourcePoolEntry.Type.CONFIG, ""));137try {138input.add(newInMemoryImageFile("/aaa/bbb", ResourcePoolEntry.Type.CONFIG, ""));139throw new AssertionError("Exception expected");140} catch (Exception e) {141// expected142}143}144145private static ResourcePoolEntry newInMemoryImageFile(String path,146ResourcePoolEntry.Type type, String content) {147return ResourcePoolEntryFactory.create(path, type, content.getBytes());148}149}150151152