001package org.eclipse.aether.transport.http;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 * 
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 * 
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import static org.junit.Assert.*;
023
024import java.net.URI;
025import java.util.ArrayList;
026import java.util.Arrays;
027import java.util.List;
028
029import org.junit.Test;
030
031public class UriUtilsTest
032{
033
034    private String resolve( URI base, String ref )
035    {
036        return UriUtils.resolve( base, URI.create( ref ) ).toString();
037    }
038
039    @Test
040    public void testResolve_BaseEmptyPath()
041    {
042        URI base = URI.create( "http://host" );
043        assertEquals( "http://host/file.jar", resolve( base, "file.jar" ) );
044        assertEquals( "http://host/dir/file.jar", resolve( base, "dir/file.jar" ) );
045        assertEquals( "http://host?arg=val", resolve( base, "?arg=val" ) );
046        assertEquals( "http://host/file?arg=val", resolve( base, "file?arg=val" ) );
047        assertEquals( "http://host/dir/file?arg=val", resolve( base, "dir/file?arg=val" ) );
048    }
049
050    @Test
051    public void testResolve_BaseRootPath()
052    {
053        URI base = URI.create( "http://host/" );
054        assertEquals( "http://host/file.jar", resolve( base, "file.jar" ) );
055        assertEquals( "http://host/dir/file.jar", resolve( base, "dir/file.jar" ) );
056        assertEquals( "http://host/?arg=val", resolve( base, "?arg=val" ) );
057        assertEquals( "http://host/file?arg=val", resolve( base, "file?arg=val" ) );
058        assertEquals( "http://host/dir/file?arg=val", resolve( base, "dir/file?arg=val" ) );
059    }
060
061    @Test
062    public void testResolve_BasePathTrailingSlash()
063    {
064        URI base = URI.create( "http://host/sub/dir/" );
065        assertEquals( "http://host/sub/dir/file.jar", resolve( base, "file.jar" ) );
066        assertEquals( "http://host/sub/dir/dir/file.jar", resolve( base, "dir/file.jar" ) );
067        assertEquals( "http://host/sub/dir/?arg=val", resolve( base, "?arg=val" ) );
068        assertEquals( "http://host/sub/dir/file?arg=val", resolve( base, "file?arg=val" ) );
069        assertEquals( "http://host/sub/dir/dir/file?arg=val", resolve( base, "dir/file?arg=val" ) );
070    }
071
072    @Test
073    public void testResolve_BasePathNoTrailingSlash()
074    {
075        URI base = URI.create( "http://host/sub/d%20r" );
076        assertEquals( "http://host/sub/d%20r/file.jar", resolve( base, "file.jar" ) );
077        assertEquals( "http://host/sub/d%20r/dir/file.jar", resolve( base, "dir/file.jar" ) );
078        assertEquals( "http://host/sub/d%20r?arg=val", resolve( base, "?arg=val" ) );
079        assertEquals( "http://host/sub/d%20r/file?arg=val", resolve( base, "file?arg=val" ) );
080        assertEquals( "http://host/sub/d%20r/dir/file?arg=val", resolve( base, "dir/file?arg=val" ) );
081    }
082
083    private List<URI> getDirs( String base, String uri )
084    {
085        return UriUtils.getDirectories( ( base != null ) ? URI.create( base ) : null, URI.create( uri ) );
086    }
087
088    private void assertUris( List<URI> actual, String... expected )
089    {
090        List<String> uris = new ArrayList<>( actual.size() );
091        for ( URI uri : actual )
092        {
093            uris.add( uri.toString() );
094        }
095        assertEquals( Arrays.asList( expected ), uris );
096    }
097
098    @Test
099    public void testGetDirectories_NoBase()
100    {
101        List<URI> parents = getDirs( null, "http://host/repo/sub/dir/file.jar" );
102        assertUris( parents, "http://host/repo/sub/dir/", "http://host/repo/sub/", "http://host/repo/" );
103
104        parents = getDirs( null, "http://host/repo/sub/dir/?file.jar" );
105        assertUris( parents, "http://host/repo/sub/dir/", "http://host/repo/sub/", "http://host/repo/" );
106
107        parents = getDirs( null, "http://host/" );
108        assertUris( parents );
109    }
110
111    @Test
112    public void testGetDirectories_ExplicitBaseTrailingSlash()
113    {
114        List<URI> parents = getDirs( "http://host/repo/", "http://host/repo/sub/dir/file.jar" );
115        assertUris( parents, "http://host/repo/sub/dir/", "http://host/repo/sub/" );
116
117        parents = getDirs( "http://host/repo/", "http://host/repo/sub/dir/?file.jar" );
118        assertUris( parents, "http://host/repo/sub/dir/", "http://host/repo/sub/" );
119
120        parents = getDirs( "http://host/repo/", "http://host/" );
121        assertUris( parents );
122    }
123
124    @Test
125    public void testGetDirectories_ExplicitBaseNoTrailingSlash()
126    {
127        List<URI> parents = getDirs( "http://host/repo", "http://host/repo/sub/dir/file.jar" );
128        assertUris( parents, "http://host/repo/sub/dir/", "http://host/repo/sub/" );
129
130        parents = getDirs( "http://host/repo", "http://host/repo/sub/dir/?file.jar" );
131        assertUris( parents, "http://host/repo/sub/dir/", "http://host/repo/sub/" );
132
133        parents = getDirs( "http://host/repo", "http://host/" );
134        assertUris( parents );
135    }
136
137}