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 2022/08/16 15:23:05 UTC

[camel] 01/02: (chores) camel-test-infra-activemq: cleanups

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

commit 49af0f8ddce5a69ff73320753946c81a6463c91f
Author: Otavio Rodolfo Piske <an...@gmail.com>
AuthorDate: Tue Aug 16 14:20:58 2022 +0200

    (chores) camel-test-infra-activemq: cleanups
    
    - Remove duplicated code for the embedded activeMQ
    - Remove commented code
    - Cleanup the initialization of embedded broker services
---
 ...e.java => AbstractActiveMQEmbeddedService.java} |  41 +++---
 .../activemq/services/ActiveMQEmbeddedService.java | 154 ++-------------------
 .../services/ActiveMQEmbeddedServiceBuilder.java   |  14 --
 .../infra/activemq/services/ActiveMQVMService.java | 143 ++-----------------
 4 files changed, 44 insertions(+), 308 deletions(-)

diff --git a/test-infra/camel-test-infra-activemq/src/test/java/org/apache/camel/test/infra/activemq/services/ActiveMQVMService.java b/test-infra/camel-test-infra-activemq/src/test/java/org/apache/camel/test/infra/activemq/services/AbstractActiveMQEmbeddedService.java
similarity index 83%
copy from test-infra/camel-test-infra-activemq/src/test/java/org/apache/camel/test/infra/activemq/services/ActiveMQVMService.java
copy to test-infra/camel-test-infra-activemq/src/test/java/org/apache/camel/test/infra/activemq/services/AbstractActiveMQEmbeddedService.java
index 037d6e5b7de..c7a9979bc59 100644
--- a/test-infra/camel-test-infra-activemq/src/test/java/org/apache/camel/test/infra/activemq/services/ActiveMQVMService.java
+++ b/test-infra/camel-test-infra-activemq/src/test/java/org/apache/camel/test/infra/activemq/services/AbstractActiveMQEmbeddedService.java
@@ -35,21 +35,21 @@ import org.slf4j.LoggerFactory;
 
 import static org.junit.jupiter.api.Assertions.fail;
 
