openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1818605 - in /openwebbeans/meecrowave/trunk: meecrowave-core/src/main/java/org/apache/meecrowave/openwebbeans/ meecrowave-junit/src/main/java/org/apache/meecrowave/internal/ meecrowave-junit/src/main/java/org/apache/meecrowave/junit/ meecr...
Date Mon, 18 Dec 2017 18:38:38 GMT
Author: rmannibucau
Date: Mon Dec 18 18:38:38 2017
New Revision: 1818605

URL: http://svn.apache.org/viewvc?rev=1818605&view=rev
Log:
ensure that even with our classloader lock we support monomeecrowave.rule case properly with
injections and we dont break CDI.current

Modified:
    openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/openwebbeans/KnownJarsFilter.java
    openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/internal/ClassLoaderLock.java
    openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/MonoMeecrowave.java
    openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/ScopeRule.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/MonoMeecrowaveRuleTest.java

Modified: openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/openwebbeans/KnownJarsFilter.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/openwebbeans/KnownJarsFilter.java?rev=1818605&r1=1818604&r2=1818605&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/openwebbeans/KnownJarsFilter.java
(original)
+++ openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/openwebbeans/KnownJarsFilter.java
Mon Dec 18 18:38:38 2017
@@ -226,6 +226,7 @@ public class KnownJarsFilter implements
         add("openjpa-");
         add("openmdx-");
         add("opensaml-");
+        add("opentest4j-");
         add("openwebbeans-");
         add("openws-");
         add("ops4j-");

