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

import junit.framework.TestCase;
import org.activemq.io.WireFormat;
import org.activemq.io.impl.DefaultWireFormat;
import org.activemq.transport.composite.CompositeTransportChannel;
import org.activemq.transport.vm.VmTransportChannel;

import javax.jms.JMSException;
import java.net.URI;

/**
 * @version $Revision: 1.1.1.1 $
 */
public class UriParseTest extends TestCase {
    protected CompositeTransportChannel channel;
    protected WireFormat wireFormat = new DefaultWireFormat();

    public void testSingleURI() throws Exception {
        assertVmSendCapacity("list:vm://foo?sendCapacity=999,vm://bar?sendCapacity=999", 999);

    }

    public void testMultipleURIs() throws Exception {
        assertVmSendCapacity("list:vm://foo?sendCapacity=999", 999);
    }

    public void testPropertiesOnCompositeChannel() throws Exception {
        assertVmSendCapacity("list:(vm://foo?sendCapacity=999)?maximumRetries=123", 999);

        assertEquals("Composite channel maximumRetries", 123, channel.getMaximumRetries());
    }

    public void testPropertiesOnReliableChannel() throws Exception {
        assertVmSendCapacity("reliable:(vm://foo?sendCapacity=999)?maximumRetries=123", 999);

        assertEquals("Composite channel maximumRetries", 123, channel.getMaximumRetries());
    }

    protected void assertVmSendCapacity(String uriText, int sendCapacity) throws JMSException, InterruptedException {
        URI uri = URI.create(uriText);
        channel = (CompositeTransportChannel) TransportChannelProvider.create(wireFormat, uri);
        channel.start();
        
        // Sleep up to 10 secs until channel gets created.
        for( int i=0; i < 100; i++ ) {
            try {
                channel.getChannel();
                break;
            } catch ( JMSException e ) {
                Thread.sleep(100);
            }
        }
        
        VmTransportChannel subChannel = (VmTransportChannel) channel.getChannel();
        assertNotNull("VM channel exists", subChannel);

        assertEquals("sendCapacity for uri: " + uriText, sendCapacity, subChannel.getSendCapacity());
    }

    protected void tearDown() throws Exception {
        if (channel != null) {
            channel.stop();
        }
    }
}
