You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by rm...@apache.org on 2018/09/20 12:50:26 UTC

svn commit: r1841466 - in /openwebbeans/meecrowave/trunk: meecrowave-core/src/main/java/org/apache/meecrowave/ meecrowave-junit/src/main/java/org/apache/meecrowave/junit5/ meecrowave-junit/src/test/java/org/apache/meecrowave/junit5/

Author: rmannibucau
Date: Thu Sep 20 12:50:26 2018
New Revision: 1841466

URL: http://svn.apache.org/viewvc?rev=1841466&view=rev
Log:
MEECROWAVE-143 [junit5] RequestScoped not activated with @MeecrowaveConfig

Added:
    openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit5/Scopes.java
    openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit5/ScopesExtension.java
    openwebbeans/meecrowave/trunk/meecrowave-junit/src/test/java/org/apache/meecrowave/junit5/MeecrowaveConfigScopesTest.java
    openwebbeans/meecrowave/trunk/meecrowave-junit/src/test/java/org/apache/meecrowave/junit5/ScopesTest.java
Modified:
    openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/Meecrowave.java
    openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit5/MeecrowaveExtension.java

Modified: openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/Meecrowave.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/Meecrowave.java?rev=1841466&r1=1841465&r2=1841466&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/Meecrowave.java (original)
+++ openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/Meecrowave.java Thu Sep 20 12:50:26 2018
@@ -1919,7 +1919,6 @@ public class Meecrowave implements AutoC
         public String getConf() {
             return conf;
         }
-
         public void setConf(final String conf) {
             this.conf = conf;
         }

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=1841466&r1=1841465&r2=1841466&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 Thu Sep 20 12:50:26 2018
@@ -18,25 +18,18 @@
  */
 package org.apache.meecrowave.junit5;
 
-import static java.util.Arrays.asList;
 import static java.util.Optional.ofNullable;
 
 import java.io.File;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
 import java.util.Optional;
-import java.util.stream.Stream;
 
 import javax.enterprise.context.spi.CreationalContext;
 
 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.extension.AfterAllCallback;
 import org.junit.jupiter.api.extension.AfterEachCallback;
 import org.junit.jupiter.api.extension.BeforeAllCallback;
@@ -47,6 +40,20 @@ public class MeecrowaveExtension extends
         implements BeforeAllCallback, AfterAllCallback, BeforeEachCallback, AfterEachCallback {
     private static final ExtensionContext.Namespace NAMESPACE = ExtensionContext.Namespace.create(MeecrowaveExtension.class.getName());
 
+    private final ScopesExtension scopes = new ScopesExtension() {
+        @Override
+        protected Optional<Class<? extends Annotation>[]> getScopes(final ExtensionContext context) {
+            return context.getElement()
+                          .map(e -> ofNullable(e.getAnnotation(MeecrowaveConfig.class))
+                                  .orElseGet(() -> context.getParent()
+                                                          .flatMap(ExtensionContext::getElement)
+                                                          .map(it -> it.getAnnotation(MeecrowaveConfig.class))
+                                                          .orElse(null)))
+                          .map(MeecrowaveConfig::scopes)
+                          .filter(s -> s.length > 0);
+        }
+    };
+
     @Override
     public void beforeAll(final ExtensionContext context) {
         final Meecrowave.Builder builder = new Meecrowave.Builder();
@@ -129,30 +136,15 @@ public class MeecrowaveExtension extends
     private void doRelease(final ExtensionContext context) {
         ofNullable(context.getStore(NAMESPACE).get(CreationalContext.class, CreationalContext.class))
                 .ifPresent(CreationalContext::release);
-        getScopes(context).ifPresent(scopes -> {
-            final ContextsService contextsService = WebBeansContext.currentInstance().getContextsService();
-            final List<Class<? extends Annotation>> list = new ArrayList<>(asList(scopes));
-            Collections.reverse(list);
-            list.forEach(s -> contextsService.endContext(s, null));
-        });
+        scopes.beforeEach(context);
     }
 
     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));
-        });
+        scopes.beforeEach(context);
         final ExtensionContext.Store store = context.getStore(NAMESPACE);
         store.put(CreationalContext.class, Injector.inject(context.getTestInstance().orElse(null)));
         Injector.injectConfig(
                 store.get(Meecrowave.Builder.class, Meecrowave.Builder.class),
                 context.getTestInstance().orElse(null));
     }
-
-    private Optional<Class<? extends Annotation>[]> getScopes(final ExtensionContext context) {
-        return context.getElement()
-                      .map(e -> e.getAnnotation(MeecrowaveConfig.class))
-                      .map(MeecrowaveConfig::scopes)
-                      .filter(s -> s.length > 0);
-    }
 }

Added: openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit5/Scopes.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit5/Scopes.java?rev=1841466&view=auto
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit5/Scopes.java (added)
+++ openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit5/Scopes.java Thu Sep 20 12:50:26 2018
@@ -0,0 +1,36 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.meecrowave.junit5;
+
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Annotation;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import org.junit.jupiter.api.extension.ExtendWith;
+
+@Target({TYPE, METHOD})
+@Retention(RUNTIME)
+@ExtendWith(ScopesExtension.class)
+public @interface Scopes {
+    Class<? extends Annotation>[] scopes() default {};
+}

