openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1840054 - in /openwebbeans/meecrowave/trunk/meecrowave-junit: ./ src/main/java/org/apache/meecrowave/junit5/ src/main/java/org/apache/meecrowave/testing/ src/test/java/org/apache/meecrowave/junit5/
Date Tue, 04 Sep 2018 18:06:17 GMT
Author: rmannibucau
Date: Tue Sep  4 18:06:17 2018
New Revision: 1840054

URL: http://svn.apache.org/viewvc?rev=1840054&view=rev
Log:
MEECROWAVE-140 ensure we work well with PER_CLASS lifecycle of junit 5

Added:
    openwebbeans/meecrowave/trunk/meecrowave-junit/src/test/java/org/apache/meecrowave/junit5/PerClassMeecrowaveConfigTest.java
      - copied, changed from r1839223, openwebbeans/meecrowave/trunk/meecrowave-junit/src/test/java/org/apache/meecrowave/junit5/MeecrowaveConfigTest.java
Modified:
    openwebbeans/meecrowave/trunk/meecrowave-junit/pom.xml
    openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit5/MeecrowaveConfig.java
    openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit5/MeecrowaveExtension.java
    openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit5/MonoMeecrowaveExtension.java
    openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/testing/MonoBase.java
    openwebbeans/meecrowave/trunk/meecrowave-junit/src/test/java/org/apache/meecrowave/junit5/MeecrowaveConfigTest.java

Modified: openwebbeans/meecrowave/trunk/meecrowave-junit/pom.xml
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-junit/pom.xml?rev=1840054&r1=1840053&r2=1840054&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-junit/pom.xml (original)
+++ openwebbeans/meecrowave/trunk/meecrowave-junit/pom.xml Tue Sep  4 18:06:17 2018
@@ -63,7 +63,7 @@
   </dependencies>
 
   <properties>
-    <junit5.version>5.3.0-M1</junit5.version>
+    <junit5.version>5.3.0</junit5.version>
     <meecrowave.build.name>${project.groupId}.junit</meecrowave.build.name>
   </properties>
 

Modified: openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit5/MeecrowaveConfig.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit5/MeecrowaveConfig.java?rev=1840054&r1=1840053&r2=1840054&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit5/MeecrowaveConfig.java
(original)
+++ openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit5/MeecrowaveConfig.java
Tue Sep  4 18:06:17 2018
@@ -84,6 +84,10 @@ public @interface MeecrowaveConfig {
     boolean tomcatWrapLoader() default false;
     String sharedLibraries() default "";
     boolean useLog4j2JulLogManager() default false;
+    String scanningPackageIncludes() default "";
+    String scanningPackageExcludes() default "";
+    String scanningIncludes() default "";
+    String scanningExcludes() default "";
 
     Class<? extends Annotation>[] scopes() default {};
 }

Modified: openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit5/MeecrowaveExtension.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit5/MeecrowaveExtension.java?rev=1840054&r1=1840053&r2=1840054&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit5/MeecrowaveExtension.java
(original)
+++ openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit5/MeecrowaveExtension.java
Tue Sep  4 18:06:17 2018
@@ -36,6 +36,7 @@ import org.apache.meecrowave.Meecrowave;
 import org.apache.meecrowave.testing.Injector;
 import org.apache.webbeans.config.WebBeansContext;
 import org.apache.webbeans.spi.ContextsService;
+import org.junit.jupiter.api.TestInstance;
 import org.junit.jupiter.api.extension.AfterAllCallback;
 import org.junit.jupiter.api.extension.AfterEachCallback;
 import org.junit.jupiter.api.extension.BeforeAllCallback;
@@ -46,7 +47,7 @@ public class MeecrowaveExtension impleme
     private static final ExtensionContext.Namespace NAMESPACE = ExtensionContext.Namespace.create(MeecrowaveExtension.class.getName());
 
     @Override
