Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/RuntimeTests/exec/SetCwd.java
41153 views
1
/*
2
* Copyright (c) 1999, 2021, 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 4156278
27
* @summary Basic functional test for
28
* Runtime.exec(String[] command, String[] env, File path) and
29
* Runtime.exec(String command, String[] env, File path).
30
*
31
* @library /test/lib
32
* @run testng/othervm SetCwd
33
*/
34
import org.testng.annotations.BeforeTest;
35
import org.testng.annotations.Test;
36
37
import java.io.*;
38
39
import static jdk.test.lib.Asserts.assertTrue;
40
41
public class SetCwd {
42
43
private static final String TEST_CLASSES = System.getProperty(
44
"test.classes", ".");
45
46
private static final String[] CMD_ARRAY = new String[2];
47
48
@BeforeTest
49
public static void setUp() throws Exception {
50
CMD_ARRAY[0] = System.getProperty("java.home") + File.separator +
51
"bin" + File.separator + "java";
52
CMD_ARRAY[1] = SimpleProcess.class.getName();
53
}
54
55
@Test
56
public void testRuntimeExecWithArray() throws Exception {
57
Process process = Runtime.getRuntime().exec(CMD_ARRAY, null,
58
new File(TEST_CLASSES));
59
verifyProcessOutput(process);
60
}
61
62
@Test
63
public void testRuntimeExecWithString() throws Exception {
64
String cmd = String.join(" ", CMD_ARRAY);
65
Process process = Runtime.getRuntime().exec(cmd, null,
66
new File(TEST_CLASSES));
67
verifyProcessOutput(process);
68
}
69
70
// Verify the process has executed by comparing its output with the expected
71
private void verifyProcessOutput(Process process) throws Exception {
72
process.waitFor();
73
assertTrue(process.exitValue() == 0);
74
75
try (BufferedReader reader = new BufferedReader(
76
new InputStreamReader(process.getInputStream()))) {
77
String line = reader.readLine();
78
if (!line.startsWith(TEST_CLASSES)) {
79
String error = String.format("Expected process output first line: " +
80
"'%s' Actual: '%s'", TEST_CLASSES, line);
81
throw new Exception(error);
82
}
83
}
84
}
85
86
// This class main will be the entry point for test subprocesses
87
static class SimpleProcess {
88
public static void main (String[] args) throws Exception {
89
File dir = new File(".");
90
System.out.println(dir.getCanonicalPath());
91
}
92
}
93
}
94
95