openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject [openwebbeans] branch master updated: OWB-1297 drop parent creational context from Instance
Date Wed, 11 Sep 2019 11:46:44 GMT
This is an automated email from the ASF dual-hosted git repository.

rmannibucau pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/openwebbeans.git


The following commit(s) were added to refs/heads/master by this push:
     new e19de31  OWB-1297 drop parent creational context from Instance
e19de31 is described below

commit e19de316ff77cd461afc9d49b4973979d76ef775
Author: Romain Manni-Bucau <rmannibucau@gmail.com>
AuthorDate: Wed Sep 11 13:46:39 2019 +0200

    OWB-1297 drop parent creational context from Instance
---
 .../webbeans/inject/instance/InstanceImpl.java     | 29 ++++------
 .../test/instance/InstanceIteratorTest.java        | 65 ++++++++++++++++++++++
 2 files changed, 77 insertions(+), 17 deletions(-)

diff --git a/webbeans-impl/src/main/java/org/apache/webbeans/inject/instance/InstanceImpl.java
b/webbeans-impl/src/main/java/org/apache/webbeans/inject/instance/InstanceImpl.java
index c4b8bd7..7fb53f4 100644
--- a/webbeans-impl/src/main/java/org/apache/webbeans/inject/instance/InstanceImpl.java
+++ b/webbeans-impl/src/main/java/org/apache/webbeans/inject/instance/InstanceImpl.java
@@ -25,13 +25,11 @@ import java.io.Serializable;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Member;
 import java.lang.reflect.Type;
-import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.IdentityHashMap;
 import java.util.Iterator;
-import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
@@ -240,23 +238,20 @@ public class InstanceImpl<T> implements Instance<T>, Serializable
     @Override
     public Iterator<T> iterator()
     {
-        Set<Bean<?>> beans = resolveBeans();
-        List<T> instances = new ArrayList<>();
-        parentCreationalContext.putInjectionPoint(injectionPoint);
-        try
-        {
-            for(Bean<?> bean : beans)
+        final Iterator<Bean<?>> beans = resolveBeans().iterator();
+        return new Iterator<T>() {
+            @Override
+            public boolean hasNext()
             {
-                T instance = create(bean);
-                instances.add(instance);
+                return beans.hasNext();
             }
-        }
-        finally
-        {
-            parentCreationalContext.removeInjectionPoint();
-        }
-        
-        return instances.iterator();
+
+            @Override
+            public T next()
+            {
+                return create(beans.next());
+            }
+        };
     }
 
     public void destroy(T instance)
diff --git a/webbeans-impl/src/test/java/org/apache/webbeans/test/instance/InstanceIteratorTest.java
b/webbeans-impl/src/test/java/org/apache/webbeans/test/instance/InstanceIteratorTest.java
index 6625304..721cfc2 100644
--- a/webbeans-impl/src/test/java/org/apache/webbeans/test/instance/InstanceIteratorTest.java
+++ b/webbeans-impl/src/test/java/org/apache/webbeans/test/instance/InstanceIteratorTest.java
@@ -22,20 +22,30 @@ import org.junit.Assert;
 import org.apache.webbeans.test.AbstractUnitTest;
 import org.junit.Test;
 
+import javax.annotation.PreDestroy;
 import javax.enterprise.context.ApplicationScoped;
 import javax.enterprise.context.RequestScoped;
+import javax.enterprise.context.spi.AlterableContext;
 import javax.enterprise.inject.Any;
 import javax.enterprise.inject.Instance;
 import javax.enterprise.inject.Produces;
+import javax.enterprise.inject.spi.Bean;
 import javax.inject.Inject;
 import javax.inject.Qualifier;
 import java.lang.annotation.Retention;
 import java.lang.annotation.Target;
+import java.util.Collection;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.stream.StreamSupport;
 
 import static java.lang.annotation.ElementType.*;
 import static java.lang.annotation.RetentionPolicy.RUNTIME;
+import static java.util.stream.Collectors.toList;
 import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertFalse;
 import static junit.framework.Assert.assertNotNull;
+import static junit.framework.Assert.assertTrue;
 
 public class InstanceIteratorTest extends AbstractUnitTest
 {
@@ -86,7 +96,62 @@ public class InstanceIteratorTest extends AbstractUnitTest
         Assert.assertTrue(instanceIteratorHolder.iterateOverContracts());
     }
 
+    @Test
+    public void testDestroyDependents()
+    {
+        ABean.COUNT.set(0);
+        startContainer(GetDependents.class, ABean.class);
+
+        final Set<Bean<?>> rbs = getBeanManager().getBeans(GetDependents.class);
+        final Bean<?> rb = getBeanManager().resolve(rbs);
+        final GetDependents getter = GetDependents.class.cast(getBeanManager().getReference(
+                rb, GetDependents.class, getBeanManager().createCreationalContext(rb)));
+        assertNotNull(getter);
+
+        final Collection<ABean> beans = getter.get();
+        assertEquals(1, beans.size());
+
+        final ABean bean = beans.iterator().next();
+        assertFalse(bean.isDestroyed());
+        assertEquals(0, ABean.COUNT.get());
+
+        final AlterableContext alterableContext = AlterableContext.class.cast(
+                getBeanManager().getContext(ApplicationScoped.class));
+        alterableContext.destroy(rb);
+
+        assertTrue(bean.isDestroyed());
+        assertEquals(1, ABean.COUNT.get());
+    }
+
+    public static class ABean
+    {
+        private static final AtomicInteger COUNT = new AtomicInteger();
+        private boolean destroyed;
+
+        public boolean isDestroyed()
+        {
+            return destroyed;
+        }
+
+        @PreDestroy
+        private void destroy()
+        {
+            destroyed = true;
+            COUNT.incrementAndGet();
+        }
+    }
+
+    @ApplicationScoped
+    public static class GetDependents
+    {
+        @Inject
+        private Instance<ABean> beans;
 
+        public Collection<ABean> get()
+        {
+            return StreamSupport.stream(beans.spliterator(), false).collect(toList());
+        }
+    }
 
     public static class InstanceHolder
     {


Mime
View raw message