1/* 2 * Copyright (c) 2017, 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 * @bug 8168254 27 * @summary Detect duplicated resources in packaged modules 28 * @modules jdk.jlink/jdk.tools.jlink.builder 29 * jdk.jlink/jdk.tools.jlink.internal 30 * jdk.jlink/jdk.tools.jlink.plugin 31 * @run build ResourceDuplicateCheckTest 32 * @run main ResourceDuplicateCheckTest 33 */ 34 35import java.net.URI; 36import java.nio.file.FileSystems; 37import java.nio.file.Files; 38import java.nio.file.Path; 39import java.nio.file.Paths; 40import java.util.Collections; 41import jdk.tools.jlink.builder.DefaultImageBuilder; 42import jdk.tools.jlink.internal.ResourcePoolEntryFactory; 43import jdk.tools.jlink.internal.ResourcePoolManager; 44import jdk.tools.jlink.plugin.PluginException; 45import jdk.tools.jlink.plugin.ResourcePoolEntry; 46 47public class ResourceDuplicateCheckTest { 48 public static void main(String[] args) throws Exception { 49 new ResourceDuplicateCheckTest().test(); 50 } 51 52 public void test() throws Exception { 53 ResourcePoolManager input = new ResourcePoolManager(); 54 // need java.base module info because OS name is retrieved from it from storeFiles 55 input.add(ResourcePoolEntryFactory.create("/java.base/module-info.class", 56 ResourcePoolEntry.Type.CLASS_OR_RESOURCE, getJavaBaseModuleInfo())); 57 58 // same NATIVE_CMD from two different modules 59 input.add(newInMemoryImageFile("/com.acme/bin/myexec", 60 ResourcePoolEntry.Type.NATIVE_CMD, "mylib")); 61 input.add(newInMemoryImageFile("/com.foo/bin/myexec", 62 ResourcePoolEntry.Type.NATIVE_CMD, "mylib")); 63 Path root = Paths.get(System.getProperty("test.classes")); 64 DefaultImageBuilder writer = new DefaultImageBuilder(root, Collections.emptyMap()); 65 try { 66 writer.storeFiles(input.resourcePool()); 67 } catch (PluginException pe) { 68 if (! pe.getMessage().contains("Duplicate resources:")) { 69 throw new AssertionError("expected duplicate resources message"); 70 } 71 } 72 } 73 74 private byte[] getJavaBaseModuleInfo() throws Exception { 75 Path path = FileSystems. 76 getFileSystem(URI.create("jrt:/")). 77 getPath("/modules/java.base/module-info.class"); 78 return Files.readAllBytes(path); 79 } 80 81 private static ResourcePoolEntry newInMemoryImageFile(String path, 82 ResourcePoolEntry.Type type, String content) { 83 return ResourcePoolEntryFactory.create(path, type, content.getBytes()); 84 } 85} 86