/**
 *
 * Copyright 2004 Hiram Chirino
 *
 * 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.itests;

import java.rmi.RemoteException;
import java.util.Hashtable;

import javax.ejb.CreateException;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import junit.framework.TestCase;

import org.codehaus.activemq.ActiveMQConnectionFactory;
import org.codehaus.activemq.util.IndentPrinter;

/**
 * @version $Revision: 1.1 $
 */
public class InboundResourceAdapterTest extends TestCase {

    protected ConnectionFactory connectionFactory;
    protected Connection connection;
    protected Session session;
    protected MessageConsumer consumer;
    protected MessageProducer producer;
    
    
	static final String TRANSFER_MDB_INPUT_QUEUE = "MDBTransferBeanInQueue";
	static final String TRANSFER_MDB_OUTPUT_QUEUE = "MDBTransferBeanOutQueue";	
	
    protected void setUp() throws Exception {
        super.setUp();
        JmsResourceProvider p = new JmsResourceProvider();
        p.setTopic(false);
        connectionFactory = p.createConnectionFactory();
        connection = p.createConnection(connectionFactory);
        System.out.println("Created connection: " + connection);
        session = p.createSession(connection);
        System.out.println("Created session: " + session);
        Destination destination = p.createDestination(session, TRANSFER_MDB_OUTPUT_QUEUE);
        System.out.println("Created destination: " + destination + " of type: " + destination.getClass());
        consumer = p.createConsumer(session, destination);
        System.out.println("Created consumer: " + consumer);

        destination = p.createDestination(session, TRANSFER_MDB_INPUT_QUEUE);
        System.out.println("Created destination: " + destination + " of type: " + destination.getClass());
        producer = p.createProducer(session, destination);
        System.out.println("Created producer: " + producer);
        connection.start();
    }

    protected void tearDown() throws Exception {
        System.out.println("Test Done.  Stats");
        ((ActiveMQConnectionFactory) connectionFactory).getFactoryStats().dump(new IndentPrinter());
        System.out.println("Closing down connection");

        connection.stop();
        connection.close();
        System.out.println("Connection closed.");
    }
	
    public void testTransferBean() throws JMSException, NamingException, RemoteException, CreateException {
    	
        String msg1 = "Hello World";
        producer.send(session.createTextMessage(msg1));
    	
    	System.out.println("Receiving message.");
    	Message inMsg = consumer.receive(1000*10);
    	
    	assertNotNull("Message is null" , inMsg);
    	assertTrue("Message should be a TextMessge" , inMsg instanceof TextMessage );
    	assertEquals(((TextMessage)inMsg).getText(), msg1);
    	
    }

    public void testLoadTransferBean() throws JMSException, NamingException, RemoteException, CreateException {
    	
    	int count = 5;
        String msg1 = "Hello World";
        for( int i=0 ; i < count; i++ ) {
        	producer.send(session.createTextMessage(msg1));
        }
        for( int i=0 ; i < count; i++ ) {
        	System.out.println("Receiving message.");
	    	Message inMsg = consumer.receive(1000*10);
	    	
	    	assertNotNull("Message is null" , inMsg);
	    	assertTrue("Message should be a TextMessge" , inMsg instanceof TextMessage );
	    	assertEquals(((TextMessage)inMsg).getText(), msg1);
        }    	
    }

    /**
     * @throws NamingException
     */
    private InitialContext createInitialContext() throws NamingException {
        Hashtable props = new Hashtable();
        props.put("java.naming.factory.initial", "org.openejb.client.RemoteInitialContextFactory");
        props.put("java.naming.provider.url", "127.0.0.1:4201");
        props.put("java.naming.security.principal", "testuser");
        props.put("java.naming.security.credentials", "testpassword");
        return new InitialContext(props);
    }


}
