changeset 16388:31e242cad4d1

Allow mx unittest to run single test method from a class
author Tom Rodriguez <tom.rodriguez@oracle.com>
date Wed, 02 Jul 2014 13:05:02 -0700
parents 2bd6dbbd7842
children c68c5fafef92
files graal/com.oracle.graal.test/src/com/oracle/graal/test/GraalJUnitCore.java mx/mx_graal.py
diffstat 2 files changed, 53 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.test/src/com/oracle/graal/test/GraalJUnitCore.java	Tue Jul 01 19:39:01 2014 -0700
+++ b/graal/com.oracle.graal.test/src/com/oracle/graal/test/GraalJUnitCore.java	Wed Jul 02 13:05:02 2014 -0700
@@ -33,10 +33,11 @@
 public class GraalJUnitCore {
 
     /**
-     * Run the tests contained in the classes named in the <code>args</code>. If all tests run
-     * successfully, exit with a status of 0. Otherwise exit with a status of 1. Write feedback
-     * while tests are running and write stack traces for all failed tests after the tests all
-     * complete.
+     * Run the tests contained in the classes named in the <code>args</code>. A single test method
+     * can be specified by adding #method after the class name. Only a single test can be run in
+     * this way. If all tests run successfully, exit with a status of 0. Otherwise exit with a
+     * status of 1. Write feedback while tests are running and write stack traces for all failed
+     * tests after the tests all complete.
      *
      * @param args names of classes in which to find tests to run
      */
@@ -46,6 +47,7 @@
         system.out().println("GraalJUnitCore");
         system.out().println("JUnit version " + Version.id());
         List<Class<?>> classes = new ArrayList<>();
+        String methodName = null;
         List<Failure> missingClasses = new ArrayList<>();
         boolean verbose = false;
         boolean enableTiming = false;
@@ -70,6 +72,27 @@
                 }
 
             } else {
+                /*
+                 * Entries of the form class#method are handled specially. Only one can be specified
+                 * on the command line as there's no obvious way to build a runner for multiple
+                 * ones.
+                 */
+                if (methodName != null) {
+                    system.out().println("Only a single class and method can be specified: " + each);
+                    System.exit(1);
+                } else if (each.contains("#")) {
+                    String[] pair = each.split("#");
+                    if (pair.length != 2) {
+                        system.out().println("Malformed class and method request: " + each);
+                        System.exit(1);
+                    } else if (classes.size() != 0) {
+                        system.out().println("Only a single class and method can be specified: " + each);
+                        System.exit(1);
+                    } else {
+                        methodName = pair[1];
+                        each = pair[0];
+                    }
+                }
                 try {
                     classes.add(Class.forName(each));
                 } catch (ClassNotFoundException e) {
@@ -99,7 +122,13 @@
             graalListener = new GCAfterTestDecorator(graalListener);
         }
         junitCore.addListener(GraalTextListener.createRunListener(graalListener));
-        Result result = junitCore.run(classes.toArray(new Class[0]));
+        Request request;
+        if (methodName == null) {
+            request = Request.classes(classes.toArray(new Class[0]));
+        } else {
+            request = Request.method(classes.get(0), methodName);
+        }
+        Result result = junitCore.run(request);
         for (Failure each : missingClasses) {
             result.getFailures().add(each);
         }
--- a/mx/mx_graal.py	Tue Jul 01 19:39:01 2014 -0700
+++ b/mx/mx_graal.py	Wed Jul 02 13:05:02 2014 -0700
@@ -974,15 +974,31 @@
         projectscp = mx.classpath([pcp.name for pcp in mx.projects_opt_limit_to_suites() if pcp.javaCompliance <= mx.java().javaCompliance])
     else:
         projs = set()
-        for t in tests:
-            found = False
+        if len(tests) == 1 and '#' in tests[0]:
+            words = tests[0].split('#')
+            if len(words) != 2:
+                mx.abort("Method specification is class#method: " + tests[0])
+            t, method = words
             for c, p in candidates.iteritems():
                 if t in c:
                     found = True
-                    classes.append(c)
+                    # this code assumes a single test will be handled by GraalJUnitCore
+                    classes.append(c + '#' + method)
                     projs.add(p.name)
             if not found:
                 mx.log('warning: no tests matched by substring "' + t)
+        else:
+            for t in tests:
+                if '#' in t:
+                    mx.abort('Method specifications can only be used in a single test: ' + t)
+                found = False
+                for c, p in candidates.iteritems():
+                    if t in c:
+                        found = True
+                        classes.append(c)
+                        projs.add(p.name)
+                if not found:
+                    mx.log('warning: no tests matched by substring "' + t)
         projectscp = mx.classpath(projs)
 
     if whitelist: