Package org.jboss.jopr.jsfunit.as5.jmsDestinations

Source Code of org.jboss.jopr.jsfunit.as5.jmsDestinations.JMSTest

/*
* JBoss, Home of Professional Open Source.
* Copyright 2009, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.jboss.jopr.jsfunit.as5.jmsDestinations;

import com.gargoylesoftware.htmlunit.html.*;
import java.io.IOException;
import java.util.Map;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.ArrayList;
import org.jboss.jopr.jsfunit.exceptions.EmbJoprTestException;
import org.jboss.jopr.jsfunit.as5.ResourceTestBase;
import org.jboss.deployers.spi.management.KnownComponentTypes;
import org.jboss.metatype.api.values.SimpleValueSupport;
import org.jboss.metatype.api.values.MetaValue;
import org.jboss.managed.api.ComponentType;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSubscriber;
import javax.jms.TopicSession;
import javax.naming.InitialContext;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSession;
import javax.jms.QueueSender;
import javax.jms.QueueReceiver;
import javax.jms.TextMessage;
import javax.jms.JMSException;
import javax.jms.DeliveryMode;
import javax.naming.NamingException;
import org.jboss.metatype.api.values.CompositeValue;
import org.jboss.metatype.api.values.CompositeValueSupport;
import org.jboss.metatype.api.values.MapCompositeValueSupport;
import org.jboss.metatype.api.types.SimpleMetaType;
import org.jboss.metatype.api.types.MapCompositeMetaType;
import org.jboss.metatype.plugins.types.MutableCompositeMetaType;

/**
* This is the base test class for JMS tests. This class
* contains helper methods for creating and deleting destinations as
* well as methods for sending/receiving messages to/from destinations.
*
* This test class should be run against JBAS 5.x.
*
* @author Farah Juma
*
*/

public abstract class JMSTest extends ResourceTestBase {
   
    // Nav panel labels for JMS destinations
    public static final String JMS_NAV_LABEL="JMS Destinations";
    public static final String TOPICS_NAV_LABEL = "Topics";
    public static final String QUEUES_NAV_LABEL = "Queues";
   
    // Template values
    public static final String QUEUE_DEFAULT_TEMPLATE="default__Queue";
    public static final String TOPIC_DEFAULT_TEMPLATE="default__Topic";
   
    // Component types
    public static final ComponentType QUEUE_COMPONENT_TYPE = KnownComponentTypes.JMSDestination.Queue.getType();
    public static final ComponentType TOPIC_COMPONENT_TYPE = KnownComponentTypes.JMSDestination.Topic.getType();
   
    // Connection factories for topic and queues
    public static final String TOPIC_FACTORY = "/ConnectionFactory";
    public static final String QUEUE_FACTORY = "/ConnectionFactory";
   
    // Topic Metrics
    public static final String ALL_MSG_COUNT = "All Message Count";
    public static final String ALL_SUB_COUNT = "All Subscriptions Count";
    public static final String DURABLE_MSG_COUNT = "Durable Message Count";
    public static final String DURABLE_SUB_COUNT = "Durable Subscriptions Count";
    public static final String NON_DURABLE_MSG_COUNT = "Non Durable Message Count";
    public static final String NON_DURABLE_SUB_COUNT = "Non Durable Subscriptions Count";
   
    // Queue Metrics
    public static final String CONSUMER_COUNT = "Consumer Count";
    public static final String DELIVERING_COUNT = "Delivering Count";
    public static final String MSG_COUNT = "Message Count";
    public static final String SCHEDULED_MSG_COUNT = "Scheduled Message Count";
    public static final String COUNT = "Count";
    public static final String COUNT_DELTA = "Count Delta";
    public static final String DEPTH = "Depth";
    public static final String DEPTH_DELTA = "Depth Delta";
    public static final String TIME_LAST_UPDATE = "Time Last Update";
   
    // Common operations
    public static final String LIST_ALL_MSGS = "List All Messages";
    public static final String LIST_DURABLE_MSGS = "List Durable Messages";
    public static final String LIST_NON_DURABLE_MSGS = "List Non Durable Messages";
    public static final String REMOVE_ALL_MSGS = "Remove All Messages";
    public static final String START = "Start";
    public static final String STOP = "Stop";
    public static final String CREATE = "Create";
    public static final String DESTROY = "Destroy";
   
