From 4648efa59b8e18c03ca63036a477cd89901eb0d2 Mon Sep 17 00:00:00 2001 From: Benjamin Bentmann Date: Thu, 24 Sep 2009 09:06:46 +0000 Subject: [PATCH] o Refactored code git-svn-id: https://svn.apache.org/repos/asf/maven/maven-3/trunk@818412 13f79535-47bb-0310-9956-ffa450edef68 --- .../repository/DefaultMirrorBuilder.java | 236 ------------------ .../repository/DefaultMirrorSelector.java | 142 +++++++++++ .../repository/LegacyRepositorySystem.java | 26 +- ...MirrorBuilder.java => MirrorSelector.java} | 26 +- .../maven/repository/MirrorProcessorTest.java | 206 +++++++-------- 5 files changed, 257 insertions(+), 379 deletions(-) delete mode 100644 maven-compat/src/main/java/org/apache/maven/repository/DefaultMirrorBuilder.java create mode 100644 maven-compat/src/main/java/org/apache/maven/repository/DefaultMirrorSelector.java rename maven-compat/src/main/java/org/apache/maven/repository/{MirrorBuilder.java => MirrorSelector.java} (61%) diff --git a/maven-compat/src/main/java/org/apache/maven/repository/DefaultMirrorBuilder.java b/maven-compat/src/main/java/org/apache/maven/repository/DefaultMirrorBuilder.java deleted file mode 100644 index 9c504e25fb..0000000000 --- a/maven-compat/src/main/java/org/apache/maven/repository/DefaultMirrorBuilder.java +++ /dev/null @@ -1,236 +0,0 @@ -package org.apache.maven.repository; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import java.net.MalformedURLException; -import java.net.URL; -import java.util.LinkedHashMap; -import java.util.List; -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.Authentication; -import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; -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, Authentication auth ) - { - 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" ); - } - - if ( !mirrors.containsKey( mirrorOf ) ) - { - ArtifactRepository mirror = repositoryFactory.createArtifactRepository( id, url, (ArtifactRepositoryLayout)null, null, null ); - - mirror.setAuthentication( auth ); - - mirrors.put( mirrorOf, mirror ); - } - } - - /** - * 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 ); - //stop on the first match. - break; - } - } - } - } - - return selectedMirror; - } - - public void clearMirrors() - { - mirrors.clear(); - anonymousMirrorIdSeed = 0; - } - - public List getMirrors( List remoteRepositories ) - { - if ( remoteRepositories != null ) - { - for ( ArtifactRepository repository : remoteRepositories) - { - // Check to see if we have a valid mirror for this repository - ArtifactRepository mirror = getMirror( repository ); - - if ( mirror != null ) - { - // We basically just want to take the URL - repository.setUrl( mirror.getUrl() ); - repository.setAuthentication( mirror.getAuthentication() ); - - // I would like a mirrored repository to be visually different but we'll put another field - // in the repository as changing the ID hoses up authentication. - repository.setId( mirror.getId() ); - } - } - } - - return remoteRepositories; - } - - // Make these available to tests - - 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 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. - */ - static 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 ( repo.substring( 1 ).equals( originalId ) ) - { - // explicitly exclude. Set result and stop processing. - result = false; - break; - } - } - // check for exact match - else if ( repo.equals( originalId ) ) - { - 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. - */ - static 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; - } - } - - -} diff --git a/maven-compat/src/main/java/org/apache/maven/repository/DefaultMirrorSelector.java b/maven-compat/src/main/java/org/apache/maven/repository/DefaultMirrorSelector.java new file mode 100644 index 0000000000..9cba5f8426 --- /dev/null +++ b/maven-compat/src/main/java/org/apache/maven/repository/DefaultMirrorSelector.java @@ -0,0 +1,142 @@ +package org.apache.maven.repository; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.List; + +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.settings.Mirror; +import org.codehaus.plexus.component.annotations.Component; + +@Component( role = MirrorSelector.class ) +public class DefaultMirrorSelector + implements MirrorSelector +{ + + private static final String WILDCARD = "*"; + + private static final String EXTERNAL_WILDCARD = "external:*"; + + public Mirror getMirror( ArtifactRepository repository, List mirrors ) + { + String repoId = repository.getId(); + + if ( repoId != null && mirrors != null ) + { + for ( Mirror mirror : mirrors ) + { + if ( repoId.equals( mirror.getMirrorOf() ) ) + { + return mirror; + } + } + + for ( Mirror mirror : mirrors ) + { + if ( matchPattern( repository, mirror.getMirrorOf() ) ) + { + return mirror; + } + } + } + + return null; + } + + /** + * 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. + */ + static 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 ( repo.substring( 1 ).equals( originalId ) ) + { + // explicitly exclude. Set result and stop processing. + result = false; + break; + } + } + // check for exact match + else if ( repo.equals( originalId ) ) + { + 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. + */ + static 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; + } + } + +} diff --git a/maven-compat/src/main/java/org/apache/maven/repository/LegacyRepositorySystem.java b/maven-compat/src/main/java/org/apache/maven/repository/LegacyRepositorySystem.java index 658efe70bc..06abc9d8a1 100644 --- a/maven-compat/src/main/java/org/apache/maven/repository/LegacyRepositorySystem.java +++ b/maven-compat/src/main/java/org/apache/maven/repository/LegacyRepositorySystem.java @@ -85,6 +85,9 @@ public class LegacyRepositorySystem @Requirement private PlexusContainer plexus; + @Requirement + private MirrorSelector mirrorSelector; + public Artifact createArtifact( String groupId, String artifactId, String version, String scope, String type ) { return artifactFactory.createArtifact( groupId, artifactId, version, scope, type ); @@ -456,28 +459,7 @@ public class LegacyRepositorySystem public Mirror getMirror( ArtifactRepository repository, List mirrors ) { - String repoId = repository.getId(); - - if ( repoId != null && mirrors != null ) - { - for ( Mirror mirror : mirrors ) - { - if ( repoId.equals( mirror.getMirrorOf() ) ) - { - return mirror; - } - } - - for ( Mirror mirror : mirrors ) - { - if ( DefaultMirrorBuilder.matchPattern( repository, mirror.getMirrorOf() ) ) - { - return mirror; - } - } - } - - return null; + return mirrorSelector.getMirror( repository, mirrors ); } public void injectMirror( List repositories, List mirrors ) diff --git a/maven-compat/src/main/java/org/apache/maven/repository/MirrorBuilder.java b/maven-compat/src/main/java/org/apache/maven/repository/MirrorSelector.java similarity index 61% rename from maven-compat/src/main/java/org/apache/maven/repository/MirrorBuilder.java rename to maven-compat/src/main/java/org/apache/maven/repository/MirrorSelector.java index 5e16e328e7..09fcb240c7 100644 --- a/maven-compat/src/main/java/org/apache/maven/repository/MirrorBuilder.java +++ b/maven-compat/src/main/java/org/apache/maven/repository/MirrorSelector.java @@ -22,15 +22,23 @@ package org.apache.maven.repository; import java.util.List; import org.apache.maven.artifact.repository.ArtifactRepository; -import org.apache.maven.artifact.repository.Authentication; +import org.apache.maven.settings.Mirror; -public interface MirrorBuilder +/** + * Handles the selection of mirrors for repositories. + * + * @author Benjamin Bentmann + */ +public interface MirrorSelector { - ArtifactRepository getMirror( ArtifactRepository repository ); - - List getMirrors( List repositories ); - - void addMirror( String id, String mirrorOf, String url, Authentication auth ); - - void clearMirrors(); + + /** + * Determines the mirror for the specified repository. + * + * @param repository The repository to determine the mirror for, must not be {@code null}. + * @param mirrors The available mirrors, may be {@code null}. + * @return The mirror specification for the repository or {@code null} if no mirror matched. + */ + Mirror getMirror( ArtifactRepository repository, List mirrors ); + } diff --git a/maven-compat/src/test/java/org/apache/maven/repository/MirrorProcessorTest.java b/maven-compat/src/test/java/org/apache/maven/repository/MirrorProcessorTest.java index 17d1938090..bb34c6ca97 100644 --- a/maven-compat/src/test/java/org/apache/maven/repository/MirrorProcessorTest.java +++ b/maven-compat/src/test/java/org/apache/maven/repository/MirrorProcessorTest.java @@ -19,195 +19,165 @@ package org.apache.maven.repository; * under the License. */ -import java.io.File; import java.util.Arrays; import java.util.List; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout; +import org.apache.maven.settings.Mirror; import org.codehaus.plexus.PlexusTestCase; public class MirrorProcessorTest extends PlexusTestCase { - private DefaultMirrorBuilder mirrorBuilder; + private DefaultMirrorSelector mirrorSelector; private ArtifactRepositoryFactory repositorySystem; protected void setUp() throws Exception { - mirrorBuilder = (DefaultMirrorBuilder) lookup( MirrorBuilder.class ); + mirrorSelector = (DefaultMirrorSelector) lookup( MirrorSelector.class ); repositorySystem = lookup( ArtifactRepositoryFactory.class ); - mirrorBuilder.clearMirrors(); } @Override protected void tearDown() throws Exception { - mirrorBuilder = null; - super.tearDown(); - } + mirrorSelector = null; + repositorySystem = null; - public void testAddMirrorWithNullRepositoryId() - { - mirrorBuilder.addMirror( null, "test", "http://www.nowhere.com/", null ); + super.tearDown(); } public void testExternalURL() { - assertTrue( DefaultMirrorBuilder.isExternalRepo( getRepo( "foo", "http://somehost" ) ) ); - assertTrue( DefaultMirrorBuilder.isExternalRepo( getRepo( "foo", "http://somehost:9090/somepath" ) ) ); - assertTrue( DefaultMirrorBuilder.isExternalRepo( getRepo( "foo", "ftp://somehost" ) ) ); - assertTrue( DefaultMirrorBuilder.isExternalRepo( getRepo( "foo", "http://192.168.101.1" ) ) ); - assertTrue( DefaultMirrorBuilder.isExternalRepo( getRepo( "foo", "http://" ) ) ); + assertTrue( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "http://somehost" ) ) ); + assertTrue( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "http://somehost:9090/somepath" ) ) ); + assertTrue( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "ftp://somehost" ) ) ); + assertTrue( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "http://192.168.101.1" ) ) ); + assertTrue( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "http://" ) ) ); // these are local - assertFalse( DefaultMirrorBuilder.isExternalRepo( getRepo( "foo", "http://localhost:8080" ) ) ); - assertFalse( DefaultMirrorBuilder.isExternalRepo( getRepo( "foo", "http://127.0.0.1:9090" ) ) ); - assertFalse( DefaultMirrorBuilder.isExternalRepo( getRepo( "foo", "file://localhost/somepath" ) ) ); - assertFalse( DefaultMirrorBuilder.isExternalRepo( getRepo( "foo", "file://localhost/D:/somepath" ) ) ); - assertFalse( DefaultMirrorBuilder.isExternalRepo( getRepo( "foo", "http://localhost" ) ) ); - assertFalse( DefaultMirrorBuilder.isExternalRepo( getRepo( "foo", "http://127.0.0.1" ) ) ); - assertFalse( DefaultMirrorBuilder.isExternalRepo( getRepo( "foo", "file:///somepath" ) ) ); - assertFalse( DefaultMirrorBuilder.isExternalRepo( getRepo( "foo", "file://D:/somepath" ) ) ); + assertFalse( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "http://localhost:8080" ) ) ); + assertFalse( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "http://127.0.0.1:9090" ) ) ); + assertFalse( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "file://localhost/somepath" ) ) ); + assertFalse( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "file://localhost/D:/somepath" ) ) ); + assertFalse( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "http://localhost" ) ) ); + assertFalse( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "http://127.0.0.1" ) ) ); + assertFalse( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "file:///somepath" ) ) ); + assertFalse( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "file://D:/somepath" ) ) ); // not a proper url so returns false; - assertFalse( DefaultMirrorBuilder.isExternalRepo( getRepo( "foo", "192.168.101.1" ) ) ); - assertFalse( DefaultMirrorBuilder.isExternalRepo( getRepo( "foo", "" ) ) ); + assertFalse( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "192.168.101.1" ) ) ); + assertFalse( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "" ) ) ); } public void testMirrorLookup() { - mirrorBuilder.addMirror( "a", "a", "http://a", null ); - mirrorBuilder.addMirror( "b", "b", "http://b", null ); + Mirror mirrorA = newMirror( "a", "a", "http://a" ); + Mirror mirrorB = newMirror( "b", "b", "http://b" ); - ArtifactRepository repo = null; - repo = mirrorBuilder.getMirrorRepository( getRepo( "a", "http://a.a" ) ); - assertEquals( "http://a", repo.getUrl() ); + List mirrors = Arrays.asList( mirrorA, mirrorB ); - repo = mirrorBuilder.getMirrorRepository( getRepo( "b", "http://a.a" ) ); - assertEquals( "http://b", repo.getUrl() ); + assertSame( mirrorA, mirrorSelector.getMirror( getRepo( "a", "http://a.a" ), mirrors ) ); - repo = mirrorBuilder.getMirrorRepository( getRepo( "c", "http://c.c" ) ); - assertEquals( "http://c.c", repo.getUrl() ); + assertSame( mirrorB, mirrorSelector.getMirror( getRepo( "b", "http://a.a" ), mirrors ) ); + assertNull( mirrorSelector.getMirror( getRepo( "c", "http://c.c" ), mirrors ) ); } public void testMirrorWildcardLookup() { - mirrorBuilder.addMirror( "a", "a", "http://a", null ); - mirrorBuilder.addMirror( "b", "b", "http://b", null ); - mirrorBuilder.addMirror( "c", "*", "http://wildcard", null ); + Mirror mirrorA = newMirror( "a", "a", "http://a" ); + Mirror mirrorB = newMirror( "b", "b", "http://b" ); + Mirror mirrorC = newMirror( "c", "*", "http://wildcard" ); - ArtifactRepository repo = null; - repo = mirrorBuilder.getMirrorRepository( getRepo( "a", "http://a.a" ) ); - assertEquals( "http://a", repo.getUrl() ); + List mirrors = Arrays.asList( mirrorA, mirrorB, mirrorC ); - repo = mirrorBuilder.getMirrorRepository( getRepo( "b", "http://a.a" ) ); - assertEquals( "http://b", repo.getUrl() ); + assertSame( mirrorA, mirrorSelector.getMirror( getRepo( "a", "http://a.a" ), mirrors ) ); - repo = mirrorBuilder.getMirrorRepository( getRepo( "c", "http://c.c" ) ); - assertEquals( "http://wildcard", repo.getUrl() ); + assertSame( mirrorB, mirrorSelector.getMirror( getRepo( "b", "http://a.a" ), mirrors ) ); + assertSame( mirrorC, mirrorSelector.getMirror( getRepo( "c", "http://c.c" ), mirrors ) ); } public void testMirrorStopOnFirstMatch() { - //exact matches win first - mirrorBuilder.addMirror( "a2", "a,b", "http://a2", null ); - mirrorBuilder.addMirror( "a", "a", "http://a", null ); - //make sure repeated entries are skipped - mirrorBuilder.addMirror( "a", "a", "http://a3", null ); + // exact matches win first + Mirror mirrorA2 = newMirror( "a2", "a,b", "http://a2" ); + Mirror mirrorA = newMirror( "a", "a", "http://a" ); + // make sure repeated entries are skipped + Mirror mirrorA3 = newMirror( "a", "a", "http://a3" ); - mirrorBuilder.addMirror( "b", "b", "http://b", null ); - mirrorBuilder.addMirror( "c", "d,e", "http://de", null ); - mirrorBuilder.addMirror( "c", "*", "http://wildcard", null ); - mirrorBuilder.addMirror( "c", "e,f", "http://ef", null ); + Mirror mirrorB = newMirror( "b", "b", "http://b" ); + Mirror mirrorC = newMirror( "c", "d,e", "http://de" ); + Mirror mirrorC2 = newMirror( "c", "*", "http://wildcard" ); + Mirror mirrorC3 = newMirror( "c", "e,f", "http://ef" ); - ArtifactRepository repo = null; - repo = mirrorBuilder.getMirrorRepository( getRepo( "a", "http://a.a" ) ); - assertEquals( "http://a", repo.getUrl() ); + List mirrors = Arrays.asList( mirrorA2, mirrorA, mirrorA3, mirrorB, mirrorC, mirrorC2, mirrorC3 ); - repo = mirrorBuilder.getMirrorRepository( getRepo( "b", "http://a.a" ) ); - assertEquals( "http://b", repo.getUrl() ); + assertSame( mirrorA, mirrorSelector.getMirror( getRepo( "a", "http://a.a" ), mirrors ) ); - repo = mirrorBuilder.getMirrorRepository( getRepo( "c", "http://c.c" ) ); - assertEquals( "http://wildcard", repo.getUrl() ); + assertSame( mirrorB, mirrorSelector.getMirror( getRepo( "b", "http://a.a" ), mirrors ) ); - repo = mirrorBuilder.getMirrorRepository( getRepo( "d", "http://d" ) ); - assertEquals( "http://de", repo.getUrl() ); + assertSame( mirrorC2, mirrorSelector.getMirror( getRepo( "c", "http://c.c" ), mirrors ) ); - repo = mirrorBuilder.getMirrorRepository( getRepo( "e", "http://e" ) ); - assertEquals( "http://de", repo.getUrl() ); + assertSame( mirrorC, mirrorSelector.getMirror( getRepo( "d", "http://d" ), mirrors ) ); - repo = mirrorBuilder.getMirrorRepository( getRepo( "f", "http://f" ) ); - assertEquals( "http://wildcard", repo.getUrl() ); + assertSame( mirrorC, mirrorSelector.getMirror( getRepo( "e", "http://e" ), mirrors ) ); + assertSame( mirrorC2, mirrorSelector.getMirror( getRepo( "f", "http://f" ), mirrors ) ); } - public void testPatterns() { - assertTrue( DefaultMirrorBuilder.matchPattern( getRepo( "a" ), "*" ) ); - assertTrue( DefaultMirrorBuilder.matchPattern( getRepo( "a" ), "*," ) ); - assertTrue( DefaultMirrorBuilder.matchPattern( getRepo( "a" ), ",*," ) ); - assertTrue( DefaultMirrorBuilder.matchPattern( getRepo( "a" ), "*," ) ); + assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a" ), "*" ) ); + assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a" ), "*," ) ); + assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a" ), ",*," ) ); + assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a" ), "*," ) ); - assertTrue( DefaultMirrorBuilder.matchPattern( getRepo( "a" ), "a" ) ); - assertTrue( DefaultMirrorBuilder.matchPattern( getRepo( "a" ), "a," ) ); - assertTrue( DefaultMirrorBuilder.matchPattern( getRepo( "a" ), ",a," ) ); - assertTrue( DefaultMirrorBuilder.matchPattern( getRepo( "a" ), "a," ) ); + assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a" ), "a" ) ); + assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a" ), "a," ) ); + assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a" ), ",a," ) ); + assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a" ), "a," ) ); - assertFalse( DefaultMirrorBuilder.matchPattern( getRepo( "b" ), "a" ) ); - assertFalse( DefaultMirrorBuilder.matchPattern( getRepo( "b" ), "a," ) ); - assertFalse( DefaultMirrorBuilder.matchPattern( getRepo( "b" ), ",a" ) ); - assertFalse( DefaultMirrorBuilder.matchPattern( getRepo( "b" ), ",a," ) ); + assertFalse( DefaultMirrorSelector.matchPattern( getRepo( "b" ), "a" ) ); + assertFalse( DefaultMirrorSelector.matchPattern( getRepo( "b" ), "a," ) ); + assertFalse( DefaultMirrorSelector.matchPattern( getRepo( "b" ), ",a" ) ); + assertFalse( DefaultMirrorSelector.matchPattern( getRepo( "b" ), ",a," ) ); - assertTrue( DefaultMirrorBuilder.matchPattern( getRepo( "a" ), "a,b" ) ); - assertTrue( DefaultMirrorBuilder.matchPattern( getRepo( "b" ), "a,b" ) ); + assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a" ), "a,b" ) ); + assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "b" ), "a,b" ) ); - assertFalse( DefaultMirrorBuilder.matchPattern( getRepo( "c" ), "a,b" ) ); + assertFalse( DefaultMirrorSelector.matchPattern( getRepo( "c" ), "a,b" ) ); - assertTrue( DefaultMirrorBuilder.matchPattern( getRepo( "a" ), "*" ) ); - assertTrue( DefaultMirrorBuilder.matchPattern( getRepo( "a" ), "*,b" ) ); - assertTrue( DefaultMirrorBuilder.matchPattern( getRepo( "a" ), "*,!b" ) ); + assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a" ), "*" ) ); + assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a" ), "*,b" ) ); + assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a" ), "*,!b" ) ); - assertFalse( DefaultMirrorBuilder.matchPattern( getRepo( "a" ), "*,!a" ) ); - assertFalse( DefaultMirrorBuilder.matchPattern( getRepo( "a" ), "!a,*" ) ); + assertFalse( DefaultMirrorSelector.matchPattern( getRepo( "a" ), "*,!a" ) ); + assertFalse( DefaultMirrorSelector.matchPattern( getRepo( "a" ), "!a,*" ) ); - assertTrue( DefaultMirrorBuilder.matchPattern( getRepo( "c" ), "*,!a" ) ); - assertTrue( DefaultMirrorBuilder.matchPattern( getRepo( "c" ), "!a,*" ) ); + assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "c" ), "*,!a" ) ); + assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "c" ), "!a,*" ) ); - assertFalse( DefaultMirrorBuilder.matchPattern( getRepo( "c" ), "!a,!c" ) ); - assertFalse( DefaultMirrorBuilder.matchPattern( getRepo( "d" ), "!a,!c*" ) ); + assertFalse( DefaultMirrorSelector.matchPattern( getRepo( "c" ), "!a,!c" ) ); + assertFalse( DefaultMirrorSelector.matchPattern( getRepo( "d" ), "!a,!c*" ) ); } public void testPatternsWithExternal() { - assertTrue( DefaultMirrorBuilder.matchPattern( getRepo( "a", "http://localhost" ), "*" ) ); - assertFalse( DefaultMirrorBuilder.matchPattern( getRepo( "a", "http://localhost" ), "external:*" ) ); + assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a", "http://localhost" ), "*" ) ); + assertFalse( DefaultMirrorSelector.matchPattern( getRepo( "a", "http://localhost" ), "external:*" ) ); - assertTrue( DefaultMirrorBuilder.matchPattern( getRepo( "a", "http://localhost" ), "external:*,a" ) ); - assertFalse( DefaultMirrorBuilder.matchPattern( getRepo( "a", "http://localhost" ), "external:*,!a" ) ); - assertTrue( DefaultMirrorBuilder.matchPattern( getRepo( "a", "http://localhost" ), "a,external:*" ) ); - assertFalse( DefaultMirrorBuilder.matchPattern( getRepo( "a", "http://localhost" ), "!a,external:*" ) ); + assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a", "http://localhost" ), "external:*,a" ) ); + assertFalse( DefaultMirrorSelector.matchPattern( getRepo( "a", "http://localhost" ), "external:*,!a" ) ); + assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a", "http://localhost" ), "a,external:*" ) ); + assertFalse( DefaultMirrorSelector.matchPattern( getRepo( "a", "http://localhost" ), "!a,external:*" ) ); - assertFalse( DefaultMirrorBuilder.matchPattern( getRepo( "c", "http://localhost" ), "!a,external:*" ) ); - assertTrue( DefaultMirrorBuilder.matchPattern( getRepo( "c", "http://somehost" ), "!a,external:*" ) ); - } - - public void testMirrorProperUrlAndProtocolAndBasedir() - { - mirrorBuilder.addMirror( "mirror-id", "central", "file:///tmp", null ); - - List repos = Arrays.asList( getRepo( "central", "http://repo1.maven.org" ) ); - repos = mirrorBuilder.getMirrors( repos ); - - ArtifactRepository repo = repos.get( 0 ); - assertEquals( "file:///tmp", repo.getUrl() ); - assertEquals( "file", repo.getProtocol() ); - assertEquals( File.separator + "tmp", repo.getBasedir() ); + assertFalse( DefaultMirrorSelector.matchPattern( getRepo( "c", "http://localhost" ), "!a,external:*" ) ); + assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "c", "http://somehost" ), "!a,external:*" ) ); } /** @@ -232,4 +202,16 @@ public class MirrorProcessorTest { return getRepo( id, "http://something" ); } + + private Mirror newMirror( String id, String mirrorOf, String url ) + { + Mirror mirror = new Mirror(); + + mirror.setId( id ); + mirror.setMirrorOf( mirrorOf ); + mirror.setUrl( url ); + + return mirror; + } + }