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