groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jwagenleit...@apache.org
Subject [2/2] groovy git commit: ensure default runners are always loaded
Date Tue, 04 Jul 2017 04:07:44 GMT
ensure default runners are always loaded


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/a02e12e1
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/a02e12e1
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/a02e12e1

Branch: refs/heads/GROOVY_2_5_X
Commit: a02e12e17783ccfc1be0083b78aeb46a0a6fd6e8
Parents: dcad42c
Author: John Wagenleitner <jwagenleitner@apache.org>
Authored: Mon Jul 3 19:48:46 2017 -0700
Committer: John Wagenleitner <jwagenleitner@apache.org>
Committed: Mon Jul 3 21:06:49 2017 -0700

----------------------------------------------------------------------
 .../groovy/plugin/GroovyRunnerRegistry.java     | 30 +++++++++++++-------
 .../plugin/GroovyRunnerRegistryTest.groovy      | 12 ++++++--
 2 files changed, 30 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/a02e12e1/src/main/org/apache/groovy/plugin/GroovyRunnerRegistry.java
----------------------------------------------------------------------
diff --git a/src/main/org/apache/groovy/plugin/GroovyRunnerRegistry.java b/src/main/org/apache/groovy/plugin/GroovyRunnerRegistry.java
index 43ed8a4..c9f65e8 100644
--- a/src/main/org/apache/groovy/plugin/GroovyRunnerRegistry.java
+++ b/src/main/org/apache/groovy/plugin/GroovyRunnerRegistry.java
@@ -47,6 +47,10 @@ import java.util.logging.Logger;
  * For compatibility with previous versions, this registry implements the
  * {@link Map} interface. All {@code null} keys and values will be ignored
  * and no exception thrown, except where noted.
+ * <p>
+ * By default the registry contains runners that are capable of running
+ * {@code JUnit 3} and {@code JUnit 4} test classes if those libraries
+ * are available to the class loader.
  *
  * @since 2.5.0
  */
@@ -113,7 +117,6 @@ public class GroovyRunnerRegistry implements Map<String, GroovyRunner>,
Iterable
             try {
                 if ((map = runnerMap) == null) {
                     runnerMap = map = new LinkedHashMap<>();
-                    loadDefaultRunners();
                     load(null);
                 }
             } finally {
@@ -123,12 +126,6 @@ public class GroovyRunnerRegistry implements Map<String, GroovyRunner>,
Iterable
         return map;
     }
 
-    private void loadDefaultRunners() {
-        register(DefaultRunners.junit3TestRunner());
-        register(DefaultRunners.junit3SuiteRunner());
-        register(DefaultRunners.junit4TestRunner());
-    }
-
     /**
      * Loads {@link GroovyRunner} instances using the {@link ServiceLoader} facility.
      *
@@ -143,19 +140,29 @@ public class GroovyRunnerRegistry implements Map<String, GroovyRunner>,
Iterable
                 return;
             }
         }
+        writeLock.lock();
         try {
             if (classLoader == null) {
                 classLoader = Thread.currentThread().getContextClassLoader();
             }
-            load0(classLoader);
+            loadDefaultRunners();
+            loadWithLock(classLoader);
         } catch (SecurityException se) {
             LOG.log(Level.WARNING, "Failed to get the context ClassLoader", se);
         } catch (ServiceConfigurationError sce) {
             LOG.log(Level.WARNING, "Failed to load GroovyRunner services from ClassLoader
" + classLoader, sce);
+        } finally {
+            writeLock.unlock();
         }
     }
 
-    private void load0(ClassLoader classLoader) {
+    private void loadDefaultRunners() {
+        register(DefaultRunners.junit3TestRunner());
+        register(DefaultRunners.junit3SuiteRunner());
+        register(DefaultRunners.junit4TestRunner());
+    }
+
+    private void loadWithLock(ClassLoader classLoader) {
         ServiceLoader<GroovyRunner> serviceLoader = ServiceLoader.load(GroovyRunner.class,
classLoader);
         for (GroovyRunner runner : serviceLoader) {
             register(runner);
@@ -346,7 +353,9 @@ public class GroovyRunnerRegistry implements Map<String, GroovyRunner>,
Iterable
     }
 
     /**
-     * Clears all registered runners from the registry.
+     * Clears all registered runners from the registry and resets
+     * the registry so that it contains only the default set of
+     * runners.
      */
     @Override
     public void clear() {
@@ -354,6 +363,7 @@ public class GroovyRunnerRegistry implements Map<String, GroovyRunner>,
Iterable
         writeLock.lock();
         try {
             map.clear();
+            loadDefaultRunners();
         } finally {
             writeLock.unlock();
         }

http://git-wip-us.apache.org/repos/asf/groovy/blob/a02e12e1/src/test/org/apache/groovy/plugin/GroovyRunnerRegistryTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/org/apache/groovy/plugin/GroovyRunnerRegistryTest.groovy b/src/test/org/apache/groovy/plugin/GroovyRunnerRegistryTest.groovy
index 7f063f5..2920d3f 100644
--- a/src/test/org/apache/groovy/plugin/GroovyRunnerRegistryTest.groovy
+++ b/src/test/org/apache/groovy/plugin/GroovyRunnerRegistryTest.groovy
@@ -37,6 +37,13 @@ class GroovyRunnerRegistryTest extends GroovyTestCase {
         assert knownRunners.isEmpty()
     }
 
+    void testDefaultRunnersAreLoaded() {
+        def reg = GroovyRunnerRegistry.getInstance()
+        reg.clear()
+        assert !reg.isEmpty()
+        testRegistryContainsDefaultRunners()
+    }
+
     void testCustomRunner() {
         DummyRunner customRunner = new DummyRunner()
         GroovyRunnerRegistry realRegistry = GroovyRunnerRegistry.getInstance()
@@ -107,9 +114,10 @@ class GroovyRunnerRegistryTest extends GroovyTestCase {
     }
 
     void testClear() {
-        assert registry.size() == knownRunners.size()
+        registry.put('DummyRunner', new DummyRunner())
+        assert registry.size() == knownRunners.size() + 1
         registry.clear()
-        assert registry.size() == 0
+        assert registry.size() == knownRunners.size()
     }
 
     void testPutAll() {


Mime
View raw message