philippkunz commented on a change in pull request #28:
URL: https://github.com/apache/openwebbeans/pull/28#discussion_r439814156



##########
File path: 
webbeans-junit5/src/main/java/org/apache/openwebbeans/junit5/internal/CdiExtension.java
##########
@@ -172,4 +182,76 @@ private void doClose(final SeContainer container)
             }
         });
     }
+
+    private Cdi getCdiConfig(ExtensionContext extensionContext)
+    {
+        return AnnotationUtils.findAnnotation(extensionContext.getElement(), 
Cdi.class).orElse(null);
+    }
+
+    private SeContainer getContainer()
+    {
+        if (testInstanceContainer != null)
+        {
+            return testInstanceContainer;
+        }
+        else
+        {
+            return reusableContainer;
+        }
+    }
+
+    @Override
+    public boolean supportsParameter(ParameterContext parameterContext, 
ExtensionContext extensionContext)
+            throws ParameterResolutionException
+    {
+        final Cdi config = 
extensionContext.getParent().map(this::getCdiConfig).orElse(null);
+        if (config == null || !config.injectParameters())
+        {
+            return false;
+        }
+
+        final SeContainer container = getContainer();
+        if (container == null)
+        {
+            return false;
+        }
+
+        Bean<?> bean = resolveParameterBean(container, parameterContext, 
extensionContext);
+        return bean != null;
+    }
+
+    @Override
+    public Object resolveParameter(ParameterContext parameterContext, 
ExtensionContext extensionContext)
+            throws ParameterResolutionException
+    {
+        final SeContainer container = getContainer();
+        if (container == null)
+        {
+            return false;
+        }
+
+        Bean<?> bean = resolveParameterBean(container, parameterContext, 
extensionContext);
+        BeanManager beanManager = container.getBeanManager();
+        CreationalContext<?> creationalContext = 
beanManager.createCreationalContext(bean);
+        creationalContexts.add(creationalContext);
+        return beanManager.getReference(bean, 
parameterContext.getParameter().getType(), creationalContext);
+    }
+
+    private Bean<?> resolveParameterBean(SeContainer container, 
ParameterContext parameterContext, ExtensionContext extensionContext)
+    {
+        BeanManager beanManager = container.getBeanManager();
+        Set<Bean<?>> beans = beanManager.getBeans(
+                parameterContext.getParameter().getType(),
+                getQualifiers(parameterContext.getParameter()));
+        return beanManager.resolve(beans);

Review comment:
       Regarding error handling you mention in "side note" I consider these 
cases relevant when a parameter is attempted to be resolved with a cdi bean:
   * no bean found -> `supportsParameter` returns false and parameter is not 
resolved in this extension
   * one bean found -> `supportsParameter` returns true and parameter is 
resolved in 
   this extension
   * more than one bean found / ambiguous -> `supportsParameter` raises an 
exception, currently an `AmbiguousResolutionException`
   
   "found" above of course implies everything cdi usually consideres such as 
qualifiers, scopes, etc..
   
   That can lead to a parameter not injected by cdi extension while a test 
expected that which will in an exception thrown by Junit and would most 
probably be caused and fixed in bean definitions, `@Cdi.classes`, etc.
   
   All the spurious cases fail this way and nothing has to be logged or 
anything.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to