tomee-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1614464 - /tomee/tomee/trunk/arquillian/arquillian-common/src/main/java/org/apache/openejb/arquillian/common/TestObserver.java
Date Tue, 29 Jul 2014 19:15:29 GMT
Author: rmannibucau
Date: Tue Jul 29 19:15:29 2014
New Revision: 1614464

URL: http://svn.apache.org/r1614464
Log:
for testng we need the right classloader before TestEvents so pushing it as we can

Modified:
    tomee/tomee/trunk/arquillian/arquillian-common/src/main/java/org/apache/openejb/arquillian/common/TestObserver.java

Modified: tomee/tomee/trunk/arquillian/arquillian-common/src/main/java/org/apache/openejb/arquillian/common/TestObserver.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/arquillian/arquillian-common/src/main/java/org/apache/openejb/arquillian/common/TestObserver.java?rev=1614464&r1=1614463&r2=1614464&view=diff
==============================================================================
--- tomee/tomee/trunk/arquillian/arquillian-common/src/main/java/org/apache/openejb/arquillian/common/TestObserver.java
(original)
+++ tomee/tomee/trunk/arquillian/arquillian-common/src/main/java/org/apache/openejb/arquillian/common/TestObserver.java
Tue Jul 29 19:15:29 2014
@@ -21,12 +21,16 @@ import org.apache.openejb.BeanContext;
 import org.apache.openejb.core.ThreadContext;
 import org.apache.openejb.loader.SystemInstance;
 import org.apache.openejb.spi.ContainerSystem;
+import org.jboss.arquillian.container.spi.context.annotation.DeploymentScoped;
+import org.jboss.arquillian.container.spi.event.container.AfterDeploy;
 import org.jboss.arquillian.container.spi.event.container.BeforeUnDeploy;
 import org.jboss.arquillian.core.api.Instance;
+import org.jboss.arquillian.core.api.InstanceProducer;
 import org.jboss.arquillian.core.api.annotation.Inject;
 import org.jboss.arquillian.core.api.annotation.Observes;
 import org.jboss.arquillian.core.spi.EventContext;
 import org.jboss.arquillian.test.spi.TestClass;
+import org.jboss.arquillian.test.spi.event.suite.ClassLifecycleEvent;
 import org.jboss.arquillian.test.spi.event.suite.TestEvent;
 
 import javax.enterprise.context.spi.CreationalContext;
@@ -38,16 +42,40 @@ public class TestObserver {
     @Inject
     private Instance<TestClass> testClass;
 
-    public void observes(@Observes final EventContext<TestEvent> event) {
+    @Inject
+    @DeploymentScoped
+    private InstanceProducer<DeploymentContext> contextProducer;
+
+    @Inject
+    private Instance<DeploymentContext> context;
+
+    public void observesDeploy(@Observes final AfterDeploy afterDeployment) {
+        contextProducer.set(new DeploymentContext(Thread.currentThread().getContextClassLoader()));
+        final ClassLoader loader = classLoader.get();
+        if (loader != null) {
+            setTCCL(loader);
+        }
+    }
+
+    public void observesUndeploy(@Observes final BeforeUnDeploy beforeUnDeploy) {
+        final DeploymentContext deploymentContext = context.get();
+        if (deploymentContext != null) {
+            setTCCL(deploymentContext.loader);
+        }
+    }
+
+    public void observesTest(@Observes final EventContext<TestEvent> event) {
+        switchLoader(event);
+    }
+
+    private void switchLoader(final EventContext<?> event) {
         if (!SystemInstance.isInitialized()) {
             event.proceed();
             return;
         }
-
         final BeanContext context = beanContext();
         ThreadContext oldCtx = null;
         ClassLoader oldCl = null;
-
         if (context != null) {
             oldCtx = ThreadContext.enter(new ThreadContext(context, null));
         } else {
@@ -56,7 +84,6 @@ public class TestObserver {
                 setTCCL(classLoader.get());
             }
         }
-
         try {
             event.proceed();
         } finally {
@@ -102,4 +129,12 @@ public class TestObserver {
         }
         return null;
     }
+
+    public static class DeploymentContext {
+        private final ClassLoader loader;
+
+        public DeploymentContext(final ClassLoader loader) {
+            this.loader = loader;
+        }
+    }
 }



Mime
View raw message