mirror of https://github.com/apache/archiva.git
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:
parent
81fd9bd44c
commit
05d0e9e6f5
|
@ -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.
|
||||
|
|
|
@ -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( "." );
|
||||
|
|
Loading…
Reference in New Issue