openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1861094 - in /openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test: AbstractUnitTest.java contexts/SerializationTest.java
Date Wed, 12 Jun 2019 07:51:18 GMT
Author: rmannibucau
Date: Wed Jun 12 07:51:17 2019
New Revision: 1861094

URL: http://svn.apache.org/viewvc?rev=1861094&view=rev
Log:
OWB-1289 test for serialization for normal scoped beans, the getReference usage is just awesome
for us in that case

Modified:
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/AbstractUnitTest.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/contexts/SerializationTest.java

Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/AbstractUnitTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/AbstractUnitTest.java?rev=1861094&r1=1861093&r2=1861094&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/AbstractUnitTest.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/AbstractUnitTest.java
Wed Jun 12 07:51:17 2019
@@ -18,8 +18,12 @@
  */
 package org.apache.webbeans.test;
 
+import static java.util.stream.Collectors.toMap;
+
+import org.apache.webbeans.config.OpenWebBeansConfiguration;
 import org.apache.webbeans.config.WebBeansContext;
 import org.apache.webbeans.config.WebBeansFinder;
+import org.apache.webbeans.corespi.DefaultSingletonService;
 import org.apache.webbeans.exception.WebBeansConfigurationException;
 import org.apache.webbeans.inject.OWBInjector;
 import org.apache.webbeans.lifecycle.StandaloneLifeCycle;
@@ -27,6 +31,7 @@ import org.apache.webbeans.lifecycle.tes
 import org.apache.webbeans.spi.ContainerLifecycle;
 import org.apache.webbeans.spi.ContextsService;
 import org.apache.webbeans.spi.ScannerService;
+import org.apache.webbeans.spi.SingletonService;
 import org.apache.webbeans.util.WebBeansUtil;
 import org.junit.After;
 import org.junit.Assert;
@@ -36,6 +41,7 @@ import javax.enterprise.inject.spi.Bean;
 import javax.enterprise.inject.spi.BeanManager;
 import javax.enterprise.inject.spi.Extension;
 import java.lang.annotation.Annotation;
+import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Type;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -45,6 +51,8 @@ import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
+import java.util.function.BinaryOperator;
+import java.util.stream.Collector;
 
 
 public abstract class AbstractUnitTest
@@ -124,19 +132,46 @@ public abstract class AbstractUnitTest
         final Collection<Class<?>> beanClasses = new ArrayList<Class<?>>();
// ensure it is updatable
         beanClasses.addAll(rawBeanClasses);
 
-        WebBeansFinder.clearInstances(WebBeansUtil.getCurrentClassLoader());
+        final ClassLoader currentClassLoader = WebBeansUtil.getCurrentClassLoader();
+        WebBeansFinder.clearInstances(currentClassLoader);
         //Creates a new container
+        final SingletonService<WebBeansContext> singletonInstance = WebBeansFinder.getSingletonService();
+        final boolean lateServiceRegistration = !DefaultSingletonService.class.isInstance(singletonInstance);
+        if (!lateServiceRegistration)
+        {
+            final Map<Class<?>, Object> servicesInstances = services.entrySet().stream()
+                    .filter(it -> !Class.class.isInstance(it.getValue()))
+                    .collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
+
+            final Map<Class<?>, Object> wbcAwareServices = new HashMap<>(services);
+            servicesInstances.keySet().forEach(wbcAwareServices::remove);
+
+            final Properties properties = wbcAwareServices.entrySet().stream()
+                    .collect(Collector.of(
+                        Properties::new,
+                        (p, e) -> p.setProperty(e.getKey().getName(), Class.class.cast(e.getValue()).getName()),
+                        (properties1, properties2) -> {
+                            properties1.putAll(properties2);
+                            return properties1;
+                        }));
+
+            final WebBeansContext context = new WebBeansContext(servicesInstances, properties);
+            DefaultSingletonService.class.cast(singletonInstance).register(currentClassLoader,
context);
+        }
         testLifecycle = new StandaloneLifeCycle()
         {
             @Override
             public void beforeInitApplication(final Properties properties)
             {
                 final WebBeansContext instance = WebBeansContext.getInstance();
-                services.forEach((k, v) ->
+                if (lateServiceRegistration)
                 {
-                    final Class key = k;
-                    instance.registerService(key, v);
-                });
+                    services.forEach((k, v) ->
+                    {
+                        final Object impl = k.cast(getServiceInstance(instance, v));
+                        instance.registerService(Class.class.cast(k), impl);
+                    });
+                }
                 if (!services.containsKey(ScannerService.class))
                 {
                     instance.registerService(ScannerService.class, new OpenWebBeansTestMetaDataDiscoveryService());
@@ -197,6 +232,20 @@ public abstract class AbstractUnitTest
         }
     }
 
