test/hotspot/jtreg/compiler/graalunit/com.oracle.mxtool.junit/com/oracle/mxtool/junit/MxJUnitWrapper.java
changeset 50908 7c51db95ccb6
child 58523 fb3d408c7a7e
equal deleted inserted replaced
50907:39d27210c627 50908:7c51db95ccb6
       
     1 /*
       
     2  * Copyright (c) 2014, 2017, 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 package com.oracle.mxtool.junit;
       
    24 
       
    25 import java.io.BufferedReader;
       
    26 import java.io.File;
       
    27 import java.io.FileNotFoundException;
       
    28 import java.io.FileOutputStream;
       
    29 import java.io.FileReader;
       
    30 import java.io.IOException;
       
    31 import java.io.PrintStream;
       
    32 import java.lang.annotation.Annotation;
       
    33 import java.lang.reflect.Method;
       
    34 import java.util.ArrayList;
       
    35 import java.util.Collections;
       
    36 import java.util.HashSet;
       
    37 import java.util.List;
       
    38 import java.util.Map;
       
    39 import java.util.Optional;
       
    40 import java.util.ServiceLoader;
       
    41 import java.util.Set;
       
    42 import java.util.regex.Matcher;
       
    43 import java.util.regex.Pattern;
       
    44 
       
    45 import org.junit.internal.JUnitSystem;
       
    46 import org.junit.internal.RealSystem;
       
    47 import org.junit.runner.Description;
       
    48 import org.junit.runner.JUnitCore;
       
    49 import org.junit.runner.Request;
       
    50 import org.junit.runner.Result;
       
    51 import org.junit.runner.Runner;
       
    52 import org.junit.runner.notification.Failure;
       
    53 import org.junit.runner.notification.RunListener;
       
    54 import org.junit.runner.notification.RunNotifier;
       
    55 import org.junit.runners.ParentRunner;
       
    56 import org.junit.runners.model.RunnerScheduler;
       
    57 
       
    58 import junit.runner.Version;
       
    59 
       
    60 public class MxJUnitWrapper {
       
    61 
       
    62     public static class MxJUnitConfig {
       
    63 
       
    64         public boolean verbose = false;
       
    65         public boolean veryVerbose = false;
       
    66         public boolean enableTiming = false;
       
    67         public boolean failFast = false;
       
    68         public boolean color = false;
       
    69         public boolean eagerStackTrace = false;
       
    70         public boolean gcAfterTest = false;
       
    71         public boolean recordResults = false;
       
    72         public int repeatCount = 1;
       
    73     }
       
    74 
       
    75     private static class RepeatingRunner extends Runner {
       
    76 
       
    77         private final Runner parent;
       
    78         private int repeat;
       
    79 
       
    80         RepeatingRunner(Runner parent, int repeat) {
       
    81             this.parent = parent;
       
    82             this.repeat = repeat;
       
    83         }
       
    84 
       
    85         @Override
       
    86         public Description getDescription() {
       
    87             return parent.getDescription();
       
    88         }
       
    89 
       
    90         @Override
       
    91         public void run(RunNotifier notifier) {
       
    92             for (int i = 0; i < repeat; i++) {
       
    93                 parent.run(notifier);
       
    94             }
       
    95         }
       
    96 
       
    97         @Override
       
    98         public int testCount() {
       
    99             return super.testCount() * repeat;
       
   100         }
       
   101     }
       
   102 
       
   103     private static class RepeatingRequest extends Request {
       
   104 
       
   105         private final Request request;
       
   106         private final int repeat;
       
   107 
       
   108         RepeatingRequest(Request request, int repeat) {
       
   109             this.request = request;
       
   110             this.repeat = repeat;
       
   111         }
       
   112 
       
   113         @Override
       
   114         public Runner getRunner() {
       
   115             return new RepeatingRunner(request.getRunner(), repeat);
       
   116         }
       
   117     }
       
   118 
       
   119     /**
       
   120      * Run the tests contained in the classes named in the <code>args</code>. A single test method
       
   121      * can be specified by adding #method after the class name. Only a single test can be run in
       
   122      * this way. If all tests run successfully, exit with a status of 0. Otherwise exit with a
       
   123      * status of 1. Write feedback while tests are running and write stack traces for all failed
       
   124      * tests after the tests all complete.
       
   125      *
       
   126      * @param args names of classes in which to find tests to run
       
   127      */
       
   128     public static void main(String... args) {
       
   129         JUnitSystem system = new RealSystem();
       
   130         JUnitCore junitCore = new JUnitCore();
       
   131         system.out().println("MxJUnitCore");
       
   132         system.out().println("JUnit version " + Version.id());
       
   133 
       
   134         MxJUnitRequest.Builder builder = new MxJUnitRequest.Builder();
       
   135         MxJUnitConfig config = new MxJUnitConfig();
       
   136 
       
   137         String[] expandedArgs = expandArgs(args);
       
   138         int i = 0;
       
   139         while (i < expandedArgs.length) {
       
   140             String each = expandedArgs[i];
       
   141             if (each.charAt(0) == '-') {
       
   142                 // command line arguments
       
   143                 if (each.contentEquals("-JUnitVerbose")) {
       
   144                     config.verbose = true;
       
   145                 } else if (each.contentEquals("-JUnitVeryVerbose")) {
       
   146                     config.veryVerbose = true;
       
   147                 } else if (each.contentEquals("-JUnitFailFast")) {
       
   148                     config.failFast = true;
       
   149                 } else if (each.contentEquals("-JUnitEnableTiming")) {
       
   150                     config.enableTiming = true;
       
   151                 } else if (each.contentEquals("-JUnitColor")) {
       
   152                     config.color = true;
       
   153                 } else if (each.contentEquals("-JUnitEagerStackTrace")) {
       
   154                     config.eagerStackTrace = true;
       
   155                 } else if (each.contentEquals("-JUnitGCAfterTest")) {
       
   156                     config.gcAfterTest = true;
       
   157                 } else if (each.contentEquals("-JUnitRecordResults")) {
       
   158                     config.recordResults = true;
       
   159                 } else if (each.contentEquals("-JUnitRepeat")) {
       
   160                     if (i + 1 >= expandedArgs.length) {
       
   161                         system.out().println("Must include argument for -JUnitRepeat");
       
   162                         System.exit(1);
       
   163                     }
       
   164                     try {
       
   165                         config.repeatCount = Integer.parseInt(expandedArgs[++i]);
       
   166                     } catch (NumberFormatException e) {
       
   167                         system.out().println("Expected integer argument for -JUnitRepeat. Found: " + expandedArgs[i]);
       
   168                         System.exit(1);
       
   169                     }
       
   170                 } else {
       
   171                     system.out().println("Unknown command line argument: " + each);
       
   172                 }
       
   173 
       
   174             } else {
       
   175 
       
   176                 try {
       
   177                     builder.addTestSpec(each);
       
   178                 } catch (MxJUnitRequest.BuilderException ex) {
       
   179                     system.out().println(ex.getMessage());
       
   180                     System.exit(1);
       
   181                 }
       
   182             }
       
   183             i++;
       
   184         }
       
   185 
       
   186         MxJUnitRequest request = builder.build();
       
   187 
       
   188         if (System.getProperty("java.specification.version").compareTo("1.9") >= 0) {
       
   189             addExports(request.classes, system.out());
       
   190         }
       
   191 
       
   192         for (RunListener p : ServiceLoader.load(RunListener.class)) {
       
   193             junitCore.addListener(p);
       
   194         }
       
   195 
       
   196         Result result = runRequest(junitCore, system, config, request);
       
   197         System.exit(result.wasSuccessful() ? 0 : 1);
       
   198     }
       
   199 
       
   200     private static PrintStream openFile(JUnitSystem system, String name) {
       
   201         File file = new File(name).getAbsoluteFile();
       
   202         try {
       
   203             FileOutputStream fos = new FileOutputStream(file);
       
   204             return new PrintStream(fos, true);
       
   205         } catch (FileNotFoundException e) {
       
   206             system.out().println("Could not open " + file + " for writing: " + e);
       
   207             System.exit(1);
       
   208             return null;
       
   209         }
       
   210     }
       
   211 
       
   212     public static Result runRequest(JUnitCore junitCore, JUnitSystem system, MxJUnitConfig config, MxJUnitRequest mxRequest) {
       
   213         final TextRunListener textListener;
       
   214         if (config.veryVerbose) {
       
   215             textListener = new VerboseTextListener(system, mxRequest.classes.size(), VerboseTextListener.SHOW_ALL_TESTS);
       
   216         } else if (config.verbose) {
       
   217             textListener = new VerboseTextListener(system, mxRequest.classes.size());
       
   218         } else {
       
   219             textListener = new TextRunListener(system);
       
   220         }
       
   221         TimingDecorator timings = config.enableTiming ? new TimingDecorator(textListener) : null;
       
   222         MxRunListener mxListener = config.enableTiming ? timings : textListener;
       
   223 
       
   224         if (config.color) {
       
   225             mxListener = new AnsiTerminalDecorator(mxListener);
       
   226         }
       
   227         if (config.eagerStackTrace) {
       
   228             mxListener = new EagerStackTraceDecorator(mxListener);
       
   229         }
       
   230         if (config.gcAfterTest) {
       
   231             mxListener = new GCAfterTestDecorator(mxListener);
       
   232         }
       
   233         if (config.recordResults) {
       
   234             PrintStream passed = openFile(system, "passed.txt");
       
   235             PrintStream failed = openFile(system, "failed.txt");
       
   236             mxListener = new TestResultLoggerDecorator(passed, failed, mxListener);
       
   237         }
       
   238 
       
   239         junitCore.addListener(TextRunListener.createRunListener(mxListener));
       
   240 
       
   241         Request request = mxRequest.getRequest();
       
   242         if (mxRequest.methodName == null) {
       
   243             if (config.failFast) {
       
   244                 Runner runner = request.getRunner();
       
   245                 if (runner instanceof ParentRunner) {
       
   246                     ParentRunner<?> parentRunner = (ParentRunner<?>) runner;
       
   247                     parentRunner.setScheduler(new RunnerScheduler() {
       
   248                         public void schedule(Runnable childStatement) {
       
   249                             if (textListener.getLastFailure() == null) {
       
   250                                 childStatement.run();
       
   251                             }
       
   252                         }
       
   253 
       
   254                         public void finished() {
       
   255                         }
       
   256                     });
       
   257                 } else {
       
   258                     system.out().println("Unexpected Runner subclass " + runner.getClass().getName() + " - fail fast not supported");
       
   259                 }
       
   260             }
       
   261         } else {
       
   262             if (config.failFast) {
       
   263                 system.out().println("Single method selected - fail fast not supported");
       
   264             }
       
   265         }
       
   266 
       
   267         if (config.repeatCount != 1) {
       
   268             request = new RepeatingRequest(request, config.repeatCount);
       
   269         }
       
   270 
       
   271         if (config.enableTiming) {
       
   272             Runtime.getRuntime().addShutdownHook(new Thread() {
       
   273                 @Override
       
   274                 public void run() {
       
   275                     printTimings(timings);
       
   276                 }
       
   277             });
       
   278         }
       
   279 
       
   280         Result result = junitCore.run(request);
       
   281         for (Failure each : mxRequest.missingClasses) {
       
   282             result.getFailures().add(each);
       
   283         }
       
   284 
       
   285         return result;
       
   286     }
       
   287 
       
   288     private static final Pattern MODULE_PACKAGE_RE = Pattern.compile("([^/]+)/(.+)");
       
   289 
       
   290     private static class Timing<T> implements Comparable<Timing<T>> {
       
   291         final T subject;
       
   292         final long value;
       
   293 
       
   294         Timing(T subject, long value) {
       
   295             this.subject = subject;
       
   296             this.value = value;
       
   297         }
       
   298 
       
   299         public int compareTo(Timing<T> o) {
       
   300             if (this.value < o.value) {
       
   301                 return -1;
       
   302             }
       
   303             if (this.value > o.value) {
       
   304                 return 1;
       
   305             }
       
   306             return 0;
       
   307         }
       
   308     }
       
   309 
       
   310     // Should never need to customize so using a system property instead
       
   311     // of a command line option for customization is fine.
       
   312     private static final int TIMINGS_TO_PRINT = Integer.getInteger("mx.junit.timings_to_print", 10);
       
   313 
       
   314     private static void printTimings(TimingDecorator timings) {
       
   315         if (TIMINGS_TO_PRINT != 0) {
       
   316             List<Timing<Class<?>>> classTimes = new ArrayList<>(timings.classTimes.size());
       
   317             List<Timing<Description>> testTimes = new ArrayList<>(timings.testTimes.size());
       
   318             for (Map.Entry<Class<?>, Long> e : timings.classTimes.entrySet()) {
       
   319                 classTimes.add(new Timing<>(e.getKey(), e.getValue()));
       
   320             }
       
   321             for (Map.Entry<Description, Long> e : timings.testTimes.entrySet()) {
       
   322                 testTimes.add(new Timing<>(e.getKey(), e.getValue()));
       
   323             }
       
   324             classTimes.sort(Collections.reverseOrder());
       
   325             testTimes.sort(Collections.reverseOrder());
       
   326 
       
   327             System.out.println();
       
   328             System.out.printf("%d longest running test classes:%n", TIMINGS_TO_PRINT);
       
   329             for (int i = 0; i < TIMINGS_TO_PRINT && i < classTimes.size(); i++) {
       
   330                 Timing<Class<?>> timing = classTimes.get(i);
       
   331                 System.out.printf(" %,10d ms    %s%n", timing.value, timing.subject.getName());
       
   332             }
       
   333             System.out.printf("%d longest running tests:%n", TIMINGS_TO_PRINT);
       
   334             for (int i = 0; i < TIMINGS_TO_PRINT && i < testTimes.size(); i++) {
       
   335                 Timing<Description> timing = testTimes.get(i);
       
   336                 System.out.printf(" %,10d ms    %s%n", timing.value, timing.subject);
       
   337             }
       
   338             Object[] current = timings.getCurrentTestDuration();
       
   339             if (current != null) {
       
   340                 System.out.printf("Test %s not finished after %d ms%n", current[0], current[1]);
       
   341             }
       
   342 
       
   343         }
       
   344     }
       
   345 
       
   346     /**
       
   347      * Adds the super types of {@code cls} to {@code supertypes}.
       
   348      */
       
   349     private static void gatherSupertypes(Class<?> cls, Set<Class<?>> supertypes) {
       
   350         if (!supertypes.contains(cls)) {
       
   351             supertypes.add(cls);
       
   352             Class<?> superclass = cls.getSuperclass();
       
   353             if (superclass != null) {
       
   354                 gatherSupertypes(superclass, supertypes);
       
   355             }
       
   356             for (Class<?> iface : cls.getInterfaces()) {
       
   357                 gatherSupertypes(iface, supertypes);
       
   358             }
       
   359         }
       
   360     }
       
   361 
       
   362     /**
       
   363      * Updates modules specified in {@code AddExport} annotations on {@code classes} to export
       
   364      * concealed packages to the annotation classes' declaring modules.
       
   365      */
       
   366     private static void addExports(Set<Class<?>> classes, PrintStream out) {
       
   367         Set<Class<?>> types = new HashSet<>();
       
   368         for (Class<?> cls : classes) {
       
   369             gatherSupertypes(cls, types);
       
   370         }
       
   371         for (Class<?> cls : types) {
       
   372             Annotation[] annos = cls.getAnnotations();
       
   373             for (Annotation a : annos) {
       
   374                 Class<? extends Annotation> annotationType = a.annotationType();
       
   375                 if (annotationType.getSimpleName().equals("AddExports")) {
       
   376                     Optional<String[]> value = getElement("value", String[].class, a);
       
   377                     if (value.isPresent()) {
       
   378                         for (String export : value.get()) {
       
   379                             Matcher m = MODULE_PACKAGE_RE.matcher(export);
       
   380                             if (m.matches()) {
       
   381                                 String moduleName = m.group(1);
       
   382                                 String packageName = m.group(2);
       
   383                                 JLModule module = JLModule.find(moduleName);
       
   384                                 if (module == null) {
       
   385                                     out.printf("%s: Cannot find module named %s specified in \"AddExports\" annotation: %s%n", cls.getName(), moduleName, a);
       
   386                                 } else {
       
   387                                     if (packageName.equals("*")) {
       
   388                                         module.exportAllPackagesTo(JLModule.fromClass(cls));
       
   389                                     } else {
       
   390                                         module.addExports(packageName, JLModule.fromClass(cls));
       
   391                                         module.addOpens(packageName, JLModule.fromClass(cls));
       
   392                                     }
       
   393                                 }
       
   394                             } else {
       
   395                                 out.printf("%s: Ignoring \"AddExports\" annotation with value not matching <module>/<package> pattern: %s%n", cls.getName(), a);
       
   396                             }
       
   397                         }
       
   398                     } else {
       
   399                         out.printf("%s: Ignoring \"AddExports\" annotation without `String value` element: %s%n", cls.getName(), a);
       
   400                     }
       
   401                 }
       
   402             }
       
   403         }
       
   404     }
       
   405 
       
   406     /**
       
   407      * Gets the value of the element named {@code name} of type {@code type} from {@code annotation}
       
   408      * if present.
       
   409      *
       
   410      * @return the requested element value wrapped in an {@link Optional} or
       
   411      *         {@link Optional#empty()} if {@code annotation} has no element named {@code name}
       
   412      * @throws AssertionError if {@code annotation} has an element of the given name but whose type
       
   413      *             is not {@code type} or if there's some problem reading the value via reflection
       
   414      */
       
   415     private static <T> Optional<T> getElement(String name, Class<T> type, Annotation annotation) {
       
   416         Class<? extends Annotation> annotationType = annotation.annotationType();
       
   417         Method valueAccessor;
       
   418         try {
       
   419             valueAccessor = annotationType.getMethod(name);
       
   420             if (!valueAccessor.getReturnType().equals(type)) {
       
   421                 throw new AssertionError(String.format("Element %s of %s is of type %s, not %s ", name, annotationType.getName(), valueAccessor.getReturnType().getName(), type.getName()));
       
   422             }
       
   423         } catch (NoSuchMethodException e) {
       
   424             return Optional.empty();
       
   425         }
       
   426         try {
       
   427             return Optional.of(type.cast(valueAccessor.invoke(annotation)));
       
   428         } catch (Exception e) {
       
   429             throw new AssertionError(String.format("Could not read %s element from %s", name, annotation), e);
       
   430         }
       
   431     }
       
   432 
       
   433     /**
       
   434      * Expand any arguments starting with @ and return the resulting argument array.
       
   435      *
       
   436      * @return the expanded argument array
       
   437      */
       
   438     private static String[] expandArgs(String[] args) {
       
   439         List<String> result = null;
       
   440         for (int i = 0; i < args.length; i++) {
       
   441             String arg = args[i];
       
   442             if (arg.length() > 0 && arg.charAt(0) == '@') {
       
   443                 if (result == null) {
       
   444                     result = new ArrayList<>();
       
   445                     for (int j = 0; j < i; j++) {
       
   446                         result.add(args[j]);
       
   447                     }
       
   448                     expandArg(arg.substring(1), result);
       
   449                 }
       
   450             } else if (result != null) {
       
   451                 result.add(arg);
       
   452             }
       
   453         }
       
   454         return result != null ? result.toArray(new String[0]) : args;
       
   455     }
       
   456 
       
   457     /**
       
   458      * Add each line from {@code filename} to the list {@code args}.
       
   459      */
       
   460     private static void expandArg(String filename, List<String> args) {
       
   461         BufferedReader br = null;
       
   462         try {
       
   463             br = new BufferedReader(new FileReader(filename));
       
   464 
       
   465             String buf;
       
   466             while ((buf = br.readLine()) != null) {
       
   467                 args.add(buf);
       
   468             }
       
   469             br.close();
       
   470         } catch (IOException ioe) {
       
   471             ioe.printStackTrace();
       
   472             System.exit(2);
       
   473         } finally {
       
   474             try {
       
   475                 if (br != null) {
       
   476                     br.close();
       
   477                 }
       
   478             } catch (IOException ioe) {
       
   479                 ioe.printStackTrace();
       
   480                 System.exit(3);
       
   481             }
       
   482         }
       
   483     }
       
   484 }