openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject svn commit: r1796427 - in /openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src: main/java/org/apache/webbeans/arquillian/standalone/ test/java/org/apache/webbeans/arquillian/test/ test/java/org/apache/webbeans/arquillian/test/beans/
Date Sat, 27 May 2017 16:11:48 GMT
Author: struberg
Date: Sat May 27 16:11:48 2017
New Revision: 1796427

URL: http://svn.apache.org/viewvc?rev=1796427&view=rev
Log:
OWB-1183 enable implicit bean archive discovery for Arquillian

Added:
    openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/test/java/org/apache/webbeans/arquillian/test/OwbArquillianImplicitBdaJarDeploymentTest.java
      - copied, changed from r1796329, openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/test/java/org/apache/webbeans/arquillian/test/OwbArquillianJarDeploymentTest.java
    openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/test/java/org/apache/webbeans/arquillian/test/beans/NonCdiBean.java
      - copied, changed from r1796329, openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/test/java/org/apache/webbeans/arquillian/test/beans/SampleUser.java
Modified:
    openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/main/java/org/apache/webbeans/arquillian/standalone/OwbArquillianScannerService.java

Modified: openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/main/java/org/apache/webbeans/arquillian/standalone/OwbArquillianScannerService.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/main/java/org/apache/webbeans/arquillian/standalone/OwbArquillianScannerService.java?rev=1796427&r1=1796426&r2=1796427&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/main/java/org/apache/webbeans/arquillian/standalone/OwbArquillianScannerService.java
(original)
+++ openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/main/java/org/apache/webbeans/arquillian/standalone/OwbArquillianScannerService.java
Sat May 27 16:11:48 2017
@@ -18,6 +18,8 @@
  */
 package org.apache.webbeans.arquillian.standalone;
 
+import javax.enterprise.inject.spi.BeanManager;
+
 import org.apache.webbeans.config.WebBeansContext;
 import org.apache.webbeans.spi.BDABeansXmlScanner;
 import org.apache.webbeans.spi.BeanArchiveService;
@@ -35,6 +37,7 @@ import org.jboss.shrinkwrap.api.spec.Jav
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.lang.annotation.Annotation;
 import java.net.URL;
 import java.net.URLConnection;
 import java.net.URLStreamHandler;
@@ -52,7 +55,9 @@ public class OwbArquillianScannerService
 
     private final boolean beansXmlBdaScanningEnabled;
     private final WebBeansContext webBeansContext;
-    private final BeanArchiveService archiveService;
+    private final BeanManager beanManager;
+    private final BeanArchiveService beanArchiveService;
+
 
     private Archive archive;
 
@@ -64,7 +69,8 @@ public class OwbArquillianScannerService
     {
         this.beansXmlBdaScanningEnabled = false;
         webBeansContext = WebBeansContext.getInstance();
-        archiveService = webBeansContext.getBeanArchiveService();
+        this.beanManager = webBeansContext.getBeanManagerImpl();
+        beanArchiveService = webBeansContext.getBeanArchiveService();
     }
 
     @Override
