1 /*
2 * Copyright (c) 2015, 2017, 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
25 /*
26 * @summary Simple jar builder
27 * Input: jarName className1 className2 ...
28 * do not specify extensions, just the names
29 * E.g. prot_domain ProtDomainA ProtDomainB
30 * Output: A jar containing compiled classes, placed in a test classes folder
31 * @library /open/test/lib
32 */
33
34 import jdk.test.lib.JDKToolFinder;
35 import jdk.test.lib.process.OutputAnalyzer;
36 import jdk.test.lib.process.ProcessTools;
37 import java.io.File;
38 import java.util.ArrayList;
39 import sun.tools.jar.Main;
40
41 public class JarBuilder {
42 // to turn DEBUG on via command line: -DJarBuilder.DEBUG=[true, TRUE]
43 private static final boolean DEBUG = Boolean.parseBoolean(System.getProperty("JarBuilder.DEBUG", "false"));
44 private static final String classDir = System.getProperty("test.classes");
45
46 public static String getJarFilePath(String jarName) {
47 return classDir + File.separator + jarName + ".jar";
48 }
49
50 // jar all files under dir, with manifest file man, with an optional versionArgs
51 // for generating a multi-release jar.
52 // The jar command is as follows:
53 // jar cmf \
54 // <path to output jar> <path to the manifest file>\
55 // -C <path to the base classes> .\
56 // --release 9 -C <path to the versioned classes> .
57 // the last line begins with "--release" corresponds to the optional versionArgs.
128
129 ArrayList<String> args = new ArrayList<String>();
130 args.add("cf");
131 args.add(jarName);
132 addClassArgs(args, jarclassDir, classNames);
133 createJar(args);
134
135 return jarName;
136 }
137
138 private static void addClassArgs(ArrayList<String> args, String jarclassDir,
139 String[] classNames) {
140
141 for (String name : classNames) {
142 args.add("-C");
143 args.add(jarclassDir);
144 args.add(name + ".class");
145 }
146 }
147
148 private static void createJar(ArrayList<String> args) {
149 if (DEBUG) printIterable("createJar args: ", args);
150
151 Main jarTool = new Main(System.out, System.err, "jar");
152 if (!jarTool.run(args.toArray(new String[1]))) {
153 throw new RuntimeException("jar operation failed");
154 }
155 }
156
157 // Many AppCDS tests use the same simple "Hello.jar" which contains
158 // simple Hello.class and does not specify additional attributes.
159 // For this common use case, use this method to get the jar path.
160 // The method will check if the jar already exists
161 // (created by another test or test run), and will create the jar
162 // if it does not exist
163 public static String getOrCreateHelloJar() throws Exception {
164 String jarPath = getJarFilePath("hello");
165
166 File jarFile = new File(jarPath);
167 if (jarFile.exists()) {
172 }
173
174 public static void compile(String dstPath, String source, String... extraArgs) throws Exception {
175 ArrayList<String> args = new ArrayList<String>();
176 args.add(JDKToolFinder.getCompileJDKTool("javac"));
177 args.add("-d");
178 args.add(dstPath);
179 if (extraArgs != null) {
180 for (String s : extraArgs) {
181 args.add(s);
182 }
183 }
184 args.add(source);
185
186 if (DEBUG) printIterable("compile args: ", args);
187
188 ProcessBuilder pb = new ProcessBuilder(args);
189 OutputAnalyzer output = new OutputAnalyzer(pb.start());
190 output.shouldHaveExitValue(0);
191 }
192
193 public static void signJar() throws Exception {
194 String keyTool = JDKToolFinder.getJDKTool("keytool");
195 String jarSigner = JDKToolFinder.getJDKTool("jarsigner");
196 String classDir = System.getProperty("test.classes");
197 String FS = File.separator;
198
199 executeProcess(keyTool,
200 "-genkey", "-keystore", "./keystore", "-alias", "mykey",
201 "-storepass", "abc123", "-keypass", "abc123",
202 "-dname", "CN=jvmtest")
203 .shouldHaveExitValue(0);
204
205 executeProcess(jarSigner,
206 "-keystore", "./keystore", "-storepass", "abc123", "-keypass",
207 "abc123", "-signedjar", classDir + FS + "signed_hello.jar",
208 classDir + FS + "hello.jar", "mykey")
209 .shouldHaveExitValue(0);
210 }
211
|
1 /*
2 * Copyright (c) 2015, 2018, 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
25 /*
26 * @summary Simple jar builder
27 * Input: jarName className1 className2 ...
28 * do not specify extensions, just the names
29 * E.g. prot_domain ProtDomainA ProtDomainB
30 * Output: A jar containing compiled classes, placed in a test classes folder
31 * @library /open/test/lib
32 */
33
34 import jdk.test.lib.JDKToolFinder;
35 import jdk.test.lib.compiler.CompilerUtils;
36 import jdk.test.lib.process.OutputAnalyzer;
37 import jdk.test.lib.process.ProcessTools;
38 import java.io.File;
39 import java.nio.file.Path;
40 import java.util.ArrayList;
41 import sun.tools.jar.Main;
42
43 public class JarBuilder {
44 // to turn DEBUG on via command line: -DJarBuilder.DEBUG=[true, TRUE]
45 private static final boolean DEBUG = Boolean.parseBoolean(System.getProperty("JarBuilder.DEBUG", "false"));
46 private static final String classDir = System.getProperty("test.classes");
47
48 public static String getJarFilePath(String jarName) {
49 return classDir + File.separator + jarName + ".jar";
50 }
51
52 // jar all files under dir, with manifest file man, with an optional versionArgs
53 // for generating a multi-release jar.
54 // The jar command is as follows:
55 // jar cmf \
56 // <path to output jar> <path to the manifest file>\
57 // -C <path to the base classes> .\
58 // --release 9 -C <path to the versioned classes> .
59 // the last line begins with "--release" corresponds to the optional versionArgs.
130
131 ArrayList<String> args = new ArrayList<String>();
132 args.add("cf");
133 args.add(jarName);
134 addClassArgs(args, jarclassDir, classNames);
135 createJar(args);
136
137 return jarName;
138 }
139
140 private static void addClassArgs(ArrayList<String> args, String jarclassDir,
141 String[] classNames) {
142
143 for (String name : classNames) {
144 args.add("-C");
145 args.add(jarclassDir);
146 args.add(name + ".class");
147 }
148 }
149
150 public static void createModularJar(String jarPath,
151 String classesDir,
152 String mainClass) throws Exception {
153 ArrayList<String> argList = new ArrayList<String>();
154 argList.add("--create");
155 argList.add("--file=" + jarPath);
156 if (mainClass != null) {
157 argList.add("--main-class=" + mainClass);
158 }
159 argList.add("-C");
160 argList.add(classesDir);
161 argList.add(".");
162 createJar(argList);
163 }
164
165 private static void createJar(ArrayList<String> args) {
166 if (DEBUG) printIterable("createJar args: ", args);
167
168 Main jarTool = new Main(System.out, System.err, "jar");
169 if (!jarTool.run(args.toArray(new String[1]))) {
170 throw new RuntimeException("jar operation failed");
171 }
172 }
173
174 // Many AppCDS tests use the same simple "Hello.jar" which contains
175 // simple Hello.class and does not specify additional attributes.
176 // For this common use case, use this method to get the jar path.
177 // The method will check if the jar already exists
178 // (created by another test or test run), and will create the jar
179 // if it does not exist
180 public static String getOrCreateHelloJar() throws Exception {
181 String jarPath = getJarFilePath("hello");
182
183 File jarFile = new File(jarPath);
184 if (jarFile.exists()) {
189 }
190
191 public static void compile(String dstPath, String source, String... extraArgs) throws Exception {
192 ArrayList<String> args = new ArrayList<String>();
193 args.add(JDKToolFinder.getCompileJDKTool("javac"));
194 args.add("-d");
195 args.add(dstPath);
196 if (extraArgs != null) {
197 for (String s : extraArgs) {
198 args.add(s);
199 }
200 }
201 args.add(source);
202
203 if (DEBUG) printIterable("compile args: ", args);
204
205 ProcessBuilder pb = new ProcessBuilder(args);
206 OutputAnalyzer output = new OutputAnalyzer(pb.start());
207 output.shouldHaveExitValue(0);
208 }
209
210 public static void compileModule(Path src,
211 Path dest,
212 String modulePathArg // arg to --module-path
213 ) throws Exception {
214 boolean compiled = false;
215 if (modulePathArg == null) {
216 compiled = CompilerUtils.compile(src, dest);
217 } else {
218 compiled = CompilerUtils.compile(src, dest,
219 "--module-path", modulePathArg);
220 }
221 if (!compiled) {
222 throw new RuntimeException("module did not compile");
223 }
224 }
225
226
227 public static void signJar() throws Exception {
228 String keyTool = JDKToolFinder.getJDKTool("keytool");
229 String jarSigner = JDKToolFinder.getJDKTool("jarsigner");
230 String classDir = System.getProperty("test.classes");
231 String FS = File.separator;
232
233 executeProcess(keyTool,
234 "-genkey", "-keystore", "./keystore", "-alias", "mykey",
235 "-storepass", "abc123", "-keypass", "abc123",
236 "-dname", "CN=jvmtest")
237 .shouldHaveExitValue(0);
238
239 executeProcess(jarSigner,
240 "-keystore", "./keystore", "-storepass", "abc123", "-keypass",
241 "abc123", "-signedjar", classDir + FS + "signed_hello.jar",
242 classDir + FS + "hello.jar", "mykey")
243 .shouldHaveExitValue(0);
244 }
245
|