test/jdk/tools/jlink/JLinkPostProcessingTest.java
author dmarkov
Mon, 23 Sep 2019 17:18:03 +0100
changeset 58328 fe46ee1d42ee
parent 47216 71c04702a3d5
permissions -rw-r--r--
8230782: Robot.createScreenCapture() fails if “awt.robot.gtk” is set to false Reviewed-by: prr, serb
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
import java.io.IOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
import java.io.UncheckedIOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
import java.nio.file.Files;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
import java.nio.file.Path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.util.Collections;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.util.List;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.util.Map;
39321
c60f34e8c057 8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents: 39129
diff changeset
    31
import java.util.function.Function;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
39321
c60f34e8c057 8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents: 39129
diff changeset
    33
import jdk.tools.jlink.plugin.Plugin;
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: 39321
diff changeset
    34
import jdk.tools.jlink.plugin.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: 39321
diff changeset
    35
import jdk.tools.jlink.plugin.ResourcePoolBuilder;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import jdk.tools.jlink.internal.PluginRepository;
39321
c60f34e8c057 8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents: 39129
diff changeset
    37
import jdk.tools.jlink.internal.PostProcessor;
c60f34e8c057 8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents: 39129
diff changeset
    38
import jdk.tools.jlink.internal.ExecutableImage;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import tests.Helper;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
 * @test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
 * @summary Test post processing
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
 * @author Jean-Francois Denise
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
 * @library ../lib
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
 * @modules java.base/jdk.internal.jimage
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
 *          jdk.jdeps/com.sun.tools.classfile
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
 *          jdk.jlink/jdk.tools.jlink.internal
43185
d75d9ff8d4e7 8171380: Remove all exports from jdk.jlink
chegar
parents: 39834
diff changeset
    49
 *          jdk.jlink/jdk.tools.jlink.plugin
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
 *          jdk.jlink/jdk.tools.jmod
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
 *          jdk.jlink/jdk.tools.jimage
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
 *          jdk.compiler
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
 * @build tests.*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
 * @run main/othervm JLinkPostProcessingTest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
public class JLinkPostProcessingTest {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
39321
c60f34e8c057 8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents: 39129
diff changeset
    58
    private static class PPPlugin implements PostProcessor, Plugin {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
        private static ExecutableImage called;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
        private static final String NAME = "pp";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
        public List<String> process(ExecutableImage image) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
            called = image;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
            Path gen = image.getHome().resolve("lib").resolve("toto.txt");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
                Files.createFile(gen);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
            } catch (IOException ex) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
                throw new UncheckedIOException(ex);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
            return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
        @Override
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: 39321
diff changeset
    76
        public ResourcePool transform(ResourcePool in, ResourcePoolBuilder out) {
39321
c60f34e8c057 8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents: 39129
diff changeset
    77
            in.transformAndCopy(Function.identity(), out);
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: 39321
diff changeset
    78
            return out.build();
39321
c60f34e8c057 8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents: 39129
diff changeset
    79
        }
c60f34e8c057 8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents: 39129
diff changeset
    80
c60f34e8c057 8160641: PostProcessingPlugin and ExecutableImage should not be part of plugin API
sundar
parents: 39129
diff changeset
    81
        @Override
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
        public String getName() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
            return NAME;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
        @Override
39129
4b2086305b68 8159593: Plugin Set<Category> getType() should return a Category
sundar
parents: 38320
diff changeset
    87
        public Category getType() {
4b2086305b68 8159593: Plugin Set<Category> getType() should return a Category
sundar
parents: 38320
diff changeset
    88
            return Category.PROCESSOR;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
        public String getDescription() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
            return NAME;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
    public static void main(String[] args) throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
        Helper helper = Helper.newHelper();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
        if (helper == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
            System.err.println("Test not run");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
            return;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
        helper.generateDefaultModules();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
        PluginRepository.registerPlugin(new PPPlugin());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
        // Generate an image and post-process in same jlink execution.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
        {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
            String[] userOptions = {"--pp"};
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
            String moduleName = "postprocessing1";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
            helper.generateDefaultJModule(moduleName, "composite2");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
            String[] res = {};
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
            String[] files = {};
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
            Path imageDir = helper.generateDefaultImage(userOptions, moduleName).assertSuccess();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
            helper.checkImage(imageDir, moduleName, res, files);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
            test(imageDir);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
        // Generate an image, post-process in 2 jlink executions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
        {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
            String[] userOptions = {};
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
            String moduleName = "postprocessing2";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
            helper.generateDefaultJModule(moduleName, "composite2");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
            String[] res = {};
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
            String[] files = {};
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
            Path imageDir = helper.generateDefaultImage(userOptions, moduleName).assertSuccess();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
            helper.checkImage(imageDir, moduleName, res, files);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
            String[] ppOptions = {"--pp"};
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
            helper.postProcessImage(imageDir, ppOptions);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
            test(imageDir);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
    private static void test(Path imageDir)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
            throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
        if (PPPlugin.called == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
            throw new Exception("Post processor not called.");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
        if (!PPPlugin.called.getHome().equals(imageDir)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
            throw new Exception("Not right imageDir " + PPPlugin.called.getHome());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
        if (PPPlugin.called.getExecutionArgs().isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
            throw new Exception("No arguments to run java...");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
        Path gen = imageDir.resolve("lib").resolve("toto.txt");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
        if (!Files.exists(gen)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
            throw new Exception("Generated file doesn;t exist");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
        PPPlugin.called = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
}