You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rm...@apache.org on 2015/05/18 14:31:21 UTC

tomee git commit: TOMEE-1587 light merge of 7.x ear support for arquillian tomee adapter

Repository: tomee
Updated Branches:
  refs/heads/tomee-1.7.x 863c1ac11 -> b901386f0


TOMEE-1587 light merge of 7.x ear support for arquillian tomee adapter


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

Branch: refs/heads/tomee-1.7.x
Commit: b901386f0e02f3f1d325195e20a9ddd8858c9e47
Parents: 863c1ac
Author: Romain Manni-Bucau <rm...@apache.org>
Authored: Mon May 18 14:30:14 2015 +0200
Committer: Romain Manni-Bucau <rm...@apache.org>
Committed: Mon May 18 14:30:14 2015 +0200

----------------------------------------------------------------------
 .../common/enrichment/OpenEJBEnricher.java      | 54 +++++++++++++-------
 .../arquillian/common/TestClassDiscoverer.java  | 22 ++++++--
 .../openejb/assembler/classic/Assembler.java    |  3 +-
 3 files changed, 57 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/b901386f/arquillian/arquillian-common/src/main/java/org/apache/openejb/arquillian/common/enrichment/OpenEJBEnricher.java
----------------------------------------------------------------------
diff --git a/arquillian/arquillian-common/src/main/java/org/apache/openejb/arquillian/common/enrichment/OpenEJBEnricher.java b/arquillian/arquillian-common/src/main/java/org/apache/openejb/arquillian/common/enrichment/OpenEJBEnricher.java
index c606858..d5e3bbc 100644
--- a/arquillian/arquillian-common/src/main/java/org/apache/openejb/arquillian/common/enrichment/OpenEJBEnricher.java
+++ b/arquillian/arquillian-common/src/main/java/org/apache/openejb/arquillian/common/enrichment/OpenEJBEnricher.java
@@ -24,6 +24,7 @@ import org.apache.openejb.OpenEJBRuntimeException;
 import org.apache.openejb.arquillian.common.mockito.MockitoEnricher;
 import org.apache.openejb.core.Operation;
 import org.apache.openejb.core.ThreadContext;
+import org.apache.openejb.core.WebContext;
 import org.apache.openejb.loader.SystemInstance;
 import org.apache.openejb.spi.ContainerSystem;
 import org.apache.openejb.util.AppFinder;
