/** 
 * 
 * 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 org.codehaus.activemq.broker.Broker;
import org.codehaus.activemq.broker.BrokerClient;
import org.codehaus.activemq.broker.BrokerClientStub;
import org.codehaus.activemq.message.ActiveMQMessage;
import org.codehaus.activemq.message.ConsumerInfo;

import javax.jms.JMSException;

/**
 * @version $Revision: 1.1 $
 */
public class TopicBrokerTest extends BrokerTestSupport {

    protected BrokerClient client;
    protected Object semaphore = new Object();

    public void testSendingMessagesToVariousDestinations() throws Exception {

        BrokerClientStub queueA = new BrokerClientStub(semaphore);
        BrokerClientStub queueB = new BrokerClientStub(semaphore);
        BrokerClientStub queueC = new BrokerClientStub(semaphore);
        BrokerClientStub queueD = new BrokerClientStub(semaphore);

        addSubscription("FOO.BAR", null, queueA, isTopic());
        addSubscription("FOO.*", null, queueB, isTopic());
        addSubscription("BAR.*", "priority = 123", queueC, isTopic());
        addSubscription("BAR.*", "x = 'ABC'", queueD, isTopic());

        ActiveMQMessage message = dispatchMessage(broker, "FOO.BAR");
        queueA.waitForMessageToArrive();
        if (isTopic()) {
            assertEquals("queueA", true, queueA.flushMessages().contains(message));
            assertEquals("queueB", true, queueB.flushMessages().contains(message));
        }
        else {
            boolean atA = queueA.flushMessages().contains(message);
            boolean atB = queueB.flushMessages().contains(message);
            assertTrue("Sent to queueA or queueB. a: " + atA + " b: " + atB, (atA && !atB) || (!atA && atB));
        }
        assertEquals("queueC", false, queueC.flushMessages().contains(message));

        message = dispatchMessage(broker, "FOO.XYZ");
        queueB.waitForMessageToArrive();
        assertEquals("queueA", false, queueA.flushMessages().contains(message));
        assertEquals("queueC", false, queueC.flushMessages().contains(message));
        assertEquals("queueD", false, queueD.flushMessages().contains(message));
        assertEquals("queueB", true, queueB.flushMessages().contains(message));

        message = createMessage("BAR.XYZ");
        message.setIntProperty("priority", 123);
        broker.sendMessage(client, message);
        queueC.waitForMessageToArrive();

        assertEquals("queueA", false, queueA.flushMessages().contains(message));
        assertEquals("queueB", false, queueB.flushMessages().contains(message));
        assertEquals("queueC", true, queueC.flushMessages().contains(message));
        assertEquals("queueD", false, queueD.flushMessages().contains(message));

        message = createMessage("BAR.XYZ");
        message.setStringProperty("x", "ABC");
        broker.sendMessage(client, message);
        queueD.waitForMessageToArrive();

        assertEquals("queueA", false, queueA.flushMessages().contains(message));
        assertEquals("queueB", false, queueB.flushMessages().contains(message));
        assertEquals("queueC", false, queueC.flushMessages().contains(message));
        assertEquals("queueD", true, queueD.flushMessages().contains(message));
    }

    protected void addSubscription(String subject, String selector, BrokerClient client, boolean topic) throws JMSException {
        ConsumerInfo info = createConsumer(subject, selector);
        broker.addMessageConsumer(client, info);
    }

    protected ActiveMQMessage dispatchMessage(Broker broker, String subject) throws JMSException, InterruptedException {
        ActiveMQMessage message = createMessage(subject);
        broker.sendMessage(client, message);
        return message;
    }

}