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