blob: ca20e3e37a0116a0c881f654cd9df070ac15dc4c [file] [log] [blame]
ksrini20a64b22008-09-24 15:07:41 -07001/*
katleman7d8178b2011-05-25 13:32:36 -07002 * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
ksrini20a64b22008-09-24 15:07:41 -07003 * 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 *
ohair2283b9d2010-05-25 15:58:33 -070019 * 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.
ksrini20a64b22008-09-24 15:07:41 -070022 */
23
ksrinif8cc5972011-04-07 12:06:32 -070024import java.nio.file.attribute.BasicFileAttributes;
25import java.nio.file.FileVisitResult;
26import java.nio.file.SimpleFileVisitor;
ksrini20a64b22008-09-24 15:07:41 -070027import javax.tools.ToolProvider;
28import java.io.BufferedReader;
29import java.io.File;
30import java.io.FileNotFoundException;
31import java.io.FileOutputStream;
ksrinif8cc5972011-04-07 12:06:32 -070032import java.io.IOException;
ksrini20a64b22008-09-24 15:07:41 -070033import java.io.InputStreamReader;
34import java.io.PrintStream;
ksrinif8cc5972011-04-07 12:06:32 -070035import java.nio.file.Files;
36import java.nio.file.Path;
ksrini20a64b22008-09-24 15:07:41 -070037import java.util.ArrayList;
38import java.util.List;
39import java.util.Map;
40import javax.tools.JavaCompiler;
41
ksrinif8cc5972011-04-07 12:06:32 -070042import static java.nio.file.StandardCopyOption.*;
43
ksrini20a64b22008-09-24 15:07:41 -070044/**
ksrinif8cc5972011-04-07 12:06:32 -070045 * This class provides some common utilities for the launcher tests.
ksrini20a64b22008-09-24 15:07:41 -070046 */
47public enum TestHelper {
48 INSTANCE;
ksrini11e7f1b2009-11-20 11:01:32 -080049 static final String JAVAHOME = System.getProperty("java.home");
ksrini20a64b22008-09-24 15:07:41 -070050 static final boolean isSDK = JAVAHOME.endsWith("jre");
51 static final String javaCmd;
ksrini11e7f1b2009-11-20 11:01:32 -080052 static final String java64Cmd;
ksrini20a64b22008-09-24 15:07:41 -070053 static final String javacCmd;
54 static final JavaCompiler compiler;
55
ksrini11e7f1b2009-11-20 11:01:32 -080056 static final boolean debug = Boolean.getBoolean("TestHelper.Debug");
ksrini20a64b22008-09-24 15:07:41 -070057 static final boolean isWindows =
58 System.getProperty("os.name", "unknown").startsWith("Windows");
ksrini11e7f1b2009-11-20 11:01:32 -080059 static final boolean is64Bit =
60 System.getProperty("sun.arch.data.model").equals("64");
61 static final boolean is32Bit =
62 System.getProperty("sun.arch.data.model").equals("32");
63 static final boolean isSolaris =
64 System.getProperty("os.name", "unknown").startsWith("SunOS");
65 static final boolean isLinux =
66 System.getProperty("os.name", "unknown").startsWith("Linux");
67 static final boolean isDualMode = isSolaris;
68 static final boolean isSparc = System.getProperty("os.arch").startsWith("sparc");
69
ksrini20a64b22008-09-24 15:07:41 -070070 static int testExitValue = 0;
71
72 static {
ksrini11e7f1b2009-11-20 11:01:32 -080073 if (is64Bit && is32Bit) {
74 throw new RuntimeException("arch model cannot be both 32 and 64 bit");
75 }
76 if (!is64Bit && !is32Bit) {
77 throw new RuntimeException("arch model is not 32 or 64 bit ?");
78 }
ksrini20a64b22008-09-24 15:07:41 -070079 compiler = ToolProvider.getSystemJavaCompiler();
80 File binDir = (isSDK) ? new File((new File(JAVAHOME)).getParentFile(), "bin")
81 : new File(JAVAHOME, "bin");
82 File javaCmdFile = (isWindows)
83 ? new File(binDir, "java.exe")
84 : new File(binDir, "java");
85 javaCmd = javaCmdFile.getAbsolutePath();
86 if (!javaCmdFile.canExecute()) {
87 throw new RuntimeException("java <" + TestHelper.javaCmd + "> must exist");
88 }
89
90 File javacCmdFile = (isWindows)
91 ? new File(binDir, "javac.exe")
92 : new File(binDir, "javac");
93 javacCmd = javacCmdFile.getAbsolutePath();
94 if (!javacCmdFile.canExecute()) {
95 throw new RuntimeException("java <" + javacCmd + "> must exist");
96 }
ksrini11e7f1b2009-11-20 11:01:32 -080097 if (isSolaris) {
98 File sparc64BinDir = new File(binDir,isSparc ? "sparcv9" : "amd64");
99 File java64CmdFile= new File(sparc64BinDir, "java");
100 if (java64CmdFile.exists() && java64CmdFile.canExecute()) {
101 java64Cmd = java64CmdFile.getAbsolutePath();
102 } else {
103 java64Cmd = null;
104 }
105 } else {
106 java64Cmd = null;
107 }
108 }
109
110 /*
ksrinif8cc5972011-04-07 12:06:32 -0700111 * is a dual mode available in the test jdk
112 */
113 static boolean dualModePresent() {
114 return isDualMode && java64Cmd != null;
115 }
116
117 /*
ksrini11e7f1b2009-11-20 11:01:32 -0800118 * usually the jre/lib/arch-name is the same as os.arch, except for x86.
119 */
120 static String getJreArch() {
121 String arch = System.getProperty("os.arch");
122 return arch.equals("x86") ? "i386" : arch;
123 }
124
125 /*
ksrinif8cc5972011-04-07 12:06:32 -0700126 * get the complementary jre arch ie. if sparc then return sparcv9 and
127 * vice-versa.
128 */
129 static String getComplementaryJreArch() {
130 String arch = System.getProperty("os.arch");
131 if (arch != null) {
132 switch (arch) {
133 case "sparc":
134 return "sparcv9";
135 case "sparcv9":
136 return "sparc";
137 case "x86":
138 return "amd64";
139 case "amd64":
140 return "i386";
141 }
142 }
143 return null;
144 }
145
146 /*
ksrini11e7f1b2009-11-20 11:01:32 -0800147 * A convenience method to create a jar with jar file name and defs
148 */
149 static void createJar(File jarName, String... mainDefs)
150 throws FileNotFoundException{
151 createJar(null, jarName, new File("Foo"), mainDefs);
ksrini20a64b22008-09-24 15:07:41 -0700152 }
153
154 /*
ksrinicafaf7c2008-10-01 09:04:42 -0700155 * A convenience method to create a java file, compile and jar it up, using
156 * the sole class file name in the jar, as the Main-Class attribute value.
ksrini20a64b22008-09-24 15:07:41 -0700157 */
158 static void createJar(File jarName, File mainClass, String... mainDefs)
159 throws FileNotFoundException {
160 createJar(null, jarName, mainClass, mainDefs);
161 }
162
163 /*
ksrinicafaf7c2008-10-01 09:04:42 -0700164 * A generic jar file creator to create a java file, compile it
165 * and jar it up, a specific Main-Class entry name in the
166 * manifest can be specified or a null to use the sole class file name
167 * as the Main-Class attribute value.
ksrini20a64b22008-09-24 15:07:41 -0700168 */
ksrinicafaf7c2008-10-01 09:04:42 -0700169 static void createJar(String mEntry, File jarName, File mainClass,
170 String... mainDefs) throws FileNotFoundException {
ksrini20a64b22008-09-24 15:07:41 -0700171 if (jarName.exists()) {
172 jarName.delete();
173 }
174 PrintStream ps = new PrintStream(new FileOutputStream(mainClass + ".java"));
175 ps.println("public class Foo {");
176 if (mainDefs != null) {
177 for (String x : mainDefs) {
178 ps.println(x);
179 }
180 }
181 ps.println("}");
182 ps.close();
183
184 String compileArgs[] = {
185 mainClass + ".java"
186 };
187 if (compiler.run(null, null, null, compileArgs) != 0) {
188 throw new RuntimeException("compilation failed " + mainClass + ".java");
189 }
ksrinicafaf7c2008-10-01 09:04:42 -0700190 if (mEntry == null) {
ksrini20a64b22008-09-24 15:07:41 -0700191 mEntry = mainClass.getName();
192 }
193 String jarArgs[] = {
194 (debug) ? "cvfe" : "cfe",
195 jarName.getAbsolutePath(),
196 mEntry,
197 mainClass.getName() + ".class"
198 };
199 sun.tools.jar.Main jarTool =
200 new sun.tools.jar.Main(System.out, System.err, "JarCreator");
201 if (!jarTool.run(jarArgs)) {
202 throw new RuntimeException("jar creation failed " + jarName);
203 }
204 }
205
ksrinif8cc5972011-04-07 12:06:32 -0700206 static void copyFile(File src, File dst) throws IOException {
207 Path parent = dst.toPath().getParent();
208 if (parent != null) {
209 Files.createDirectories(parent);
210 }
211 Files.copy(src.toPath(), dst.toPath(), COPY_ATTRIBUTES, REPLACE_EXISTING);
212 }
213
214 static void recursiveDelete(File target) throws IOException {
215 if (!target.exists()) {
216 return;
217 }
218 Files.walkFileTree(target.toPath(), new SimpleFileVisitor<Path>() {
219 @Override
220 public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
221 try {
222 Files.deleteIfExists(dir);
223 } catch (IOException ex) {
224 System.out.println("Error: could not delete: " + dir.toString());
225 System.out.println(ex.getMessage());
226 return FileVisitResult.TERMINATE;
227 }
228 return FileVisitResult.CONTINUE;
229 }
230 @Override
231 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
232 try {
233 Files.deleteIfExists(file);
234 } catch (IOException ex) {
235 System.out.println("Error: could not delete: " + file.toString());
236 System.out.println(ex.getMessage());
237 return FileVisitResult.TERMINATE;
238 }
239 return FileVisitResult.CONTINUE;
240 }
241 });
242 }
243
ksrini11e7f1b2009-11-20 11:01:32 -0800244 static TestResult doExec(String...cmds) {
245 return doExec(null, cmds);
246 }
247
ksrini20a64b22008-09-24 15:07:41 -0700248 /*
ksrinicafaf7c2008-10-01 09:04:42 -0700249 * A method which executes a java cmd and returns the results in a container
ksrini20a64b22008-09-24 15:07:41 -0700250 */
ksrini11e7f1b2009-11-20 11:01:32 -0800251 static TestResult doExec(Map<String, String> envToSet, String...cmds) {
ksrini20a64b22008-09-24 15:07:41 -0700252 String cmdStr = "";
253 for (String x : cmds) {
254 cmdStr = cmdStr.concat(x + " ");
255 }
256 ProcessBuilder pb = new ProcessBuilder(cmds);
257 Map<String, String> env = pb.environment();
ksrini11e7f1b2009-11-20 11:01:32 -0800258 if (envToSet != null) {
259 env.putAll(envToSet);
260 }
ksrini20a64b22008-09-24 15:07:41 -0700261 BufferedReader rdr = null;
262 try {
ksrinif8cc5972011-04-07 12:06:32 -0700263 List<String> outputList = new ArrayList<>();
ksrini20a64b22008-09-24 15:07:41 -0700264 pb.redirectErrorStream(true);
265 Process p = pb.start();
266 rdr = new BufferedReader(new InputStreamReader(p.getInputStream()));
267 String in = rdr.readLine();
268 while (in != null) {
269 outputList.add(in);
270 in = rdr.readLine();
271 }
272 p.waitFor();
273 p.destroy();
ksrinif8cc5972011-04-07 12:06:32 -0700274
275 return new TestHelper.TestResult(cmdStr, p.exitValue(), outputList,
276 env, new Throwable("current stack of the test"));
ksrini20a64b22008-09-24 15:07:41 -0700277 } catch (Exception ex) {
278 ex.printStackTrace();
279 throw new RuntimeException(ex.getMessage());
280 }
281 }
282
283 /*
284 * A class to encapsulate the test results and stuff, with some ease
285 * of use methods to check the test results.
286 */
287 static class TestResult {
288 StringBuilder status;
289 int exitValue;
290 List<String> testOutput;
ksrinif8cc5972011-04-07 12:06:32 -0700291 Map<String, String> env;
292 Throwable t;
ksrini20a64b22008-09-24 15:07:41 -0700293
ksrinif8cc5972011-04-07 12:06:32 -0700294 public TestResult(String str, int rv, List<String> oList,
295 Map<String, String> env, Throwable t) {
ksrini11e7f1b2009-11-20 11:01:32 -0800296 status = new StringBuilder("Executed command: " + str + "\n");
ksrini20a64b22008-09-24 15:07:41 -0700297 exitValue = rv;
298 testOutput = oList;
ksrinif8cc5972011-04-07 12:06:32 -0700299 this.env = env;
300 this.t = t;
ksrini20a64b22008-09-24 15:07:41 -0700301 }
302
ksrini11e7f1b2009-11-20 11:01:32 -0800303 void appendStatus(String x) {
304 status = status.append(" " + x + "\n");
305 }
306
ksrini20a64b22008-09-24 15:07:41 -0700307 void checkNegative() {
308 if (exitValue == 0) {
ksrini11e7f1b2009-11-20 11:01:32 -0800309 appendStatus("Error: test must not return 0 exit value");
ksrini20a64b22008-09-24 15:07:41 -0700310 testExitValue++;
311 }
312 }
313
314 void checkPositive() {
315 if (exitValue != 0) {
ksrini11e7f1b2009-11-20 11:01:32 -0800316 appendStatus("Error: test did not return 0 exit value");
ksrini20a64b22008-09-24 15:07:41 -0700317 testExitValue++;
318 }
319 }
320
321 boolean isOK() {
322 return exitValue == 0;
323 }
324
325 boolean isZeroOutput() {
326 if (!testOutput.isEmpty()) {
ksrini11e7f1b2009-11-20 11:01:32 -0800327 appendStatus("Error: No message from cmd please");
ksrini20a64b22008-09-24 15:07:41 -0700328 testExitValue++;
329 return false;
330 }
331 return true;
332 }
333
334 boolean isNotZeroOutput() {
335 if (testOutput.isEmpty()) {
ksrini11e7f1b2009-11-20 11:01:32 -0800336 appendStatus("Error: Missing message");
ksrini20a64b22008-09-24 15:07:41 -0700337 testExitValue++;
338 return false;
339 }
340 return true;
341 }
342
ksrini11e7f1b2009-11-20 11:01:32 -0800343 @Override
ksrini20a64b22008-09-24 15:07:41 -0700344 public String toString() {
ksrinif8cc5972011-04-07 12:06:32 -0700345 status.append("++++Begin Test Info++++\n");
346 status.append("++++Test Environment++++\n");
347 for (String x : env.keySet()) {
348 status.append(x).append("=").append(env.get(x)).append("\n");
349 }
350 status.append("++++Test Output++++\n");
ksrini11e7f1b2009-11-20 11:01:32 -0800351 for (String x : testOutput) {
352 appendStatus(x);
ksrini20a64b22008-09-24 15:07:41 -0700353 }
ksrinif8cc5972011-04-07 12:06:32 -0700354 status.append("++++Test Stack Trace++++\n");
355 status.append(t.toString());
356 for (StackTraceElement e : t.getStackTrace()) {
357 status.append(e.toString());
358 }
359 status.append("++++End of Test Info++++\n");
ksrini20a64b22008-09-24 15:07:41 -0700360 return status.toString();
361 }
362
363 boolean contains(String str) {
364 for (String x : testOutput) {
365 if (x.contains(str)) {
366 return true;
367 }
368 }
ksrini11e7f1b2009-11-20 11:01:32 -0800369 appendStatus("Error: string <" + str + "> not found");
370 testExitValue++;
371 return false;
372 }
373
374 boolean matches(String stringToMatch) {
375 for (String x : testOutput) {
376 if (x.matches(stringToMatch)) {
377 return true;
378 }
379 }
380 appendStatus("Error: string <" + stringToMatch + "> not found");
ksrini20a64b22008-09-24 15:07:41 -0700381 testExitValue++;
382 return false;
383 }
384 }
385}