001    package org.apache.maven.model.path;
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    
022    import junit.framework.TestCase;
023    
024    /**
025     * @author Benjamin Bentmann
026     */
027    public class DefaultUrlNormalizerTest
028        extends TestCase
029    {
030    
031        private UrlNormalizer normalizer;
032    
033        @Override
034        protected void setUp()
035            throws Exception
036        {
037            super.setUp();
038    
039            normalizer = new DefaultUrlNormalizer();
040        }
041    
042        @Override
043        protected void tearDown()
044            throws Exception
045        {
046            normalizer = null;
047    
048            super.tearDown();
049        }
050    
051        private String normalize( String url )
052        {
053            return normalizer.normalize( url );
054        }
055    
056        public void testNullSafe()
057        {
058            assertNull( normalize( null ) );
059        }
060    
061        public void testTrailingSlash()
062        {
063            assertEquals( "", normalize( "" ) );
064            assertEquals( "http://server.org/dir", normalize( "http://server.org/dir" ) );
065            assertEquals( "http://server.org/dir/", normalize( "http://server.org/dir/" ) );
066        }
067    
068        public void testRemovalOfParentRefs()
069        {
070            assertEquals( "http://server.org/child", normalize( "http://server.org/parent/../child" ) );
071            assertEquals( "http://server.org/child", normalize( "http://server.org/grand/parent/../../child" ) );
072    
073            assertEquals( "http://server.org//child", normalize( "http://server.org/parent/..//child" ) );
074            assertEquals( "http://server.org/child", normalize( "http://server.org/parent//../child" ) );
075        }
076    
077        public void testPreservationOfDoubleSlashes()
078        {
079            assertEquals( "scm:hg:ssh://localhost//home/user", normalize( "scm:hg:ssh://localhost//home/user" ) );
080            assertEquals( "file:////UNC/server", normalize( "file:////UNC/server" ) );
081            assertEquals( "[fetch=]http://server.org/[push=]ssh://server.org/",
082                          normalize( "[fetch=]http://server.org/[push=]ssh://server.org/" ) );
083        }
084    
085    }