    // Topic-specific operations
    public static final String LIST_ALL_SUB = "List All Subscriptions";
    public static final String LIST_ALL_SUB_AS_HTML = "List All Subscriptions As HTML";
    public static final String LIST_DURABLE_SUB = "List Durable Subscriptions";
    public static final String LIST_DURABLE_SUB_AS_HTML = "List Durable Subscriptions As HTML";
    public static final String LIST_NON_DURABLE_SUB = "List Non-Durable Subscriptions";
    public static final String LIST_NON_DURABLE_SUB_AS_HTML = "List Non-Durable Subscriptions As HTML";
   
    // Queue-specific operations
    public static final String LIST_MSG_COUNTER_AS_HTML = "List Message Counter As HTML";
    public static final String LIST_MSG_COUNTER_HISTORY_AS_HTML = "List Message Counter History As HTML";
    public static final String RESET_MSG_COUNTER = "Reset Message Counter";
    public static final String RESET_MSG_COUNTER_HISTORY = "Reset Message Counter History";
   
    // Column numbers for the headers in the "List * Messages" results table
    public static final int JMS_CORRELATION_ID = 0;
    public static final int JMS_MESSAGE_ID = 1;
    public static final int JMS_TIMESTAMP = 2;
   
    public enum DestinationType {
        QUEUE(QUEUES_NAV_LABEL, "Queue"),
        TOPIC(TOPICS_NAV_LABEL, "Topic");
       
        public final String navLabel;
        public final String name;
       
        DestinationType(String navLabel, String name) {
            this.navLabel = navLabel;
            this.name = name;
        }
       
        public String getNavLabel() {
            return this.navLabel;
        }
       
        public String getName() {
            return this.name;
        }
    }
   
    // Useful constants for the tests
    public static final String SERVICE_FILE_ROOT_ELEMENT = "server";
    public static final String SERVICE_FILE_CHILD_ELEMENT = "mbean";
    public static final String CLIENT_ID = "EmbjoprTest";
    public static final String EMBJOPR_TEST_PROPERTY = "EmbjoprTestProperty";
    public static final String EMBJOPR_TEST_SELECTOR = EMBJOPR_TEST_PROPERTY + "=0";
    public static final long SCHEDULED_TIME = 3600000;
    public static final String DLQ = "jboss.messaging.destination:name=DLQ,service=Queue";
    public static final String EXPIRY_QUEUE = "jboss.messaging.destination:name=ExpiryQueue,service=Queue";
    public static final String SERVER_PEER = "jboss.messaging:service=ServerPeer";
    public static final String DURABLE = "Durable";
    public static final String NON_DURABLE = "Non Durable";
   
    public TopicConnection topicConnection = null;
    public QueueConnection queueConnection = null;
   
    /** The following variables are needed for tests that use secure topics/queues. **/
   
    // The meta type
    protected static final MapCompositeMetaType securityConfType;

    // The composite meta type.
    public static MutableCompositeMetaType composite;

    static
    {
        // Create the meta type
        composite = new MutableCompositeMetaType("SecurityConfig", "The security config");
        composite.addItem("read", "read permission", SimpleMetaType.BOOLEAN);
        composite.addItem("write", "write permission", SimpleMetaType.BOOLEAN);
        composite.addItem("create", "create permission", SimpleMetaType.BOOLEAN);
        composite.freeze();
        securityConfType = new MapCompositeMetaType(composite);
    }
   
    /**
     * Create a new topic or queue using the given destination type and properties.
     */
    protected void createDestination(DestinationType destinationType,
                                     String templateName,
                                     Map<String, MetaValue> propertiesMap) throws IOException, EmbJoprTestException {
        expandNavTreeArrow(JMS_NAV_LABEL);
        createResource(destinationType.getNavLabel(), templateName, propertiesMap);
    }
     
    /**
     * Delete the given JMS destination.
     *
     * Assumes the JMS Destinations tree node is already expanded.
     */
    protected void deleteDestination(DestinationType destinationType,
                                     String destinationName) throws Exception {
        //refreshTreeNode(JMS_NAV_LABEL);
        clickNavTreeLink(destinationType.getNavLabel());
        deleteResource(RESOURCE_SUMMARY_FORM, destinationName);
    }
        
