Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/tools/jps/JpsHelper.java
38840 views
/*1* Copyright (c) 2014, 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 static jdk.testlibrary.Asserts.assertGreaterThan;24import static jdk.testlibrary.Asserts.assertTrue;2526import java.io.BufferedWriter;27import java.io.File;28import java.io.FileWriter;29import java.io.IOException;30import java.util.ArrayList;31import java.util.Arrays;32import java.util.List;3334import jdk.testlibrary.Asserts;35import jdk.testlibrary.JDKToolLauncher;36import jdk.testlibrary.OutputAnalyzer;37import jdk.testlibrary.Utils;3839/**40* The helper class for running jps utility and verifying output from it41*/42public final class JpsHelper {4344/**45* Helper class for handling jps arguments46*/47public enum JpsArg {48q,49l,50m,51v,52V;5354/**55* Generate all possible combinations of {@link JpsArg}56* (31 argument combinations and no arguments case)57*/58public static List<List<JpsArg>> generateCombinations() {59final int argCount = JpsArg.values().length;60// If there are more than 30 args this algorithm will overflow.61Asserts.assertLessThan(argCount, 31, "Too many args");6263List<List<JpsArg>> combinations = new ArrayList<>();64int combinationCount = (int) Math.pow(2, argCount);65for (int currCombo = 0; currCombo < combinationCount; ++currCombo) {66List<JpsArg> combination = new ArrayList<>();67for (int position = 0; position < argCount; ++position) {68int bit = 1 << position;69if ((bit & currCombo) != 0) {70combination.add(JpsArg.values()[position]);71}72}73combinations.add(combination);74}75return combinations;76}7778/**79* Return combination of {@link JpsArg} as a String array80*/81public static String[] asCmdArray(List<JpsArg> jpsArgs) {82List<String> list = new ArrayList<>();83for (JpsArg jpsArg : jpsArgs) {84list.add("-" + jpsArg.toString());85}86return list.toArray(new String[list.size()]);87}8889}9091/**92* VM arguments to start test application with93*/94public static final String[] VM_ARGS = {"-Xmx512m", "-XX:+PrintGCDetails"};95/**96* VM flag to start test application with97*/98public static final String VM_FLAG = "+DisableExplicitGC";99100private static File vmFlagsFile = null;101private static List<String> testVmArgs = null;102private static File manifestFile = null;103104/**105* Create a file containing VM_FLAG in the working directory106*/107public static File getVmFlagsFile() throws IOException {108if (vmFlagsFile == null) {109vmFlagsFile = new File("vmflags");110try (BufferedWriter output = new BufferedWriter(new FileWriter(vmFlagsFile))) {111output.write(VM_FLAG);112}113vmFlagsFile.deleteOnExit();114}115return vmFlagsFile;116}117118/**119* Return a list of VM arguments120*/121public static List<String> getVmArgs() throws IOException {122if (testVmArgs == null) {123testVmArgs = new ArrayList<>();124testVmArgs.addAll(Arrays.asList(VM_ARGS));125testVmArgs.add("-XX:Flags=" + getVmFlagsFile().getAbsolutePath());126}127return testVmArgs;128}129130/**131* Start jps utility without any arguments132*/133public static OutputAnalyzer jps() throws Exception {134return jps(null, null);135}136137/**138* Start jps utility with tool arguments139*/140public static OutputAnalyzer jps(String... toolArgs) throws Exception {141return jps(null, Arrays.asList(toolArgs));142}143144/**145* Start jps utility with VM args and tool arguments146*/147public static OutputAnalyzer jps(List<String> vmArgs, List<String> toolArgs) throws Exception {148JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jps");149if (vmArgs != null) {150for (String vmArg : vmArgs) {151launcher.addVMArg(vmArg);152}153}154if (toolArgs != null) {155for (String toolArg : toolArgs) {156launcher.addToolArg(toolArg);157}158}159160ProcessBuilder processBuilder = new ProcessBuilder(launcher.getCommand());161System.out.println(Arrays.toString(processBuilder.command().toArray()).replace(",", ""));162OutputAnalyzer output = new OutputAnalyzer(processBuilder.start());163System.out.println(output.getOutput());164165return output;166}167168/**169* Verify jps output contains pids and programs' name information.170* The function will discard any lines that come before the first line with pid.171* This can happen if the JVM outputs a warning message for some reason172* before running jps.173*174* The output can look like:175* 35536 Jps176* 35417 Main177* 31103 org.eclipse.equinox.launcher_1.3.0.v20120522-1813.jar178*/179public static void verifyJpsOutput(OutputAnalyzer output, String regex) throws Exception {180output.shouldHaveExitValue(0);181int matchedCount = output.shouldMatchByLineFrom(regex, regex);182assertGreaterThan(matchedCount , 0, "Found no lines matching pattern: " + regex);183}184185/**186* Compare jps output with a content in a file line by line187*/188public static void verifyOutputAgainstFile(OutputAnalyzer output) throws IOException {189String testSrc = System.getProperty("test.src", "?");190File file = new File(testSrc, "usage.out");191List<String> fileOutput = Utils.fileAsList(file);192List<String> outputAsLines = output.asLines();193assertTrue(outputAsLines.containsAll(fileOutput),194"The ouput should contain all content of " + file.getAbsolutePath());195}196197private static File getManifest(String className) throws IOException {198if (manifestFile == null) {199manifestFile = new File(className + ".mf");200try (BufferedWriter output = new BufferedWriter(new FileWriter(manifestFile))) {201output.write("Main-Class: " + className + Utils.NEW_LINE);202}203}204return manifestFile;205}206207/**208* Build a jar of test classes in runtime209*/210public static File buildJar(String className) throws Exception {211File jar = new File(className + ".jar");212213List<String> jarArgs = new ArrayList<>();214jarArgs.add("-cfm");215jarArgs.add(jar.getAbsolutePath());216File manifestFile = getManifest(className);217jarArgs.add(manifestFile.getAbsolutePath());218String testClassPath = System.getProperty("test.class.path", "?");219for (String path : testClassPath.split(File.pathSeparator)) {220jarArgs.add("-C");221jarArgs.add(path);222jarArgs.add(".");223}224225System.out.println("Running jar " + jarArgs.toString());226sun.tools.jar.Main jarTool = new sun.tools.jar.Main(System.out, System.err, "jar");227if (!jarTool.run(jarArgs.toArray(new String[jarArgs.size()]))) {228throw new Exception("jar failed: args=" + jarArgs.toString());229}230231manifestFile.delete();232jar.deleteOnExit();233234return jar;235}236237}238239240