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

import junit.framework.TestCase;
import org.codehaus.activemq.broker.Broker;
import org.codehaus.activemq.broker.impl.DefaultBroker;
import org.codehaus.activemq.filter.Filter;
import org.codehaus.activemq.filter.FilterFactory;
import org.codehaus.activemq.filter.FilterFactoryImpl;
import org.codehaus.activemq.message.ActiveMQDestination;
import org.codehaus.activemq.message.ActiveMQMessage;
import org.codehaus.activemq.message.ActiveMQQueue;
import org.codehaus.activemq.message.ActiveMQTopic;
import org.codehaus.activemq.message.ConsumerInfo;
import org.codehaus.activemq.util.IdGenerator;

import javax.jms.JMSException;

/**
 * Useful base class for Broker related test cases
 *
 * @version $Revision: 1.1 $
 */
public abstract class BrokerTestSupport extends TestCase {

    private static final String BROKER_NAME = "TEST_BROKER";

    protected Broker broker;
    protected FilterFactory filterFactory = new FilterFactoryImpl();
    protected IdGenerator idGenerator = new IdGenerator();

    protected boolean isTopic() {
        return true;
    }

    protected ActiveMQMessage createMessage(String subject) {
        ActiveMQMessage message = new ActiveMQMessage();
        message.setJMSMessageID(idGenerator.generateId());
        message.setJMSDestination(createDestination(subject));
        return message;
    }

    protected Filter createFilter(String subject, String selector) throws JMSException {
        return filterFactory.createFilter(createDestination(subject), selector);
    }

    protected ActiveMQDestination createDestination(String subject) {
        if (isTopic()) {
            return new ActiveMQTopic(subject);
        }
        else {
            return new ActiveMQQueue(subject);
        }
    }

    protected Broker createStore() {
        return new DefaultBroker(BROKER_NAME);
    }

    protected ConsumerInfo createConsumer(String subject) {
        return createConsumer(subject, null);
    }

    protected ConsumerInfo createConsumer(String subject, String selector) {
        ConsumerInfo info = new ConsumerInfo();
        info.setConsumerId(idGenerator.generateId());
        info.setDestination(createDestination(subject));
        info.setSelector(selector);
        return info;
    }

    protected void setUp() throws Exception {
        broker = new DefaultBroker(BROKER_NAME);
        broker.start();
    }

    protected void tearDown() throws Exception {
        broker.stop();
    }
}
