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