You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2023/03/15 16:24:46 UTC

[camel] branch main updated: CAMEL-18957: prevent a few NPEs if the context is not created

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

orpiske 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 bbd7c363cfc CAMEL-18957: prevent a few NPEs if the context is not created
bbd7c363cfc is described below

commit bbd7c363cfc47fa82e2afc28d95b9bd0033022ba
Author: Otavio Rodolfo Piske <an...@gmail.com>
AuthorDate: Wed Mar 15 09:11:46 2023 -0300

    CAMEL-18957: prevent a few NPEs if the context is not created
---
 .../test/infra/core/DefaultCamelContextExtension.java     |  4 ++++
 .../test/infra/core/DefaultContextLifeCycleManager.java   | 15 +++++++++++++--
 .../test/infra/core/TransientCamelContextExtension.java   |  3 +++
 3 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/test-infra/camel-test-infra-core/src/test/java/org/apache/camel/test/infra/core/DefaultCamelContextExtension.java b/test-infra/camel-test-infra-core/src/test/java/org/apache/camel/test/infra/core/DefaultCamelContextExtension.java
index bd746ea12f2..be965607ef5 100644
--- a/test-infra/camel-test-infra-core/src/test/java/org/apache/camel/test/infra/core/DefaultCamelContextExtension.java
+++ b/test-infra/camel-test-infra-core/src/test/java/org/apache/camel/test/infra/core/DefaultCamelContextExtension.java
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.test.infra.core;
 
+import java.util.Objects;
+
 import org.apache.camel.BindToRegistry;
 import org.apache.camel.CamelContext;
 import org.apache.camel.ConsumerTemplate;
@@ -26,6 +28,7 @@ import org.apache.camel.ProducerTemplate;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.test.infra.core.annotations.ContextFixture;
 import org.apache.camel.test.infra.core.annotations.RouteFixture;
+import org.apache.camel.util.ObjectHelper;
 import org.junit.jupiter.api.extension.ExtensionContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -69,6 +72,7 @@ public class DefaultCamelContextExtension extends AbstractCamelContextExtension
     @Override
     public void beforeAll(ExtensionContext extensionContext) throws Exception {
         context = createCamelContext(fixtureProcessor, extensionContext);
+        Objects.requireNonNull(context, "Cannot run the test because the context is null");
 
         producerTemplate = context.createProducerTemplate();
         producerTemplate.start();
diff --git a/test-infra/camel-test-infra-core/src/test/java/org/apache/camel/test/infra/core/DefaultContextLifeCycleManager.java b/test-infra/camel-test-infra-core/src/test/java/org/apache/camel/test/infra/core/DefaultContextLifeCycleManager.java
index 6cadadd2f73..5316a30479e 100644
--- a/test-infra/camel-test-infra-core/src/test/java/org/apache/camel/test/infra/core/DefaultContextLifeCycleManager.java
+++ b/test-infra/camel-test-infra-core/src/test/java/org/apache/camel/test/infra/core/DefaultContextLifeCycleManager.java
@@ -19,12 +19,15 @@ package org.apache.camel.test.infra.core;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.component.mock.MockEndpoint;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * A default lifecycle manager suitable for most of the tests within Camel and end-user applications
  */
 public class DefaultContextLifeCycleManager implements ContextLifeCycleManager {
     public static final int DEFAULT_SHUTDOWN_TIMEOUT = 10;
+    private static final Logger LOG = LoggerFactory.getLogger(DefaultContextLifeCycleManager.class);
     private int shutdownTimeout = DEFAULT_SHUTDOWN_TIMEOUT;
     private boolean reset = true;
 
@@ -47,12 +50,20 @@ public class DefaultContextLifeCycleManager implements ContextLifeCycleManager {
 
     @Override
     public void afterAll(CamelContext context) {
-        context.shutdown();
+        if (context != null) {
+            context.shutdown();
+        } else {
+            LOG.error("Cannot run the JUnit's afterAll because the context is null: a problem may have prevented the context from starting");
+        }
     }
 
     @Override
     public void beforeAll(CamelContext context) {
-        context.getShutdownStrategy().setTimeout(shutdownTimeout);
+        if (context != null) {
+            context.getShutdownStrategy().setTimeout(shutdownTimeout);
+        } else {
+            LOG.error("Cannot run the JUnit's beforeAll because the context is null: a problem may have prevented the context from starting");
+        }
     }
 
     @Override
diff --git a/test-infra/camel-test-infra-core/src/test/java/org/apache/camel/test/infra/core/TransientCamelContextExtension.java b/test-infra/camel-test-infra-core/src/test/java/org/apache/camel/test/infra/core/TransientCamelContextExtension.java
index e3a6762fcd4..275dd50c4d2 100644
--- a/test-infra/camel-test-infra-core/src/test/java/org/apache/camel/test/infra/core/TransientCamelContextExtension.java
+++ b/test-infra/camel-test-infra-core/src/test/java/org/apache/camel/test/infra/core/TransientCamelContextExtension.java
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.test.infra.core;
 
+import java.util.Objects;
+
 import org.apache.camel.BindToRegistry;
 import org.apache.camel.CamelContext;
 import org.apache.camel.ConsumerTemplate;
@@ -83,6 +85,7 @@ public class TransientCamelContextExtension extends AbstractCamelContextExtensio
 
     private void recreateContext(ExtensionContext extensionContext) {
         context = createCamelContext(fixtureProcessor, extensionContext);
+        Objects.requireNonNull(context, "Cannot run the test because the context is null");
 
         producerTemplate = context.createProducerTemplate();
         producerTemplate.start();