jdk/test/tools/jmod/JmodTest.java
author mchung
Wed, 12 Oct 2016 15:41:00 -0700
changeset 41484 834b7539ada3
parent 39882 2a5433a2eca5
child 41916 eb4aa81c6c5f
permissions -rw-r--r--
8164689: Retrofit jar, jlink, jmod as a ToolProvider Reviewed-by: alanb, lancea
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, 2016, 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
 * @library /lib/testlibrary
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 39882
diff changeset
    27
 * @modules jdk.compiler
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 39882
diff changeset
    28
 *          jdk.jlink
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
 * @build jdk.testlibrary.FileUtils CompilerUtils
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
 * @run testng JmodTest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
 * @summary Basic test for jmod
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.io.*;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.lang.module.ModuleDescriptor;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.lang.reflect.Method;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.nio.file.*;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.util.*;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.util.function.Consumer;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.util.regex.Pattern;
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 39882
diff changeset
    41
import java.util.spi.ToolProvider;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import java.util.stream.Stream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import jdk.testlibrary.FileUtils;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import org.testng.annotations.BeforeTest;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import org.testng.annotations.Test;
39882
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
    46
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
    47
import static java.io.File.pathSeparator;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
import static java.lang.module.ModuleDescriptor.Version;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
import static java.nio.charset.StandardCharsets.UTF_8;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
import static java.util.stream.Collectors.toSet;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
import static org.testng.Assert.*;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
public class JmodTest {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 39882
diff changeset
    55
    static final ToolProvider JMOD_TOOL = ToolProvider.findFirst("jmod")
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 39882
diff changeset
    56
        .orElseThrow(() ->
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 39882
diff changeset
    57
            new RuntimeException("jmod tool not found")
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 39882
diff changeset
    58
        );
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 39882
diff changeset
    59
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
    static final String TEST_SRC = System.getProperty("test.src", ".");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
    static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
    static final Path EXPLODED_DIR = Paths.get("build");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
    static final Path MODS_DIR = Paths.get("jmods");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
    static final String CLASSES_PREFIX = "classes/";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
    static final String CMDS_PREFIX = "bin/";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
    static final String LIBS_PREFIX = "native/";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
    static final String CONFIGS_PREFIX = "conf/";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
    @BeforeTest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
    public void buildExplodedModules() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
        if (Files.exists(EXPLODED_DIR))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
            FileUtils.deleteFileTreeWithRetry(EXPLODED_DIR);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
        for (String name : new String[] { "foo"/*, "bar", "baz"*/ } ) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
            Path dir = EXPLODED_DIR.resolve(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
            assertTrue(compileModule(name, dir.resolve("classes")));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
            createCmds(dir.resolve("bin"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
            createLibs(dir.resolve("lib"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
            createConfigs(dir.resolve("conf"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
        if (Files.exists(MODS_DIR))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
            FileUtils.deleteFileTreeWithRetry(MODS_DIR);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
        Files.createDirectories(MODS_DIR);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
    @Test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
    public void testList() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
        String cp = EXPLODED_DIR.resolve("foo").resolve("classes").toString();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
        jmod("create",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
             "--class-path", cp,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
             MODS_DIR.resolve("foo.jmod").toString())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
            .assertSuccess();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
        jmod("list",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
             MODS_DIR.resolve("foo.jmod").toString())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
            .assertSuccess()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
            .resultChecker(r -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
                // asserts dependent on the exact contents of foo
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
                assertContains(r.output, CLASSES_PREFIX + "module-info.class");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
                assertContains(r.output, CLASSES_PREFIX + "jdk/test/foo/Foo.class");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
                assertContains(r.output, CLASSES_PREFIX + "jdk/test/foo/internal/Message.class");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
            });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
    @Test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
    public void testMainClass() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
        Path jmod = MODS_DIR.resolve("fooMainClass.jmod");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
        FileUtils.deleteFileIfExistsWithRetry(jmod);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
        String cp = EXPLODED_DIR.resolve("foo").resolve("classes").toString();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
        jmod("create",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
             "--class-path", cp,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
             "--main-class", "jdk.test.foo.Foo",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
             jmod.toString())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
            .assertSuccess()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
            .resultChecker(r -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
                Optional<String> omc = getModuleDescriptor(jmod).mainClass();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
                assertTrue(omc.isPresent());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
                assertEquals(omc.get(), "jdk.test.foo.Foo");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
            });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
    @Test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
    public void testModuleVersion() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
        Path jmod = MODS_DIR.resolve("fooVersion.jmod");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
        FileUtils.deleteFileIfExistsWithRetry(jmod);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
        String cp = EXPLODED_DIR.resolve("foo").resolve("classes").toString();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
        jmod("create",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
             "--class-path", cp,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
             "--module-version", "5.4.3",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
             jmod.toString())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
            .assertSuccess()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
            .resultChecker(r -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
                Optional<Version> ov = getModuleDescriptor(jmod).version();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
                assertTrue(ov.isPresent());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
                assertEquals(ov.get().toString(), "5.4.3");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
            });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
    @Test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
    public void testConfig() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
        Path jmod = MODS_DIR.resolve("fooConfig.jmod");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
        FileUtils.deleteFileIfExistsWithRetry(jmod);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
        Path cp = EXPLODED_DIR.resolve("foo").resolve("classes");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
        Path cf = EXPLODED_DIR.resolve("foo").resolve("conf");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
        jmod("create",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
             "--class-path", cp.toString(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
             "--config", cf.toString(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
             jmod.toString())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
            .assertSuccess()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
            .resultChecker(r -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
                try (Stream<String> s1 = findFiles(cf).map(p -> CONFIGS_PREFIX + p);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
                     Stream<String> s2 = findFiles(cp).map(p -> CLASSES_PREFIX + p)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
                    Set<String> expectedFilenames = Stream.concat(s1, s2)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
                                                          .collect(toSet());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
                    assertJmodContent(jmod, expectedFilenames);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
            });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
    @Test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
    public void testCmds() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
        Path jmod = MODS_DIR.resolve("fooCmds.jmod");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
        FileUtils.deleteFileIfExistsWithRetry(jmod);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
        Path cp = EXPLODED_DIR.resolve("foo").resolve("classes");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
        Path bp = EXPLODED_DIR.resolve("foo").resolve("bin");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
        jmod("create",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
             "--cmds", bp.toString(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
             "--class-path", cp.toString(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
             jmod.toString())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   176
            .assertSuccess()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
            .resultChecker(r -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   178
                try (Stream<String> s1 = findFiles(bp).map(p -> CMDS_PREFIX + p);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   179
                     Stream<String> s2 = findFiles(cp).map(p -> CLASSES_PREFIX + p)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   180
                    Set<String> expectedFilenames = Stream.concat(s1,s2)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   181
                                                          .collect(toSet());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
                    assertJmodContent(jmod, expectedFilenames);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   183
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   184
            });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   185
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   186
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   187
    @Test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   188
    public void testLibs() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   189
        Path jmod = MODS_DIR.resolve("fooLibs.jmod");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   190
        FileUtils.deleteFileIfExistsWithRetry(jmod);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   191
        Path cp = EXPLODED_DIR.resolve("foo").resolve("classes");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   192
        Path lp = EXPLODED_DIR.resolve("foo").resolve("lib");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   193
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   194
        jmod("create",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   195
             "--libs=", lp.toString(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   196
             "--class-path", cp.toString(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   197
             jmod.toString())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   198
            .assertSuccess()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   199
            .resultChecker(r -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   200
                try (Stream<String> s1 = findFiles(lp).map(p -> LIBS_PREFIX + p);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   201
                     Stream<String> s2 = findFiles(cp).map(p -> CLASSES_PREFIX + p)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   202
                    Set<String> expectedFilenames = Stream.concat(s1,s2)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   203
                                                          .collect(toSet());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   204
                    assertJmodContent(jmod, expectedFilenames);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   205
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   206
            });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   207
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   209
    @Test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   210
    public void testAll() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   211
        Path jmod = MODS_DIR.resolve("fooAll.jmod");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   212
        FileUtils.deleteFileIfExistsWithRetry(jmod);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   213
        Path cp = EXPLODED_DIR.resolve("foo").resolve("classes");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   214
        Path bp = EXPLODED_DIR.resolve("foo").resolve("bin");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   215
        Path lp = EXPLODED_DIR.resolve("foo").resolve("lib");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   216
        Path cf = EXPLODED_DIR.resolve("foo").resolve("conf");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   217
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   218
        jmod("create",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   219
             "--conf", cf.toString(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   220
             "--cmds=", bp.toString(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   221
             "--libs=", lp.toString(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   222
             "--class-path", cp.toString(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   223
             jmod.toString())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   224
            .assertSuccess()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   225
            .resultChecker(r -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   226
                try (Stream<String> s1 = findFiles(lp).map(p -> LIBS_PREFIX + p);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   227
                     Stream<String> s2 = findFiles(cp).map(p -> CLASSES_PREFIX + p);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   228
                     Stream<String> s3 = findFiles(bp).map(p -> CMDS_PREFIX + p);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   229
                     Stream<String> s4 = findFiles(cf).map(p -> CONFIGS_PREFIX + p)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   230
                    Set<String> expectedFilenames = Stream.concat(Stream.concat(s1,s2),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   231
                                                                  Stream.concat(s3, s4))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   232
                                                          .collect(toSet());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   233
                    assertJmodContent(jmod, expectedFilenames);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   234
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   235
            });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   236
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   237
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   238
    @Test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   239
    public void testExcludes() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   240
        Path jmod = MODS_DIR.resolve("fooLibs.jmod");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
        FileUtils.deleteFileIfExistsWithRetry(jmod);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   242
        Path cp = EXPLODED_DIR.resolve("foo").resolve("classes");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
        Path lp = EXPLODED_DIR.resolve("foo").resolve("lib");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   244
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   245
        jmod("create",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   246
             "--libs=", lp.toString(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   247
             "--class-path", cp.toString(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   248
             "--exclude", "**internal**",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   249
             "--exclude", "first.so",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   250
             jmod.toString())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   251
             .assertSuccess()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   252
             .resultChecker(r -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   253
                 Set<String> expectedFilenames = new HashSet<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   254
                 expectedFilenames.add(CLASSES_PREFIX + "module-info.class");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   255
                 expectedFilenames.add(CLASSES_PREFIX + "jdk/test/foo/Foo.class");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   256
                 expectedFilenames.add(LIBS_PREFIX + "second.so");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   257
                 expectedFilenames.add(LIBS_PREFIX + "third/third.so");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   258
                 assertJmodContent(jmod, expectedFilenames);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   259
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   260
                 Set<String> unexpectedFilenames = new HashSet<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   261
                 unexpectedFilenames.add(CLASSES_PREFIX + "jdk/test/foo/internal/Message.class");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   262
                 unexpectedFilenames.add(LIBS_PREFIX + "first.so");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   263
                 assertJmodDoesNotContain(jmod, unexpectedFilenames);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   264
             });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   265
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   266
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   267
    @Test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   268
    public void describe() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   269
        String cp = EXPLODED_DIR.resolve("foo").resolve("classes").toString();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   270
        jmod("create",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   271
             "--class-path", cp,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   272
              MODS_DIR.resolve("describeFoo.jmod").toString())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   273
             .assertSuccess();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   274
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   275
        jmod("describe",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   276
             MODS_DIR.resolve("describeFoo.jmod").toString())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   277
             .assertSuccess()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   278
             .resultChecker(r -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   279
                 // Expect similar output: "foo,  requires mandated java.base
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   280
                 // exports jdk.test.foo,  conceals jdk.test.foo.internal"
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   281
                 Pattern p = Pattern.compile("\\s+foo\\s+requires\\s+mandated\\s+java.base");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   282
                 assertTrue(p.matcher(r.output).find(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   283
                           "Expecting to find \"foo, requires java.base\"" +
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   284
                                "in output, but did not: [" + r.output + "]");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   285
                 p = Pattern.compile(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   286
                        "exports\\s+jdk.test.foo\\s+conceals\\s+jdk.test.foo.internal");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   287
                 assertTrue(p.matcher(r.output).find(),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   288
                           "Expecting to find \"exports ..., conceals ...\"" +
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   289
                                "in output, but did not: [" + r.output + "]");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   290
             });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   291
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   292
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   293
    @Test
39882
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   294
    public void testDuplicateEntries() throws IOException {
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   295
        Path jmod = MODS_DIR.resolve("testDuplicates.jmod");
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   296
        FileUtils.deleteFileIfExistsWithRetry(jmod);
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   297
        String cp = EXPLODED_DIR.resolve("foo").resolve("classes").toString();
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   298
        Path lp = EXPLODED_DIR.resolve("foo").resolve("lib");
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   299
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   300
        jmod("create",
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   301
             "--class-path", cp + pathSeparator + cp,
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   302
             jmod.toString())
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   303
             .assertSuccess()
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   304
             .resultChecker(r ->
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   305
                 assertContains(r.output, "Warning: ignoring duplicate entry")
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   306
             );
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   307
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   308
        FileUtils.deleteFileIfExistsWithRetry(jmod);
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   309
        jmod("create",
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   310
             "--class-path", cp,
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   311
             "--libs", lp.toString() + pathSeparator + lp.toString(),
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   312
             jmod.toString())
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   313
             .assertSuccess()
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   314
             .resultChecker(r ->
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   315
                 assertContains(r.output, "Warning: ignoring duplicate entry")
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   316
             );
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   317
    }
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   318
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   319
    @Test
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   320
    public void testIgnoreModuleInfoInOtherSections() throws IOException {
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   321
        Path jmod = MODS_DIR.resolve("testIgnoreModuleInfoInOtherSections.jmod");
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   322
        FileUtils.deleteFileIfExistsWithRetry(jmod);
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   323
        String cp = EXPLODED_DIR.resolve("foo").resolve("classes").toString();
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   324
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   325
        jmod("create",
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   326
            "--class-path", cp,
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   327
            "--libs", cp,
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   328
            jmod.toString())
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   329
            .assertSuccess()
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   330
            .resultChecker(r ->
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   331
                assertContains(r.output, "Warning: ignoring entry")
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   332
            );
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   333
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   334
        FileUtils.deleteFileIfExistsWithRetry(jmod);
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   335
        jmod("create",
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   336
             "--class-path", cp,
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   337
             "--cmds", cp,
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   338
             jmod.toString())
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   339
             .assertSuccess()
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   340
             .resultChecker(r ->
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   341
                 assertContains(r.output, "Warning: ignoring entry")
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   342
             );
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   343
    }
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   344
2a5433a2eca5 8134779: (jmod) ZipException is thrown if there are duplicate resources
chegar
parents: 36511
diff changeset
   345
    @Test
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   346
    public void testVersion() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   347
        jmod("--version")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   348
            .assertSuccess()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   349
            .resultChecker(r -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   350
                assertContains(r.output, System.getProperty("java.version"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   351
            });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   352
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   353
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   354
    @Test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   355
    public void testHelp() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   356
        jmod("--help")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   357
            .assertSuccess()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   358
            .resultChecker(r ->
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   359
                assertTrue(r.output.startsWith("Usage: jmod"), "Help not printed")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   360
            );
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   361
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   362
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   363
    @Test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   364
    public void testTmpFileAlreadyExists() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   365
        // Implementation detail: jmod tool creates <jmod-file>.tmp
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   366
        // Ensure that there are no problems if existing
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   367
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   368
        Path jmod = MODS_DIR.resolve("testTmpFileAlreadyExists.jmod");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   369
        Path tmp = MODS_DIR.resolve("testTmpFileAlreadyExists.jmod.tmp");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   370
        FileUtils.deleteFileIfExistsWithRetry(jmod);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   371
        FileUtils.deleteFileIfExistsWithRetry(tmp);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   372
        Files.createFile(tmp);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   373
        String cp = EXPLODED_DIR.resolve("foo").resolve("classes").toString();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   374
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   375
        jmod("create",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   376
             "--class-path", cp,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   377
             jmod.toString())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   378
            .assertSuccess()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   379
            .resultChecker(r ->
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   380
                assertTrue(Files.notExists(tmp), "Unexpected tmp file:" + tmp)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   381
            );
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   382
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   383
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   384
    @Test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   385
    public void testTmpFileRemoved() throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   386
        // Implementation detail: jmod tool creates <jmod-file>.tmp
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   387
        // Ensure that it is removed in the event of a failure.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   388
        // The failure in this case is a class in the unnamed package.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   389
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   390
        Path jmod = MODS_DIR.resolve("testTmpFileRemoved.jmod");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   391
        Path tmp = MODS_DIR.resolve("testTmpFileRemoved.jmod.tmp");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   392
        FileUtils.deleteFileIfExistsWithRetry(jmod);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   393
        FileUtils.deleteFileIfExistsWithRetry(tmp);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   394
        String cp = EXPLODED_DIR.resolve("foo").resolve("classes") + File.pathSeparator +
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   395
                    EXPLODED_DIR.resolve("foo").resolve("classes")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   396
                                .resolve("jdk").resolve("test").resolve("foo").toString();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   397
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   398
        jmod("create",
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   399
             "--class-path", cp,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   400
             jmod.toString())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   401
             .assertFailure()
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   402
             .resultChecker(r -> {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   403
                 assertContains(r.output, "unnamed package");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   404
                 assertTrue(Files.notExists(tmp), "Unexpected tmp file:" + tmp);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   405
             });
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   406
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   407
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   408
    // ---
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   409
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   410
    static boolean compileModule(String name, Path dest) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   411
        return CompilerUtils.compile(SRC_DIR.resolve(name), dest);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   412
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   413
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   414
    static void assertContains(String output, String subString) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   415
        if (output.contains(subString))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   416
            assertTrue(true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   417
        else
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   418
            assertTrue(false,"Expected to find [" + subString + "], in output ["
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   419
                           + output + "]" + "\n");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   420
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   421
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   422
    static ModuleDescriptor getModuleDescriptor(Path jmod) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   423
        ClassLoader cl = ClassLoader.getSystemClassLoader();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   424
        try (FileSystem fs = FileSystems.newFileSystem(jmod, cl)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   425
            String p = "/classes/module-info.class";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   426
            try (InputStream is = Files.newInputStream(fs.getPath(p))) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   427
                return ModuleDescriptor.read(is);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   428
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   429
        } catch (IOException ioe) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   430
            throw new UncheckedIOException(ioe);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   431
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   432
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   433
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   434
    static Stream<String> findFiles(Path dir) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   435
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   436
            return Files.find(dir, Integer.MAX_VALUE, (p, a) -> a.isRegularFile())
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   437
                        .map(dir::relativize)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   438
                        .map(Path::toString)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   439
                        .map(p -> p.replace(File.separator, "/"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   440
        } catch (IOException x) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   441
            throw new UncheckedIOException(x);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   442
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   443
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   444
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   445
    static Set<String> getJmodContent(Path jmod) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   446
        JmodResult r = jmod("list", jmod.toString()).assertSuccess();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   447
        return Stream.of(r.output.split("\r?\n")).collect(toSet());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   448
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   449
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   450
    static void assertJmodContent(Path jmod, Set<String> expected) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   451
        Set<String> actual = getJmodContent(jmod);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   452
        if (!Objects.equals(actual, expected)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   453
            Set<String> unexpected = new HashSet<>(actual);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   454
            unexpected.removeAll(expected);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   455
            Set<String> notFound = new HashSet<>(expected);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   456
            notFound.removeAll(actual);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   457
            StringBuilder sb = new StringBuilder();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   458
            sb.append("Unexpected but found:\n");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   459
            unexpected.forEach(s -> sb.append("\t" + s + "\n"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   460
            sb.append("Expected but not found:\n");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   461
            notFound.forEach(s -> sb.append("\t" + s + "\n"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   462
            assertTrue(false, "Jmod content check failed.\n" + sb.toString());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   463
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   464
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   465
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   466
    static void assertJmodDoesNotContain(Path jmod, Set<String> unexpectedNames) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   467
        Set<String> actual = getJmodContent(jmod);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   468
        Set<String> unexpected = new HashSet<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   469
        for (String name : unexpectedNames) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   470
            if (actual.contains(name))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   471
                unexpected.add(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   472
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   473
        if (!unexpected.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   474
            StringBuilder sb = new StringBuilder();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   475
            for (String s : unexpected)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   476
                sb.append("Unexpected but found: " + s + "\n");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   477
            sb.append("In :");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   478
            for (String s : actual)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   479
                sb.append("\t" + s + "\n");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   480
            assertTrue(false, "Jmod content check failed.\n" + sb.toString());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   481
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   482
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   483
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   484
    static JmodResult jmod(String... args) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   485
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   486
        PrintStream ps = new PrintStream(baos);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   487
        System.out.println("jmod " + Arrays.asList(args));
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 39882
diff changeset
   488
        int ec = JMOD_TOOL.run(ps, ps, args);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   489
        return new JmodResult(ec, new String(baos.toByteArray(), UTF_8));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   490
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   491
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   492
    static class JmodResult {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   493
        final int exitCode;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   494
        final String output;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   495
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   496
        JmodResult(int exitValue, String output) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   497
            this.exitCode = exitValue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   498
            this.output = output;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   499
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   500
        JmodResult assertSuccess() { assertTrue(exitCode == 0, output); return this; }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   501
        JmodResult assertFailure() { assertTrue(exitCode != 0, output); return this; }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   502
        JmodResult resultChecker(Consumer<JmodResult> r) { r.accept(this); return this; }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   503
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   504
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   505
    static void createCmds(Path dir) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   506
        List<String> files = Arrays.asList(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   507
                "first", "second", "third" + File.separator + "third");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   508
        createFiles(dir, files);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   509
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   510
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   511
    static void createLibs(Path dir) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   512
        List<String> files = Arrays.asList(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   513
                "first.so", "second.so", "third" + File.separator + "third.so");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   514
        createFiles(dir, files);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   515
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   516
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   517
    static void createConfigs(Path dir) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   518
        List<String> files = Arrays.asList(
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   519
                "first.cfg", "second.cfg", "third" + File.separator + "third.cfg");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   520
        createFiles(dir, files);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   521
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   522
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   523
    static void createFiles(Path dir, List<String> filenames) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   524
        for (String name : filenames) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   525
            Path file = dir.resolve(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   526
            Files.createDirectories(file.getParent());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   527
            Files.createFile(file);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   528
            try (OutputStream os  = Files.newOutputStream(file)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   529
                os.write("blahblahblah".getBytes(UTF_8));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   530
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   531
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   532
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   533
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   534
    // Standalone entry point.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   535
    public static void main(String[] args) throws Throwable {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   536
        JmodTest test = new JmodTest();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   537
        test.buildExplodedModules();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   538
        for (Method m : JmodTest.class.getDeclaredMethods()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   539
            if (m.getAnnotation(Test.class) != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   540
                System.out.println("Invoking " + m.getName());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   541
                m.invoke(test);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   542
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   543
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   544
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   545
}