@@ -151,7 +157,7 @@ public class OwbArquillianScannerService
 
         if (metainfBeansXmlUrl != null || webBeansXmlUrl != null)
         {
-            final BeanArchiveService.BeanArchiveInformation info = archiveService.getBeanArchiveInformation(webBeansXmlUrl
!= null ? webBeansXmlUrl : metainfBeansXmlUrl);
+            final BeanArchiveService.BeanArchiveInformation info = beanArchiveService.getBeanArchiveInformation(webBeansXmlUrl
!= null ? webBeansXmlUrl : metainfBeansXmlUrl);
 
             // in this case we need to scan the WEB-INF/classses folder for .class files
             Map<ArchivePath, Node> classes = archive.getContent(Filters.include(WEB_INF_CLASS_FOLDER
+ ".*\\.class"));
@@ -188,17 +194,16 @@ public class OwbArquillianScannerService
 
         if (beansXmlUrl == null)
         {
-            // this is not a CDI archive
-            return;
+            // that means we switch to 'Implicit BDA' mode
+            beansXmlUrl = getBeanXmlUrl(archive, "/");
         }
 
-
         // otherwise we store it for later use
         beansXmls.add(beansXmlUrl);
 
-        // and now add all classes
+        // and now scan all classes those classes acording to their BDA info
         Map<ArchivePath, Node> classes = archive.getContent(Filters.include(".*\\.class"));
-        scanClasses(archiveService.getBeanArchiveInformation(beansXmlUrl), classes, null);
+        scanClasses(beanArchiveService.getBeanArchiveInformation(beansXmlUrl), classes, null);
     }
 
     /**
@@ -209,6 +214,12 @@ public class OwbArquillianScannerService
     private void scanClasses(final BeanArchiveService.BeanArchiveInformation info,
                              final Map<ArchivePath, Node> classes, String classBasePath)
     {
+        if (info != null && info.getBeanDiscoveryMode() == BeanArchiveService.BeanDiscoveryMode.NONE)
+        {
+            // this jar should not get scanned at all.
+            return;
+        }
+
         for (Map.Entry<ArchivePath, Node> classEntry : classes.entrySet())
         {
             String className = classEntry.getKey().get();
@@ -229,24 +240,51 @@ public class OwbArquillianScannerService
 
             className = className.replace('/', '.');
 
-            if (info != null && info.isClassExcluded(className))
-            {
-                continue;
-            }
-
-
+            Class<?> beanClass = null;
             try
             {
-                Class<?> beanClass = Class.forName(className);
-                beanClasses.add(beanClass);
+                beanClass = Class.forName(className);
             }
             catch (ClassNotFoundException cnfe)
             {
                 throw new RuntimeException("Could not scan class", cnfe);
             }
+
+            if (info != null && info.isClassExcluded(className))
+            {
+                continue;
+            }
+            if (info != null && info.getBeanDiscoveryMode() == BeanArchiveService.BeanDiscoveryMode.ANNOTATED)
+            {
+                // only classes with a 'Bean Defining Annotation should get included
+                boolean hasBeanDefiningAnnotation = false;
+                for (Annotation annotation : beanClass.getAnnotations())
+                {
+                    if (isBeanDefiningAnnotation(annotation))
+                    {
+                        hasBeanDefiningAnnotation = true;
+                        break;
+                    }
+                }
+                if (!hasBeanDefiningAnnotation)
+                {
+                    continue;
+                }
+            }
+
+            beanClasses.add(beanClass);
         }
     }
 
+    protected boolean isBeanDefiningAnnotation(Annotation annotation)
+    {
+        Class<? extends Annotation> annotationType = annotation.annotationType();
+        boolean isBeanAnnotation = beanManager.isScope(annotationType);
+        isBeanAnnotation = isBeanAnnotation || beanManager.isStereotype(annotationType);
+
+        return isBeanAnnotation;
+    }
+
     private URL getBeanXmlUrl(Archive archive, String beansXmlPath)
     {
         final Node beansXml = archive.get(beansXmlPath);

Copied: openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/test/java/org/apache/webbeans/arquillian/test/OwbArquillianImplicitBdaJarDeploymentTest.java
(from r1796329, openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/test/java/org/apache/webbeans/arquillian/test/OwbArquillianJarDeploymentTest.java)
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/test/java/org/apache/webbeans/arquillian/test/OwbArquillianImplicitBdaJarDeploymentTest.java?p2=openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/test/java/org/apache/webbeans/arquillian/test/OwbArquillianImplicitBdaJarDeploymentTest.java&p1=openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/test/java/org/apache/webbeans/arquillian/test/OwbArquillianJarDeploymentTest.java&r1=1796329&r2=1796427&rev=1796427&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/test/java/org/apache/webbeans/arquillian/test/OwbArquillianJarDeploymentTest.java
(original)
+++ openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/test/java/org/apache/webbeans/arquillian/test/OwbArquillianImplicitBdaJarDeploymentTest.java
Sat May 27 16:11:48 2017
@@ -18,39 +18,38 @@
  */
 package org.apache.webbeans.arquillian.test;
 
