test/jdk/tools/jmod/hashes/HashesTest.java
author eosterlund
Wed, 21 Mar 2018 14:38:32 +0100
changeset 49484 ee8fa73b90f9
parent 48841 0937e5f799df
child 52111 367b2cd49ec5
permissions -rw-r--r--
8198949: Modularize arraycopy stub routine GC barriers Reviewed-by: rkennke, pliden
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
     1
/**
48841
0937e5f799df 8190378: Removal of CORBA and Java EE modules
lancea
parents: 47216
diff changeset
     2
 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
     4
 *
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
     8
 *
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    13
 * accompanied this code).
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    14
 *
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    18
 *
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    21
 * questions.
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    22
 */
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    23
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    24
/*
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    25
 * @test
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
    26
 * @bug 8160286
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    27
 * @summary Test the recording and checking of module hashes
45191
ba48b1679390 8180724: move ModuleInfoMaker to the top level testlibrary
iignatyev
parents: 43712
diff changeset
    28
 * @library /test/lib
41817
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41484
diff changeset
    29
 * @modules java.base/jdk.internal.misc
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41484
diff changeset
    30
 *          java.base/jdk.internal.module
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
    31
 *          jdk.compiler
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
    32
 *          jdk.jartool
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 40261
diff changeset
    33
 *          jdk.jlink
45467
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 45191
diff changeset
    34
 * @build jdk.test.lib.compiler.ModuleInfoMaker
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 45191
diff changeset
    35
 *        jdk.test.lib.compiler.CompilerUtils
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    36
 * @run testng HashesTest
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    37
 */
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    38
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
    39
import java.io.File;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    40
import java.io.IOException;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    41
import java.io.InputStream;
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
    42
import java.io.UncheckedIOException;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    43
import java.lang.module.ModuleDescriptor;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    44
import java.lang.module.ModuleFinder;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    45
import java.lang.module.ModuleReader;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    46
import java.lang.module.ModuleReference;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    47
import java.nio.file.FileVisitResult;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    48
import java.nio.file.Files;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    49
import java.nio.file.Path;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    50
import java.nio.file.Paths;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    51
import java.nio.file.SimpleFileVisitor;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    52
import java.nio.file.attribute.BasicFileAttributes;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    53
import java.util.ArrayList;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    54
import java.util.Arrays;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    55
import java.util.Collections;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    56
import java.util.List;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    57
import java.util.Set;
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 40261
diff changeset
    58
import java.util.spi.ToolProvider;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    59
import java.util.stream.Collectors;
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
    60
import java.util.stream.Stream;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    61
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 41817
diff changeset
    62
import jdk.internal.module.ModuleInfo;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    63
import jdk.internal.module.ModuleHashes;
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 41817
diff changeset
    64
import jdk.internal.module.ModulePath;
41817
b90ad1de93ea 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41484
diff changeset
    65
45191
ba48b1679390 8180724: move ModuleInfoMaker to the top level testlibrary
iignatyev
parents: 43712
diff changeset
    66
import jdk.test.lib.compiler.ModuleInfoMaker;
ba48b1679390 8180724: move ModuleInfoMaker to the top level testlibrary
iignatyev
parents: 43712
diff changeset
    67
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    68
import org.testng.annotations.Test;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    69
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    70
import static org.testng.Assert.*;
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
    71
