jdk/test/tools/jlink/bindservices/BindServices.java
author anazarov
Thu, 20 Apr 2017 22:46:07 -0700
changeset 44783 1b4068aea6df
parent 44747 dc7378149f20
child 45393 de4e1efc8eec
permissions -rw-r--r--
8178323: Add negative tests for bind services Jlink feature Reviewed-by: mchung
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
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    36
import static jdk.testlibrary.ProcessTools.*;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    37
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    38
import org.testng.annotations.BeforeTest;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    39
import org.testng.annotations.Test;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    40
import static org.testng.Assert.*;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    41
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
 * @test
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    44
 * @bug 8174826
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    45
 * @library /lib/testlibrary
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    46
 * @modules jdk.compiler jdk.jlink
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    47
 * @build BindServices CompilerUtils jdk.testlibrary.ProcessTools
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    48
 * @run testng BindServices
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    49
 */
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    50
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    51
public class BindServices {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    52
    private static final String JAVA_HOME = System.getProperty("java.home");
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    53
    private static final String TEST_SRC = System.getProperty("test.src");
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    54
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    55
    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
    56
    private static final Path MODS_DIR = Paths.get("mods");
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    57
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    58
    private static final String MODULE_PATH =
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    59
        Paths.get(JAVA_HOME, "jmods").toString() +
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    60
            File.pathSeparator + MODS_DIR.toString();
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    61
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    62
    // the names of the modules in this test
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    63
    private static String[] modules = new String[] {"m1", "m2", "m3"};
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    64
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    65
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    66
    private static boolean hasJmods() {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    67
        if (!Files.exists(Paths.get(JAVA_HOME, "jmods"))) {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    68
            System.err.println("Test skipped. NO jmods directory");
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    69
            return false;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    70
        }
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    71
        return true;
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
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
     * Compiles all modules used by the test
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
    @BeforeTest
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    78
    public void compileAll() throws Throwable {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    79
        if (!hasJmods()) return;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    80
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    81
        for (String mn : modules) {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    82
            Path msrc = SRC_DIR.resolve(mn);
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    83
            assertTrue(CompilerUtils.compile(msrc, MODS_DIR,
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    84
                "--module-source-path", SRC_DIR.toString()));
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    85
        }
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    86
    }
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
    @Test
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    89
    public void noServiceBinding() throws Throwable {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    90
        if (!hasJmods()) return;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    91
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    92
        Path dir = Paths.get("noServiceBinding");
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
        // no service binding and does not link m2,m3 providers.
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    95
        JLink.run("--output", dir.toString(),
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    96
                  "--module-path", MODULE_PATH,
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    97
                  "--add-modules", "m1").output();
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    98
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
    99
        testImage(dir, "m1");
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
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   102
    @Test
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   103
    public void fullServiceBinding() throws Throwable {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   104
        if (!hasJmods()) return;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   105
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   106
        Path dir = Paths.get("fullServiceBinding");
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
        // full service binding
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   109
        // 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
   110
        // 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
   111
        // provider to p2.T
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   112
        JLink.run("--output", dir.toString(),
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   113
                  "--module-path", MODULE_PATH,
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   114
                  "--add-modules", "m1",
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   115
                  "--bind-services",
44747
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   116
                  "--limit-modules", "m1,m2,m3");
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   117
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   118
        testImage(dir, "m1", "m2", "m3");
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
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   121
    @Test
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   122
    public void testVerbose() throws Throwable {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   123
        if (!hasJmods()) return;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   124
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   125
        Path dir = Paths.get("verbose");
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
        List<String> output =
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   128
            JLink.run("--output", dir.toString(),
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   129
                      "--module-path", MODULE_PATH,
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   130
                      "--add-modules", "m1",
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   131
                      "--bind-services",
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   132
                      "--verbose",
44747
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   133
                      "--limit-modules", "m1,m2,m3").output();
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   134
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   135
        List<String> expected = List.of(
44747
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   136
            "m1 " + MODS_DIR.resolve("m1").toUri().toString(),
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   137
            "m2 " + MODS_DIR.resolve("m2").toUri().toString(),
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   138
            "m3 " + MODS_DIR.resolve("m3").toUri().toString(),
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   139
            "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
   140
            "m1 provides p1.S used by m1",
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   141
            "m2 provides p1.S used by m1",
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   142
            "m2 provides p2.T used by m2",
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   143
            "m3 provides p2.T used by m2",
dc7378149f20 8178404: jlink --suggest-providers should list providers from observable modules
mchung
parents: 44367
diff changeset
   144
            "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
   145
        );
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   146
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   147
        assertTrue(output.containsAll(expected));
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
        testImage(dir, "m1", "m2", "m3");
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
44783
1b4068aea6df 8178323: Add negative tests for bind services Jlink feature
anazarov
parents: 44747
diff changeset
   152
    @Test
1b4068aea6df 8178323: Add negative tests for bind services Jlink feature
anazarov
parents: 44747
diff changeset
   153
    public void testVerboseAndNoBindServices() throws Throwable {
1b4068aea6df 8178323: Add negative tests for bind services Jlink feature
anazarov
parents: 44747
diff changeset
   154
        if (!hasJmods()) return;
1b4068aea6df 8178323: Add negative tests for bind services Jlink feature
anazarov
parents: 44747
diff changeset
   155
1b4068aea6df 8178323: Add negative tests for bind services Jlink feature
anazarov
parents: 44747
diff changeset
   156
        Path dir = Paths.get("verboseNoBind");
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
        List<String> output =
1b4068aea6df 8178323: Add negative tests for bind services Jlink feature
anazarov
parents: 44747
diff changeset
   159
            JLink.run("--output", dir.toString(),
1b4068aea6df 8178323: Add negative tests for bind services Jlink feature
anazarov
parents: 44747
diff changeset
   160
                      "--module-path", MODULE_PATH,
1b4068aea6df 8178323: Add negative tests for bind services Jlink feature
anazarov
parents: 44747
diff changeset
   161
                      "--verbose",
1b4068aea6df 8178323: Add negative tests for bind services Jlink feature
anazarov
parents: 44747
diff changeset
   162
                      "--add-modules", "m1").output();
1b4068aea6df 8178323: Add negative tests for bind services Jlink feature
anazarov
parents: 44747
diff changeset
   163
1b4068aea6df 8178323: Add negative tests for bind services Jlink feature
anazarov
parents: 44747
diff changeset
   164
        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
   165
1b4068aea6df 8178323: Add negative tests for bind services Jlink feature
anazarov
parents: 44747
diff changeset
   166
        testImage(dir, "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
44367
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   169
    /*
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   170
     * 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
   171
     */
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   172
    private void testImage(Path javaHome, String... modules) throws Throwable {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   173
        Path java = javaHome.resolve("bin").resolve("java");
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   174
        String[] cmd = Stream.concat(
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   175
            Stream.of(java.toString(), "-m", "m1/p1.Main"),
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   176
            Stream.of(modules)).toArray(String[]::new);
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   177
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   178
        assertTrue(executeProcess(cmd).outputTo(System.out)
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   179
                                      .errorTo(System.out)
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   180
                                      .getExitValue() == 0);
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   181
    }
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   182
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   183
    static class JLink {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   184
        static final ToolProvider JLINK_TOOL = ToolProvider.findFirst("jlink")
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   185
            .orElseThrow(() ->
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   186
                new RuntimeException("jlink tool not found")
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   187
            );
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   188
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   189
        static JLink run(String... options) {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   190
            JLink jlink = new JLink();
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   191
            assertTrue(jlink.execute(options) == 0);
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   192
            return jlink;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   193
        }
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   194
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   195
        final List<String> output = new ArrayList<>();
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   196
        private int execute(String... options) {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   197
            System.out.println("jlink " +
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   198
                Stream.of(options).collect(Collectors.joining(" ")));
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   199
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   200
            StringWriter writer = new StringWriter();
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   201
            PrintWriter pw = new PrintWriter(writer);
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   202
            int rc = JLINK_TOOL.run(pw, pw, options);
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   203
            System.out.println(writer.toString());
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   204
            Stream.of(writer.toString().split("\\v"))
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   205
                  .map(String::trim)
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   206
                  .forEach(output::add);
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   207
            return rc;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   208
        }
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   209
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   210
        boolean contains(String s) {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   211
            return output.contains(s);
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   212
        }
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   213
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   214
        List<String> output() {
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   215
            return output;
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   216
        }
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   217
    }
a81c9c6619fb 8174826: jlink support for linking in service provider modules
mchung
parents:
diff changeset
   218
}