    /**
     * Create a basic queue. Return the mapping of property names to property
     * values.
     */
    protected Map<String, MetaValue> createQueue(String queueName) throws IOException, EmbJoprTestException {
   
        Map<String, MetaValue> propertiesMap = new LinkedHashMap<String, MetaValue>();
        propertiesMap.put("name", SimpleValueSupport.wrap(queueName));
        propertiesMap.put("JNDIName", SimpleValueSupport.wrap(queueName));
        propertiesMap.put("clustered", SimpleValueSupport.wrap(Boolean.FALSE));
        propertiesMap.put("downCacheSize", SimpleValueSupport.wrap(new Integer(1500)));
        propertiesMap.put("fullSize", SimpleValueSupport.wrap(new Integer(76000)));
        propertiesMap.put("maxSize", SimpleValueSupport.wrap(new Integer(76000)));
        /** propertiesMap.put("messageCounterHistoryDayLimit", SimpleValueSupport.wrap(new Integer(5))); **/
        propertiesMap.put("maxDeliveryAttempts", SimpleValueSupport.wrap(new Integer(5)));
        propertiesMap.put("pageSize", SimpleValueSupport.wrap(new Integer(1500)));
       
        createDestination(DestinationType.QUEUE, QUEUE_DEFAULT_TEMPLATE, propertiesMap);
        client.click(SAVE_BUTTON);
       
        return propertiesMap;
    }
   
    /**
     * Create a basic topic. Return the mapping of property names to property
     * values.
     */
    protected Map<String, MetaValue> createTopic(String topicName) throws IOException, EmbJoprTestException {
        Map<String, MetaValue> propertiesMap = new LinkedHashMap<String, MetaValue>();
        propertiesMap.put("name", SimpleValueSupport.wrap(topicName));
        propertiesMap.put("JNDIName", SimpleValueSupport.wrap(topicName));
        propertiesMap.put("clustered", SimpleValueSupport.wrap(Boolean.TRUE));
        propertiesMap.put("fullSize", SimpleValueSupport.wrap(new Integer(70000)));
        propertiesMap.put("maxSize", SimpleValueSupport.wrap(new Integer(70000)));
        propertiesMap.put("redeliveryDelay", SimpleValueSupport.wrap(new Long(60000)));
        propertiesMap.put("downCacheSize", SimpleValueSupport.wrap(new Integer(2000)));
       
        createDestination(DestinationType.TOPIC, TOPIC_DEFAULT_TEMPLATE, propertiesMap);
        client.click(SAVE_BUTTON);
       
        return propertiesMap;
    }
   
    protected String getDestinationDeploymentFile(String jndiName) {
        return System.getProperty("jsfunit.deploy.dir") + "/" + jndiName + "-service.xml";
    }
   
    /**
     * Create a new topic or queue with a security role configured.
     */
    protected void createDestinationWithSecurityRole(DestinationType destinationType,
                                                     String destinationTemplate,
                                                     String jndiName,
                                                     String expectedMessage,
                                                     ComponentType componentType,
                                                     String roleName,
                                                     boolean canRead,
                                                     boolean canWrite,
                                                     boolean canCreate) throws Exception {

        Map<String, MetaValue> propertiesMap = new LinkedHashMap<String, MetaValue>();
        propertiesMap.put("name", SimpleValueSupport.wrap(jndiName));
        propertiesMap.put("JNDIName", SimpleValueSupport.wrap(jndiName));

        // Fill in the property values
        createDestination(destinationType, destinationTemplate, propertiesMap);

        // Add a security role
        createSecurityRole(roleName, canRead, canWrite, canCreate);

        checkClientAndServerMessages(expectedMessage, expectedMessage, false);

        // Make sure the destination is deployed and that the securityConfig property
        // was set correctly
        assertTrue("The destination is not deployed ",
                   isDeployed(jndiName + "-service.xml"));

        Map<String, MetaValue> actualProperties = getComponentProperties(jndiName, componentType);

        Map<String, MetaValue> values = new HashMap<String, MetaValue>();
        values.put(roleName, createCompositeValue(true, true, true));
        CompositeValue expectedSecurityConfigProp = new MapCompositeValueSupport(values, securityConfType);

        assertEquals("The 'securityConfig' property was not set correctly: ",
                     expectedSecurityConfigProp,
                     actualProperties.get("securityConfig"));
    }

