deltaspike-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gpetra...@apache.org
Subject git commit: DELTASPIKE-593 skip mocking support in case of intercepted beans
Date Fri, 23 May 2014 09:21:11 GMT
Repository: deltaspike
Updated Branches:
  refs/heads/master 0f18eccc4 -> 8bfcbb6e9


DELTASPIKE-593 skip mocking support in case of intercepted beans


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

Branch: refs/heads/master
Commit: 8bfcbb6e9798bb488309afcda1a32cae8714a2f3
Parents: 0f18ecc
Author: gpetracek <gpetracek@apache.org>
Authored: Fri May 23 10:51:55 2014 +0200
Committer: gpetracek <gpetracek@apache.org>
Committed: Fri May 23 11:16:54 2014 +0200

----------------------------------------------------------------------
 .../testcontrol/spi/mock/MockFilter.java        |  3 +-
 .../impl/mock/DefaultMockFilter.java            | 77 ++++++++++++++++++--
 .../testcontrol/impl/mock/MockExtension.java    |  4 +-
 3 files changed, 75 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/8bfcbb6e/deltaspike/modules/test-control/api/src/main/java/org/apache/deltaspike/testcontrol/spi/mock/MockFilter.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/test-control/api/src/main/java/org/apache/deltaspike/testcontrol/spi/mock/MockFilter.java
b/deltaspike/modules/test-control/api/src/main/java/org/apache/deltaspike/testcontrol/spi/mock/MockFilter.java
index 23cfbc3..43e682c 100644
--- a/deltaspike/modules/test-control/api/src/main/java/org/apache/deltaspike/testcontrol/spi/mock/MockFilter.java
+++ b/deltaspike/modules/test-control/api/src/main/java/org/apache/deltaspike/testcontrol/spi/mock/MockFilter.java
@@ -21,8 +21,9 @@ package org.apache.deltaspike.testcontrol.spi.mock;
 import org.apache.deltaspike.core.spi.activation.Deactivatable;
 
 import javax.enterprise.inject.spi.Annotated;
+import javax.enterprise.inject.spi.BeanManager;
 
 public interface MockFilter extends Deactivatable
 {
-    boolean isMockedImplementationSupported(Annotated annotated);
+    boolean isMockedImplementationSupported(BeanManager beanManager, Annotated annotated);
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/8bfcbb6e/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/DefaultMockFilter.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/DefaultMockFilter.java
b/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/DefaultMockFilter.java
index 71b215a..3feecc2 100644
--- a/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/DefaultMockFilter.java
+++ b/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/DefaultMockFilter.java
@@ -23,28 +23,93 @@ import org.apache.deltaspike.testcontrol.spi.mock.MockFilter;
 import javax.enterprise.inject.spi.Annotated;
 import javax.enterprise.inject.spi.AnnotatedMember;
 import javax.enterprise.inject.spi.AnnotatedType;
+import javax.enterprise.inject.spi.BeanManager;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Member;
+import java.util.Set;
+import java.util.logging.Logger;
 
 public class DefaultMockFilter implements MockFilter
 {
+    private static final Logger LOG = Logger.getLogger(DefaultMockFilter.class.getName());
+
     private static final String DS_BASE_PACKAGE = "org.apache.deltaspike.";
+    private static final String JAVA_BASE_PACKAGE = "java.";
+    private static final String JAVAX_BASE_PACKAGE = "javax.";
     private static final String OWB_BASE_PACKAGE = "org.apache.webbeans.";
     private static final String WELD_BASE_PACKAGE = "org.jboss.weld.";
 
     @Override
-    public boolean isMockedImplementationSupported(Annotated annotated)
+    public boolean isMockedImplementationSupported(BeanManager beanManager, Annotated annotated)
     {
-        String packageName = null;
-
+        Class origin = null;
         if (annotated instanceof AnnotatedType)
         {
-            packageName = ((AnnotatedType)annotated).getJavaClass().getPackage().getName();
+
+            origin = ((AnnotatedType)annotated).getJavaClass();
+            if (isAnnotatedTypeWithInterceptorAnnotation(beanManager, annotated.getAnnotations(),
origin.getName()))
+            {
+                return false;
+            }
         }
         else if (annotated instanceof AnnotatedMember)
         {
-            packageName = ((AnnotatedMember)annotated).getJavaMember().getDeclaringClass().getPackage().getName();
+            Member member = ((AnnotatedMember)annotated).getJavaMember();
+            origin = member.getDeclaringClass();
+            if (isAnnotatedTypeWithInterceptorAnnotation(beanManager, annotated.getAnnotations(),
member.toString()))
+            {
+                return false;
+            }
         }
 
-        return packageName != null && !isInternalPackage(packageName);
+        return origin != null && !isInternalPackage(origin.getPackage().getName());
+    }
+
+    protected boolean isAnnotatedTypeWithInterceptorAnnotation(BeanManager beanManager,
+                                                               Set<Annotation> annotations,
+                                                               String origin)
+    {
+        for (Annotation annotation : annotations)
+        {
+            if (isStandardAnnotation(annotation))
+            {
+                continue;
+            }
+
+            if (beanManager.isInterceptorBinding(annotation.annotationType()) ||
+                    (beanManager.isStereotype(annotation.annotationType()) &&
+                            isStereotypeWithInterceptor(annotation, beanManager)))
+            {
+                LOG.warning("Skip mocking intercepted bean " + origin);
+
+                return true;
+            }
+        }
+        return false;
+    }
+
+    protected boolean isStereotypeWithInterceptor(Annotation stereotypeAnnotation, BeanManager
beanManager)
+    {
+        for (Annotation annotation : stereotypeAnnotation.annotationType().getAnnotations())
+        {
+            if (isStandardAnnotation(annotation))
+            {
+                continue;
+            }
+
+            if (beanManager.isInterceptorBinding(annotation.annotationType()) ||
+                    isStereotypeWithInterceptor(annotation, beanManager))
+            {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    protected boolean isStandardAnnotation(Annotation annotation)
+    {
+        return annotation.annotationType().getName().startsWith(JAVA_BASE_PACKAGE) ||
+                annotation.annotationType().getName().startsWith(JAVAX_BASE_PACKAGE);
     }
 
     protected boolean isInternalPackage(String packageName)

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/8bfcbb6e/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/MockExtension.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/MockExtension.java
b/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/MockExtension.java
index a708ca1..2610d19 100644
--- a/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/MockExtension.java
+++ b/deltaspike/modules/test-control/impl/src/main/java/org/apache/deltaspike/testcontrol/impl/mock/MockExtension.java
@@ -60,7 +60,7 @@ public class MockExtension implements Extension, Deactivatable
 
         for (MockFilter mockFilter : mockFilters)
         {
-            if (!mockFilter.isMockedImplementationSupported(processInjectionTarget.getAnnotatedType()))
+            if (!mockFilter.isMockedImplementationSupported(beanManager, processInjectionTarget.getAnnotatedType()))
             {
                 return;
             }
@@ -106,7 +106,7 @@ public class MockExtension implements Extension, Deactivatable
 
         for (MockFilter mockFilter : mockFilters)
         {
-            if (!mockFilter.isMockedImplementationSupported(processProducer.getAnnotatedMember()))
+            if (!mockFilter.isMockedImplementationSupported(beanManager, processProducer.getAnnotatedMember()))
             {
                 return;
             }


Mime
View raw message