package org.codehaus.plexus.util;

/* ====================================================================
 *   Copyright 2001-2004 The Apache Software Foundation.
 *
 *   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.
 * ====================================================================
 */

import junit.framework.TestCase;

/**
 * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
 * @version $Id$
 */
public class PathToolTest
    extends TestCase
{
    /**
     * @throws Exception
     */
    public void testGetRelativePath()
        throws Exception
    {
        assertEquals( PathTool.getRelativePath( null, null ), "" );
        assertEquals( PathTool.getRelativePath( null, "/usr/local/java/bin" ), "" );
        assertEquals( PathTool.getRelativePath( "/usr/local/", null ), "" );
        assertEquals( PathTool.getRelativePath( "/usr/local/", "/usr/local/java/bin" ), ".." );
        assertEquals( PathTool.getRelativePath( "/usr/local/", "/usr/local/java/bin/java.sh" ), "../.." );
        assertEquals( PathTool.getRelativePath( "/usr/local/java/bin/java.sh", "/usr/local/" ), "" );
    }

    /**
     * @throws Exception
     */
    public void testGetDirectoryComponent()
        throws Exception
    {
        assertEquals( PathTool.getDirectoryComponent( null ), "" );
        assertEquals( PathTool.getDirectoryComponent( "/usr/local/java/bin" ), "/usr/local/java" );
        assertEquals( PathTool.getDirectoryComponent( "/usr/local/java/bin/" ), "/usr/local/java/bin" );
        assertEquals( PathTool.getDirectoryComponent( "/usr/local/java/bin/java.sh" ), "/usr/local/java/bin" );
    }

    /**
     * @throws Exception
     */
    public void testCalculateLink()
        throws Exception
    {
        assertEquals( PathTool.calculateLink( "/index.html", "../.." ), "../../index.html" );
        assertEquals( PathTool.calculateLink( "http://plexus.codehaus.org/plexus-utils/index.html", "../.." ),
                      "http://plexus.codehaus.org/plexus-utils/index.html" );
        assertEquals( PathTool.calculateLink( "/usr/local/java/bin/java.sh", "../.." ),
                      "../../usr/local/java/bin/java.sh" );
        assertEquals( PathTool.calculateLink( "../index.html", "/usr/local/java/bin" ),
                      "/usr/local/java/bin/../index.html" );
        assertEquals( PathTool.calculateLink( "../index.html", "http://plexus.codehaus.org/plexus-utils" ),
                      "http://plexus.codehaus.org/plexus-utils/../index.html" );
    }

    /**
     * @throws Exception
     */
    public void testGetRelativeWebPath()
        throws Exception
    {
        assertEquals( PathTool.getRelativeWebPath( null, null ), "" );
        assertEquals( PathTool.getRelativeWebPath( null, "http://plexus.codehaus.org/" ), "" );
        assertEquals( PathTool.getRelativeWebPath( "http://plexus.codehaus.org/", null ), "" );
        assertEquals( PathTool.getRelativeWebPath( "http://plexus.codehaus.org/",
                                                   "http://plexus.codehaus.org/plexus-utils/index.html" ),
                      "plexus-utils/index.html" );
        assertEquals( PathTool.getRelativeWebPath( "http://plexus.codehaus.org/plexus-utils/index.html",
                                                   "http://plexus.codehaus.org/" ), "../../" );
    }

    /**
     * @throws Exception
     */
    public void testGetRelativeFilePath()
        throws Exception
    {
        if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
        {
            assertEquals( PathTool.getRelativeFilePath( null, null ), "" );
            assertEquals( PathTool.getRelativeFilePath( null, "c:\\tools\\java\\bin" ), "" );
            assertEquals( PathTool.getRelativeFilePath( "c:\\tools\\java", null ), "" );
            assertEquals( PathTool.getRelativeFilePath( "c:\\tools", "c:\\tools\\java\\bin" ), "java\\bin" );
            assertEquals( PathTool.getRelativeFilePath( "c:\\tools", "c:\\tools\\java\\bin\\" ), "java\\bin\\" );
            assertEquals( PathTool.getRelativeFilePath( "c:\\tools\\java\\bin", "c:\\tools" ), "..\\.." );
            assertEquals( PathTool.getRelativeFilePath( "c:\\tools\\", "c:\\tools\\java\\bin\\java.exe" ),
                          "java\\bin\\java.exe" );
            assertEquals( PathTool.getRelativeFilePath( "c:\\tools\\java\\bin\\java.sh", "c:\\tools" ), "..\\..\\.." );
            assertEquals( PathTool.getRelativeFilePath( "c:\\tools", "c:\\bin" ), "..\\bin" );
            assertEquals( PathTool.getRelativeFilePath( "c:\\bin", "c:\\tools" ), "..\\tools" );
        }
        else
        {
            assertEquals( PathTool.getRelativeFilePath( null, null ), "" );
            assertEquals( PathTool.getRelativeFilePath( null, "/usr/local/java/bin" ), "" );
            assertEquals( PathTool.getRelativeFilePath( "/usr/local", null ), "" );
            assertEquals( PathTool.getRelativeFilePath( "/usr/local", "/usr/local/java/bin" ), "java/bin" );
            assertEquals( PathTool.getRelativeFilePath( "/usr/local", "/usr/local/java/bin/" ), "java/bin" );
            assertEquals( PathTool.getRelativeFilePath( "/usr/local/java/bin", "/usr/local/" ), "../.." );
            assertEquals( PathTool.getRelativeFilePath( "/usr/local/", "/usr/local/java/bin/java.sh" ),
                          "java/bin/java.sh" );
            assertEquals( PathTool.getRelativeFilePath( "/usr/local/java/bin/java.sh", "/usr/local/" ), "../../.." );
            assertEquals( PathTool.getRelativeFilePath( "/usr/local/", "/bin" ), "../../bin" );
            assertEquals( PathTool.getRelativeFilePath( "/bin", "/usr/local/" ), "../usr/local" );
        }
    }
}

@@ -5584,32 +5584,33 @@
 n/%22 ), %22java/bin
+/
 %22 );%0A           
@@ -5692,32 +5692,33 @@
 ocal/%22 ), %22../..
+/
 %22 );%0A           
@@ -5967,24 +5967,25 @@
 ), %22../../..
+/
 %22 );%0A       
@@ -6065,32 +6065,32 @@
  %22../../bin%22 );%0A
+
             asse
@@ -6140,33 +6140,32 @@
 in%22, %22/usr/local
-/
 %22 ), %22../usr/loc

fix unit tests on unix platform