openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject svn commit: r1814559 - in /openwebbeans/meecrowave/trunk: meecrowave-junit/src/main/java/org/apache/meecrowave/junit/ meecrowave-junit/src/main/java/org/apache/meecrowave/testing/ meecrowave-junit/src/test/java/org/apache/meecrowave/junit/ meecrowave-o...
Date Wed, 08 Nov 2017 07:59:28 GMT
Author: struberg
Date: Wed Nov  8 07:59:27 2017
New Revision: 1814559

URL: http://svn.apache.org/viewvc?rev=1814559&view=rev
Log:
MEECROWAVE-77 wrap unit tests runs in separate ClassLoader 

Modified:
    openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/MeecrowaveRule.java
    openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/MeecrowaveRuleBase.java
    openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/testing/MonoBase.java
    openwebbeans/meecrowave/trunk/meecrowave-junit/src/test/java/org/apache/meecrowave/junit/MeecrowaveRuleTest.java
    openwebbeans/meecrowave/trunk/meecrowave-oauth2/src/test/java/org/apache/meecrowave/oauth2/OAuth2Test.java

Modified: openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/MeecrowaveRule.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/MeecrowaveRule.java?rev=1814559&r1=1814558&r2=1814559&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/MeecrowaveRule.java
(original)
+++ openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/MeecrowaveRule.java
Wed Nov  8 07:59:27 2017
@@ -18,12 +18,17 @@
  */
 package org.apache.meecrowave.junit;
 
+import java.net.URL;
+import java.net.URLClassLoader;
+
 import org.apache.meecrowave.Meecrowave;
 
 public class MeecrowaveRule extends MeecrowaveRuleBase<MeecrowaveRule> {
     private final Meecrowave.Builder configuration;
     private final String context;
 
+    private ClassLoader meecrowaveCL;
+
     public MeecrowaveRule() {
         this(new Meecrowave.Builder().randomHttpPort(), "");
     }
@@ -42,4 +47,17 @@ public class MeecrowaveRule extends Meec
     protected AutoCloseable onStart() {
         return new Meecrowave(configuration).bake(context);
     }
+
+    @Override
+    protected ClassLoader getClassLoader() {
+        if (meecrowaveCL == null) {
+            ClassLoader currentCL = Thread.currentThread().getContextClassLoader();
+            if (currentCL == null) {
+                this.getClass().getClassLoader();
+            }
+
+            meecrowaveCL = new URLClassLoader(new URL[0], currentCL);
+        }
+        return meecrowaveCL;
+    }
 }

Modified: openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/MeecrowaveRuleBase.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/MeecrowaveRuleBase.java?rev=1814559&r1=1814558&r2=1814559&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/MeecrowaveRuleBase.java
(original)
+++ openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/MeecrowaveRuleBase.java
Wed Nov  8 07:59:27 2017
@@ -43,6 +43,11 @@ public abstract class MeecrowaveRuleBase
         return new Statement() {
             @Override
             public void evaluate() throws Throwable {
+                ClassLoader oldCL = Thread.currentThread().getContextClassLoader();
+                ClassLoader newCl = getClassLoader();
+                if (newCl != null) {
+                    Thread.currentThread().setContextClassLoader(newCl);
+                }
                 try (final AutoCloseable closeable = onStart()) {
                     started.set(true);
                     final Collection<CreationalContext<?>> contexts = toInject.stream().map(MeecrowaveRuleBase::doInject).collect(toList());
@@ -53,6 +58,9 @@ public abstract class MeecrowaveRuleBase
                         started.set(false);
                     }
                 }
+                finally {
+                    Thread.currentThread().setContextClassLoader(oldCL);
+                }
             }
         };
     }
@@ -78,4 +86,8 @@ public abstract class MeecrowaveRuleBase
     public abstract Meecrowave.Builder getConfiguration();
 
     protected abstract AutoCloseable onStart();
+
+    protected ClassLoader getClassLoader() {
+        return null;
+    }
 }

