1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
|
/*
* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
import jdk.test.lib.Utils;
import jdk.test.lib.BuildHelper;
import jdk.test.lib.JDKToolFinder;
import jdk.test.lib.Platform;
import jdk.test.lib.cds.CDSOptions;
import jdk.test.lib.cds.CDSTestUtils;
import jdk.test.lib.cds.CDSTestUtils.Result;
import jdk.test.lib.process.ProcessTools;
import jdk.test.lib.process.OutputAnalyzer;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Date;
/**
* This is a test utility class for common AppCDS test functionality.
*
* Various methods use (String ...) for passing VM options. Note that the order
* of the VM options are important in certain cases. Many methods take arguments like
*
* (String prefix[], String suffix[], String... opts)
*
* Note that the order of the VM options is:
*
* prefix + opts + suffix
*/
public class TestCommon extends CDSTestUtils {
private static final String JSA_FILE_PREFIX = System.getProperty("user.dir") +
File.separator + "appcds-";
private static final SimpleDateFormat timeStampFormat =
new SimpleDateFormat("HH'h'mm'm'ss's'SSS");
private static final String timeoutFactor =
System.getProperty("test.timeout.factor", "1.0");
private static String currentArchiveName;
// Call this method to start new archive with new unique name
public static void startNewArchiveName() {
deletePriorArchives();
currentArchiveName = JSA_FILE_PREFIX +
timeStampFormat.format(new Date()) + ".jsa";
}
// Call this method to get current archive name
public static String getCurrentArchiveName() {
return currentArchiveName;
}
public static void setCurrentArchiveName(String archiveName) {
currentArchiveName = archiveName;
}
// Attempt to clean old archives to preserve space
// Archives are large artifacts (20Mb or more), and much larger than
// most other artifacts created in jtreg testing.
// Therefore it is a good idea to clean the old archives when they are not needed.
// In most cases the deletion attempt will succeed; on rare occasion the
// delete operation will fail since the system or VM process still holds a handle
// to the file; in such cases the File.delete() operation will silently fail, w/o
// throwing an exception, thus allowing testing to continue.
public static void deletePriorArchives() {
File dir = new File(System.getProperty("user.dir"));
String files[] = dir.list();
for (String name : files) {
if (name.startsWith("appcds-") && name.endsWith(".jsa")) {
if (!(new File(dir, name)).delete())
System.out.println("deletePriorArchives(): delete failed for file " + name);
}
}
}
// Create AppCDS archive using most common args - convenience method
// Legacy name preserved for compatibility
public static OutputAnalyzer dump(String appJar, String appClasses[],
String... suffix) throws Exception {
return createArchive(appJar, appClasses, suffix);
}
// Create AppCDS archive using most common args - convenience method
public static OutputAnalyzer createArchive(String appJar, String appClasses[],
String... suffix) throws Exception {
AppCDSOptions opts = (new AppCDSOptions()).setAppJar(appJar)
.setAppClasses(appClasses);
opts.addSuffix(suffix);
return createArchive(opts);
}
public static String[] makeCommandLineForAppCDS(String... args) throws Exception {
return args;
}
// Create AppCDS archive using appcds options
public static OutputAnalyzer createArchive(AppCDSOptions opts)
throws Exception {
ArrayList<String> cmd = new ArrayList<String>();
File classList = makeClassList(opts.appClasses);
startNewArchiveName();
for (String p : opts.prefix) cmd.add(p);
if (opts.appJar != null) {
cmd.add("-cp");
cmd.add(opts.appJar);
} else {
cmd.add("-cp");
cmd.add("\"\"");
}
cmd.add("-Xshare:dump");
cmd.add("-Xlog:cds,cds+hashtables");
cmd.add("-XX:ExtraSharedClassListFile=" + classList.getPath());
if (opts.archiveName == null)
opts.archiveName = getCurrentArchiveName();
cmd.add("-XX:SharedArchiveFile=" + opts.archiveName);
for (String s : opts.suffix) cmd.add(s);
String[] cmdLine = cmd.toArray(new String[cmd.size()]);
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true, makeCommandLineForAppCDS(cmdLine));
return executeAndLog(pb, "dump");
}
// Execute JVM using AppCDS archive with specified AppCDSOptions
public static OutputAnalyzer runWithArchive(AppCDSOptions opts)
throws Exception {
ArrayList<String> cmd = new ArrayList<String>();
for (String p : opts.prefix) cmd.add(p);
cmd.add("-Xshare:" + opts.xShareMode);
cmd.add("-showversion");
cmd.add("-XX:SharedArchiveFile=" + getCurrentArchiveName());
cmd.add("-Dtest.timeout.factor=" + timeoutFactor);
if (opts.appJar != null) {
cmd.add("-cp");
cmd.add(opts.appJar);
}
for (String s : opts.suffix) cmd.add(s);
String[] cmdLine = cmd.toArray(new String[cmd.size()]);
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true, makeCommandLineForAppCDS(cmdLine));
return executeAndLog(pb, "exec");
}
public static OutputAnalyzer execCommon(String... suffix) throws Exception {
AppCDSOptions opts = (new AppCDSOptions());
opts.addSuffix(suffix);
return runWithArchive(opts);
}
// This is the new API for running a Java process with CDS enabled.
// See comments in the CDSTestUtils.Result class for how to use this method.
public static Result run(String... suffix) throws Exception {
AppCDSOptions opts = (new AppCDSOptions());
opts.addSuffix(suffix);
return new Result(opts, runWithArchive(opts));
}
public static OutputAnalyzer exec(String appJar, String... suffix) throws Exception {
AppCDSOptions opts = (new AppCDSOptions()).setAppJar(appJar);
opts.addSuffix(suffix);
return runWithArchive(opts);
}
public static Result runWithModules(String prefix[], String upgrademodulepath, String modulepath,
String mid, String... testClassArgs) throws Exception {
AppCDSOptions opts = makeModuleOptions(prefix, upgrademodulepath, modulepath,
mid, testClassArgs);
return new Result(opts, runWithArchive(opts));
}
public static OutputAnalyzer execAuto(String... suffix) throws Exception {
AppCDSOptions opts = (new AppCDSOptions());
opts.addSuffix(suffix).setXShareMode("auto");
return runWithArchive(opts);
}
public static OutputAnalyzer execOff(String... suffix) throws Exception {
AppCDSOptions opts = (new AppCDSOptions());
opts.addSuffix(suffix).setXShareMode("off");
return runWithArchive(opts);
}
private static AppCDSOptions makeModuleOptions(String prefix[], String upgrademodulepath, String modulepath,
String mid, String testClassArgs[]) {
AppCDSOptions opts = (new AppCDSOptions());
opts.addPrefix(prefix);
if (upgrademodulepath == null) {
opts.addSuffix("-p", modulepath, "-m", mid);
} else {
opts.addSuffix("--upgrade-module-path", upgrademodulepath,
"-p", modulepath, "-m", mid);
}
opts.addSuffix(testClassArgs);
return opts;
}
public static OutputAnalyzer execModule(String prefix[], String upgrademodulepath, String modulepath,
String mid, String... testClassArgs)
throws Exception {
AppCDSOptions opts = makeModuleOptions(prefix, upgrademodulepath, modulepath,
mid, testClassArgs);
return runWithArchive(opts);
}
// A common operation: dump, then check results
public static OutputAnalyzer testDump(String appJar, String appClasses[],
String... suffix) throws Exception {
OutputAnalyzer output = dump(appJar, appClasses, suffix);
output.shouldContain("Loading classes to share");
output.shouldHaveExitValue(0);
return output;
}
/**
* Simple test -- dump and execute appJar with the given appClasses in classlist.
*/
public static OutputAnalyzer test(String appJar, String appClasses[], String... args)
throws Exception {
testDump(appJar, appClasses);
OutputAnalyzer output = exec(appJar, args);
return checkExec(output);
}
public static OutputAnalyzer checkExecReturn(OutputAnalyzer output, int ret,
boolean checkContain, String... matches) throws Exception {
try {
for (String s : matches) {
if (checkContain) {
output.shouldContain(s);
} else {
output.shouldNotContain(s);
}
}
output.shouldHaveExitValue(ret);
} catch (Exception e) {
checkCommonExecExceptions(output, e);
}
return output;
}
// Convenience concatenation utils
public static String[] list(String ...args) {
return args;
}
public static String[] list(String arg, int count) {
ArrayList<String> stringList = new ArrayList<String>();
for (int i = 0; i < count; i++) {
stringList.add(arg);
}
String outputArray[] = stringList.toArray(new String[stringList.size()]);
return outputArray;
}
public static String[] concat(String... args) {
return list(args);
}
public static String[] concat(String prefix[], String... extra) {
ArrayList<String> list = new ArrayList<String>();
for (String s : prefix) {
list.add(s);
}
for (String s : extra) {
list.add(s);
}
return list.toArray(new String[list.size()]);
}
// ===================== Concatenate paths
public static String concatPaths(String... paths) {
String prefix = "";
String s = "";
for (String p : paths) {
s += prefix;
s += p;
prefix = File.pathSeparator;
}
return s;
}
public static String getTestJar(String jar) {
File jarFile = CDSTestUtils.getTestArtifact(jar, true);
if (!jarFile.isFile()) {
throw new RuntimeException("Not a regular file: " + jarFile.getPath());
}
return jarFile.getPath();
}
public static String getTestDir(String d) {
File dirFile = CDSTestUtils.getTestArtifact(d, true);
if (!dirFile.isDirectory()) {
throw new RuntimeException("Not a directory: " + dirFile.getPath());
}
return dirFile.getPath();
}
public static boolean checkOutputStrings(String outputString1,
String outputString2,
String split_regex) {
String[] sa1 = outputString1.split(split_regex);
String[] sa2 = outputString2.split(split_regex);
Arrays.sort(sa1);
Arrays.sort(sa2);
int i = 0;
for (String s : sa1) {
if (!s.equals(sa2[i])) {
throw new RuntimeException(s + " is different from " + sa2[i]);
}
i ++;
}
return true;
}
}
|