IntegrationTest.java revision 14869:f0bdc4ddb086
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 24import java.io.File; 25import java.io.IOException; 26import java.io.UncheckedIOException; 27import java.nio.file.Files; 28import java.nio.file.Path; 29import java.nio.file.Paths; 30import java.util.ArrayList; 31import java.util.Collections; 32import java.util.HashMap; 33import java.util.HashSet; 34import java.util.List; 35import java.util.Map; 36import java.util.Set; 37import jdk.tools.jlink.Jlink; 38import jdk.tools.jlink.Jlink.JlinkConfiguration; 39import jdk.tools.jlink.Jlink.PluginsConfiguration; 40import jdk.tools.jlink.builder.DefaultImageBuilder; 41import jdk.tools.jlink.plugin.ExecutableImage; 42import jdk.tools.jlink.plugin.ModulePool; 43import jdk.tools.jlink.plugin.PostProcessorPlugin; 44import jdk.tools.jlink.plugin.TransformerPlugin; 45import jdk.tools.jlink.internal.plugins.DefaultCompressPlugin; 46import jdk.tools.jlink.internal.plugins.StripDebugPlugin; 47import jdk.tools.jlink.plugin.Plugin; 48 49import tests.Helper; 50import tests.JImageGenerator; 51 52/* 53 * @test 54 * @summary Test integration API 55 * @author Jean-Francois Denise 56 * @library ../lib 57 * @modules java.base/jdk.internal.jimage 58 * jdk.jdeps/com.sun.tools.classfile 59 * jdk.jlink/jdk.tools.jlink 60 * jdk.jlink/jdk.tools.jlink.builder 61 * jdk.jlink/jdk.tools.jlink.internal 62 * jdk.jlink/jdk.tools.jlink.internal.plugins 63 * jdk.jlink/jdk.tools.jmod 64 * jdk.jlink/jdk.tools.jimage 65 * jdk.compiler 66 * @build tests.* 67 * @run main IntegrationTest 68 */ 69public class IntegrationTest { 70 71 private static final List<Integer> ordered = new ArrayList<>(); 72 73 public static class MyPostProcessor implements PostProcessorPlugin { 74 75 public static final String NAME = "mypostprocessor"; 76 77 @Override 78 public List<String> process(ExecutableImage image) { 79 try { 80 Files.createFile(image.getHome().resolve("toto.txt")); 81 return null; 82 } catch (IOException ex) { 83 throw new UncheckedIOException(ex); 84 } 85 } 86 87 @Override 88 public String getName() { 89 return NAME; 90 } 91 92 @Override 93 public Category getType() { 94 return Category.PROCESSOR; 95 } 96 97 @Override 98 public void configure(Map<String, String> config) { 99 throw new UnsupportedOperationException("Shouldn't be called"); 100 } 101 } 102 103 public static class MyPlugin1 implements TransformerPlugin { 104 105 Integer index; 106 Set<String> after; 107 Set<String> before; 108 109 private MyPlugin1(Integer index, Set<String> after, Set<String> before) { 110 this.index = index; 111 this.after = after; 112 this.before = before; 113 } 114 115 @Override 116 public Set<String> isAfter() { 117 return after; 118 } 119 120 @Override 121 public Set<String> isBefore() { 122 return before; 123 } 124 125 @Override 126 public String getName() { 127 return NAME + index; 128 } 129 130 @Override 131 public void visit(ModulePool in, ModulePool out) { 132 System.err.println(NAME + index); 133 ordered.add(index); 134 in.transformAndCopy((file) -> { 135 return file; 136 }, out); 137 } 138 139 @Override 140 public String getDescription() { 141 return null; 142 } 143 144 @Override 145 public String getOption() { 146 return null; 147 } 148 static final String NAME = "myprovider"; 149 static final String INDEX = "INDEX"; 150 151 @Override 152 public void configure(Map<String, String> config) { 153 throw new UnsupportedOperationException("Shouldn't be called"); 154 } 155 } 156 157 public static void main(String[] args) throws Exception { 158 159 Helper helper = Helper.newHelper(); 160 if (helper == null) { 161 System.err.println("Test not run"); 162 return; 163 } 164 apitest(); 165 test(); 166 testOrder(); 167 testCycleOrder(); 168 } 169 170 private static void apitest() throws Exception { 171 boolean failed = false; 172 Jlink jl = new Jlink(); 173 174 try { 175 jl.build(null); 176 failed = true; 177 } catch (Exception ex) { 178 // XXX OK 179 } 180 if (failed) { 181 throw new Exception("Should have failed"); 182 } 183 System.out.println(jl); 184 185 JlinkConfiguration config 186 = new JlinkConfiguration(null, null, null, null); 187 188 System.out.println(config); 189 190 Plugin p = Jlink.newPlugin("toto", Collections.emptyMap(), null); 191 if (p != null) { 192 throw new Exception("Plugin should be null"); 193 } 194 195 Plugin p2 = Jlink.newPlugin("compress", Collections.emptyMap(), null); 196 if (p2 == null) { 197 throw new Exception("Plugin should not be null"); 198 } 199 } 200 201 private static void test() throws Exception { 202 Jlink jlink = new Jlink(); 203 Path output = Paths.get("integrationout"); 204 List<Path> modulePaths = new ArrayList<>(); 205 File jmods 206 = JImageGenerator.getJModsDir(new File(System.getProperty("test.jdk"))); 207 modulePaths.add(jmods.toPath()); 208 Set<String> mods = new HashSet<>(); 209 mods.add("java.management"); 210 Set<String> limits = new HashSet<>(); 211 limits.add("java.management"); 212 JlinkConfiguration config = new Jlink.JlinkConfiguration(output, 213 modulePaths, mods, limits, null); 214 215 List<Plugin> lst = new ArrayList<>(); 216 217 //Strip debug 218 { 219 Map<String, String> config1 = new HashMap<>(); 220 config1.put(StripDebugPlugin.NAME, ""); 221 Plugin strip = Jlink.newPlugin("strip-debug", config1, null); 222 lst.add(strip); 223 } 224 // compress 225 { 226 Map<String, String> config1 = new HashMap<>(); 227 config1.put(DefaultCompressPlugin.NAME, "2"); 228 Plugin compress 229 = Jlink.newPlugin("compress", config1, null); 230 lst.add(compress); 231 } 232 // Post processor 233 { 234 lst.add(new MyPostProcessor()); 235 } 236 // Image builder 237 DefaultImageBuilder builder = new DefaultImageBuilder(output); 238 PluginsConfiguration plugins 239 = new Jlink.PluginsConfiguration(lst, builder, null); 240 241 jlink.build(config, plugins); 242 243 if (!Files.exists(output)) { 244 throw new AssertionError("Directory not created"); 245 } 246 File jimage = new File(output.toString(), "lib" + File.separator + "modules"); 247 if (!jimage.exists()) { 248 throw new AssertionError("jimage not generated"); 249 } 250 File release = new File(output.toString(), "release"); 251 if (!release.exists()) { 252 throw new AssertionError("release not generated"); 253 } 254 255 if (!Files.exists(output.resolve("toto.txt"))) { 256 throw new AssertionError("Post processing not called"); 257 } 258 259 } 260 261 private static void testOrder() throws Exception { 262 Jlink jlink = new Jlink(); 263 Path output = Paths.get("integrationout2"); 264 List<Path> modulePaths = new ArrayList<>(); 265 File jmods 266 = JImageGenerator.getJModsDir(new File(System.getProperty("test.jdk"))); 267 modulePaths.add(jmods.toPath()); 268 Set<String> mods = new HashSet<>(); 269 mods.add("java.management"); 270 Set<String> limits = new HashSet<>(); 271 limits.add("java.management"); 272 JlinkConfiguration config = new Jlink.JlinkConfiguration(output, 273 modulePaths, mods, limits, null); 274 275 List<Plugin> lst = new ArrayList<>(); 276 277 // Order is Plug1>Plug2>Plug3 278 // Plug1 279 280 281 // TRANSFORMER 3, must be after 2. 282 { 283 Set<String> after = new HashSet<>(); 284 after.add(MyPlugin1.NAME+"2"); 285 lst.add(new MyPlugin1(3, after, Collections.emptySet())); 286 } 287 288 // TRANSFORMER 2, must be after 1. 289 { 290 Set<String> after = new HashSet<>(); 291 after.add(MyPlugin1.NAME+"1"); 292 lst.add(new MyPlugin1(2, after, Collections.emptySet())); 293 } 294 295 // TRANSFORMER 1 296 { 297 Set<String> before = new HashSet<>(); 298 before.add(MyPlugin1.NAME+"2"); 299 lst.add(new MyPlugin1(1, Collections.emptySet(), before)); 300 } 301 302 // Image builder 303 DefaultImageBuilder builder = new DefaultImageBuilder(output); 304 PluginsConfiguration plugins 305 = new Jlink.PluginsConfiguration(lst, builder, null); 306 307 jlink.build(config, plugins); 308 309 if (ordered.isEmpty()) { 310 throw new AssertionError("Plugins not called"); 311 } 312 List<Integer> clone = new ArrayList<>(); 313 clone.addAll(ordered); 314 Collections.sort(clone); 315 if (!clone.equals(ordered)) { 316 throw new AssertionError("Ordered is not properly sorted" + ordered); 317 } 318 } 319 320 private static void testCycleOrder() throws Exception { 321 Jlink jlink = new Jlink(); 322 Path output = Paths.get("integrationout3"); 323 List<Path> modulePaths = new ArrayList<>(); 324 File jmods 325 = JImageGenerator.getJModsDir(new File(System.getProperty("test.jdk"))); 326 modulePaths.add(jmods.toPath()); 327 Set<String> mods = new HashSet<>(); 328 mods.add("java.management"); 329 Set<String> limits = new HashSet<>(); 330 limits.add("java.management"); 331 JlinkConfiguration config = new Jlink.JlinkConfiguration(output, 332 modulePaths, mods, limits, null); 333 334 List<Plugin> lst = new ArrayList<>(); 335 336 // packager 1 337 { 338 Set<String> before = new HashSet<>(); 339 before.add(MyPlugin1.NAME+"2"); 340 lst.add(new MyPlugin1(1, Collections.emptySet(), before)); 341 } 342 343 // packager 2 344 { 345 Set<String> before = new HashSet<>(); 346 before.add(MyPlugin1.NAME+"1"); 347 lst.add(new MyPlugin1(2, Collections.emptySet(), before)); 348 } 349 350 // Image builder 351 DefaultImageBuilder builder = new DefaultImageBuilder(output); 352 PluginsConfiguration plugins 353 = new Jlink.PluginsConfiguration(lst, builder, null); 354 boolean failed = false; 355 try { 356 jlink.build(config, plugins); 357 failed = true; 358 } catch (Exception ex) { 359 // XXX OK 360 } 361 if (failed) { 362 throw new AssertionError("Should have failed"); 363 } 364 } 365} 366