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