+    private Object getServiceInstance(final WebBeansContext instance, final Object v)
+    {
+        try
+        {
+            return Class.class.isInstance(v) ?
+                    Class.class.cast(v).getConstructor(WebBeansContext.class).newInstance(instance)
:
+                    v;
+        }
+        catch (final Exception e)
+        {
+            throw new IllegalArgumentException(e);
+        }
+    }
+
     public void inject(final Object bean)
     {
         OWBInjector.inject(getBeanManager(), bean, null);
@@ -263,6 +312,10 @@ public abstract class AbstractUnitTest
         return (T) getBeanManager().getReference(bean, Object.class, getBeanManager().createCreationalContext(bean));
     }
 
+    protected <T> void addService(final Class<T> type, final Class<? extends
T> instance)
+    {
+        this.services.put(type, instance);
+    }
 
     protected <T> void addService(final Class<T> type, final T instance)
     {

Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/contexts/SerializationTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/contexts/SerializationTest.java?rev=1861094&r1=1861093&r2=1861094&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/contexts/SerializationTest.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/contexts/SerializationTest.java
Wed Jun 12 07:51:17 2019
@@ -19,9 +19,13 @@
 package org.apache.webbeans.test.contexts;
 
 
+import static org.junit.Assert.assertTrue;
+
 import org.apache.webbeans.config.WebBeansContext;
 import org.apache.webbeans.container.SerializableBean;
 import org.apache.webbeans.context.SessionContext;
+import org.apache.webbeans.service.ClassLoaderProxyService;
+import org.apache.webbeans.spi.DefiningClassService;
 import org.apache.webbeans.test.AbstractUnitTest;
 import org.apache.webbeans.test.contexts.serialize.AppScopedBean;
 import org.apache.webbeans.test.contexts.serialize.SessScopedBean;
@@ -45,7 +49,9 @@ import org.apache.webbeans.util.WebBeans
 
 import org.junit.Assert;
 
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.TestName;
 
 
 import javax.enterprise.context.SessionScoped;
@@ -56,6 +62,7 @@ import javax.enterprise.inject.spi.BeanM
 import java.io.*;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Properties;
 import java.util.Set;
 
 
@@ -64,6 +71,8 @@ import java.util.Set;
  */
 public class SerializationTest extends AbstractUnitTest
 {
+    @Rule
+    public final TestName testName = new TestName();
 
     @SuppressWarnings("unchecked")
     @Test
@@ -156,6 +165,20 @@ public class SerializationTest extends A
     @Test
     public void testProxySerialization() throws Exception
     {
+        doProxySerialization();
+    }
+
+    @Test
+    public void testProxySerializationWithClassLoaderProxy() throws Exception
+    {
+        addService(DefiningClassService.class, ClassLoaderProxyService.class);
+        doProxySerialization();
+        assertTrue(ClassLoaderProxyService.class.isInstance(
+                getWebBeansContext().getService(DefiningClassService.class)));
+    }
+
+    private void doProxySerialization() throws IOException, ClassNotFoundException
+    {
         Collection<Class<?>> classes = new ArrayList<Class<?>>();
 
         // add a few random classes
@@ -167,28 +190,28 @@ public class SerializationTest extends A
         Set<Bean<?>> beans = getBeanManager().getBeans(SessScopedBean.class);
         Assert.assertNotNull(beans);
         Assert.assertTrue(beans.size() == 1);
-        
+
         @SuppressWarnings("unchecked")
         Bean<SessScopedBean> bean = (Bean<SessScopedBean>) beans.iterator().next();
         CreationalContext<SessScopedBean> ssbCreational = getBeanManager().createCreationalContext(bean);
         Assert.assertNotNull(ssbCreational);
-        
+
         SessScopedBean reference = (SessScopedBean) getBeanManager().getReference(bean, SessScopedBean.class,
ssbCreational);
         Assert.assertNotNull(reference);
         Assert.assertTrue(reference instanceof OwbNormalScopeProxy);
-        
+
         reference.getApp().setI(4711);
-        
+
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         ObjectOutputStream oos = new ObjectOutputStream(baos);
         oos.writeObject(reference);
         byte[] ba = baos.toByteArray();
-        
+
         ByteArrayInputStream bais = new ByteArrayInputStream(ba);
         ObjectInputStream ois = new ObjectInputStream(bais);
         SessScopedBean ssb2 =  (SessScopedBean) ois.readObject();
         Assert.assertNotNull(ssb2);
-        
+
         Assert.assertNotNull(ssb2.getApp());
         Assert.assertTrue(ssb2.getApp().getI() == 4711);
     }



Mime
View raw message