author | minqi |
Mon, 08 Dec 2014 00:25:10 -0800 | |
changeset 28103 | 7e1de7f8a5b9 |
parent 28098 | fbc3debb3d50 |
parent 27792 | 0c0ccfb397f7 |
child 28412 | adcb8be2a2b0 |
permissions | -rw-r--r-- |
25987 | 1 |
/* |
2 |
* Copyright (c) 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.assertGreaterThan; |
|
25 |
import static jdk.testlibrary.Asserts.assertTrue; |
|
26 |
||
27 |
import java.io.BufferedWriter; |
|
28 |
import java.io.File; |
|
29 |
import java.io.FileWriter; |
|
30 |
import java.io.IOException; |
|
31 |
import java.util.ArrayList; |
|
32 |
import java.util.Arrays; |
|
33 |
import java.util.List; |
|
34 |
||
35 |
import jdk.testlibrary.Asserts; |
|
36 |
import jdk.testlibrary.JDKToolLauncher; |
|
37 |
import jdk.testlibrary.OutputAnalyzer; |
|
38 |
import jdk.testlibrary.Utils; |
|
27189
b90845965ee9
8056143: interrupted java/lang/management/MemoryMXBean/LowMemoryTest.java leaves running process
jbachorik
parents:
25987
diff
changeset
|
39 |
import jdk.testlibrary.ProcessTools; |
25987 | 40 |
|
41 |
/** |
|
42 |
* The helper class for running jps utility and verifying output from it |
|
43 |
*/ |
|
44 |
public final class JpsHelper { |
|
45 |
||
46 |
/** |
|
47 |
* Helper class for handling jps arguments |
|
48 |
*/ |
|
49 |
public enum JpsArg { |
|
50 |
q, |
|
51 |
l, |
|
52 |
m, |
|
53 |
v, |
|
54 |
V; |
|
55 |
||
56 |
/** |
|
57 |
* Generate all possible combinations of {@link JpsArg} |
|
58 |
* (31 argument combinations and no arguments case) |
|
59 |
*/ |
|
60 |
public static List<List<JpsArg>> generateCombinations() { |
|
61 |
final int argCount = JpsArg.values().length; |
|
62 |
// If there are more than 30 args this algorithm will overflow. |
|
63 |
Asserts.assertLessThan(argCount, 31, "Too many args"); |
|
64 |
||
65 |
List<List<JpsArg>> combinations = new ArrayList<>(); |
|
66 |
int combinationCount = (int) Math.pow(2, argCount); |
|
67 |
for (int currCombo = 0; currCombo < combinationCount; ++currCombo) { |
|
68 |
List<JpsArg> combination = new ArrayList<>(); |
|
69 |
for (int position = 0; position < argCount; ++position) { |
|
70 |
int bit = 1 << position; |
|
71 |
if ((bit & currCombo) != 0) { |
|
72 |
combination.add(JpsArg.values()[position]); |
|
73 |
} |
|
74 |
} |
|
75 |
combinations.add(combination); |
|
76 |
} |
|
77 |
return combinations; |
|
78 |
} |
|
79 |
||
80 |
/** |
|
81 |
* Return combination of {@link JpsArg} as a String array |
|
82 |
*/ |
|
83 |
public static String[] asCmdArray(List<JpsArg> jpsArgs) { |
|
84 |
List<String> list = new ArrayList<>(); |
|
85 |
for (JpsArg jpsArg : jpsArgs) { |
|
86 |
list.add("-" + jpsArg.toString()); |
|
87 |
} |
|
88 |
return list.toArray(new String[list.size()]); |
|
89 |
} |
|
90 |
||
91 |
} |
|
92 |
||
93 |
/** |
|
94 |
* VM arguments to start test application with |
|
95 |
*/ |
|
27792
0c0ccfb397f7
8062536: [TESTBUG] Conflicting GC combinations in jdk tests
eistepan
parents:
27189
diff
changeset
|
96 |
public static final String[] VM_ARGS = {"-Xmx512m", "-XX:+PrintGCDetails"}; |
25987 | 97 |
/** |
98 |
* VM flag to start test application with |
|
99 |
*/ |
|
100 |
public static final String VM_FLAG = "+DisableExplicitGC"; |
|
101 |
||
102 |
private static File vmFlagsFile = null; |
|
103 |
private static List<String> testVmArgs = null; |
|
104 |
private static File manifestFile = null; |
|
105 |
||
106 |
/** |
|
107 |
* Create a file containing VM_FLAG in the working directory |
|
108 |
*/ |
|
109 |
public static File getVmFlagsFile() throws IOException { |
|
110 |
if (vmFlagsFile == null) { |
|
111 |
vmFlagsFile = new File("vmflags"); |
|
112 |
try (BufferedWriter output = new BufferedWriter(new FileWriter(vmFlagsFile))) { |
|
113 |
output.write(VM_FLAG); |
|
114 |
} |
|
115 |
vmFlagsFile.deleteOnExit(); |
|
116 |
} |
|
117 |
return vmFlagsFile; |
|
118 |
} |
|
119 |
||
120 |
/** |
|
121 |
* Return a list of VM arguments |
|
122 |
*/ |
|
123 |
public static List<String> getVmArgs() throws IOException { |
|
124 |
if (testVmArgs == null) { |
|
125 |
testVmArgs = new ArrayList<>(); |
|
126 |
testVmArgs.addAll(Arrays.asList(VM_ARGS)); |
|
127 |
testVmArgs.add("-XX:Flags=" + getVmFlagsFile().getAbsolutePath()); |
|
128 |
} |
|
129 |
return testVmArgs; |
|
130 |
} |
|
131 |
||
132 |
/** |
|
133 |
* Start jps utility without any arguments |
|
134 |
*/ |
|
135 |
public static OutputAnalyzer jps() throws Exception { |
|
136 |
return jps(null, null); |
|
137 |
} |
|
138 |
||
139 |
/** |
|
140 |
* Start jps utility with tool arguments |
|
141 |
*/ |
|
142 |
public static OutputAnalyzer jps(String... toolArgs) throws Exception { |
|
143 |
return jps(null, Arrays.asList(toolArgs)); |
|
144 |
} |
|
145 |
||
146 |
/** |
|
147 |
* Start jps utility with VM args and tool arguments |
|
148 |
*/ |
|
149 |
public static OutputAnalyzer jps(List<String> vmArgs, List<String> toolArgs) throws Exception { |
|
150 |
JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jps"); |
|
151 |
if (vmArgs != null) { |
|
152 |
for (String vmArg : vmArgs) { |
|
153 |
launcher.addVMArg(vmArg); |
|
154 |
} |
|
155 |
} |
|
156 |
if (toolArgs != null) { |
|
157 |
for (String toolArg : toolArgs) { |
|
158 |
launcher.addToolArg(toolArg); |
|
159 |
} |
|
160 |
} |
|
161 |
||
162 |
ProcessBuilder processBuilder = new ProcessBuilder(launcher.getCommand()); |
|
163 |
System.out.println(Arrays.toString(processBuilder.command().toArray()).replace(",", "")); |
|
27189
b90845965ee9
8056143: interrupted java/lang/management/MemoryMXBean/LowMemoryTest.java leaves running process
jbachorik
parents:
25987
diff
changeset
|
164 |
OutputAnalyzer output = ProcessTools.executeProcess(processBuilder); |
25987 | 165 |
System.out.println(output.getOutput()); |
166 |
||
167 |
return output; |
|
168 |
} |
|
169 |
||
170 |
/** |
|
28098
fbc3debb3d50
8066106: sun/tools/jps/TestJpsClass.java failed to remove stale attach pid file
ykantser
parents:
27189
diff
changeset
|
171 |
* Verify jps stdout contains only pids and programs' name information. |
fbc3debb3d50
8066106: sun/tools/jps/TestJpsClass.java failed to remove stale attach pid file
ykantser
parents:
27189
diff
changeset
|
172 |
* jps stderr may contain VM warning messages which will be ignored. |
25987 | 173 |
* |
174 |
* The output can look like: |
|
175 |
* 35536 Jps |
|
176 |
* 35417 Main |
|
177 |
* 31103 org.eclipse.equinox.launcher_1.3.0.v20120522-1813.jar |
|
178 |
*/ |
|
179 |
public static void verifyJpsOutput(OutputAnalyzer output, String regex) throws Exception { |
|
180 |
output.shouldHaveExitValue(0); |
|
28098
fbc3debb3d50
8066106: sun/tools/jps/TestJpsClass.java failed to remove stale attach pid file
ykantser
parents:
27189
diff
changeset
|
181 |
int matchedCount = output.stdoutShouldMatchByLine(regex); |
25987 | 182 |
assertGreaterThan(matchedCount , 0, "Found no lines matching pattern: " + regex); |
28098
fbc3debb3d50
8066106: sun/tools/jps/TestJpsClass.java failed to remove stale attach pid file
ykantser
parents:
27189
diff
changeset
|
183 |
output.stderrShouldNotMatch("[E|e]xception"); |
fbc3debb3d50
8066106: sun/tools/jps/TestJpsClass.java failed to remove stale attach pid file
ykantser
parents:
27189
diff
changeset
|
184 |
output.stderrShouldNotMatch("[E|e]rror"); |
25987 | 185 |
} |
186 |
||
187 |
/** |
|
188 |
* Compare jps output with a content in a file line by line |
|
189 |
*/ |
|
190 |
public static void verifyOutputAgainstFile(OutputAnalyzer output) throws IOException { |
|
191 |
String testSrc = System.getProperty("test.src", "?"); |
|
192 |
File file = new File(testSrc, "usage.out"); |
|
193 |
List<String> fileOutput = Utils.fileAsList(file); |
|
194 |
List<String> outputAsLines = output.asLines(); |
|
195 |
assertTrue(outputAsLines.containsAll(fileOutput), |
|
196 |
"The ouput should contain all content of " + file.getAbsolutePath()); |
|
197 |
} |
|
198 |
||
199 |
private static File getManifest(String className) throws IOException { |
|
200 |
if (manifestFile == null) { |
|
201 |
manifestFile = new File(className + ".mf"); |
|
202 |
try (BufferedWriter output = new BufferedWriter(new FileWriter(manifestFile))) { |
|
203 |
output.write("Main-Class: " + className + Utils.NEW_LINE); |
|
204 |
} |
|
205 |
} |
|
206 |
return manifestFile; |
|
207 |
} |
|
208 |
||
209 |
/** |
|
210 |
* Build a jar of test classes in runtime |
|
211 |
*/ |
|
212 |
public static File buildJar(String className) throws Exception { |
|
213 |
File jar = new File(className + ".jar"); |
|
214 |
||
215 |
List<String> jarArgs = new ArrayList<>(); |
|
216 |
jarArgs.add("-cfm"); |
|
217 |
jarArgs.add(jar.getAbsolutePath()); |
|
218 |
File manifestFile = getManifest(className); |
|
219 |
jarArgs.add(manifestFile.getAbsolutePath()); |
|
220 |
String testClassPath = System.getProperty("test.class.path", "?"); |
|
221 |
for (String path : testClassPath.split(File.pathSeparator)) { |
|
222 |
jarArgs.add("-C"); |
|
223 |
jarArgs.add(path); |
|
224 |
jarArgs.add("."); |
|
225 |
} |
|
226 |
||
227 |
System.out.println("Running jar " + jarArgs.toString()); |
|
228 |
sun.tools.jar.Main jarTool = new sun.tools.jar.Main(System.out, System.err, "jar"); |
|
229 |
if (!jarTool.run(jarArgs.toArray(new String[jarArgs.size()]))) { |
|
230 |
throw new Exception("jar failed: args=" + jarArgs.toString()); |
|
231 |
} |
|
232 |
||
233 |
manifestFile.delete(); |
|
234 |
jar.deleteOnExit(); |
|
235 |
||
236 |
return jar; |
|
237 |
} |
|
238 |
||
239 |
} |