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.nio.file.Files;
  26 import java.nio.file.Path;
  27 import java.util.ArrayList;
  28 import java.util.List;
  29 import java.util.Map;
  30 
  31 import jdk.tools.jlink.internal.ImagePluginProviderRepository;
  32 import jdk.tools.jlink.plugins.ExecutableImage;
  33 import jdk.tools.jlink.plugins.OnOffPostProcessingPluginProvider;
  34 import jdk.tools.jlink.plugins.PostProcessingPlugin;
  35 import jdk.tools.jlink.plugins.ProcessingManager;
  36 import jdk.tools.jlink.plugins.ProcessingManager.ProcessingSession;
  37 import jdk.tools.jlink.plugins.ProcessingManager.RunningProcess;
  38 import tests.Helper;
  39 import tests.JImageGenerator;
  40 import tests.JImageGenerator.InMemoryFile;
  41 
  42 /*
  43  * @test
  44  * @summary Test post processing
  45  * @author Jean-Francois Denise
  46  * @library ../lib
  47  * @modules java.base/jdk.internal.jimage
  48  *          jdk.jdeps/com.sun.tools.classfile
  49  *          jdk.jlink/jdk.tools.jlink
  50  *          jdk.jlink/jdk.tools.jlink.internal
  51  *          jdk.jlink/jdk.tools.jmod
  52  *          jdk.jlink/jdk.tools.jimage
  53  *          jdk.compiler
  54  * @build tests.*
  55  * @run main/othervm JLinkPostProcessingTest
  56  */
  57 public class JLinkPostProcessingTest {
  58 
  59     private static class PostProcessingTest extends OnOffPostProcessingPluginProvider {
  60 
  61         private static ExecutableImage called;
  62         @Override
  63         public PostProcessingPlugin[] createPlugins(Map<String, String> otherOptions) throws IOException {
  64             return new PostProcessingPlugin[]{new PPPlugin()};
  65         }
  66 
  67         private static boolean isWindows() {
  68             return System.getProperty("os.name").startsWith("Windows");
  69         }
  70 
  71         private static class PPPlugin implements PostProcessingPlugin {
  72 
  73             @Override
  74             public List<String> process(ProcessingManager manager) throws Exception {
  75                 called = manager.getImage();
  76                 List<String> args = new ArrayList<>();
  77                 args.add("-version");
  78 
  79                 ProcessingSession session = manager.newSession("test");
  80                 {
  81                     RunningProcess i = session.newImageProcess(args);
  82                     String str = i.getStdout();
  83                     if (!str.isEmpty()) {
  84                         throw new Exception("Unexpected out " + str);
  85                     }
  86                     String str2 = i.getStderr();
  87                     if (str2.isEmpty()) {
  88                         throw new Exception("Version not print ");
  89                     } else {
  90                         System.out.println("REMOTE PROCESS output: " + str2);
  91                     }
  92                     if (i.getExitCode() != 0) {
  93                         throw new Exception("Not valid exit code " + i.getExitCode());
  94                     }
  95                 }
  96 
  97                 {
  98                     ProcessBuilder builder = new ProcessBuilder(manager.getImage().
  99                             getHome().resolve("bin").
 100                             resolve(isWindows() ? "java.exe" : "java").toString(), "-version");
 101                     RunningProcess i = session.newRunningProcess(builder);
 102                     String str = i.getStdout();
 103                     if (!str.isEmpty()) {
 104                         throw new Exception("Unexpected out " + str);
 105                     }
 106                     String str2 = i.getStderr();
 107                     if (str2.isEmpty()) {
 108                         throw new Exception("Version not print ");
 109                     } else {
 110                         System.out.println("REMOTE PROCESS output: " + str2);
 111                     }
 112                     if (i.getExitCode() != 0) {
 113                         throw new Exception("Not valid exit code " + i.getExitCode());
 114                     }
 115                 }
 116 
 117 
 118                 session.close();
 119 
 120                 Path gen = manager.getImage().getHome().resolve("lib").resolve("toto.txt");
 121                 Files.createFile(gen);
 122                 return null;
 123             }
 124 
 125             @Override
 126             public String getName() {
 127                 return NAME;
 128             }
 129 
 130         }
 131         private static final String NAME = "pp";
 132 
 133         PostProcessingTest() {
 134             super(NAME, "");
 135         }
 136 
 137         @Override
 138         public String getToolOption() {
 139             return NAME;
 140         }
 141 
 142         @Override
 143         public Map<String, String> getAdditionalOptions() {
 144             return null;
 145         }
 146 
 147         @Override
 148         public String getCategory() {
 149             return PROCESSOR;
 150         }
 151 
 152     }
 153     public static void main(String[] args) throws Exception {
 154 
 155         Helper helper = Helper.newHelper();
 156         if (helper == null) {
 157             System.err.println("Test not run");
 158             return;
 159         }
 160         helper.generateDefaultModules();
 161 
 162         ImagePluginProviderRepository.registerPluginProvider(new PostProcessingTest());
 163 
 164         // Generate an image and post-process in same jlink execution.
 165         {
 166             String[] userOptions = {"--pp", "on"};
 167             String moduleName = "postprocessing1";
 168             helper.generateDefaultJModule(moduleName, "composite2");
 169             String[] res = {};
 170             String[] files = {};
 171             Path imageDir = helper.generateDefaultImage(userOptions, moduleName).assertSuccess();
 172             helper.checkImage(imageDir, moduleName, res, files);
 173 
 174             test(imageDir);
 175         }
 176 
 177         // Generate an image, post-process in 2 jlink executions.
 178         {
 179             String[] userOptions = {};
 180             String moduleName = "postprocessing2";
 181             helper.generateDefaultJModule(moduleName, "composite2");
 182             String[] res = {};
 183             String[] files = {};
 184             Path imageDir = helper.generateDefaultImage(userOptions, moduleName).assertSuccess();
 185             helper.checkImage(imageDir, moduleName, res, files);
 186 
 187             String[] ppOptions = {"--pp", "on"};
 188             helper.postProcessImage(imageDir, ppOptions);
 189             test(imageDir);
 190         }
 191     }
 192 
 193     private static void test(Path imageDir)
 194             throws Exception {
 195         if (PostProcessingTest.called == null) {
 196             throw new Exception("Post processor not called.");
 197         }
 198         if (!PostProcessingTest.called.getHome().equals(imageDir)) {
 199             throw new Exception("Not right imageDir " + PostProcessingTest.called.getHome());
 200         }
 201         if (PostProcessingTest.called.getExecutionArgs().isEmpty()) {
 202             throw new Exception("No arguments to run java...");
 203         }
 204         Path gen = imageDir.resolve("lib").resolve("toto.txt");
 205         if (!Files.exists(gen)) {
 206             throw new Exception("Generated file doesn;t exist");
 207         }
 208         PostProcessingTest.called = null;
 209     }
 210 }