1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.IOException;
  25 import java.io.UncheckedIOException;
  26 import java.nio.file.Files;
  27 import java.nio.file.Path;
  28 import java.util.Collections;
  29 import java.util.List;
  30 import java.util.Map;
  31 import java.util.function.Function;
  32 
  33 import jdk.tools.jlink.plugin.Plugin;
  34 import jdk.tools.jlink.plugin.ResourcePool;
  35 import jdk.tools.jlink.plugin.ResourcePoolBuilder;
  36 import jdk.tools.jlink.internal.PluginRepository;
  37 import jdk.tools.jlink.internal.PostProcessor;
  38 import jdk.tools.jlink.internal.ExecutableImage;
  39 import tests.Helper;
  40 
  41 /*
  42  * @test
  43  * @summary Test post processing
  44  * @author Jean-Francois Denise
  45  * @library ../lib
  46  * @modules java.base/jdk.internal.jimage
  47  *          jdk.jdeps/com.sun.tools.classfile
  48  *          jdk.jlink/jdk.tools.jlink.internal
  49  *          jdk.jlink/jdk.tools.jmod
  50  *          jdk.jlink/jdk.tools.jimage
  51  *          jdk.compiler
  52  * @build tests.*
  53  * @run main/othervm JLinkPostProcessingTest
  54  */
  55 public class JLinkPostProcessingTest {
  56 
  57     private static class PPPlugin implements PostProcessor, Plugin {
  58 
  59         private static ExecutableImage called;
  60         private static final String NAME = "pp";
  61 
  62         @Override
  63         public List<String> process(ExecutableImage image) {
  64             called = image;
  65             Path gen = image.getHome().resolve("lib").resolve("toto.txt");
  66             try {
  67                 Files.createFile(gen);
  68             } catch (IOException ex) {
  69                 throw new UncheckedIOException(ex);
  70             }
  71             return null;
  72         }
  73 
  74         @Override
  75         public ResourcePool transform(ResourcePool in, ResourcePoolBuilder out) {
  76             in.transformAndCopy(Function.identity(), out);
  77             return out.build();
  78         }
  79 
  80         @Override
  81         public String getName() {
  82             return NAME;
  83         }
  84 
  85         @Override
  86         public Category getType() {
  87             return Category.PROCESSOR;
  88         }
  89 
  90         @Override
  91         public String getDescription() {
  92             return NAME;
  93         }
  94     }
  95 
  96     public static void main(String[] args) throws Exception {
  97 
  98         Helper helper = Helper.newHelper();
  99         if (helper == null) {
 100             System.err.println("Test not run");
 101             return;
 102         }
 103         helper.generateDefaultModules();
 104 
 105         PluginRepository.registerPlugin(new PPPlugin());
 106 
 107         // Generate an image and post-process in same jlink execution.
 108         {
 109             String[] userOptions = {"--pp"};
 110             String moduleName = "postprocessing1";
 111             helper.generateDefaultJModule(moduleName, "composite2");
 112             String[] res = {};
 113             String[] files = {};
 114             Path imageDir = helper.generateDefaultImage(userOptions, moduleName).assertSuccess();
 115             helper.checkImage(imageDir, moduleName, res, files);
 116 
 117             test(imageDir);
 118         }
 119 
 120         // Generate an image, post-process in 2 jlink executions.
 121         {
 122             String[] userOptions = {};
 123             String moduleName = "postprocessing2";
 124             helper.generateDefaultJModule(moduleName, "composite2");
 125             String[] res = {};
 126             String[] files = {};
 127             Path imageDir = helper.generateDefaultImage(userOptions, moduleName).assertSuccess();
 128             helper.checkImage(imageDir, moduleName, res, files);
 129 
 130             String[] ppOptions = {"--pp"};
 131             helper.postProcessImage(imageDir, ppOptions);
 132             test(imageDir);
 133         }
 134     }
 135 
 136     private static void test(Path imageDir)
 137             throws Exception {
 138         if (PPPlugin.called == null) {
 139             throw new Exception("Post processor not called.");
 140         }
 141         if (!PPPlugin.called.getHome().equals(imageDir)) {
 142             throw new Exception("Not right imageDir " + PPPlugin.called.getHome());
 143         }
 144         if (PPPlugin.called.getExecutionArgs().isEmpty()) {
 145             throw new Exception("No arguments to run java...");
 146         }
 147         Path gen = imageDir.resolve("lib").resolve("toto.txt");
 148         if (!Files.exists(gen)) {
 149             throw new Exception("Generated file doesn;t exist");
 150         }
 151         PPPlugin.called = null;
 152     }
 153 }