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

import junit.framework.TestCase;

import java.net.URI;
import java.util.Map;

/**
 * @version $Revision: 1.1.1.1 $
 */
public class URIHelperTest extends TestCase {
    
    public void testQuery() throws Exception {
        URI uri = new URI("vm://localhost?syncSend=true&foo=bar");

        String query = uri.getQuery();

        assertEquals("query", "syncSend=true&foo=bar", query);

        Map map = URIHelper.parseQuery(uri);
        assertEquals("map size: " + map, 2, map.size());
        assertEquals("map['syncSend'] for " + map, "true", map.get("syncSend"));
        assertEquals("map['foo'] for " + map, "bar", map.get("foo"));
        
        
        uri = new URI("peer://development?syncSend=true&foo=bar");
        query = uri.getQuery();

        assertEquals("query", "syncSend=true&foo=bar", query);
        
        uri = new URI("peer://(tcp://localhost:0?syncSend=false&foo=bar),tcp://localhost:223,?syncSend=true&foo=bar");
        query = URIHelper.getLastQuery(uri);

        assertEquals("query", "syncSend=true&foo=bar", query);
    }
    
    public void testNext() throws Exception {
        String addr1 = "tcp://localhost:6070";
        String addr2 = "tcp://localhost:6470";
        
        URIHelper helper = new URIHelper("(" + addr1 + "," + addr2 + ")");
        assertTrue(helper.hasNext());
        assertTrue(helper.getNext().equals(addr1));
        assertTrue(helper.hasNext());
        assertTrue(helper.getNext().equals(addr2));
        assertTrue(helper.hasNext() == false);
        
    }
    
    
    
    public void testLastPath() throws Exception {
        String path = "testPath";
        String addr = "peer://tcp://localhost5067,tcp://localhost:67707/" + path + "?blob=rtt";
        URI uri = new URI(addr);
        
        assertTrue(URIHelper.getLastPath(uri).equals(path));
     
        addr = "peer://tcp://localhost5067,tcp://localhost:67707/" + path;
        uri = new URI(addr);
        assertTrue(URIHelper.getLastPath(uri).equals(path));
        
        addr = "peer://tcp://localhost5067,tcp://localhost:67707";
        uri = new URI(addr);
        assertTrue(URIHelper.getLastPath(uri)==null);
        
        addr = "peer:///" + path;
        uri = new URI(addr);
        assertTrue(URIHelper.getLastPath(uri).equals(path));
        
        addr = "peer:///";
        uri = new URI(addr);
        assertTrue(URIHelper.getLastPath(uri)==null);
        
        addr = "vm://localhost";
        uri = new URI(addr);
        assertNull(URIHelper.getLastPath(uri));

        addr = "tcp://localhost:50607:/" + path;
        uri = new URI(addr);
        assertTrue(URIHelper.getLastPath(uri).equals(path));
        
        addr = "reliable:tcp://localhost:61617/" + path;
        uri = new URI(addr);
        assertTrue(URIHelper.getLastPath(uri).equals(path));
    }
}
