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

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

import java.util.Iterator;
import java.util.List;

/**
 * @version $Revision$
 */
public class SpringTest extends TestCase {

    protected XmlBeanFactory factory;
    protected SpringConsumer consumer;
    protected SpringProducer producer;

    public void testSenderWithSpringXml() throws Exception {
        String config = "org/activemq/spring/spring.xml";
        assertSenderConfig(config);
    }

    public void testSenderWithSpringXmlAndQueue() throws Exception {
        String config = "org/activemq/spring/spring-queue.xml";
        assertSenderConfig(config);
    }

    public void testSenderWithSpringXmlUsingJNDI() throws Exception {
        String config = "org/activemq/spring/spring-jndi.xml";
        assertSenderConfig(config);
    }

    public void testSenderWithSpringXmlEmbeddedBrokerConfiguredViaXml() throws Exception {
        String config = "org/activemq/spring/spring-embedded.xml";
        assertSenderConfig(config);
    }

    protected void assertSenderConfig(String config) throws Exception {
        factory = new XmlBeanFactory(new ClassPathResource(config));

        consumer = (SpringConsumer) factory.getBean("consumer");
        assertTrue("Found a valid consumer", consumer != null);

        consumer.start();

        producer = (SpringProducer) factory.getBean("producer");
        assertTrue("Found a valid producer", producer != null);

        consumer.flushMessages();
        producer.start();

        // lets sleep a little to give the JMS time to dispatch stuff
        consumer.waitForMessagesToArrive(producer.getMessageCount());

        // now lets check that the consumer has received some messages
        List messages = consumer.flushMessages();
        System.out.println("Consumer has received messages....");
        for (Iterator iter = messages.iterator(); iter.hasNext();) {
            Object message = iter.next();
            System.out.println("Received: " + message);
        }

        assertEquals("Message count", producer.getMessageCount(), messages.size());
    }

    protected void tearDown() throws Exception {
        if (consumer != null) {
            consumer.stop();
        }
        if (producer != null) {
            producer.stop();
        }

        if (factory != null) {
            factory.destroySingletons();
        }
    }

}
