Path: blob/master/test/jdk/tools/jlink/ResourceDuplicateCheckTest.java
41144 views
/*1* Copyright (c) 2017, 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* @bug 816825426* @summary Detect duplicated resources in packaged modules27* @modules jdk.jlink/jdk.tools.jlink.builder28* jdk.jlink/jdk.tools.jlink.internal29* jdk.jlink/jdk.tools.jlink.plugin30* @run build ResourceDuplicateCheckTest31* @run main ResourceDuplicateCheckTest32*/3334import java.net.URI;35import java.nio.file.FileSystems;36import java.nio.file.Files;37import java.nio.file.Path;38import java.nio.file.Paths;39import java.util.Collections;40import jdk.tools.jlink.builder.DefaultImageBuilder;41import jdk.tools.jlink.internal.ResourcePoolEntryFactory;42import jdk.tools.jlink.internal.ResourcePoolManager;43import jdk.tools.jlink.plugin.PluginException;44import jdk.tools.jlink.plugin.ResourcePoolEntry;4546public class ResourceDuplicateCheckTest {47public static void main(String[] args) throws Exception {48new ResourceDuplicateCheckTest().test();49}5051public void test() throws Exception {52ResourcePoolManager input = new ResourcePoolManager();53// need java.base module info because OS name is retrieved from it from storeFiles54input.add(ResourcePoolEntryFactory.create("/java.base/module-info.class",55ResourcePoolEntry.Type.CLASS_OR_RESOURCE, getJavaBaseModuleInfo()));5657// same NATIVE_CMD from two different modules58input.add(newInMemoryImageFile("/com.acme/bin/myexec",59ResourcePoolEntry.Type.NATIVE_CMD, "mylib"));60input.add(newInMemoryImageFile("/com.foo/bin/myexec",61ResourcePoolEntry.Type.NATIVE_CMD, "mylib"));62Path root = Paths.get(System.getProperty("test.classes"));63DefaultImageBuilder writer = new DefaultImageBuilder(root, Collections.emptyMap());64try {65writer.storeFiles(input.resourcePool());66} catch (PluginException pe) {67if (! pe.getMessage().contains("Duplicate resources:")) {68throw new AssertionError("expected duplicate resources message");69}70}71}7273private byte[] getJavaBaseModuleInfo() throws Exception {74Path path = FileSystems.75getFileSystem(URI.create("jrt:/")).76getPath("/modules/java.base/module-info.class");77return Files.readAllBytes(path);78}7980private static ResourcePoolEntry newInMemoryImageFile(String path,81ResourcePoolEntry.Type type, String content) {82return ResourcePoolEntryFactory.create(path, type, content.getBytes());83}84}858687