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