ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 1 | /* |
katleman | 7d8178b | 2011-05-25 13:32:36 -0700 | [diff] [blame^] | 2 | * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved. |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 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 | * |
ohair | 2283b9d | 2010-05-25 15:58:33 -0700 | [diff] [blame] | 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. |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 22 | */ |
| 23 | |
ksrini | f8cc597 | 2011-04-07 12:06:32 -0700 | [diff] [blame] | 24 | import java.nio.file.attribute.BasicFileAttributes; |
| 25 | import java.nio.file.FileVisitResult; |
| 26 | import java.nio.file.SimpleFileVisitor; |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 27 | import javax.tools.ToolProvider; |
| 28 | import java.io.BufferedReader; |
| 29 | import java.io.File; |
| 30 | import java.io.FileNotFoundException; |
| 31 | import java.io.FileOutputStream; |
ksrini | f8cc597 | 2011-04-07 12:06:32 -0700 | [diff] [blame] | 32 | import java.io.IOException; |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 33 | import java.io.InputStreamReader; |
| 34 | import java.io.PrintStream; |
ksrini | f8cc597 | 2011-04-07 12:06:32 -0700 | [diff] [blame] | 35 | import java.nio.file.Files; |
| 36 | import java.nio.file.Path; |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 37 | import java.util.ArrayList; |
| 38 | import java.util.List; |
| 39 | import java.util.Map; |
| 40 | import javax.tools.JavaCompiler; |
| 41 | |
ksrini | f8cc597 | 2011-04-07 12:06:32 -0700 | [diff] [blame] | 42 | import static java.nio.file.StandardCopyOption.*; |
| 43 | |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 44 | /** |
ksrini | f8cc597 | 2011-04-07 12:06:32 -0700 | [diff] [blame] | 45 | * This class provides some common utilities for the launcher tests. |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 46 | */ |
| 47 | public enum TestHelper { |
| 48 | INSTANCE; |
ksrini | 11e7f1b | 2009-11-20 11:01:32 -0800 | [diff] [blame] | 49 | static final String JAVAHOME = System.getProperty("java.home"); |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 50 | static final boolean isSDK = JAVAHOME.endsWith("jre"); |
| 51 | static final String javaCmd; |
ksrini | 11e7f1b | 2009-11-20 11:01:32 -0800 | [diff] [blame] | 52 | static final String java64Cmd; |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 53 | static final String javacCmd; |
| 54 | static final JavaCompiler compiler; |
| 55 | |
ksrini | 11e7f1b | 2009-11-20 11:01:32 -0800 | [diff] [blame] | 56 | static final boolean debug = Boolean.getBoolean("TestHelper.Debug"); |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 57 | static final boolean isWindows = |
| 58 | System.getProperty("os.name", "unknown").startsWith("Windows"); |
ksrini | 11e7f1b | 2009-11-20 11:01:32 -0800 | [diff] [blame] | 59 | 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 | |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 70 | static int testExitValue = 0; |
| 71 | |
| 72 | static { |
ksrini | 11e7f1b | 2009-11-20 11:01:32 -0800 | [diff] [blame] | 73 | 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 | } |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 79 | 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 | } |
ksrini | 11e7f1b | 2009-11-20 11:01:32 -0800 | [diff] [blame] | 97 | 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 | /* |
ksrini | f8cc597 | 2011-04-07 12:06:32 -0700 | [diff] [blame] | 111 | * is a dual mode available in the test jdk |
| 112 | */ |
| 113 | static boolean dualModePresent() { |
| 114 | return isDualMode && java64Cmd != null; |
| 115 | } |
| 116 | |
| 117 | /* |
ksrini | 11e7f1b | 2009-11-20 11:01:32 -0800 | [diff] [blame] | 118 | * 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 | /* |
ksrini | f8cc597 | 2011-04-07 12:06:32 -0700 | [diff] [blame] | 126 | * 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 | /* |
ksrini | 11e7f1b | 2009-11-20 11:01:32 -0800 | [diff] [blame] | 147 | * 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); |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /* |
ksrini | cafaf7c | 2008-10-01 09:04:42 -0700 | [diff] [blame] | 155 | * 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. |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 157 | */ |
| 158 | static void createJar(File jarName, File mainClass, String... mainDefs) |
| 159 | throws FileNotFoundException { |
| 160 | createJar(null, jarName, mainClass, mainDefs); |
| 161 | } |
| 162 | |
| 163 | /* |
ksrini | cafaf7c | 2008-10-01 09:04:42 -0700 | [diff] [blame] | 164 | * 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. |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 168 | */ |
ksrini | cafaf7c | 2008-10-01 09:04:42 -0700 | [diff] [blame] | 169 | static void createJar(String mEntry, File jarName, File mainClass, |
| 170 | String... mainDefs) throws FileNotFoundException { |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 171 | 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 | } |
ksrini | cafaf7c | 2008-10-01 09:04:42 -0700 | [diff] [blame] | 190 | if (mEntry == null) { |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 191 | 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 | |
ksrini | f8cc597 | 2011-04-07 12:06:32 -0700 | [diff] [blame] | 206 | 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 | |
ksrini | 11e7f1b | 2009-11-20 11:01:32 -0800 | [diff] [blame] | 244 | static TestResult doExec(String...cmds) { |
| 245 | return doExec(null, cmds); |
| 246 | } |
| 247 | |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 248 | /* |
ksrini | cafaf7c | 2008-10-01 09:04:42 -0700 | [diff] [blame] | 249 | * A method which executes a java cmd and returns the results in a container |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 250 | */ |
ksrini | 11e7f1b | 2009-11-20 11:01:32 -0800 | [diff] [blame] | 251 | static TestResult doExec(Map<String, String> envToSet, String...cmds) { |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 252 | 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(); |
ksrini | 11e7f1b | 2009-11-20 11:01:32 -0800 | [diff] [blame] | 258 | if (envToSet != null) { |
| 259 | env.putAll(envToSet); |
| 260 | } |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 261 | BufferedReader rdr = null; |
| 262 | try { |
ksrini | f8cc597 | 2011-04-07 12:06:32 -0700 | [diff] [blame] | 263 | List<String> outputList = new ArrayList<>(); |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 264 | 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(); |
ksrini | f8cc597 | 2011-04-07 12:06:32 -0700 | [diff] [blame] | 274 | |
| 275 | return new TestHelper.TestResult(cmdStr, p.exitValue(), outputList, |
| 276 | env, new Throwable("current stack of the test")); |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 277 | } 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; |
ksrini | f8cc597 | 2011-04-07 12:06:32 -0700 | [diff] [blame] | 291 | Map<String, String> env; |
| 292 | Throwable t; |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 293 | |
ksrini | f8cc597 | 2011-04-07 12:06:32 -0700 | [diff] [blame] | 294 | public TestResult(String str, int rv, List<String> oList, |
| 295 | Map<String, String> env, Throwable t) { |
ksrini | 11e7f1b | 2009-11-20 11:01:32 -0800 | [diff] [blame] | 296 | status = new StringBuilder("Executed command: " + str + "\n"); |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 297 | exitValue = rv; |
| 298 | testOutput = oList; |
ksrini | f8cc597 | 2011-04-07 12:06:32 -0700 | [diff] [blame] | 299 | this.env = env; |
| 300 | this.t = t; |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 301 | } |
| 302 | |
ksrini | 11e7f1b | 2009-11-20 11:01:32 -0800 | [diff] [blame] | 303 | void appendStatus(String x) { |
| 304 | status = status.append(" " + x + "\n"); |
| 305 | } |
| 306 | |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 307 | void checkNegative() { |
| 308 | if (exitValue == 0) { |
ksrini | 11e7f1b | 2009-11-20 11:01:32 -0800 | [diff] [blame] | 309 | appendStatus("Error: test must not return 0 exit value"); |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 310 | testExitValue++; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | void checkPositive() { |
| 315 | if (exitValue != 0) { |
ksrini | 11e7f1b | 2009-11-20 11:01:32 -0800 | [diff] [blame] | 316 | appendStatus("Error: test did not return 0 exit value"); |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 317 | testExitValue++; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | boolean isOK() { |
| 322 | return exitValue == 0; |
| 323 | } |
| 324 | |
| 325 | boolean isZeroOutput() { |
| 326 | if (!testOutput.isEmpty()) { |
ksrini | 11e7f1b | 2009-11-20 11:01:32 -0800 | [diff] [blame] | 327 | appendStatus("Error: No message from cmd please"); |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 328 | testExitValue++; |
| 329 | return false; |
| 330 | } |
| 331 | return true; |
| 332 | } |
| 333 | |
| 334 | boolean isNotZeroOutput() { |
| 335 | if (testOutput.isEmpty()) { |
ksrini | 11e7f1b | 2009-11-20 11:01:32 -0800 | [diff] [blame] | 336 | appendStatus("Error: Missing message"); |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 337 | testExitValue++; |
| 338 | return false; |
| 339 | } |
| 340 | return true; |
| 341 | } |
| 342 | |
ksrini | 11e7f1b | 2009-11-20 11:01:32 -0800 | [diff] [blame] | 343 | @Override |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 344 | public String toString() { |
ksrini | f8cc597 | 2011-04-07 12:06:32 -0700 | [diff] [blame] | 345 | 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"); |
ksrini | 11e7f1b | 2009-11-20 11:01:32 -0800 | [diff] [blame] | 351 | for (String x : testOutput) { |
| 352 | appendStatus(x); |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 353 | } |
ksrini | f8cc597 | 2011-04-07 12:06:32 -0700 | [diff] [blame] | 354 | 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"); |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 360 | 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 | } |
ksrini | 11e7f1b | 2009-11-20 11:01:32 -0800 | [diff] [blame] | 369 | 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"); |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 381 | testExitValue++; |
| 382 | return false; |
| 383 | } |
| 384 | } |
| 385 | } |