diff --git a/jdk/test/ProblemList.txt b/jdk/test/ProblemList.txt index e87acf9134a..4ad08ff08bd 100644 --- a/jdk/test/ProblemList.txt +++ b/jdk/test/ProblemList.txt @@ -134,8 +134,6 @@ java/lang/instrument/NativeMethodPrefixAgent.java generic-all # 8044591 com/sun/management/GarbageCollectorMXBean/GarbageCollectionNotificationContentTest.java generic-all com/sun/management/GarbageCollectorMXBean/GarbageCollectionNotificationTest.java generic-all -# 8056143 -java/lang/management/MemoryMXBean/LowMemoryTest.java generic-all # 8058492 java/lang/management/ThreadMXBean/FindDeadlocks.java generic-all @@ -308,7 +306,7 @@ sun/tools/jstatd/TestJstatdServer.java generic-all sun/tools/jstatd/TestJstatdPort.java generic-all sun/tools/jstatd/TestJstatdPortAndServer.java generic-all -# 8046285 8027668 +# 8046285 8027668 sun/tools/jstatd/TestJstatdExternalRegistry.java generic-all # 6456333 diff --git a/jdk/test/java/lang/instrument/DaemonThread/TestDaemonThreadLauncher.java b/jdk/test/java/lang/instrument/DaemonThread/TestDaemonThreadLauncher.java index 9660bbfa9dc..419cfb3bbbd 100644 --- a/jdk/test/java/lang/instrument/DaemonThread/TestDaemonThreadLauncher.java +++ b/jdk/test/java/lang/instrument/DaemonThread/TestDaemonThreadLauncher.java @@ -23,21 +23,14 @@ */ -import jdk.testlibrary.JDKToolLauncher; import jdk.testlibrary.OutputAnalyzer; import jdk.testlibrary.ProcessTools; -import java.io.IOException; -import java.nio.file.Path; - public class TestDaemonThreadLauncher { - - private static ProcessBuilder processBuilder = new ProcessBuilder(); - public static void main(String args[]) throws Exception { for(int i=0; i<50; i++) { ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-javaagent:DummyAgent.jar", "TestDaemonThread", "."); - OutputAnalyzer analyzer = new OutputAnalyzer(pb.start()); + OutputAnalyzer analyzer = ProcessTools.executeProcess(pb); analyzer.shouldNotContain("ASSERTION FAILED"); } } diff --git a/jdk/test/java/lang/instrument/PremainClass/NoPremainAgentTest.java b/jdk/test/java/lang/instrument/PremainClass/NoPremainAgentTest.java index d9710d3bece..ac01286cd71 100644 --- a/jdk/test/java/lang/instrument/PremainClass/NoPremainAgentTest.java +++ b/jdk/test/java/lang/instrument/PremainClass/NoPremainAgentTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -46,7 +46,7 @@ public class NoPremainAgentTest { Utils.addTestJavaOpts(testArgs.split("\\s+"))); System.out.println("testjvm.cmd:" + Utils.getCommandLine(pb)); - OutputAnalyzer output = new OutputAnalyzer(pb.start()); + OutputAnalyzer output = ProcessTools.executeProcess(pb); System.out.println("testjvm.stdout:" + output.getStdout()); System.out.println("testjvm.stderr:" + output.getStderr()); diff --git a/jdk/test/java/lang/instrument/PremainClass/PremainClassTest.java b/jdk/test/java/lang/instrument/PremainClass/PremainClassTest.java index 2d20ae98dc0..9c25afa5dae 100644 --- a/jdk/test/java/lang/instrument/PremainClass/PremainClassTest.java +++ b/jdk/test/java/lang/instrument/PremainClass/PremainClassTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -47,7 +47,7 @@ public class PremainClassTest { Utils.addTestJavaOpts(testArgs.split("\\s+"))); System.out.println("testjvm.cmd:" + Utils.getCommandLine(pb)); - OutputAnalyzer output = new OutputAnalyzer(pb.start()); + OutputAnalyzer output = ProcessTools.executeProcess(pb); System.out.println("testjvm.stdout:" + output.getStdout()); System.out.println("testjvm.stderr:" + output.getStderr()); diff --git a/jdk/test/java/lang/instrument/PremainClass/ZeroArgPremainAgentTest.java b/jdk/test/java/lang/instrument/PremainClass/ZeroArgPremainAgentTest.java index fb6fab1a677..653f8331e51 100644 --- a/jdk/test/java/lang/instrument/PremainClass/ZeroArgPremainAgentTest.java +++ b/jdk/test/java/lang/instrument/PremainClass/ZeroArgPremainAgentTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -46,7 +46,7 @@ public class ZeroArgPremainAgentTest { Utils.addTestJavaOpts(testArgs.split("\\s+"))); System.out.println("testjvm.cmd:" + Utils.getCommandLine(pb)); - OutputAnalyzer output = new OutputAnalyzer(pb.start()); + OutputAnalyzer output = ProcessTools.executeProcess(pb); System.out.println("testjvm.stdout:" + output.getStdout()); System.out.println("testjvm.stderr:" + output.getStderr()); diff --git a/jdk/test/lib/testlibrary/OutputAnalyzerReportingTest.java b/jdk/test/lib/testlibrary/OutputAnalyzerReportingTest.java index daf669e620c..f05229c9504 100644 --- a/jdk/test/lib/testlibrary/OutputAnalyzerReportingTest.java +++ b/jdk/test/lib/testlibrary/OutputAnalyzerReportingTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -21,6 +21,7 @@ * questions. */ +package jdk.testlibrary; /* * @test @@ -28,13 +29,13 @@ * such as printing additional diagnostic info * (exit code, stdout, stderr, command line, etc.) * @library /testlibrary + * @build jdk.testlibrary.* + * @run main jdk.testlibrary.OutputAnalyzerReportingTest */ import java.io.ByteArrayOutputStream; import java.io.PrintStream; -import jdk.testlibrary.OutputAnalyzer; - public class OutputAnalyzerReportingTest { public static void main(String[] args) throws Exception { diff --git a/jdk/test/lib/testlibrary/OutputAnalyzerTest.java b/jdk/test/lib/testlibrary/OutputAnalyzerTest.java index f8ad49e7a43..af2b8f6b14a 100644 --- a/jdk/test/lib/testlibrary/OutputAnalyzerTest.java +++ b/jdk/test/lib/testlibrary/OutputAnalyzerTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -20,15 +20,16 @@ * or visit www.oracle.com if you need additional information or have any * questions. */ +package jdk.testlibrary; /* * @test * @summary Test the OutputAnalyzer utility class * @library /testlibrary + * @build jdk.testlibrary.* + * @run main jdk.testlibrary.OutputAnalyzerTest */ -import jdk.testlibrary.OutputAnalyzer; - public class OutputAnalyzerTest { public static void main(String args[]) throws Exception { diff --git a/jdk/test/lib/testlibrary/jdk/testlibrary/JcmdBase.java b/jdk/test/lib/testlibrary/jdk/testlibrary/JcmdBase.java index f8a212e2bdb..c4bbedccfc0 100644 --- a/jdk/test/lib/testlibrary/jdk/testlibrary/JcmdBase.java +++ b/jdk/test/lib/testlibrary/jdk/testlibrary/JcmdBase.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -108,7 +108,7 @@ public final class JcmdBase { } processBuilder.command(launcher.getCommand()); System.out.println(Arrays.toString(processBuilder.command().toArray()).replace(",", "")); - OutputAnalyzer output = new OutputAnalyzer(processBuilder.start()); + OutputAnalyzer output = ProcessTools.executeProcess(processBuilder); System.out.println(output.getOutput()); return output; diff --git a/jdk/test/lib/testlibrary/jdk/testlibrary/OutputAnalyzer.java b/jdk/test/lib/testlibrary/jdk/testlibrary/OutputAnalyzer.java index 602df9f0143..ad1d2ab8ea3 100644 --- a/jdk/test/lib/testlibrary/jdk/testlibrary/OutputAnalyzer.java +++ b/jdk/test/lib/testlibrary/jdk/testlibrary/OutputAnalyzer.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -25,9 +25,13 @@ package jdk.testlibrary; import static jdk.testlibrary.Asserts.*; +import java.io.ByteArrayOutputStream; + import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -35,34 +39,37 @@ import java.util.regex.Pattern; * Utility class for verifying output and exit value from a {@code Process}. */ public final class OutputAnalyzer { - + private final OutputBuffer output; private final String stdout; private final String stderr; private final int exitValue; /** * Create an OutputAnalyzer, a utility class for verifying output and exit - * value from a Process + * value from a Process. + *

