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

import javax.jms.JMSException;

import org.codehaus.activemq.ActiveMQConnection;
import org.codehaus.activemq.broker.impl.BrokerContainerImpl;
import org.codehaus.activemq.message.ActiveMQQueue;
import org.codehaus.activemq.service.MessageContainerAdmin;
import org.codehaus.activemq.test.TestSupport;

/**
 * @version $Revision: 1.1 $
 */
public class BrokerAdminTest extends TestSupport {
    
    private static final String TEST_QUEUE_NAME = "TestQueue";
    private static final String TEST_TOPIC_NAME = "TestTopic";
    
    public static final String URL = ActiveMQConnection.DEFAULT_URL;
    protected BrokerContainer container;

    protected void setUp() throws Exception {
        container = new BrokerContainerImpl(URL);
        container.start();
        super.setUp();
    }

    protected void tearDown() throws Exception {
        container.stop();
    }
    
    public void testInitialState() throws JMSException {
        
        // The broker should start with zero destinations.
        BrokerAdmin brokerAdmin = getBrokerAdmin();
        MessageContainerAdmin[] admins = brokerAdmin.listMessageContainerAdmin();
        assertEquals(admins.length, 0);
        
    }

    public void testCreateQueue() throws JMSException {
        
        // The broker should start with zero destinations.
        BrokerAdmin brokerAdmin = getBrokerAdmin();
        brokerAdmin.createMessageContainer(new ActiveMQQueue(TEST_QUEUE_NAME)  );
        
        MessageContainerAdmin[] admins = brokerAdmin.listMessageContainerAdmin();
        assertEquals(admins.length, 1);
        
        assertEquals(admins[0].getDestinationName(), TEST_QUEUE_NAME);        
    }
    
    public void testDeleteQueue() throws JMSException {
        
        // The broker should start with zero destinations.
        BrokerAdmin brokerAdmin = getBrokerAdmin();
        brokerAdmin.createMessageContainer(new ActiveMQQueue(TEST_QUEUE_NAME)  );
        brokerAdmin.destoryMessageContainer(new ActiveMQQueue(TEST_QUEUE_NAME)  );
        
        MessageContainerAdmin[] admins = brokerAdmin.listMessageContainerAdmin();
        assertEquals(admins.length, 0);
    }
    

    /**
     * @return
     * 
     */
    private BrokerAdmin getBrokerAdmin() {
        return container.getBroker().getBrokerAdmin();
    }
}
