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.File;
  25 import java.io.FileReader;
  26 import java.io.IOException;
  27 import java.io.UncheckedIOException;
  28 import java.nio.file.Files;
  29 import java.nio.file.Path;
  30 import java.nio.file.Paths;
  31 import java.util.ArrayList;
  32 import java.util.Collections;
  33 import java.util.HashMap;
  34 import java.util.HashSet;
  35 import java.util.List;
  36 import java.util.Map;
  37 import java.util.Properties;
  38 import java.util.Set;
  39 import java.util.function.Function;
  40 import jdk.tools.jlink.internal.Jlink;
  41 import jdk.tools.jlink.builder.DefaultImageBuilder;
  42 import jdk.tools.jlink.plugin.ResourcePool;
  43 import jdk.tools.jlink.plugin.ResourcePoolBuilder;
  44 import jdk.tools.jlink.plugin.Plugin;
  45 import jdk.tools.jlink.internal.ExecutableImage;
  46 import jdk.tools.jlink.internal.Jlink.JlinkConfiguration;
  47 import jdk.tools.jlink.internal.Jlink.PluginsConfiguration;
  48 import jdk.tools.jlink.internal.PostProcessor;
  49 import jdk.tools.jlink.internal.plugins.DefaultCompressPlugin;
  50 import jdk.tools.jlink.internal.plugins.StripDebugPlugin;
  51 
  52 import tests.Helper;
  53 import tests.JImageGenerator;
  54 
  55 /*
  56  * @test
  57  * @summary Test integration API
  58  * @author Jean-Francois Denise
  59  * @library ../lib
  60  * @modules java.base/jdk.internal.jimage
  61  *          jdk.jdeps/com.sun.tools.classfile
  62  *          jdk.jlink/jdk.tools.jlink.builder
  63  *          jdk.jlink/jdk.tools.jlink.internal
  64  *          jdk.jlink/jdk.tools.jlink.internal.plugins
  65  *          jdk.jlink/jdk.tools.jlink.plugin
  66  *          jdk.jlink/jdk.tools.jmod
  67  *          jdk.jlink/jdk.tools.jimage
  68  *          jdk.compiler
  69  * @build tests.*
  70  * @run main IntegrationTest
  71  */
  72 public class IntegrationTest {
  73 
  74     private static final List<Integer> ordered = new ArrayList<>();
  75 
  76     public static class MyPostProcessor implements PostProcessor, Plugin {
  77 
  78         public static final String NAME = "mypostprocessor";
  79 
  80         @Override
  81         public List<String> process(ExecutableImage image) {
  82             try {
  83                 Files.createFile(image.getHome().resolve("toto.txt"));
  84                 return null;
  85             } catch (IOException ex) {
  86                 throw new UncheckedIOException(ex);
  87             }
  88         }
  89 
  90         @Override
  91         public String getName() {
  92             return NAME;
  93         }
  94 
  95         @Override
  96         public Category getType() {
  97             return Category.PROCESSOR;
  98         }
  99 
 100         @Override
 101         public void configure(Map<String, String> config) {
 102             throw new UnsupportedOperationException("Shouldn't be called");
 103         }
 104 
 105         @Override
 106         public ResourcePool transform(ResourcePool in, ResourcePoolBuilder out) {
 107             in.transformAndCopy(Function.identity(), out);
 108             return out.build();
 109         }
 110     }
 111 
 112     public static void main(String[] args) throws Exception {
 113 
 114         Helper helper = Helper.newHelper();
 115         if (helper == null) {
 116             System.err.println("Test not run");
 117             return;
 118         }
 119         apitest();
 120         test();
 121     }
 122 
 123     private static void apitest() throws Exception {
 124         boolean failed = false;
 125         Jlink jl = new Jlink();
 126 
 127         try {
 128             jl.build(null);
 129             failed = true;
 130         } catch (Exception ex) {
 131             // XXX OK
 132         }
 133         if (failed) {
 134             throw new Exception("Should have failed");
 135         }
 136         System.out.println(jl);
 137 
 138         JlinkConfiguration config
 139                 = new JlinkConfiguration(null, null, null, null);
 140 
 141         System.out.println(config);
 142 
 143         Plugin p = Jlink.newPlugin("toto", Collections.emptyMap(), null);
 144         if (p != null) {
 145             throw new Exception("Plugin should be null");
 146         }
 147 
 148         Plugin p2 = Jlink.newPlugin("compress", Map.of("compress", "1"), null);
 149         if (p2 == null) {
 150             throw new Exception("Plugin should not be null");
 151         }
 152     }
 153 
 154     private static void test() throws Exception {
 155         Jlink jlink = new Jlink();
 156         Path output = Paths.get("integrationout");
 157         List<Path> modulePaths = new ArrayList<>();
 158         File jmods
 159                 = JImageGenerator.getJModsDir(new File(System.getProperty("test.jdk")));
 160         modulePaths.add(jmods.toPath());
 161         Set<String> mods = new HashSet<>();
 162         mods.add("java.management");
 163         Set<String> limits = new HashSet<>();
 164         limits.add("java.management");
 165         JlinkConfiguration config = new Jlink.JlinkConfiguration(output,
 166                 modulePaths, mods, limits, null);
 167 
 168         List<Plugin> lst = new ArrayList<>();
 169 
 170         //Strip debug
 171         {
 172             Map<String, String> config1 = new HashMap<>();
 173             config1.put(StripDebugPlugin.NAME, "");
 174             Plugin strip = Jlink.newPlugin("strip-debug", config1, null);
 175             lst.add(strip);
 176         }
 177         // compress
 178         {
 179             Map<String, String> config1 = new HashMap<>();
 180             config1.put(DefaultCompressPlugin.NAME, "2");
 181             Plugin compress
 182                     = Jlink.newPlugin("compress", config1, null);
 183             lst.add(compress);
 184         }
 185         // Post processor
 186         {
 187             lst.add(new MyPostProcessor());
 188         }
 189         // Image builder
 190         DefaultImageBuilder builder = new DefaultImageBuilder(output, Collections.emptyMap());
 191         PluginsConfiguration plugins
 192                 = new Jlink.PluginsConfiguration(lst, builder, null);
 193 
 194         jlink.build(config, plugins);
 195 
 196         if (!Files.exists(output)) {
 197             throw new AssertionError("Directory not created");
 198         }
 199         File jimage = new File(output.toString(), "lib" + File.separator + "modules");
 200         if (!jimage.exists()) {
 201             throw new AssertionError("jimage not generated");
 202         }
 203         File release = new File(output.toString(), "release");
 204         if (!release.exists()) {
 205             throw new AssertionError("release not generated");
 206         }
 207 
 208         Properties props = new Properties();
 209         try (FileReader reader = new FileReader(release)) {
 210             props.load(reader);
 211         }
 212 
 213         checkReleaseProperty(props, "JAVA_VERSION");
 214         checkReleaseProperty(props, "JAVA_FULL_VERSION");
 215         checkReleaseProperty(props, "OS_NAME");
 216         checkReleaseProperty(props, "OS_ARCH");
 217         checkReleaseProperty(props, "OS_VERSION");
 218 
 219         if (!Files.exists(output.resolve("toto.txt"))) {
 220             throw new AssertionError("Post processing not called");
 221         }
 222 
 223     }
 224 
 225     static void checkReleaseProperty(Properties props, String name) {
 226         if (! props.containsKey(name)) {
 227             throw new AssertionError("release file does not contain property : " + name);
 228         }
 229 
 230         // property value is of min. length 3 and double quoted at the ends.
 231         String value = props.getProperty(name);
 232         if (value.length() < 3 ||
 233             value.charAt(0) != '"' ||
 234             value.charAt(value.length() - 1) != '"') {
 235             throw new AssertionError("release property " + name + " is not quoted property");
 236         }
 237     }
 238 }