This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 5cc99a2  CAMEL-17633: Simplify the code by removing CamelCdiContext 
(#7055)
5cc99a2 is described below

commit 5cc99a26fa3f63e8625c94f11f14f789969b779c
Author: Nicolas Filotto <[email protected]>
AuthorDate: Mon Feb 28 11:34:54 2022 +0100

    CAMEL-17633: Simplify the code by removing CamelCdiContext (#7055)
---
 .../org/apache/camel/test/cdi/AdviceRoute.java     |  4 +-
 .../org/apache/camel/test/cdi/CamelCdiContext.java | 46 ----------------------
 .../apache/camel/test/cdi/CamelCdiDeployment.java  | 14 ++++---
 .../apache/camel/test/cdi/CamelCdiExtension.java   | 37 +++++++++--------
 .../camel/test/cdi/CamelCdiParameterizedTest.java  |  2 +-
 5 files changed, 30 insertions(+), 73 deletions(-)

diff --git 
a/components/camel-test/camel-test-cdi-junit5/src/main/java/org/apache/camel/test/cdi/AdviceRoute.java
 
b/components/camel-test/camel-test-cdi-junit5/src/main/java/org/apache/camel/test/cdi/AdviceRoute.java
index eaab2df..009abd2 100644
--- 
a/components/camel-test/camel-test-cdi-junit5/src/main/java/org/apache/camel/test/cdi/AdviceRoute.java
+++ 
b/components/camel-test/camel-test-cdi-junit5/src/main/java/org/apache/camel/test/cdi/AdviceRoute.java
@@ -29,7 +29,7 @@ import org.apache.camel.builder.AdviceWithRouteBuilder;
  * A special annotation meant for the class of type {@code 
AdviceWithRouteBuilder} in order to provide the id of the
  * route to <i>advice</i> but also to indicate that the start-up of the Camel 
context needs to be delayed to
  * <i>advice</i> properly the route.
- *
+ * <p/>
  * In the next example, the {@code AdviceWithRouteBuilder} of type {@code 
ReplaceDirectWithMockBuilder} advises the
  * route whose identifier is <i>main-route</i>.
  *
@@ -56,7 +56,7 @@ import org.apache.camel.builder.AdviceWithRouteBuilder;
 public @interface AdviceRoute {
 
     /**
-     * @return Gives the id of the route to <i>advice</i>.
+     * @return the id of the route to <i>advice</i>.
      */
     String value();
 }
diff --git 
a/components/camel-test/camel-test-cdi-junit5/src/main/java/org/apache/camel/test/cdi/CamelCdiContext.java
 
b/components/camel-test/camel-test-cdi-junit5/src/main/java/org/apache/camel/test/cdi/CamelCdiContext.java
deleted file mode 100644
index 2b7d386..0000000
--- 
a/components/camel-test/camel-test-cdi-junit5/src/main/java/org/apache/camel/test/cdi/CamelCdiContext.java
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * 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.camel.test.cdi;
-
-import java.util.concurrent.atomic.AtomicReference;
-
-import javax.enterprise.inject.spi.BeanManager;
-
-final class CamelCdiContext {
-
-    private final AtomicReference<BeanManager> manager = new 
AtomicReference<>();
-
-    BeanManager getBeanManager() {
-        BeanManager manager = this.manager.get();
-        if (manager == null) {
-            throw new IllegalStateException("Bean manager is not set!");
-        }
-        return manager;
-    }
-
-    void setBeanManager(BeanManager manager) {
-        if (!this.manager.compareAndSet(null, manager)) {
-            throw new IllegalStateException("Bean manager already set!");
-        }
-    }
-
-    void unsetBeanManager() {
-        if (manager.getAndSet(null) == null) {
-            throw new IllegalStateException("Bean manager is not set!");
-        }
-    }
-}
diff --git 
a/components/camel-test/camel-test-cdi-junit5/src/main/java/org/apache/camel/test/cdi/CamelCdiDeployment.java
 
b/components/camel-test/camel-test-cdi-junit5/src/main/java/org/apache/camel/test/cdi/CamelCdiDeployment.java
index 92df73d..cc1515b7 100644
--- 
a/components/camel-test/camel-test-cdi-junit5/src/main/java/org/apache/camel/test/cdi/CamelCdiDeployment.java
+++ 
b/components/camel-test/camel-test-cdi-junit5/src/main/java/org/apache/camel/test/cdi/CamelCdiDeployment.java
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.test.cdi;
 
+import javax.enterprise.inject.spi.BeanManager;
+
 import org.apache.camel.cdi.CdiCamelExtension;
 import org.jboss.weld.config.ConfigurationKey;
 import org.jboss.weld.environment.se.Weld;
@@ -24,13 +26,16 @@ import org.junit.jupiter.api.extension.ExtensionContext;
 
 final class CamelCdiDeployment implements 
ExtensionContext.Store.CloseableResource {
 
-    private final CamelCdiContext context;
+    private final BeanManager beanManager;
     private final WeldContainer container;
 
-    CamelCdiDeployment(Class<?> test, CamelCdiContext context) {
-        this.context = context;
+    CamelCdiDeployment(Class<?> test) {
         this.container = createWeldContainer(test);
-        context.setBeanManager(container.getBeanManager());
+        this.beanManager = container.getBeanManager();
+    }
+
+    BeanManager beanManager() {
+        return beanManager;
     }
 
     private static WeldContainer createWeldContainer(Class<?> test) {
@@ -66,6 +71,5 @@ final class CamelCdiDeployment implements 
ExtensionContext.Store.CloseableResour
     @Override
     public void close() {
         container.shutdown();
-        context.unsetBeanManager();
     }
 }
diff --git 
a/components/camel-test/camel-test-cdi-junit5/src/main/java/org/apache/camel/test/cdi/CamelCdiExtension.java
 
b/components/camel-test/camel-test-cdi-junit5/src/main/java/org/apache/camel/test/cdi/CamelCdiExtension.java
index 2277fa7..abb5431 100644
--- 
a/components/camel-test/camel-test-cdi-junit5/src/main/java/org/apache/camel/test/cdi/CamelCdiExtension.java
+++ 
b/components/camel-test/camel-test-cdi-junit5/src/main/java/org/apache/camel/test/cdi/CamelCdiExtension.java
@@ -19,13 +19,15 @@ package org.apache.camel.test.cdi;
 import javax.enterprise.inject.spi.Bean;
 import javax.enterprise.inject.spi.BeanManager;
 
-import org.junit.jupiter.api.extension.BeforeAllCallback;
+import org.junit.jupiter.api.extension.Extension;
 import org.junit.jupiter.api.extension.ExtensionContext;
 import org.junit.jupiter.api.extension.ParameterContext;
 import org.junit.jupiter.api.extension.ParameterResolver;
 import org.junit.jupiter.api.extension.TestInstanceFactory;
 import org.junit.jupiter.api.extension.TestInstanceFactoryContext;
 
+import static 
org.junit.jupiter.api.extension.ExtensionContext.Namespace.create;
+
 /**
  * The JUnit 5 extension of Camel CDI, this extension allows to inject any 
existing beans in the test class but also the
  * Camel specific beans directly as parameter of the test methods. The non 
Camel specific beans cannot be injected as
@@ -75,26 +77,22 @@ import 
org.junit.jupiter.api.extension.TestInstanceFactoryContext;
  * @see AdviceRoute
  * @see Beans
  */
-public final class CamelCdiExtension implements BeforeAllCallback, 
TestInstanceFactory, ParameterResolver {
+public final class CamelCdiExtension implements Extension, 
TestInstanceFactory, ParameterResolver {
 
     /**
-     * The name of key used to store the Camel CDI deployment.
+     * The namespace used to store the state of the test.
      */
-    private static final String CAMEL_CDI_DEPLOYMENT = "camel-cdi-deployment";
+    private static final ExtensionContext.Namespace NAMESPACE = 
create(CamelCdiExtension.class);
     /**
-     * The CDI context from which the {@code BeanManager} is retrieved.
+     * The name of key used to store the Camel CDI deployment.
      */
-    private final CamelCdiContext camelCdiContext = new CamelCdiContext();
-
-    @Override
-    public void beforeAll(ExtensionContext context) {
-        getCamelCdiDeployment(context).put(CAMEL_CDI_DEPLOYMENT,
-                new CamelCdiDeployment(context.getRequiredTestClass(), 
camelCdiContext));
-    }
+    private static final String CAMEL_CDI_DEPLOYMENT = "camel-cdi-deployment";
 
     @Override
-    public Object createTestInstance(TestInstanceFactoryContext 
factoryContext, ExtensionContext extensionContext) {
-        final BeanManager manager = camelCdiContext.getBeanManager();
+    public Object createTestInstance(TestInstanceFactoryContext 
factoryContext, ExtensionContext context) {
+        final BeanManager manager = 
getCamelCdiDeployment(context).getOrComputeIfAbsent(
+                CAMEL_CDI_DEPLOYMENT, k -> new 
CamelCdiDeployment(context.getRequiredTestClass()), CamelCdiDeployment.class)
+                .beanManager();
         final Bean<?> bean = manager.getBeans(factoryContext.getTestClass(), 
AnyLiteral.INSTANCE).iterator().next();
         // TODO: manage lifecycle of @Dependent beans
         return manager.getReference(bean, bean.getBeanClass(), 
manager.createCreationalContext(bean));
@@ -108,19 +106,20 @@ public final class CamelCdiExtension implements 
BeforeAllCallback, TestInstanceF
     }
 
     @Override
-    public Object resolveParameter(ParameterContext parameterContext, 
ExtensionContext extensionContext) {
-        final BeanManager manager = camelCdiContext.getBeanManager();
+    public Object resolveParameter(ParameterContext parameterContext, 
ExtensionContext context) {
+        final BeanManager manager = 
getCamelCdiDeployment(context).get(CAMEL_CDI_DEPLOYMENT, 
CamelCdiDeployment.class)
+                .beanManager();
         // TODO: use a proper CreationalContext...
         return manager.getInjectableReference(
                 new FrameworkMethodInjectionPoint(
-                        extensionContext.getRequiredTestMethod(), 
parameterContext.getIndex(), manager),
+                        context.getRequiredTestMethod(), 
parameterContext.getIndex(), manager),
                 manager.createCreationalContext(null));
     }
 
     /**
-     * @return the store in which the Camel CDI deployment of the current test 
class the is stored
+     * @return the store in which the Camel CDI deployment of the current test 
class is stored
      */
     private ExtensionContext.Store getCamelCdiDeployment(ExtensionContext 
context) {
-        return context.getStore(ExtensionContext.Namespace.create(getClass(), 
context.getRequiredTestClass()));
+        return context.getStore(NAMESPACE);
     }
 }
diff --git 
a/components/camel-test/camel-test-cdi-junit5/src/test/java/org/apache/camel/test/cdi/CamelCdiParameterizedTest.java
 
b/components/camel-test/camel-test-cdi-junit5/src/test/java/org/apache/camel/test/cdi/CamelCdiParameterizedTest.java
index 3d33814..24d571f 100644
--- 
a/components/camel-test/camel-test-cdi-junit5/src/test/java/org/apache/camel/test/cdi/CamelCdiParameterizedTest.java
+++ 
b/components/camel-test/camel-test-cdi-junit5/src/test/java/org/apache/camel/test/cdi/CamelCdiParameterizedTest.java
@@ -41,7 +41,7 @@ class CamelCdiParameterizedTest {
     ProducerTemplate producer;
 
     @ParameterizedTest
-    @ValueSource(strings = { "hello", "parametrized", "test" })
+    @ValueSource(strings = { "hello", "parameterized", "test" })
     void test(String value) throws Exception {
         mock.expectedMessageCount(1);
         mock.expectedBodiesReceived(value);

Reply via email to