482 return output;
483 }
484
485
486 // get the file object for the test artifact
487 public static File getTestArtifact(String name, boolean checkExistence) {
488 File dir = new File(System.getProperty("test.classes", "."));
489 File file = new File(dir, name);
490
491 if (checkExistence && !file.exists()) {
492 throw new RuntimeException("Cannot find " + file.getPath());
493 }
494
495 return file;
496 }
497
498
499 // create file containing the specified class list
500 public static File makeClassList(String classes[])
501 throws Exception {
502 return makeClassList(getTestName() + "-", classes);
503 }
504
505 // create file containing the specified class list
506 public static File makeClassList(String testCaseName, String classes[])
507 throws Exception {
508
509 File classList = getTestArtifact(testCaseName + "test.classlist", false);
510 FileOutputStream fos = new FileOutputStream(classList);
511 PrintStream ps = new PrintStream(fos);
512
513 addToClassList(ps, classes);
514
515 ps.close();
516 fos.close();
517
518 return classList;
519 }
520
521
522 public static void addToClassList(PrintStream ps, String classes[])
523 throws IOException
524 {
525 if (classes != null) {
526 for (String s : classes) {
527 ps.println(s);
528 }
529 }
530 }
531
532
533 // Optimization for getting a test name.
534 // Test name does not change during execution of the test,
535 // but getTestName() uses stack walking hence it is expensive.
536 // Therefore cache it and reuse it.
537 private static String testName;
538 public static String getTestName() {
539 if (testName == null) {
540 testName = Utils.getTestName();
541 }
542 return testName;
543 }
544
545 private static final SimpleDateFormat timeStampFormat =
546 new SimpleDateFormat("HH'h'mm'm'ss's'SSS");
547
548 private static String defaultArchiveName;
549
550 // Call this method to start new archive with new unique name
551 public static void startNewArchiveName() {
552 defaultArchiveName = getTestName() +
553 timeStampFormat.format(new Date()) + ".jsa";
554 }
555
556 public static String getDefaultArchiveName() {
557 return defaultArchiveName;
558 }
559
560
561 // ===================== FILE ACCESS convenience methods
562 public static File getOutputFile(String name) {
563 File dir = new File(System.getProperty("test.classes", "."));
564 return new File(dir, getTestName() + "-" + name);
565 }
566
567
568 public static File getOutputSourceFile(String name) {
569 File dir = new File(System.getProperty("test.classes", "."));
570 return new File(dir, name);
571 }
572
573
574 public static File getSourceFile(String name) {
575 File dir = new File(System.getProperty("test.src", "."));
576 return new File(dir, name);
577 }
578
579
580 // ============================= Logging
581 public static OutputAnalyzer executeAndLog(ProcessBuilder pb, String logName) throws Exception {
582 long started = System.currentTimeMillis();
583 OutputAnalyzer output = new OutputAnalyzer(pb.start());
584 String outputFileNamePrefix =
585 getTestName() + "-" + String.format("%04d", getNextLogCounter()) + "-" + logName;
586
587 writeFile(getOutputFile(outputFileNamePrefix + ".stdout"), output.getStdout());
588 writeFile(getOutputFile(outputFileNamePrefix + ".stderr"), output.getStderr());
589 System.out.println("[ELAPSED: " + (System.currentTimeMillis() - started) + " ms]");
590 System.out.println("[logging stdout to " + outputFileNamePrefix + ".stdout]");
591 System.out.println("[logging stderr to " + outputFileNamePrefix + ".stderr]");
592 System.out.println("[STDERR]\n" + output.getStderr());
593
594 if (copyChildStdoutToMainStdout)
595 System.out.println("[STDOUT]\n" + output.getStdout());
596
597 return output;
598 }
599
600
601 private static void writeFile(File file, String content) throws Exception {
602 FileOutputStream fos = new FileOutputStream(file);
603 PrintStream ps = new PrintStream(fos);
604 ps.print(content);
605 ps.close();
|
482 return output;
483 }
484
485
486 // get the file object for the test artifact
487 public static File getTestArtifact(String name, boolean checkExistence) {
488 File dir = new File(System.getProperty("test.classes", "."));
489 File file = new File(dir, name);
490
491 if (checkExistence && !file.exists()) {
492 throw new RuntimeException("Cannot find " + file.getPath());
493 }
494
495 return file;
496 }
497
498
499 // create file containing the specified class list
500 public static File makeClassList(String classes[])
501 throws Exception {
502 return makeClassList(testName + "-", classes);
503 }
504
505 // create file containing the specified class list
506 public static File makeClassList(String testCaseName, String classes[])
507 throws Exception {
508
509 File classList = getTestArtifact(testCaseName + "test.classlist", false);
510 FileOutputStream fos = new FileOutputStream(classList);
511 PrintStream ps = new PrintStream(fos);
512
513 addToClassList(ps, classes);
514
515 ps.close();
516 fos.close();
517
518 return classList;
519 }
520
521
522 public static void addToClassList(PrintStream ps, String classes[])
523 throws IOException
524 {
525 if (classes != null) {
526 for (String s : classes) {
527 ps.println(s);
528 }
529 }
530 }
531
532 private static String testName = Utils.TEST_NAME.replace('/', '.');
533
534 private static final SimpleDateFormat timeStampFormat =
535 new SimpleDateFormat("HH'h'mm'm'ss's'SSS");
536
537 private static String defaultArchiveName;
538
539 // Call this method to start new archive with new unique name
540 public static void startNewArchiveName() {
541 defaultArchiveName = testName +
542 timeStampFormat.format(new Date()) + ".jsa";
543 }
544
545 public static String getDefaultArchiveName() {
546 return defaultArchiveName;
547 }
548
549
550 // ===================== FILE ACCESS convenience methods
551 public static File getOutputFile(String name) {
552 File dir = new File(System.getProperty("test.classes", "."));
553 return new File(dir, testName + "-" + name);
554 }
555
556
557 public static File getOutputSourceFile(String name) {
558 File dir = new File(System.getProperty("test.classes", "."));
559 return new File(dir, name);
560 }
561
562
563 public static File getSourceFile(String name) {
564 File dir = new File(System.getProperty("test.src", "."));
565 return new File(dir, name);
566 }
567
568
569 // ============================= Logging
570 public static OutputAnalyzer executeAndLog(ProcessBuilder pb, String logName) throws Exception {
571 long started = System.currentTimeMillis();
572 OutputAnalyzer output = new OutputAnalyzer(pb.start());
573 String outputFileNamePrefix =
574 testName + "-" + String.format("%04d", getNextLogCounter()) + "-" + logName;
575
576 writeFile(getOutputFile(outputFileNamePrefix + ".stdout"), output.getStdout());
577 writeFile(getOutputFile(outputFileNamePrefix + ".stderr"), output.getStderr());
578 System.out.println("[ELAPSED: " + (System.currentTimeMillis() - started) + " ms]");
579 System.out.println("[logging stdout to " + outputFileNamePrefix + ".stdout]");
580 System.out.println("[logging stderr to " + outputFileNamePrefix + ".stderr]");
581 System.out.println("[STDERR]\n" + output.getStderr());
582
583 if (copyChildStdoutToMainStdout)
584 System.out.println("[STDOUT]\n" + output.getStdout());
585
586 return output;
587 }
588
589
590 private static void writeFile(File file, String content) throws Exception {
591 FileOutputStream fos = new FileOutputStream(file);
592 PrintStream ps = new PrintStream(fos);
593 ps.print(content);
594 ps.close();
|