/** 
 * 
 * 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 javax.jms.TemporaryQueue;
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 TempDestinationAdvisorTest extends TestCase{
    
    private ActiveMQConnectionFactory fac;
    private Connection connection;
    private SynchronizedBoolean started;
    
    
    protected void setUp() throws Exception{
        started = new SynchronizedBoolean(false);
        fac = new ActiveMQConnectionFactory("vm://localhost");
        connection = fac.createConnection();
        connection.start();
    }
    
    protected void tearDown() throws  Exception{
        connection.close();
    }
    public void testAdvisories() throws Exception{
        Connection testCon = fac.createConnection();
        testCon.start();
        Session s = testCon.createSession(false,Session.AUTO_ACKNOWLEDGE);
        TemporaryQueue dest = s.createTemporaryQueue();
        TempDestinationAdvisor test = new TempDestinationAdvisor(connection,dest);
        test.start();
        assertTrue("Destination should be active",test.isActive(dest));
        dest.delete();
        
        assertFalse("Destination should no longer be active",test.isActive(dest));
        test.stop();
        
        testCon = fac.createConnection();
        testCon.start();
        s = testCon.createSession(false,Session.AUTO_ACKNOWLEDGE);
        dest = s.createTemporaryQueue();
        
        test = new TempDestinationAdvisor(connection,dest);
        test.start();
        
        assertTrue(test.isActive(dest));
        testCon.close();
        
        assertFalse(test.isActive(dest));
        test.stop();
    }

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