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 /* 25 * @test 26 * @summary Test the OutputAnalyzer utility class 27 * @modules java.management 28 * @library /test/lib 29 * @run main OutputAnalyzerTest 30 */ 31 32 import jdk.test.lib.process.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 String nonExistingString = "cccc"; 41 42 OutputAnalyzer output = new OutputAnalyzer(stdout, stderr); 43 44 if (!stdout.equals(output.getStdout())) { 45 throw new Exception("getStdout() returned '" + output.getStdout() 46 + "', expected '" + stdout + "'"); 47 } 48 49 if (!stderr.equals(output.getStderr())) { 50 throw new Exception("getStderr() returned '" + output.getStderr() 51 + "', 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(nonExistingString); 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( 73 "stdoutShouldContain() failed to throw exception"); 74 } catch (RuntimeException e) { 75 // expected 76 } 77 78 try { 79 output.stderrShouldContain(stdout); 80 throw new Exception( 81 "stdoutShouldContain() failed to throw exception"); 82 } catch (RuntimeException e) { 83 // expected 84 } 85 86 try { 87 output.shouldNotContain(nonExistingString); 88 output.stdoutShouldNotContain(nonExistingString); 89 output.stderrShouldNotContain(nonExistingString); 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 String stdoutPattern = "[a]"; 116 String stdoutByLinePattern = "a*"; 117 String stderrPattern = "[b]"; 118 String nonExistingPattern = "[c]"; 119 String byLinePattern = "[ab]*"; 120 121 // Should match 122 try { 123 output.shouldMatch(stdoutPattern); 124 output.stdoutShouldMatch(stdoutPattern); 125 output.shouldMatch(stderrPattern); 126 output.stderrShouldMatch(stderrPattern); 127 } catch (RuntimeException e) { 128 throw new Exception("shouldMatch() failed", e); 129 } 130 131 try { 132 output.shouldMatch(nonExistingPattern); 133 throw new Exception("shouldMatch() failed to throw exception"); 134 } catch (RuntimeException e) { 135 // expected 136 } 137 138 try { 139 output.stdoutShouldMatch(stderrPattern); 140 throw new Exception( 141 "stdoutShouldMatch() failed to throw exception"); 142 } catch (RuntimeException e) { 143 // expected 144 } 145 146 try { 147 output.stderrShouldMatch(stdoutPattern); 148 throw new Exception( 149 "stderrShouldMatch() failed to throw exception"); 150 } catch (RuntimeException e) { 151 // expected 152 } 153 154 try { 155 output.shouldMatchByLine(byLinePattern); 156 } catch (RuntimeException e) { 157 throw new Exception("shouldMatchByLine() failed", e); 158 } 159 160 try { 161 output.shouldMatchByLine(nonExistingPattern); 162 throw new Exception("shouldMatchByLine() failed to throw exception"); 163 } catch (RuntimeException e) { 164 // expected 165 } 166 167 try { 168 output.stdoutShouldMatchByLine(stdoutByLinePattern); 169 } catch (RuntimeException e) { 170 throw new Exception("stdoutShouldMatchByLine() failed", e); 171 } 172 173 // Should not match 174 try { 175 output.shouldNotMatch(nonExistingPattern); 176 output.stdoutShouldNotMatch(nonExistingPattern); 177 output.stderrShouldNotMatch(nonExistingPattern); 178 } catch (RuntimeException e) { 179 throw new Exception("shouldNotMatch() failed", e); 180 } 181 182 try { 183 output.shouldNotMatch(stdoutPattern); 184 throw new Exception("shouldNotMatch() failed to throw exception"); 185 } catch (RuntimeException e) { 186 // expected 187 } 188 189 try { 190 output.stdoutShouldNotMatch(stdoutPattern); 191 throw new Exception("shouldNotMatch() failed to throw exception"); 192 } catch (RuntimeException e) { 193 // expected 194 } 195 196 try { 197 output.stderrShouldNotMatch(stderrPattern); 198 throw new Exception("shouldNotMatch() failed to throw exception"); 199 } catch (RuntimeException e) { 200 // expected 201 } 202 } 203 204 }