Skip to content

Commit b5922c3

Browse files
committed
8305846: Support compilation in Proc test utility
Reviewed-by: valeriep
1 parent 73ac710 commit b5922c3

File tree

4 files changed

+154
-13
lines changed

4 files changed

+154
-13
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) 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+
public class A {
24+
public static void main(String[] args) throws Exception {
25+
B.go();
26+
}
27+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 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+
public class B {
24+
public static void go() {
25+
System.out.println("Hello");
26+
System.err.println("World");
27+
}
28+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) 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+
import jdk.test.lib.process.Proc;
24+
25+
/*
26+
* @test
27+
* @bug 8305846
28+
* @library /test/lib
29+
*/
30+
public class Launcher {
31+
public static void main(String[] args) throws Exception {
32+
Proc.create("A")
33+
.compile()
34+
.start()
35+
.output()
36+
.stdoutShouldContain("Hello")
37+
.stderrShouldContain("World");
38+
}
39+
}

test/lib/jdk/test/lib/process/Proc.java

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,6 +23,8 @@
2323

2424
package jdk.test.lib.process;
2525

26+
import jdk.test.lib.compiler.CompilerUtils;
27+
2628
import java.io.BufferedReader;
2729
import java.io.File;
2830
import java.io.IOException;
@@ -118,7 +120,11 @@ public class Proc {
118120
private boolean inheritIO = false;
119121
private boolean noDump = false;
120122

121-
private List<String> cp; // user-provided classpath
123+
private boolean addcp; // user-provided classpath is appended
124+
private List<String> cp; // user-provided classpaths
125+
126+
private boolean compile; // compile the program as well
127+
122128
private String clazz; // Class to launch
123129
private String debug; // debug flag, controller will show data
124130
// transfer between procs. If debug is set,
@@ -195,15 +201,20 @@ public Proc inheritProp(String k) {
195201
}
196202
return this;
197203
}
198-
// Sets classpath. If not called, Proc will choose a classpath. If called
199-
// with no arg, no classpath will be used. Can be called multiple times.
204+
// Sets classpath. Can be called multiple times.
200205
public Proc cp(String... s) {
201206
if (cp == null) {
202207
cp = new ArrayList<>();
203208
}
204209
cp.addAll(Arrays.asList(s));
205210
return this;
206211
}
212+
// Adds classpath to defaults. Can be called multiple times.
213+
// Once called, addcp is always true.
214+
public Proc addcp(String... s) {
215+
addcp = true;
216+
return cp(s);
217+
}
207218
// Adds a permission to policy. Can be called multiple times.
208219
// All perm() calls after a series of grant() calls are grouped into
209220
// a single grant block. perm() calls before any grant() call are grouped
@@ -259,6 +270,34 @@ public Proc grant(String v) {
259270
grant.append(v).append(", ");
260271
return this;
261272
}
273+
// Compile as well
274+
public Proc compile() {
275+
compile = true;
276+
return this;
277+
}
278+
279+
// get full classpath.
280+
// 1. Default classpath used if neither cp() or addcp() is called
281+
// 2. User provided classpath (can be empty) used if only cp() is called
282+
// 3. User provided classpath + default classpath used, otherwise
283+
String fullcp() {
284+
if (cp == null) {
285+
return System.getProperty("test.class.path") + File.pathSeparator +
286+
System.getProperty("test.src.path");
287+
} else {
288+
var newcp = new ArrayList<>(cp);
289+
if (addcp) {
290+
newcp.add(System.getProperty("test.class.path"));
291+
newcp.add(System.getProperty("test.src.path"));
292+
}
293+
if (!newcp.isEmpty()) {
294+
return newcp.stream().collect(Collectors.joining(File.pathSeparator));
295+
} else {
296+
return null;
297+
}
298+
}
299+
}
300+
262301
// Starts the proc
263302
public Proc start() throws IOException {
264303
List<String> cmd = new ArrayList<>();
@@ -282,18 +321,26 @@ public Proc start() throws IOException {
282321
}
283322
}
284323

285-
Collections.addAll(cmd, splitProperty("test.vm.opts"));
286-
Collections.addAll(cmd, splitProperty("test.java.opts"));
287-
288-
if (cp == null) {
289-
cmd.add("-cp");
290-
cmd.add(System.getProperty("test.class.path") + File.pathSeparator +
291-
System.getProperty("test.src.path"));
292-
} else if (!cp.isEmpty()) {
324+
var lcp = fullcp();
325+
if (lcp != null) {
293326
cmd.add("-cp");
294-
cmd.add(cp.stream().collect(Collectors.joining(File.pathSeparator)));
327+
cmd.add(lcp);
295328
}
296329

330+
if (compile) {
331+
boolean comp = CompilerUtils.compile(
332+
Path.of(System.getProperty("test.src"), clazz + ".java"),
333+
Path.of(System.getProperty("test.classes")),
334+
cmd.subList(1, cmd.size()).toArray(new String[0]));
335+
// subList(1): all options added without launcher name
336+
if (!comp) {
337+
throw new RuntimeException("Compilation error");
338+
}
339+
}
340+
341+
Collections.addAll(cmd, splitProperty("test.vm.opts"));
342+
Collections.addAll(cmd, splitProperty("test.java.opts"));
343+
297344
if (!secprop.isEmpty()) {
298345
Path p = Path.of(getId("security"));
299346
try (OutputStream fos = Files.newOutputStream(p);

0 commit comments

Comments
 (0)