<commit_before>package brooklyn.entity.messaging.activemq;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;

import javax.jms.Connection;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import brooklyn.entity.basic.ApplicationBuilder;
import brooklyn.entity.basic.Entities;
import brooklyn.entity.java.UsesJmx;
import brooklyn.entity.java.UsesJmx.JmxAgentModes;
import brooklyn.entity.proxying.EntitySpec;
import brooklyn.entity.trait.Startable;
import brooklyn.location.Location;
import brooklyn.test.EntityTestUtils;
import brooklyn.test.entity.TestApplication;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;

/**
 * Test the operation of the {@link ActiveMQBroker} class.
 */
public class ActiveMQIntegrationTest {
    private static final Logger log = LoggerFactory.getLogger(ActiveMQIntegrationTest.class);

    private TestApplication app;
    private Location testLocation;
    private ActiveMQBroker activeMQ;

    @BeforeMethod(alwaysRun = true)
    public void setup() throws Exception {
        app = ApplicationBuilder.newManagedApp(TestApplication.class);
        testLocation = app.newLocalhostProvisioningLocation();
    }

    @AfterMethod(alwaysRun = true)
    public void shutdown() throws Exception {
        if (app != null) Entities.destroyAll(app.getManagementContext());
    }

    /**
     * Test that the broker starts up and sets SERVICE_UP correctly.
     */
    @Test(groups = "Integration")
    public void canStartupAndShutdown() throws Exception {
        activeMQ = app.createAndManageChild(EntitySpec.create(ActiveMQBroker.class));

        activeMQ.start(ImmutableList.of(testLocation));
        EntityTestUtils.assertAttributeEqualsEventually(ImmutableMap.of("timeout", 10*60*1000), activeMQ, Startable.SERVICE_UP, true);
        log.info("JMX URL is "+activeMQ.getAttribute(UsesJmx.JMX_URL));
        activeMQ.stop();
        assertFalse(activeMQ.getAttribute(Startable.SERVICE_UP));
    }

    /**
     * Test that the broker starts up and sets SERVICE_UP correctly,
     * when a jmx port is supplied
     */
    @Test(groups = "Integration")
    public void canStartupAndShutdownWithCustomJmx() throws Exception {
        activeMQ = app.createAndManageChild(EntitySpec.create(ActiveMQBroker.class)
                .configure("jmxPort", "11099+"));
       
        activeMQ.start(ImmutableList.of(testLocation));
        EntityTestUtils.assertAttributeEqualsEventually(ImmutableMap.of("timeout", 10*60*1000), activeMQ, Startable.SERVICE_UP, true);
        log.info("JMX URL is "+activeMQ.getAttribute(UsesJmx.JMX_URL));
        activeMQ.stop();
        assertFalse(activeMQ.getAttribute(Startable.SERVICE_UP));
    }

    @Test(groups = "Integration")
    public void canStartTwo() throws Exception {
        ActiveMQBroker activeMQ1 = app.createAndManageChild(EntitySpec.create(ActiveMQBroker.class));
        ActiveMQBroker activeMQ2 = app.createAndManageChild(EntitySpec.create(ActiveMQBroker.class));

        activeMQ1.start(ImmutableList.of(testLocation));
        EntityTestUtils.assertAttributeEqualsEventually(ImmutableMap.of("timeout", 10*60*1000), activeMQ1, Startable.SERVICE_UP, true);
        log.info("JMX URL is "+activeMQ1.getAttribute(UsesJmx.JMX_URL));

        activeMQ2.start(ImmutableList.of(testLocation));
        EntityTestUtils.assertAttributeEqualsEventually(ImmutableMap.of("timeout", 10*60*1000), activeMQ2, Startable.SERVICE_UP, true);
        log.info("JMX URL is "+activeMQ2.getAttribute(UsesJmx.JMX_URL));
    }

