jdk/test/com/sun/jdi/OnThrowTest.java
author jjg
Thu, 22 May 2008 15:51:41 -0700
changeset 655 1ebc7ce89018
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6705945: com.sun.tools.javac.zip files do not have valid copyright Reviewed-by: mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
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       OnThrowTest.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 *   @bug        6263814
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *   @summary    Test for -agentlib::[onthrow,launch]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *   @author     Kelly O'Hair
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *  @run compile -g OnThrowTest.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *  @run compile -g OnThrowTarget.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *  @run compile -g VMConnection.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *  @run main/othervm OnThrowTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.FileWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.BufferedInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class OnThrowTest extends Object {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    /* Full pathname for file to be touched or created by the launch */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private String touchFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    OnThrowTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        /* Name of touch file */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        touchFile = System.getProperty("test.classes") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                    File.separator + "OnThrowLaunchTouchFile";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        /* Make sure it's gone when we start */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        File f = new File(touchFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        f.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        if ( f.exists() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            throw new Exception("Test failed: Cannot remove old touch file: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                  touchFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /* Used to see if touch file exists */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private boolean touchFileExists() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        File f = new File(touchFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        return f.exists();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Run an arbitrary command
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public void run(String[] cmdStrings) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        StringBuffer stdoutBuffer = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        StringBuffer stderrBuffer = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        String CR = System.getProperty("line.separator");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        int subprocessStatus = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        System.out.print(CR + "runCommand method about to execute: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        for (int iNdx = 0; iNdx < cmdStrings.length; iNdx++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            System.out.print(" ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            System.out.print(cmdStrings[iNdx]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        System.out.println(CR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            Process process = Runtime.getRuntime().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                exec(VMConnection.insertDebuggeeVMOptions(cmdStrings));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            int BUFFERSIZE = 4096;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
             * Gather up the output of the subprocess using non-blocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
             * reads so we can get both the subprocess stdout and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
             * subprocess stderr without overfilling any buffers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            BufferedInputStream is =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                new BufferedInputStream(process.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            int isLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            byte[] isBuf = new byte[BUFFERSIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            BufferedInputStream es =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                new BufferedInputStream(process.getErrorStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            int esLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            byte[] esBuf = new byte[BUFFERSIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                isLen = is.read(isBuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                if (isLen > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    stdoutBuffer.append(new String(isBuf, 0, isLen));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                esLen = es.read(esBuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                if (esLen > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    stderrBuffer.append(new String(esBuf, 0, esLen));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            } while ((isLen > -1) || (esLen > -1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                process.waitFor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                subprocessStatus = process.exitValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                process = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            } catch(java.lang.InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                System.err.println("InterruptedException: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                throw new Exception("Test failed: process interrupted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        } catch(IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            System.err.println("IO error: " + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            throw new Exception("Test failed: IO error running process");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        System.out.println(CR + "--- Return code was: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                           CR + Integer.toString(subprocessStatus));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        System.out.println(CR + "--- Return stdout was: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                           CR + stdoutBuffer.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        System.out.println(CR + "--- Return stderr was: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                           CR + stderrBuffer.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        OnThrowTest myTest = new OnThrowTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        String launch = System.getProperty("test.classes") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                        File.separator + "OnThrowLaunch.sh";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        File f = new File(launch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        f.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        FileWriter fw = new FileWriter(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        fw.write("#!/bin/sh\n echo OK $* > " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                 myTest.touchFile.replace('\\','/') + "\n exit 0\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        fw.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        fw.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if ( ! f.exists() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            throw new Exception("Test failed: sh file not created: " + launch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        String javaExe = System.getProperty("java.home") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                         File.separator + "bin" + File.separator + "java";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        String targetClass = "OnThrowTarget";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        String cmds [] = {javaExe,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                          "-agentlib:jdwp=transport=dt_socket," +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                          "onthrow=OnThrowException,server=y,suspend=n," +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                          "launch=" + "sh " + launch.replace('\\','/'),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                          targetClass};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        /* Run the target app, which will launch the launch script */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        myTest.run(cmds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if ( !myTest.touchFileExists() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            throw new Exception("Test failed: touch file not found: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                  myTest.touchFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        System.out.println("Test passed: launch create file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
}