test/jdk/tools/jlink/NativeTest.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 36511 jdk/test/tools/jlink/NativeTest.java@9d0388c6b336
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:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
 * @test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
 * @summary Test config, cmd and lib directories of jmod.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
 * @author Andrei Eremeev
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
 * @library ../lib
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
 * @modules java.base/jdk.internal.jimage
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
 *          jdk.jdeps/com.sun.tools.classfile
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
 *          jdk.jlink/jdk.tools.jlink.internal
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
 *          jdk.jlink/jdk.tools.jmod
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
 *          jdk.jlink/jdk.tools.jimage
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
 *          jdk.compiler
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
 * @build tests.*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
 * @run main NativeTest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.io.File;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.io.IOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.nio.file.Files;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import java.nio.file.Path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import java.util.Arrays;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import tests.Helper;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
import tests.JImageGenerator;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
public class NativeTest {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
    private final Helper helper;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
    public NativeTest(Helper helper) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
        this.helper = helper;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
    public static void main(String[] args) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
        Helper helper = Helper.newHelper();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
        if (helper == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
            System.err.println("Not run");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
            return;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
        new NativeTest(helper).test();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
    public void test() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
        String moduleName = "native_library";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
        Path classesDir = helper.generateModuleCompiledClasses(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
                helper.getJmodSrcDir(), helper.getJmodClassesDir(), moduleName);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
        Path libsDir = helper.getJmodDir().resolve("lib").resolve(moduleName);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
        Path configDir = helper.getJmodDir().resolve("config").resolve(moduleName);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
        Path cmdDir = helper.getJmodDir().resolve("cmd").resolve(moduleName);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
        Path config = writeFile(configDir.resolve("config.txt"), "AAAA\nBBBB");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
        Path cmd = writeFile(cmdDir.resolve("ls.sh"), "ls");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
        Path lib = writeFile(libsDir.resolve("native.so"), "native library");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
        JImageGenerator.getJModTask()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
                .addClassPath(classesDir)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
                .addNativeLibraries(libsDir)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
                .addCmds(cmdDir)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
                .addConfig(configDir)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
                .jmod(helper.createNewJmodFile(moduleName))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
                .create()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
                .assertSuccess();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
        String[] expectedFiles = new String[] {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
                "bin" + File.separator + cmd.getFileName(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
                "conf" + File.separator + config.getFileName(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
                "lib" + File.separator + lib.getFileName()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
        };
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
        Path image = JImageGenerator.getJLinkTask()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
                .modulePath(helper.defaultModulePath())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
                .addMods(moduleName)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
                .output(helper.createNewImageDir(moduleName))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
                .call().assertSuccess();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
        helper.checkImage(image, moduleName, null, null, expectedFiles);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
    private Path writeFile(Path path, String content) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
        if (path.getParent() != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
            Files.createDirectories(path.getParent());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
        Files.write(path, Arrays.asList(content.split("\n")));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
        return path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
}