Path: blob/master/test/jdk/tools/jlink/JLinkPostProcessingTest.java
41144 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.IOException;24import java.io.UncheckedIOException;25import java.nio.file.Files;26import java.nio.file.Path;27import java.util.Collections;28import java.util.List;29import java.util.Map;30import java.util.function.Function;3132import jdk.tools.jlink.plugin.Plugin;33import jdk.tools.jlink.plugin.ResourcePool;34import jdk.tools.jlink.plugin.ResourcePoolBuilder;35import jdk.tools.jlink.internal.PluginRepository;36import jdk.tools.jlink.internal.PostProcessor;37import jdk.tools.jlink.internal.ExecutableImage;38import tests.Helper;3940/*41* @test42* @summary Test post processing43* @author Jean-Francois Denise44* @library ../lib45* @modules java.base/jdk.internal.jimage46* jdk.jdeps/com.sun.tools.classfile47* jdk.jlink/jdk.tools.jlink.internal48* jdk.jlink/jdk.tools.jlink.plugin49* jdk.jlink/jdk.tools.jmod50* jdk.jlink/jdk.tools.jimage51* jdk.compiler52* @build tests.*53* @run main/othervm JLinkPostProcessingTest54*/55public class JLinkPostProcessingTest {5657private static class PPPlugin implements PostProcessor, Plugin {5859private static ExecutableImage called;60private static final String NAME = "pp";6162@Override63public List<String> process(ExecutableImage image) {64called = image;65Path gen = image.getHome().resolve("lib").resolve("toto.txt");66try {67Files.createFile(gen);68} catch (IOException ex) {69throw new UncheckedIOException(ex);70}71return null;72}7374@Override75public ResourcePool transform(ResourcePool in, ResourcePoolBuilder out) {76in.transformAndCopy(Function.identity(), out);77return out.build();78}7980@Override81public String getName() {82return NAME;83}8485@Override86public Category getType() {87return Category.PROCESSOR;88}8990@Override91public String getDescription() {92return NAME;93}94}9596public static void main(String[] args) throws Exception {9798Helper helper = Helper.newHelper();99if (helper == null) {100System.err.println("Test not run");101return;102}103helper.generateDefaultModules();104105PluginRepository.registerPlugin(new PPPlugin());106107// Generate an image and post-process in same jlink execution.108{109String[] userOptions = {"--pp"};110String moduleName = "postprocessing1";111helper.generateDefaultJModule(moduleName, "composite2");112String[] res = {};113String[] files = {};114Path imageDir = helper.generateDefaultImage(userOptions, moduleName).assertSuccess();115helper.checkImage(imageDir, moduleName, res, files);116117test(imageDir);118}119120// Generate an image, post-process in 2 jlink executions.121{122String[] userOptions = {};123String moduleName = "postprocessing2";124helper.generateDefaultJModule(moduleName, "composite2");125String[] res = {};126String[] files = {};127Path imageDir = helper.generateDefaultImage(userOptions, moduleName).assertSuccess();128helper.checkImage(imageDir, moduleName, res, files);129130String[] ppOptions = {"--pp"};131helper.postProcessImage(imageDir, ppOptions);132test(imageDir);133}134}135136private static void test(Path imageDir)137throws Exception {138if (PPPlugin.called == null) {139throw new Exception("Post processor not called.");140}141if (!PPPlugin.called.getHome().equals(imageDir)) {142throw new Exception("Not right imageDir " + PPPlugin.called.getHome());143}144if (PPPlugin.called.getExecutionArgs().isEmpty()) {145throw new Exception("No arguments to run java...");146}147Path gen = imageDir.resolve("lib").resolve("toto.txt");148if (!Files.exists(gen)) {149throw new Exception("Generated file doesn;t exist");150}151PPPlugin.called = null;152}153}154155156