Path: blob/master/test/jdk/sun/tools/jinfo/JInfoTest.java
41149 views
/*1* Copyright (c) 2005, 2020, 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.ArrayList;24import java.util.Arrays;25import java.util.List;26import java.util.regex.Matcher;27import java.util.regex.Pattern;28import java.io.IOException;2930import jdk.test.lib.JDKToolLauncher;31import jdk.test.lib.Utils;32import jdk.test.lib.process.OutputAnalyzer;33import jdk.test.lib.process.ProcessTools;34import jdk.test.lib.apps.LingeredApp;3536/*37* @test38* @summary Unit test for jinfo utility39*40* @library /test/lib41* @modules java.base/jdk.internal.misc42* java.management43* jdk.jcmd44*45* @run main JInfoTest46*/47public class JInfoTest {4849private static ProcessBuilder processBuilder = new ProcessBuilder();5051public static void main(String[] args) throws Exception {52classNameMatch();53setMultipleFlags();54setFlag();55}5657private static void setFlag() throws Exception {58System.out.println("#### setFlag ####");59LingeredApp app1 = new JInfoTestLingeredApp();60LingeredApp app2 = new JInfoTestLingeredApp();61try {62String[] params = new String[0];;63LingeredApp.startAppExactJvmOpts(app1, params);64LingeredApp.startAppExactJvmOpts(app2, params);65OutputAnalyzer output = jinfo("-flag", "MinHeapFreeRatio=1", "JInfoTestLingeredApp");66output.shouldHaveExitValue(0);67output = jinfo("-flag", "MinHeapFreeRatio", "JInfoTestLingeredApp");68output.shouldHaveExitValue(0);69documentMatch(output.getStdout(), ".*MinHeapFreeRatio=1.*MinHeapFreeRatio=1.*");70} finally {71// LingeredApp.stopApp can throw an exception72try {73JInfoTestLingeredApp.stopApp(app1);74} finally {75JInfoTestLingeredApp.stopApp(app2);76}77}78}7980private static void setMultipleFlags() throws Exception {81System.out.println("#### setMultipleFlags ####");82OutputAnalyzer output = jinfo("-sysprops", "-flag", "MinHeapFreeRatio=1", "-flags", "JInfoTestLingeredApp");83output.shouldHaveExitValue(1);84}8586private static void classNameMatch() throws Exception {87System.out.println("#### classNameMatch ####");88LingeredApp app1 = new JInfoTestLingeredApp();89LingeredApp app2 = new JInfoTestLingeredApp();90try {91String[] params = new String[0];92LingeredApp.startAppExactJvmOpts(app1, params);93LingeredApp.startAppExactJvmOpts(app2, params);94OutputAnalyzer output = jinfo("JInfoTestLingeredApp");95output.shouldHaveExitValue(0);96// "Runtime Environment" written once per proc97documentMatch(output.getStdout(), ".*Runtime Environment.*Runtime Environment.*");98} finally {99// LingeredApp.stopApp can throw an exception100try {101JInfoTestLingeredApp.stopApp(app1);102} finally {103JInfoTestLingeredApp.stopApp(app2);104}105}106}107108private static void documentMatch(String data, String pattern){109Matcher matcher = Pattern.compile(pattern, Pattern.DOTALL).matcher(data);110if (!matcher.find()) {111throw new RuntimeException("'" + pattern + "' missing from stdout \n");112}113}114115private static OutputAnalyzer jinfo(String... toolArgs) throws Exception {116JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jinfo");117launcher.addVMArgs(Utils.getTestJavaOpts());118if (toolArgs != null) {119for (String toolArg : toolArgs) {120launcher.addToolArg(toolArg);121}122}123124processBuilder.command(launcher.getCommand());125OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);126127return output;128}129}130131// Sometime there is LingeredApp's from other test still around132class JInfoTestLingeredApp extends LingeredApp {133}134135136