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