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

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.MessageConsumer;
import javax.jms.Session;

import junit.framework.TestCase;

import org.activemq.ActiveMQConnectionFactory;

import EDU.oswego.cs.dl.util.concurrent.Latch;
import EDU.oswego.cs.dl.util.concurrent.WaitableBoolean;

/**
 *
 * ProducerDemandAdvisorTest
 */
public class ProducerDemandAdvisorTest extends TestCase implements ProducerDemandListener {
    
    private Connection connection;
    private Session session;
    private Destination destination;
    private String destinationName = "foo.bar";
    private WaitableBoolean inDemand = new WaitableBoolean(false);
        
    protected void setUp() throws Exception{
        ActiveMQConnectionFactory fac = new ActiveMQConnectionFactory("vm://localhost");
        connection = fac.createConnection();
        session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
        destination = session.createTopic(destinationName);
        connection.start();
    }
    
    protected void tearDown() throws  Exception{
        connection.close();
    }
    
    public void testAdvisories() throws Exception{
        ProducerDemandAdvisor advisor = new ProducerDemandAdvisor(connection,destination);
        advisor.setDemandListener(this);
        advisor.start();

        assertDemandChange(false, 2000);

        MessageConsumer consumer = session.createConsumer(destination);
        assertDemandChange(true, 2000);
        session.close();
        assertDemandChange(false, 5000);
    }
    
    public void testAdvisoriesWhenConsumerAllreadyExists() throws Exception{
        MessageConsumer consumer = session.createConsumer(destination);

        ProducerDemandAdvisor advisor = new ProducerDemandAdvisor(connection,destination);
        advisor.setDemandListener(this);
        advisor.start();

        assertDemandChange(true, 2000);
        session.close();
        assertDemandChange(false, 2000);
    }

    public void assertDemandChange(final boolean value, long timeout) {
        final Latch change = new Latch();
        try {
            new Thread() {
                public void run() {
                    try {
                        inDemand.whenEqual(value, new Runnable() {
                            public void run() {
                                change.release();
                            }
                        });
                    } catch (InterruptedException e) {
                    }                    
                }
            }.start();
            assertTrue("Demand did not change to: "+value, change.attempt(timeout));
        } catch (InterruptedException e) {
            fail("Interuppted: "+e);
        }
    }
    
    public void onEvent(ProducerDemandEvent event) {
        inDemand.set(event.isInDemand());
    }
    
}
