jdk/test/tools/jlink/plugins/FileCopierPluginTest.java
author alanb
Thu, 01 Dec 2016 08:57:53 +0000
changeset 42338 a60f280f803c
parent 40124 01f8745abd3c
permissions -rw-r--r--
8169069: Module system implementation refresh (11/2016) Reviewed-by: plevart, chegar, psandoz, mchung, alanb, dfuchs, naoto, coffeys, weijun Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com, claes.redestad@oracle.com, mark.reinhold@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
 * @test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
 * @summary Test files copy plugin
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
 * @author Jean-Francois Denise
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
 * @modules jdk.jlink/jdk.tools.jlink.internal
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37779
diff changeset
    29
 *          jdk.jlink/jdk.tools.jlink.builder
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
 *          jdk.jlink/jdk.tools.jlink.internal.plugins
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
 * @run main FileCopierPluginTest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.io.File;
40124
01f8745abd3c 8163116: jlink exclude VM plugin does not fully support cross platform image creation
sundar
parents: 40121
diff changeset
    35
import java.net.URI;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.nio.file.Files;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.nio.file.Path;
40124
01f8745abd3c 8163116: jlink exclude VM plugin does not fully support cross platform image creation
sundar
parents: 40121
diff changeset
    38
import java.nio.file.Paths;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.util.Collections;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.util.HashMap;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.util.Map;
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 38320
diff changeset
    42
import jdk.tools.jlink.internal.ResourcePoolManager;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import jdk.tools.jlink.builder.DefaultImageBuilder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import jdk.tools.jlink.internal.plugins.FileCopierPlugin;
40121
4204d69de3a9 8146721: FileCopierPlugin should not create fake module
sundar
parents: 39834
diff changeset
    46
import jdk.tools.jlink.plugin.PluginException;
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 38320
diff changeset
    47
import jdk.tools.jlink.plugin.ResourcePoolEntry;
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 38320
diff changeset
    48
