jdk/test/com/sun/jdi/OptionTest.java
changeset 2 90ce3da70b43
child 4514 7758bdd54629
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 2004-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       OptionTest
       
    26  *   @bug        5095072
       
    27  *   @summary    Test for misc jdwp options, just that the option is parsed
       
    28  *   @author     Kelly O'Hair (copied from Tim Bell's NoLaunchOptionTest)
       
    29  *
       
    30  *  @run compile -g OptionTest.java
       
    31  *  @run compile -g HelloWorld.java
       
    32  *  @run compile -g VMConnection.java
       
    33  *  @run main/othervm OptionTest
       
    34  */
       
    35 public class OptionTest extends Object {
       
    36     private Process subprocess;
       
    37     private int subprocessStatus;
       
    38     private static final String CR = System.getProperty("line.separator");
       
    39     private static final int BUFFERSIZE = 4096;
       
    40     public static final int RETSTAT = 0;
       
    41     public static final int STDOUT = 1;
       
    42     public static final int STDERR = 2;
       
    43 
       
    44     /**
       
    45      * Run an arbitrary command and return the results to caller.
       
    46      *
       
    47      * @param an array of String containing the command
       
    48      *        to run and any flags or parameters to the command.
       
    49      *
       
    50      * @return completion status, stderr and stdout as array of String
       
    51      *  Look for:
       
    52      *    return status in result[OptionTest.RETSTAT]
       
    53      *    standard out in result[OptionTest.STDOUT]
       
    54      *    standard err in result[OptionTest.STDERR]
       
    55      *
       
    56      */
       
    57     public String[] run (String[] cmdStrings) {
       
    58         StringBuffer stdoutBuffer = new StringBuffer();
       
    59         StringBuffer stderrBuffer = new StringBuffer();
       
    60 
       
    61         System.out.print(CR + "runCommand method about to execute: ");
       
    62         for (int iNdx = 0; iNdx < cmdStrings.length; iNdx++) {
       
    63             System.out.print(" ");
       
    64             System.out.print(cmdStrings[iNdx]);
       
    65         }
       
    66         System.out.println(CR);
       
    67         try {
       
    68             Process process = Runtime.getRuntime().exec(cmdStrings);
       
    69             /*
       
    70              * Gather up the output of the subprocess using non-blocking
       
    71              * reads so we can get both the subprocess stdout and the
       
    72              * subprocess stderr without overfilling any buffers.
       
    73              */
       
    74             java.io.BufferedInputStream is =
       
    75                 new java.io.BufferedInputStream(process.getInputStream());
       
    76             int isLen = 0;
       
    77             byte[] isBuf = new byte[BUFFERSIZE];
       
    78 
       
    79             java.io.BufferedInputStream es =
       
    80                 new java.io.BufferedInputStream(process.getErrorStream());
       
    81             int esLen = 0;
       
    82             byte[] esBuf = new byte[BUFFERSIZE];
       
    83 
       
    84             do {
       
    85                 isLen = is.read(isBuf);
       
    86                 if (isLen > 0) {
       
    87                     stdoutBuffer.append(
       
    88                                         new String(isBuf, 0, isLen));
       
    89                 }
       
    90                 esLen = es.read(esBuf);
       
    91                 if (esLen > 0) {
       
    92                     stderrBuffer.append(
       
    93                                         new String(esBuf, 0, esLen));
       
    94                 }
       
    95             } while ((isLen > -1) || (esLen > -1));
       
    96             try {
       
    97                 process.waitFor();
       
    98                 subprocessStatus = process.exitValue();
       
    99                 process = null;
       
   100             } catch(java.lang.InterruptedException e) {
       
   101                 System.err.println("InterruptedException: " + e);
       
   102             }
       
   103 
       
   104         } catch(java.io.IOException ex) {
       
   105             System.err.println("IO error: " + ex);
       
   106         }
       
   107         String[] result =
       
   108             new String[] {
       
   109                 Integer.toString(subprocessStatus),
       
   110                 stdoutBuffer.toString(),
       
   111                 stderrBuffer.toString()
       
   112         };
       
   113 
       
   114         System.out.println(CR + "--- Return code was: " +
       
   115                            CR + result[RETSTAT]);
       
   116         System.out.println(CR + "--- Return stdout was: " +
       
   117                            CR + result[STDOUT]);
       
   118         System.out.println(CR + "--- Return stderr was: " +
       
   119                            CR + result[STDERR]);
       
   120 
       
   121         return result;
       
   122     }
       
   123 
       
   124     public static void main(String[] args) throws Exception {
       
   125         String javaExe = System.getProperty("java.home") +
       
   126             java.io.File.separator + "bin" +
       
   127             java.io.File.separator + "java";
       
   128         String targetClass = "HelloWorld";
       
   129         String baseOptions = "transport=dt_socket" +
       
   130                               ",address=8000" +
       
   131                               ",server=y" +
       
   132                               ",suspend=n";
       
   133 
       
   134         /* Option combinations to try (combos faster, fewer exec's) */
       
   135         String options[] =  {
       
   136                 "timeout=0,mutf8=y,quiet=y,stdalloc=y,strict=n",
       
   137                 "timeout=200000,mutf8=n,quiet=n,stdalloc=n,strict=y"
       
   138                 };
       
   139 
       
   140         for ( String option : options) {
       
   141             String cmds [] = {javaExe,
       
   142                               "-agentlib:jdwp=" + baseOptions + "," + option,
       
   143                               targetClass};
       
   144             OptionTest myTest = new OptionTest();
       
   145             String results [] = myTest.run(VMConnection.insertDebuggeeVMOptions(cmds));
       
   146             if (!(results[RETSTAT].equals("0")) ||
       
   147                 (results[STDERR].startsWith("ERROR:"))) {
       
   148                 throw new Exception("Test failed: jdwp doesn't like " + cmds[1]);
       
   149             }
       
   150         }
       
   151         System.out.println("Test passed: status = 0");
       
   152     }
       
   153 }