-    public void beforeAll(final ExtensionContext context) throws Exception {
+    public void beforeAll(final ExtensionContext context) {
         final Meecrowave.Builder builder = new Meecrowave.Builder();
         final Optional<MeecrowaveConfig> meecrowaveConfig = context.getElement().map(e
-> e.getAnnotation(MeecrowaveConfig.class));
         final String ctx;
@@ -90,25 +91,34 @@ public class MeecrowaveExtension impleme
         context.getStore(NAMESPACE).put(Meecrowave.class.getName(), meecrowave);
         context.getStore(NAMESPACE).put(Meecrowave.Builder.class.getName(), builder);
         meecrowave.bake(ctx);
+
+        if (isPerClass(context)) {
+            doInject(context);
+        }
     }
 
     @Override
-    public void afterAll(final ExtensionContext context) throws Exception {
-        Meecrowave.class.cast(context.getStore(NAMESPACE).get(Meecrowave.class.getName())).close();
+    public void afterAll(final ExtensionContext context) {
+        if (isPerClass(context)) {
+            Meecrowave.class.cast(context.getStore(NAMESPACE).get(Meecrowave.class.getName())).close();
+        }
     }
 
     @Override
-    public void beforeEach(final ExtensionContext context) throws Exception {
-        getScopes(context).ifPresent(scopes -> {
-           final ContextsService contextsService = WebBeansContext.currentInstance().getContextsService();
-           Stream.of(scopes).forEach(s -> contextsService.startContext(s, null));
-        });
-        context.getStore(NAMESPACE).put(CreationalContext.class.getName(), Injector.inject(context.getTestInstance().orElse(null)));
-        Injector.injectConfig(Meecrowave.Builder.class.cast(context.getStore(NAMESPACE).get(Meecrowave.Builder.class.getName())),
context.getTestInstance().orElse(null));
+    public void beforeEach(final ExtensionContext context) {
+        if (!isPerClass(context)) {
+            doInject(context);
+        }
     }
 
     @Override
-    public void afterEach(final ExtensionContext context) throws Exception {
+    public void afterEach(final ExtensionContext context) {
+        if (!isPerClass(context)) {
+            doRelease(context);
+        }
+    }
+
+    private void doRelease(final ExtensionContext context) {
         CreationalContext.class.cast(context.getStore(NAMESPACE).get(CreationalContext.class.getName())).release();
         getScopes(context).ifPresent(scopes -> {
             final ContextsService contextsService = WebBeansContext.currentInstance().getContextsService();
@@ -118,6 +128,21 @@ public class MeecrowaveExtension impleme
         });
     }
 
+    private Boolean isPerClass(final ExtensionContext context) {
+        return context.getTestInstanceLifecycle()
+                .map(it -> it.equals(TestInstance.Lifecycle.PER_CLASS))
+                .orElse(false);
+    }
+
+    private void doInject(final ExtensionContext context) {
+        getScopes(context).ifPresent(scopes -> {
+            final ContextsService contextsService = WebBeansContext.currentInstance().getContextsService();
+            Stream.of(scopes).forEach(s -> contextsService.startContext(s, null));
+        });
+        context.getStore(NAMESPACE).put(CreationalContext.class.getName(), Injector.inject(context.getTestInstance().orElse(null)));
+        Injector.injectConfig(Meecrowave.Builder.class.cast(context.getStore(NAMESPACE).get(Meecrowave.Builder.class.getName())),
context.getTestInstance().orElse(null));
+    }
+
     private Optional<Class<? extends Annotation>[]> getScopes(final ExtensionContext
context) {
         return context.getElement()
                       .map(e -> e.getAnnotation(MeecrowaveConfig.class))

Modified: openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit5/MonoMeecrowaveExtension.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit5/MonoMeecrowaveExtension.java?rev=1840054&r1=1840053&r2=1840054&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit5/MonoMeecrowaveExtension.java
(original)
+++ openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit5/MonoMeecrowaveExtension.java
Tue Sep  4 18:06:17 2018
@@ -22,30 +22,60 @@ import javax.enterprise.context.spi.Crea
 
 import org.apache.meecrowave.testing.Injector;
 import org.apache.meecrowave.testing.MonoBase;
+import org.junit.jupiter.api.TestInstance;
+import org.junit.jupiter.api.extension.AfterAllCallback;
 import org.junit.jupiter.api.extension.AfterEachCallback;
 import org.junit.jupiter.api.extension.BeforeAllCallback;
 import org.junit.jupiter.api.extension.BeforeEachCallback;
 import org.junit.jupiter.api.extension.ExtensionContext;
 
-public class MonoMeecrowaveExtension implements BeforeAllCallback, BeforeEachCallback, AfterEachCallback
{
+public class MonoMeecrowaveExtension implements BeforeAllCallback, BeforeEachCallback, AfterEachCallback,
AfterAllCallback {
     private static final MonoBase BASE = new MonoBase();
     private static final ExtensionContext.Namespace NAMESPACE = ExtensionContext.Namespace.create(MonoMeecrowaveExtension.class.getName());
 
     @Override
     public void beforeAll(final ExtensionContext context) {
         context.getStore(NAMESPACE).put(MonoBase.Instance.class.getName(), BASE.startIfNeeded());
+        if (isPerClass(context)) {
+            doInject(context);
+        }
     }
 
     @Override
     public void beforeEach(final ExtensionContext context) {
+        if (!isPerClass(context)) {
+            doInject(context);
+        }
+    }
+
+    @Override
+    public void afterEach(final ExtensionContext context) {
+        if (!isPerClass(context)) {
+            doRelease(context);
+        }
+    }
+
+    @Override
+    public void afterAll(final ExtensionContext context) {
+        if (isPerClass(context)) {
+            doRelease(context);
+        }
+    }
+
+    private void doRelease(ExtensionContext context) {
+        CreationalContext.class.cast(context.getStore(NAMESPACE).get(CreationalContext.class.getName())).release();
+    }
+
+    private void doInject(final ExtensionContext context) {
         context.getStore(NAMESPACE).put(CreationalContext.class.getName(), Injector.inject(context.getTestInstance().orElse(null)));
         Injector.injectConfig(
                 MonoBase.Instance.class.cast(context.getStore(NAMESPACE).get(MonoBase.Instance.class.getName())).getConfiguration(),
                 context.getTestInstance().orElse(null));
     }
 
-    @Override
-    public void afterEach(final ExtensionContext context) {
-        CreationalContext.class.cast(context.getStore(NAMESPACE).get(CreationalContext.class.getName())).release();
+    private Boolean isPerClass(final ExtensionContext context) {
+        return context.getTestInstanceLifecycle()
+                .map(it -> it.equals(TestInstance.Lifecycle.PER_CLASS))
+                .orElse(false);
     }
 }

Modified: openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/testing/MonoBase.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/testing/MonoBase.java?rev=1840054&r1=1840053&r2=1840054&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/testing/MonoBase.java
(original)
+++ openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/testing/MonoBase.java
Tue Sep  4 18:06:17 2018
@@ -42,10 +42,9 @@ public class MonoBase {
             final Meecrowave meecrowave = new Meecrowave(configuration);
             if (CONTAINER.compareAndSet(null, new Instance(meecrowave, configuration, containerLoader)))
{
                 final Configuration runnerConfig = StreamSupport.stream(ServiceLoader.load(Configuration.class)
-                                                                                     .spliterator(),
false)
-                                                                .sorted(Comparator.comparingInt(Configuration::order))
-                                                                .findFirst()
-                                                                .orElseGet(() -> new Configuration()
{});
+                        .spliterator(), false)
+                        .min(Comparator.comparingInt(Configuration::order))
+                        .orElseGet(() -> new Configuration() {});
 
                 runnerConfig.beforeStarts();
 

Modified: openwebbeans/meecrowave/trunk/meecrowave-junit/src/test/java/org/apache/meecrowave/junit5/MeecrowaveConfigTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-junit/src/test/java/org/apache/meecrowave/junit5/MeecrowaveConfigTest.java?rev=1840054&r1=1840053&r2=1840054&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-junit/src/test/java/org/apache/meecrowave/junit5/MeecrowaveConfigTest.java
(original)
+++ openwebbeans/meecrowave/trunk/meecrowave-junit/src/test/java/org/apache/meecrowave/junit5/MeecrowaveConfigTest.java
Tue Sep  4 18:06:17 2018
@@ -32,12 +32,12 @@ import static org.junit.Assert.assertEqu
 import static org.junit.Assert.fail;
 
 @MeecrowaveConfig
-public class MeecrowaveConfigTest {
+class MeecrowaveConfigTest {
     @ConfigurationInject
     private Meecrowave.Builder config;
 
     @Test
-    public void run() throws MalformedURLException {
+    void run() throws MalformedURLException {
         assertEquals("simple", slurp(new URL("http://localhost:" + config.getHttpPort() +
"/api/test")));
     }
 

Copied: openwebbeans/meecrowave/trunk/meecrowave-junit/src/test/java/org/apache/meecrowave/junit5/PerClassMeecrowaveConfigTest.java
(from r1839223, openwebbeans/meecrowave/trunk/meecrowave-junit/src/test/java/org/apache/meecrowave/junit5/MeecrowaveConfigTest.java)
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-junit/src/test/java/org/apache/meecrowave/junit5/PerClassMeecrowaveConfigTest.java?p2=openwebbeans/meecrowave/trunk/meecrowave-junit/src/test/java/org/apache/meecrowave/junit5/PerClassMeecrowaveConfigTest.java&p1=openwebbeans/meecrowave/trunk/meecrowave-junit/src/test/java/org/apache/meecrowave/junit5/MeecrowaveConfigTest.java&r1=1839223&r2=1840054&rev=1840054&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-junit/src/test/java/org/apache/meecrowave/junit5/MeecrowaveConfigTest.java
(original)
+++ openwebbeans/meecrowave/trunk/meecrowave-junit/src/test/java/org/apache/meecrowave/junit5/PerClassMeecrowaveConfigTest.java
Tue Sep  4 18:06:17 2018
@@ -18,27 +18,60 @@
  */
 package org.apache.meecrowave.junit5;
 
-import org.apache.meecrowave.Meecrowave;
-import org.apache.meecrowave.io.IO;
-import org.apache.meecrowave.testing.ConfigurationInject;
-import org.junit.jupiter.api.Test;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.fail;
+import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS;
 
 import java.io.IOException;
 import java.io.InputStream;
-import java.net.MalformedURLException;
 import java.net.URL;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.fail;
+import javax.enterprise.context.ApplicationScoped;
+import javax.inject.Inject;
 
-@MeecrowaveConfig
-public class MeecrowaveConfigTest {
+import org.apache.meecrowave.Meecrowave;
+import org.apache.meecrowave.io.IO;
+import org.apache.meecrowave.testing.ConfigurationInject;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInstance;
+
+@TestInstance(PER_CLASS)
+@MeecrowaveConfig(scanningPackageIncludes = "org.apache.meecrowave.junit5.PerClassMeecrowaveConfigTest")
+class PerClassMeecrowaveConfigTest {
     @ConfigurationInject
     private Meecrowave.Builder config;
 
+    @Inject
+    private Bean bean;
+
+    private static Bean instance;
+
+    @Test
+    void m1() {
+        doTest();
+    }
+
     @Test
-    public void run() throws MalformedURLException {
-        assertEquals("simple", slurp(new URL("http://localhost:" + config.getHttpPort() +
"/api/test")));
+    void m2() {
+        doTest();
+    }
+
+    private void doTest() {
+        if (instance == null) {
+            first();
+        } else {
+            second();
+        }
+    }
+
+    private void first() {
+        assertEquals("ok", bean.get());
+        instance = bean;
+    }
+
+    private void second() {
+        assertSame(instance, bean);
     }
 
     private String slurp(final URL url) {
@@ -49,4 +82,11 @@ public class MeecrowaveConfigTest {
         }
         return null;
     }
+
+    @ApplicationScoped
+    public static class Bean {
+        String get() {
+            return "ok";
+        }
+    }
 }



Mime
View raw message