/** 
 * 
 * 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 java.util.Iterator;
import java.util.List;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.activemq.ActiveMQConnectionFactory;
import org.activemq.message.ActiveMQDestination;
import org.activemq.message.ProducerInfo;
import EDU.oswego.cs.dl.util.concurrent.CopyOnWriteArrayList;
import EDU.oswego.cs.dl.util.concurrent.SynchronizedBoolean;
import junit.framework.TestCase;
/**
 * A helper class for listening for MessageProducer advisories
 */



/**
 *
 * ProducerAdvisorTest
 */
public class ProducerAdvisorTest extends TestCase implements ProducerAdvisoryEventListener{
    
    private Connection connection;
    private Session session;
    private Destination destination;
    private String destinationName = "foo.bar";
    private SynchronizedBoolean started;
    
    
    protected void setUp() throws Exception{
        started = new SynchronizedBoolean(false);
        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{
        ProducerAdvisor test = new ProducerAdvisor(connection,destination);
        test.addListener(this);
        test.start();
        MessageProducer producer = session.createProducer(destination);
        synchronized(started){
            if (!started.get()){
                started.wait(2000);
            }
        }
        assertTrue(started.get());
        session.close();
        synchronized(started){
            if (started.get()){
                started.wait(2000);
            }
        }
        assertFalse(started.get());
    }

    public void onEvent(ProducerAdvisoryEvent event) {
        ProducerInfo info = event.getInfo();
        started.set(info.isStarted());
        synchronized(started){
            started.notify();
        }
        
    }
    
}
