test/jdk/tools/jlink/bindservices/BindServices.java
author prr
Fri, 25 May 2018 12:12:24 -0700
changeset 50347 b2f046ae8eb6
parent 47216 71c04702a3d5
child 51675 b487c1e914d0
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
     1
/*
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
     4
 *
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
     7
 * published by the Free Software Foundation.
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
     8
 *
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    13
 * accompanied this code).
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    14
 *
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    18
 *
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    21
 * questions.
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    22
 */
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    23
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    24
import java.io.File;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    25
import java.io.PrintWriter;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    26
import java.io.StringWriter;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    27
import java.nio.file.Files;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    28
import java.nio.file.Path;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    29
import java.nio.file.Paths;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    30
import java.util.ArrayList;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    31
import java.util.List;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    32
import java.util.spi.ToolProvider;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    33
import java.util.stream.Collectors;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    34
import java.util.stream.Stream;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    35
45393
de4e1efc8eec 8181335: remove packageless CompilerUtils
iignatyev
parents: 44783
diff changeset
    36
import jdk.test.lib.compiler.CompilerUtils;
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    37
import static jdk.testlibrary.ProcessTools.*;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    38
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    39
import org.testng.annotations.BeforeTest;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    40
import org.testng.annotations.Test;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    41
import static org.testng.Assert.*;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    42
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    43
/**
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    44
 * @test
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    45
 * @bug 8174826
45393
de4e1efc8eec 8181335: remove packageless CompilerUtils
iignatyev
parents: 44783
diff changeset
    46
 * @library /lib/testlibrary /test/lib
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    47
 * @modules jdk.compiler jdk.jlink
45393
de4e1efc8eec 8181335: remove packageless CompilerUtils
iignatyev
parents: 44783
diff changeset
    48
 * @build BindServices jdk.testlibrary.ProcessTools
de4e1efc8eec 8181335: remove packageless CompilerUtils
iignatyev
parents: 44783
diff changeset
    49
 *        jdk.test.lib.compiler.CompilerUtils
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    50
 * @run testng BindServices
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    51
 */
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    52
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    53
public class BindServices {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    54
    private static final String JAVA_HOME = System.getProperty("java.home");
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    55
    private static final String TEST_SRC = System.getProperty("test.src");
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    56
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    57
    private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    58
    private static final Path MODS_DIR = Paths.get("mods");
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    59
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    60
    private static final String MODULE_PATH =
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    61
        Paths.get(JAVA_HOME, "jmods").toString() +
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    62
            File.pathSeparator + MODS_DIR.toString();
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    63
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    64
    // the names of the modules in this test
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    65
    private static String[] modules = new String[] {"m1", "m2", "m3"};
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    66
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    67
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    68
    private static boolean hasJmods() {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    69
        if (!Files.exists(Paths.get(JAVA_HOME, "jmods"))) {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    70
            System.err.println("Test skipped. NO jmods directory");
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    71
            return false;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    72
        }
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    73
        return true;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    74
    }
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    75
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    76
    /*
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    77
     * Compiles all modules used by the test
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    78
     */
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    79
    @BeforeTest
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    80
    public void compileAll() throws Throwable {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    81
        if (!hasJmods()) return;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    82
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    83
        for (String mn : modules) {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    84
            Path msrc = SRC_DIR.resolve(mn);
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    85
            assertTrue(CompilerUtils.compile(msrc, MODS_DIR,
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    86
                "--module-source-path", SRC_DIR.toString()));
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    87
        }
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    88
    }
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    89
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    90
    @Test
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    91
    public void noServiceBinding() throws Throwable {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    92
        if (!hasJmods()) return;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    93
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    94
        Path dir = Paths.get("noServiceBinding");
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    95
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    96
        // no service binding and does not link m2,m3 providers.
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    97
        JLink.run("--output", dir.toString(),
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    98
                  "--module-path", MODULE_PATH,
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    99
                  "--add-modules", "m1").output();
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   100
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   101
        testImage(dir, "m1");
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   102
    }
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   103
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   104
    @Test
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   105
    public void fullServiceBinding() throws Throwable {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   106
        if (!hasJmods()) return;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   107
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   108
        Path dir = Paths.get("fullServiceBinding");
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   109
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   110
        // full service binding
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   111
        // m2 is a provider used by m1.  During service binding, when m2 is
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   112
        // resolved, m2 uses p2.T that causes m3 to be linked as it is a
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   113
        // provider to p2.T
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   114
        JLink.run("--output", dir.toString(),
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   115
                  "--module-path", MODULE_PATH,
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   116
                  "--add-modules", "m1",
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   117
                  "--bind-services",
44747
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   118
                  "--limit-modules", "m1,m2,m3");
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   119
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   120
        testImage(dir, "m1", "m2", "m3");
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   121
    }
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   122
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   123
    @Test
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   124
    public void testVerbose() throws Throwable {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   125
        if (!hasJmods()) return;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   126
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   127
        Path dir = Paths.get("verbose");
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   128
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   129
        List<String> output =
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   130
            JLink.run("--output", dir.toString(),
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   131
                      "--module-path", MODULE_PATH,
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   132
                      "--add-modules", "m1",
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   133
                      "--bind-services",
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   134
                      "--verbose",
44747
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   135
                      "--limit-modules", "m1,m2,m3").output();
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   136
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   137
        List<String> expected = List.of(
44747
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   138
            "m1 " + MODS_DIR.resolve("m1").toUri().toString(),
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   139
            "m2 " + MODS_DIR.resolve("m2").toUri().toString(),
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   140
            "m3 " + MODS_DIR.resolve("m3").toUri().toString(),
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   141
            "java.base provides java.nio.file.spi.FileSystemProvider used by java.base",
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   142
            "m1 provides p1.S used by m1",
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   143
            "m2 provides p1.S used by m1",
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   144
            "m2 provides p2.T used by m2",
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   145
            "m3 provides p2.T used by m2",
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   146
            "m3 provides p3.S not used by any observable module"
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   147
        );
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   148
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   149
        assertTrue(output.containsAll(expected));
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   150
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   151
        testImage(dir, "m1", "m2", "m3");
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   152
    }
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   153
44783
1b4068aea6df 8178323: Add negative tests for bind services Jlink feature
anazarov
parents: 44747
diff changeset
   154
    @Test
1b4068aea6df 8178323: Add negative tests for bind services Jlink feature
anazarov
parents: 44747
diff changeset
   155
    public void testVerboseAndNoBindServices() throws Throwable {
1b4068aea6df 8178323: Add negative tests for bind services Jlink feature
anazarov
parents: 44747
diff changeset
   156
        if (!hasJmods()) return;
1b4068aea6df 8178323: Add negative tests for bind services Jlink feature
anazarov
parents: 44747
diff changeset
   157
1b4068aea6df 8178323: Add negative tests for bind services Jlink feature
anazarov
parents: 44747
diff changeset
   158
        Path dir = Paths.get("verboseNoBind");
1b4068aea6df 8178323: Add negative tests for bind services Jlink feature
anazarov
parents: 44747
diff changeset
   159
1b4068aea6df 8178323: Add negative tests for bind services Jlink feature
anazarov
parents: 44747
diff changeset
   160
        List<String> output =
1b4068aea6df 8178323: Add negative tests for bind services Jlink feature
anazarov
parents: 44747
diff changeset
   161
            JLink.run("--output", dir.toString(),
1b4068aea6df 8178323: Add negative tests for bind services Jlink feature
anazarov
parents: 44747
diff changeset
   162
                      "--module-path", MODULE_PATH,
1b4068aea6df 8178323: Add negative tests for bind services Jlink feature
anazarov
parents: 44747
diff changeset
   163
                      "--verbose",
1b4068aea6df 8178323: Add negative tests for bind services Jlink feature
anazarov
parents: 44747
diff changeset
   164
                      "--add-modules", "m1").output();
1b4068aea6df 8178323: Add negative tests for bind services Jlink feature
anazarov
parents: 44747
diff changeset
   165
1b4068aea6df 8178323: Add negative tests for bind services Jlink feature
anazarov
parents: 44747
diff changeset
   166
        assertTrue(output.contains("m1 provides p1.S used by m1"));
1b4068aea6df 8178323: Add negative tests for bind services Jlink feature
anazarov
parents: 44747
diff changeset
   167
1b4068aea6df 8178323: Add negative tests for bind services Jlink feature
anazarov
parents: 44747
diff changeset
   168
        testImage(dir, "m1");
1b4068aea6df 8178323: Add negative tests for bind services Jlink feature
anazarov
parents: 44747
diff changeset
   169
    }
1b4068aea6df 8178323: Add negative tests for bind services Jlink feature
anazarov
parents: 44747
diff changeset
   170
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   171
    /*
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   172
     * Tests the given ${java.home} to only contain the specified modules
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   173
     */
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   174
    private void testImage(Path javaHome, String... modules) throws Throwable {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   175
        Path java = javaHome.resolve("bin").resolve("java");
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   176
        String[] cmd = Stream.concat(
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   177
            Stream.of(java.toString(), "-m", "m1/p1.Main"),
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   178
            Stream.of(modules)).toArray(String[]::new);
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   179
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   180
        assertTrue(executeProcess(cmd).outputTo(System.out)
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   181
                                      .errorTo(System.out)
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   182
                                      .getExitValue() == 0);
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   183
    }
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   184
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   185
    static class JLink {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   186
        static final ToolProvider JLINK_TOOL = ToolProvider.findFirst("jlink")
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   187
            .orElseThrow(() ->
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   188
                new RuntimeException("jlink tool not found")
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   189
            );
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   190
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   191
        static JLink run(String... options) {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   192
            JLink jlink = new JLink();
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   193
            assertTrue(jlink.execute(options) == 0);
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   194
            return jlink;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   195
        }
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   196
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   197
        final List<String> output = new ArrayList<>();
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   198
        private int execute(String... options) {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   199
            System.out.println("jlink " +
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   200
                Stream.of(options).collect(Collectors.joining(" ")));
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   201
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   202
            StringWriter writer = new StringWriter();
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   203
            PrintWriter pw = new PrintWriter(writer);
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   204
            int rc = JLINK_TOOL.run(pw, pw, options);
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   205
            System.out.println(writer.toString());
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   206
            Stream.of(writer.toString().split("\\v"))
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   207
                  .map(String::trim)
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   208
                  .forEach(output::add);
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   209
            return rc;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   210
        }
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   211
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   212
        boolean contains(String s) {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   213
            return output.contains(s);
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   214
        }
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   215
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   216
        List<String> output() {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   217
            return output;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   218
        }
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   219
    }
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   220
}