ctornqvi@4501: /* ctornqvi@4501: * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. ctornqvi@4501: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ctornqvi@4501: * ctornqvi@4501: * This code is free software; you can redistribute it and/or modify it ctornqvi@4501: * under the terms of the GNU General Public License version 2 only, as ctornqvi@4501: * published by the Free Software Foundation. ctornqvi@4501: * ctornqvi@4501: * This code is distributed in the hope that it will be useful, but WITHOUT ctornqvi@4501: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ctornqvi@4501: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ctornqvi@4501: * version 2 for more details (a copy is included in the LICENSE file that ctornqvi@4501: * accompanied this code). ctornqvi@4501: * ctornqvi@4501: * You should have received a copy of the GNU General Public License version ctornqvi@4501: * 2 along with this work; if not, write to the Free Software Foundation, ctornqvi@4501: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ctornqvi@4501: * ctornqvi@4501: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ctornqvi@4501: * or visit www.oracle.com if you need additional information or have any ctornqvi@4501: * questions. ctornqvi@4501: */ ctornqvi@4501: ctornqvi@4501: /* ctornqvi@4501: * @test ctornqvi@4501: * @summary Test the OutputAnalyzer utility class ctornqvi@4501: * @library /testlibrary ctornqvi@4501: */ ctornqvi@4501: ctornqvi@4501: import com.oracle.java.testlibrary.OutputAnalyzer; ctornqvi@4501: ctornqvi@4501: public class OutputAnalyzerTest { ctornqvi@4501: ctornqvi@4501: public static void main(String args[]) throws Exception { ctornqvi@4501: ctornqvi@4501: String stdout = "aaaaaa"; ctornqvi@4501: String stderr = "bbbbbb"; ctornqvi@4501: ctornqvi@4885: // Regexps used for testing pattern matching of the test input ctornqvi@4885: String stdoutPattern = "[a]"; ctornqvi@4885: String stderrPattern = "[b]"; ctornqvi@4885: String nonExistingPattern = "[c]"; ctornqvi@4885: ctornqvi@4501: OutputAnalyzer output = new OutputAnalyzer(stdout, stderr); ctornqvi@4501: ctornqvi@4501: if (!stdout.equals(output.getStdout())) { ctornqvi@4501: throw new Exception("getStdout() returned '" + output.getStdout() + "', expected '" + stdout + "'"); ctornqvi@4501: } ctornqvi@4501: ctornqvi@4501: if (!stderr.equals(output.getStderr())) { ctornqvi@4501: throw new Exception("getStderr() returned '" + output.getStderr() + "', expected '" + stderr + "'"); ctornqvi@4501: } ctornqvi@4501: ctornqvi@4501: try { ctornqvi@4501: output.shouldContain(stdout); ctornqvi@4501: output.stdoutShouldContain(stdout); ctornqvi@4501: output.shouldContain(stderr); ctornqvi@4501: output.stderrShouldContain(stderr); ctornqvi@4501: } catch (RuntimeException e) { ctornqvi@4501: throw new Exception("shouldContain() failed", e); ctornqvi@4501: } ctornqvi@4501: ctornqvi@4501: try { ctornqvi@4501: output.shouldContain("cccc"); ctornqvi@4501: throw new Exception("shouldContain() failed to throw exception"); ctornqvi@4501: } catch (RuntimeException e) { ctornqvi@4501: // expected ctornqvi@4501: } ctornqvi@4501: ctornqvi@4501: try { ctornqvi@4501: output.stdoutShouldContain(stderr); ctornqvi@4501: throw new Exception("stdoutShouldContain() failed to throw exception"); ctornqvi@4501: } catch (RuntimeException e) { ctornqvi@4501: // expected ctornqvi@4501: } ctornqvi@4501: ctornqvi@4501: try { ctornqvi@4501: output.stderrShouldContain(stdout); ctornqvi@4501: throw new Exception("stdoutShouldContain() failed to throw exception"); ctornqvi@4501: } catch (RuntimeException e) { ctornqvi@4501: // expected ctornqvi@4501: } ctornqvi@4501: ctornqvi@4501: try { ctornqvi@4501: output.shouldNotContain("cccc"); ctornqvi@4501: output.stdoutShouldNotContain("cccc"); ctornqvi@4501: output.stderrShouldNotContain("cccc"); ctornqvi@4501: } catch (RuntimeException e) { ctornqvi@4501: throw new Exception("shouldNotContain() failed", e); ctornqvi@4501: } ctornqvi@4501: ctornqvi@4501: try { ctornqvi@4501: output.shouldNotContain(stdout); ctornqvi@4501: throw new Exception("shouldContain() failed to throw exception"); ctornqvi@4501: } catch (RuntimeException e) { ctornqvi@4501: // expected ctornqvi@4501: } ctornqvi@4501: ctornqvi@4501: try { ctornqvi@4501: output.stdoutShouldNotContain(stdout); ctornqvi@4501: throw new Exception("shouldContain() failed to throw exception"); ctornqvi@4501: } catch (RuntimeException e) { ctornqvi@4501: // expected ctornqvi@4501: } ctornqvi@4501: ctornqvi@4501: try { ctornqvi@4885: output.stderrShouldNotContain(stderr); ctornqvi@4885: throw new Exception("shouldContain() failed to throw exception"); ctornqvi@4501: } catch (RuntimeException e) { ctornqvi@4885: // expected ctornqvi@4885: } ctornqvi@4885: ctornqvi@4885: // Should match ctornqvi@4885: try { ctornqvi@4885: output.shouldMatch(stdoutPattern); ctornqvi@4885: output.stdoutShouldMatch(stdoutPattern); ctornqvi@4885: output.shouldMatch(stderrPattern); ctornqvi@4885: output.stderrShouldMatch(stderrPattern); ctornqvi@4885: } catch (RuntimeException e) { ctornqvi@4885: throw new Exception("shouldMatch() failed", e); ctornqvi@4885: } ctornqvi@4885: ctornqvi@4885: try { ctornqvi@4885: output.shouldMatch(nonExistingPattern); ctornqvi@4885: throw new Exception("shouldMatch() failed to throw exception"); ctornqvi@4885: } catch (RuntimeException e) { ctornqvi@4885: // expected ctornqvi@4885: } ctornqvi@4885: ctornqvi@4885: try { ctornqvi@4885: output.stdoutShouldMatch(stderrPattern); ctornqvi@4885: throw new Exception( ctornqvi@4885: "stdoutShouldMatch() failed to throw exception"); ctornqvi@4885: } catch (RuntimeException e) { ctornqvi@4885: // expected ctornqvi@4885: } ctornqvi@4885: ctornqvi@4885: try { ctornqvi@4885: output.stderrShouldMatch(stdoutPattern); ctornqvi@4885: throw new Exception( ctornqvi@4885: "stderrShouldMatch() failed to throw exception"); ctornqvi@4885: } catch (RuntimeException e) { ctornqvi@4885: // expected ctornqvi@4885: } ctornqvi@4885: ctornqvi@4885: // Should not match ctornqvi@4885: try { ctornqvi@4885: output.shouldNotMatch(nonExistingPattern); ctornqvi@4885: output.stdoutShouldNotMatch(nonExistingPattern); ctornqvi@4885: output.stderrShouldNotMatch(nonExistingPattern); ctornqvi@4885: } catch (RuntimeException e) { ctornqvi@4885: throw new Exception("shouldNotMatch() failed", e); ctornqvi@4885: } ctornqvi@4885: ctornqvi@4885: try { ctornqvi@4885: output.shouldNotMatch(stdoutPattern); ctornqvi@4885: throw new Exception("shouldNotMatch() failed to throw exception"); ctornqvi@4885: } catch (RuntimeException e) { ctornqvi@4885: // expected ctornqvi@4885: } ctornqvi@4885: ctornqvi@4885: try { ctornqvi@4885: output.stdoutShouldNotMatch(stdoutPattern); ctornqvi@4885: throw new Exception("shouldNotMatch() failed to throw exception"); ctornqvi@4885: } catch (RuntimeException e) { ctornqvi@4885: // expected ctornqvi@4885: } ctornqvi@4885: ctornqvi@4885: try { ctornqvi@4885: output.stderrShouldNotMatch(stderrPattern); ctornqvi@4885: throw new Exception("shouldNotMatch() failed to throw exception"); ctornqvi@4885: } catch (RuntimeException e) { ctornqvi@4885: // expected ctornqvi@4501: } stefank@5706: stefank@5706: { stefank@5706: String aaaa = "aaaa"; stefank@5706: String result = output.firstMatch(aaaa); stefank@5706: if (!aaaa.equals(result)) { stefank@5706: throw new Exception("firstMatch(String) faild to match. Expected: " + aaaa + " got: " + result); stefank@5706: } stefank@5706: } stefank@5706: stefank@5706: { stefank@5706: String aa = "aa"; stefank@5706: String aa_grouped_aa = aa + "(" + aa + ")"; stefank@5706: String result = output.firstMatch(aa_grouped_aa, 1); stefank@5706: if (!aa.equals(result)) { stefank@5706: throw new Exception("firstMatch(String, int) failed to match. Expected: " + aa + " got: " + result); stefank@5706: } stefank@5706: } ctornqvi@4501: } ctornqvi@4501: }