+import javax.enterprise.inject.spi.BeanManager;
 import javax.inject.Inject;
 
+import org.apache.webbeans.arquillian.test.beans.NonCdiBean;
 import org.apache.webbeans.arquillian.test.beans.SampleUser;
 import org.jboss.arquillian.container.test.api.Deployment;
 import org.jboss.arquillian.junit.Arquillian;
 import org.jboss.shrinkwrap.api.ShrinkWrap;
-import org.jboss.shrinkwrap.api.asset.EmptyAsset;
 import org.jboss.shrinkwrap.api.spec.JavaArchive;
-
 import org.junit.Assert;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
 
 /**
- * This test verifies the deployment of a JAR packaging.
+ * This test verifies the deployment of a JAR packaging with implicit BDAs.
+ * This is if NO beans.xml is present!
  */
 @RunWith(Arquillian.class)
-public class OwbArquillianJarDeploymentTest
+public class OwbArquillianImplicitBdaJarDeploymentTest
 {
+    private @Inject SampleUser sampleUser;
 
-    @Inject
-    private SampleUser sampleUser;
-
+    private @Inject BeanManager bm;
 
     @Deployment
     public static JavaArchive deploy()
     {
         JavaArchive testJar = ShrinkWrap
-                .create(JavaArchive.class, "sampleUserTest.jar")
-                .addClass(OwbArquillianJarDeploymentTest.class)
-                .addPackage(SampleUser.class.getPackage())
-                .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
+                .create(JavaArchive.class, "sampleImplicitBdaTest.jar")
+                .addClass(OwbArquillianImplicitBdaJarDeploymentTest.class)
+                .addPackage(SampleUser.class.getPackage());
 
         return testJar;
     }
@@ -63,5 +62,8 @@ public class OwbArquillianJarDeploymentT
 
         sampleUser.setFirstName("Karl");
         Assert.assertEquals("Karl", sampleUser.getFirstName());
+
+        // but the NonCdiBean should not be picked up in an implicit BDA
+        Assert.assertEquals(0, bm.getBeans(NonCdiBean.class).size());
     }
 }

Copied: openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/test/java/org/apache/webbeans/arquillian/test/beans/NonCdiBean.java
(from r1796329, openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/test/java/org/apache/webbeans/arquillian/test/beans/SampleUser.java)
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/test/java/org/apache/webbeans/arquillian/test/beans/NonCdiBean.java?p2=openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/test/java/org/apache/webbeans/arquillian/test/beans/NonCdiBean.java&p1=openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/test/java/org/apache/webbeans/arquillian/test/beans/SampleUser.java&r1=1796329&r2=1796427&rev=1796427&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/test/java/org/apache/webbeans/arquillian/test/beans/SampleUser.java
(original)
+++ openwebbeans/trunk/webbeans-arquillian/owb-arquillian-standalone/src/test/java/org/apache/webbeans/arquillian/test/beans/NonCdiBean.java
Sat May 27 16:11:48 2017
@@ -18,31 +18,22 @@
  */
 package org.apache.webbeans.arquillian.test.beans;
 
-import javax.annotation.PostConstruct;
-import javax.enterprise.context.SessionScoped;
 import java.io.Serializable;
 
 /**
- * A sample User to get injected into an Arquillian test
+ * A class which doesn't use any CDI stuff.
  */
-@SessionScoped
-public class SampleUser implements Serializable
+public class NonCdiBean implements Serializable
 {
-    private String firstName;
+    private int i;
 
-    @PostConstruct
-    public void init()
+    public int getI()
     {
-        firstName = "Hans";
+        return i;
     }
 
-    public String getFirstName()
+    public void setI(int i)
     {
-        return firstName;
-    }
-
-    public void setFirstName(String firstName)
-    {
-        this.firstName = firstName;
+        this.i = i;
     }
 }



Mime
View raw message