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

{ {
return path; return path;
} }
return toUrl( new File( path ) ); return toUrl( new File( path ) );
} }


} }
} }


/**
* 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 ) public static String getRelative( String basedir, File file )
{ {
return getRelative( basedir, file.getAbsolutePath() ); 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 ) public static String getRelative( String basedir, String child )
{ {
if ( basedir.endsWith( File.separator ) )
{
basedir = basedir.substring( 0, basedir.length() - 1 );
}

if ( child.startsWith( basedir ) ) if ( child.startsWith( basedir ) )
{ {
// simple solution. // simple solution.

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

public class PathUtilTest public class PathUtilTest
extends TestCase extends TestCase
{ {
public void testToRelative()
public void testToRelativeWithoutSlash()
{ {
assertEquals( "path/to/resource.xml", PathUtil.getRelative( "/home/user/foo/repository", assertEquals( "path/to/resource.xml", PathUtil.getRelative( "/home/user/foo/repository",
"/home/user/foo/repository/path/to/resource.xml" ) ); "/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() public void testToUrlRelativePath()
{ {

Loading…
Cancel
Save