0N/A/*
2362N/A * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test OnThrowTest.java
0N/A * @bug 6263814
0N/A * @summary Test for -agentlib::[onthrow,launch]
0N/A * @author Kelly O'Hair
0N/A *
0N/A * @run compile -g OnThrowTest.java
0N/A * @run compile -g OnThrowTarget.java
0N/A * @run compile -g VMConnection.java
0N/A * @run main/othervm OnThrowTest
0N/A */
0N/A
0N/Aimport java.io.File;
0N/Aimport java.io.FileWriter;
0N/Aimport java.io.BufferedInputStream;
0N/Aimport java.io.IOException;
0N/A
0N/Apublic class OnThrowTest extends Object {
0N/A
0N/A /* Full pathname for file to be touched or created by the launch */
0N/A private String touchFile;
0N/A
0N/A OnThrowTest() throws Exception {
0N/A /* Name of touch file */
0N/A touchFile = System.getProperty("test.classes") +
0N/A File.separator + "OnThrowLaunchTouchFile";
0N/A /* Make sure it's gone when we start */
0N/A File f = new File(touchFile);
0N/A f.delete();
0N/A if ( f.exists() ) {
0N/A throw new Exception("Test failed: Cannot remove old touch file: " +
0N/A touchFile);
0N/A }
0N/A }
0N/A
0N/A /* Used to see if touch file exists */
0N/A private boolean touchFileExists() {
0N/A File f = new File(touchFile);
0N/A return f.exists();
0N/A }
0N/A
0N/A /**
0N/A * Run an arbitrary command
0N/A *
0N/A */
0N/A public void run(String[] cmdStrings) throws Exception {
0N/A StringBuffer stdoutBuffer = new StringBuffer();
0N/A StringBuffer stderrBuffer = new StringBuffer();
0N/A String CR = System.getProperty("line.separator");
0N/A int subprocessStatus = 1;
0N/A
0N/A System.out.print(CR + "runCommand method about to execute: ");
0N/A for (int iNdx = 0; iNdx < cmdStrings.length; iNdx++) {
0N/A System.out.print(" ");
0N/A System.out.print(cmdStrings[iNdx]);
0N/A }
0N/A System.out.println(CR);
0N/A
0N/A try {
0N/A Process process = Runtime.getRuntime().
0N/A exec(VMConnection.insertDebuggeeVMOptions(cmdStrings));
0N/A int BUFFERSIZE = 4096;
0N/A /*
0N/A * Gather up the output of the subprocess using non-blocking
0N/A * reads so we can get both the subprocess stdout and the
0N/A * subprocess stderr without overfilling any buffers.
0N/A */
0N/A BufferedInputStream is =
0N/A new BufferedInputStream(process.getInputStream());
0N/A int isLen = 0;
0N/A byte[] isBuf = new byte[BUFFERSIZE];
0N/A
0N/A BufferedInputStream es =
0N/A new BufferedInputStream(process.getErrorStream());
0N/A int esLen = 0;
0N/A byte[] esBuf = new byte[BUFFERSIZE];
0N/A
0N/A do {
0N/A isLen = is.read(isBuf);
0N/A if (isLen > 0) {
0N/A stdoutBuffer.append(new String(isBuf, 0, isLen));
0N/A }
0N/A esLen = es.read(esBuf);
0N/A if (esLen > 0) {
0N/A stderrBuffer.append(new String(esBuf, 0, esLen));
0N/A }
0N/A } while ((isLen > -1) || (esLen > -1));
0N/A
0N/A try {
0N/A process.waitFor();
0N/A subprocessStatus = process.exitValue();
0N/A process = null;
0N/A } catch(java.lang.InterruptedException e) {
0N/A System.err.println("InterruptedException: " + e);
0N/A throw new Exception("Test failed: process interrupted");
0N/A }
0N/A
0N/A } catch(IOException ex) {
0N/A System.err.println("IO error: " + ex);
0N/A throw new Exception("Test failed: IO error running process");
0N/A }
0N/A
0N/A System.out.println(CR + "--- Return code was: " +
0N/A CR + Integer.toString(subprocessStatus));
0N/A System.out.println(CR + "--- Return stdout was: " +
0N/A CR + stdoutBuffer.toString());
0N/A System.out.println(CR + "--- Return stderr was: " +
0N/A CR + stderrBuffer.toString());
0N/A
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A
0N/A OnThrowTest myTest = new OnThrowTest();
0N/A
0N/A String launch = System.getProperty("test.classes") +
0N/A File.separator + "OnThrowLaunch.sh";
0N/A File f = new File(launch);
0N/A f.delete();
0N/A FileWriter fw = new FileWriter(f);
0N/A fw.write("#!/bin/sh\n echo OK $* > " +
0N/A myTest.touchFile.replace('\\','/') + "\n exit 0\n");
0N/A fw.flush();
0N/A fw.close();
0N/A if ( ! f.exists() ) {
0N/A throw new Exception("Test failed: sh file not created: " + launch);
0N/A }
0N/A
0N/A String javaExe = System.getProperty("java.home") +
0N/A File.separator + "bin" + File.separator + "java";
0N/A String targetClass = "OnThrowTarget";
0N/A String cmds [] = {javaExe,
0N/A "-agentlib:jdwp=transport=dt_socket," +
0N/A "onthrow=OnThrowException,server=y,suspend=n," +
0N/A "launch=" + "sh " + launch.replace('\\','/'),
0N/A targetClass};
0N/A
0N/A /* Run the target app, which will launch the launch script */
0N/A myTest.run(cmds);
0N/A if ( !myTest.touchFileExists() ) {
0N/A throw new Exception("Test failed: touch file not found: " +
0N/A myTest.touchFile);
0N/A }
0N/A
0N/A System.out.println("Test passed: launch create file");
0N/A }
0N/A
0N/A}