<commit_before>/**
 *
 * Copyright 2005-2006 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.activemq.test;

import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * @version $Revision: 1.2 $
 */
public class JmsSendReceiveTestSupport extends TestSupport implements MessageListener {
    private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
            .getLog(JmsSendReceiveTestSupport.class);
    
    protected int messageCount = 100;
    protected String[] data;
    protected Session session;
    protected Session consumeSession;
    protected MessageConsumer consumer;
    protected MessageProducer producer;
    protected Destination consumerDestination;
    protected Destination producerDestination;
    protected List messages = createConcurrentList();
    protected boolean topic = true;
    protected boolean durable = false;
    protected int deliveryMode = DeliveryMode.PERSISTENT;
    protected final Object lock = new Object();
    protected boolean verbose = false;
    protected boolean useSeparateSession = false;
    protected boolean largeMessages = false;
    protected int largeMessageLoopSize = 4 * 1024;

    /*
     * @see junit.framework.TestCase#setUp()
     */
    protected void setUp() throws Exception {
        super.setUp();
        String temp = System.getProperty("messageCount");
        
        if (temp != null) {
            int i = Integer.parseInt(temp);
            if (i > 0) {
                messageCount = i;
            }
        }
        
        log.info("Message count for test case is: " + messageCount);
        data = new String[messageCount];
        
        for (int i = 0; i < messageCount; i++) {
            data[i] = createMessageText(i);
        }
    }


    protected String createMessageText(int i) {
        if (largeMessages) {
            return createMessageBodyText();
        }
        else {
            return "Text for message: " + i + " at " + new Date();
        }
    }

    protected String createMessageBodyText() {
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < largeMessageLoopSize; i++) {
            buffer.append("0123456789");
        }
        return buffer.toString();
    }

    /**
     * Test if all the messages sent are being received.  
     * 
     * @throws Exception
     */
    public void testSendReceive() throws Exception {
        messages.clear();
        
        for (int i = 0; i < data.length; i++) {
            Message message = session.createTextMessage(data[i]);
            Thread.sleep(200);
            if (verbose) {
                log.info("About to send a message: " + message + " with text: " + data[i]);
            }
            
            producer.send(producerDestination, message);
        }
        
        assertMessagesAreReceived();
        log.info("" + data.length + " messages(s) received, closing down connections");
    }

    /**
     * Waits to receive the messages and performs the test if all messages have been received and 
     * are in sequential order.  
     * 
     * @throws JMSException
     */
    protected void assertMessagesAreReceived() throws JMSException {
        waitForMessagesToBeDelivered();
        assertMessagesReceivedAreValid(messages);
    }

    /**<commit_msg>Doing a sleep inside the send loop makes TwoBrokerTopicSendReceiveLotsOfMessagesUsingTcpTest take forever!<commit_after>/**
 *
 * Copyright 2005-2006 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.activemq.test;

import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * @version $Revision: 1.2 $
 */
public class JmsSendReceiveTestSupport extends TestSupport implements MessageListener {
    private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
            .getLog(JmsSendReceiveTestSupport.class);
    
    protected int messageCount = 100;
    protected String[] data;
    protected Session session;
    protected Session consumeSession;
    protected MessageConsumer consumer;
    protected MessageProducer producer;
    protected Destination consumerDestination;
    protected Destination producerDestination;
    protected List messages = createConcurrentList();
    protected boolean topic = true;
    protected boolean durable = false;
    protected int deliveryMode = DeliveryMode.PERSISTENT;
    protected final Object lock = new Object();
    protected boolean verbose = false;
    protected boolean useSeparateSession = false;
    protected boolean largeMessages = false;
    protected int largeMessageLoopSize = 4 * 1024;

    /*
     * @see junit.framework.TestCase#setUp()
     */
    protected void setUp() throws Exception {
        super.setUp();
        String temp = System.getProperty("messageCount");
        
        if (temp != null) {
            int i = Integer.parseInt(temp);
            if (i > 0) {
                messageCount = i;
            }
        }
        
        log.info("Message count for test case is: " + messageCount);
        data = new String[messageCount];
        
        for (int i = 0; i < messageCount; i++) {
            data[i] = createMessageText(i);
        }
    }


    protected String createMessageText(int i) {
        if (largeMessages) {
            return createMessageBodyText();
        }
        else {
            return "Text for message: " + i + " at " + new Date();
        }
    }

    protected String createMessageBodyText() {
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < largeMessageLoopSize; i++) {
            buffer.append("0123456789");
        }
        return buffer.toString();
    }

    /**
     * Test if all the messages sent are being received.  
     * 
     * @throws Exception
     */
    public void testSendReceive() throws Exception {
        messages.clear();
        
        Thread.sleep(1000);
        for (int i = 0; i < data.length; i++) {
            Message message = session.createTextMessage(data[i]);
            if (verbose) {
                log.info("About to send a message: " + message + " with text: " + data[i]);
            }
            
            producer.send(producerDestination, message);
        }
        
        assertMessagesAreReceived();
        log.info("" + data.length + " messages(s) received, closing down connections");
    }

    /**
     * Waits to receive the messages and performs the test if all messages have been received and 
     * are in sequential order.  
     * 
     * @throws JMSException
     */
    protected void assertMessagesAreReceived() throws JMSException {
        waitForMessagesToBeDelivered();
        assertMessagesReceivedAreValid(messages);
    }

    /**
{'fraction_non_alphanumeric': 0.09003028185418123, 'fraction_numerical': 0.009201024924295365, 'mean_word_length': 2.5883827831174258, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2773517', 'n_tokens_mistral': 2286, 'n_tokens_neox': 2197, 'n_words': 712}