< prev index next >

test/testlibrary/com/oracle/java/testlibrary/OutputAnalyzer.java

Print this page
rev 7753 : 8071909: Port testlibrary improvments in jdk/test to hotspot/test as required for DCMD test port
Reviewed-by:


   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package com.oracle.java.testlibrary;
  25 
  26 import java.io.IOException;


  27 import java.util.regex.Matcher;
  28 import java.util.regex.Pattern;
  29 
  30 public final class OutputAnalyzer {
  31 
  32   private final String stdout;
  33   private final String stderr;
  34   private final int exitValue;
  35 
  36   /**
  37    * Create an OutputAnalyzer, a utility class for verifying output and exit
  38    * value from a Process
  39    *
  40    * @param process Process to analyze
  41    * @throws IOException If an I/O error occurs.
  42    */
  43   public OutputAnalyzer(Process process) throws IOException {
  44     OutputBuffer output = ProcessTools.getOutput(process);
  45     exitValue = process.exitValue();
  46     this.stdout = output.getStdout();


  52    *
  53    * @param buf String buffer to analyze
  54    */
  55   public OutputAnalyzer(String buf) {
  56     this(buf, buf);
  57   }
  58 
  59   /**
  60    * Create an OutputAnalyzer, a utility class for verifying output
  61    *
  62    * @param stdout stdout buffer to analyze
  63    * @param stderr stderr buffer to analyze
  64    */
  65   public OutputAnalyzer(String stdout, String stderr) {
  66     this.stdout = stdout;
  67     this.stderr = stderr;
  68     exitValue = -1;
  69   }
  70 
  71   /**




















































  72    * Verify that the stdout and stderr contents of output buffer contains the string
  73    *
  74    * @param expectedString String that buffer should contain
  75    * @throws RuntimeException If the string was not found
  76    */
  77   public OutputAnalyzer shouldContain(String expectedString) {
  78     if (!stdout.contains(expectedString) && !stderr.contains(expectedString)) {
  79         reportDiagnosticSummary();
  80         throw new RuntimeException("'" + expectedString + "' missing from stdout/stderr \n");
  81     }
  82     return this;
  83   }
  84 
  85   /**
  86    * Verify that the stdout contents of output buffer contains the string
  87    *
  88    * @param expectedString String that buffer should contain
  89    * @throws RuntimeException If the string was not found
  90    */
  91   public OutputAnalyzer stdoutShouldContain(String expectedString) {


 347   public String getStdout() {
 348     return stdout;
 349   }
 350 
 351   /**
 352    * Get the contents of the stderr buffer
 353    *
 354    * @return Content of the stderr buffer
 355    */
 356   public String getStderr() {
 357     return stderr;
 358   }
 359 
 360   /**
 361    * Get the process exit value
 362    *
 363    * @return Process exit value
 364    */
 365   public int getExitValue() {
 366     return exitValue;



















 367   }
 368 }


   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package com.oracle.java.testlibrary;
  25 
  26 import java.io.IOException;
  27 import java.util.ArrayList;
  28 import java.util.List;
  29 import java.util.regex.Matcher;
  30 import java.util.regex.Pattern;
  31 
  32 public final class OutputAnalyzer {
  33 
  34   private final String stdout;
  35   private final String stderr;
  36   private final int exitValue;
  37 
  38   /**
  39    * Create an OutputAnalyzer, a utility class for verifying output and exit
  40    * value from a Process
  41    *
  42    * @param process Process to analyze
  43    * @throws IOException If an I/O error occurs.
  44    */
  45   public OutputAnalyzer(Process process) throws IOException {
  46     OutputBuffer output = ProcessTools.getOutput(process);
  47     exitValue = process.exitValue();
  48     this.stdout = output.getStdout();


  54    *
  55    * @param buf String buffer to analyze
  56    */
  57   public OutputAnalyzer(String buf) {
  58     this(buf, buf);
  59   }
  60 
  61   /**
  62    * Create an OutputAnalyzer, a utility class for verifying output
  63    *
  64    * @param stdout stdout buffer to analyze
  65    * @param stderr stderr buffer to analyze
  66    */
  67   public OutputAnalyzer(String stdout, String stderr) {
  68     this.stdout = stdout;
  69     this.stderr = stderr;
  70     exitValue = -1;
  71   }
  72 
  73   /**
  74    * Verify that the stdout contents of output buffer is empty
  75    *
  76    * @throws RuntimeException
  77    *             If stdout was not empty
  78    */
  79   public void stdoutShouldBeEmpty() {
  80     if (!getStdout().isEmpty()) {
  81       reportDiagnosticSummary();
  82       throw new RuntimeException("stdout was not empty");
  83     }
  84   }
  85 
  86   /**
  87    * Verify that the stderr contents of output buffer is empty
  88    *
  89    * @throws RuntimeException
  90    *             If stderr was not empty
  91    */
  92   public void stderrShouldBeEmpty() {
  93     if (!getStderr().isEmpty()) {
  94       reportDiagnosticSummary();
  95       throw new RuntimeException("stderr was not empty");
  96     }
  97   }
  98 
  99   /**
 100    * Verify that the stdout contents of output buffer is not empty
 101    *
 102    * @throws RuntimeException
 103    *             If stdout was empty
 104    */
 105   public void stdoutShouldNotBeEmpty() {
 106     if (getStdout().isEmpty()) {
 107       reportDiagnosticSummary();
 108       throw new RuntimeException("stdout was empty");
 109     }
 110   }
 111 
 112   /**
 113    * Verify that the stderr contents of output buffer is not empty
 114    *
 115    * @throws RuntimeException
 116    *             If stderr was empty
 117    */
 118   public void stderrShouldNotBeEmpty() {
 119     if (getStderr().isEmpty()) {
 120       reportDiagnosticSummary();
 121       throw new RuntimeException("stderr was empty");
 122     }
 123   }
 124 
 125     /**
 126    * Verify that the stdout and stderr contents of output buffer contains the string
 127    *
 128    * @param expectedString String that buffer should contain
 129    * @throws RuntimeException If the string was not found
 130    */
 131   public OutputAnalyzer shouldContain(String expectedString) {
 132     if (!stdout.contains(expectedString) && !stderr.contains(expectedString)) {
 133         reportDiagnosticSummary();
 134         throw new RuntimeException("'" + expectedString + "' missing from stdout/stderr \n");
 135     }
 136     return this;
 137   }
 138 
 139   /**
 140    * Verify that the stdout contents of output buffer contains the string
 141    *
 142    * @param expectedString String that buffer should contain
 143    * @throws RuntimeException If the string was not found
 144    */
 145   public OutputAnalyzer stdoutShouldContain(String expectedString) {


 401   public String getStdout() {
 402     return stdout;
 403   }
 404 
 405   /**
 406    * Get the contents of the stderr buffer
 407    *
 408    * @return Content of the stderr buffer
 409    */
 410   public String getStderr() {
 411     return stderr;
 412   }
 413 
 414   /**
 415    * Get the process exit value
 416    *
 417    * @return Process exit value
 418    */
 419   public int getExitValue() {
 420     return exitValue;
 421   }
 422 
 423   /**
 424    * Get the contents of the output buffer (stdout and stderr) as list of strings.
 425    * Output will be split by system property 'line.separator'.
 426    *
 427    * @return Contents of the output buffer as list of strings
 428    */
 429   public List<String> asLines() {
 430     return asLines(getOutput());
 431   }
 432 
 433   private List<String> asLines(String buffer) {
 434     List<String> l = new ArrayList<>();
 435     String[] a = buffer.split(Utils.NEW_LINE);
 436     for (String string : a) {
 437       l.add(string);
 438     }
 439     return l;
 440   }
 441 }
< prev index next >