Path: blob/master/test/lib-test/jdk/test/lib/process/OutputAnalyzerTest.java
43745 views
/*1* Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @summary Test the OutputAnalyzer utility class26* @modules java.management27* @library /test/lib28* @run main OutputAnalyzerTest29*/3031import jdk.test.lib.process.OutputAnalyzer;3233public class OutputAnalyzerTest {3435public static void main(String args[]) throws Exception {3637String stdout = "aaaaaa";38String stderr = "bbbbbb";39String nonExistingString = "cccc";4041OutputAnalyzer output = new OutputAnalyzer(stdout, stderr);4243if (!stdout.equals(output.getStdout())) {44throw new Exception("getStdout() returned '" + output.getStdout()45+ "', expected '" + stdout + "'");46}4748if (!stderr.equals(output.getStderr())) {49throw new Exception("getStderr() returned '" + output.getStderr()50+ "', expected '" + stderr + "'");51}5253try {54output.shouldContain(stdout);55output.stdoutShouldContain(stdout);56output.shouldContain(stderr);57output.stderrShouldContain(stderr);58} catch (RuntimeException e) {59throw new Exception("shouldContain() failed", e);60}6162try {63output.shouldContain(nonExistingString);64throw new Exception("shouldContain() failed to throw exception");65} catch (RuntimeException e) {66// expected67}6869try {70output.stdoutShouldContain(stderr);71throw new Exception(72"stdoutShouldContain() failed to throw exception");73} catch (RuntimeException e) {74// expected75}7677try {78output.stderrShouldContain(stdout);79throw new Exception(80"stdoutShouldContain() failed to throw exception");81} catch (RuntimeException e) {82// expected83}8485try {86output.shouldNotContain(nonExistingString);87output.stdoutShouldNotContain(nonExistingString);88output.stderrShouldNotContain(nonExistingString);89} catch (RuntimeException e) {90throw new Exception("shouldNotContain() failed", e);91}9293try {94output.shouldNotContain(stdout);95throw new Exception("shouldContain() failed to throw exception");96} catch (RuntimeException e) {97// expected98}99100try {101output.stdoutShouldNotContain(stdout);102throw new Exception("shouldContain() failed to throw exception");103} catch (RuntimeException e) {104// expected105}106107try {108output.stderrShouldNotContain(stderr);109throw new Exception("shouldContain() failed to throw exception");110} catch (RuntimeException e) {111// expected112}113114String stdoutPattern = "[a]";115String stdoutByLinePattern = "a*";116String stderrPattern = "[b]";117String nonExistingPattern = "[c]";118String byLinePattern = "[ab]*";119120// Should match121try {122output.shouldMatch(stdoutPattern);123output.stdoutShouldMatch(stdoutPattern);124output.shouldMatch(stderrPattern);125output.stderrShouldMatch(stderrPattern);126} catch (RuntimeException e) {127throw new Exception("shouldMatch() failed", e);128}129130try {131output.shouldMatch(nonExistingPattern);132throw new Exception("shouldMatch() failed to throw exception");133} catch (RuntimeException e) {134// expected135}136137try {138output.stdoutShouldMatch(stderrPattern);139throw new Exception(140"stdoutShouldMatch() failed to throw exception");141} catch (RuntimeException e) {142// expected143}144145try {146output.stderrShouldMatch(stdoutPattern);147throw new Exception(148"stderrShouldMatch() failed to throw exception");149} catch (RuntimeException e) {150// expected151}152153try {154output.shouldMatchByLine(byLinePattern);155} catch (RuntimeException e) {156throw new Exception("shouldMatchByLine() failed", e);157}158159try {160output.shouldMatchByLine(nonExistingPattern);161throw new Exception("shouldMatchByLine() failed to throw exception");162} catch (RuntimeException e) {163// expected164}165166try {167output.stdoutShouldMatchByLine(stdoutByLinePattern);168} catch (RuntimeException e) {169throw new Exception("stdoutShouldMatchByLine() failed", e);170}171172// Should not match173try {174output.shouldNotMatch(nonExistingPattern);175output.stdoutShouldNotMatch(nonExistingPattern);176output.stderrShouldNotMatch(nonExistingPattern);177} catch (RuntimeException e) {178throw new Exception("shouldNotMatch() failed", e);179}180181try {182output.shouldNotMatch(stdoutPattern);183throw new Exception("shouldNotMatch() failed to throw exception");184} catch (RuntimeException e) {185// expected186}187188try {189output.stdoutShouldNotMatch(stdoutPattern);190throw new Exception("shouldNotMatch() failed to throw exception");191} catch (RuntimeException e) {192// expected193}194195try {196output.stderrShouldNotMatch(stderrPattern);197throw new Exception("shouldNotMatch() failed to throw exception");198} catch (RuntimeException e) {199// expected200}201202{203String aaaa = "aaaa";204String result = output.firstMatch(aaaa);205if (!aaaa.equals(result)) {206throw new Exception("firstMatch(String) failed to match. Expected: " + aaaa + " got: " + result);207}208}209210{211String aa = "aa";212String aa_grouped_aa = aa + "(" + aa + ")";213String result = output.firstMatch(aa_grouped_aa, 1);214if (!aa.equals(result)) {215throw new Exception("firstMatch(String, int) failed to match. Expected: " + aa + " got: " + result);216}217}218}219220}221222223