1 /*
   2  * Copyright (c) 2019, 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 package jdk.jpackage.test;
  25 
  26 import java.util.ArrayList;
  27 import java.util.List;
  28 import java.util.function.Function;
  29 import java.util.function.Predicate;
  30 import java.util.stream.Collectors;
  31 import static jdk.jpackage.test.TestBuilder.CMDLINE_ARG_PREFIX;
  32 
  33 
  34 public final class Main {
  35     public static void main(String args[]) throws Throwable {
  36         boolean listTests = false;
  37         List<TestInstance> tests = new ArrayList<>();
  38         try (TestBuilder testBuilder = new TestBuilder(tests::add)) {
  39             for (var arg : args) {
  40                 TestBuilder.trace(String.format("Parsing [%s]...", arg));
  41 
  42                 if ((CMDLINE_ARG_PREFIX + "list").equals(arg)) {
  43                     listTests = true;
  44                     continue;
  45                 }
  46 
  47                 boolean success = false;
  48                 try {
  49                     testBuilder.processCmdLineArg(arg);
  50                     success = true;
  51                 } catch (Throwable throwable) {
  52                     TKit.unbox(throwable);
  53                 } finally {
  54                     if (!success) {
  55                         TKit.log(
  56                                 String.format("Error processing parameter=[%s]",
  57                                         arg));
  58                     }
  59                 }
  60             }
  61         }
  62 
  63         // Order tests by their full names to have stable test sequence.
  64         List<TestInstance> orderedTests = tests.stream()
  65                 .sorted((a, b) -> a.fullName().compareTo(b.fullName()))
  66                 .collect(Collectors.toList());
  67 
  68         if (listTests) {
  69             // Just list the tests
  70             orderedTests.stream().forEach(test -> System.out.println(String.format(
  71                     "%s; workDir=[%s]", test.fullName(), test.workDir())));
  72             return;
  73         }
  74 
  75         TKit.withExtraLogStream(() -> runTests(orderedTests));
  76     }
  77 
  78     private static void runTests(List<TestInstance> tests) {
  79         TKit.runTests(tests);
  80 
  81         final long passedCount = tests.stream().filter(TestInstance::passed).count();
  82         TKit.log(String.format("[==========] %d tests ran", tests.size()));
  83         TKit.log(String.format("[  PASSED  ] %d %s", passedCount,
  84                 passedCount == 1 ? "test" : "tests"));
  85 
  86         reportDetails(tests, "[  SKIPPED ]", TestInstance::skipped, false);
  87         reportDetails(tests, "[  FAILED  ]", TestInstance::failed, true);
  88 
  89         var withSkipped = reportSummary(tests, "SKIPPED", TestInstance::skipped);
  90         var withFailures = reportSummary(tests, "FAILED", TestInstance::failed);
  91 
  92         if (withFailures != null) {
  93             throw withFailures;
  94         }
  95 
  96         if (withSkipped != null) {
  97             tests.stream().filter(TestInstance::skipped).findFirst().get().rethrowIfSkipped();
  98         }
  99     }
 100 
 101     private static long reportDetails(List<TestInstance> tests,
 102             String label, Predicate<TestInstance> selector, boolean printWorkDir) {
 103 
 104         final Function<TestInstance, String> makeMessage = test -> {
 105             if (printWorkDir) {
 106                 return String.format("%s %s; workDir=[%s]", label,
 107                         test.fullName(), test.workDir());
 108             }
 109             return String.format("%s %s", label, test.fullName());
 110         };
 111 
 112         final long count = tests.stream().filter(selector).count();
 113         if (count != 0) {
 114             TKit.log(String.format("%s %d %s, listed below", label, count, count
 115                     == 1 ? "test" : "tests"));
 116             tests.stream().filter(selector).map(makeMessage).forEachOrdered(
 117                     TKit::log);
 118         }
 119 
 120         return count;
 121     }
 122 
 123     private static RuntimeException reportSummary(List<TestInstance> tests,
 124             String label, Predicate<TestInstance> selector) {
 125         final long count = tests.stream().filter(selector).count();
 126         if (count != 0) {
 127             final String message = String.format("%d %s %s", count, label, count
 128                     == 1 ? "TEST" : "TESTS");
 129             TKit.log(message);
 130             return new RuntimeException(message);
 131         }
 132 
 133         return null;
 134     }
 135 }