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.IOException;
25import java.io.UncheckedIOException;
26import java.nio.file.Files;
27import java.nio.file.Path;
28import java.util.Collections;
29import java.util.List;
30import java.util.Map;
31import java.util.function.Function;
32
33import jdk.tools.jlink.plugin.Plugin;
34import jdk.tools.jlink.plugin.ResourcePool;
35import jdk.tools.jlink.plugin.ResourcePoolBuilder;
36import jdk.tools.jlink.internal.PluginRepository;
37import jdk.tools.jlink.internal.PostProcessor;
38import jdk.tools.jlink.internal.ExecutableImage;
39import tests.Helper;
40
41/*
42 * @test
43 * @summary Test post processing
44 * @author Jean-Francois Denise
45 * @library ../lib
46 * @modules java.base/jdk.internal.jimage
47 *          jdk.jdeps/com.sun.tools.classfile
48 *          jdk.jlink/jdk.tools.jlink.internal
49 *          jdk.jlink/jdk.tools.jlink.plugin
50 *          jdk.jlink/jdk.tools.jmod
51 *          jdk.jlink/jdk.tools.jimage
52 *          jdk.compiler
53 * @build tests.*
54 * @run main/othervm JLinkPostProcessingTest
55 */
56public class JLinkPostProcessingTest {
57
58    private static class PPPlugin implements PostProcessor, Plugin {
59
60        private static ExecutableImage called;
61        private static final String NAME = "pp";
62
63        @Override
64        public List<String> process(ExecutableImage image) {
65            called = image;
66            Path gen = image.getHome().resolve("lib").resolve("toto.txt");
67            try {
68                Files.createFile(gen);
69            } catch (IOException ex) {
70                throw new UncheckedIOException(ex);
71            }
72            return null;
73        }
74
75        @Override
76        public ResourcePool transform(ResourcePool in, ResourcePoolBuilder out) {
77            in.transformAndCopy(Function.identity(), out);
78            return out.build();
79        }
80
81        @Override
82        public String getName() {
83            return NAME;
84        }
85
86        @Override
87        public Category getType() {
88            return Category.PROCESSOR;
89        }
90
91        @Override
92        public String getDescription() {
93            return NAME;
94        }
95    }
96
97    public static void main(String[] args) throws Exception {
98
99        Helper helper = Helper.newHelper();
100        if (helper == null) {
101            System.err.println("Test not run");
102            return;
103        }
104        helper.generateDefaultModules();
105
106        PluginRepository.registerPlugin(new PPPlugin());
107
108        // Generate an image and post-process in same jlink execution.
109        {
110            String[] userOptions = {"--pp"};
111            String moduleName = "postprocessing1";
112            helper.generateDefaultJModule(moduleName, "composite2");
113            String[] res = {};
114            String[] files = {};
115            Path imageDir = helper.generateDefaultImage(userOptions, moduleName).assertSuccess();
116            helper.checkImage(imageDir, moduleName, res, files);
117
118            test(imageDir);
119        }
120
121        // Generate an image, post-process in 2 jlink executions.
122        {
123            String[] userOptions = {};
124            String moduleName = "postprocessing2";
125            helper.generateDefaultJModule(moduleName, "composite2");
126            String[] res = {};
127            String[] files = {};
128            Path imageDir = helper.generateDefaultImage(userOptions, moduleName).assertSuccess();
129            helper.checkImage(imageDir, moduleName, res, files);
130
131            String[] ppOptions = {"--pp"};
132            helper.postProcessImage(imageDir, ppOptions);
133            test(imageDir);
134        }
135    }
136
137    private static void test(Path imageDir)
138            throws Exception {
139        if (PPPlugin.called == null) {
140            throw new Exception("Post processor not called.");
141        }
142        if (!PPPlugin.called.getHome().equals(imageDir)) {
143            throw new Exception("Not right imageDir " + PPPlugin.called.getHome());
144        }
145        if (PPPlugin.called.getExecutionArgs().isEmpty()) {
146            throw new Exception("No arguments to run java...");
147        }
148        Path gen = imageDir.resolve("lib").resolve("toto.txt");
149        if (!Files.exists(gen)) {
150            throw new Exception("Generated file doesn;t exist");
151        }
152        PPPlugin.called = null;
153    }
154}
155