Added: openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit5/ScopesExtension.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit5/ScopesExtension.java?rev=1841466&view=auto
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit5/ScopesExtension.java (added)
+++ openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit5/ScopesExtension.java Thu Sep 20 12:50:26 2018
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.meecrowave.junit5;
+
+import static java.util.Arrays.asList;
+import static java.util.Optional.ofNullable;
+
+import java.lang.annotation.Annotation;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Optional;
+import java.util.stream.Stream;
+
+import org.apache.webbeans.config.WebBeansContext;
+import org.apache.webbeans.spi.ContextsService;
+import org.junit.jupiter.api.extension.AfterEachCallback;
+import org.junit.jupiter.api.extension.BeforeEachCallback;
+import org.junit.jupiter.api.extension.ExtensionContext;
+
+public class ScopesExtension implements BeforeEachCallback, AfterEachCallback {
+
+    private static final ExtensionContext.Namespace NAMESPACE = ExtensionContext.Namespace
+            .create(ScopesExtension.class.getName());
+
+    @Override
+    public void beforeEach(final ExtensionContext extensionContext) {
+        getScopes(extensionContext).ifPresent(scopes -> {
+            final ContextsService contextsService = WebBeansContext.currentInstance().getContextsService();
+            Stream.of(scopes).forEach(s -> contextsService.startContext(s, null));
+            extensionContext.getStore(NAMESPACE).put(Holder.class, new Holder(scopes));
+        });
+    }
+
+    @Override
+    public void afterEach(final ExtensionContext extensionContext) {
+        ofNullable(extensionContext.getStore(NAMESPACE).get(Holder.class, Holder.class)).map(h -> h.scopes).ifPresent(scopes -> {
+            final ContextsService contextsService = WebBeansContext.currentInstance().getContextsService();
+            final List<Class<? extends Annotation>> list = new ArrayList<>(asList(scopes));
+            Collections.reverse(list);
+            list.forEach(s -> contextsService.endContext(s, null));
+        });
+    }
+
+    protected Optional<Class<? extends Annotation>[]> getScopes(final ExtensionContext context) {
+        return context.getElement()
+                .map(e -> ofNullable(e.getAnnotation(Scopes.class)).orElseGet(() -> context.getParent()
+                        .flatMap(ExtensionContext::getElement).map(it -> it.getAnnotation(Scopes.class)).orElse(null)))
+                .map(Scopes::scopes).filter(s -> s.length > 0);
+    }
+
+    private static class Holder {
+
+        private final Class<? extends Annotation>[] scopes;
+
+        private Holder(final Class<? extends Annotation>[] scopes) {
+            this.scopes = scopes;
+        }
+    }
+}

Added: openwebbeans/meecrowave/trunk/meecrowave-junit/src/test/java/org/apache/meecrowave/junit5/MeecrowaveConfigScopesTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-junit/src/test/java/org/apache/meecrowave/junit5/MeecrowaveConfigScopesTest.java?rev=1841466&view=auto
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-junit/src/test/java/org/apache/meecrowave/junit5/MeecrowaveConfigScopesTest.java (added)
+++ openwebbeans/meecrowave/trunk/meecrowave-junit/src/test/java/org/apache/meecrowave/junit5/MeecrowaveConfigScopesTest.java Thu Sep 20 12:50:26 2018
@@ -0,0 +1,36 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.meecrowave.junit5;
+
+import static org.junit.Assert.assertTrue;
+
+import java.net.MalformedURLException;
+
+import javax.enterprise.context.RequestScoped;
+
+import org.apache.webbeans.config.WebBeansContext;
+import org.junit.jupiter.api.Test;
+
+@MeecrowaveConfig(scopes = RequestScoped.class)
+class MeecrowaveConfigScopesTest {
+    @Test
+    void run() {
+        assertTrue(WebBeansContext.currentInstance().getContextsService().getCurrentContext(RequestScoped.class, false).isActive());
+    }
+}

Added: openwebbeans/meecrowave/trunk/meecrowave-junit/src/test/java/org/apache/meecrowave/junit5/ScopesTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-junit/src/test/java/org/apache/meecrowave/junit5/ScopesTest.java?rev=1841466&view=auto
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-junit/src/test/java/org/apache/meecrowave/junit5/ScopesTest.java (added)
+++ openwebbeans/meecrowave/trunk/meecrowave-junit/src/test/java/org/apache/meecrowave/junit5/ScopesTest.java Thu Sep 20 12:50:26 2018
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.meecrowave.junit5;
+
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import javax.enterprise.context.RequestScoped;
+import javax.enterprise.context.spi.Context;
+
+import org.apache.webbeans.config.WebBeansContext;
+import org.junit.jupiter.api.Test;
+
+@MeecrowaveConfig
+@Scopes(scopes = RequestScoped.class)
+class ScopesTest {
+    @Test
+    @Scopes
+    void overrided() {
+        assertNull(getContext());
+    }
+
+    @Test
+    void clazz() {
+        assertTrue(getContext().isActive());
+    }
+
+    private Context getContext() {
+        return WebBeansContext.currentInstance()
+                              .getContextsService()
+                              .getCurrentContext(RequestScoped.class, false);
+    }
+}