db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From krist...@apache.org
Subject svn commit: r1242681 - in /db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit: BaseTestCase.java SpawnedProcess.java
Date Fri, 10 Feb 2012 05:39:02 GMT
Author: kristwaa
Date: Fri Feb 10 05:39:01 2012
New Revision: 1242681

URL: http://svn.apache.org/viewvc?rev=1242681&view=rev
Log:
DERBY-5608: BaseTestCase.readProcessOutput should read getInputStream() and getErrorStream()
in separate threads

Use SpawnedProcess to read stdout/stderr from the subprocess, since it already
has code to do this in separate threads.

Patch file: derby-5608-1a-use_spawnedprocess.diff

Modified:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseTestCase.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/SpawnedProcess.java

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseTestCase.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseTestCase.java?rev=1242681&r1=1242680&r2=1242681&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseTestCase.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseTestCase.java Fri Feb
10 05:39:01 2012
@@ -28,12 +28,9 @@ import java.io.BufferedInputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
 import java.io.FilenameFilter;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.OutputStream;
 import java.io.Reader;
 import java.io.PrintStream;
 import java.io.PrintWriter;
@@ -758,60 +755,33 @@ public abstract class BaseTestCase
         return ("IBM Corporation".equals(
                 getSystemProperty("java.vendor")));
     }
-    
-   /**
-    * Reads output from a process and returns it as a string.
-    * This will block until the process terminates.
-    * 
-    * @param pr a running process
-    * @return output of the process, both STDOUT and STDERR
-    * @throws InterruptedException
-    */
-   public static String readProcessOutput(Process pr) throws InterruptedException {
-		InputStream is = pr.getInputStream();
-		InputStream es = pr.getErrorStream();
-		if (is == null) {
-			fail("Unexpectedly receiving no text from the process");
-		}
-		String output = "";
-		try {
-		      output += "<STDOUT> " + inputStreamToString(is) + "<END STDOUT>\n";
-		      output += "<STDERR>" + inputStreamToString(es) + "<END STDERR>\n";
 
-		} catch (Exception e) {
-		    fail("Exception accessing inputstream from process", e);
-		}
-		
-		// wait until the process exits
-		pr.waitFor();
-		
-		return output;
-	}
-   
     /**
-     * Read contents of an input stream to a String
+     * Reads output from a process and returns it as a string.
+     * <p>
+     * This will block until the process terminates.
      * 
-     * @param is
-     * @return String with input stream contents
-     * @throws IOException
-     */
-    private static String inputStreamToString(InputStream is) throws IOException {
-
-        String isout = "";
-        char[] ca = new char[1024];
-        // Create an InputStreamReader with default encoding; we're hoping
-        // this to be en.
-        InputStreamReader inStream;
-        inStream = new InputStreamReader(is);
-
-        // keep reading from the stream until all done
-        int charsRead;
-        while ((charsRead = inStream.read(ca, 0, ca.length)) != -1) {
-            isout = isout + new String(ca, 0, charsRead);
-        }
-        return isout;
+     * @param pr a running process
+     * @return Output of the process, both STDOUT and STDERR.
+     * @throws InterruptedException if interrupted while waiting for the
+     *      subprocess or one of the output collector threads to terminate
+     */
+    public static String readProcessOutput(Process pr)
+            throws InterruptedException {
+        SpawnedProcess wrapper = new SpawnedProcess(pr, "readProcessOutput");
+        wrapper.suppressOutputOnComplete();
+        try {
+            wrapper.complete(false);
+        } catch (IOException ioe) {
+            fail("process completion method failed", ioe);
+        }
+        String output = "<STDOUT> " + wrapper.getFullServerOutput() +
+                "<END STDOUT>\n";
+        output += "<STDERR>" + wrapper.getFullServerError() +
+                "<END STDERR>\n";
+        return output;
     }
-   
+
     /**
      * Remove the directory and its contents.
      * @param path Path of the directory

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/SpawnedProcess.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/SpawnedProcess.java?rev=1242681&r1=1242680&r2=1242681&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/SpawnedProcess.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/SpawnedProcess.java Fri
Feb 10 05:39:01 2012
@@ -41,6 +41,8 @@ public final class SpawnedProcess {
 
     private final StreamSaver outSaver;
 
+    private boolean suppressOutput;
+
     public SpawnedProcess(Process javaProcess, String name) {
         this.javaProcess = javaProcess;
         this.name = name;
@@ -52,6 +54,17 @@ public final class SpawnedProcess {
     }
 
     /**
+     * Causes output obtained from the subprocess to be suppressed when
+     * executing the {@code complete}-methods.
+     *
+     * @see #getFullServerOutput() to obtain suppressed output from stdout
+     * @see #getFullServerError() to obtain suppressed output from stderr
+     */
+    public void suppressOutputOnComplete() {
+        suppressOutput = true;
+    }
+
+    /**
      * Get the Java Process object
      */
     public Process getProcess() {
@@ -70,7 +83,7 @@ public final class SpawnedProcess {
      * should be called first.
      * </p>
      */
-    public String getFullServerOutput() throws Exception {
+    public String getFullServerOutput() throws InterruptedException {
         // First wait until we've read all the output.
         outSaver.thread.join();
 
@@ -80,6 +93,23 @@ public final class SpawnedProcess {
     }
     
     /**
+     * Get the full server error output (stderr) as a string using the default
+     * encoding which is assumed is how it was originally written.
+     * <p>
+     * This method should only be called after the process has completed.
+     * That is, {@link #complete(boolean)} or {@link #complete(boolean, long)}
+     * should be called first.
+     */
+    public String getFullServerError() throws InterruptedException {
+        // First wait until we've read all the output on stderr.
+        errSaver.thread.join();
+
+        synchronized (this) {
+            return errSaver.stream.toString();
+        }
+    }
+
+    /**
      * Position offset for getNextServerOutput().
      */
     int stdOutReadOffset;
@@ -191,7 +221,7 @@ public final class SpawnedProcess {
 
             // Always write the error
             ByteArrayOutputStream err = errSaver.stream;
-            if (err.size() != 0) {
+            if (!suppressOutput && err.size() != 0) {
                 System.err.println("START-SPAWNED:" + name + " ERROR OUTPUT:");
                 err.writeTo(System.err);
                 System.err.println("END-SPAWNED  :" + name + " ERROR OUTPUT:");
@@ -200,7 +230,8 @@ public final class SpawnedProcess {
             // Only write the error if it appeared the server
             // failed in some way.
             ByteArrayOutputStream out = outSaver.stream;
-            if ((destroy || exitCode != 0) && out.size() != 0) {
+            if (!suppressOutput && (destroy || exitCode != 0) &&
+                    out.size() != 0) {
                 System.out.println("START-SPAWNED:" + name
                         + " STANDARD OUTPUT: exit code=" + exitCode);
                 out.writeTo(System.out);



Mime
View raw message