1 /*
 2  * Copyright (c) 2020, 2023, 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  * @bug 8023130 8166026
27  * @summary Unit test for java.lang.ProcessBuilder inheritance of standard output and standard error streams
28  * @library /test/lib
29  * @build jdk.test.lib.process.*
30  * @run testng InheritIOTest
31  */
32 
33 import java.util.List;
34 import static java.lang.ProcessBuilder.Redirect.INHERIT;
35 import jdk.test.lib.process.OutputAnalyzer;
36 import jdk.test.lib.process.ProcessTools;
37 import org.testng.annotations.DataProvider;
38 import org.testng.annotations.Test;
39 import static org.testng.Assert.*;
40 
41 public class InheritIOTest {
42 
43     private static final String EXIT_VALUE_TEMPLATE = "exit value: %d";
44     private static final String EXPECTED_RESULT_STDOUT = "message";
45     private static final String EXPECTED_RESULT_STDERR = EXIT_VALUE_TEMPLATE.formatted(0);
46 
47     @DataProvider
48     public Object[][] testCases() {
49         return new Object[][]{
50              new Object[] { List.of("InheritIOTest$TestInheritIO", "printf", EXPECTED_RESULT_STDOUT) },
51              new Object[] { List.of("InheritIOTest$TestRedirectInherit", "printf", EXPECTED_RESULT_STDOUT) }
52         };
53     }
54 
55     @Test(dataProvider = "testCases")
56     public void testInheritWithoutRedirect(List<String> arguments) throws Throwable {
57         ProcessBuilder processBuilder = ProcessTools.createLimitedTestJavaProcessBuilder(arguments);
58         OutputAnalyzer outputAnalyzer = ProcessTools.executeCommand(processBuilder);
59         outputAnalyzer.shouldHaveExitValue(0);
60         assertEquals(outputAnalyzer.getStdout(), EXPECTED_RESULT_STDOUT);
61         assertEquals(outputAnalyzer.getStderr(), EXPECTED_RESULT_STDERR);
62     }
63 
64     public static class TestInheritIO {
65         public static void main(String args[]) throws Throwable {
66             int err = new ProcessBuilder(args).inheritIO().start().waitFor();
67             System.err.printf(EXIT_VALUE_TEMPLATE, err);
68             System.exit(err);
69         }
70     }
71 
72     public static class TestRedirectInherit {
73         public static void main(String args[]) throws Throwable {
74             int err = new ProcessBuilder(args)
75                     .redirectInput(INHERIT)
76                     .redirectOutput(INHERIT)
77                     .redirectError(INHERIT)
78                     .start().waitFor();
79             System.err.printf(EXIT_VALUE_TEMPLATE, err);
80             System.exit(err);
81         }
82     }
83 
84 }