jdk/test/tools/jlink/plugins/FileCopierPluginTest.java
author sundar
Thu, 04 Aug 2016 19:39:42 +0530
changeset 40121 4204d69de3a9
parent 39834 53a6fb443c20
child 40124 01f8745abd3c
permissions -rw-r--r--
8146721: FileCopierPlugin should not create fake module Reviewed-by: jlaskey, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2015, 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;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.nio.file.Files;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.nio.file.Path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.util.Collections;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.util.HashMap;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
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
    40
import jdk.tools.jlink.internal.ResourcePoolManager;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import jdk.tools.jlink.builder.DefaultImageBuilder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import jdk.tools.jlink.internal.plugins.FileCopierPlugin;
40121
4204d69de3a9 8146721: FileCopierPlugin should not create fake module
sundar
parents: 39834
diff changeset
    44
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
    45
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
    46
import jdk.tools.jlink.plugin.ResourcePool;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
public class FileCopierPluginTest {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
    public static void main(String[] args) throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
        new FileCopierPluginTest().test();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
     * 3 cases - Absolute, no target ==> copy in image root dir - Absolute and
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
     * target ==> copy in image root dir/target - Relative ==> copy from JDK
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
     * home dir.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
     * @throws Exception
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
    public void test() throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
        FileCopierPlugin plug = new FileCopierPlugin();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
        String content = "You \n should \n be \bthere.\n";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
        String name = "sample.txt";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
        File src = new File("src");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
        src.mkdir();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
        // Need a fake bin
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
        File bin = new File("bin");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
        bin.mkdir();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
        File txt = new File(src, name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
        txt.createNewFile();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
        String target = "target" + File.separator + name;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
        Files.write(txt.toPath(), content.getBytes());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
        File lic = new File(System.getProperty("java.home"), "LICENSE");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
        StringBuilder builder = new StringBuilder();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
        int expected = lic.exists() ? 4 : 3;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
        if (lic.exists()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
            builder.append("LICENSE,");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
        builder.append(txt.getAbsolutePath()+",");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
        builder.append(txt.getAbsolutePath() + "=" + target+",");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
        builder.append(src.getAbsolutePath() + "=src2");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
        Map<String, String> conf = new HashMap<>();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
        conf.put(FileCopierPlugin.NAME, builder.toString());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
        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
    89
        ResourcePoolManager poolMgr = new ResourcePoolManager();
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
    90
        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
    91
                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
    92
                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
    93
        if (pool.entryCount() != expected) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
            throw new AssertionError("Wrong number of added files");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
        }
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37779
diff changeset
    96
        pool.entries().forEach(f -> {
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
    97
            if (!f.type().equals(ResourcePoolEntry.Type.OTHER)) {
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
                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
    99
                        + " for file " + f.path());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
            }
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
   101
            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
   102
                throw new AssertionError("Null stream for file " + f.path());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
            }
38320
e24c7029e8ba 8156914: jlink API minor cleanups
sundar
parents: 37779
diff changeset
   104
        });
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
        Path root = new File(".").toPath();
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   106
        DefaultImageBuilder imgbuilder = new DefaultImageBuilder(root);
40121
4204d69de3a9 8146721: FileCopierPlugin should not create fake module
sundar
parents: 39834
diff changeset
   107
        try {
4204d69de3a9 8146721: FileCopierPlugin should not create fake module
sundar
parents: 39834
diff changeset
   108
            imgbuilder.storeFiles(pool);
4204d69de3a9 8146721: FileCopierPlugin should not create fake module
sundar
parents: 39834
diff changeset
   109
        } catch (PluginException e) {
4204d69de3a9 8146721: FileCopierPlugin should not create fake module
sundar
parents: 39834
diff changeset
   110
            // We didn't add any .class resource of the java.base module!
4204d69de3a9 8146721: FileCopierPlugin should not create fake module
sundar
parents: 39834
diff changeset
   111
            // This cannot happen in non-testing scenario as java.base module
4204d69de3a9 8146721: FileCopierPlugin should not create fake module
sundar
parents: 39834
diff changeset
   112
            // is minimum mandatory module in a .jimage. jlink depends on java.base
4204d69de3a9 8146721: FileCopierPlugin should not create fake module
sundar
parents: 39834
diff changeset
   113
            // to generate 'release' file. If the current exception came from that
4204d69de3a9 8146721: FileCopierPlugin should not create fake module
sundar
parents: 39834
diff changeset
   114
            // part of the code, then it is okay.
4204d69de3a9 8146721: FileCopierPlugin should not create fake module
sundar
parents: 39834
diff changeset
   115
            if (!e.getMessage().contains("No module-info for java.base module")) {
4204d69de3a9 8146721: FileCopierPlugin should not create fake module
sundar
parents: 39834
diff changeset
   116
                throw e;
4204d69de3a9 8146721: FileCopierPlugin should not create fake module
sundar
parents: 39834
diff changeset
   117
            }
4204d69de3a9 8146721: FileCopierPlugin should not create fake module
sundar
parents: 39834
diff changeset
   118
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
        if (lic.exists()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
            File license = new File(root.toFile(), "LICENSE");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
            if (!license.exists() || license.length() == 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
                throw new AssertionError("Invalide license file "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
                        + license.getAbsoluteFile());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
        File sample1 = new File(root.toFile(), txt.getName());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
        if (!sample1.exists() || sample1.length() == 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
            throw new AssertionError("Invalide sample1 file "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
                    + sample1.getAbsoluteFile());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
        if (!new String(Files.readAllBytes(sample1.toPath())).equals(content)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
            throw new AssertionError("Invalid Content in sample1");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
        File sample2 = new File(root.toFile(), target);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
        if (!sample2.exists() || sample2.length() == 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
            throw new AssertionError("Invalide sample2 file "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
                    + sample2.getAbsoluteFile());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
        if (!new String(Files.readAllBytes(sample2.toPath())).equals(content)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
            throw new AssertionError("Invalid Content in sample2");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
        File src2 = new File(root.toFile(), "src2");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
        if (!src2.exists() || src2.list().length != 1) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
            throw new AssertionError("Invalide src2 dir "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
                    + src2.getAbsoluteFile());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
        File f = src2.listFiles()[0];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
        if (!f.getName().equals(txt.getName())) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
            throw new AssertionError("Invalide file name in src2 dir "
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
                    + f.getAbsoluteFile());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
        if (!new String(Files.readAllBytes(f.toPath())).equals(content)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
            throw new AssertionError("Invalid Content in src2 dir");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
}