Modified: openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/internal/ClassLoaderLock.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/internal/ClassLoaderLock.java?rev=1818605&r1=1818604&r2=1818605&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/internal/ClassLoaderLock.java
(original)
+++ openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/internal/ClassLoaderLock.java
Mon Dec 18 18:38:38 2017
@@ -34,6 +34,9 @@ public final class ClassLoaderLock {
         if (currentCL == null) {
             currentCL = ClassLoaderLock.class.getClassLoader();
         }
+        if (Boolean.getBoolean("meecrowave.junit.classloaderlock.off")) { // safeguard for
advanced cases
+            return currentCL;
+        }
 
         final SingletonService<WebBeansContext> singletonService = WebBeansFinder.getSingletonService();
         synchronized (singletonService) {

Modified: openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/MonoMeecrowave.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/MonoMeecrowave.java?rev=1818605&r1=1818604&r2=1818605&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/MonoMeecrowave.java
(original)
+++ openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/MonoMeecrowave.java
Mon Dec 18 18:38:38 2017
@@ -38,8 +38,6 @@ import org.junit.runners.model.Statement
 
 public class MonoMeecrowave {
     private static final MonoBase BASE = new MonoBase();
-    private static final AutoCloseable NOOP_CLOSEABLE = () -> {
-    };
 
     public static class Runner extends BlockJUnit4ClassRunner {
         public Runner(final Class<?> klass) throws InitializationError {
@@ -52,14 +50,21 @@ public class MonoMeecrowave {
             rules.add((base, method, target) -> new Statement() {
                 @Override
                 public void evaluate() throws Throwable {
-                    BASE.startIfNeeded();
+                    final MonoBase.Instance instance = BASE.startIfNeeded();
 
-                    configInjection(test.getClass(), test);
-                    final CreationalContext<?> creationalContext = Injector.inject(test);
+                    final Thread thread = Thread.currentThread();
+                    final ClassLoader originalLoader = thread.getContextClassLoader();
+                    thread.setContextClassLoader(instance.getStartupClassLoader());
                     try {
-                        base.evaluate();
-                    } finally {
-                        creationalContext.release();
+                        configInjection(test.getClass(), test);
+                        final CreationalContext<?> creationalContext = Injector.inject(test);
+                        try {
+                            base.evaluate();
+                        } finally {
+                            creationalContext.release();
+                        }
+                    } finally{
+                        thread.setContextClassLoader(originalLoader);
                     }
                 }
 
@@ -83,8 +88,11 @@ public class MonoMeecrowave {
 
         @Override
         protected AutoCloseable onStart() {
-            BASE.startIfNeeded();
-            return NOOP_CLOSEABLE;
+            final MonoBase.Instance instance = BASE.startIfNeeded();
+            final Thread thread = Thread.currentThread();
+            final ClassLoader originalLoader = thread.getContextClassLoader();
+            thread.setContextClassLoader(instance.getStartupClassLoader());
+            return () -> thread.setContextClassLoader(originalLoader);
         }
 
         @Override

Modified: openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/ScopeRule.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/ScopeRule.java?rev=1818605&r1=1818604&r2=1818605&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/ScopeRule.java
(original)
+++ openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/ScopeRule.java
Mon Dec 18 18:38:38 2017
@@ -22,13 +22,8 @@ import static java.util.Arrays.asList;
 
 import java.lang.annotation.Annotation;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Collections;
-import java.util.Comparator;
 import java.util.List;
-import java.util.stream.Stream;
-import javax.enterprise.context.RequestScoped;
-import javax.enterprise.context.SessionScoped;
 
 import org.apache.webbeans.config.WebBeansContext;
 import org.apache.webbeans.spi.ContextsService;
@@ -39,8 +34,8 @@ import org.junit.runners.model.Statement
 public class ScopeRule implements TestRule {
     private final List<Class<? extends Annotation>> scopes;
 
-    public ScopeRule(final Class<? extends Annotation>... scopes) {
-        this.scopes = new ArrayList<>(asList(scopes));
+    public ScopeRule(final Class<?>... scopes) {
+        this.scopes = new ArrayList(asList(scopes));
     }
 
     @Override

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=1818605&r1=1818604&r2=1818605&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
Mon Dec 18 18:38:38 2017
@@ -28,13 +28,10 @@ import java.util.concurrent.atomic.Atomi
 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 final AtomicReference<Instance> CONTAINER = new AtomicReference<>();
 
     public Meecrowave.Builder doBoot() {
         final Meecrowave.Builder configuration = new Meecrowave.Builder().randomHttpPort().noShutdownHook(/*the
rule does*/);
-        CONFIGURATION.compareAndSet(null, configuration);
-
         boolean unlocked = false;
         ClassLoaderLock.LOCK.lock();
         try {
@@ -42,8 +39,8 @@ public class MonoBase {
             ClassLoaderLock.LOCK.lock();
             final ClassLoader containerLoader = ClassLoaderLock.getUsableContainerLoader();
 
-            final Meecrowave meecrowave = new Meecrowave(CONFIGURATION.get());
-            if (CONTAINER.compareAndSet(null, meecrowave)) {
+            final Meecrowave meecrowave = new Meecrowave(configuration);
+            if (CONTAINER.compareAndSet(null, new Instance(meecrowave, configuration, containerLoader)))
{
                 final Configuration runnerConfig = StreamSupport.stream(ServiceLoader.load(Configuration.class)
                                                                                      .spliterator(),
false)
                                                                 .sorted(Comparator.comparingInt(Configuration::order))
@@ -102,10 +99,10 @@ public class MonoBase {
     }
 
     public Meecrowave.Builder getConfiguration() {
-        return CONFIGURATION.get();
+        return CONTAINER.get().configuration;
     }
 
-    public Meecrowave.Builder startIfNeeded() {
+    public Instance startIfNeeded() {
         if (CONTAINER.get() == null) { // yes synchro could be simpler but it does the job,
feel free to rewrite it
             synchronized (CONTAINER) {
                 if (CONTAINER.get() == null) {
@@ -113,10 +110,32 @@ public class MonoBase {
                 }
             }
         }
-        return getConfiguration();
+        return CONTAINER.get();
     }
 
+    public static final class Instance {
+        private final Meecrowave container;
+        private final Meecrowave.Builder configuration;
+        private final ClassLoader startupClassLoader;
+
+        private Instance(final Meecrowave container, final Meecrowave.Builder configuration,
final ClassLoader startupClassLoader) {
+            this.container = container;
+            this.configuration = configuration;
+            this.startupClassLoader = startupClassLoader;
+        }
+
+        public Meecrowave getContainer() {
+            return container;
+        }
 
+        public Meecrowave.Builder getConfiguration() {
+            return configuration;
+        }
+
+        public ClassLoader getStartupClassLoader() {
+            return startupClassLoader;
+        }
+    }
 
     public interface Configuration {
         default int order() {

Modified: openwebbeans/meecrowave/trunk/meecrowave-junit/src/test/java/org/apache/meecrowave/junit/MonoMeecrowaveRuleTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-junit/src/test/java/org/apache/meecrowave/junit/MonoMeecrowaveRuleTest.java?rev=1818605&r1=1818604&r2=1818605&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-junit/src/test/java/org/apache/meecrowave/junit/MonoMeecrowaveRuleTest.java
(original)
+++ openwebbeans/meecrowave/trunk/meecrowave-junit/src/test/java/org/apache/meecrowave/junit/MonoMeecrowaveRuleTest.java
Mon Dec 18 18:38:38 2017
@@ -20,6 +20,7 @@ package org.apache.meecrowave.junit;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.fail;
+import static org.junit.rules.RuleChain.outerRule;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -32,17 +33,16 @@ import javax.inject.Inject;
 import org.apache.meecrowave.io.IO;
 import org.app.MyAppClass;
 import org.app.MyReqClass;
-import org.junit.ClassRule;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.TestRule;
 
 public class MonoMeecrowaveRuleTest {
-    @ClassRule
-    public static final MonoMeecrowave.Rule RULE = new MonoMeecrowave.Rule();
+    private final MonoMeecrowave.Rule meecrowave = new MonoMeecrowave.Rule().inject(this);
 
     @Rule
-    public final TestRule scopeRule = new ScopeRule(RequestScoped.class, SessionScoped.class);
+    public final TestRule scopeRule = outerRule(meecrowave)
+            .around(new ScopeRule(RequestScoped.class, SessionScoped.class));
 
     @Rule
     public final TestRule injectRule = new InjectRule(this);
@@ -55,14 +55,14 @@ public class MonoMeecrowaveRuleTest {
 
     @Test
     public void test() throws IOException {
-        assertEquals("simple", slurp(new URL("http://localhost:" + RULE.getConfiguration().getHttpPort()
+ "/api/test")));
+        assertEquals("simple", slurp(new URL("http://localhost:" + meecrowave.getConfiguration().getHttpPort()
+ "/api/test")));
 
         testScopes();
     }
 
     @Test
     public void anotherTest() throws IOException {
-        assertEquals("simple", slurp(new URL("http://localhost:" + RULE.getConfiguration().getHttpPort()
+ "/api/test")));
+        assertEquals("simple", slurp(new URL("http://localhost:" + meecrowave.getConfiguration().getHttpPort()
+ "/api/test")));
 
         testScopes();
     }



Mime
View raw message