+ * OutputAnalyzer should never be instantiated directly - + * use {@linkplain ProcessTools#executeProcess(p)} instead * * @param process * Process to analyze * @throws IOException * If an I/O error occurs. */ - public OutputAnalyzer(Process process) throws IOException { - OutputBuffer output = ProcessTools.getOutput(process); - exitValue = process.exitValue(); - this.stdout = output.getStdout(); - this.stderr = output.getStderr(); + OutputAnalyzer(Process process) throws IOException { + output = new OutputBuffer(process); + exitValue = -1; + this.stdout = null; + this.stderr = null; } /** - * Create an OutputAnalyzer, a utility class for verifying output + * Create an OutputAnalyzer, a utility class for verifying output. * * @param buf * String buffer to analyze */ - public OutputAnalyzer(String buf) { + OutputAnalyzer(String buf) { this(buf, buf); } @@ -74,7 +81,8 @@ public final class OutputAnalyzer { * @param stderr * stderr buffer to analyze */ - public OutputAnalyzer(String stdout, String stderr) { + OutputAnalyzer(String stdout, String stderr) { + this.output = null; this.stdout = stdout; this.stderr = stderr; exitValue = -1; @@ -90,8 +98,8 @@ public final class OutputAnalyzer { * If the string was not found */ public void shouldContain(String expectedString) { - if (!stdout.contains(expectedString) - && !stderr.contains(expectedString)) { + if (!getStdout().contains(expectedString) + && !getStderr().contains(expectedString)) { reportDiagnosticSummary(); throw new RuntimeException("'" + expectedString + "' missing from stdout/stderr \n"); @@ -107,7 +115,7 @@ public final class OutputAnalyzer { * If the string was not found */ public void stdoutShouldContain(String expectedString) { - if (!stdout.contains(expectedString)) { + if (!getStdout().contains(expectedString)) { reportDiagnosticSummary(); throw new RuntimeException("'" + expectedString + "' missing from stdout \n"); @@ -123,7 +131,7 @@ public final class OutputAnalyzer { * If the string was not found */ public void stderrShouldContain(String expectedString) { - if (!stderr.contains(expectedString)) { + if (!getStderr().contains(expectedString)) { reportDiagnosticSummary(); throw new RuntimeException("'" + expectedString + "' missing from stderr \n"); @@ -140,12 +148,12 @@ public final class OutputAnalyzer { * If the string was found */ public void shouldNotContain(String notExpectedString) { - if (stdout.contains(notExpectedString)) { + if (getStdout().contains(notExpectedString)) { reportDiagnosticSummary(); throw new RuntimeException("'" + notExpectedString + "' found in stdout \n"); } - if (stderr.contains(notExpectedString)) { + if (getStderr().contains(notExpectedString)) { reportDiagnosticSummary(); throw new RuntimeException("'" + notExpectedString + "' found in stderr \n"); @@ -162,7 +170,7 @@ public final class OutputAnalyzer { * If the string was found */ public void stdoutShouldNotContain(String notExpectedString) { - if (stdout.contains(notExpectedString)) { + if (getStdout().contains(notExpectedString)) { reportDiagnosticSummary(); throw new RuntimeException("'" + notExpectedString + "' found in stdout \n"); @@ -179,7 +187,7 @@ public final class OutputAnalyzer { * If the string was found */ public void stderrShouldNotContain(String notExpectedString) { - if (stderr.contains(notExpectedString)) { + if (getStderr().contains(notExpectedString)) { reportDiagnosticSummary(); throw new RuntimeException("'" + notExpectedString + "' found in stderr \n"); @@ -196,9 +204,9 @@ public final class OutputAnalyzer { */ public void shouldMatch(String pattern) { Matcher stdoutMatcher = Pattern.compile(pattern, Pattern.MULTILINE) - .matcher(stdout); + .matcher(getStdout()); Matcher stderrMatcher = Pattern.compile(pattern, Pattern.MULTILINE) - .matcher(stderr); + .matcher(getStderr()); if (!stdoutMatcher.find() && !stderrMatcher.find()) { reportDiagnosticSummary(); throw new RuntimeException("'" + pattern @@ -215,7 +223,7 @@ public final class OutputAnalyzer { */ public void stdoutShouldMatch(String pattern) { Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher( - stdout); + getStdout()); if (!matcher.find()) { reportDiagnosticSummary(); throw new RuntimeException("'" + pattern @@ -232,7 +240,7 @@ public final class OutputAnalyzer { */ public void stderrShouldMatch(String pattern) { Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher( - stderr); + getStderr()); if (!matcher.find()) { reportDiagnosticSummary(); throw new RuntimeException("'" + pattern @@ -250,13 +258,13 @@ public final class OutputAnalyzer { */ public void shouldNotMatch(String pattern) { Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher( - stdout); + getStdout()); if (matcher.find()) { reportDiagnosticSummary(); throw new RuntimeException("'" + pattern + "' found in stdout: '" + matcher.group() + "' \n"); } - matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(stderr); + matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(getStderr()); if (matcher.find()) { reportDiagnosticSummary(); throw new RuntimeException("'" + pattern + "' found in stderr: '" @@ -274,7 +282,7 @@ public final class OutputAnalyzer { */ public void stdoutShouldNotMatch(String pattern) { Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher( - stdout); + getStdout()); if (matcher.find()) { reportDiagnosticSummary(); throw new RuntimeException("'" + pattern + "' found in stdout \n"); @@ -291,7 +299,7 @@ public final class OutputAnalyzer { */ public void stderrShouldNotMatch(String pattern) { Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher( - stderr); + getStderr()); if (matcher.find()) { reportDiagnosticSummary(); throw new RuntimeException("'" + pattern + "' found in stderr \n"); @@ -310,9 +318,9 @@ public final class OutputAnalyzer { */ public String firstMatch(String pattern, int group) { Matcher stderrMatcher = Pattern.compile(pattern, Pattern.MULTILINE) - .matcher(stderr); + .matcher(getStderr()); Matcher stdoutMatcher = Pattern.compile(pattern, Pattern.MULTILINE) - .matcher(stdout); + .matcher(getStdout()); if (stderrMatcher.find()) { return stderrMatcher.group(group); } @@ -357,7 +365,7 @@ public final class OutputAnalyzer { * exit code Note: the command line is printed by the ProcessTools */ private void reportDiagnosticSummary() { - String msg = " stdout: [" + stdout + "];\n" + " stderr: [" + stderr + String msg = " stdout: [" + getStdout() + "];\n" + " stderr: [" + getStderr() + "]\n" + " exitValue = " + getExitValue() + "\n"; System.err.println(msg); @@ -369,7 +377,7 @@ public final class OutputAnalyzer { * @return Content of the output buffer */ public String getOutput() { - return stdout + stderr; + return getStdout() + getStderr(); } /** @@ -378,7 +386,7 @@ public final class OutputAnalyzer { * @return Content of the stdout buffer */ public String getStdout() { - return stdout; + return output == null ? stdout : output.getStdout(); } /** @@ -387,7 +395,7 @@ public final class OutputAnalyzer { * @return Content of the stderr buffer */ public String getStderr() { - return stderr; + return output == null ? stderr : output.getStderr(); } /** @@ -396,7 +404,7 @@ public final class OutputAnalyzer { * @return Process exit value */ public int getExitValue() { - return exitValue; + return output == null ? exitValue : output.getExitValue(); } /** @@ -492,5 +500,4 @@ public final class OutputAnalyzer { return matchedCount; } - } diff --git a/jdk/test/lib/testlibrary/jdk/testlibrary/OutputBuffer.java b/jdk/test/lib/testlibrary/jdk/testlibrary/OutputBuffer.java index b5e021e8a9d..c104e81207e 100644 --- a/jdk/test/lib/testlibrary/jdk/testlibrary/OutputBuffer.java +++ b/jdk/test/lib/testlibrary/jdk/testlibrary/OutputBuffer.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -23,9 +23,23 @@ package jdk.testlibrary; -public class OutputBuffer { - private final String stdout; - private final String stderr; +import java.io.ByteArrayOutputStream; +import java.util.concurrent.CancellationException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; + +class OutputBuffer { + private static class OutputBufferException extends RuntimeException { + public OutputBufferException(Throwable cause) { + super(cause); + } + } + + private final Process p; + private final Future outTask; + private final Future errTask; + private final ByteArrayOutputStream stderrBuffer = new ByteArrayOutputStream(); + private final ByteArrayOutputStream stdoutBuffer = new ByteArrayOutputStream(); /** * Create an OutputBuffer, a class for storing and managing stdout and @@ -36,9 +50,15 @@ public class OutputBuffer { * @param stderr * stderr result */ - public OutputBuffer(String stdout, String stderr) { - this.stdout = stdout; - this.stderr = stderr; + OutputBuffer(Process p) { + this.p = p; + StreamPumper outPumper = new StreamPumper(p.getInputStream(), + stdoutBuffer); + StreamPumper errPumper = new StreamPumper(p.getErrorStream(), + stderrBuffer); + + outTask = outPumper.process(); + errTask = errPumper.process(); } /** @@ -47,7 +67,15 @@ public class OutputBuffer { * @return stdout result */ public String getStdout() { - return stdout; + try { + outTask.get(); + return stdoutBuffer.toString(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new OutputBufferException(e); + } catch (ExecutionException | CancellationException e) { + throw new OutputBufferException(e); + } } /** @@ -56,6 +84,23 @@ public class OutputBuffer { * @return stderr result */ public String getStderr() { - return stderr; + try { + errTask.get(); + return stderrBuffer.toString(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new OutputBufferException(e); + } catch (ExecutionException | CancellationException e) { + throw new OutputBufferException(e); + } + } + + public int getExitValue() { + try { + return p.waitFor(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new OutputBufferException(e); + } } } diff --git a/jdk/test/lib/testlibrary/jdk/testlibrary/ProcessThread.java b/jdk/test/lib/testlibrary/jdk/testlibrary/ProcessThread.java index 0aee786e7b1..9cd49ada770 100644 --- a/jdk/test/lib/testlibrary/jdk/testlibrary/ProcessThread.java +++ b/jdk/test/lib/testlibrary/jdk/testlibrary/ProcessThread.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -154,12 +154,14 @@ public class ProcessThread extends TestThread { // Will block... try { + this.process.waitFor(); output = new OutputAnalyzer(this.process); } catch (Throwable t) { String name = Thread.currentThread().getName(); System.out.println(String.format("ProcessThread[%s] failed: %s", name, t.toString())); throw t; } finally { + this.process.destroyForcibly().waitFor(); String logMsg = ProcessTools.getProcessLog(processBuilder, output); System.out.println(logMsg); } diff --git a/jdk/test/lib/testlibrary/jdk/testlibrary/ProcessTools.java b/jdk/test/lib/testlibrary/jdk/testlibrary/ProcessTools.java index 8935d967f5d..4698a078b78 100644 --- a/jdk/test/lib/testlibrary/jdk/testlibrary/ProcessTools.java +++ b/jdk/test/lib/testlibrary/jdk/testlibrary/ProcessTools.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -23,7 +23,6 @@ package jdk.testlibrary; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; import java.lang.management.ManagementFactory; @@ -34,7 +33,6 @@ import java.util.ArrayList; import java.util.Collections; import java.util.concurrent.CountDownLatch; import java.util.Map; -import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; @@ -213,55 +211,6 @@ public final class ProcessTools { return startProcess(name, processBuilder, linePredicate, 0, TimeUnit.SECONDS); } - /** - * Pumps stdout and stderr from running the process into a String. - * - * @param processBuilder - * ProcessHandler to run. - * @return Output from process. - * @throws IOException - * If an I/O error occurs. - */ - public static OutputBuffer getOutput(ProcessBuilder processBuilder) - throws IOException { - return getOutput(processBuilder.start()); - } - - /** - * Pumps stdout and stderr the running process into a String. - * - * @param process - * Process to pump. - * @return Output from process. - * @throws IOException - * If an I/O error occurs. - */ - public static OutputBuffer getOutput(Process process) throws IOException { - ByteArrayOutputStream stderrBuffer = new ByteArrayOutputStream(); - ByteArrayOutputStream stdoutBuffer = new ByteArrayOutputStream(); - StreamPumper outPumper = new StreamPumper(process.getInputStream(), - stdoutBuffer); - StreamPumper errPumper = new StreamPumper(process.getErrorStream(), - stderrBuffer); - - Future outTask = outPumper.process(); - Future errTask = errPumper.process(); - - try { - process.waitFor(); - outTask.get(); - errTask.get(); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - return null; - } catch (ExecutionException e) { - throw new IOException(e); - } - - return new OutputBuffer(stdoutBuffer.toString(), - stderrBuffer.toString()); - } - /** * Get the process id of the current running Java process * @@ -343,34 +292,51 @@ public final class ProcessTools { * The command line will be like: * {test.jdk}/bin/java {test.vm.opts} {test.java.opts} cmds * + * The jvm process will have exited before this method returns. + * * @param cmds User specifed arguments. * @return The output from the process. */ - public static OutputAnalyzer executeTestJvm(String... cmds) throws Throwable { + public static OutputAnalyzer executeTestJvm(String... cmds) throws Exception { ProcessBuilder pb = createJavaProcessBuilder(Utils.addTestJavaOpts(cmds)); return executeProcess(pb); } /** * Executes a process, waits for it to finish and returns the process output. + * The process will have exited before this method returns. * @param pb The ProcessBuilder to execute. - * @return The output from the process. + * @return The {@linkplain OutputAnalyzer} instance wrapping the process. */ - public static OutputAnalyzer executeProcess(ProcessBuilder pb) throws Throwable { + public static OutputAnalyzer executeProcess(ProcessBuilder pb) throws Exception { OutputAnalyzer output = null; + Process p = null; + boolean failed = false; try { - output = new OutputAnalyzer(pb.start()); + p = pb.start(); + output = new OutputAnalyzer(p); + p.waitFor(); + return output; } catch (Throwable t) { + failed = true; System.out.println("executeProcess() failed: " + t); throw t; } finally { - System.out.println(getProcessLog(pb, output)); + if (p != null) { + p.destroyForcibly().waitFor(); + } + if (failed) { + System.err.println(getProcessLog(pb, output)); + } } } /** * Executes a process, waits for it to finish and returns the process output. + * + * The process will have exited before this method returns. + * * @param cmds The command line to execute. * @return The output from the process. */ @@ -394,6 +360,7 @@ public final class ProcessTools { logMsg.append("exitvalue: " + exitValue + nl); logMsg.append("stderr: " + stderr + nl); logMsg.append("stdout: " + stdout + nl); + return logMsg.toString(); } diff --git a/jdk/test/sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java b/jdk/test/sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java index 75082098890..b4f609bddeb 100644 --- a/jdk/test/sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java +++ b/jdk/test/sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java @@ -34,7 +34,7 @@ import java.util.Set; import java.util.UUID; import java.util.concurrent.Semaphore; -import jdk.testlibrary.OutputBuffer; +import jdk.testlibrary.OutputAnalyzer; import jdk.testlibrary.ProcessTools; import sun.jvmstat.monitor.MonitorException; import sun.jvmstat.monitor.MonitoredHost; @@ -305,13 +305,13 @@ public final class MonitorVmStartTerminate { Runtime.getRuntime().removeShutdownHook(shutdownHook); } - private void executeJava() throws Exception, IOException { + private void executeJava() throws Throwable { String className = JavaProcess.class.getName(); String classPath = System.getProperty("test.classes"); ProcessBuilder pb = ProcessTools.createJavaProcessBuilder( "-Dtest.timeout.factor=" + System.getProperty("test.timeout.factor", "1.0"), "-cp", classPath, className, mainArgsIdentifier); - OutputBuffer ob = ProcessTools.getOutput(pb.start()); + OutputAnalyzer ob = ProcessTools.executeProcess(pb); System.out.println("Java Process " + getMainArgsIdentifier() + " stderr:" + ob.getStderr()); System.err.println("Java Process " + getMainArgsIdentifier() + " stdout:" diff --git a/jdk/test/sun/management/jdp/DynamicLauncher.java b/jdk/test/sun/management/jdp/DynamicLauncher.java index e6e332058d7..b8e5710dbef 100644 --- a/jdk/test/sun/management/jdp/DynamicLauncher.java +++ b/jdk/test/sun/management/jdp/DynamicLauncher.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -63,7 +63,7 @@ public abstract class DynamicLauncher { protected OutputAnalyzer runVM() throws Exception { String[] options = this.options(); ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(options); - OutputAnalyzer out = new OutputAnalyzer(pb.start()); + OutputAnalyzer out = ProcessTools.executeProcess(pb); System.out.println(out.getStdout()); System.err.println(out.getStderr()); return out; diff --git a/jdk/test/sun/management/jmxremote/bootstrap/AbstractFilePermissionTest.java b/jdk/test/sun/management/jmxremote/bootstrap/AbstractFilePermissionTest.java index a2e0c892003..ac7a8d47504 100644 --- a/jdk/test/sun/management/jmxremote/bootstrap/AbstractFilePermissionTest.java +++ b/jdk/test/sun/management/jmxremote/bootstrap/AbstractFilePermissionTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -176,16 +176,15 @@ public abstract class AbstractFilePermissionTest { command.toArray(new String[command.size()])); System.out.println("test cmdline: " + Arrays.toString(processBuilder.command().toArray()).replace(",", "")); - Process p = processBuilder.start(); - OutputAnalyzer output = new OutputAnalyzer(p); + OutputAnalyzer output = ProcessTools.executeProcess(processBuilder); System.out.println("test output:"); System.out.println(output.getOutput()); - if ((p.exitValue() == 0) || + if ((output.getExitValue() == 0) || !output.getOutput().contains("Exception thrown by the agent : " + "java.rmi.server.ExportException: Port already in use")) { - return p.exitValue(); + return output.getExitValue(); } } diff --git a/jdk/test/sun/management/jmxremote/bootstrap/RmiRegistrySslTest.java b/jdk/test/sun/management/jmxremote/bootstrap/RmiRegistrySslTest.java index eaeaa392936..dd1b3d1a043 100644 --- a/jdk/test/sun/management/jmxremote/bootstrap/RmiRegistrySslTest.java +++ b/jdk/test/sun/management/jmxremote/bootstrap/RmiRegistrySslTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -188,15 +188,14 @@ public class RmiRegistrySslTest { ProcessBuilder processBuilder = ProcessTools.createJavaProcessBuilder(command.toArray(new String[command.size()])); - Process p = processBuilder.start(); - OutputAnalyzer output = new OutputAnalyzer(p); + OutputAnalyzer output = ProcessTools.executeProcess(processBuilder); System.out.println("test output:"); System.out.println(output.getOutput()); if (!output.getOutput().contains("Exception thrown by the agent : " + "java.rmi.server.ExportException: Port already in use")) { - return p.exitValue(); + return output.getExitValue(); } } throw new Error("Cannot find free port"); diff --git a/jdk/test/sun/tools/jhat/HatHeapDump1Test.java b/jdk/test/sun/tools/jhat/HatHeapDump1Test.java index babd09d44db..eab353e7249 100644 --- a/jdk/test/sun/tools/jhat/HatHeapDump1Test.java +++ b/jdk/test/sun/tools/jhat/HatHeapDump1Test.java @@ -52,7 +52,7 @@ public class HatHeapDump1Test { "-Xverify:all", "-agentlib:hprof=heap=dump,format=b,file=" + dumpFile.getAbsolutePath(), className); - OutputAnalyzer output = new OutputAnalyzer(processBuilder.start()); + OutputAnalyzer output = ProcessTools.executeProcess(processBuilder); System.out.println(output.getOutput()); output.shouldHaveExitValue(0); output.shouldContain("Dumping Java heap ... done"); @@ -79,7 +79,7 @@ public class HatHeapDump1Test { ProcessBuilder processBuilder = new ProcessBuilder(); processBuilder.command(launcher.getCommand()); System.out.println(Arrays.toString(processBuilder.command().toArray()).replace(",", "")); - OutputAnalyzer output = new OutputAnalyzer(processBuilder.start()); + OutputAnalyzer output = ProcessTools.executeProcess(processBuilder); System.out.println(output.getOutput()); return output; diff --git a/jdk/test/sun/tools/jmap/BasicJMapTest.java b/jdk/test/sun/tools/jmap/BasicJMapTest.java index 3630e9da288..a3c5d2c0e3e 100644 --- a/jdk/test/sun/tools/jmap/BasicJMapTest.java +++ b/jdk/test/sun/tools/jmap/BasicJMapTest.java @@ -91,7 +91,7 @@ public class BasicJMapTest { processBuilder.command(launcher.getCommand()); System.out.println(Arrays.toString(processBuilder.command().toArray()).replace(",", "")); - OutputAnalyzer output = new OutputAnalyzer(processBuilder.start()); + OutputAnalyzer output = ProcessTools.executeProcess(processBuilder); System.out.println(output.getOutput()); return output; diff --git a/jdk/test/sun/tools/jps/JpsHelper.java b/jdk/test/sun/tools/jps/JpsHelper.java index 84bc6beccb8..4e00cc95de4 100644 --- a/jdk/test/sun/tools/jps/JpsHelper.java +++ b/jdk/test/sun/tools/jps/JpsHelper.java @@ -36,6 +36,7 @@ import jdk.testlibrary.Asserts; import jdk.testlibrary.JDKToolLauncher; import jdk.testlibrary.OutputAnalyzer; import jdk.testlibrary.Utils; +import jdk.testlibrary.ProcessTools; /** * The helper class for running jps utility and verifying output from it @@ -160,7 +161,7 @@ public final class JpsHelper { ProcessBuilder processBuilder = new ProcessBuilder(launcher.getCommand()); System.out.println(Arrays.toString(processBuilder.command().toArray()).replace(",", "")); - OutputAnalyzer output = new OutputAnalyzer(processBuilder.start()); + OutputAnalyzer output = ProcessTools.executeProcess(processBuilder); System.out.println(output.getOutput()); return output; diff --git a/jdk/test/sun/tools/jps/TestJpsClass.java b/jdk/test/sun/tools/jps/TestJpsClass.java index 0133e9c7c58..a7ad136544b 100644 --- a/jdk/test/sun/tools/jps/TestJpsClass.java +++ b/jdk/test/sun/tools/jps/TestJpsClass.java @@ -55,7 +55,7 @@ public class TestJpsClass { cmd.add("monkey"); ProcessBuilder processBuilder = ProcessTools.createJavaProcessBuilder(cmd.toArray(new String[cmd.size()])); - OutputAnalyzer output = new OutputAnalyzer(processBuilder.start()); + OutputAnalyzer output = ProcessTools.executeProcess(processBuilder); System.out.println(output.getOutput()); output.shouldHaveExitValue(0); } diff --git a/jdk/test/sun/tools/jps/TestJpsJar.java b/jdk/test/sun/tools/jps/TestJpsJar.java index 37230ae8e6a..7414c529cdf 100644 --- a/jdk/test/sun/tools/jps/TestJpsJar.java +++ b/jdk/test/sun/tools/jps/TestJpsJar.java @@ -55,7 +55,7 @@ public class TestJpsJar { cmd.add("monkey"); ProcessBuilder processBuilder = ProcessTools.createJavaProcessBuilder(cmd.toArray(new String[cmd.size()])); - OutputAnalyzer output = new OutputAnalyzer(processBuilder.start()); + OutputAnalyzer output = ProcessTools.executeProcess(processBuilder); System.out.println(output.getOutput()); output.shouldHaveExitValue(0); } diff --git a/jdk/test/sun/tools/jps/TestJpsJarRelative.java b/jdk/test/sun/tools/jps/TestJpsJarRelative.java index 8b7def59e9d..6716419f954 100644 --- a/jdk/test/sun/tools/jps/TestJpsJarRelative.java +++ b/jdk/test/sun/tools/jps/TestJpsJarRelative.java @@ -55,7 +55,7 @@ public class TestJpsJarRelative { cmd.add("monkey"); ProcessBuilder processBuilder = ProcessTools.createJavaProcessBuilder(cmd.toArray(new String[cmd.size()])); - OutputAnalyzer output = new OutputAnalyzer(processBuilder.start()); + OutputAnalyzer output = ProcessTools.executeProcess(processBuilder); System.out.println(output.getOutput()); output.shouldHaveExitValue(0); } diff --git a/jdk/test/sun/tools/jstack/BasicJStackTest.java b/jdk/test/sun/tools/jstack/BasicJStackTest.java index 4b3c3656bcd..9efdf14d234 100644 --- a/jdk/test/sun/tools/jstack/BasicJStackTest.java +++ b/jdk/test/sun/tools/jstack/BasicJStackTest.java @@ -66,7 +66,7 @@ public class BasicJStackTest { processBuilder.command(launcher.getCommand()); System.out.println(Arrays.toString(processBuilder.command().toArray()).replace(",", "")); - OutputAnalyzer output = new OutputAnalyzer(processBuilder.start()); + OutputAnalyzer output = ProcessTools.executeProcess(processBuilder); System.out.println(output.getOutput()); return output; diff --git a/jdk/test/sun/tools/jstatd/JstatdTest.java b/jdk/test/sun/tools/jstatd/JstatdTest.java index 9cb1f60561e..73a625cef7c 100644 --- a/jdk/test/sun/tools/jstatd/JstatdTest.java +++ b/jdk/test/sun/tools/jstatd/JstatdTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -33,7 +33,6 @@ import static jdk.testlibrary.Asserts.*; import jdk.testlibrary.JDKToolLauncher; import jdk.testlibrary.OutputAnalyzer; import jdk.testlibrary.ProcessThread; -import jdk.testlibrary.TestThread; import jdk.testlibrary.Utils; import jdk.testlibrary.ProcessTools; @@ -145,8 +144,8 @@ public final class JstatdTest { return output; } - private OutputAnalyzer waitForJstatdRMI(ProcessBuilder pb) throws IOException, InterruptedException { - OutputAnalyzer output = new OutputAnalyzer(pb.start()); + private OutputAnalyzer waitForJstatdRMI(ProcessBuilder pb) throws Exception { + OutputAnalyzer output = ProcessTools.executeProcess(pb); String remoteHost = (serverName != null) ? serverName : "JStatRemoteHost"; while (output.getExitValue() != 0) { @@ -155,7 +154,7 @@ public final class JstatdTest { if (out.contains("RMI Registry not available") || out.contains("RMI Server " + remoteHost + " not available")) { Thread.sleep(100); - output = new OutputAnalyzer(pb.start()); + output = ProcessTools.executeProcess(pb); } else { output.shouldHaveExitValue(0); } diff --git a/jdk/test/sun/tools/jstatd/TestJstatdUsage.java b/jdk/test/sun/tools/jstatd/TestJstatdUsage.java index abec798df6f..976c1173cc0 100644 --- a/jdk/test/sun/tools/jstatd/TestJstatdUsage.java +++ b/jdk/test/sun/tools/jstatd/TestJstatdUsage.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -23,6 +23,7 @@ import jdk.testlibrary.JDKToolLauncher; import jdk.testlibrary.OutputAnalyzer; +import jdk.testlibrary.ProcessTools; /* * @test @@ -42,7 +43,7 @@ public class TestJstatdUsage { JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jstatd"); launcher.addToolArg(option); ProcessBuilder processBuilder = new ProcessBuilder(launcher.getCommand()); - OutputAnalyzer output = new OutputAnalyzer(processBuilder.start()); + OutputAnalyzer output = ProcessTools.executeProcess(processBuilder); output.shouldContain("usage: jstatd [-nr] [-p port] [-n rminame]"); output.shouldHaveExitValue(1);