    /**
     * Test that setting the 'queue' property causes a named queue to be created.
     */
    @Test(groups = "Integration")
    public void testCreatingQueuesDefault() throws Exception {
        String url = testCreatingQueuesInternal(null);
        // localhost default is rmi
        Assert.assertTrue(url.contains("rmi"));
    }

    @Test(groups = "Integration")
    public void testCreatingQueuesRmi() throws Exception {
        String url = testCreatingQueuesInternal(JmxAgentModes.JMX_RMI_CUSTOM_AGENT);
        Assert.assertTrue(url.contains("rmi://"));
        Assert.assertFalse(url.contains("rmi:///jndi"));
        Assert.assertFalse(url.contains("jmxmp"));
    }

    @Test(groups = "Integration")
    public void testCreatingQueuesJmxmp() throws Exception {
        String url = testCreatingQueuesInternal(JmxAgentModes.JMXMP);
        // localhost default is rmi
        Assert.assertTrue(url.contains("jmxmp"));
        Assert.assertFalse(url.contains("rmi"));
    }

    @Test(groups = "Integration")
    public void testCreatingQueuesNoAgent() throws Exception {
        String url = testCreatingQueuesInternal(JmxAgentModes.NONE);
        // localhost default is rmi
        Assert.assertTrue(url.contains("rmi:///jndi"));
        Assert.assertFalse(url.contains("jmxmp"));
    }

