jdk
86 строк · 3.1 Кб
1/*
2* Copyright (c) 2024, 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
24import java.io.IOException;
25import java.io.PrintWriter;
26import java.nio.file.Path;
27import java.util.Arrays;
28
29import java.io.StringWriter;
30import java.util.spi.ToolProvider;
31
32import jdk.test.lib.process.OutputAnalyzer;
33import jdk.test.lib.util.JarUtils;
34
35public class JNativeScanTestBase {
36
37public static final String MODULE_PATH = "mods";
38
39private static final ToolProvider JNATIVESCAN_TOOL = ToolProvider.findFirst("jnativescan")
40.orElseThrow(() -> new RuntimeException("jnativescan tool not found"));
41
42public static OutputAnalyzer jnativescan(String... args) {
43return run(JNATIVESCAN_TOOL, args);
44}
45
46private static OutputAnalyzer run(ToolProvider tp, String[] commands) {
47int rc;
48StringWriter sw = new StringWriter();
49StringWriter esw = new StringWriter();
50
51try (PrintWriter pw = new PrintWriter(sw);
52PrintWriter epw = new PrintWriter(esw)) {
53System.out.println("Running " + tp.name() + ", Command: " + Arrays.toString(commands));
54rc = tp.run(pw, epw, commands);
55}
56OutputAnalyzer output = new OutputAnalyzer(sw.toString(), esw.toString(), rc);
57output.outputTo(System.out);
58output.errorTo(System.err);
59return output;
60}
61
62public static Path makeModularJar(String moduleName) throws IOException {
63Path jarPath = Path.of(MODULE_PATH, moduleName + ".jar");
64Path moduleRoot = moduleRoot(moduleName);
65JarUtils.createJarFile(jarPath, moduleRoot);
66return jarPath;
67}
68
69public static Path moduleRoot(String name) {
70return Path.of(System.getProperty("test.module.path")).resolve(name);
71}
72
73public static OutputAnalyzer assertSuccess(OutputAnalyzer output) {
74if (output.getExitValue() != 0) {
75throw new IllegalStateException("tool run failed");
76}
77return output;
78}
79
80public static OutputAnalyzer assertFailure(OutputAnalyzer output) {
81if (output.getExitValue() == 0) {
82throw new IllegalStateException("tool run succeeded");
83}
84return output;
85}
86}
87