ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. |
| 4 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 5 | * |
| 6 | * This code is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License version 2 only, as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 | * version 2 for more details (a copy is included in the LICENSE file that |
| 14 | * accompanied this code). |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License version |
| 17 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 18 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | * |
| 20 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 21 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 22 | * have any questions. |
| 23 | */ |
| 24 | |
| 25 | import javax.tools.ToolProvider; |
| 26 | import java.io.BufferedReader; |
| 27 | import java.io.File; |
| 28 | import java.io.FileNotFoundException; |
| 29 | import java.io.FileOutputStream; |
| 30 | import java.io.InputStreamReader; |
| 31 | import java.io.PrintStream; |
| 32 | import java.util.ArrayList; |
| 33 | import java.util.List; |
| 34 | import java.util.Map; |
| 35 | import javax.tools.JavaCompiler; |
| 36 | |
| 37 | /** |
| 38 | * This class provides some common utilites for the launcher tests. |
| 39 | */ |
| 40 | public enum TestHelper { |
| 41 | INSTANCE; |
ksrini | 11e7f1b | 2009-11-20 11:01:32 -0800 | [diff] [blame^] | 42 | static final String JAVAHOME = System.getProperty("java.home"); |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 43 | static final boolean isSDK = JAVAHOME.endsWith("jre"); |
| 44 | static final String javaCmd; |
ksrini | 11e7f1b | 2009-11-20 11:01:32 -0800 | [diff] [blame^] | 45 | static final String java64Cmd; |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 46 | static final String javacCmd; |
| 47 | static final JavaCompiler compiler; |
| 48 | |
ksrini | 11e7f1b | 2009-11-20 11:01:32 -0800 | [diff] [blame^] | 49 | static final boolean debug = Boolean.getBoolean("TestHelper.Debug"); |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 50 | static final boolean isWindows = |
| 51 | System.getProperty("os.name", "unknown").startsWith("Windows"); |
ksrini | 11e7f1b | 2009-11-20 11:01:32 -0800 | [diff] [blame^] | 52 | static final boolean is64Bit = |
| 53 | System.getProperty("sun.arch.data.model").equals("64"); |
| 54 | static final boolean is32Bit = |
| 55 | System.getProperty("sun.arch.data.model").equals("32"); |
| 56 | static final boolean isSolaris = |
| 57 | System.getProperty("os.name", "unknown").startsWith("SunOS"); |
| 58 | static final boolean isLinux = |
| 59 | System.getProperty("os.name", "unknown").startsWith("Linux"); |
| 60 | static final boolean isDualMode = isSolaris; |
| 61 | static final boolean isSparc = System.getProperty("os.arch").startsWith("sparc"); |
| 62 | |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 63 | static int testExitValue = 0; |
| 64 | |
| 65 | static { |
ksrini | 11e7f1b | 2009-11-20 11:01:32 -0800 | [diff] [blame^] | 66 | if (is64Bit && is32Bit) { |
| 67 | throw new RuntimeException("arch model cannot be both 32 and 64 bit"); |
| 68 | } |
| 69 | if (!is64Bit && !is32Bit) { |
| 70 | throw new RuntimeException("arch model is not 32 or 64 bit ?"); |
| 71 | } |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 72 | compiler = ToolProvider.getSystemJavaCompiler(); |
| 73 | File binDir = (isSDK) ? new File((new File(JAVAHOME)).getParentFile(), "bin") |
| 74 | : new File(JAVAHOME, "bin"); |
| 75 | File javaCmdFile = (isWindows) |
| 76 | ? new File(binDir, "java.exe") |
| 77 | : new File(binDir, "java"); |
| 78 | javaCmd = javaCmdFile.getAbsolutePath(); |
| 79 | if (!javaCmdFile.canExecute()) { |
| 80 | throw new RuntimeException("java <" + TestHelper.javaCmd + "> must exist"); |
| 81 | } |
| 82 | |
| 83 | File javacCmdFile = (isWindows) |
| 84 | ? new File(binDir, "javac.exe") |
| 85 | : new File(binDir, "javac"); |
| 86 | javacCmd = javacCmdFile.getAbsolutePath(); |
| 87 | if (!javacCmdFile.canExecute()) { |
| 88 | throw new RuntimeException("java <" + javacCmd + "> must exist"); |
| 89 | } |
ksrini | 11e7f1b | 2009-11-20 11:01:32 -0800 | [diff] [blame^] | 90 | if (isSolaris) { |
| 91 | File sparc64BinDir = new File(binDir,isSparc ? "sparcv9" : "amd64"); |
| 92 | File java64CmdFile= new File(sparc64BinDir, "java"); |
| 93 | if (java64CmdFile.exists() && java64CmdFile.canExecute()) { |
| 94 | java64Cmd = java64CmdFile.getAbsolutePath(); |
| 95 | } else { |
| 96 | java64Cmd = null; |
| 97 | } |
| 98 | } else { |
| 99 | java64Cmd = null; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * usually the jre/lib/arch-name is the same as os.arch, except for x86. |
| 105 | */ |
| 106 | static String getJreArch() { |
| 107 | String arch = System.getProperty("os.arch"); |
| 108 | return arch.equals("x86") ? "i386" : arch; |
| 109 | } |
| 110 | |
| 111 | /* |
| 112 | * A convenience method to create a jar with jar file name and defs |
| 113 | */ |
| 114 | static void createJar(File jarName, String... mainDefs) |
| 115 | throws FileNotFoundException{ |
| 116 | createJar(null, jarName, new File("Foo"), mainDefs); |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /* |
ksrini | cafaf7c | 2008-10-01 09:04:42 -0700 | [diff] [blame] | 120 | * A convenience method to create a java file, compile and jar it up, using |
| 121 | * 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] | 122 | */ |
| 123 | static void createJar(File jarName, File mainClass, String... mainDefs) |
| 124 | throws FileNotFoundException { |
| 125 | createJar(null, jarName, mainClass, mainDefs); |
| 126 | } |
| 127 | |
| 128 | /* |
ksrini | cafaf7c | 2008-10-01 09:04:42 -0700 | [diff] [blame] | 129 | * A generic jar file creator to create a java file, compile it |
| 130 | * and jar it up, a specific Main-Class entry name in the |
| 131 | * manifest can be specified or a null to use the sole class file name |
| 132 | * as the Main-Class attribute value. |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 133 | */ |
ksrini | cafaf7c | 2008-10-01 09:04:42 -0700 | [diff] [blame] | 134 | static void createJar(String mEntry, File jarName, File mainClass, |
| 135 | String... mainDefs) throws FileNotFoundException { |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 136 | if (jarName.exists()) { |
| 137 | jarName.delete(); |
| 138 | } |
| 139 | PrintStream ps = new PrintStream(new FileOutputStream(mainClass + ".java")); |
| 140 | ps.println("public class Foo {"); |
| 141 | if (mainDefs != null) { |
| 142 | for (String x : mainDefs) { |
| 143 | ps.println(x); |
| 144 | } |
| 145 | } |
| 146 | ps.println("}"); |
| 147 | ps.close(); |
| 148 | |
| 149 | String compileArgs[] = { |
| 150 | mainClass + ".java" |
| 151 | }; |
| 152 | if (compiler.run(null, null, null, compileArgs) != 0) { |
| 153 | throw new RuntimeException("compilation failed " + mainClass + ".java"); |
| 154 | } |
ksrini | cafaf7c | 2008-10-01 09:04:42 -0700 | [diff] [blame] | 155 | if (mEntry == null) { |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 156 | mEntry = mainClass.getName(); |
| 157 | } |
| 158 | String jarArgs[] = { |
| 159 | (debug) ? "cvfe" : "cfe", |
| 160 | jarName.getAbsolutePath(), |
| 161 | mEntry, |
| 162 | mainClass.getName() + ".class" |
| 163 | }; |
| 164 | sun.tools.jar.Main jarTool = |
| 165 | new sun.tools.jar.Main(System.out, System.err, "JarCreator"); |
| 166 | if (!jarTool.run(jarArgs)) { |
| 167 | throw new RuntimeException("jar creation failed " + jarName); |
| 168 | } |
| 169 | } |
| 170 | |
ksrini | 11e7f1b | 2009-11-20 11:01:32 -0800 | [diff] [blame^] | 171 | static TestResult doExec(String...cmds) { |
| 172 | return doExec(null, cmds); |
| 173 | } |
| 174 | |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 175 | /* |
ksrini | cafaf7c | 2008-10-01 09:04:42 -0700 | [diff] [blame] | 176 | * 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] | 177 | */ |
ksrini | 11e7f1b | 2009-11-20 11:01:32 -0800 | [diff] [blame^] | 178 | static TestResult doExec(Map<String, String> envToSet, String...cmds) { |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 179 | String cmdStr = ""; |
| 180 | for (String x : cmds) { |
| 181 | cmdStr = cmdStr.concat(x + " "); |
| 182 | } |
| 183 | ProcessBuilder pb = new ProcessBuilder(cmds); |
| 184 | Map<String, String> env = pb.environment(); |
ksrini | 11e7f1b | 2009-11-20 11:01:32 -0800 | [diff] [blame^] | 185 | if (envToSet != null) { |
| 186 | env.putAll(envToSet); |
| 187 | } |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 188 | BufferedReader rdr = null; |
| 189 | try { |
| 190 | List<String> outputList = new ArrayList<String>(); |
| 191 | pb.redirectErrorStream(true); |
| 192 | Process p = pb.start(); |
| 193 | rdr = new BufferedReader(new InputStreamReader(p.getInputStream())); |
| 194 | String in = rdr.readLine(); |
| 195 | while (in != null) { |
| 196 | outputList.add(in); |
| 197 | in = rdr.readLine(); |
| 198 | } |
| 199 | p.waitFor(); |
| 200 | p.destroy(); |
| 201 | return new TestHelper.TestResult(cmdStr, p.exitValue(), outputList); |
| 202 | } catch (Exception ex) { |
| 203 | ex.printStackTrace(); |
| 204 | throw new RuntimeException(ex.getMessage()); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * A class to encapsulate the test results and stuff, with some ease |
| 210 | * of use methods to check the test results. |
| 211 | */ |
| 212 | static class TestResult { |
| 213 | StringBuilder status; |
| 214 | int exitValue; |
| 215 | List<String> testOutput; |
| 216 | |
| 217 | public TestResult(String str, int rv, List<String> oList) { |
ksrini | 11e7f1b | 2009-11-20 11:01:32 -0800 | [diff] [blame^] | 218 | status = new StringBuilder("Executed command: " + str + "\n"); |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 219 | exitValue = rv; |
| 220 | testOutput = oList; |
| 221 | } |
| 222 | |
ksrini | 11e7f1b | 2009-11-20 11:01:32 -0800 | [diff] [blame^] | 223 | void appendStatus(String x) { |
| 224 | status = status.append(" " + x + "\n"); |
| 225 | } |
| 226 | |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 227 | void checkNegative() { |
| 228 | if (exitValue == 0) { |
ksrini | 11e7f1b | 2009-11-20 11:01:32 -0800 | [diff] [blame^] | 229 | appendStatus("Error: test must not return 0 exit value"); |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 230 | testExitValue++; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | void checkPositive() { |
| 235 | if (exitValue != 0) { |
ksrini | 11e7f1b | 2009-11-20 11:01:32 -0800 | [diff] [blame^] | 236 | appendStatus("Error: test did not return 0 exit value"); |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 237 | testExitValue++; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | boolean isOK() { |
| 242 | return exitValue == 0; |
| 243 | } |
| 244 | |
| 245 | boolean isZeroOutput() { |
| 246 | if (!testOutput.isEmpty()) { |
ksrini | 11e7f1b | 2009-11-20 11:01:32 -0800 | [diff] [blame^] | 247 | appendStatus("Error: No message from cmd please"); |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 248 | testExitValue++; |
| 249 | return false; |
| 250 | } |
| 251 | return true; |
| 252 | } |
| 253 | |
| 254 | boolean isNotZeroOutput() { |
| 255 | if (testOutput.isEmpty()) { |
ksrini | 11e7f1b | 2009-11-20 11:01:32 -0800 | [diff] [blame^] | 256 | appendStatus("Error: Missing message"); |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 257 | testExitValue++; |
| 258 | return false; |
| 259 | } |
| 260 | return true; |
| 261 | } |
| 262 | |
ksrini | 11e7f1b | 2009-11-20 11:01:32 -0800 | [diff] [blame^] | 263 | @Override |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 264 | public String toString() { |
ksrini | 11e7f1b | 2009-11-20 11:01:32 -0800 | [diff] [blame^] | 265 | status = status.append("++++Test Output Begin++++\n"); |
| 266 | for (String x : testOutput) { |
| 267 | appendStatus(x); |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 268 | } |
ksrini | 11e7f1b | 2009-11-20 11:01:32 -0800 | [diff] [blame^] | 269 | status = status.append("++++Test Output End++++\n"); |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 270 | return status.toString(); |
| 271 | } |
| 272 | |
| 273 | boolean contains(String str) { |
| 274 | for (String x : testOutput) { |
| 275 | if (x.contains(str)) { |
| 276 | return true; |
| 277 | } |
| 278 | } |
ksrini | 11e7f1b | 2009-11-20 11:01:32 -0800 | [diff] [blame^] | 279 | appendStatus("Error: string <" + str + "> not found"); |
| 280 | testExitValue++; |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | boolean matches(String stringToMatch) { |
| 285 | for (String x : testOutput) { |
| 286 | if (x.matches(stringToMatch)) { |
| 287 | return true; |
| 288 | } |
| 289 | } |
| 290 | appendStatus("Error: string <" + stringToMatch + "> not found"); |
ksrini | 20a64b2 | 2008-09-24 15:07:41 -0700 | [diff] [blame] | 291 | testExitValue++; |
| 292 | return false; |
| 293 | } |
| 294 | } |
| 295 | } |