Path: blob/master/test/jdk/java/lang/ProcessBuilder/InheritIOTest.java
41149 views
/*1* Copyright (c) 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* @bug 8023130 816602626* @summary Unit test for java.lang.ProcessBuilder inheritance of standard output and standard error streams27* @library /test/lib28* @build jdk.test.lib.process.*29* @run testng InheritIOTest30*/3132import java.util.List;33import static java.lang.ProcessBuilder.Redirect.INHERIT;34import jdk.test.lib.process.OutputAnalyzer;35import jdk.test.lib.process.ProcessTools;36import org.testng.annotations.DataProvider;37import org.testng.annotations.Test;38import static org.testng.Assert.*;3940public class InheritIOTest {4142private static final String EXIT_VALUE_TEMPLATE = "exit value: %d";43private static final String EXPECTED_RESULT_STDOUT = "message";44private static final String EXPECTED_RESULT_STDERR = EXIT_VALUE_TEMPLATE.formatted(0);4546@DataProvider47public Object[][] testCases() {48return new Object[][]{49new Object[] { List.of("InheritIOTest$TestInheritIO", "printf", EXPECTED_RESULT_STDOUT) },50new Object[] { List.of("InheritIOTest$TestRedirectInherit", "printf", EXPECTED_RESULT_STDOUT) }51};52}5354@Test(dataProvider = "testCases")55public void testInheritWithoutRedirect(List<String> arguments) throws Throwable {56ProcessBuilder processBuilder = ProcessTools.createJavaProcessBuilder(arguments);57OutputAnalyzer outputAnalyzer = ProcessTools.executeCommand(processBuilder);58outputAnalyzer.shouldHaveExitValue(0);59assertEquals(outputAnalyzer.getStdout(), EXPECTED_RESULT_STDOUT);60assertEquals(outputAnalyzer.getStderr(), EXPECTED_RESULT_STDERR);61}6263public static class TestInheritIO {64public static void main(String args[]) throws Throwable {65int err = new ProcessBuilder(args).inheritIO().start().waitFor();66System.err.printf(EXIT_VALUE_TEMPLATE, err);67System.exit(err);68}69}7071public static class TestRedirectInherit {72public static void main(String args[]) throws Throwable {73int err = new ProcessBuilder(args)74.redirectInput(INHERIT)75.redirectOutput(INHERIT)76.redirectError(INHERIT)77.start().waitFor();78System.err.printf(EXIT_VALUE_TEMPLATE, err);79System.exit(err);80}81}8283}848586