/** 
 * 
 * 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.jca;

import junit.framework.TestCase;
import org.codehaus.activemq.ActiveMQConnectionFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

import javax.jms.Connection;
import javax.jms.MessageProducer;
import javax.jms.Session;

/**
 * @version $Revision: 1.2 $
 */
public class JCAContainerTestSupport extends TestCase {
    protected XmlBeanFactory beanFactory;
    protected JCAContainer container;

    // to send some messages
    protected Connection connection;
    protected Session session;
    protected MessageProducer producer;

    protected void setUp() throws Exception {
        beanFactory = createBeanFactory();

        container = (JCAContainer) beanFactory.getBean("activeMQContainer");
        assertTrue("Should have created a JCAContainer", container != null);

        // now lets create the JMS connection
        connection = createConnection();
        
        // start the connection in case we do some consumption on it
        connection.start();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        producer = session.createProducer(null);
    }


    protected void tearDown() throws Exception {
        beanFactory.destroySingletons();
        if (connection != null) {
            connection.close();
        }
    }

    protected String getBrokerURL() {
        return "vm://localhost";
    }

    protected XmlBeanFactory createBeanFactory() {
        return new XmlBeanFactory(new ClassPathResource("spring.xml", getClass()));
    }

    protected Connection createConnection() throws Exception {
        return new ActiveMQConnectionFactory(getBrokerURL()).createConnection();
    }
}