-public class ActiveMQVMService implements ActiveMQService, ConnectionFactoryAware, BeforeEachCallback, AfterEachCallback {
-    private static final Logger LOG = LoggerFactory.getLogger(ActiveMQVMService.class);
+public abstract class AbstractActiveMQEmbeddedService
+        implements ActiveMQService, ConnectionFactoryAware, BeforeEachCallback, AfterEachCallback {
+    private static final Logger LOG = LoggerFactory.getLogger(AbstractActiveMQEmbeddedService.class);
     private final BrokerService brokerService;
     private final boolean recycle;
 
-    public ActiveMQVMService() {
+    public AbstractActiveMQEmbeddedService() {
         this(ActiveMQEmbeddedServiceBuilder.defaultBroker().brokerService());
     }
 
-    public ActiveMQVMService(BrokerService brokerService) {
-        this.brokerService = brokerService;
-        this.recycle = false;
+    public AbstractActiveMQEmbeddedService(BrokerService brokerService) {
+        this(brokerService, false);
     }
 
-    public ActiveMQVMService(BrokerService brokerService, boolean recycle) {
+    public AbstractActiveMQEmbeddedService(BrokerService brokerService, boolean recycle) {
         this.brokerService = brokerService;
         this.recycle = recycle;
     }
@@ -93,11 +93,6 @@ public class ActiveMQVMService implements ActiveMQService, ConnectionFactoryAwar
         }
     }
 
-    @Override
-    public String serviceAddress() {
-        return getVmURL();
-    }
-
     @Override
     public String userName() {
         return null;
@@ -116,6 +111,10 @@ public class ActiveMQVMService implements ActiveMQService, ConnectionFactoryAwar
         return brokerService;
     }
 
+    protected String getBrokerUri(int connector) {
+        return getBrokerUri(brokerService, connector);
+    }
+
     public static String getBrokerUri(BrokerService broker, int connector) {
         try {
             return broker.getTransportConnectors().get(connector).getPublishableConnectString();
@@ -135,7 +134,7 @@ public class ActiveMQVMService implements ActiveMQService, ConnectionFactoryAwar
     }
 
     @Override
-    public void afterEach(ExtensionContext extensionContext) throws Exception {
+    public void afterEach(ExtensionContext extensionContext) {
         if (recycle) {
             shutdown();
         }
@@ -143,20 +142,20 @@ public class ActiveMQVMService implements ActiveMQService, ConnectionFactoryAwar
     }
 
     @Override
-    public void beforeEach(ExtensionContext extensionContext) throws Exception {
+    public void beforeEach(ExtensionContext extensionContext) {
         if (recycle) {
             initialize();
         }
     }
 
-    public String getVmURL() {
-        return getVmURL(true);
-    }
+    public abstract String getVmURL();
+
+    public abstract String getVmURL(boolean create);
 
-    public String getVmURL(boolean failoverURL) {
+    protected String getVmURL(boolean failoverURL, boolean create) {
         return failoverURL
-                ? String.format("failover:(%s?create=true)", brokerService.getVmConnectorURI().toString())
-                : this.brokerService.getVmConnectorURI().toString() + "?create=true";
+                ? String.format("failover:(%s?create=%b)", brokerService.getVmConnectorURI().toString(), create)
+                : this.brokerService.getVmConnectorURI().toString() + "?create=" + create;
     }
 
     public DestinationViewMBean getQueueMBean(String queueName) throws MalformedObjectNameException {
@@ -180,6 +179,6 @@ public class ActiveMQVMService implements ActiveMQService, ConnectionFactoryAwar
 
     @Deprecated
     public ConnectionFactory createConnectionFactory(Integer maximumRedeliveries) {
-        return ConnectionFactoryHelper.createConnectionFactory(getVmURL(), maximumRedeliveries);
+        return ConnectionFactoryHelper.createConnectionFactory(getVmURL(true), maximumRedeliveries);
     }
 }
diff --git a/test-infra/camel-test-infra-activemq/src/test/java/org/apache/camel/test/infra/activemq/services/ActiveMQEmbeddedService.java b/test-infra/camel-test-infra-activemq/src/test/java/org/apache/camel/test/infra/activemq/services/ActiveMQEmbeddedService.java
index 70ccc2129e9..f624742a6ca 100644
--- a/test-infra/camel-test-infra-activemq/src/test/java/org/apache/camel/test/infra/activemq/services/ActiveMQEmbeddedService.java
+++ b/test-infra/camel-test-infra-activemq/src/test/java/org/apache/camel/test/infra/activemq/services/ActiveMQEmbeddedService.java
@@ -17,171 +17,39 @@
 
 package org.apache.camel.test.infra.activemq.services;
 
-import java.io.IOException;
-import java.net.URISyntaxException;
-
-import javax.jms.ConnectionFactory;
-import javax.management.MalformedObjectNameException;
-import javax.management.ObjectName;
-
-import org.apache.activemq.ActiveMQConnectionFactory;
 import org.apache.activemq.broker.BrokerService;
-import org.apache.activemq.broker.jmx.DestinationViewMBean;
-import org.apache.camel.test.infra.activemq.common.ConnectionFactoryHelper;
-import org.junit.jupiter.api.extension.AfterEachCallback;
-import org.junit.jupiter.api.extension.BeforeEachCallback;
-import org.junit.jupiter.api.extension.ExtensionContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import static org.junit.jupiter.api.Assertions.fail;
-
-public class ActiveMQEmbeddedService implements ActiveMQService, ConnectionFactoryAware, BeforeEachCallback, AfterEachCallback {
+/**
+ * A regular embedded broker that uses the standard transport protocols (i.e.: AMQP, OpenWire, etc)
+ */
+public class ActiveMQEmbeddedService extends AbstractActiveMQEmbeddedService {
     private static final Logger LOG = LoggerFactory.getLogger(ActiveMQEmbeddedService.class);
-    private final BrokerService brokerService;
-    private final boolean recycle;
 
     public ActiveMQEmbeddedService() {
         this(ActiveMQEmbeddedServiceBuilder.defaultBroker().brokerService());
     }
 
     public ActiveMQEmbeddedService(BrokerService brokerService) {
-        this.brokerService = brokerService;
-        this.recycle = false;
+        this(brokerService, false);
     }
 
     public ActiveMQEmbeddedService(BrokerService brokerService, boolean recycle) {
-        this.brokerService = brokerService;
-        this.recycle = recycle;
-    }
-
-    @Override
-    public void initialize() {
-        LOG.info("Trying to start the embedded ActiveMQ");
-        try {
-            brokerService.start();
-            brokerService.waitUntilStarted();
-            LOG.info("Embedded ActiveMQ running at {}", serviceAddress());
-        } catch (Exception e) {
-            LOG.warn("Unable to start embedded ActiveMQ broker: {}", e.getMessage(), e);
-            fail(e.getMessage());
-        }
-    }
-
-    @Override
-    public void shutdown() {
-        LOG.debug("Trying to stop the embedded ActiveMQ");
-        try {
-            brokerService.stop();
-            brokerService.waitUntilStopped();
-            LOG.debug("Embedded ActiveMQ stopped");
-        } catch (Exception e) {
-            LOG.warn("Error stopping embedded ActiveMQ broker: {}", e.getMessage(), e);
-        }
+        super(brokerService, recycle);
     }
 
-    public void restart() {
-        shutdown();
-
-        LOG.info("Trying to start the restart ActiveMQ");
-        try {
-            brokerService.start(true);
-            brokerService.waitUntilStarted();
-            LOG.info("Embedded ActiveMQ running at {}", serviceAddress());
-        } catch (Exception e) {
-            LOG.warn("Unable to start embedded ActiveMQ broker: {}", e.getMessage(), e);
-            fail(e.getMessage());
-        }
-    }
-
-    @Override
-    public String serviceAddress() {
-        return getBrokerUri(brokerService, 0);
-    }
-
-    @Override
-    public String userName() {
-        return null;
+    public String getVmURL() {
+        return getVmURL(true, false);
     }
 
     @Override
-    public String password() {
+    public String getVmURL(boolean create) {
         return null;
     }
 
-    public int getConnectionCount() {
-        return brokerService.getTransportConnectors().get(0).getConnections().size();
-    }
-
-    public BrokerService getBrokerService() {
-        return brokerService;
-    }
-
-    public static String getBrokerUri(BrokerService broker, int connector) {
-        try {
-            return broker.getTransportConnectors().get(connector).getPublishableConnectString();
-        } catch (Exception e) {
-            LOG.warn("Unable to get ActiveMQ broker address: {}", e.getMessage(), e);
-            return null;
-        }
-    }
-
-    public int getPort() {
-        try {
-            return brokerService.getTransportConnectors().get(0).getServer().getSocketAddress().getPort();
-        } catch (URISyntaxException | IOException e) {
-            LOG.error("Error getting the port: {}", e.getMessage());
-            throw new RuntimeException("Error getting the port", e);
-        }
-    }
-
-    @Override
-    public void afterEach(ExtensionContext extensionContext) throws Exception {
-        if (recycle) {
-            shutdown();
-        }
-
-    }
-
     @Override
-    public void beforeEach(ExtensionContext extensionContext) throws Exception {
-        if (recycle) {
-            initialize();
-        }
-    }
-
-    public String getVmURL() {
-        return getVmURL(true);
-    }
-
-    public String getVmURL(boolean failoverURL) {
-        return failoverURL
-                ? String.format("failover:(%s?create=false)", brokerService.getVmConnectorURI().toString())
-                : this.brokerService.getVmConnectorURI().toString() + "?create=false";
-    }
-
-    public DestinationViewMBean getQueueMBean(String queueName) throws MalformedObjectNameException {
-        return getDestinationMBean(queueName, false);
-    }
-
-    public DestinationViewMBean getDestinationMBean(String destinationName, boolean topic) throws MalformedObjectNameException {
-        String domain = "org.apache.activemq";
-        String destinationType = topic ? "Topic" : "Queue";
-        ObjectName name = new ObjectName(
-                String.format("%s:type=Broker,brokerName=localhost,destinationType=%s,destinationName=%s",
-                        domain, destinationType, destinationName));
-        return (DestinationViewMBean) brokerService.getManagementContext().newProxyInstance(name,
-                DestinationViewMBean.class, true);
-    }
-
-    @Deprecated
-    public ConnectionFactory createConnectionFactory() {
-        return createConnectionFactory(null);
-    }
-
-    @Deprecated
-    public ConnectionFactory createConnectionFactory(Integer maximumRedeliveries) {
-        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(getVmURL());
-        return ConnectionFactoryHelper.createConnectionFactory(connectionFactory, maximumRedeliveries);
+    public String serviceAddress() {
+        return getBrokerUri(0);
     }
 }
diff --git a/test-infra/camel-test-infra-activemq/src/test/java/org/apache/camel/test/infra/activemq/services/ActiveMQEmbeddedServiceBuilder.java b/test-infra/camel-test-infra-activemq/src/test/java/org/apache/camel/test/infra/activemq/services/ActiveMQEmbeddedServiceBuilder.java
index dcf6139a403..25aa2af28e0 100644
--- a/test-infra/camel-test-infra-activemq/src/test/java/org/apache/camel/test/infra/activemq/services/ActiveMQEmbeddedServiceBuilder.java
+++ b/test-infra/camel-test-infra-activemq/src/test/java/org/apache/camel/test/infra/activemq/services/ActiveMQEmbeddedServiceBuilder.java
@@ -603,20 +603,6 @@ public class ActiveMQEmbeddedServiceBuilder {
         return defaultBroker(ActiveMQEmbeddedServiceBuilder.class.getSimpleName());
     }
 
-    /*
-    public static ConnectionFactory createPersistentConnectionFactory(String options) {
-        String url = createPersistentBrokerUrl(options);
-        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
-        // optimize AMQ to be as fast as possible so unit testing is quicker
-        connectionFactory.setCopyMessageOnSend(false);
-        connectionFactory.setOptimizeAcknowledge(true);
-        connectionFactory.setOptimizedMessageDispatch(true);
-        connectionFactory.setAlwaysSessionAsync(false);
-        connectionFactory.setTrustAllPackages(true);
-        return connectionFactory;
-    }
-     */
-
     public static ActiveMQEmbeddedServiceBuilder defaultBroker(String name) {
         return new ActiveMQEmbeddedServiceBuilder()
                 .withDeleteAllMessagesOnStartup(true)
diff --git a/test-infra/camel-test-infra-activemq/src/test/java/org/apache/camel/test/infra/activemq/services/ActiveMQVMService.java b/test-infra/camel-test-infra-activemq/src/test/java/org/apache/camel/test/infra/activemq/services/ActiveMQVMService.java
index 037d6e5b7de..3924d35174f 100644
--- a/test-infra/camel-test-infra-activemq/src/test/java/org/apache/camel/test/infra/activemq/services/ActiveMQVMService.java
+++ b/test-infra/camel-test-infra-activemq/src/test/java/org/apache/camel/test/infra/activemq/services/ActiveMQVMService.java
@@ -17,85 +17,43 @@
 
 package org.apache.camel.test.infra.activemq.services;
 
-import java.io.IOException;
-import java.net.URISyntaxException;
-
-import javax.jms.ConnectionFactory;
-import javax.management.MalformedObjectNameException;
-import javax.management.ObjectName;
-
 import org.apache.activemq.broker.BrokerService;
-import org.apache.activemq.broker.jmx.DestinationViewMBean;
-import org.apache.camel.test.infra.activemq.common.ConnectionFactoryHelper;
-import org.junit.jupiter.api.extension.AfterEachCallback;
-import org.junit.jupiter.api.extension.BeforeEachCallback;
-import org.junit.jupiter.api.extension.ExtensionContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import static org.junit.jupiter.api.Assertions.fail;
-
-public class ActiveMQVMService implements ActiveMQService, ConnectionFactoryAware, BeforeEachCallback, AfterEachCallback {
+/**
+ * An embedded broker that relies on the VM transport for communication.
+ *
+ * @see <a href="https://activemq.apache.org/vm-transport-reference">VM Transport Reference</a>
+ */
+public class ActiveMQVMService extends AbstractActiveMQEmbeddedService {
     private static final Logger LOG = LoggerFactory.getLogger(ActiveMQVMService.class);
-    private final BrokerService brokerService;
-    private final boolean recycle;
 
     public ActiveMQVMService() {
         this(ActiveMQEmbeddedServiceBuilder.defaultBroker().brokerService());
     }
 
     public ActiveMQVMService(BrokerService brokerService) {
-        this.brokerService = brokerService;
-        this.recycle = false;
+        this(brokerService, true);
     }
 
     public ActiveMQVMService(BrokerService brokerService, boolean recycle) {
-        this.brokerService = brokerService;
-        this.recycle = recycle;
+        super(brokerService, recycle);
     }
 
     @Override
-    public void initialize() {
-        LOG.info("Trying to start the embedded ActiveMQ");
-        try {
-            brokerService.start();
-            brokerService.waitUntilStarted();
-            LOG.info("Embedded ActiveMQ running at {}", serviceAddress());
-        } catch (Exception e) {
-            LOG.warn("Unable to start embedded ActiveMQ broker: {}", e.getMessage(), e);
-            fail(e.getMessage());
-        }
+    public String getVmURL() {
+        return getVmURL(true);
     }
 
     @Override
-    public void shutdown() {
-        LOG.debug("Trying to stop the embedded ActiveMQ");
-        try {
-            brokerService.stop();
-            brokerService.waitUntilStopped();
-            LOG.debug("Embedded ActiveMQ stopped");
-        } catch (Exception e) {
-            LOG.warn("Error stopping embedded ActiveMQ broker: {}", e.getMessage(), e);
-        }
-    }
-
-    public void restart() {
-        shutdown();
-
-        LOG.info("Trying to start the restart ActiveMQ");
-        try {
-            brokerService.start(true);
-            brokerService.waitUntilStarted();
-            LOG.info("Embedded ActiveMQ running at {}", serviceAddress());
-        } catch (Exception e) {
-            LOG.warn("Unable to start embedded ActiveMQ broker: {}", e.getMessage(), e);
-            fail(e.getMessage());
-        }
+    public String getVmURL(boolean create) {
+        return getVmURL(false, create);
     }
 
     @Override
     public String serviceAddress() {
-        return getVmURL();
+        return getVmURL(true);
     }
 
     @Override
@@ -107,79 +65,4 @@ public class ActiveMQVMService implements ActiveMQService, ConnectionFactoryAwar
     public String password() {
         return null;
     }
-
-    public int getConnectionCount() {
-        return brokerService.getTransportConnectors().get(0).getConnections().size();
-    }
-
-    public BrokerService getBrokerService() {
-        return brokerService;
-    }
-
-    public static String getBrokerUri(BrokerService broker, int connector) {
-        try {
-            return broker.getTransportConnectors().get(connector).getPublishableConnectString();
-        } catch (Exception e) {
-            LOG.warn("Unable to get ActiveMQ broker address: {}", e.getMessage(), e);
-            return null;
-        }
-    }
-
-    public int getPort() {
-        try {
-            return brokerService.getTransportConnectors().get(0).getServer().getSocketAddress().getPort();
-        } catch (URISyntaxException | IOException e) {
-            LOG.error("Error getting the port: {}", e.getMessage());
-            throw new RuntimeException("Error getting the port", e);
-        }
-    }
-
-    @Override
-    public void afterEach(ExtensionContext extensionContext) throws Exception {
-        if (recycle) {
-            shutdown();
-        }
-
-    }
-
-    @Override
-    public void beforeEach(ExtensionContext extensionContext) throws Exception {
-        if (recycle) {
-            initialize();
-        }
-    }
-
-    public String getVmURL() {
-        return getVmURL(true);
-    }
-
-    public String getVmURL(boolean failoverURL) {
-        return failoverURL
-                ? String.format("failover:(%s?create=true)", brokerService.getVmConnectorURI().toString())
-                : this.brokerService.getVmConnectorURI().toString() + "?create=true";
-    }
-
-    public DestinationViewMBean getQueueMBean(String queueName) throws MalformedObjectNameException {
-        return getDestinationMBean(queueName, false);
-    }
-
-    public DestinationViewMBean getDestinationMBean(String destinationName, boolean topic) throws MalformedObjectNameException {
-        String domain = "org.apache.activemq";
-        String destinationType = topic ? "Topic" : "Queue";
-        ObjectName name = new ObjectName(
-                String.format("%s:type=Broker,brokerName=localhost,destinationType=%s,destinationName=%s",
-                        domain, destinationType, destinationName));
-        return (DestinationViewMBean) brokerService.getManagementContext().newProxyInstance(name,
-                DestinationViewMBean.class, true);
-    }
-
-    @Deprecated
-    public ConnectionFactory createConnectionFactory() {
-        return createConnectionFactory(null);
-    }
-
-    @Deprecated
-    public ConnectionFactory createConnectionFactory(Integer maximumRedeliveries) {
-        return ConnectionFactoryHelper.createConnectionFactory(getVmURL(), maximumRedeliveries);
-    }
 }