J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2002-2006 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test NoLaunchOptionTest.java |
| 26 | * @bug 4554734 4724714 |
| 27 | * @summary Test for -Xrunjdwp:[onthrow,onuncaught] suboptions require launch suboption |
| 28 | * @author Tim Bell |
| 29 | * |
| 30 | * @run compile -g NoLaunchOptionTest.java |
| 31 | * @build VMConnection |
| 32 | * @run main/othervm NoLaunchOptionTest |
| 33 | */ |
| 34 | public class NoLaunchOptionTest extends Object { |
| 35 | private Process subprocess; |
| 36 | private int subprocessStatus; |
| 37 | private static final String CR = System.getProperty("line.separator"); |
| 38 | private static final int BUFFERSIZE = 4096; |
| 39 | public static final int RETSTAT = 0; |
| 40 | public static final int STDOUT = 1; |
| 41 | public static final int STDERR = 2; |
| 42 | |
| 43 | /** |
| 44 | * Run an arbitrary command and return the results to caller. |
| 45 | * |
| 46 | * @param an array of String containing the command |
| 47 | * to run and any flags or parameters to the command. |
| 48 | * |
| 49 | * @return completion status, stderr and stdout as array of String |
| 50 | * Look for: |
| 51 | * return status in result[NoLaunchOptionTest.RETSTAT] |
| 52 | * standard out in result[NoLaunchOptionTest.STDOUT] |
| 53 | * standard err in result[NoLaunchOptionTest.STDERR] |
| 54 | * |
| 55 | */ |
| 56 | public String[] run (String[] cmdStrings) { |
| 57 | StringBuffer stdoutBuffer = new StringBuffer(); |
| 58 | StringBuffer stderrBuffer = new StringBuffer(); |
| 59 | |
| 60 | System.out.print(CR + "runCommand method about to execute: "); |
| 61 | for (int iNdx = 0; iNdx < cmdStrings.length; iNdx++) { |
| 62 | System.out.print(" "); |
| 63 | System.out.print(cmdStrings[iNdx]); |
| 64 | } |
| 65 | System.out.println(CR); |
| 66 | try { |
| 67 | Process process = Runtime.getRuntime().exec(cmdStrings); |
| 68 | /* |
| 69 | * Gather up the output of the subprocess using non-blocking |
| 70 | * reads so we can get both the subprocess stdout and the |
| 71 | * subprocess stderr without overfilling any buffers. |
| 72 | */ |
| 73 | java.io.BufferedInputStream is = |
| 74 | new java.io.BufferedInputStream(process.getInputStream()); |
| 75 | int isLen = 0; |
| 76 | byte[] isBuf = new byte[BUFFERSIZE]; |
| 77 | |
| 78 | java.io.BufferedInputStream es = |
| 79 | new java.io.BufferedInputStream(process.getErrorStream()); |
| 80 | int esLen = 0; |
| 81 | byte[] esBuf = new byte[BUFFERSIZE]; |
| 82 | |
| 83 | do { |
| 84 | isLen = is.read(isBuf); |
| 85 | if (isLen > 0) { |
| 86 | stdoutBuffer.append( |
| 87 | new String(isBuf, 0, isLen)); |
| 88 | } |
| 89 | esLen = es.read(esBuf); |
| 90 | if (esLen > 0) { |
| 91 | stderrBuffer.append( |
| 92 | new String(esBuf, 0, esLen)); |
| 93 | } |
| 94 | } while ((isLen > -1) || (esLen > -1)); |
| 95 | try { |
| 96 | process.waitFor(); |
| 97 | subprocessStatus = process.exitValue(); |
| 98 | process = null; |
| 99 | } catch(java.lang.InterruptedException e) { |
| 100 | System.err.println("InterruptedException: " + e); |
| 101 | } |
| 102 | |
| 103 | } catch(java.io.IOException ex) { |
| 104 | System.err.println("IO error: " + ex); |
| 105 | } |
| 106 | String[] result = |
| 107 | new String[] { |
| 108 | Integer.toString(subprocessStatus), |
| 109 | stdoutBuffer.toString(), |
| 110 | stderrBuffer.toString() |
| 111 | }; |
| 112 | |
| 113 | System.out.println(CR + "--- Return code was: " + |
| 114 | CR + result[RETSTAT]); |
| 115 | System.out.println(CR + "--- Return stdout was: " + |
| 116 | CR + result[STDOUT]); |
| 117 | System.out.println(CR + "--- Return stderr was: " + |
| 118 | CR + result[STDERR]); |
| 119 | |
| 120 | return result; |
| 121 | } |
| 122 | |
| 123 | public static void main(String[] args) throws Exception { |
| 124 | String javaExe = System.getProperty("java.home") + |
| 125 | java.io.File.separator + "bin" + |
| 126 | java.io.File.separator + "java"; |
| 127 | String targetClass = "NotAClass"; |
| 128 | String cmds [] = {javaExe, |
| 129 | "-agentlib:jdwp=transport=dt_socket,address=8000," + |
| 130 | "onthrow=java.lang.ClassNotFoundException,suspend=n", |
| 131 | targetClass}; |
| 132 | NoLaunchOptionTest myTest = new NoLaunchOptionTest(); |
| 133 | String results [] = myTest.run(VMConnection.insertDebuggeeVMOptions(cmds)); |
| 134 | if ((results[RETSTAT].equals("1")) && |
| 135 | (results[STDERR].startsWith("ERROR:"))) { |
| 136 | System.out.println("Test passed: status = 1 with warning messages " + |
| 137 | "is expected and normal for this test"); |
| 138 | } else { |
| 139 | throw new Exception("Test failed: unspecified test failure"); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | } |