This is an automated email from the ASF dual-hosted git repository. elharo pushed a commit to branch uri in repository https://gitbox.apache.org/repos/asf/maven-doxia-sitetools.git
commit db76fea346d57b1dcecd5ee45e354d0e9788c29f Author: Elliotte Rusty Harold <[email protected]> AuthorDate: Wed Feb 19 11:06:49 2020 -0500 handle more URI formats --- .../apache/maven/doxia/tools/DefaultSiteTool.java | 44 ++++++++++++++++++-- .../maven/doxia/tools/DefaultSiteToolTest.java | 48 ++++++++++++++++++++++ 2 files changed, 88 insertions(+), 4 deletions(-) diff --git a/doxia-integration-tools/src/main/java/org/apache/maven/doxia/tools/DefaultSiteTool.java b/doxia-integration-tools/src/main/java/org/apache/maven/doxia/tools/DefaultSiteTool.java index 002402b..eaafa6c 100644 --- a/doxia-integration-tools/src/main/java/org/apache/maven/doxia/tools/DefaultSiteTool.java +++ b/doxia-integration-tools/src/main/java/org/apache/maven/doxia/tools/DefaultSiteTool.java @@ -126,7 +126,6 @@ public class DefaultSiteTool // Public methods // ---------------------------------------------------------------------- - /** {@inheritDoc} */ public Artifact getSkinArtifactFromRepository( ArtifactRepository localRepository, List<ArtifactRepository> remoteArtifactRepositories, DecorationModel decoration ) @@ -174,7 +173,6 @@ public class DefaultSiteTool return artifact; } - /** {@inheritDoc} */ public Artifact getDefaultSkinArtifact( ArtifactRepository localRepository, List<ArtifactRepository> remoteArtifactRepositories ) throws SiteToolException @@ -182,11 +180,35 @@ public class DefaultSiteTool return getSkinArtifactFromRepository( localRepository, remoteArtifactRepositories, new DecorationModel() ); } - /** {@inheritDoc} */ + /** + * This method is not implemented according to the URI specification and has many weird + * corner cases where it doesn't do the right thing. Please consider using a better + * implemented method from a different library such as org.apache.http.client.utils.URIUtils#resolve. + */ + @Deprecated public String getRelativePath( String to, String from ) { checkNotNull( "to", to ); checkNotNull( "from", from ); + + if (to.contains(":") && from.contains(":")) { + String toScheme = to.substring(0, to.indexOf( ':' )); + String fromScheme = from.substring( 0, from.indexOf( ':' ) ); + if ( !toScheme.equals( fromScheme ) ) + { + return to; + } + } + else // at least one is not absolute + { + return to; + } + + + // check for multiple colons; java.net.URL can't handle these + if ( colonCount( to ) > 1 || colonCount( from ) > 1 ) { + return to; + } URL toUrl = null; URL fromUrl = null; @@ -207,6 +229,7 @@ public class DefaultSiteTool catch ( MalformedURLException e1 ) { getLogger().warn( "Unable to load a URL for '" + to + "': " + e.getMessage() ); + return to; } } @@ -223,6 +246,7 @@ public class DefaultSiteTool catch ( MalformedURLException e1 ) { getLogger().warn( "Unable to load a URL for '" + from + "': " + e.getMessage() ); + return to; } } @@ -272,6 +296,18 @@ public class DefaultSiteTool return relativePath; } + private static int colonCount( String s ) + { + int count = 0; + for ( char c : s.toCharArray() ) { + if ( c == ':' ) + { + count++; + } + } + return count; + } + private static String getRelativeFilePath( final String oldPath, final String newPath ) { // normalize the path delimiters @@ -1496,7 +1532,7 @@ public class DefaultSiteTool final Properties properties = new Properties(); final String corePomProperties = "META-INF/maven/org.apache.maven/maven-core/pom.properties"; final InputStream in = MavenProject.class.getClassLoader().getResourceAsStream( corePomProperties ); - try + try { properties.load( in ); } diff --git a/doxia-integration-tools/src/test/java/org/apache/maven/doxia/tools/DefaultSiteToolTest.java b/doxia-integration-tools/src/test/java/org/apache/maven/doxia/tools/DefaultSiteToolTest.java index 0d54c5b..f712273 100644 --- a/doxia-integration-tools/src/test/java/org/apache/maven/doxia/tools/DefaultSiteToolTest.java +++ b/doxia-integration-tools/src/test/java/org/apache/maven/doxia/tools/DefaultSiteToolTest.java @@ -24,11 +24,24 @@ import org.junit.Test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; +import org.codehaus.plexus.logging.Logger; +import org.codehaus.plexus.logging.console.ConsoleLogger; +import org.junit.Before; + /** * @author <a href="mailto:[email protected]">Vincent Siveton</a> */ public class DefaultSiteToolTest { + + private DefaultSiteTool tool = new DefaultSiteTool(); + + @Before + public void setUp() { + Logger logger = new ConsoleLogger(Logger.LEVEL_WARN, "tool"); + tool.enableLogging(logger); + } + /** * test getNormalizedPath(). */ @@ -56,4 +69,39 @@ public class DefaultSiteToolTest assertEquals( "file:/Documents and Settings/", DefaultSiteTool.getNormalizedPath( "file://Documents and Settings/" ) ); } + + @Test + public void testGetRelativePath() + { + assertEquals( "../bar.html", tool.getRelativePath("http://example.com/foo/bar.html", "http://example.com/foo/baz.html")); + } + + @Test + public void testGetRelativePath_same() + { + assertEquals( "", tool.getRelativePath("http://example.com/foo/bar.html", "http://example.com/foo/bar.html")); + } + + @Test + public void testGetRelativePath_differentSchemes() + { + assertEquals( "scp://example.com/foo/bar.html", + tool.getRelativePath("scp://example.com/foo/bar.html", "http://example.com/foo/bar.html")); + assertEquals( "file:///tmp/bloop", + tool.getRelativePath("file:///tmp/bloop", "scp://localhost:/tmp/blop")); + } + + @Test + public void testGetRelativePath_differentDomains() + { + assertEquals("https://example.org/bar.html", + tool.getRelativePath( "https://example.org/bar.html", "https://example.com/bar.html")); + assertEquals("dav:https://nexus2.mysite.net:123/nexus/content/sites/site/mysite-child/2.0.0/", + tool.getRelativePath( + "dav:https://nexus2.mysite.net:123/nexus/content/sites/site/mysite-child/2.0.0/", + "dav:https://nexus1.mysite.net:123/nexus/content/sites/site/mysite-parent/1.0.0/") ); + } + + + }
