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 
  35 import java.net.ServerSocket;
  36 
  37 public class NoLaunchOptionTest extends Object {
  38     private Process subprocess;
  39     private int subprocessStatus;
  40     private static final String CR = System.getProperty("line.separator");
  41     private static final int BUFFERSIZE = 4096;
  42     public static final int RETSTAT = 0;
  43     public static final int STDOUT = 1;
  44     public static final int STDERR = 2;
  45 
  46     /**
  47      * Run an arbitrary command and return the results to caller.
  48      *
  49      * @param an array of String containing the command
  50      *        to run and any flags or parameters to the command.
  51      *
  52      * @return completion status, stderr and stdout as array of String
  53      *  Look for:
  54      *    return status in result[NoLaunchOptionTest.RETSTAT]
  55      *    standard out in result[NoLaunchOptionTest.STDOUT]
  56      *    standard err in result[NoLaunchOptionTest.STDERR]
  57      *
  58      */
  59     public String[] run (String[] cmdStrings) {
  60         StringBuffer stdoutBuffer = new StringBuffer();
  61         StringBuffer stderrBuffer = new StringBuffer();
  62 
  63         System.out.print(CR + "runCommand method about to execute: ");
  64         for (int iNdx = 0; iNdx < cmdStrings.length; iNdx++) {
  65             System.out.print(" ");
  66             System.out.print(cmdStrings[iNdx]);
  67         }
  68         System.out.println(CR);
  69         try {
  70             Process process = Runtime.getRuntime().exec(cmdStrings);
  71             /*
  72              * Gather up the output of the subprocess using non-blocking
  73              * reads so we can get both the subprocess stdout and the
  74              * subprocess stderr without overfilling any buffers.
  75              */
  76             java.io.BufferedInputStream is =
  77                 new java.io.BufferedInputStream(process.getInputStream());
  78             int isLen = 0;
  79             byte[] isBuf = new byte[BUFFERSIZE];
  80 
  81             java.io.BufferedInputStream es =
  82                 new java.io.BufferedInputStream(process.getErrorStream());
  83             int esLen = 0;
  84             byte[] esBuf = new byte[BUFFERSIZE];
  85 
  86             do {
  87                 isLen = is.read(isBuf);
  88                 if (isLen > 0) {
  89                     stdoutBuffer.append(
  90                                         new String(isBuf, 0, isLen));
  91                 }
  92                 esLen = es.read(esBuf);
  93                 if (esLen > 0) {
  94                     stderrBuffer.append(
  95                                         new String(esBuf, 0, esLen));
  96                 }
  97             } while ((isLen > -1) || (esLen > -1));
  98             try {
  99                 process.waitFor();
 100                 subprocessStatus = process.exitValue();
 101                 process = null;
 102             } catch(java.lang.InterruptedException e) {
 103                 System.err.println("InterruptedException: " + e);
 104             }
 105 
 106         } catch(java.io.IOException ex) {
 107             System.err.println("IO error: " + ex);
 108         }
 109         String[] result =
 110             new String[] {
 111                 Integer.toString(subprocessStatus),
 112                 stdoutBuffer.toString(),
 113                 stderrBuffer.toString()
 114         };
 115 
 116         System.out.println(CR + "--- Return code was: " +
 117                            CR + result[RETSTAT]);
 118         System.out.println(CR + "--- Return stdout was: " +
 119                            CR + result[STDOUT]);
 120         System.out.println(CR + "--- Return stderr was: " +
 121                            CR + result[STDERR]);
 122 
 123         return result;
 124     }
 125 
 126     public static void main(String[] args) throws Exception {
 127         // find a free port
 128         ServerSocket ss = new ServerSocket(0);
 129         int port = ss.getLocalPort();
 130         ss.close();
 131         String address = String.valueOf(port);
 132 
 133         String javaExe = System.getProperty("java.home") +
 134             java.io.File.separator + "bin" +
 135             java.io.File.separator + "java";
 136         String targetClass = "NotAClass";
 137         String cmds [] = {javaExe,
 138                           "-agentlib:jdwp=transport=dt_socket,address=" + 
 139                           address + "," +
 140                           "onthrow=java.lang.ClassNotFoundException,suspend=n",
 141                           targetClass};
 142         NoLaunchOptionTest myTest = new NoLaunchOptionTest();
 143         String results [] = myTest.run(VMConnection.insertDebuggeeVMOptions(cmds));
 144         if ((results[RETSTAT].equals("1")) &&
 145             (results[STDERR].startsWith("ERROR:"))) {
 146             System.out.println("Test passed: status = 1 with warning messages " +
 147                                "is expected and normal for this test");
 148         } else {
 149             throw new Exception("Test failed: unspecified test failure");
 150         }
 151     }
 152 
 153 }