jdk/test/tools/jlink/plugins/LegalFilePluginTest.java
author alanb
Fri, 07 Apr 2017 08:05:54 +0000
changeset 44545 83b611b88ac8
parent 42670 d833113eb7d7
child 45393 de4e1efc8eec
permissions -rw-r--r--
8177530: Module system implementation refresh (4/2017) Reviewed-by: mchung, alanb Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42670
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
     1
/**
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
     4
 *
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
     7
 * published by the Free Software Foundation.
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
     8
 *
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    13
 * accompanied this code).
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    14
 *
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    18
 *
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    21
 * questions.
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    22
 */
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    23
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    24
/*
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    25
 * @test
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    26
 * @bug 8169925
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    27
 * @summary Validate the license files deduplicated in the image
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    28
 * @library /lib/testlibrary
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    29
 * @modules jdk.compiler
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    30
 *          jdk.jlink
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    31
 * @build CompilerUtils
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    32
 * @run testng LegalFilePluginTest
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    33
 */
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    34
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    35
import java.io.BufferedWriter;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    36
import java.io.File;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    37
import java.io.IOException;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    38
import java.io.PrintWriter;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    39
import java.io.StringWriter;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    40
import java.io.UncheckedIOException;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    41
import java.nio.file.FileVisitResult;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    42
import java.nio.file.Files;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    43
import java.nio.file.Path;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    44
import java.nio.file.Paths;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    45
import java.nio.file.SimpleFileVisitor;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    46
import java.nio.file.attribute.BasicFileAttributes;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    47
import java.util.ArrayList;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    48
import java.util.HashMap;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    49
import java.util.HashSet;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    50
import java.util.List;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    51
import java.util.Map;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    52
import java.util.Set;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    53
import java.util.spi.ToolProvider;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    54
import java.util.stream.Collectors;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    55
import java.util.stream.Stream;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    56
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    57
import org.testng.annotations.BeforeTest;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    58
import org.testng.annotations.DataProvider;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    59
import org.testng.annotations.Test;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    60
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    61
import static org.testng.Assert.*;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    62
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    63
public class LegalFilePluginTest {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    64
    static final ToolProvider JMOD_TOOL = ToolProvider.findFirst("jmod")
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    65
        .orElseThrow(() ->
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    66
            new RuntimeException("jmod tool not found")
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    67
        );
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    68
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    69
    static final ToolProvider JLINK_TOOL = ToolProvider.findFirst("jlink")
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    70
        .orElseThrow(() ->
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    71
            new RuntimeException("jlink tool not found")
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    72
        );
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    73
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    74
    static final Path MODULE_PATH = Paths.get(System.getProperty("java.home"), "jmods");
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    75
    static final Path SRC_DIR = Paths.get("src");
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    76
    static final Path MODS_DIR = Paths.get("mods");
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    77
    static final Path JMODS_DIR = Paths.get("jmods");
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    78
    static final Path LEGAL_DIR = Paths.get("legal");
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    79
    static final Path IMAGES_DIR = Paths.get("images");
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    80
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    81
    static final Map<List<String>, Map<String,String>> LICENSES = Map.of(
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    82
        // Key is module name and requires
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    83
        // Value is a map of filename to the file content
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    84
        List.of("m1"),       Map.of("LICENSE",         "m1 LICENSE",
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    85
                                    "m1-license.txt",  "m1 license",
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    86
                                    "test-license",    "test license v1"),
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    87
        List.of("m2", "m1"), Map.of("m2-license",      "m2 license",
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    88
                                    "test-license",    "test license v1"),
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    89
        List.of("m3"),       Map.of("m3-license.md",   "m3 license",
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    90
                                    "test-license",    "test license v3"),
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    91
        List.of("m4"),       Map.of("test-license",    "test license v4")
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    92
    );
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    93
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    94
    @BeforeTest
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    95
    private void setup() throws Exception {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    96
        List<JmodFileBuilder> builders = new ArrayList<>();
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    97
        for (Map.Entry<List<String>, Map<String,String>> e : LICENSES.entrySet()) {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    98
            List<String> names = e.getKey();
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
    99
            String mn = names.get(0);
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   100
            JmodFileBuilder builder = new JmodFileBuilder(mn);
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   101
            builders.add(builder);
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   102
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   103
            if (names.size() > 1) {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   104
                names.subList(1, names.size())
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   105
                     .stream()
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   106
                     .forEach(builder::requires);
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   107
            }
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   108
            e.getValue().entrySet()
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   109
               .stream()
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   110
               .forEach(f -> builder.licenseFile(f.getKey(), f.getValue()));
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   111
            // generate source
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   112
            builder.writeModuleInfo();
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   113
        }
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   114
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   115
        // create jmod file
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   116
        for (JmodFileBuilder builder: builders) {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   117
            builder.build();
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   118
        }
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   119
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   120
    }
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   121
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   122
    private String imageDir(String dir) {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   123
        return IMAGES_DIR.resolve(dir).toString();
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   124
    }
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   125
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   126
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   127
    @DataProvider(name = "modules")
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   128
    public Object[][] jlinkoptions() {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   129
        String m2TestLicenseContent =
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   130
            symlinkContent(Paths.get("legal", "m2", "test-license"),
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   131
                           Paths.get("legal", "m1", "test-license"),
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   132
                            "test license v1");
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   133
        // options and expected header files & man pages
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   134
        return new Object[][] {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   135
            {   new String [] {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   136
                    "test1",
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   137
                    "--add-modules=m1",
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   138
                },
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   139
                Map.of( "m1/LICENSE",        "m1 LICENSE",
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   140
                        "m1/m1-license.txt", "m1 license",
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   141
                        "m1/test-license",   "test license v1")
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   142
            },
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   143
            {   new String [] {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   144
                    "test2",
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   145
                    "--add-modules=m1,m2",
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   146
                },
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   147
                Map.of( "m1/LICENSE",        "m1 LICENSE",
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   148
                        "m1/m1-license.txt", "m1 license",
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   149
                        "m1/test-license",   "test license v1",
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   150
                        "m2/m2-license",     "m2 license",
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   151
                        "m2/test-license",   m2TestLicenseContent),
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   152
            },
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   153
            {   new String [] {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   154
                "test3",
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   155
                "--add-modules=m2,m3",
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   156
            },
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   157
                Map.of( "m1/LICENSE",        "m1 LICENSE",
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   158
                        "m1/m1-license.txt", "m1 license",
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   159
                        "m1/test-license",   "test license v1",
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   160
                        "m2/m2-license",     "m2 license",
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   161
                        "m2/test-license",   m2TestLicenseContent,
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   162
                        "m3/m3-license.md",  "m3 license",
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   163
                        "m3/test-license",   "test license v3"),
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   164
            },
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   165
        };
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   166
    }
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   167
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   168
    private static String symlinkContent(Path source, Path target, String content) {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   169
        String osName = System.getProperty("os.name");
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   170
        if (!osName.startsWith("Windows") && MODULE_PATH.getFileSystem()
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   171
                                                        .supportedFileAttributeViews()
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   172
                                                        .contains("posix")) {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   173
            // symlink created
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   174
            return content;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   175
        } else {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   176
            // tiny file is created
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   177
            Path symlink = source.getParent().relativize(target);
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   178
            return String.format("Please see %s", symlink.toString());
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   179
        }
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   180
    }
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   181
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   182
    @Test(dataProvider = "modules")
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   183
    public void test(String[] opts, Map<String,String> expectedFiles) throws Exception {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   184
        if (Files.notExists(MODULE_PATH)) {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   185
            // exploded image
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   186
            return;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   187
        }
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   188
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   189
        String dir = opts[0];
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   190
        List<String> options = new ArrayList<>();
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   191
        for (int i = 1; i < opts.length; i++) {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   192
            options.add(opts[i]);
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   193
        }
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   194
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   195
        String mpath = MODULE_PATH.toString() + File.pathSeparator +
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   196
                       JMODS_DIR.toString();
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   197
        Stream.of("--module-path", mpath,
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   198
                  "--output", imageDir(dir))
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   199
              .forEach(options::add);
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   200
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   201
        Path image = createImage(dir, options);
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   202
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   203
        Files.walk(image.resolve("legal"), Integer.MAX_VALUE)
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   204
            .filter(p -> Files.isRegularFile(p))
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   205
            .filter(p -> p.getParent().endsWith("m1") ||
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   206
                         p.getParent().endsWith("m2") ||
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   207
                         p.getParent().endsWith("m3") ||
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   208
                         p.getParent().endsWith("m4"))
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   209
            .forEach(p -> {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   210
                String fn = image.resolve("legal").relativize(p)
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   211
                                 .toString()
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   212
                                 .replace(File.separatorChar, '/');
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   213
                System.out.println(fn);
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   214
                if (!expectedFiles.containsKey(fn)) {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   215
                    throw new RuntimeException(fn + " should not be in the image");
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   216
                }
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   217
                compareFileContent(p, expectedFiles.get(fn));
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   218
            });
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   219
    }
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   220
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   221
    @Test
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   222
    public void errorIfNotSameContent() {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   223
        if (Files.notExists(MODULE_PATH)) {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   224
            // exploded image
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   225
            return;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   226
        }
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   227
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   228
        String dir = "test";
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   229
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   230
        String mpath = MODULE_PATH.toString() + File.pathSeparator +
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   231
                       JMODS_DIR.toString();
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   232
        List<String> options = Stream.of("--dedup-legal-notices",
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   233
                                         "error-if-not-same-content",
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   234
                                         "--module-path", mpath,
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   235
                                         "--add-modules=m3,m4",
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   236
                                         "--output", imageDir(dir))
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   237
                                     .collect(Collectors.toList());
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   238
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   239
        StringWriter writer = new StringWriter();
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   240
        PrintWriter pw = new PrintWriter(writer);
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   241
        System.out.println("jlink " + options.stream().collect(Collectors.joining(" ")));
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   242
        int rc = JLINK_TOOL.run(pw, pw,
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   243
                                options.toArray(new String[0]));
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   244
        assertTrue(rc != 0);
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   245
        assertTrue(writer.toString().trim()
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   246
                         .matches("Error:.*/m4/legal/m4/test-license .*contain different content"));
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   247
    }
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   248
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   249
    private void compareFileContent(Path file, String content) {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   250
        try {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   251
            byte[] bytes = Files.readAllBytes(file);
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   252
            byte[] expected = String.format("%s%n", content).getBytes();
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   253
            assertEquals(bytes, expected, String.format("%s not matched:%nfile: %s%nexpected:%s%n",
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   254
                file.toString(), new String(bytes), new String(expected)));
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   255
        } catch (IOException e) {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   256
            throw new UncheckedIOException(e);
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   257
        }
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   258
    }
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   259
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   260
    private Path createImage(String outputDir, List<String> options) {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   261
        System.out.println("jlink " + options.stream().collect(Collectors.joining(" ")));
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   262
        int rc = JLINK_TOOL.run(System.out, System.out,
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   263
                                options.toArray(new String[0]));
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   264
        assertTrue(rc == 0);
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   265
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   266
        return IMAGES_DIR.resolve(outputDir);
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   267
    }
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   268
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   269
    private void deleteDirectory(Path dir) throws IOException {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   270
        Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   271
            @Override
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   272
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   273
                throws IOException
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   274
            {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   275
                Files.delete(file);
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   276
                return FileVisitResult.CONTINUE;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   277
            }
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   278
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   279
            @Override
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   280
            public FileVisitResult postVisitDirectory(Path dir, IOException exc)
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   281
                throws IOException
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   282
            {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   283
                Files.delete(dir);
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   284
                return FileVisitResult.CONTINUE;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   285
            }
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   286
        });
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   287
    }
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   288
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   289
    /**
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   290
     * Builder to create JMOD file
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   291
     */
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   292
    class JmodFileBuilder {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   293
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   294
        final String name;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   295
        final Set<String> requires = new HashSet<>();
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   296
        final Map<String, String> licenses = new HashMap<>();
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   297
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   298
        JmodFileBuilder(String name) throws IOException {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   299
            this.name = name;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   300
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   301
            Path msrc = SRC_DIR.resolve(name);
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   302
            if (Files.exists(msrc)) {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   303
                deleteDirectory(msrc);
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   304
            }
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   305
        }
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   306
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   307
        JmodFileBuilder writeModuleInfo()throws IOException {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   308
            Path msrc = SRC_DIR.resolve(name);
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   309
            Files.createDirectories(msrc);
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   310
            Path minfo = msrc.resolve("module-info.java");
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   311
            try (BufferedWriter bw = Files.newBufferedWriter(minfo);
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   312
                 PrintWriter writer = new PrintWriter(bw)) {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   313
                writer.format("module %s {%n", name);
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   314
                for (String req : requires) {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   315
                    writer.format("    requires %s;%n", req);
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   316
                }
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   317
                writer.format("}%n");
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   318
            }
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   319
            return this;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   320
        }
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   321
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   322
        JmodFileBuilder licenseFile(String filename, String content) {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   323
            licenses.put(filename, content);
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   324
            return this;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   325
        }
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   326
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   327
        JmodFileBuilder requires(String name) {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   328
            requires.add(name);
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   329
            return this;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   330
        }
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   331
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   332
        Path build() throws IOException {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   333
            compileModule();
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   334
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   335
            Path mdir = LEGAL_DIR.resolve(name);
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   336
            for (Map.Entry<String,String> e : licenses.entrySet()) {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   337
                Files.createDirectories(mdir);
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   338
                String filename = e.getKey();
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   339
                String content = e.getValue();
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   340
                Path file = mdir.resolve(filename);
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   341
                try (BufferedWriter writer = Files.newBufferedWriter(file);
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   342
                     PrintWriter pw = new PrintWriter(writer)) {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   343
                    pw.println(content);
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   344
                }
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   345
            }
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   346
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   347
            return createJmodFile();
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   348
        }
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   349
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   350
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   351
        void compileModule() throws IOException {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   352
            Path msrc = SRC_DIR.resolve(name);
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   353
            assertTrue(CompilerUtils.compile(msrc, MODS_DIR,
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   354
                                             "--module-source-path",
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   355
                                             SRC_DIR.toString()));
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   356
        }
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   357
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   358
        Path createJmodFile() throws IOException {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   359
            Path mclasses = MODS_DIR.resolve(name);
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   360
            Files.createDirectories(JMODS_DIR);
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   361
            Path outfile = JMODS_DIR.resolve(name + ".jmod");
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   362
            List<String> args = new ArrayList<>();
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   363
            args.add("create");
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   364
            // add classes
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   365
            args.add("--class-path");
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   366
            args.add(mclasses.toString());
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   367
            if (licenses.size() > 0) {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   368
                args.add("--legal-notices");
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   369
                args.add(LEGAL_DIR.resolve(name).toString());
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   370
            }
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   371
            args.add(outfile.toString());
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   372
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   373
            if (Files.exists(outfile))
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   374
                Files.delete(outfile);
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   375
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   376
            System.out.println("jmod " +
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   377
                args.stream().collect(Collectors.joining(" ")));
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   378
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   379
            int rc = JMOD_TOOL.run(System.out, System.out,
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   380
                                   args.toArray(new String[args.size()]));
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   381
            if (rc != 0) {
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   382
                throw new AssertionError("jmod failed: rc = " + rc);
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   383
            }
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   384
            return outfile;
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   385
        }
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   386
    }
d833113eb7d7 8169925: Organize licenses by module in source, JMOD file, and run-time image
mchung
parents:
diff changeset
   387
}