public void testMessagingClientTopic() throws Exception {

    String clientId = "0";
    MessagingClient messagingClient = new JmsMessagingClient(clientId, this, properties, false);

    messagingClient.start();
    sendMessage(TOPIC_NAME);
    checkMessage(clientId, TOPIC);
    messagingClient.stop(true);
  }
  public void testMessagingClientQueue() throws Exception {

    String clientId = "3";
    MessagingClient messagingClient = new JmsMessagingClient(clientId, this, properties, false);

    messagingClient.start();
    sendMessage(QUEUE_NAME);
    checkMessage(clientId, QUEUE);
    messagingClient.stop(true);
  }
  public void testMessageSelectors() throws Exception {

    String clientId = "13";
    // Selector to include test message
    String messageSelector = propertyName + " LIKE 'test%'";
    MessagingClient messagingClient =
        new JmsMessagingClient(clientId, this, properties, messageSelector, false);
    messagingClient.start();
    sendMessage(TOPIC_NAME);
    checkMessage(clientId, TOPIC);
    messagingClient.stop(true);

    clientId = "14";
    // Selector to omit test message
    messageSelector = propertyName + " LIKE 'testing%'";
    messagingClient = new JmsMessagingClient(clientId, this, properties, messageSelector, false);
    messagingClient.start();
    sendMessage(TOPIC_NAME);
    checkNoMessages();
    messagingClient.stop(true);
  }
  public void testMessagingClientDurableTopic() throws Exception {

    String clientId = "1";
    MessagingClient messagingClient = new JmsMessagingClient(clientId, this, properties, true);

    // Establish that the client can start and receive messages
    messagingClient.start();
    sendMessage(TOPIC_NAME);
    checkMessage(clientId, TOPIC);
    messagingClient.stop(false);

    // Check to see if messages are received in a durable fashion
    sendMessage(TOPIC_NAME);
    messagingClient.start();
    checkMessage(clientId, TOPIC);
    messagingClient.stop(true);

    // Make sure durable subscriptions were closed
    sendMessage(TOPIC_NAME);
    messagingClient.start();
    checkNoMessages();
    messagingClient.stop(true);
  }
  public void testMessagingClientMultipleTopics() throws Exception {

    String clientId = "2";
    String topicName = "additionalTopic";
    String topic = "fedora.test.additional";
    properties.setProperty("topic." + topicName, topic);
    MessagingClient messagingClient = new JmsMessagingClient(clientId, this, properties, true);

    messagingClient.start();
    sendMessage(TOPIC_NAME);
    checkMessage(clientId, TOPIC);
    sendMessage(topicName);
    checkMessage(clientId, topic);
    messagingClient.stop(true);
  }
  public void testInvalidProperties() throws Exception {
    // Null properties
    try {
      new JmsMessagingClient("4", this, null, false);
      fail("Creating a Messagingient with null properties " + "should throw an exception");
    } catch (MessagingException me) {
      assertTrue(me.getMessage().contains("Connection properties may not be null"));
    }

    // Missing all properties
    properties = new Properties();

    try {
      new JmsMessagingClient("5", this, properties, false);
      fail("Creating a Messaging Client with no properties " + "should throw an exception");
    } catch (MessagingException me) {
      assertTrue(me.getMessage().contains("Propery values"));
      assertTrue(me.getMessage().contains("must be provided"));
    }

    // Missing connection factory property
    properties = new Properties();
    properties.setProperty(
        Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
    properties.setProperty(
        Context.PROVIDER_URL,
        "vm://localhost?broker.useShutdownHook=false&broker.persistent=false");

    try {
      new JmsMessagingClient("6", this, properties, false);
      fail(
          "Creating a Messaging Client with no connection factory "
              + "property should throw an exception");
    } catch (MessagingException me) {
      assertTrue(me.getMessage().contains("Propery values"));
      assertTrue(me.getMessage().contains("must be provided"));
    }

    // Missing provider url property
    properties = new Properties();
    properties.setProperty(
        Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
    properties.setProperty(JMSManager.CONNECTION_FACTORY_NAME, "ConnectionFactory");

    try {
      new JmsMessagingClient("7", this, properties, false);
      fail(
          "Creating a Messaging Client with no provider url "
              + "property should throw an exception");
    } catch (MessagingException me) {
      assertTrue(me.getMessage().contains("Propery values"));
      assertTrue(me.getMessage().contains("must be provided"));
    }

    // Missing initial context factory property
    properties = new Properties();
    properties.setProperty(
        Context.PROVIDER_URL,
        "vm://localhost?broker.useShutdownHook=false&broker.persistent=false");
    properties.setProperty(JMSManager.CONNECTION_FACTORY_NAME, "ConnectionFactory");

    try {
      new JmsMessagingClient("8", this, properties, false);
      fail(
          "Creating a Messaging Client with no initial context factory "
              + "property should throw an exception");
    } catch (MessagingException me) {
      assertTrue(me.getMessage().contains("Propery values"));
      assertTrue(me.getMessage().contains("must be provided"));
    }

    // Missing destination properties
    properties = new Properties();
    properties.setProperty(
        Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
    properties.setProperty(
        Context.PROVIDER_URL,
        "vm://localhost?broker.useShutdownHook=false&broker.persistent=false");
    properties.setProperty(JMSManager.CONNECTION_FACTORY_NAME, "ConnectionFactory");
    try {
      MessagingClient messagingClient = new JmsMessagingClient("9", this, properties, false);
      messagingClient.start();
      fail(
          "Starting a Messaging Client with no destination "
              + "properties should throw an exception");
    } catch (MessagingException me) {
      assertTrue(me.getMessage().contains("No destinations available"));
    }

    // Invalid initial context factory
    properties = new Properties();
    properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "test.InvalidInitialContextFactory");
    properties.setProperty(
        Context.PROVIDER_URL,
        "vm://localhost?broker.useShutdownHook=false&broker.persistent=false");
    properties.setProperty(JMSManager.CONNECTION_FACTORY_NAME, "ConnectionFactory");
    try {
      MessagingClient messagingClient = new JmsMessagingClient("10", this, properties, false);
      messagingClient.start();
      fail(
          "Starting a Messaging Client with an invalid initial "
              + "context factory should throw an exception");
    } catch (MessagingException me) {
      assertTrue(me.getMessage().contains("Cannot instantiate"));
    }

    // Invalid provider url
    properties = new Properties();
    properties.setProperty(
        Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
    properties.setProperty(Context.PROVIDER_URL, "tcp://localhost:00000");
    properties.setProperty(JMSManager.CONNECTION_FACTORY_NAME, "ConnectionFactory");
    try {
      MessagingClient messagingClient = new JmsMessagingClient("11", this, properties, false);
      messagingClient.start();
      fail(
          "Starting a Messaging Client with an invalid "
              + "provider url should throw an exception");
    } catch (MessagingException me) {
      assertTrue(me.getMessage().contains("Could not connect to broker URL"));
    }

    // Invalid connection factory
    properties = new Properties();
    properties.setProperty(
        Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
    properties.setProperty(
        Context.PROVIDER_URL,
        "vm://localhost?broker.useShutdownHook=false&broker.persistent=false");
    properties.setProperty(JMSManager.CONNECTION_FACTORY_NAME, "InvalidValue");
    try {
      MessagingClient messagingClient = new JmsMessagingClient("12", this, properties, false);
      messagingClient.start();
      fail(
          "Starting a Messaging Client with an invalid connection "
              + "factory name should throw an exception");
    } catch (MessagingException me) {
      assertTrue(me.getMessage().contains("jndiLookup(InvalidValue) failed"));
    }
  }