jdk/test/lib/testlibrary/OutputAnalyzerTest.java
changeset 15645 8d05e519bffb
child 27189 b90845965ee9
equal deleted inserted replaced
15644:2dfa520c7303 15645:8d05e519bffb
       
     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 jdk.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         String nonExistingString = "cccc";
       
    39 
       
    40         OutputAnalyzer output = new OutputAnalyzer(stdout, stderr);
       
    41 
       
    42         if (!stdout.equals(output.getStdout())) {
       
    43             throw new Exception("getStdout() returned '" + output.getStdout()
       
    44                     + "', expected '" + stdout + "'");
       
    45         }
       
    46 
       
    47         if (!stderr.equals(output.getStderr())) {
       
    48             throw new Exception("getStderr() returned '" + output.getStderr()
       
    49                     + "', expected '" + stderr + "'");
       
    50         }
       
    51 
       
    52         try {
       
    53             output.shouldContain(stdout);
       
    54             output.stdoutShouldContain(stdout);
       
    55             output.shouldContain(stderr);
       
    56             output.stderrShouldContain(stderr);
       
    57         } catch (RuntimeException e) {
       
    58             throw new Exception("shouldContain() failed", e);
       
    59         }
       
    60 
       
    61         try {
       
    62             output.shouldContain(nonExistingString);
       
    63             throw new Exception("shouldContain() failed to throw exception");
       
    64         } catch (RuntimeException e) {
       
    65             // expected
       
    66         }
       
    67 
       
    68         try {
       
    69             output.stdoutShouldContain(stderr);
       
    70             throw new Exception(
       
    71                     "stdoutShouldContain() failed to throw exception");
       
    72         } catch (RuntimeException e) {
       
    73             // expected
       
    74         }
       
    75 
       
    76         try {
       
    77             output.stderrShouldContain(stdout);
       
    78             throw new Exception(
       
    79                     "stdoutShouldContain() failed to throw exception");
       
    80         } catch (RuntimeException e) {
       
    81             // expected
       
    82         }
       
    83 
       
    84         try {
       
    85             output.shouldNotContain(nonExistingString);
       
    86             output.stdoutShouldNotContain(nonExistingString);
       
    87             output.stderrShouldNotContain(nonExistingString);
       
    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         String stdoutPattern = "[a]";
       
   114         String stderrPattern = "[b]";
       
   115         String nonExistingPattern = "[c]";
       
   116 
       
   117         // Should match
       
   118         try {
       
   119             output.shouldMatch(stdoutPattern);
       
   120             output.stdoutShouldMatch(stdoutPattern);
       
   121             output.shouldMatch(stderrPattern);
       
   122             output.stderrShouldMatch(stderrPattern);
       
   123         } catch (RuntimeException e) {
       
   124             throw new Exception("shouldMatch() failed", e);
       
   125         }
       
   126 
       
   127         try {
       
   128             output.shouldMatch(nonExistingPattern);
       
   129             throw new Exception("shouldMatch() failed to throw exception");
       
   130         } catch (RuntimeException e) {
       
   131             // expected
       
   132         }
       
   133 
       
   134         try {
       
   135             output.stdoutShouldMatch(stderrPattern);
       
   136             throw new Exception(
       
   137                     "stdoutShouldMatch() failed to throw exception");
       
   138         } catch (RuntimeException e) {
       
   139             // expected
       
   140         }
       
   141 
       
   142         try {
       
   143             output.stderrShouldMatch(stdoutPattern);
       
   144             throw new Exception(
       
   145                     "stderrShouldMatch() failed to throw exception");
       
   146         } catch (RuntimeException e) {
       
   147             // expected
       
   148         }
       
   149 
       
   150         // Should not match
       
   151         try {
       
   152             output.shouldNotMatch(nonExistingPattern);
       
   153             output.stdoutShouldNotMatch(nonExistingPattern);
       
   154             output.stderrShouldNotMatch(nonExistingPattern);
       
   155         } catch (RuntimeException e) {
       
   156             throw new Exception("shouldNotMatch() failed", e);
       
   157         }
       
   158 
       
   159         try {
       
   160             output.shouldNotMatch(stdoutPattern);
       
   161             throw new Exception("shouldNotMatch() failed to throw exception");
       
   162         } catch (RuntimeException e) {
       
   163             // expected
       
   164         }
       
   165 
       
   166         try {
       
   167             output.stdoutShouldNotMatch(stdoutPattern);
       
   168             throw new Exception("shouldNotMatch() failed to throw exception");
       
   169         } catch (RuntimeException e) {
       
   170             // expected
       
   171         }
       
   172 
       
   173         try {
       
   174             output.stderrShouldNotMatch(stderrPattern);
       
   175             throw new Exception("shouldNotMatch() failed to throw exception");
       
   176         } catch (RuntimeException e) {
       
   177             // expected
       
   178         }
       
   179     }
       
   180 
       
   181 }