jdk/test/tools/jlink/JLinkPostProcessingTest.java
author sundar
Tue, 21 Jun 2016 19:05:34 +0530
changeset 39129 4b2086305b68
parent 38320 e24c7029e8ba
child 39321 c60f34e8c057
permissions -rw-r--r--
8159593: Plugin Set<Category> getType() should return a Category Reviewed-by: jlaskey
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;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import jdk.tools.jlink.internal.PluginRepository;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import jdk.tools.jlink.plugin.ExecutableImage;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import jdk.tools.jlink.plugin.PostProcessorPlugin;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import tests.Helper;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
 * @test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
 * @summary Test post processing
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
 * @author Jean-Francois Denise
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
 * @library ../lib
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
 * @modules java.base/jdk.internal.jimage
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
 *          jdk.jdeps/com.sun.tools.classfile
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
 *          jdk.jlink/jdk.tools.jlink.internal
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
 *          jdk.jlink/jdk.tools.jmod
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
 *          jdk.jlink/jdk.tools.jimage
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
 *          jdk.compiler
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
 * @build tests.*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
 * @run main/othervm JLinkPostProcessingTest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
public class JLinkPostProcessingTest {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
    private static class PPPlugin implements PostProcessorPlugin {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
        private static ExecutableImage called;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
        private static final String NAME = "pp";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
        public List<String> process(ExecutableImage image) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
            called = image;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
            Path gen = image.getHome().resolve("lib").resolve("toto.txt");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
                Files.createFile(gen);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
            } catch (IOException ex) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
                throw new UncheckedIOException(ex);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
            return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
        public String getName() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
            return NAME;
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
39129
4b2086305b68 8159593: Plugin Set<Category> getType() should return a Category
sundar
parents: 38320
diff changeset
    76
        public Category getType() {
4b2086305b68 8159593: Plugin Set<Category> getType() should return a Category
sundar
parents: 38320
diff changeset
    77
            return Category.PROCESSOR;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
        public String getDescription() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
            return NAME;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
        }
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
    public static void main(String[] args) throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
        Helper helper = Helper.newHelper();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
        if (helper == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
            System.err.println("Test not run");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
            return;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
        helper.generateDefaultModules();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
        PluginRepository.registerPlugin(new PPPlugin());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
        // Generate an image and post-process in same jlink execution.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
        {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
            String[] userOptions = {"--pp"};
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
            String moduleName = "postprocessing1";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
            helper.generateDefaultJModule(moduleName, "composite2");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
            String[] res = {};
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
            String[] files = {};
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
            Path imageDir = helper.generateDefaultImage(userOptions, moduleName).assertSuccess();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
            helper.checkImage(imageDir, moduleName, res, files);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
            test(imageDir);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
        // Generate an image, post-process in 2 jlink executions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
        {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
            String[] userOptions = {};
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
            String moduleName = "postprocessing2";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
            helper.generateDefaultJModule(moduleName, "composite2");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
            String[] res = {};
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
            String[] files = {};
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
            Path imageDir = helper.generateDefaultImage(userOptions, moduleName).assertSuccess();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
            helper.checkImage(imageDir, moduleName, res, files);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
            String[] ppOptions = {"--pp"};
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
            helper.postProcessImage(imageDir, ppOptions);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
            test(imageDir);
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
    private static void test(Path imageDir)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
            throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
        if (PPPlugin.called == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
            throw new Exception("Post processor not called.");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
        if (!PPPlugin.called.getHome().equals(imageDir)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
            throw new Exception("Not right imageDir " + PPPlugin.called.getHome());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
        if (PPPlugin.called.getExecutionArgs().isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
            throw new Exception("No arguments to run java...");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
        Path gen = imageDir.resolve("lib").resolve("toto.txt");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
        if (!Files.exists(gen)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
            throw new Exception("Generated file doesn;t exist");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
        PPPlugin.called = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
}