jdk/test/com/sun/jdi/NoLaunchOptionTest.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 4514 7758bdd54629
child 20764 ad561abdaf7b
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4514
diff changeset
     2
 * Copyright (c) 2002, 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4514
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4514
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4514
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 *   @test       NoLaunchOptionTest.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 *   @bug        4554734 4724714
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *   @summary    Test for -Xrunjdwp:[onthrow,onuncaught] suboptions require launch suboption
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *   @author     Tim Bell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *  @run compile -g NoLaunchOptionTest.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *  @build VMConnection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *  @run main/othervm NoLaunchOptionTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
4514
7758bdd54629 6906210: Fix another minor typo in test/Makefile
ohair
parents: 2
diff changeset
    34
7758bdd54629 6906210: Fix another minor typo in test/Makefile
ohair
parents: 2
diff changeset
    35
import java.net.ServerSocket;
7758bdd54629 6906210: Fix another minor typo in test/Makefile
ohair
parents: 2
diff changeset
    36
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class NoLaunchOptionTest extends Object {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private Process subprocess;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private int subprocessStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private static final String CR = System.getProperty("line.separator");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static final int BUFFERSIZE = 4096;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    public static final int RETSTAT = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    public static final int STDOUT = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public static final int STDERR = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * Run an arbitrary command and return the results to caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * @param an array of String containing the command
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     *        to run and any flags or parameters to the command.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * @return completion status, stderr and stdout as array of String
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     *  Look for:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     *    return status in result[NoLaunchOptionTest.RETSTAT]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     *    standard out in result[NoLaunchOptionTest.STDOUT]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     *    standard err in result[NoLaunchOptionTest.STDERR]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public String[] run (String[] cmdStrings) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        StringBuffer stdoutBuffer = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        StringBuffer stderrBuffer = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        System.out.print(CR + "runCommand method about to execute: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        for (int iNdx = 0; iNdx < cmdStrings.length; iNdx++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            System.out.print(" ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            System.out.print(cmdStrings[iNdx]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        System.out.println(CR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            Process process = Runtime.getRuntime().exec(cmdStrings);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
             * Gather up the output of the subprocess using non-blocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
             * reads so we can get both the subprocess stdout and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
             * subprocess stderr without overfilling any buffers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            java.io.BufferedInputStream is =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                new java.io.BufferedInputStream(process.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            int isLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            byte[] isBuf = new byte[BUFFERSIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            java.io.BufferedInputStream es =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                new java.io.BufferedInputStream(process.getErrorStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            int esLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            byte[] esBuf = new byte[BUFFERSIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                isLen = is.read(isBuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                if (isLen > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                    stdoutBuffer.append(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                                        new String(isBuf, 0, isLen));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                esLen = es.read(esBuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                if (esLen > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    stderrBuffer.append(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                                        new String(esBuf, 0, esLen));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            } while ((isLen > -1) || (esLen > -1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                process.waitFor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                subprocessStatus = process.exitValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                process = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            } catch(java.lang.InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                System.err.println("InterruptedException: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        } catch(java.io.IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            System.err.println("IO error: " + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        String[] result =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                Integer.toString(subprocessStatus),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                stdoutBuffer.toString(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                stderrBuffer.toString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        System.out.println(CR + "--- Return code was: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                           CR + result[RETSTAT]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        System.out.println(CR + "--- Return stdout was: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                           CR + result[STDOUT]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        System.out.println(CR + "--- Return stderr was: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                           CR + result[STDERR]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public static void main(String[] args) throws Exception {
4514
7758bdd54629 6906210: Fix another minor typo in test/Makefile
ohair
parents: 2
diff changeset
   127
        // find a free port
7758bdd54629 6906210: Fix another minor typo in test/Makefile
ohair
parents: 2
diff changeset
   128
        ServerSocket ss = new ServerSocket(0);
7758bdd54629 6906210: Fix another minor typo in test/Makefile
ohair
parents: 2
diff changeset
   129
        int port = ss.getLocalPort();
7758bdd54629 6906210: Fix another minor typo in test/Makefile
ohair
parents: 2
diff changeset
   130
        ss.close();
7758bdd54629 6906210: Fix another minor typo in test/Makefile
ohair
parents: 2
diff changeset
   131
        String address = String.valueOf(port);
7758bdd54629 6906210: Fix another minor typo in test/Makefile
ohair
parents: 2
diff changeset
   132
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        String javaExe = System.getProperty("java.home") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            java.io.File.separator + "bin" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            java.io.File.separator + "java";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        String targetClass = "NotAClass";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        String cmds [] = {javaExe,
4514
7758bdd54629 6906210: Fix another minor typo in test/Makefile
ohair
parents: 2
diff changeset
   138
                          "-agentlib:jdwp=transport=dt_socket,address=" +
7758bdd54629 6906210: Fix another minor typo in test/Makefile
ohair
parents: 2
diff changeset
   139
                          address + "," +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                          "onthrow=java.lang.ClassNotFoundException,suspend=n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                          targetClass};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        NoLaunchOptionTest myTest = new NoLaunchOptionTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        String results [] = myTest.run(VMConnection.insertDebuggeeVMOptions(cmds));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if ((results[RETSTAT].equals("1")) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            (results[STDERR].startsWith("ERROR:"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            System.out.println("Test passed: status = 1 with warning messages " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                               "is expected and normal for this test");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            throw new Exception("Test failed: unspecified test failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
}