test/jdk/tools/jlink/ResourceDuplicateCheckTest.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 43191 jdk/test/tools/jlink/ResourceDuplicateCheckTest.java@3a4ebb59aa0e
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
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
43191
3a4ebb59aa0e 8172982: tools/jlink/ResourceDuplicateCheckTest.java requires jdk.tools.jlink.plugin to be exported
chegar
parents: 43187
diff changeset
    30
 *          jdk.jlink/jdk.tools.jlink.plugin
43187
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    31
 * @run build ResourceDuplicateCheckTest
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    32
 * @run main ResourceDuplicateCheckTest
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
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    35
import java.net.URI;
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    36
import java.nio.file.FileSystems;
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    37
import java.nio.file.Files;
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    38
import java.nio.file.Path;
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    39
import java.nio.file.Paths;
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    40
import java.util.Collections;
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    41
import jdk.tools.jlink.builder.DefaultImageBuilder;
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    42
import jdk.tools.jlink.internal.ResourcePoolEntryFactory;
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    43
import jdk.tools.jlink.internal.ResourcePoolManager;
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    44
import jdk.tools.jlink.plugin.PluginException;
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    45
import jdk.tools.jlink.plugin.ResourcePoolEntry;
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    46
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    47
public class ResourceDuplicateCheckTest {
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    48
    public static void main(String[] args) throws Exception {
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    49
        new ResourceDuplicateCheckTest().test();
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
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    52
    public void test() throws Exception {
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    53
        ResourcePoolManager input = new ResourcePoolManager();
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    54
        // 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
    55
        input.add(ResourcePoolEntryFactory.create("/java.base/module-info.class",
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    56
                    ResourcePoolEntry.Type.CLASS_OR_RESOURCE, getJavaBaseModuleInfo()));
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    57
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    58
        // same NATIVE_CMD from two different modules
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    59
        input.add(newInMemoryImageFile("/com.acme/bin/myexec",
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    60
                    ResourcePoolEntry.Type.NATIVE_CMD, "mylib"));
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    61
        input.add(newInMemoryImageFile("/com.foo/bin/myexec",
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    62
                    ResourcePoolEntry.Type.NATIVE_CMD, "mylib"));
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    63
        Path root = Paths.get(System.getProperty("test.classes"));
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    64
        DefaultImageBuilder writer = new DefaultImageBuilder(root, Collections.emptyMap());
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    65
        try {
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    66
            writer.storeFiles(input.resourcePool());
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    67
        } catch (PluginException pe) {
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    68
            if (! pe.getMessage().contains("Duplicate resources:")) {
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    69
                throw new AssertionError("expected duplicate resources message");
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
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    74
    private byte[] getJavaBaseModuleInfo() throws Exception {
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    75
        Path path = FileSystems.
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    76
                getFileSystem(URI.create("jrt:/")).
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    77
                getPath("/modules/java.base/module-info.class");
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    78
        return Files.readAllBytes(path);
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
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    81
    private static ResourcePoolEntry newInMemoryImageFile(String path,
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    82
            ResourcePoolEntry.Type type, String content) {
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    83
        return ResourcePoolEntryFactory.create(path, type, content.getBytes());
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    84
    }
88cde350ac5f 8168254: Detect duplicated resources in packaged modules
sundar
parents:
diff changeset
    85
}