/** 
 * 
 * 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.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.activemq.ActiveMQConnection;
import org.activemq.ActiveMQConnectionFactory;
import org.activemq.broker.BrokerContainer;
import org.activemq.broker.impl.BrokerContainerImpl;
import org.activemq.message.ActiveMQQueue;
import org.activemq.message.ActiveMQTextMessage;
import org.activemq.message.ActiveMQTopic;
import org.activemq.util.IdGenerator;
import EDU.oswego.cs.dl.util.concurrent.SynchronizedInt;

/**
 * @version $Revision: 1.1.1.1 $
 */
public class RemoteTransportTest extends TestCase{
    protected BrokerContainer remoteBroker;
    protected Connection producerConnection;
    protected Connection consumerConnection;
    protected MessageConsumer consumer;
    protected MessageProducer producer;
    protected Session producerSession;
    
    
    protected void setUp() throws Exception{
        String URL = "reliable://" + ActiveMQConnection.DEFAULT_BROKER_URL;
       

        remoteBroker = new BrokerContainerImpl("remoteBroker");
        remoteBroker.addConnector(ActiveMQConnection.DEFAULT_BROKER_URL);
        remoteBroker.start();
        
        ActiveMQConnectionFactory fac = new ActiveMQConnectionFactory("remote://" + URL + "?brokerName=receiver");
        consumerConnection = fac.createConnection();
        consumerConnection.setClientID(new IdGenerator().generateId());
        consumerConnection.start();
        
        fac = new ActiveMQConnectionFactory("remote://" + URL + "?brokerName=sender");
        producerConnection = fac.createConnection();
        producerConnection.start();
        
        
        
    }
    
    private void doDurableTestSetup() throws Exception{
        Session s = consumerConnection.createSession(false,Session.AUTO_ACKNOWLEDGE);
        Topic destination = s.createTopic(getClass().getName());
        consumer = s.createDurableSubscriber(destination, destination.toString());
       
        producerSession = producerConnection.createSession(false,Session.AUTO_ACKNOWLEDGE);
        producer = producerSession.createProducer(destination);
    }
    
    private void doQueueTestSetup() throws Exception{
        Session s = consumerConnection.createSession(false,Session.AUTO_ACKNOWLEDGE);
        Queue destination = s.createQueue(getClass().getName());
        consumer = s.createConsumer(destination);
       
        producerSession = producerConnection.createSession(false,Session.AUTO_ACKNOWLEDGE);
        producer = producerSession.createProducer(destination);
    }
    
    protected void tearDown() throws Exception{
        remoteBroker.stop();
        consumerConnection.close();
        producerConnection.close();
    }
    
    
    
    public void doTest() 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 = producerSession.createTextMessage("test:"+i);
            producer.send(msg);
            list.add(msg);
        }
        
        for (int i =0; i < COUNT; i++){
            Message msg = consumer.receive(1000);
            assertNotNull("recieve: " + i + "NO message received!",msg);
            Message sentMsg = (Message)list.get(i);
            assertTrue("count " + i + " unexpected message: " +msg, msg.getJMSMessageID().equals(sentMsg.getJMSMessageID()));
        }
    }
    
    public void XtestDurableSendReceive() throws Exception {
        doDurableTestSetup();
        doTest();
    }
    
    public void testQueueSendReceive() throws Exception {
        doQueueTestSetup();
        doTest();
    }
    
    
}