Path: blob/master/test/hotspot/jtreg/testlibrary_tests/ctw/CtwTest.java
40942 views
/*1* Copyright (c) 2013, 2021, 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*/2223import java.util.Arrays;24import java.util.List;25import java.util.Collections;26import java.util.ArrayList;2728import java.io.File;29import java.io.Writer;30import java.io.FileWriter;31import java.io.IOException;32import java.io.BufferedReader;3334import java.nio.file.Files;35import java.nio.file.Paths;36import java.nio.file.StandardCopyOption;37import java.nio.charset.Charset;3839import jdk.test.lib.Platform;40import jdk.test.lib.JDKToolFinder;41import jdk.test.lib.process.OutputAnalyzer;42import jdk.test.lib.process.ProcessTools;4344public abstract class CtwTest {45private static final String LOG_FILE = "ctw.log";46private static final String[] CTW_COMMAND = {47"-Xbootclasspath/a:.",48"-XX:+UnlockDiagnosticVMOptions",49"-XX:+WhiteBoxAPI",50"-Dsun.hotspot.tools.ctw.logfile=" + LOG_FILE,51"--add-exports", "java.base/jdk.internal.access=ALL-UNNAMED",52"--add-exports", "java.base/jdk.internal.jimage=ALL-UNNAMED",53"--add-exports", "java.base/jdk.internal.misc=ALL-UNNAMED",54"--add-exports", "java.base/jdk.internal.reflect=ALL-UNNAMED",55sun.hotspot.tools.ctw.CompileTheWorld.class.getName(),56};57protected final String[] shouldContain;58protected CtwTest(String[] shouldContain) {59this.shouldContain = shouldContain;60}6162public void run(String[] args) throws Exception {63if (args.length == 0) {64throw new Error("args is empty");65}66switch (args[0]) {67case "prepare":68prepare();69break;70case "check":71check();72break;73case "compile":74compile(args);75break;76default:77throw new Error("unregonized action -- " + args[0]);78}79}8081protected void prepare() throws Exception { }8283protected void check() throws Exception {84try (BufferedReader r = Files.newBufferedReader(Paths.get(LOG_FILE),85Charset.defaultCharset())) {86OutputAnalyzer output = readOutput(r);87for (String test : shouldContain) {88output.shouldContain(test);89}90}91}9293protected void compile(String[] args) throws Exception {94// concat CTW_COMMAND and args w/o 0th element95String[] cmd = Arrays.copyOf(CTW_COMMAND, CTW_COMMAND.length + args.length - 1);96System.arraycopy(args, 1, cmd, CTW_COMMAND.length, args.length - 1);97if (Platform.isWindows()) {98// arguments with '*' has to be quoted on windows99for (int i = 0; i < cmd.length; ++i) {100if (cmd[i].charAt(0) != '"' && cmd[i].indexOf('*') >= 0) {101cmd[i] = '"' + cmd[i] + '"';102}103}104}105ProcessBuilder pb = ProcessTools.createTestJvm(cmd);106OutputAnalyzer output = new OutputAnalyzer(pb.start());107dump(output, "compile");108output.shouldHaveExitValue(0);109}110111private static OutputAnalyzer readOutput(BufferedReader reader)112throws IOException {113StringBuilder builder = new StringBuilder();114String eol = String.format("%n");115String line;116117while ((line = reader.readLine()) != null) {118builder.append(line);119builder.append(eol);120}121return new OutputAnalyzer(builder.toString(), "");122}123124protected void dump(OutputAnalyzer output, String name) {125try (Writer w = new FileWriter(name + ".out")) {126String s = output.getStdout();127w.write(s, s.length(), 0);128} catch (IOException io) {129io.printStackTrace();130}131try (Writer w = new FileWriter(name + ".err")) {132String s = output.getStderr();133w.write(s, s.length(), 0);134} catch (IOException io) {135io.printStackTrace();136}137}138139protected ProcessBuilder createJarProcessBuilder(String... command)140throws Exception {141String javapath = JDKToolFinder.getJDKTool("jar");142143ArrayList<String> args = new ArrayList<>();144args.add(javapath);145Collections.addAll(args, command);146147return new ProcessBuilder(args.toArray(new String[args.size()]));148}149}150151152