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