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.IOException;
25 import java.io.PrintWriter;
26 import java.io.StringWriter;
27 import java.lang.reflect.Layer;
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.List;
34 import java.util.stream.Stream;
35
36 import jdk.tools.jlink.plugin.Plugin;
37 import jdk.tools.jlink.internal.PluginRepository;
38 import tests.Helper;
39 import tests.JImageGenerator;
40 import tests.JImageGenerator.InMemoryFile;
41
42 /*
43 * @test
44 * @summary Test image creation
45 * @author Jean-Francois Denise
46 * @library ../lib
47 * @modules java.base/jdk.internal.jimage
48 * jdk.jdeps/com.sun.tools.classfile
49 * jdk.jlink/jdk.tools.jlink.internal
50 * jdk.jlink/jdk.tools.jmod
51 * jdk.jlink/jdk.tools.jimage
52 * jdk.compiler
53 * @build tests.*
54 * @run main/othervm -verbose:gc -Xmx1g JLinkTest
55 */
56 public class JLinkTest {
57
58 public static void main(String[] args) throws Exception {
59
60 Helper helper = Helper.newHelper();
61 if (helper == null) {
62 System.err.println("Test not run");
63 return;
64 }
65 helper.generateDefaultModules();
66 int numPlugins = 12;
67 {
68 // number of built-in plugins
69 List<Plugin> builtInPlugins = new ArrayList<>();
70 builtInPlugins.addAll(PluginRepository.getPlugins(Layer.boot()));
71 for (Plugin p : builtInPlugins) {
72 p.getState();
73 p.getType();
74 }
75 if (builtInPlugins.size() != numPlugins) {
76 throw new AssertionError("Found plugins doesn't match expected number : " +
77 numPlugins + "\n" + builtInPlugins);
78 }
79 }
80
81 {
82 String moduleName = "bug8134651";
83 JImageGenerator.getJLinkTask()
84 .modulePath(helper.defaultModulePath())
85 .output(helper.createNewImageDir(moduleName))
86 .addMods("leaf1")
87 .option("")
88 .call().assertSuccess();
89 JImageGenerator.getJLinkTask()
90 .modulePath(helper.defaultModulePath())
91 .addMods("leaf1")
92 .option("--output")
93 .option("")
94 .call().assertFailure("Error: no value given for --output");
95 JImageGenerator.getJLinkTask()
96 .modulePath("")
97 .output(helper.createNewImageDir(moduleName))
98 .addMods("leaf1")
99 .option("")
100 .call().assertFailure("Error: no value given for --modulepath");
101 }
102
103 {
104 String moduleName = "filter";
105 Path jmod = helper.generateDefaultJModule(moduleName).assertSuccess();
106 String className = "_A.class";
107 JImageGenerator.addFiles(jmod, new InMemoryFile(className, new byte[0]));
108 Path image = helper.generateDefaultImage(moduleName).assertSuccess();
109 helper.checkImage(image, moduleName, new String[] {"/" + moduleName + "/" + className}, null);
110 }
111
112 {
113 // Help
114 StringWriter writer = new StringWriter();
115 jdk.tools.jlink.internal.Main.run(new String[]{"--help"}, new PrintWriter(writer));
116 String output = writer.toString();
117 if (output.split("\n").length < 10) {
118 System.err.println(output);
119 throw new AssertionError("Help");
120 }
121 }
122
123 {
124 // License files
125 String copied = "LICENSE";
126 String[] arr = copied.split(",");
127 String[] copyFiles = new String[2];
128 copyFiles[0] = "--copy-files";
129 copyFiles[1] = copied;
130 Path imageDir = helper.generateDefaultImage(copyFiles, "composite2").assertSuccess();
131 helper.checkImage(imageDir, "composite2", null, null, arr);
132 }
133
134 {
135 // List plugins
136 StringWriter writer = new StringWriter();
137 jdk.tools.jlink.internal.Main.run(new String[]{"--list-plugins"}, new PrintWriter(writer));
138 String output = writer.toString();
139 long number = Stream.of(output.split("\\R"))
140 .filter((s) -> s.matches("Plugin Name:.*"))
141 .count();
142 if (number != numPlugins) {
143 System.err.println(output);
144 throw new AssertionError("Found: " + number + " expected " + numPlugins);
145 }
146 }
147
148 // filter out files and resources + Skip debug + compress
149 {
150 String[] userOptions = {"--compress", "2", "--strip-debug",
151 "--exclude-resources", "*.jcov, */META-INF/*", "--exclude-files",
152 "*" + Helper.getDebugSymbolsExtension()};
153 String moduleName = "excludezipskipdebugcomposite2";
154 helper.generateDefaultJModule(moduleName, "composite2");
155 String[] res = {".jcov", "/META-INF/"};
156 String[] files = {Helper.getDebugSymbolsExtension()};
157 Path imageDir = helper.generateDefaultImage(userOptions, moduleName).assertSuccess();
158 helper.checkImage(imageDir, moduleName, res, files);
159 }
160
161 // filter out + Skip debug + compress with filter + sort resources
162 {
163 String[] userOptions2 = {"--compress=2:compress-filter=^/java.base/*",
164 "--strip-debug", "--exclude-resources",
165 "*.jcov, */META-INF/*", "--sort-resources",
166 "*/module-info.class,/sortcomposite2/*,*/javax/management/*"};
167 String moduleName = "excludezipfilterskipdebugcomposite2";
168 helper.generateDefaultJModule(moduleName, "composite2");
169 String[] res = {".jcov", "/META-INF/"};
170 Path imageDir = helper.generateDefaultImage(userOptions2, moduleName).assertSuccess();
171 helper.checkImage(imageDir, moduleName, res, null);
172 }
173
174 // default compress
175 {
176 testCompress(helper, "compresscmdcomposite2", "--compress", "2");
177 }
178
179 {
180 testCompress(helper, "compressfiltercmdcomposite2",
181 "--compress=2:filter=^/java.base/java/lang/*");
182 }
183
184 // compress 0
185 {
186 testCompress(helper, "compress0filtercmdcomposite2",
187 "--compress=0:filter=^/java.base/java/lang/*");
188 }
189
190 // compress 1
191 {
192 testCompress(helper, "compress1filtercmdcomposite2",
193 "--compress=1:filter=^/java.base/java/lang/*");
194 }
195
196 // compress 2
197 {
198 testCompress(helper, "compress2filtercmdcomposite2",
199 "--compress=2:filter=^/java.base/java/lang/*");
200 }
201
202 // invalid compress level
203 {
204 String[] userOptions = {"--compress", "invalid"};
205 String moduleName = "invalidCompressLevel";
206 helper.generateDefaultJModule(moduleName, "composite2");
207 helper.generateDefaultImage(userOptions, moduleName).assertFailure("Error: Invalid compression level invalid");
208 }
209
210 // @file
211 {
212 Path path = Paths.get("embedded.properties");
213 Files.write(path, Collections.singletonList("--strip-debug --addmods " +
214 "toto.unknown --compress UNKNOWN\n"));
215 String[] userOptions = {"@", path.toAbsolutePath().toString()};
216 String moduleName = "configembeddednocompresscomposite2";
217 helper.generateDefaultJModule(moduleName, "composite2");
218 Path imageDir = helper.generateDefaultImage(userOptions, moduleName).assertSuccess();
219 helper.checkImage(imageDir, moduleName, null, null);
220 }
221
222 }
223
224 private static void testCompress(Helper helper, String moduleName, String... userOptions) throws IOException {
225 helper.generateDefaultJModule(moduleName, "composite2");
226 Path imageDir = helper.generateDefaultImage(userOptions, moduleName).assertSuccess();
227 helper.checkImage(imageDir, moduleName, null, null);
228 }
229 }
--- EOF ---