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