Modified: openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/testing/MonoBase.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/testing/MonoBase.java?rev=1814559&r1=1814558&r2=1814559&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/testing/MonoBase.java
(original)
+++ openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/testing/MonoBase.java
Wed Nov  8 07:59:27 2017
@@ -21,6 +21,8 @@ package org.apache.meecrowave.testing;
 import org.apache.meecrowave.Meecrowave;
 
 import java.io.File;
+import java.net.URL;
+import java.net.URLClassLoader;
 import java.util.Comparator;
 import java.util.ServiceLoader;
 import java.util.concurrent.atomic.AtomicReference;
@@ -29,11 +31,15 @@ import java.util.stream.StreamSupport;
 public class MonoBase {
     private static final AtomicReference<Meecrowave> CONTAINER = new AtomicReference<>();
     private static final AtomicReference<Meecrowave.Builder> CONFIGURATION = new AtomicReference<>();
+    private static ClassLoader monoClassLoader = null;
 
     public Meecrowave.Builder doBoot() {
         final Meecrowave.Builder configuration = new Meecrowave.Builder().randomHttpPort().noShutdownHook(/*the
rule does*/);
         CONFIGURATION.compareAndSet(null, configuration);
 
+        ClassLoader originalCL = Thread.currentThread().getContextClassLoader();
+        monoClassLoader = new URLClassLoader(new URL[0], originalCL);
+
         final Meecrowave meecrowave = new Meecrowave(CONFIGURATION.get());
         if (CONTAINER.compareAndSet(null, meecrowave)) {
             final Configuration runnerConfig = StreamSupport.stream(ServiceLoader.load(Configuration.class).spliterator(),
false)

Modified: openwebbeans/meecrowave/trunk/meecrowave-junit/src/test/java/org/apache/meecrowave/junit/MeecrowaveRuleTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-junit/src/test/java/org/apache/meecrowave/junit/MeecrowaveRuleTest.java?rev=1814559&r1=1814558&r2=1814559&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-junit/src/test/java/org/apache/meecrowave/junit/MeecrowaveRuleTest.java
(original)
+++ openwebbeans/meecrowave/trunk/meecrowave-junit/src/test/java/org/apache/meecrowave/junit/MeecrowaveRuleTest.java
Wed Nov  8 07:59:27 2017
@@ -19,23 +19,36 @@
 package org.apache.meecrowave.junit;
 
 import org.apache.meecrowave.io.IO;
+import org.app.MyAppClass;
 import org.junit.ClassRule;
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.TestRule;
 
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
 
+import javax.inject.Inject;
+
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.fail;
 
 public class MeecrowaveRuleTest {
     @ClassRule
     public static final MeecrowaveRule RULE = new MeecrowaveRule();
 
+    @Rule
+    public final TestRule injectRule = new InjectRule(this);
+
+    private @Inject MyAppClass myApp;
+
     @Test
     public void test() throws IOException {
         assertEquals("simple", slurp(new URL("http://localhost:" + RULE.getConfiguration().getHttpPort()
+ "/api/test")));
+
+        assertNotNull(myApp);
     }
 
     private String slurp(final URL url) {

Modified: openwebbeans/meecrowave/trunk/meecrowave-oauth2/src/test/java/org/apache/meecrowave/oauth2/OAuth2Test.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-oauth2/src/test/java/org/apache/meecrowave/oauth2/OAuth2Test.java?rev=1814559&r1=1814558&r2=1814559&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-oauth2/src/test/java/org/apache/meecrowave/oauth2/OAuth2Test.java
(original)
+++ openwebbeans/meecrowave/trunk/meecrowave-oauth2/src/test/java/org/apache/meecrowave/oauth2/OAuth2Test.java
Wed Nov  8 07:59:27 2017
@@ -206,7 +206,7 @@ public class OAuth2Test {
         final CachingProvider provider = Caching.getCachingProvider();
         final CacheManager cacheManager = provider.getCacheManager(
                 ClassLoaderUtils.getResource("default-oauth2.jcs", OAuth2Test.class).toURI(),
-                provider.getDefaultClassLoader());
+                Thread.currentThread().getContextClassLoader());
         Cache<String, org.apache.cxf.rs.security.oauth2.common.Client> cache;
         try {
             cache = cacheManager



Mime
View raw message