Path: blob/master/test/jdk/tools/jlink/IntegrationTest.java
41144 views
/*1* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.File;24import java.io.FileReader;25import java.io.IOException;26import java.io.UncheckedIOException;27import java.nio.ByteOrder;28import java.nio.file.Files;29import java.nio.file.Path;30import java.nio.file.Paths;31import java.util.ArrayList;32import java.util.Collections;33import java.util.HashMap;34import java.util.HashSet;35import java.util.List;36import java.util.Map;37import java.util.Properties;38import java.util.Set;39import java.util.function.Function;40import jdk.tools.jlink.internal.Jlink;41import jdk.tools.jlink.internal.JlinkTask;42import jdk.tools.jlink.builder.DefaultImageBuilder;43import jdk.tools.jlink.plugin.ResourcePool;44import jdk.tools.jlink.plugin.ResourcePoolBuilder;45import jdk.tools.jlink.plugin.Plugin;46import jdk.tools.jlink.internal.ExecutableImage;47import jdk.tools.jlink.internal.Jlink.JlinkConfiguration;48import jdk.tools.jlink.internal.Jlink.PluginsConfiguration;49import jdk.tools.jlink.internal.PostProcessor;50import jdk.tools.jlink.internal.plugins.DefaultCompressPlugin;51import jdk.tools.jlink.internal.plugins.DefaultStripDebugPlugin;5253import tests.Helper;54import tests.JImageGenerator;5556/*57* @test58* @summary Test integration API59* @author Jean-Francois Denise60* @library ../lib61* @modules java.base/jdk.internal.jimage62* jdk.jdeps/com.sun.tools.classfile63* jdk.jlink/jdk.tools.jlink.builder64* jdk.jlink/jdk.tools.jlink.internal65* jdk.jlink/jdk.tools.jlink.internal.plugins66* jdk.jlink/jdk.tools.jlink.plugin67* jdk.jlink/jdk.tools.jmod68* jdk.jlink/jdk.tools.jimage69* jdk.compiler70* @build tests.*71* @run main/othervm -Xmx1g IntegrationTest72*/73public class IntegrationTest {7475private static final List<Integer> ordered = new ArrayList<>();7677public static class MyPostProcessor implements PostProcessor, Plugin {7879public static final String NAME = "mypostprocessor";8081@Override82public List<String> process(ExecutableImage image) {83try {84Files.createFile(image.getHome().resolve("toto.txt"));85return null;86} catch (IOException ex) {87throw new UncheckedIOException(ex);88}89}9091@Override92public String getName() {93return NAME;94}9596@Override97public Category getType() {98return Category.PROCESSOR;99}100101@Override102public void configure(Map<String, String> config) {103throw new UnsupportedOperationException("Shouldn't be called");104}105106@Override107public ResourcePool transform(ResourcePool in, ResourcePoolBuilder out) {108in.transformAndCopy(Function.identity(), out);109return out.build();110}111}112113public static void main(String[] args) throws Exception {114115Helper helper = Helper.newHelper();116if (helper == null) {117System.err.println("Test not run");118return;119}120apitest();121test();122}123124private static void apitest() throws Exception {125boolean failed = false;126Jlink jl = new Jlink();127128try {129jl.build(null);130failed = true;131} catch (Exception ex) {132// XXX OK133}134if (failed) {135throw new Exception("Should have failed");136}137System.out.println(jl);138139Plugin p = Jlink.newPlugin("toto", Collections.emptyMap(), null);140if (p != null) {141throw new Exception("Plugin should be null");142}143144Plugin p2 = Jlink.newPlugin("compress", Map.of("compress", "1"), null);145if (p2 == null) {146throw new Exception("Plugin should not be null");147}148}149150private static void test() throws Exception {151Jlink jlink = new Jlink();152Path output = Paths.get("integrationout");153List<Path> modulePaths = new ArrayList<>();154File jmods155= JImageGenerator.getJModsDir(new File(System.getProperty("test.jdk")));156modulePaths.add(jmods.toPath());157Set<String> mods = new HashSet<>();158mods.add("java.management");159Set<String> limits = new HashSet<>();160limits.add("java.management");161JlinkConfiguration config = new Jlink.JlinkConfiguration(output,162mods, ByteOrder.nativeOrder(),163JlinkTask.newModuleFinder(modulePaths, limits, mods));164165List<Plugin> lst = new ArrayList<>();166167//Strip debug168{169Map<String, String> config1 = new HashMap<>();170Plugin strip = Jlink.newPlugin("strip-debug", config1, null);171config1.put(strip.getName(), "");172lst.add(strip);173}174// compress175{176Map<String, String> config1 = new HashMap<>();177String pluginName = "compress";178config1.put(pluginName, "2");179Plugin compress180= Jlink.newPlugin(pluginName, config1, null);181if(!pluginName.equals(compress.getName())) {182throw new AssertionError("compress plugin name doesn't match test constant");183}184lst.add(compress);185}186// Post processor187{188lst.add(new MyPostProcessor());189}190// Image builder191DefaultImageBuilder builder = new DefaultImageBuilder(output, Collections.emptyMap());192PluginsConfiguration plugins193= new Jlink.PluginsConfiguration(lst, builder, null);194195jlink.build(config, plugins);196197if (!Files.exists(output)) {198throw new AssertionError("Directory not created");199}200File jimage = new File(output.toString(), "lib" + File.separator + "modules");201if (!jimage.exists()) {202throw new AssertionError("jimage not generated");203}204File release = new File(output.toString(), "release");205if (!release.exists()) {206throw new AssertionError("release not generated");207}208209Properties props = new Properties();210try (FileReader reader = new FileReader(release)) {211props.load(reader);212}213214checkReleaseProperty(props, "JAVA_VERSION");215216if (!Files.exists(output.resolve("toto.txt"))) {217throw new AssertionError("Post processing not called");218}219220}221222static void checkReleaseProperty(Properties props, String name) {223if (! props.containsKey(name)) {224throw new AssertionError("release file does not contain property : " + name);225}226227// property value is of min. length 3 and double quoted at the ends.228String value = props.getProperty(name);229if (value.length() < 3 ||230value.charAt(0) != '"' ||231value.charAt(value.length() - 1) != '"') {232throw new AssertionError("release property " + name + " is not quoted property");233}234}235}236237238