    /**
     * Create a security role.
     */
    protected void createSecurityRole(String roleName, boolean canRead,
                                      boolean canWrite, boolean canCreate) throws IOException {

        HtmlForm form = (HtmlForm)client.getElement(RESOURCE_CONFIGURATION_FORM);
        String xpath = ".//a[@title=\"" + ADD_NEW_CONFIG_BUTTON + "\"]";

        HtmlAnchor addSecurityRoleButton = (HtmlAnchor)form.getFirstByXPath(xpath);
        addSecurityRoleButton.click();

        // Create the new role
        Map<String, MetaValue> securityRoleMap = new LinkedHashMap<String, MetaValue>();
        securityRoleMap.put("name", SimpleValueSupport.wrap(roleName));
        securityRoleMap.put("read", SimpleValueSupport.wrap(canRead));
        securityRoleMap.put("write", SimpleValueSupport.wrap(canWrite));
        securityRoleMap.put("create", SimpleValueSupport.wrap(canCreate));

        Map<String, String> formattedPropertiesMap = formatPropertiesMap(securityRoleMap);
        fillOutForm(formattedPropertiesMap, EDIT_RESOURCE_CONFIGURATION_FORM)

        client.click(EDIT_RESOURCE_CONFIGURATION_FORM_OK_BUTTON);
        client.click(SAVE_BUTTON);
    }

    /**
     * Set up a security role.
     */
    protected CompositeValue createCompositeValue(Boolean read, Boolean write, Boolean create) {
        Map<String, MetaValue> map = new HashMap<String, MetaValue>();

        map.put("read", new SimpleValueSupport(SimpleMetaType.BOOLEAN, read));
        map.put("write", new SimpleValueSupport(SimpleMetaType.BOOLEAN, write));
        map.put("create", new SimpleValueSupport(SimpleMetaType.BOOLEAN, create));

        return new CompositeValueSupport(composite, map);
    }

    /**
     * Check that the metrics for the given destination are correct.
     */
    protected void checkDestinationMetrics(String jndiName,
                                           Map<String, String> expectedMetrics,
                                           ArrayList<String> summaryMetrics,
                                           DestinationType destinationType) throws Exception {
        checkResourceMetrics(JMS_NAV_LABEL, destinationType.getNavLabel(),
                             jndiName, expectedMetrics, summaryMetrics);

        // Clean up
        disconnect();
        deleteDestination(destinationType, jndiName)
    }
   
    /**
     * Create a map of property names to property values for a particular
     * destination. The desired property names are given by specificProperties.
     * (This is useful when we need to check the values of some specific properties
     * only - eg. the configuration tests)
     */
    protected Map<String, MetaValue> getSpecificComponentProperties(String componentName,
                                                                    ComponentType type) throws Exception {
       
        // @TODO: add messageCounterHistoryDayLimit to this list if this property
        // becomes configurable again
        String[] specificProperties = new String[] {"name", "JNDIName", "clustered",
                                                    "downCacheSize", "fullSize",
                                                    "maxDeliveryAttempts", "maxSize",
                                                    "pageSize",
                                                    "redeliveryDelay", "DLQ", "expiryQueue", "serverPeer"};
        return super.getSpecificComponentProperties(componentName, type, specificProperties);
    }
   
    /**
     * Create a topic session and return it.
     */
    protected TopicSession createTopicSession() throws Exception {
      InitialContext context = new InitialContext();
      Object tmp = context.lookup(TOPIC_FACTORY);
     
      TopicConnectionFactory tcf = (TopicConnectionFactory)tmp;
      topicConnection = tcf.createTopicConnection();
      topicConnection.setClientID(CLIENT_ID);
     
      TopicSession topicSession = topicConnection.createTopicSession(Boolean.FALSE,
                                                                     TopicSession.AUTO_ACKNOWLEDGE);
      topicConnection.start();
     
      return topicSession;
    }
   
    /**
     * Create a non-durable topic subscriber and return it.
     */
    protected TopicSubscriber createNonDurableTopicSubscriber(TopicSession session,
                                                              Topic topic) throws JMSException {
        return session.createSubscriber(topic);
    }
   
    /**
     * Create a durable topic subscriber and return it.
     */
    protected TopicSubscriber createDurableTopicSubscriber(TopicSession session,
                                                           Topic topic,
                                                           String subscriptionName) throws JMSException {
        return session.createDurableSubscriber(topic, subscriptionName);
    }
   
    /**
     * Create a TopicPublisher for the given topic and
     * publish the given number of messages.
     */
    protected void publishMessages(TopicSession session,
                                   Topic topic,
                                   int numMessages) throws JMSException {
        TopicPublisher publisher = session.createPublisher(topic);

        for(int i = 0; i < numMessages; i++) {
            TextMessage message = session.createTextMessage("Message " + i);
            publisher.publish(message);
        }

        publisher.close();
    }
   
    /**
     * Lookup and return the topic given by jndiName.
     */
    protected Topic getTopic(String jndiName) throws NamingException {
        InitialContext context = new InitialContext();
        Topic topic = (Topic)context.lookup(jndiName);

        return topic;
    }
   
