Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/tools/jcmd/TestJcmdSanity.java
38840 views
1
/*
2
* Copyright (c) 2011, 2014, 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
24
import static jdk.testlibrary.Asserts.*;
25
26
import java.io.File;
27
import java.io.IOException;
28
import java.util.List;
29
30
import jdk.testlibrary.JcmdBase;
31
import jdk.testlibrary.OutputAnalyzer;
32
import jdk.testlibrary.ProcessTools;
33
import jdk.testlibrary.Utils;
34
35
/**
36
* Unit test for jcmd utility. The test will send different diagnostic command
37
* requests to the current java process.
38
*/
39
/*
40
* @test
41
* @bug 7104647 7154822
42
* @library /lib/testlibrary
43
* @run main TestJcmdSanity
44
*/
45
public class TestJcmdSanity {
46
47
private static final String TEST_SRC = System.getProperty("test.src").trim();
48
private static final String[] VM_ARGS = new String[] { "-XX:+UsePerfData" };
49
private static final String JCMD_COMMAND_REGEX = "(\\w|\\.)*";
50
private static final String PERF_COUNTER_REGEX = "(\\w|\\.)*\\=.*";
51
52
public static void main(String[] args) throws Exception {
53
testJcmdPidHelp();
54
testJcmdPidHelpHelp();
55
testJcmdPid_f();
56
testJcmdPidPerfCounterPrint();
57
testJcmdPidBigScript();
58
}
59
60
/**
61
* jcmd -J-XX:+UsePerfData pid help
62
*/
63
private static void testJcmdPidHelp() throws Exception {
64
OutputAnalyzer output = JcmdBase.jcmd(VM_ARGS,
65
new String[] {"help"});
66
output.shouldHaveExitValue(0);
67
output.shouldNotContain("Exception");
68
output.shouldContain(Integer.toString(ProcessTools.getProcessId()) + ":");
69
matchJcmdCommands(output);
70
output.shouldContain("For more information about a specific command use 'help <command>'.");
71
}
72
73
/**
74
* jcmd -J-XX:+UsePerfData pid help help
75
*/
76
private static void testJcmdPidHelpHelp() throws Exception {
77
OutputAnalyzer output = JcmdBase.jcmd(VM_ARGS,
78
new String[] {"help", "help"});
79
80
output.shouldHaveExitValue(0);
81
verifyOutputAgainstFile(output);
82
}
83
84
/**
85
* jcmd -J-XX:+UsePerfData pid PerfCounter.print
86
*/
87
private static void testJcmdPidPerfCounterPrint() throws Exception {
88
OutputAnalyzer output = JcmdBase.jcmd(VM_ARGS,
89
new String[] {"PerfCounter.print"});
90
91
output.shouldHaveExitValue(0);
92
matchPerfCounters(output);
93
}
94
95
/**
96
* jcmd -J-XX:+UsePerfData pid -f dcmd-script.txt
97
*/
98
private static void testJcmdPid_f() throws Exception {
99
File scrpitFile = new File(TEST_SRC, "dcmd-script.txt");
100
OutputAnalyzer output = JcmdBase.jcmd(VM_ARGS,
101
new String[] {"-f", scrpitFile.getAbsolutePath()});
102
103
output.shouldHaveExitValue(0);
104
verifyOutputAgainstFile(output);
105
}
106
107
/**
108
* Tests that it possible send a file over 1024 bytes large via jcmd -f.
109
*
110
* jcmd -J-XX:+UsePerfData pid -f dcmd-big-script.txt
111
*/
112
private static void testJcmdPidBigScript() throws Exception {
113
File scrpitFile = new File(TEST_SRC, "dcmd-big-script.txt");
114
OutputAnalyzer output = JcmdBase.jcmd(VM_ARGS,
115
new String[] {"-f", scrpitFile.getAbsolutePath()});
116
117
output.shouldHaveExitValue(0);
118
output.shouldNotContain("Exception");
119
output.shouldContain(System.getProperty("java.vm.name").trim());
120
}
121
122
/**
123
* Verifies the listed jcmd commands match a certain pattern.
124
*
125
* The output of the jcmd commands should look like:
126
* VM.uptime
127
* VM.flags
128
* VM.system_properties
129
*
130
* @param output The generated output from the jcmd.
131
* @throws Exception
132
*/
133
private static void matchJcmdCommands(OutputAnalyzer output) throws Exception {
134
int matchedCount = output.shouldMatchByLine(JCMD_COMMAND_REGEX,
135
"help",
136
JCMD_COMMAND_REGEX);
137
assertGreaterThan(matchedCount , 0,
138
"Found no lines matching pattern: " + JCMD_COMMAND_REGEX);
139
}
140
141
/**
142
* Verifies the generated output from the PerfCounter.print command
143
* matches a certain pattern.
144
*
145
* The output of perf counters should look like:
146
* java.property.java.vm.name="Java HotSpot(TM) 64-Bit Server VM"
147
* java.threads.daemon=7
148
* sun.rt.javaCommand="com.sun.javatest.regtest.MainWrapper /tmp/jtreg/jtreg-workdir/classes/sun/tools/jcmd/TestJcmdSanity.jta"
149
*
150
* @param output The generated output from the PerfCounter.print command.
151
* @throws Exception
152
*/
153
private static void matchPerfCounters(OutputAnalyzer output) throws Exception {
154
int matchedCount = output.shouldMatchByLineFrom(PERF_COUNTER_REGEX,
155
PERF_COUNTER_REGEX);
156
assertGreaterThan(matchedCount , 0,
157
"Found no lines matching pattern: " + PERF_COUNTER_REGEX);
158
}
159
160
private static void verifyOutputAgainstFile(OutputAnalyzer output) throws IOException {
161
File file = new File(TEST_SRC, "help_help.out");
162
List<String> fileOutput = Utils.fileAsList(file);
163
List<String> outputAsLines = output.asLines();
164
assertTrue(outputAsLines.containsAll(fileOutput),
165
"The ouput should contain all content of " + file.getAbsolutePath());
166
}
167
168
}
169
170