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 /*
  25  * @test
  26  * @summary Test the OutputAnalyzer utility class
  27  * @library /testlibrary
  28  * @build com.oracle.java.testlibrary.*
  29  * @run main/othervm OutputAnalyzerTest
  30  */
  31 
  32 import com.oracle.java.testlibrary.OutputAnalyzer;
  33 
  34 public class OutputAnalyzerTest {
  35 
  36   public static void main(String args[]) throws Exception {
  37 
  38     String stdout = "aaaaaa";
  39     String stderr = "bbbbbb";
  40 
  41     // Regexps used for testing pattern matching of the test input
  42     String stdoutPattern = "[a]";
  43     String stderrPattern = "[b]";
  44     String nonExistingPattern = "[c]";
  45 
  46     OutputAnalyzer output = new OutputAnalyzer(stdout, stderr);
  47 
  48     if (!stdout.equals(output.getStdout())) {
  49       throw new Exception("getStdout() returned '" + output.getStdout() + "', expected '" + stdout + "'");
  50     }
  51 
  52     if (!stderr.equals(output.getStderr())) {
  53       throw new Exception("getStderr() returned '" + output.getStderr() + "', expected '" + stderr + "'");
  54     }
  55 
  56     try {
  57       output.shouldContain(stdout);
  58       output.stdoutShouldContain(stdout);
  59       output.shouldContain(stderr);
  60       output.stderrShouldContain(stderr);
  61     } catch (RuntimeException e) {
  62       throw new Exception("shouldContain() failed", e);
  63     }
  64 
  65     try {
  66       output.shouldContain("cccc");
  67       throw new Exception("shouldContain() failed to throw exception");
  68     } catch (RuntimeException e) {
  69       // expected
  70     }
  71 
  72     try {
  73       output.stdoutShouldContain(stderr);
  74       throw new Exception("stdoutShouldContain() failed to throw exception");
  75     } catch (RuntimeException e) {
  76       // expected
  77     }
  78 
  79     try {
  80       output.stderrShouldContain(stdout);
  81       throw new Exception("stdoutShouldContain() failed to throw exception");
  82     } catch (RuntimeException e) {
  83       // expected
  84     }
  85 
  86     try {
  87       output.shouldNotContain("cccc");
  88       output.stdoutShouldNotContain("cccc");
  89       output.stderrShouldNotContain("cccc");
  90     } catch (RuntimeException e) {
  91       throw new Exception("shouldNotContain() failed", e);
  92     }
  93 
  94     try {
  95       output.shouldNotContain(stdout);
  96       throw new Exception("shouldContain() failed to throw exception");
  97     } catch (RuntimeException e) {
  98       // expected
  99     }
 100 
 101     try {
 102       output.stdoutShouldNotContain(stdout);
 103       throw new Exception("shouldContain() failed to throw exception");
 104     } catch (RuntimeException e) {
 105       // expected
 106     }
 107 
 108     try {
 109         output.stderrShouldNotContain(stderr);
 110         throw new Exception("shouldContain() failed to throw exception");
 111     } catch (RuntimeException e) {
 112         // expected
 113     }
 114 
 115     // Should match
 116     try {
 117         output.shouldMatch(stdoutPattern);
 118         output.stdoutShouldMatch(stdoutPattern);
 119         output.shouldMatch(stderrPattern);
 120         output.stderrShouldMatch(stderrPattern);
 121     } catch (RuntimeException e) {
 122         throw new Exception("shouldMatch() failed", e);
 123     }
 124 
 125     try {
 126         output.shouldMatch(nonExistingPattern);
 127         throw new Exception("shouldMatch() failed to throw exception");
 128     } catch (RuntimeException e) {
 129         // expected
 130     }
 131 
 132     try {
 133         output.stdoutShouldMatch(stderrPattern);
 134         throw new Exception(
 135                 "stdoutShouldMatch() failed to throw exception");
 136     } catch (RuntimeException e) {
 137         // expected
 138     }
 139 
 140     try {
 141         output.stderrShouldMatch(stdoutPattern);
 142         throw new Exception(
 143                 "stderrShouldMatch() failed to throw exception");
 144     } catch (RuntimeException e) {
 145         // expected
 146     }
 147 
 148     // Should not match
 149     try {
 150         output.shouldNotMatch(nonExistingPattern);
 151         output.stdoutShouldNotMatch(nonExistingPattern);
 152         output.stderrShouldNotMatch(nonExistingPattern);
 153     } catch (RuntimeException e) {
 154         throw new Exception("shouldNotMatch() failed", e);
 155     }
 156 
 157     try {
 158         output.shouldNotMatch(stdoutPattern);
 159         throw new Exception("shouldNotMatch() failed to throw exception");
 160     } catch (RuntimeException e) {
 161         // expected
 162     }
 163 
 164     try {
 165         output.stdoutShouldNotMatch(stdoutPattern);
 166         throw new Exception("shouldNotMatch() failed to throw exception");
 167     } catch (RuntimeException e) {
 168         // expected
 169     }
 170 
 171     try {
 172         output.stderrShouldNotMatch(stderrPattern);
 173         throw new Exception("shouldNotMatch() failed to throw exception");
 174     } catch (RuntimeException e) {
 175         // expected
 176     }
 177 
 178     {
 179       String aaaa = "aaaa";
 180       String result = output.firstMatch(aaaa);
 181       if (!aaaa.equals(result)) {
 182         throw new Exception("firstMatch(String) faild to match. Expected: " + aaaa + " got: " + result);
 183       }
 184     }
 185 
 186     {
 187       String aa = "aa";
 188       String aa_grouped_aa = aa + "(" + aa + ")";
 189       String result = output.firstMatch(aa_grouped_aa, 1);
 190       if (!aa.equals(result)) {
 191         throw new Exception("firstMatch(String, int) failed to match. Expected: " + aa + " got: " + result);
 192       }
 193     }
 194   }
 195 }