    /**
     * Create a queue session and return it.
     */
    protected QueueSession createQueueSession() throws Exception {
      InitialContext context = new InitialContext();
      Object tmp = context.lookup(QUEUE_FACTORY);
     
      QueueConnectionFactory qcf = (QueueConnectionFactory)tmp;
      queueConnection = qcf.createQueueConnection();
     
      QueueSession queueSession = queueConnection.createQueueSession(Boolean.FALSE,
                                                                     QueueSession.AUTO_ACKNOWLEDGE);
      queueConnection.start();
     
      return queueSession;
    }
   
    /**
     * Create a QueueReceiver and return it.
     */
    protected QueueReceiver createQueueReceiver(QueueSession session,
                                                Queue queue) throws JMSException {
      return session.createReceiver(queue);
    }
   
    /**
     * Create a QueueSender for the given queue and send the
     * given number of messages.
     */
    protected void sendMessages(QueueSession session,
                                Queue queue,
                                int numMessages) throws JMSException {

        // Send durable messages
        sendMessagesAndSpecifyDeliveryMode(session, queue,
                                           numMessages, DeliveryMode.PERSISTENT);
    }
   
    /**
     * Create a QueueSender for the given queue and send the given
     * number of messages.
     *
     * @param deliveryMode is the delivery mode for the QueueSender
     */
    protected void sendMessagesAndSpecifyDeliveryMode(QueueSession session,
                                                      Queue queue,
                                                      int numMessages,
                                                      int deliveryMode) throws JMSException {
        QueueSender sender = session.createSender(queue);
        sender.setDeliveryMode(deliveryMode);

        for(int i = 0; i < numMessages; i++) {
            TextMessage message = session.createTextMessage("Message " + i);
            message.setIntProperty(EMBJOPR_TEST_PROPERTY, 0);
            sender.send(message);
        }

        sender.close();
    }
   
    /**
     * Create a queue session and then send the given number
     * of messages to the given queue.
     */
    protected void createQueueSessionAndSendMessages(int numMessages,
                                                     String jndiName) throws Exception {
        QueueSession session = createQueueSession();
        Queue queue = getQueue(jndiName);
        sendMessages(session, queue, numMessages);
    }
   
    /**
     * Create a QueueSender for the given queue and schedule the
     * given number of messages.
     */
    protected void sendScheduledMessages(QueueSession session,
                                         Queue queue,
                                         int numMessages) throws JMSException {
        QueueSender sender = session.createSender(queue);

        for(int i = 0; i < numMessages; i++) {

            // Schedule the message
            TextMessage message = session.createTextMessage("Message " + i);

            long now = System.currentTimeMillis();
            message.setLongProperty("JMS_JBOSS_SCHEDULED_DELIVERY", now + SCHEDULED_TIME);        

            sender.send(message);
        }

        sender.close();
    }
   
    /**
     * Lookup and return the queue given by jndiName.
     */
    protected Queue getQueue(String jndiName) throws NamingException {
      InitialContext context = new InitialContext();
      Queue queue = (Queue)context.lookup(jndiName);
     
      return queue;
    }
       
   
    /**
     * Close all connections.
     */
    protected void disconnect() throws JMSException {

        if(topicConnection != null) {
            topicConnection.close();
            topicConnection = null;
        }

        if(queueConnection != null) {
            queueConnection.close();
            queueConnection = null;
        }
    }
   
    /**
     * Get the list of metrics that appear on the summary
     * page for topics.
     */
    protected ArrayList<String> getTopicSummaryMetrics() {
      ArrayList<String> topicSummaryMetrics = new ArrayList<String>();
     
      // The metrics that appear on the "Summary" page
      topicSummaryMetrics.add(ALL_MSG_COUNT);
      topicSummaryMetrics.add(ALL_SUB_COUNT);
     
      return topicSummaryMetrics;
    }
   
    /**
     * Get the list of metrics that appear on the summary
     * page for queues.
     */
    protected ArrayList<String> getQueueSummaryMetrics() {
      ArrayList<String> queueSummaryMetrics = new ArrayList<String>();
     
      // The metrics that appear on the "Summary" page
      queueSummaryMetrics.add(CONSUMER_COUNT);
      queueSummaryMetrics.add(MSG_COUNT);
     
      return queueSummaryMetrics;
    }
   