import jdk.tools.jlink.plugin.ResourcePool;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
public class FileCopierPluginTest {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
    public static void main(String[] args) throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
        new FileCopierPluginTest().test();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
     * 3 cases - Absolute, no target ==> copy in image root dir - Absolute and
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
     * target ==> copy in image root dir/target - Relative ==> copy from JDK
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
     * home dir.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
     * @throws Exception
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
    public void test() throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
        FileCopierPlugin plug = new FileCopierPlugin();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
        String content = "You \n should \n be \bthere.\n";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
        String name = "sample.txt";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
        File src = new File("src");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
        src.mkdir();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
        // Need a fake bin
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
        File bin = new File("bin");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
        bin.mkdir();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
        File txt = new File(src, name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
        txt.createNewFile();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
        String target = "target" + File.separator + name;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
        Files.write(txt.toPath(), content.getBytes());
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40124
diff changeset
    78
        File lic = new File(System.getProperty("java.home"), "LICENSE.txt");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
        StringBuilder builder = new StringBuilder();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
        int expected = lic.exists() ? 4 : 3;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
        if (lic.exists()) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40124
diff changeset
    82
            builder.append("LICENSE.txt,");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
        builder.append(txt.getAbsolutePath()+",");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
        builder.append(txt.getAbsolutePath() + "=" + target+",");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
        builder.append(src.getAbsolutePath() + "=src2");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
        Map<String, String> conf = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
        conf.put(FileCopierPlugin.NAME, builder.toString());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
        plug.configure(conf);
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 38320
diff changeset
    91
        ResourcePoolManager poolMgr = new ResourcePoolManager();
40124
01f8745abd3c 8163116: jlink exclude VM plugin does not fully support cross platform image creation
sundar
parents: 40121
diff changeset
    92
        // java.base/module-info.class is used to add "release" file
01f8745abd3c 8163116: jlink exclude VM plugin does not fully support cross platform image creation
sundar
parents: 40121
diff changeset
    93
        // We read it from jrt-fs and add a ResourcePoolEntry
01f8745abd3c 8163116: jlink exclude VM plugin does not fully support cross platform image creation
sundar
parents: 40121
diff changeset
    94
        poolMgr.add(
01f8745abd3c 8163116: jlink exclude VM plugin does not fully support cross platform image creation
sundar
parents: 40121
diff changeset
    95
            ResourcePoolEntry.create("/java.base/module-info.class",
01f8745abd3c 8163116: jlink exclude VM plugin does not fully support cross platform image creation
sundar
parents: 40121
diff changeset
    96
                ResourcePoolEntry.Type.CLASS_OR_RESOURCE, getJavaBaseModuleInfo()));
01f8745abd3c 8163116: jlink exclude VM plugin does not fully support cross platform image creation
sundar
parents: 40121
diff changeset
    97
        expected++;
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 38320
diff changeset
    98
        ResourcePool pool = plug.transform(
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 38320
diff changeset
    99
                new ResourcePoolManager().resourcePool(),
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 38320
diff changeset
   100
                poolMgr.resourcePoolBuilder());
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 38320
diff changeset
   101
        if (pool.entryCount() != expected) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
            throw new AssertionError("Wrong number of added files");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
        }
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37779
diff changeset
   104
        pool.entries().forEach(f -> {
40124
01f8745abd3c 8163116: jlink exclude VM plugin does not fully support cross platform image creation
sundar
parents: 40121
diff changeset
   105
            if (!f.type().equals(ResourcePoolEntry.Type.OTHER) &&
01f8745abd3c 8163116: jlink exclude VM plugin does not fully support cross platform image creation
sundar
parents: 40121
diff changeset
   106
                !f.type().equals(ResourcePoolEntry.Type.CLASS_OR_RESOURCE)) {
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 38320
diff changeset
   107
                throw new AssertionError("Invalid type " + f.type()
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 38320
diff changeset
   108
                        + " for file " + f.path());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
            }
39834
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 38320
diff changeset
   110
            if (f.content() == null) {
53a6fb443c20 8162538: plugin API should avoid read only pool, have module view separated from resource view and have pool builder to modify
sundar
parents: 38320
diff changeset
   111
                throw new AssertionError("Null stream for file " + f.path());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
            }
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37779
diff changeset
   113
        });
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
        Path root = new File(".").toPath();
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   115
        DefaultImageBuilder imgbuilder = new DefaultImageBuilder(root);
40124
01f8745abd3c 8163116: jlink exclude VM plugin does not fully support cross platform image creation
sundar
parents: 40121
diff changeset
   116
        imgbuilder.storeFiles(pool);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
        if (lic.exists()) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40124
diff changeset
   119
            File license = new File(root.toFile(), "LICENSE.txt");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
            if (!license.exists() || license.length() == 0) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40124
diff changeset
   121
                throw new AssertionError("Invalid license file "
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
                        + license.getAbsoluteFile());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
        File sample1 = new File(root.toFile(), txt.getName());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
        if (!sample1.exists() || sample1.length() == 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
            throw new AssertionError("Invalide sample1 file "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
                    + sample1.getAbsoluteFile());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
        if (!new String(Files.readAllBytes(sample1.toPath())).equals(content)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
            throw new AssertionError("Invalid Content in sample1");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
        File sample2 = new File(root.toFile(), target);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
        if (!sample2.exists() || sample2.length() == 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
            throw new AssertionError("Invalide sample2 file "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
                    + sample2.getAbsoluteFile());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
        if (!new String(Files.readAllBytes(sample2.toPath())).equals(content)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
            throw new AssertionError("Invalid Content in sample2");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
        File src2 = new File(root.toFile(), "src2");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
        if (!src2.exists() || src2.list().length != 1) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
            throw new AssertionError("Invalide src2 dir "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
                    + src2.getAbsoluteFile());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
        File f = src2.listFiles()[0];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
        if (!f.getName().equals(txt.getName())) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
            throw new AssertionError("Invalide file name in src2 dir "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
                    + f.getAbsoluteFile());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
        if (!new String(Files.readAllBytes(f.toPath())).equals(content)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
            throw new AssertionError("Invalid Content in src2 dir");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
    }
40124
01f8745abd3c 8163116: jlink exclude VM plugin does not fully support cross platform image creation
sundar
parents: 40121
diff changeset
   158
01f8745abd3c 8163116: jlink exclude VM plugin does not fully support cross platform image creation
sundar
parents: 40121
diff changeset
   159
    // read java.base/module-info.class from jrt-fs
01f8745abd3c 8163116: jlink exclude VM plugin does not fully support cross platform image creation
sundar
parents: 40121
diff changeset
   160
    private static Path getJavaBaseModuleInfo() {
01f8745abd3c 8163116: jlink exclude VM plugin does not fully support cross platform image creation
sundar
parents: 40121
diff changeset
   161
        return Paths.get(URI.create("jrt:/modules/java.base/module-info.class"));
01f8745abd3c 8163116: jlink exclude VM plugin does not fully support cross platform image creation
sundar
parents: 40121
diff changeset
   162
    }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
}