From 0f4e3cd06141eb203db404acfd8906157a7223b2 Mon Sep 17 00:00:00 2001 From: Jason van Zyl Date: Fri, 27 Feb 2009 15:38:14 +0000 Subject: [PATCH] o adding back in mirroring code but separated into a component and reactivated the tests git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@748558 13f79535-47bb-0310-9956-ffa450edef68 --- .../repository/DefaultMirrorBuilder.java | 184 ++++++++++ .../LegacyMavenRepositorySystem.java | 327 ------------------ .../repository/MavenRepositorySystem.java | 12 - .../maven/repository/MirrorBuilder.java | 20 ++ .../maven/repository/MirrorProcessorTest.java | 189 ++++++++++ 5 files changed, 393 insertions(+), 339 deletions(-) create mode 100644 maven-repository/src/main/java/org/apache/maven/repository/DefaultMirrorBuilder.java create mode 100644 maven-repository/src/main/java/org/apache/maven/repository/MirrorBuilder.java create mode 100644 maven-repository/src/test/java/org/apache/maven/repository/MirrorProcessorTest.java diff --git a/maven-repository/src/main/java/org/apache/maven/repository/DefaultMirrorBuilder.java b/maven-repository/src/main/java/org/apache/maven/repository/DefaultMirrorBuilder.java new file mode 100644 index 0000000000..1921839c02 --- /dev/null +++ b/maven-repository/src/main/java/org/apache/maven/repository/DefaultMirrorBuilder.java @@ -0,0 +1,184 @@ +package org.apache.maven.repository; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Set; + +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; +import org.apache.maven.artifact.repository.DefaultArtifactRepository; +import org.codehaus.plexus.component.annotations.Component; +import org.codehaus.plexus.component.annotations.Requirement; +import org.codehaus.plexus.logging.Logger; + +@Component(role=MirrorBuilder.class) +public class DefaultMirrorBuilder + implements MirrorBuilder +{ + private static final String WILDCARD = "*"; + + private static final String EXTERNAL_WILDCARD = "external:*"; + + private static int anonymousMirrorIdSeed = 0; + + @Requirement + private Logger logger; + + @Requirement + private ArtifactRepositoryFactory repositoryFactory; + + //used LinkedMap to preserve the order. + private Map mirrors = new LinkedHashMap(); + + public void addMirror( String id, String mirrorOf, String url ) + { + if ( id == null ) + { + id = "mirror-" + anonymousMirrorIdSeed++; + logger.warn( "You are using a mirror that doesn't declare an element. Using \'" + id + "\' instead:\nId: " + id + "\nmirrorOf: " + mirrorOf + "\nurl: " + url + "\n" ); + } + + ArtifactRepository mirror = new DefaultArtifactRepository( id, url, null ); + + System.out.println( mirror + " --> " + mirrorOf ); + + mirrors.put( mirrorOf, mirror ); + } + + public ArtifactRepository getMirrorRepository( ArtifactRepository repository ) + { + ArtifactRepository mirror = getMirror( repository ); + if ( mirror != null ) + { + String id = mirror.getId(); + if ( id == null ) + { + // TODO: this should be illegal in settings.xml + id = repository.getId(); + } + + logger.debug( "Using mirror: " + mirror.getId() + " for repository: " + repository.getId() + "\n(mirror url: " + mirror.getUrl() + ")" ); + repository = repositoryFactory.createArtifactRepository( id, mirror.getUrl(), + repository.getLayout(), repository.getSnapshots(), + repository.getReleases() ); + } + return repository; + } + + /** + * This method finds a matching mirror for the selected repository. If there is an exact match, + * this will be used. If there is no exact match, then the list of mirrors is examined to see if + * a pattern applies. + * + * @param originalRepository See if there is a mirror for this repository. + * @return the selected mirror or null if none are found. + */ + public ArtifactRepository getMirror( ArtifactRepository originalRepository ) + { + ArtifactRepository selectedMirror = mirrors.get( originalRepository.getId() ); + if ( null == selectedMirror ) + { + // Process the patterns in order. First one that matches wins. + Set keySet = mirrors.keySet(); + if ( keySet != null ) + { + for ( String pattern : keySet ) + { + if ( matchPattern( originalRepository, pattern ) ) + { + selectedMirror = mirrors.get( pattern ); + } + } + } + + } + + return selectedMirror; + } + + /** + * This method checks if the pattern matches the originalRepository. Valid patterns: * = + * everything external:* = everything not on the localhost and not file based. repo,repo1 = repo + * or repo1 *,!repo1 = everything except repo1 + * + * @param originalRepository to compare for a match. + * @param pattern used for match. Currently only '*' is supported. + * @return true if the repository is a match to this pattern. + */ + public boolean matchPattern( ArtifactRepository originalRepository, String pattern ) + { + boolean result = false; + String originalId = originalRepository.getId(); + + // simple checks first to short circuit processing below. + if ( WILDCARD.equals( pattern ) || pattern.equals( originalId ) ) + { + result = true; + } + else + { + // process the list + String[] repos = pattern.split( "," ); + for ( String repo : repos ) + { + // see if this is a negative match + if ( repo.length() > 1 && repo.startsWith( "!" ) ) + { + if ( originalId.equals( repo.substring( 1 ) ) ) + { + // explicitly exclude. Set result and stop processing. + result = false; + break; + } + } + // check for exact match + else if ( originalId.equals( repo ) ) + { + result = true; + break; + } + // check for external:* + else if ( EXTERNAL_WILDCARD.equals( repo ) && isExternalRepo( originalRepository ) ) + { + result = true; + // don't stop processing in case a future segment explicitly excludes this repo + } + else if ( WILDCARD.equals( repo ) ) + { + result = true; + // don't stop processing in case a future segment explicitly excludes this repo + } + } + } + return result; + } + + + /** + * Checks the URL to see if this repository refers to an external repository + * + * @param originalRepository + * @return true if external. + */ + public boolean isExternalRepo( ArtifactRepository originalRepository ) + { + try + { + URL url = new URL( originalRepository.getUrl() ); + return !( url.getHost().equals( "localhost" ) || url.getHost().equals( "127.0.0.1" ) || url.getProtocol().equals( "file" ) ); + } + catch ( MalformedURLException e ) + { + // bad url just skip it here. It should have been validated already, but the wagon lookup will deal with it + return false; + } + } + + public void clearMirrors() + { + mirrors.clear(); + anonymousMirrorIdSeed = 0; + } +} diff --git a/maven-repository/src/main/java/org/apache/maven/repository/LegacyMavenRepositorySystem.java b/maven-repository/src/main/java/org/apache/maven/repository/LegacyMavenRepositorySystem.java index cefcd393ed..0365e06e0f 100644 --- a/maven-repository/src/main/java/org/apache/maven/repository/LegacyMavenRepositorySystem.java +++ b/maven-repository/src/main/java/org/apache/maven/repository/LegacyMavenRepositorySystem.java @@ -532,16 +532,6 @@ public class LegacyMavenRepositorySystem return artifactResolver.resolve( request ); } - // ------------------------------------------------------------------------ - // Extracted from DefaultWagonManager - // ------------------------------------------------------------------------ - - private static final String WILDCARD = "*"; - - private static final String EXTERNAL_WILDCARD = "external:*"; - - private static int anonymousMirrorIdSeed = 0; - private boolean online = true; private boolean interactive = true; @@ -554,147 +544,6 @@ public class LegacyMavenRepositorySystem private Map serverPermissionsMap = new HashMap(); - //used LinkedMap to preserve the order. - private Map mirrors = new LinkedHashMap(); - - public ArtifactRepository getMirrorRepository( ArtifactRepository repository ) - { - ArtifactRepository mirror = getMirror( repository ); - if ( mirror != null ) - { - String id = mirror.getId(); - if ( id == null ) - { - // TODO: this should be illegal in settings.xml - id = repository.getId(); - } - - logger.debug( "Using mirror: " + mirror.getId() + " for repository: " + repository.getId() + "\n(mirror url: " + mirror.getUrl() + ")" ); - repository = artifactRepositoryFactory.createArtifactRepository( id, mirror.getUrl(), repository.getLayout(), repository.getSnapshots(), repository.getReleases() ); - } - return repository; - } - - /** - * This method finds a matching mirror for the selected repository. If there is an exact match, - * this will be used. If there is no exact match, then the list of mirrors is examined to see if - * a pattern applies. - * - * @param originalRepository See if there is a mirror for this repository. - * @return the selected mirror or null if none are found. - */ - public ArtifactRepository getMirror( ArtifactRepository originalRepository ) - { - ArtifactRepository selectedMirror = mirrors.get( originalRepository.getId() ); - if ( null == selectedMirror ) - { - // Process the patterns in order. First one that matches wins. - Set keySet = mirrors.keySet(); - if ( keySet != null ) - { - for ( String pattern : keySet ) - { - if ( matchPattern( originalRepository, pattern ) ) - { - selectedMirror = mirrors.get( pattern ); - } - } - } - - } - return selectedMirror; - } - - /** - * This method checks if the pattern matches the originalRepository. Valid patterns: * = - * everything external:* = everything not on the localhost and not file based. repo,repo1 = repo - * or repo1 *,!repo1 = everything except repo1 - * - * @param originalRepository to compare for a match. - * @param pattern used for match. Currently only '*' is supported. - * @return true if the repository is a match to this pattern. - */ - public boolean matchPattern( ArtifactRepository originalRepository, String pattern ) - { - boolean result = false; - String originalId = originalRepository.getId(); - - // simple checks first to short circuit processing below. - if ( WILDCARD.equals( pattern ) || pattern.equals( originalId ) ) - { - result = true; - } - else - { - // process the list - String[] repos = pattern.split( "," ); - for ( String repo : repos ) - { - // see if this is a negative match - if ( repo.length() > 1 && repo.startsWith( "!" ) ) - { - if ( originalId.equals( repo.substring( 1 ) ) ) - { - // explicitly exclude. Set result and stop processing. - result = false; - break; - } - } - // check for exact match - else if ( originalId.equals( repo ) ) - { - result = true; - break; - } - // check for external:* - else if ( EXTERNAL_WILDCARD.equals( repo ) && isExternalRepo( originalRepository ) ) - { - result = true; - // don't stop processing in case a future segment explicitly excludes this repo - } - else if ( WILDCARD.equals( repo ) ) - { - result = true; - // don't stop processing in case a future segment explicitly excludes this repo - } - } - } - return result; - } - - /** - * Checks the URL to see if this repository refers to an external repository - * - * @param originalRepository - * @return true if external. - */ - public boolean isExternalRepo( ArtifactRepository originalRepository ) - { - try - { - URL url = new URL( originalRepository.getUrl() ); - return !( url.getHost().equals( "localhost" ) || url.getHost().equals( "127.0.0.1" ) || url.getProtocol().equals( "file" ) ); - } - catch ( MalformedURLException e ) - { - // bad url just skip it here. It should have been validated already, but the wagon lookup will deal with it - return false; - } - } - - public void addMirror( String id, String mirrorOf, String url ) - { - if ( id == null ) - { - id = "mirror-" + anonymousMirrorIdSeed++; - logger.warn( "You are using a mirror that doesn't declare an element. Using \'" + id + "\' instead:\nId: " + id + "\nmirrorOf: " + mirrorOf + "\nurl: " + url + "\n" ); - } - - ArtifactRepository mirror = new DefaultArtifactRepository( id, url, null ); - - mirrors.put( mirrorOf, mirror ); - } - public void setOnline( boolean online ) { this.online = online; @@ -777,180 +626,4 @@ public class LegacyMavenRepositorySystem { return artifactMetadataSource.retrieveRelocatedArtifact( artifact, localRepository, remoteRepositories ); } - - // Test for this stuff - - /* - - public void testAddMirrorWithNullRepositoryId() - { - wagonManager.addMirror( null, "test", "http://www.nowhere.com/" ); - } - - public void testGetArtifactSha1MissingMd5Present() - throws IOException, UnsupportedProtocolException, TransferFailedException, ResourceDoesNotExistException - { - Artifact artifact = createTestPomArtifact( "target/test-data/get-remote-artifact" ); - - ArtifactRepository repo = createStringRepo(); - - StringWagon wagon = (StringWagon) wagonManager.getWagon( "string" ); - wagon.addExpectedContent( repo.getLayout().pathOf( artifact ), "expected" ); - wagon.addExpectedContent( repo.getLayout().pathOf( artifact ) + ".md5", "bad_checksum" ); - - wagonManager.getArtifact( artifact, repo, true ); - - assertTrue( artifact.getFile().exists() ); - } - - public void testExternalURL() - { - DefaultWagonManager mgr = new DefaultWagonManager(); - assertTrue( mgr.isExternalRepo( getRepo( "foo", "http://somehost" ) ) ); - assertTrue( mgr.isExternalRepo( getRepo( "foo", "http://somehost:9090/somepath" ) ) ); - assertTrue( mgr.isExternalRepo( getRepo( "foo", "ftp://somehost" ) ) ); - assertTrue( mgr.isExternalRepo( getRepo( "foo", "http://192.168.101.1" ) ) ); - assertTrue( mgr.isExternalRepo( getRepo( "foo", "http://" ) ) ); - // these are local - assertFalse( mgr.isExternalRepo( getRepo( "foo", "http://localhost:8080" ) ) ); - assertFalse( mgr.isExternalRepo( getRepo( "foo", "http://127.0.0.1:9090" ) ) ); - assertFalse( mgr.isExternalRepo( getRepo( "foo", "file://localhost/somepath" ) ) ); - assertFalse( mgr.isExternalRepo( getRepo( "foo", "file://localhost/D:/somepath" ) ) ); - assertFalse( mgr.isExternalRepo( getRepo( "foo", "http://localhost" ) ) ); - assertFalse( mgr.isExternalRepo( getRepo( "foo", "http://127.0.0.1" ) ) ); - assertFalse( mgr.isExternalRepo( getRepo( "foo", "file:///somepath" ) ) ); - assertFalse( mgr.isExternalRepo( getRepo( "foo", "file://D:/somepath" ) ) ); - - // not a proper url so returns false; - assertFalse( mgr.isExternalRepo( getRepo( "foo", "192.168.101.1" ) ) ); - assertFalse( mgr.isExternalRepo( getRepo( "foo", "" ) ) ); - } - - public void testMirrorLookup() - { - wagonManager.addMirror( "a", "a", "http://a" ); - wagonManager.addMirror( "b", "b", "http://b" ); - - ArtifactRepository repo = null; - repo = wagonManager.getMirrorRepository( getRepo( "a", "http://a.a" ) ); - assertEquals( "http://a", repo.getUrl() ); - - repo = wagonManager.getMirrorRepository( getRepo( "b", "http://a.a" ) ); - assertEquals( "http://b", repo.getUrl() ); - - repo = wagonManager.getMirrorRepository( getRepo( "c", "http://c.c" ) ); - assertEquals( "http://c.c", repo.getUrl() ); - - } - - public void testMirrorWildcardLookup() - { - wagonManager.addMirror( "a", "a", "http://a" ); - wagonManager.addMirror( "b", "b", "http://b" ); - wagonManager.addMirror( "c", "*", "http://wildcard" ); - - ArtifactRepository repo = null; - repo = wagonManager.getMirrorRepository( getRepo( "a", "http://a.a" ) ); - assertEquals( "http://a", repo.getUrl() ); - - repo = wagonManager.getMirrorRepository( getRepo( "b", "http://a.a" ) ); - assertEquals( "http://b", repo.getUrl() ); - - repo = wagonManager.getMirrorRepository( getRepo( "c", "http://c.c" ) ); - assertEquals( "http://wildcard", repo.getUrl() ); - - } - - public void testMirrorStopOnFirstMatch() - { - //exact matches win first - wagonManager.addMirror( "a2", "a,b", "http://a2" ); - wagonManager.addMirror( "a", "a", "http://a" ); - //make sure repeated entries are skipped - wagonManager.addMirror( "a", "a", "http://a3" ); - - wagonManager.addMirror( "b", "b", "http://b" ); - wagonManager.addMirror( "c", "d,e", "http://de" ); - wagonManager.addMirror( "c", "*", "http://wildcard" ); - wagonManager.addMirror( "c", "e,f", "http://ef" ); - - - - ArtifactRepository repo = null; - repo = wagonManager.getMirrorRepository( getRepo( "a", "http://a.a" ) ); - assertEquals( "http://a", repo.getUrl() ); - - repo = wagonManager.getMirrorRepository( getRepo( "b", "http://a.a" ) ); - assertEquals( "http://b", repo.getUrl() ); - - repo = wagonManager.getMirrorRepository( getRepo( "c", "http://c.c" ) ); - assertEquals( "http://wildcard", repo.getUrl() ); - - repo = wagonManager.getMirrorRepository( getRepo( "d", "http://d" ) ); - assertEquals( "http://de", repo.getUrl() ); - - repo = wagonManager.getMirrorRepository( getRepo( "e", "http://e" ) ); - assertEquals( "http://de", repo.getUrl() ); - - repo = wagonManager.getMirrorRepository( getRepo( "f", "http://f" ) ); - assertEquals( "http://wildcard", repo.getUrl() ); - - } - - - public void testPatterns() - { - DefaultWagonManager mgr = new DefaultWagonManager(); - - assertTrue( mgr.matchPattern( getRepo( "a" ), "*" ) ); - assertTrue( mgr.matchPattern( getRepo( "a" ), "*," ) ); - assertTrue( mgr.matchPattern( getRepo( "a" ), ",*," ) ); - assertTrue( mgr.matchPattern( getRepo( "a" ), "*," ) ); - - assertTrue( mgr.matchPattern( getRepo( "a" ), "a" ) ); - assertTrue( mgr.matchPattern( getRepo( "a" ), "a," ) ); - assertTrue( mgr.matchPattern( getRepo( "a" ), ",a," ) ); - assertTrue( mgr.matchPattern( getRepo( "a" ), "a," ) ); - - assertFalse( mgr.matchPattern( getRepo( "b" ), "a" ) ); - assertFalse( mgr.matchPattern( getRepo( "b" ), "a," ) ); - assertFalse( mgr.matchPattern( getRepo( "b" ), ",a" ) ); - assertFalse( mgr.matchPattern( getRepo( "b" ), ",a," ) ); - - assertTrue( mgr.matchPattern( getRepo( "a" ), "a,b" ) ); - assertTrue( mgr.matchPattern( getRepo( "b" ), "a,b" ) ); - - assertFalse( mgr.matchPattern( getRepo( "c" ), "a,b" ) ); - - assertTrue( mgr.matchPattern( getRepo( "a" ), "*" ) ); - assertTrue( mgr.matchPattern( getRepo( "a" ), "*,b" ) ); - assertTrue( mgr.matchPattern( getRepo( "a" ), "*,!b" ) ); - - assertFalse( mgr.matchPattern( getRepo( "a" ), "*,!a" ) ); - assertFalse( mgr.matchPattern( getRepo( "a" ), "!a,*" ) ); - - assertTrue( mgr.matchPattern( getRepo( "c" ), "*,!a" ) ); - assertTrue( mgr.matchPattern( getRepo( "c" ), "!a,*" ) ); - - assertFalse( mgr.matchPattern( getRepo( "c" ), "!a,!c" ) ); - assertFalse( mgr.matchPattern( getRepo( "d" ), "!a,!c*" ) ); - } - - public void testPatternsWithExternal() - { - DefaultWagonManager mgr = new DefaultWagonManager(); - - assertTrue( mgr.matchPattern( getRepo( "a", "http://localhost" ), "*" ) ); - assertFalse( mgr.matchPattern( getRepo( "a", "http://localhost" ), "external:*" ) ); - - assertTrue( mgr.matchPattern( getRepo( "a", "http://localhost" ), "external:*,a" ) ); - assertFalse( mgr.matchPattern( getRepo( "a", "http://localhost" ), "external:*,!a" ) ); - assertTrue( mgr.matchPattern( getRepo( "a", "http://localhost" ), "a,external:*" ) ); - assertFalse( mgr.matchPattern( getRepo( "a", "http://localhost" ), "!a,external:*" ) ); - - assertFalse( mgr.matchPattern( getRepo( "c", "http://localhost" ), "!a,external:*" ) ); - assertTrue( mgr.matchPattern( getRepo( "c", "http://somehost" ), "!a,external:*" ) ); - } - - */ } diff --git a/maven-repository/src/main/java/org/apache/maven/repository/MavenRepositorySystem.java b/maven-repository/src/main/java/org/apache/maven/repository/MavenRepositorySystem.java index 00a325a61d..678fbc126c 100644 --- a/maven-repository/src/main/java/org/apache/maven/repository/MavenRepositorySystem.java +++ b/maven-repository/src/main/java/org/apache/maven/repository/MavenRepositorySystem.java @@ -111,18 +111,6 @@ public interface MavenRepositorySystem public Artifact retrieveRelocatedArtifact( Artifact artifact, ArtifactRepository localRepository, List remoteRepositories ) throws ArtifactMetadataRetrievalException; - // Mirrors - - ArtifactRepository getMirrorRepository( ArtifactRepository repository ); - - ArtifactRepository getMirror( ArtifactRepository originalRepository ); - - boolean matchPattern( ArtifactRepository originalRepository, String pattern ); - - boolean isExternalRepo( ArtifactRepository originalRepository ); - - void addMirror( String id, String mirrorOf, String url ); - // Network enablement void setOnline( boolean online ); diff --git a/maven-repository/src/main/java/org/apache/maven/repository/MirrorBuilder.java b/maven-repository/src/main/java/org/apache/maven/repository/MirrorBuilder.java new file mode 100644 index 0000000000..8e5adeadcd --- /dev/null +++ b/maven-repository/src/main/java/org/apache/maven/repository/MirrorBuilder.java @@ -0,0 +1,20 @@ +package org.apache.maven.repository; + +import org.apache.maven.artifact.repository.ArtifactRepository; + +public interface MirrorBuilder +{ + ArtifactRepository getMirror( ArtifactRepository originalRepository ); + + void addMirror( String id, String mirrorOf, String url ); + + void clearMirrors(); + + // These need to go + + boolean isExternalRepo( ArtifactRepository originalRepository ); + + boolean matchPattern( ArtifactRepository originalRepository, String pattern ); + + ArtifactRepository getMirrorRepository( ArtifactRepository repository ); +} diff --git a/maven-repository/src/test/java/org/apache/maven/repository/MirrorProcessorTest.java b/maven-repository/src/test/java/org/apache/maven/repository/MirrorProcessorTest.java new file mode 100644 index 0000000000..8aca70b904 --- /dev/null +++ b/maven-repository/src/test/java/org/apache/maven/repository/MirrorProcessorTest.java @@ -0,0 +1,189 @@ +package org.apache.maven.repository; + +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.repository.DefaultArtifactRepository; +import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout; +import org.codehaus.plexus.PlexusTestCase; + +public class MirrorProcessorTest + extends PlexusTestCase +{ + private MirrorBuilder mirrorBuilder; + + protected void setUp() + throws Exception + { + mirrorBuilder = lookup( MirrorBuilder.class ); + mirrorBuilder.clearMirrors(); + } + + public void testAddMirrorWithNullRepositoryId() + { + mirrorBuilder.addMirror( null, "test", "http://www.nowhere.com/" ); + } + + public void testExternalURL() + { + assertTrue( mirrorBuilder.isExternalRepo( getRepo( "foo", "http://somehost" ) ) ); + assertTrue( mirrorBuilder.isExternalRepo( getRepo( "foo", "http://somehost:9090/somepath" ) ) ); + assertTrue( mirrorBuilder.isExternalRepo( getRepo( "foo", "ftp://somehost" ) ) ); + assertTrue( mirrorBuilder.isExternalRepo( getRepo( "foo", "http://192.168.101.1" ) ) ); + assertTrue( mirrorBuilder.isExternalRepo( getRepo( "foo", "http://" ) ) ); + // these are local + assertFalse( mirrorBuilder.isExternalRepo( getRepo( "foo", "http://localhost:8080" ) ) ); + assertFalse( mirrorBuilder.isExternalRepo( getRepo( "foo", "http://127.0.0.1:9090" ) ) ); + assertFalse( mirrorBuilder.isExternalRepo( getRepo( "foo", "file://localhost/somepath" ) ) ); + assertFalse( mirrorBuilder.isExternalRepo( getRepo( "foo", "file://localhost/D:/somepath" ) ) ); + assertFalse( mirrorBuilder.isExternalRepo( getRepo( "foo", "http://localhost" ) ) ); + assertFalse( mirrorBuilder.isExternalRepo( getRepo( "foo", "http://127.0.0.1" ) ) ); + assertFalse( mirrorBuilder.isExternalRepo( getRepo( "foo", "file:///somepath" ) ) ); + assertFalse( mirrorBuilder.isExternalRepo( getRepo( "foo", "file://D:/somepath" ) ) ); + + // not a proper url so returns false; + assertFalse( mirrorBuilder.isExternalRepo( getRepo( "foo", "192.168.101.1" ) ) ); + assertFalse( mirrorBuilder.isExternalRepo( getRepo( "foo", "" ) ) ); + } + + public void testMirrorLookup() + { + mirrorBuilder.addMirror( "a", "a", "http://a" ); + mirrorBuilder.addMirror( "b", "b", "http://b" ); + + ArtifactRepository repo = null; + repo = mirrorBuilder.getMirrorRepository( getRepo( "a", "http://a.a" ) ); + assertEquals( "http://a", repo.getUrl() ); + + repo = mirrorBuilder.getMirrorRepository( getRepo( "b", "http://a.a" ) ); + assertEquals( "http://b", repo.getUrl() ); + + repo = mirrorBuilder.getMirrorRepository( getRepo( "c", "http://c.c" ) ); + assertEquals( "http://c.c", repo.getUrl() ); + + } + + public void testMirrorWildcardLookup() + { + mirrorBuilder.addMirror( "a", "a", "http://a" ); + mirrorBuilder.addMirror( "b", "b", "http://b" ); + mirrorBuilder.addMirror( "c", "*", "http://wildcard" ); + + ArtifactRepository repo = null; + repo = mirrorBuilder.getMirrorRepository( getRepo( "a", "http://a.a" ) ); + assertEquals( "http://a", repo.getUrl() ); + + repo = mirrorBuilder.getMirrorRepository( getRepo( "b", "http://a.a" ) ); + assertEquals( "http://b", repo.getUrl() ); + + repo = mirrorBuilder.getMirrorRepository( getRepo( "c", "http://c.c" ) ); + assertEquals( "http://wildcard", repo.getUrl() ); + + } + + public void xtestMirrorStopOnFirstMatch() + { + //exact matches win first + mirrorBuilder.addMirror( "a2", "a,b", "http://a2" ); + mirrorBuilder.addMirror( "a", "a", "http://a" ); + //make sure repeated entries are skipped + mirrorBuilder.addMirror( "a", "a", "http://a3" ); + + mirrorBuilder.addMirror( "b", "b", "http://b" ); + mirrorBuilder.addMirror( "c", "d,e", "http://de" ); + mirrorBuilder.addMirror( "c", "*", "http://wildcard" ); + mirrorBuilder.addMirror( "c", "e,f", "http://ef" ); + + ArtifactRepository repo = null; + repo = mirrorBuilder.getMirrorRepository( getRepo( "a", "http://a.a" ) ); + assertEquals( "http://a", repo.getUrl() ); + + repo = mirrorBuilder.getMirrorRepository( getRepo( "b", "http://a.a" ) ); + assertEquals( "http://b", repo.getUrl() ); + + repo = mirrorBuilder.getMirrorRepository( getRepo( "c", "http://c.c" ) ); + assertEquals( "http://wildcard", repo.getUrl() ); + + repo = mirrorBuilder.getMirrorRepository( getRepo( "d", "http://d" ) ); + assertEquals( "http://de", repo.getUrl() ); + + repo = mirrorBuilder.getMirrorRepository( getRepo( "e", "http://e" ) ); + assertEquals( "http://de", repo.getUrl() ); + + repo = mirrorBuilder.getMirrorRepository( getRepo( "f", "http://f" ) ); + assertEquals( "http://wildcard", repo.getUrl() ); + + } + + + public void testPatterns() + { + assertTrue( mirrorBuilder.matchPattern( getRepo( "a" ), "*" ) ); + assertTrue( mirrorBuilder.matchPattern( getRepo( "a" ), "*," ) ); + assertTrue( mirrorBuilder.matchPattern( getRepo( "a" ), ",*," ) ); + assertTrue( mirrorBuilder.matchPattern( getRepo( "a" ), "*," ) ); + + assertTrue( mirrorBuilder.matchPattern( getRepo( "a" ), "a" ) ); + assertTrue( mirrorBuilder.matchPattern( getRepo( "a" ), "a," ) ); + assertTrue( mirrorBuilder.matchPattern( getRepo( "a" ), ",a," ) ); + assertTrue( mirrorBuilder.matchPattern( getRepo( "a" ), "a," ) ); + + assertFalse( mirrorBuilder.matchPattern( getRepo( "b" ), "a" ) ); + assertFalse( mirrorBuilder.matchPattern( getRepo( "b" ), "a," ) ); + assertFalse( mirrorBuilder.matchPattern( getRepo( "b" ), ",a" ) ); + assertFalse( mirrorBuilder.matchPattern( getRepo( "b" ), ",a," ) ); + + assertTrue( mirrorBuilder.matchPattern( getRepo( "a" ), "a,b" ) ); + assertTrue( mirrorBuilder.matchPattern( getRepo( "b" ), "a,b" ) ); + + assertFalse( mirrorBuilder.matchPattern( getRepo( "c" ), "a,b" ) ); + + assertTrue( mirrorBuilder.matchPattern( getRepo( "a" ), "*" ) ); + assertTrue( mirrorBuilder.matchPattern( getRepo( "a" ), "*,b" ) ); + assertTrue( mirrorBuilder.matchPattern( getRepo( "a" ), "*,!b" ) ); + + assertFalse( mirrorBuilder.matchPattern( getRepo( "a" ), "*,!a" ) ); + assertFalse( mirrorBuilder.matchPattern( getRepo( "a" ), "!a,*" ) ); + + assertTrue( mirrorBuilder.matchPattern( getRepo( "c" ), "*,!a" ) ); + assertTrue( mirrorBuilder.matchPattern( getRepo( "c" ), "!a,*" ) ); + + assertFalse( mirrorBuilder.matchPattern( getRepo( "c" ), "!a,!c" ) ); + assertFalse( mirrorBuilder.matchPattern( getRepo( "d" ), "!a,!c*" ) ); + } + + public void testPatternsWithExternal() + { + assertTrue( mirrorBuilder.matchPattern( getRepo( "a", "http://localhost" ), "*" ) ); + assertFalse( mirrorBuilder.matchPattern( getRepo( "a", "http://localhost" ), "external:*" ) ); + + assertTrue( mirrorBuilder.matchPattern( getRepo( "a", "http://localhost" ), "external:*,a" ) ); + assertFalse( mirrorBuilder.matchPattern( getRepo( "a", "http://localhost" ), "external:*,!a" ) ); + assertTrue( mirrorBuilder.matchPattern( getRepo( "a", "http://localhost" ), "a,external:*" ) ); + assertFalse( mirrorBuilder.matchPattern( getRepo( "a", "http://localhost" ), "!a,external:*" ) ); + + assertFalse( mirrorBuilder.matchPattern( getRepo( "c", "http://localhost" ), "!a,external:*" ) ); + assertTrue( mirrorBuilder.matchPattern( getRepo( "c", "http://somehost" ), "!a,external:*" ) ); + } + + /** + * Build an ArtifactRepository object. + * + * @param id + * @param url + * @return + */ + private ArtifactRepository getRepo( String id, String url ) + { + return new DefaultArtifactRepository( id, url, new DefaultRepositoryLayout() ); + } + + /** + * Build an ArtifactRepository object. + * + * @param id + * @return + */ + private ArtifactRepository getRepo( String id ) + { + return getRepo( id, "http://something" ); + } +}