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

Print this page


   1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   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.Arrays;


  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();
  49     this.stderr = output.getStderr();
  50   }
  51 
  52   /**
  53    * Create an OutputAnalyzer, a utility class for verifying output


















  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    */


   1 /*
   2  * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   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.Arrays;
  28 import java.io.File;
  29 import java.nio.file.Files;
  30 import java.util.List;
  31 import java.util.regex.Matcher;
  32 import java.util.regex.Pattern;
  33 
  34 public final class OutputAnalyzer {
  35 
  36   private final String stdout;
  37   private final String stderr;
  38   private final int exitValue;
  39 
  40   /**
  41    * Create an OutputAnalyzer, a utility class for verifying output and exit
  42    * value from a Process
  43    *
  44    * @param process Process to analyze
  45    * @throws IOException If an I/O error occurs.
  46    */
  47   public OutputAnalyzer(Process process) throws IOException {
  48     OutputBuffer output = ProcessTools.getOutput(process);
  49     exitValue = process.exitValue();
  50     this.stdout = output.getStdout();
  51     this.stderr = output.getStderr();
  52   }
  53 
  54   /**
  55    * Create an OutputAnalyzer, a utility class for verifying output
  56    * loaded from the specified file.
  57    *
  58    * The loaded output can be retrived with getStdout or/and getOutput
  59    * methods, getStderr always returns an empty string.
  60    *
  61    * @param file a text file to analyze
  62    * @throws IOException If an I/O error occurs.
  63    **/
  64   public OutputAnalyzer(File file) throws IOException {
  65       this(new String(Files.readAllBytes(file.toPath())));
  66   }
  67 
  68   /**
  69    * Create an OutputAnalyzer, a utility class for verifying output
  70    * passed as a string.
  71    *
  72    * The output can be retrived with getStdout or/and getOutput
  73    * methods, getStderr always returns an empty string.
  74    *
  75    * @param buf String buffer to analyze
  76    */
  77   public OutputAnalyzer(String buf) {
  78       this(buf, "");
  79   }
  80 
  81   /**
  82    * Create an OutputAnalyzer, a utility class for verifying output
  83    *
  84    * @param stdout stdout buffer to analyze
  85    * @param stderr stderr buffer to analyze
  86    */
  87   public OutputAnalyzer(String stdout, String stderr) {
  88     this.stdout = stdout;
  89     this.stderr = stderr;
  90     exitValue = -1;
  91   }
  92 
  93   /**
  94    * Verify that the stdout contents of output buffer is empty
  95    *
  96    * @throws RuntimeException
  97    *             If stdout was not empty
  98    */