import static java.lang.module.ModuleDescriptor.Requires.Modifier.*;
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    72
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    73
public class HashesTest {
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 40261
diff changeset
    74
    static final ToolProvider JMOD_TOOL = ToolProvider.findFirst("jmod")
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 40261
diff changeset
    75
        .orElseThrow(() ->
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 40261
diff changeset
    76
            new RuntimeException("jmod tool not found")
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 40261
diff changeset
    77
        );
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
    78
    static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
    79
        .orElseThrow(() ->
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
    80
            new RuntimeException("jar tool not found")
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
    81
        );
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
    82
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
    83
    private final Path mods;
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
    84
    private final Path srcDir;
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
    85
    private final Path lib;
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
    86
    private final ModuleInfoMaker builder;
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
    87
    HashesTest(Path dest) throws IOException {
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
    88
        if (Files.exists(dest)) {
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
    89
            deleteDirectory(dest);
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
    90
        }
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
    91
        this.mods = dest.resolve("mods");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
    92
        this.srcDir = dest.resolve("src");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
    93
        this.lib = dest.resolve("lib");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
    94
        this.builder = new ModuleInfoMaker(srcDir);
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
    95
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
    96
        Files.createDirectories(lib);
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
    97
        Files.createDirectories(mods);
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
    98
    }
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
    99
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   100
    @Test
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   101
    public static void test() throws IOException {
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   102
        Path dest = Paths.get("test");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   103
        HashesTest ht = new HashesTest(dest);
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   104
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   105
        // create modules for test cases
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   106
        ht.makeModule("m2");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   107
        ht.makeModule("m3");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   108
        ht.makeModule("m1", "m2", "m3");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   110
        ht.makeModule("org.bar", TRANSITIVE, "m1");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   111
        ht.makeModule("org.foo", TRANSITIVE, "org.bar");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   112
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   113
        // create JMOD for m1, m2, m3
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   114
        ht.makeJmod("m2");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   115
        ht.makeJmod("m3");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   116
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   117
        // no hash is recorded since m1 has outgoing edges
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   118
        ht.jmodHashModules("m1", ".*");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   119
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   120
        // no hash is recorded in m1, m2, m3
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   121
        assertTrue(ht.hashes("m1") == null);
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   122
        assertTrue(ht.hashes("m2") == null);
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   123
        assertTrue(ht.hashes("m3") == null);
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   124
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   125
        // hash m1 in m2
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   126
        ht.jmodHashModules("m2",  "m1");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   127
        ht.checkHashes("m2", "m1");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   128
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   129
        // hash m1 in m2
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   130
        ht.jmodHashModules("m2",  ".*");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   131
        ht.checkHashes("m2", "m1");
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   132
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   133
        // create m2.jmod with no hash
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   134
        ht.makeJmod("m2");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   135
        // run jmod hash command to hash m1 in m2 and m3
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   136
        runJmod(List.of("hash", "--module-path", ht.lib.toString(),
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   137
                        "--hash-modules", ".*"));
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   138
        ht.checkHashes("m2", "m1");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   139
        ht.checkHashes("m3", "m1");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   140
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   141
        // check transitive requires
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   142
        ht.makeJmod("org.bar");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   143
        ht.makeJmod("org.foo");
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   144
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   145
        ht.jmodHashModules("org.bar", "org.*");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   146
        ht.checkHashes("org.bar", "org.foo");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   147
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   148
        ht.jmodHashModules( "m3", ".*");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   149
        ht.checkHashes("m3", "org.foo", "org.bar", "m1");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   150
    }
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   151
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   152
    @Test
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   153
    public static void multiBaseModules() throws IOException {
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   154
        Path dest = Paths.get("test2");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   155
        HashesTest ht = new HashesTest(dest);
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   156
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   157
        /*
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   158
         * y2 -----------> y1
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   159
         *    |______
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   160
         *    |      |
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   161
         *    V      V
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   162
         *    z3 -> z2
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   163
         *    |      |
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   164
         *    |      V
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   165
         *    |---> z1
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   166
         */
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   167
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   168
        ht.makeModule("z1");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   169
        ht.makeModule("z2", "z1");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   170
        ht.makeModule("z3", "z1", "z2");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   171
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   172
        ht.makeModule("y1");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   173
        ht.makeModule("y2", "y1", "z2", "z3");
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   174
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   175
        Set<String> ys = Set.of("y1", "y2");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   176
        Set<String> zs = Set.of("z1", "z2", "z3");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   177
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   178
        // create JMOD files
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   179
        Stream.concat(ys.stream(), zs.stream()).forEach(ht::makeJmod);
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   180
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   181
        // run jmod hash command
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   182
        runJmod(List.of("hash", "--module-path", ht.lib.toString(),
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   183
                        "--hash-modules", ".*"));
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   184
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   185
        /*
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   186
         * z1 and y1 are the modules with hashes recorded.
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   187
         */
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   188
        ht.checkHashes("y1", "y2");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   189
        ht.checkHashes("z1", "z2", "z3", "y2");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   190
        Stream.concat(ys.stream(), zs.stream())
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   191
              .filter(mn -> !mn.equals("y1") && !mn.equals("z1"))
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   192
              .forEach(mn -> assertTrue(ht.hashes(mn) == null));
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   193
    }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   194
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   195
    @Test
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   196
    public static void mixJmodAndJarFile() throws IOException {
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   197
        Path dest = Paths.get("test3");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   198
        HashesTest ht = new HashesTest(dest);
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   199
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   200
        /*
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   201
         * j3 -----------> j2
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   202
         *    |______
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   203
         *    |      |
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   204
         *    V      V
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   205
         *    m3 -> m2
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   206
         *    |      |
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   207
         *    |      V
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   208
         *    |---> m1 -> j1 -> jdk.jlink
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   209
         */
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   210
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   211
        ht.makeModule("j1");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   212
        ht.makeModule("j2");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   213
        ht.makeModule("m1", "j1");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   214
        ht.makeModule("m2", "m1");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   215
        ht.makeModule("m3", "m1", "m2");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   216
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   217
        ht.makeModule("j3", "j2", "m2", "m3");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   218
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   219
        Set<String> jars = Set.of("j1", "j2", "j3");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   220
        Set<String> jmods = Set.of("m1", "m2", "m3");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   221
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   222
        // create JMOD and JAR files
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   223
        jars.forEach(ht::makeJar);
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   224
        jmods.forEach(ht::makeJmod);
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   225
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   226
        // run jmod hash command
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   227
        runJmod(List.of("hash", "--module-path", ht.lib.toString(),
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   228
                        "--hash-modules", "^j.*|^m.*"));
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   229
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   230
        /*
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   231
         * j1 and j2 are the modules with hashes recorded.
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   232
         */
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   233
        ht.checkHashes("j2", "j3");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   234
        ht.checkHashes("j1", "m1", "m2", "m3", "j3");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   235
        Stream.concat(jars.stream(), jmods.stream())
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   236
              .filter(mn -> !mn.equals("j1") && !mn.equals("j2"))
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   237
              .forEach(mn -> assertTrue(ht.hashes(mn) == null));
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   238
    }
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   239
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   240
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   241
    @Test
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   242
    public static void testImageJmods() throws IOException {
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   243
        Path mpath = Paths.get(System.getProperty("java.home"), "jmods");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   244
        if (!Files.exists(mpath)) {
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   245
            return;
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   246
        }
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   247
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   248
        Path dest = Paths.get("test5");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   249
        HashesTest ht = new HashesTest(dest);
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   250
        ht.makeModule("m1", "jdk.compiler", "jdk.attach");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   251
        ht.makeModule("m2", "m1");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   252
        ht.makeModule("m3", "java.compiler");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   253
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   254
        ht.makeJmod("m1");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   255
        ht.makeJmod("m2");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   256
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   257
        runJmod(List.of("hash",
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   258
                        "--module-path",
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   259
                        mpath.toString() + File.pathSeparator + ht.lib.toString(),
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   260
                        "--hash-modules", ".*"));
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   261
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   262
        validateImageJmodsTest(ht, mpath);
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   263
    }
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   264
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   265
    @Test
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   266
    public static void testImageJmods1() throws IOException {
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   267
        Path mpath = Paths.get(System.getProperty("java.home"), "jmods");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   268
        if (!Files.exists(mpath)) {
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   269
            return;
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   270
        }
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   271
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   272
        Path dest = Paths.get("test6");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   273
        HashesTest ht = new HashesTest(dest);
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   274
        ht.makeModule("m1", "jdk.compiler", "jdk.attach");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   275
        ht.makeModule("m2", "m1");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   276
        ht.makeModule("m3", "java.compiler");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   277
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   278
        ht.makeJar("m2");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   279
        ht.makeJar("m1",
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   280
                    "--module-path",
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   281
                    mpath.toString() + File.pathSeparator + ht.lib.toString(),
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   282
                    "--hash-modules", ".*");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   283
        validateImageJmodsTest(ht, mpath);
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   284
    }
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   285
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   286
    private static void validateImageJmodsTest(HashesTest ht, Path mpath)
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   287
        throws IOException
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   288
    {
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   289
        // hash is recorded in m1 and not any other packaged modules on module path
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   290
        ht.checkHashes("m1", "m2");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   291
        assertTrue(ht.hashes("m2") == null);
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   292
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   293
        // should not override any JDK packaged modules
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43109
diff changeset
   294
        ModuleFinder finder = ModulePath.of(Runtime.version(), true, mpath);
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   295
        assertTrue(ht.hashes(finder,"jdk.compiler") == null);
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   296
        assertTrue(ht.hashes(finder,"jdk.attach") == null);
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   297
    }
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   298
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   299
    private void checkHashes(String mn, String... hashModules) throws IOException {
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   300
        ModuleHashes hashes = hashes(mn);
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   301
        assertTrue(hashes.names().equals(Set.of(hashModules)));
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   302
    }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   303
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   304
    private ModuleHashes hashes(String name) {
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43109
diff changeset
   305
        ModuleFinder finder = ModulePath.of(Runtime.version(), true, lib);
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   306
        return hashes(finder, name);
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   307
    }
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   308
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   309
    private ModuleHashes hashes(ModuleFinder finder, String name) {
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   310
        ModuleReference mref = finder.find(name).orElseThrow(RuntimeException::new);
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   311
        try {
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   312
            ModuleReader reader = mref.open();
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   313
            try (InputStream in = reader.open("module-info.class").get()) {
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   314
                ModuleHashes hashes = ModuleInfo.read(in, null).recordedHashes();
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   315
                System.out.format("hashes in module %s %s%n", name,
42703
20c39ea4a507 8170987: Module system implementation refresh (12/2016)
alanb
parents: 41817
diff changeset
   316
                    (hashes != null) ? "present" : "absent");
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   317
                if (hashes != null) {
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   318
                    hashes.names().stream().sorted().forEach(n ->
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   319
                        System.out.format("  %s %s%n", n, toHex(hashes.hashFor(n)))
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   320
                    );
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   321
                }
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   322
                return hashes;
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   323
            } finally {
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   324
                reader.close();
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   325
            }
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   326
        } catch (IOException e) {
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   327
            throw new UncheckedIOException(e);
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   328
        }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   329
    }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   330
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   331
    private String toHex(byte[] ba) {
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   332
        StringBuilder sb = new StringBuilder(ba.length);
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   333
        for (byte b: ba) {
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   334
            sb.append(String.format("%02x", b & 0xff));
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   335
        }
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   336
        return sb.toString();
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   337
    }
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   338
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   339
    private void deleteDirectory(Path dir) throws IOException {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   340
        Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   341
            @Override
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   342
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   343
                throws IOException
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   344
            {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   345
                Files.delete(file);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   346
                return FileVisitResult.CONTINUE;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   347
            }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   348
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   349
            @Override
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   350
            public FileVisitResult postVisitDirectory(Path dir, IOException exc)
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   351
                throws IOException
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   352
            {
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   353
                Files.delete(dir);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   354
                return FileVisitResult.CONTINUE;
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   355
            }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   356
        });
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   357
    }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   358
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   359
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   360
    private void makeModule(String mn, String... deps) throws IOException {
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   361
        makeModule(mn, null, deps);
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   362
    }
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   363
45191
ba48b1679390 8180724: move ModuleInfoMaker to the top level testlibrary
iignatyev
parents: 43712
diff changeset
   364
    private void makeModule(String mn, ModuleDescriptor.Requires.Modifier mod, String... deps)
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   365
        throws IOException
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   366
    {
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   367
        if (mod != null && mod != TRANSITIVE && mod != STATIC) {
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   368
            throw new IllegalArgumentException(mod.toString());
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   369
        }
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   370
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   371
        StringBuilder sb = new StringBuilder();
45191
ba48b1679390 8180724: move ModuleInfoMaker to the top level testlibrary
iignatyev
parents: 43712
diff changeset
   372
        sb.append("module ")
ba48b1679390 8180724: move ModuleInfoMaker to the top level testlibrary
iignatyev
parents: 43712
diff changeset
   373
          .append(mn)
ba48b1679390 8180724: move ModuleInfoMaker to the top level testlibrary
iignatyev
parents: 43712
diff changeset
   374
          .append(" {")
ba48b1679390 8180724: move ModuleInfoMaker to the top level testlibrary
iignatyev
parents: 43712
diff changeset
   375
          .append("\n");
ba48b1679390 8180724: move ModuleInfoMaker to the top level testlibrary
iignatyev
parents: 43712
diff changeset
   376
        Arrays.stream(deps)
ba48b1679390 8180724: move ModuleInfoMaker to the top level testlibrary
iignatyev
parents: 43712
diff changeset
   377
              .forEach(req -> {
ba48b1679390 8180724: move ModuleInfoMaker to the top level testlibrary
iignatyev
parents: 43712
diff changeset
   378
                  sb.append("    requires ");
ba48b1679390 8180724: move ModuleInfoMaker to the top level testlibrary
iignatyev
parents: 43712
diff changeset
   379
                  if (mod != null) {
ba48b1679390 8180724: move ModuleInfoMaker to the top level testlibrary
iignatyev
parents: 43712
diff changeset
   380
                      sb.append(mod.toString().toLowerCase())
ba48b1679390 8180724: move ModuleInfoMaker to the top level testlibrary
iignatyev
parents: 43712
diff changeset
   381
                        .append(" ");
ba48b1679390 8180724: move ModuleInfoMaker to the top level testlibrary
iignatyev
parents: 43712
diff changeset
   382
                  }
ba48b1679390 8180724: move ModuleInfoMaker to the top level testlibrary
iignatyev
parents: 43712
diff changeset
   383
                  sb.append(req)
ba48b1679390 8180724: move ModuleInfoMaker to the top level testlibrary
iignatyev
parents: 43712
diff changeset
   384
                    .append(";\n");
ba48b1679390 8180724: move ModuleInfoMaker to the top level testlibrary
iignatyev
parents: 43712
diff changeset
   385
              });
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   386
        sb.append("}\n");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   387
        builder.writeJavaFiles(mn, sb.toString());
45191
ba48b1679390 8180724: move ModuleInfoMaker to the top level testlibrary
iignatyev
parents: 43712
diff changeset
   388
        builder.compile(mn, mods);
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   389
    }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   390
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   391
    private void jmodHashModules(String moduleName, String hashModulesPattern) {
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   392
        makeJmod(moduleName, "--module-path", lib.toString(),
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   393
                 "--hash-modules", hashModulesPattern);
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   394
    }
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   395
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   396
    private void makeJmod(String moduleName, String... options) {
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   397
        Path mclasses = mods.resolve(moduleName);
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   398
        Path outfile = lib.resolve(moduleName + ".jmod");
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   399
        List<String> args = new ArrayList<>();
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   400
        args.add("create");
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   401
        Collections.addAll(args, options);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   402
        Collections.addAll(args, "--class-path", mclasses.toString(),
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   403
                           outfile.toString());
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   404
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   405
        if (Files.exists(outfile)) {
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   406
            try {
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   407
                Files.delete(outfile);
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   408
            } catch (IOException e) {
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   409
                throw new UncheckedIOException(e);
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   410
            }
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   411
        }
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   412
        runJmod(args);
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   413
    }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   414
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   415
    private static void runJmod(List<String> args) {
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 40261
diff changeset
   416
        int rc = JMOD_TOOL.run(System.out, System.out, args.toArray(new String[args.size()]));
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   417
        System.out.println("jmod " + args.stream().collect(Collectors.joining(" ")));
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   418
        if (rc != 0) {
43109
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   419
            throw new AssertionError("jmod failed: rc = " + rc);
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   420
        }
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   421
    }
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   422
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   423
    private void makeJar(String moduleName, String... options) {
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   424
        Path mclasses = mods.resolve(moduleName);
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   425
        Path outfile = lib.resolve(moduleName + ".jar");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   426
        List<String> args = new ArrayList<>();
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   427
        Stream.concat(Stream.of("--create",
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   428
                                "--file=" + outfile.toString()),
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   429
                      Arrays.stream(options))
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   430
              .forEach(args::add);
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   431
        args.add("-C");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   432
        args.add(mclasses.toString());
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   433
        args.add(".");
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   434
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   435
        if (Files.exists(outfile)) {
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   436
            try {
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   437
                Files.delete(outfile);
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   438
            } catch (IOException e) {
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   439
                throw new UncheckedIOException(e);
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   440
            }
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   441
        }
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   442
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   443
        int rc = JAR_TOOL.run(System.out, System.out, args.toArray(new String[args.size()]));
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   444
        System.out.println("jar " + args.stream().collect(Collectors.joining(" ")));
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   445
        if (rc != 0) {
fe275140c3f1 8160286: jmod hash is creating unlinkable modules
mchung
parents: 42703
diff changeset
   446
            throw new AssertionError("jar failed: rc = " + rc);
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   447
        }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   448
    }
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents:
diff changeset
   449
}