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 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
|
/*
* Copyright (c) 1998, 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 java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Iterator;
import com.sun.javatest.Harness;
import com.sun.javatest.InterviewParameters;
import com.sun.javatest.Parameters;
import com.sun.javatest.Status;
import com.sun.javatest.TestDescription;
import com.sun.javatest.TestFilter;
import com.sun.javatest.TestFinder;
import com.sun.javatest.TestResult;
import com.sun.javatest.TestResultTable;
import com.sun.javatest.TestSuite;
import com.sun.javatest.WorkDirectory;
import com.sun.javatest.regtest.config.RegressionTestFinder;
import com.sun.javatest.regtest.config.RegressionTestSuite;
import com.sun.javatest.regtest.config.RegressionParameters;
import com.sun.javatest.regtest.exec.RegressionScript;
public class Basic
{
public static void main(String[] args) {
try {
Basic basic = new Basic(args);
System.exit(0);
} catch (Throwable t) {
t.printStackTrace();
System.exit(1);
}
}
public Basic(String[] args) {
if (args.length != 6) {
failed("wrong number of args, expected 6, got " + args.length);
}
int argc = 0;
String testSuitePath = args[argc++];
String reportDir = args[argc++];
String workDirPath = args[argc++];
String jdkPath = args[argc++];
String envVars = args[argc++];
String modeOpt = args[argc++];
System.out.println("testsuite: " + testSuitePath);
System.out.println("reportDir: " + reportDir);
System.out.println("workDir: " + workDirPath);
System.out.println("jdk: " + jdkPath);
System.out.println("envVars: " + envVars);
System.out.println("mode: " + modeOpt);
try {
testSuite = new RegressionTestSuite(new File(testSuitePath),
new TestFinder.ErrorHandler() {
public void error(String msg) {
System.err.println("Error: " + msg);
}
});
File wd = new File(workDirPath);
if (WorkDirectory.isWorkDirectory(wd))
workDir = WorkDirectory.open(wd, testSuite);
else
workDir = WorkDirectory.convert(wd, testSuite);
String[] regtest_args = {
"-a",
"-e", envVars,
"-w", workDir.getPath(),
"-r", reportDir,
"-jdk", jdkPath,
"-observer", Basic.Observer.class.getName(),
"-observerPath", System.getProperty("java.class.path"),
modeOpt,
testSuite.getPath()
};
System.out.println(INFO_INITIAL_RUN);
System.setProperty("javatest.regtest.args", "old");
com.sun.javatest.regtest.Main m = new com.sun.javatest.regtest.Main();
m.run(regtest_args);
System.out.println(INFO_OK + "...batch execution");
setExpectedTestStats();
System.out.println("checking selection according to previous state");
for (int i = 0; i < statusList.length; i++)
checkPreviousStateSelection(statusList[i]);
System.out.println(INFO_OK);
System.out.println("checking selection according to verb");
for (int i = 0; i < actionList.length; i++)
checkAction(actionList[i]);
System.out.println(INFO_OK);
} catch (Harness.Fault e) {
failed(ERR_HARNESS_RUN_PROB, e);
} catch (InterruptedException e) {
failed(ERR_HARNESS_INTR);
} catch (WorkDirectory.Fault e) {
failed(ERR_BAD_VAL, e);
} catch (FileNotFoundException e) {
failed(ERR_BAD_VAL, e);
} catch (TestSuite.Fault f) {
failed(ERR_SUITE, f);
} catch (com.sun.javatest.regtest.BadArgs e) {
failed(ERR_BAD_ARGS, e);
} catch (com.sun.javatest.regtest.Main.Fault e) {
failed(ERR_FAULT, e);
}
}
//----------internal methods------------------------------------------------
/**
* When a new test is added to the basic test suite, in general, the only
* modifications that should be needed will occur in this method.
*/
private void setExpectedTestStats() {
// status counts
int numPassed = 0, numFailed = 0, numError = 0;
// action counts
int numApplet = 0, numBuild = 0, numClean = 0, numCompile = 0,
numIgnore = 0, numMain = 0, numShell = 0, numMisc = 0;
int numJUnit = 0;
int numTestNG = 0;
int numDriver = 0;
// applet
numPassed += 2; numFailed += 4; numError += 11;
numApplet += 17;
// build
numPassed += 6; numFailed += 0; numError += 5;
numBuild = 10;
numClean += 7;
numMain += 1;
// clean
numPassed += 4; numFailed += 0; numError += 6;
numClean += 10;
numShell += 1;
// compile
numPassed += 5; numFailed += 4; numError += 12;
numClean += 2;
numCompile += 21;
numMain += 1;
// ignore
numPassed += 0; numFailed += 0; numError += 2;
numIgnore += 2;
// junit
numPassed += 6; numFailed += 6; numError += 8 + 8 /*Pass/Fail, badarg*/;
numJUnit += 28;
// TestNG
numPassed += 3; numFailed += 2; numError += 2;
numTestNG += 5;
numMain += 2;
// main
numPassed += 12; numFailed += 19; numError += 12;
numMain += 43;
// driver
numPassed += 3; numFailed += 4; numError += 10;
numDriver += 17;
// shell
numPassed += 7; numFailed += 5; numError += 3;
numShell += 15;
// misc
numPassed += 41; numFailed += 0; numError += 8;
numMain += 49;
// tempFiles
numPassed += 3;
numMain += 2;
numShell += 1;
actionTable.put("applet", Integer.valueOf(numApplet));
actionTable.put("build", Integer.valueOf(numBuild));
actionTable.put("clean", Integer.valueOf(numClean));
actionTable.put("compile", Integer.valueOf(numCompile));
actionTable.put("driver", Integer.valueOf(numDriver));
actionTable.put("ignore", Integer.valueOf(numIgnore));
actionTable.put("main", Integer.valueOf(numMain));
actionTable.put("junit", Integer.valueOf(numJUnit));
actionTable.put("testng", Integer.valueOf(numTestNG));
actionTable.put("shell", Integer.valueOf(numShell));
actionTable.put("misc", Integer.valueOf(numMisc));
statusTable.put("passed", Integer.valueOf(numPassed));
statusTable.put("failed", Integer.valueOf(numFailed));
statusTable.put("error", Integer.valueOf(numError));
}
private void checkPreviousStateSelection(String s) {
int expected = statusTable.get(s);
int state;
String prefix;
if (s.equals("passed")) {
state = Status.PASSED;
prefix = "Passed: ";
} else if (s.equals("failed")) {
state = Status.FAILED;
prefix = "Failed: ";
} else { // if (s.equals("Error:"))
state = Status.ERROR;
prefix = "Error: ";
}
try {
RegressionParameters params = setParameters();
boolean[] b = new boolean[Status.NUM_STATES];
b[state] = true;
params.setPriorStatusValues(b);
int found = 0;
Iterator<TestResult> it = createTestQueue(params);
while (it.hasNext()) {
TestResult tr = it.next();
TestDescription td = tr.getDescription();
if (!td.getTitle().startsWith(prefix)) {
System.err.println(REP_STATE + state);
System.err.println(REP_FOUND_TEST + tr.getTestName());
System.err.println(REP_TITLE + td.getTitle());
System.err.println(REP_EXP_TITLE + prefix);
failed(ERR_REP_UEXP_TEST);
}
found++;
}
if (found != expected) {
System.err.println(REP_STATE + state + " (" + s + ")");
System.err.println(REP_EXP_NUM_TEST + expected);
System.err.println(REP_FOUND + found);
failed(ERR_REP_WRONG_NUM);
}
}
catch (FileNotFoundException e) {
failed(ERR_HARNESS_RUN_PROB, e);
}
catch (TestSuite.Fault e) {
failed(ERR_HARNESS_RUN_PROB, e);
}
catch (TestResult.Fault f) {
failed(ERR_BAD_TR, f);
}
catch (TestResultTable.Fault f) {
failed(ERR_BAD_FILES, f);
}
System.out.println(" " + s + ": " + expected);
}
private void checkAction(String action) {
int expected = actionTable.get(action);
try {
Iterator<TestResult> it = createTestQueue(setParameters());
int found = 0;
TestDescription td;
while (it.hasNext()) {
td = it.next().getDescription();
String actions = td.getParameter("run");
int pos = 0;
// a td may have more than one of the target action
while ((pos = actions.indexOf(action, pos)) != -1) {
found++;
if (found > expected) {
System.err.println(REP_ACTION + action);
System.err.println(REP_FOUND_TEST + td.getRootRelativeFile());
System.err.println(ERR_REP_UEXP_TEST);
}
pos += actions.length();
}
}
if (found != expected) {
System.err.println(REP_ACTION + action);
System.err.println(REP_EXP_NUM_TEST + expected);
System.err.println(REP_FOUND + found);
failed(ERR_REP_WRONG_NUM);
}
}
catch (FileNotFoundException e) {
failed(ERR_HARNESS_RUN_PROB, e);
}
catch (TestSuite.Fault e) {
failed(ERR_HARNESS_RUN_PROB, e);
}
catch (TestResult.Fault f) {
failed(ERR_BAD_TR, f);
}
catch (TestResultTable.Fault f) {
failed(ERR_BAD_FILES, f);
}
System.out.println(" " + action + ": " + expected);
}
private void failed(String s, Exception e) {
e.printStackTrace();
System.err.println(s + e);
throw new Error(s);
}
private void failed(String s) {
System.err.println(s);
throw new Error(s);
}
private RegressionParameters setParameters() {
RegressionParameters rp = null;
try {
rp = new RegressionParameters("basic", testSuite, System.err::println);
rp.setWorkDirectory(workDir);
rp.setTests((String[])null);
rp.setExcludeLists(new File[0]);
rp.setKeywordsExpr("!manual");
rp.setPriorStatusValues(null);
}
catch (RegressionParameters.Fault e) {
failed(ERR_BAD_PARAMS, e);
};
return rp;
}
/**
* Returns an iterator containing the selected TestResults.
*/
private Iterator<TestResult> createTestQueue(InterviewParameters p)
throws FileNotFoundException, TestSuite.Fault, TestResultTable.Fault
{
TestResultTable trt = p.getWorkDirectory().getTestResultTable();
if (trt.getTestFinder() == null)
trt.setTestFinder(p.getTestSuite().getTestFinder());
File[] tests = stringsToFiles(p.getTests());
TestFilter[] filters = p.getFilters();
trt.waitUntilReady(); // required for samevm mode
@SuppressWarnings("unchecked")
Iterator<TestResult> iter = trt.getIterator(tests, filters);
return iter;
}
private static File[] stringsToFiles(String[] tests) {
if (tests == null)
return null;
File[] files = new File[tests.length];
for (int i = 0; i < tests.length; i++)
files[i] = new File(tests[i]);
return files;
}
//--------------------------------------------------------------------------
public static class Observer implements Harness.Observer
{
public void startingTestRun(Parameters params) { }
public void startingTest(TestResult tr) { }
public void finishedTest(TestResult tr) { }
public void stoppingTestRun() { }
@SuppressWarnings("deprecation")
public void finishedTesting() { }
public void finishedTestRun(boolean allOK) { }
public void error(String s) {
System.err.println("Error: " + s);
}
}
//----------statics---------------------------------------------------------
private static final String
INFO_INITIAL_RUN = "running tests to set status to known state",
INFO_OK = "ok",
ERR_HARNESS_RUN_PROB = "problem running harness: ",
ERR_HARNESS_INTR = "harness interrupted: ",
ERR_BAD_FILES = "bad initial files: ",
ERR_BAD_VAL = "bad value: ",
ERR_BAD_TR = "bad test result: ",
ERR_BAD_ARGS = "bad arguments: ",
ERR_BAD_PARAMS = "bad parameters: ",
ERR_FAULT = "fault: ",
ERR_IO_ERROR = "IOException: ",
ERR_REP_UEXP_TEST = "unexpected test found",
ERR_REP_WRONG_NUM = "wrong number of tests found",
ERR_SUITE = "problem initializing testsuite: ",
REP_STATE = "state: ",
REP_FOUND_TEST = "found test: ",
REP_TITLE = "title: ",
REP_EXP_TITLE = "expected prefix of title: ",
REP_EXP_NUM_TEST = "expected number of tests: ",
REP_FOUND = "found: ",
REP_ACTION = "action: ";
private static final String[] actionList = {
"applet", "build", "clean", "compile", "driver", "ignore", "junit", "testng", "main", "shell", "misc"
};
private static final String[] statusList = {
"passed", "failed", "error"
};
private static final String FILESEP = System.getProperty("file.separator");
//----------member variables------------------------------------------------
private RegressionTestSuite testSuite;
private WorkDirectory workDir;
private Hashtable<String,Integer> statusTable = new Hashtable<String,Integer>();
private Hashtable<String,Integer> actionTable = new Hashtable<String,Integer>();
}
|