Browse Source

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
tags/archiva-1.0-beta-2
Joakim Erdfelt 17 years ago
parent
commit
05d0e9e6f5

+ 20
- 1
archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/utils/PathUtil.java View File

@@ -39,7 +39,7 @@ public class PathUtil
{
return path;
}
return toUrl( new File( path ) );
}

@@ -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.

+ 7
- 1
archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/utils/PathUtilTest.java View File

@@ -34,11 +34,17 @@ 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()
{

Loading…
Cancel
Save