    /**
     * Make sure we close any topic or queue
     * connections.
     */
    protected void tearDown() throws Exception
    {
      super.tearDown();
      disconnect();
    }
   
    /**
     * Create a topic session, a durable subscriber, and a non-durable
     * subscriber. Then, send the specified number of messages to the
     * given topic.
     */
    protected void createTopicSessionAndSendMessages(int numMessages,
                                                     String jndiName) throws Exception {
        TopicSession session = createTopicSession();
        Topic topic = getTopic(jndiName);
       
        // Create 1 durable subscriber, 1 non-durable subscriber
        // and then publish the specified number of messages
        createDurableTopicSubscriber(session, topic, jndiName + "Subscriber");
        createNonDurableTopicSubscriber(session, topic);
       
        publishMessages(session, topic, numMessages);
    }

    /**
     * Create a topic session and the given number of durable subscribers.
     */
    protected void createTopicSessionAndDurableSubscribers(int numSubscribers,
                                                           String jndiName) throws Exception {
        TopicSession session = createTopicSession();
        Topic topic = getTopic(jndiName);
       
        for(int i = 0; i < numSubscribers; i++) {
            createDurableTopicSubscriber(session, topic, jndiName + "Subscriber" + i);
        }
    }
   
    /**
     * Create a topic session and the given number of non-durable subscribers.
     */
    protected void createTopicSessionAndNonDurableSubscribers(int numSubscribers,
                                                              String jndiName) throws Exception {
        TopicSession session = createTopicSession();
        Topic topic = getTopic(jndiName);
       
        for(int i = 0; i < numSubscribers; i++) {
            createNonDurableTopicSubscriber(session, topic);
        }
    }
   
    /**
     * Create a topic session and the given number of durable and non-durable subscribers.
     */
    protected void createTopicSessionAndMultipleSubscribers(int numSubscribers,
                                                            String jndiName) throws Exception {
        TopicSession session = createTopicSession();
        Topic topic = getTopic(jndiName);
       
        for(int i = 0; i < numSubscribers; i++) {
            createNonDurableTopicSubscriber(session, topic);
            createDurableTopicSubscriber(session, topic, jndiName + "Subscriber" + i);
        }
    }
   
    /**
     * Create a topic session and specify the given username and password
     * when creating the TopicConnection.
     */
    protected TopicSession createTopicSessionAndSpecifyUser(String username,
                                                            String password) throws Exception {
        InitialContext context = new InitialContext();
        Object tmp = context.lookup(TOPIC_FACTORY);

        TopicConnectionFactory tcf = (TopicConnectionFactory)tmp;
        topicConnection = tcf.createTopicConnection(username, password);
        topicConnection.setClientID(CLIENT_ID);

        TopicSession topicSession = topicConnection.createTopicSession(Boolean.FALSE,
                                                                       TopicSession.AUTO_ACKNOWLEDGE);
        topicConnection.start();

        return topicSession;
    }
   
    /**
     * Create a topic session and a non-durable subscriber. Then, send the
     * specified number of messages to the given topic.
     */
    protected void createTopicSessionAndSendNonDurableMessages(int numMessages,
                                                               String jndiName) throws Exception {
        InitialContext context = new InitialContext();
        Object tmp = context.lookup(TOPIC_FACTORY);
       
        TopicConnectionFactory tcf = (TopicConnectionFactory)tmp;
        topicConnection = tcf.createTopicConnection();
        topicConnection.setClientID(CLIENT_ID);
       
        TopicSession session = topicConnection.createTopicSession(Boolean.FALSE,
                                                                  TopicSession.AUTO_ACKNOWLEDGE);
        Topic topic = getTopic(jndiName);
       
        createNonDurableTopicSubscriber(session, topic);
       
        publishMessages(session, topic, numMessages);
    }

    /**
     * Create a queue session and specify the given username and password
     * when creating the QueueConnection.
     */
    protected QueueSession createQueueSessionAndSpecifyUser(String username,
                                                            String password) throws Exception {
        InitialContext context = new InitialContext();
        Object tmp = context.lookup(QUEUE_FACTORY);

        QueueConnectionFactory qcf = (QueueConnectionFactory)tmp;
        queueConnection = qcf.createQueueConnection(username, password);

        QueueSession queueSession = queueConnection.createQueueSession(Boolean.FALSE,
                                                                       QueueSession.AUTO_ACKNOWLEDGE);
        queueConnection.start();

        return queueSession;
    }
}
TOP

Related Classes of org.jboss.jopr.jsfunit.as5.jmsDestinations.JMSTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.