/** 
 * 
 * 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.Session;
import junit.framework.TestCase;
import org.activemq.ActiveMQConnectionFactory;
import org.activemq.message.ConnectionInfo;
import EDU.oswego.cs.dl.util.concurrent.SynchronizedBoolean;
/**
 * A helper class for listening for MessageConnection advisories
 */



/**
 *
 * ConnectionAdvisorTest
 */
public class ConnectionAdvisorTest extends TestCase implements ConnectionAdvisoryEventListener{
    
    private ActiveMQConnectionFactory fac;
    private Connection connection;
    private Session session;
    private SynchronizedBoolean started;
    
    
    protected void setUp() throws Exception{
        started = new SynchronizedBoolean(false);
        fac = new ActiveMQConnectionFactory("vm://localhost");
        connection = fac.createConnection();
        session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
        connection.start();
    }
    
    protected void tearDown() throws  Exception{
        connection.close();
    }
    public void testAdvisories() throws Exception{
        ConnectionAdvisor test = new ConnectionAdvisor(connection);
        test.addListener(this);
        test.start();
        Connection testConnection = fac.createConnection();
        testConnection.start();
        synchronized(started){
            if (!started.get()){
                started.wait(5000);
            }
        }
        assertTrue(started.get());
        
    }

    public void onEvent(ConnectionAdvisoryEvent event) {
        System.out.println(event);
        ConnectionInfo info = event.getInfo();
        started.set(info.isStarted());
        synchronized(started){
            started.notify();
        }
        
    }
    
}
