jdk/test/tools/jlink/ResourceDuplicateCheckTest.java
author sundar
Wed, 18 Jan 2017 19:35:41 +0530
changeset 43187 88cde350ac5f
child 43191 3a4ebb59aa0e
permissions -rw-r--r--
8168254: Detect duplicated resources in packaged modules Reviewed-by: mchung, jlaskey
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43187
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
     1
/*
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
     4
 *
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
     8
 *
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    13
 * accompanied this code).
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    14
 *
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    18
 *
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    21
 * questions.
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    22
 */
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    23
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    24
/*
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    25
 * @test
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    26
 * @bug 8168254
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    27
 * @summary Detect duplicated resources in packaged modules
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    28
 * @modules jdk.jlink/jdk.tools.jlink.builder
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    29
 *          jdk.jlink/jdk.tools.jlink.internal
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    30
 * @run build ResourceDuplicateCheckTest
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    31
 * @run main ResourceDuplicateCheckTest
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    32
 */
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    33
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    34
import java.net.URI;
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    35
import java.nio.file.FileSystems;
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    36
import java.nio.file.Files;
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    37
import java.nio.file.Path;
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    38
import java.nio.file.Paths;
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    39
import java.util.Collections;
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    40
import jdk.tools.jlink.builder.DefaultImageBuilder;
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    41
import jdk.tools.jlink.internal.ResourcePoolEntryFactory;
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    42
import jdk.tools.jlink.internal.ResourcePoolManager;
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    43
import jdk.tools.jlink.plugin.PluginException;
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    44
import jdk.tools.jlink.plugin.ResourcePoolEntry;
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    45
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    46
public class ResourceDuplicateCheckTest {
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    47
    public static void main(String[] args) throws Exception {
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    48
        new ResourceDuplicateCheckTest().test();
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    49
    }
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    50
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    51
    public void test() throws Exception {
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    52
        ResourcePoolManager input = new ResourcePoolManager();
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    53
        // need java.base module info because OS name is retrieved from it from storeFiles
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    54
        input.add(ResourcePoolEntryFactory.create("/java.base/module-info.class",
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    55
                    ResourcePoolEntry.Type.CLASS_OR_RESOURCE, getJavaBaseModuleInfo()));
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    56
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    57
        // same NATIVE_CMD from two different modules
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    58
        input.add(newInMemoryImageFile("/com.acme/bin/myexec",
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    59
                    ResourcePoolEntry.Type.NATIVE_CMD, "mylib"));
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    60
        input.add(newInMemoryImageFile("/com.foo/bin/myexec",
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    61
                    ResourcePoolEntry.Type.NATIVE_CMD, "mylib"));
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    62
        Path root = Paths.get(System.getProperty("test.classes"));
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    63
        DefaultImageBuilder writer = new DefaultImageBuilder(root, Collections.emptyMap());
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    64
        try {
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    65
            writer.storeFiles(input.resourcePool());
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    66
        } catch (PluginException pe) {
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    67
            if (! pe.getMessage().contains("Duplicate resources:")) {
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    68
                throw new AssertionError("expected duplicate resources message");
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    69
            }
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    70
        }
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    71
    }
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    72
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    73
    private byte[] getJavaBaseModuleInfo() throws Exception {
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    74
        Path path = FileSystems.
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    75
                getFileSystem(URI.create("jrt:/")).
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    76
                getPath("/modules/java.base/module-info.class");
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    77
        return Files.readAllBytes(path);
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    78
    }
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    79
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    80
    private static ResourcePoolEntry newInMemoryImageFile(String path,
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    81
            ResourcePoolEntry.Type type, String content) {
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    82
        return ResourcePoolEntryFactory.create(path, type, content.getBytes());
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    83
    }
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    84
}