Fixing subtle bug in PathUtil.toRelative() encountered during Metadata work.

git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/trunk@569758 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Joakim Erdfelt 2007-08-26 03:16:11 +00:00
parent 81fd9bd44c
commit 05d0e9e6f5
2 changed files with 27 additions and 2 deletions

View File

@ -61,13 +61,32 @@ public class PathUtil
}
}
/**
* Given a basedir and a child file, return the relative path to the child.
*
* @param basedir the basedir.
* @param file the file to get the relative path for.
* @return the relative path to the child. (NOTE: this path will NOT start with a {@link File#separator} character)
*/
public static String getRelative( String basedir, File file )
{
return getRelative( basedir, file.getAbsolutePath() );
}
/**
* Given a basedir and a child file, return the relative path to the child.
*
* @param basedir the basedir.
* @param child the child path (can be a full path)
* @return the relative path to the child. (NOTE: this path will NOT start with a {@link File#separator} character)
*/
public static String getRelative( String basedir, String child )
{
if ( basedir.endsWith( File.separator ) )
{
basedir = basedir.substring( 0, basedir.length() - 1 );
}
if ( child.startsWith( basedir ) )
{
// simple solution.

View File

@ -34,12 +34,18 @@ import junit.framework.TestCase;
public class PathUtilTest
extends TestCase
{
public void testToRelative()
public void testToRelativeWithoutSlash()
{
assertEquals( "path/to/resource.xml", PathUtil.getRelative( "/home/user/foo/repository",
"/home/user/foo/repository/path/to/resource.xml" ) );
}
public void testToRelativeWithSlash()
{
assertEquals( "path/to/resource.xml", PathUtil.getRelative( "/home/user/foo/repository/",
"/home/user/foo/repository/path/to/resource.xml" ) );
}
public void testToUrlRelativePath()
{
File workingDir = new File( "." );