/** 
 * 
 * Copyright 2004 Protique Ltd
 * 
 * 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.codehaus.activemq.transport.remote;
import java.util.ArrayList;
import javax.jms.*;
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 junit.framework.TestCase;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.activemq.ActiveMQConnection;
import org.codehaus.activemq.ActiveMQConnectionFactory;
import org.codehaus.activemq.broker.BrokerContainer;
import org.codehaus.activemq.broker.impl.BrokerContainerImpl;
import org.codehaus.activemq.message.ActiveMQQueue;
import org.codehaus.activemq.message.ActiveMQTextMessage;
import org.codehaus.activemq.message.ActiveMQTopic;
import org.codehaus.activemq.util.IdGenerator;
import EDU.oswego.cs.dl.util.concurrent.SynchronizedInt;

/**
 * @version $Revision: 1.1 $
 */
public class RemoteTransportTest extends TestCase{
    protected BrokerContainer remoteBroker;
    protected Connection sender;
    protected Connection receiver;
    protected MessageConsumer consumer;
    protected MessageProducer producer;
    protected Session senderSession;
    
    
    protected void setUp() throws Exception{
        String URL = "reliable://" + ActiveMQConnection.DEFAULT_URL;
       

        remoteBroker = new BrokerContainerImpl("remoteBroker");
        remoteBroker.addConnector(ActiveMQConnection.DEFAULT_URL);
        remoteBroker.start();
        
        ActiveMQConnectionFactory fac = new ActiveMQConnectionFactory("remote://" + URL + "?brokerName=receiver");
        receiver = fac.createConnection();
        receiver.setClientID(new IdGenerator().generateId());
        receiver.start();
        Session s = receiver.createSession(false,Session.AUTO_ACKNOWLEDGE);
        Topic destination = s.createTopic(getClass().getName());
        consumer = s.createDurableSubscriber(destination, destination.toString());
        fac = new ActiveMQConnectionFactory("remote://" + URL + "?brokerName=sender");
        sender = fac.createConnection();
        sender.start();
        senderSession = sender.createSession(false,Session.AUTO_ACKNOWLEDGE);
        producer = senderSession.createProducer(destination);
        
    }
    
    protected void tearDown() throws Exception{
        remoteBroker.stop();
        receiver.close();
        sender.close();
    }
    
    public void testSendReceive() throws Exception {
        Thread.sleep(3000);//make sure everything is connected
        int COUNT = 100;
        ArrayList list = new ArrayList(COUNT);
        for (int i =0; i < COUNT; i++){
            Message msg = senderSession.createTextMessage("test:"+i);
            producer.send(msg);
            list.add(msg);
        }
        
        for (int i =0; i < COUNT; i++){
            Message msg = consumer.receive(1000);
            assertNotNull("NO message received!",msg);
            Message sentMsg = (Message)list.get(i);
            assertTrue("Unexpected message: " +msg, msg.getJMSMessageID().equals(sentMsg.getJMSMessageID()));
        }
    }
    
    
}