1/*
2 * Copyright (c) 2005, 2017, 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.util.ArrayList;
25import java.util.Arrays;
26import java.util.List;
27import java.util.regex.Matcher;
28import java.util.regex.Pattern;
29import java.io.IOException;
30
31import jdk.test.lib.JDKToolLauncher;
32import jdk.test.lib.process.OutputAnalyzer;
33import jdk.test.lib.process.ProcessTools;
34import jdk.test.lib.apps.LingeredApp;
35
36/*
37 * @test
38 * @summary Unit test for jinfo utility
39 *
40 * @library /test/lib
41 * @modules java.base/jdk.internal.misc
42 *          java.management
43 *          jdk.jcmd
44 *
45 * @run main JInfoTest
46 */
47public class JInfoTest {
48
49    private static ProcessBuilder processBuilder = new ProcessBuilder();
50
51    public static void main(String[] args) throws Exception {
52        classNameMatch();
53        setMultipleFlags();
54        setFlag();
55    }
56
57    private static void setFlag() throws Exception {
58        System.out.println("#### setFlag ####");
59        LingeredApp app1 = new JInfoTestLingeredApp();
60        LingeredApp app2 = new JInfoTestLingeredApp();
61        try {
62            ArrayList<String> params = new ArrayList<String>();
63            LingeredApp.startApp(params, app1);
64            LingeredApp.startApp(params, app2);
65            OutputAnalyzer output = jinfo("-flag", "MinHeapFreeRatio=1", "JInfoTestLingeredApp");
66            output.shouldHaveExitValue(0);
67            output = jinfo("-flag", "MinHeapFreeRatio", "JInfoTestLingeredApp");
68            output.shouldHaveExitValue(0);
69            documentMatch(output.getStdout(), ".*MinHeapFreeRatio=1.*MinHeapFreeRatio=1.*");
70        } finally {
71            JInfoTestLingeredApp.stopApp(app1);
72            JInfoTestLingeredApp.stopApp(app2);
73        }
74    }
75
76    private static void setMultipleFlags() throws Exception {
77        System.out.println("#### setMultipleFlags ####");
78        OutputAnalyzer output = jinfo("-sysprops", "-flag", "MinHeapFreeRatio=1", "-flags", "JInfoTestLingeredApp");
79        output.shouldHaveExitValue(1);
80    }
81
82    private static void classNameMatch() throws Exception {
83        System.out.println("#### classNameMatch ####");
84        LingeredApp app1 = new JInfoTestLingeredApp();
85        LingeredApp app2 = new JInfoTestLingeredApp();
86        try {
87            ArrayList<String> params = new ArrayList<String>();
88            LingeredApp.startApp(params, app1);
89            LingeredApp.startApp(params, app2);
90            OutputAnalyzer output = jinfo("JInfoTestLingeredApp");
91            output.shouldHaveExitValue(0);
92            // "Runtime Environment" written once per proc
93            documentMatch(output.getStdout(), ".*Runtime Environment.*Runtime Environment.*");
94        } finally {
95            JInfoTestLingeredApp.stopApp(app1);
96            JInfoTestLingeredApp.stopApp(app2);
97        }
98    }
99
100    private static void documentMatch(String data, String pattern){
101        Matcher matcher = Pattern.compile(pattern, Pattern.DOTALL).matcher(data);
102        if (!matcher.find()) {
103            throw new RuntimeException("'" + pattern + "' missing from stdout \n");
104        }
105    }
106
107    private static OutputAnalyzer jinfo(String... toolArgs) throws Exception {
108        JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jinfo");
109        if (toolArgs != null) {
110            for (String toolArg : toolArgs) {
111                launcher.addToolArg(toolArg);
112            }
113        }
114
115        processBuilder.command(launcher.getCommand());
116        OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
117
118        return output;
119    }
120}
121
122// Sometime there is LingeredApp's from other test still around
123class JInfoTestLingeredApp extends LingeredApp {
124}
125
126