@@ -65,25 +66,32 @@ public final class OpenEJBEnricher {
 
         final BeanContext context = SystemInstance.get().getComponent(ContainerSystem.class).getBeanContext(ctx.getId() + "_" + testInstance.getClass().getName());
 
-        final BeanManagerImpl bm = findBeanManager(ctx);
-        if (bm != null && bm.isInUse()) {
-            try {
-                final Set<Bean<?>> beans = bm.getBeans(testInstance.getClass());
-                final Bean<?> bean = bm.resolve(beans);
-                final CreationalContext<?> cc = bm.createCreationalContext(bean);
-                if (context != null) {
-                    context.set(CreationalContext.class, cc);
-                }
-                OWBInjector.inject(bm, testInstance, cc);
-            } catch (final Throwable t) {
-                LOGGER.log(Level.SEVERE, "Failed injection on: " + testInstance.getClass(), t);
-                if (RuntimeException.class.isInstance(t)) {
-                    throw RuntimeException.class.cast(t);
+        final WebBeansContext appWBC = ctx.getWebBeansContext();
+        final BeanManagerImpl bm = appWBC.getBeanManagerImpl();
+
+        boolean ok = false;
+        for (final WebContext web : ctx.getWebContexts()) {
+            final WebBeansContext webBeansContext = web.getWebBeansContext();
+            final BeanManagerImpl webAppBm = webBeansContext.getBeanManagerImpl();
+            if (webBeansContext != appWBC && webAppBm.isInUse()) {
+                try {
+                    doInject(testInstance, context, webAppBm);
+                    ok = true;
+                    break;
+                } catch (final Exception e) {
+                    // no-op, try next
                 }
-                if (Exception.class.isInstance(t)) {
-                    throw new OpenEJBRuntimeException(Exception.class.cast(t));
+            }
+        }
+        if (bm != null && bm.isInUse() && !ok) {
+            try {
+                doInject(testInstance, context, bm);
+            } catch (final Exception e) {
+                LOGGER.log(Level.SEVERE, "Failed injection on: " + testInstance.getClass(), e);
+                if (RuntimeException.class.isInstance(e)) {
+                    throw RuntimeException.class.cast(e);
                 }
-                // ignoring other cases for the moment, let manage some OWB API change without making all tests failing
+                throw new OpenEJBRuntimeException(e);
             }
         }
 
@@ -91,7 +99,7 @@ public final class OpenEJBEnricher {
             final ThreadContext callContext = new ThreadContext(context, null, Operation.INJECTION);
             final ThreadContext oldContext = ThreadContext.enter(callContext);
             try {
-                final InjectionProcessor processor = new InjectionProcessor<Object>(testInstance, context.getInjections(), context.getJndiContext());
+                final InjectionProcessor processor = new InjectionProcessor(testInstance, context.getInjections(), context.getJndiContext());
                 processor.createInstance();
             } catch (final OpenEJBException e) {
                 // ignored
@@ -101,6 +109,16 @@ public final class OpenEJBEnricher {
         }
     }
 
+    private static void doInject(final Object testInstance, final BeanContext context, final BeanManagerImpl bm) throws Exception {
+        final Set<Bean<?>> beans = bm.getBeans(testInstance.getClass());
+        final Bean<?> bean = bm.resolve(beans);
+        final CreationalContext<?> cc = bm.createCreationalContext(bean);
+        if (context != null) {
+            context.set(CreationalContext.class, cc);
+        }
+        OWBInjector.inject(bm, testInstance, cc);
+    }
+
     private static BeanManagerImpl findBeanManager(final AppContext ctx) {
         if (ctx != null) {
             return ctx.getWebBeansContext().getBeanManagerImpl();

http://git-wip-us.apache.org/repos/asf/tomee/blob/b901386f/arquillian/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/TestClassDiscoverer.java
----------------------------------------------------------------------
diff --git a/arquillian/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/TestClassDiscoverer.java b/arquillian/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/TestClassDiscoverer.java
index a9e06d0..45566fd 100644
--- a/arquillian/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/TestClassDiscoverer.java
+++ b/arquillian/arquillian-tomee-common/src/main/java/org/apache/openejb/arquillian/common/TestClassDiscoverer.java
@@ -75,6 +75,8 @@ public class TestClassDiscoverer implements AdditionalBeanDiscoverer {
         // keep it since CukeSpace doesn't rely on JUnit or TestNG @Test so it stays mandatory
         final File file = module.getFile();
         final String line = findTestName(file, module.getClassLoader());
+        String moduleId = null;
+        ClassLoader loader = null;
         if (line != null) {
             String name;
             final int endIndex = line.indexOf('#');
@@ -89,10 +91,18 @@ public class TestClassDiscoverer implements AdditionalBeanDiscoverer {
 
             if (name != null) {
                 try {
-                    // call some reflection methods to make it fail if some dep are missing...
                     testClasses.add(module.getClassLoader().loadClass(name));
                 } catch (final Throwable e) {
-                    // no-op
+                    for (final WebModule web : module.getWebModules()) {
+                        try {
+                            testClasses.add(web.getClassLoader().loadClass(name));
+                            moduleId = web.getModuleId();
+                            loader = web.getClassLoader();
+                            break;
+                        } catch (final Throwable e2) {
+                            // no-op
+                        }
+                    }
                 }
             }
         }
@@ -130,7 +140,13 @@ public class TestClassDiscoverer implements AdditionalBeanDiscoverer {
             bean.setTransactionType(TransactionType.BEAN);
             final EjbDeployment ejbDeployment = openejbJar.addEjbDeployment(bean);
             ejbDeployment.setDeploymentId(ejbName);
-            module.getEjbModules().add(new EjbModule(ejbJar, openejbJar));
+            final EjbModule ejbModule = new EjbModule(ejbJar, openejbJar);
+            if (moduleId != null) {
+                ejbModule.setWebapp(true);
+                ejbModule.getProperties().put("openejb.ejbmodule.webappId", moduleId);
+                ejbModule.setClassLoader(loader); // can be a web module so set it
+            }
+            module.getEjbModules().add(ejbModule);
         }
 
         return module;

http://git-wip-us.apache.org/repos/asf/tomee/blob/b901386f/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java b/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java
index 9c3243f..e5c1899 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java
@@ -1195,7 +1195,8 @@ public class Assembler extends AssemblerTool implements org.apache.openejb.spi.A
             if (!appInfo.webAppAlone) {
                 if (webappId == null) {
                     skip = ejbJar.webapp; // we look for the lib part of the ear so deploy only if not a webapp
-                } else if (!ejbJar.webapp || !ejbJar.moduleId.equals(webappId)) {
+                } else if (!ejbJar.webapp
+                        || (!ejbJar.moduleId.equals(webappId) && !ejbJar.properties.getProperty("openejb.ejbmodule.webappId", "-").equals(webappId))) {
                     skip = true; // we look for a particular webapp deployment so deploy only if this webapp
                 }
             }