    public String testCreatingQueuesInternal(JmxAgentModes mode) throws Exception {
        String queueName = "testQueue";
        int number = 20;
        String content = "01234567890123456789012345678901";

        // Start broker with a configured queue
        // FIXME Not yet using app.createAndManageChild because later in test do activeMQ.queueNames,
        // which is not on interface
        activeMQ = app.createAndManageChild(EntitySpec.create(ActiveMQBroker.class)<commit_msg>Fix ActiveMQIntegrationTest<commit_after>package brooklyn.entity.messaging.activemq;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;

import javax.jms.Connection;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import brooklyn.entity.basic.ApplicationBuilder;
import brooklyn.entity.basic.Entities;
import brooklyn.entity.java.UsesJmx;
import brooklyn.entity.java.UsesJmx.JmxAgentModes;
import brooklyn.entity.proxying.EntitySpec;
import brooklyn.entity.trait.Startable;
import brooklyn.location.Location;
import brooklyn.test.EntityTestUtils;
import brooklyn.test.entity.TestApplication;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;

/**
 * Test the operation of the {@link ActiveMQBroker} class.
 */
public class ActiveMQIntegrationTest {
    private static final Logger log = LoggerFactory.getLogger(ActiveMQIntegrationTest.class);

    private TestApplication app;
    private Location testLocation;
    private ActiveMQBroker activeMQ;

    @BeforeMethod(alwaysRun = true)
    public void setup() throws Exception {
        app = ApplicationBuilder.newManagedApp(TestApplication.class);
        testLocation = app.newLocalhostProvisioningLocation();
    }

    @AfterMethod(alwaysRun = true)
    public void shutdown() throws Exception {
        if (app != null) Entities.destroyAll(app.getManagementContext());
    }

    /**
     * Test that the broker starts up and sets SERVICE_UP correctly.
     */
    @Test(groups = "Integration")
    public void canStartupAndShutdown() throws Exception {
        activeMQ = app.createAndManageChild(EntitySpec.create(ActiveMQBroker.class));

        activeMQ.start(ImmutableList.of(testLocation));
        EntityTestUtils.assertAttributeEqualsEventually(ImmutableMap.of("timeout", 10*60*1000), activeMQ, Startable.SERVICE_UP, true);
        log.info("JMX URL is "+activeMQ.getAttribute(UsesJmx.JMX_URL));
        activeMQ.stop();
        assertFalse(activeMQ.getAttribute(Startable.SERVICE_UP));
    }

    /**
     * Test that the broker starts up and sets SERVICE_UP correctly,
     * when a jmx port is supplied
     */
    @Test(groups = "Integration")
    public void canStartupAndShutdownWithCustomJmx() throws Exception {
        activeMQ = app.createAndManageChild(EntitySpec.create(ActiveMQBroker.class)
                .configure("jmxPort", "11099+"));
       
        activeMQ.start(ImmutableList.of(testLocation));
        EntityTestUtils.assertAttributeEqualsEventually(ImmutableMap.of("timeout", 10*60*1000), activeMQ, Startable.SERVICE_UP, true);
        log.info("JMX URL is "+activeMQ.getAttribute(UsesJmx.JMX_URL));
        activeMQ.stop();
        assertFalse(activeMQ.getAttribute(Startable.SERVICE_UP));
    }

    @Test(groups = "Integration")
    public void canStartTwo() throws Exception {
        ActiveMQBroker activeMQ1 = app.createAndManageChild(EntitySpec.create(ActiveMQBroker.class));
        ActiveMQBroker activeMQ2 = app.createAndManageChild(EntitySpec.create(ActiveMQBroker.class));

        activeMQ1.start(ImmutableList.of(testLocation));
        EntityTestUtils.assertAttributeEqualsEventually(ImmutableMap.of("timeout", 10*60*1000), activeMQ1, Startable.SERVICE_UP, true);
        log.info("JMX URL is "+activeMQ1.getAttribute(UsesJmx.JMX_URL));

        activeMQ2.start(ImmutableList.of(testLocation));
        EntityTestUtils.assertAttributeEqualsEventually(ImmutableMap.of("timeout", 10*60*1000), activeMQ2, Startable.SERVICE_UP, true);
        log.info("JMX URL is "+activeMQ2.getAttribute(UsesJmx.JMX_URL));
    }

    /**
     * Test that setting the 'queue' property causes a named queue to be created.
     */
    @Test(groups = "Integration")
    public void testCreatingQueuesDefault() throws Exception {
        String url = testCreatingQueuesInternal(null);
        // localhost default is jmxmp
        Assert.assertTrue(url.contains("jmxmp"), "url="+url);
    }

    @Test(groups = "Integration")
    public void testCreatingQueuesRmi() throws Exception {
        String url = testCreatingQueuesInternal(JmxAgentModes.JMX_RMI_CUSTOM_AGENT);
        Assert.assertTrue(url.contains("rmi://"), "url="+url);
        Assert.assertFalse(url.contains("rmi:///jndi"), "url="+url);
        Assert.assertFalse(url.contains("jmxmp"), "url="+url);
    }

    @Test(groups = "Integration")
    public void testCreatingQueuesJmxmp() throws Exception {
        String url = testCreatingQueuesInternal(JmxAgentModes.JMXMP);
        // localhost default is rmi
        Assert.assertTrue(url.contains("jmxmp"), "url="+url);
        Assert.assertFalse(url.contains("rmi"), "url="+url);
    }

    @Test(groups = "Integration")
    public void testCreatingQueuesNoAgent() throws Exception {
        String url = testCreatingQueuesInternal(JmxAgentModes.NONE);
        // localhost default is rmi
        Assert.assertTrue(url.contains("rmi:///jndi"), "url="+url);
        Assert.assertFalse(url.contains("jmxmp"), "url="+url);
    }

    public String testCreatingQueuesInternal(JmxAgentModes mode) throws Exception {
        String queueName = "testQueue";
        int number = 20;
        String content = "01234567890123456789012345678901";

        // Start broker with a configured queue
        // FIXME Not yet using app.createAndManageChild because later in test do activeMQ.queueNames,
        // which is not on interface
        activeMQ = app.createAndManageChild(EntitySpec.create(ActiveMQBroker.class)
{'fraction_non_alphanumeric': 0.10582639714625446, 'fraction_numerical': 0.013759130287073212, 'mean_word_length': 4.044987146529563, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6821556', 'n_tokens_mistral': 3617, 'n_tokens_neox': 3422, 'n_words': 658}