mirror of https://github.com/apache/maven.git
o Refactored code
git-svn-id: https://svn.apache.org/repos/asf/maven/maven-3/trunk@818412 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
3412df6ea7
commit
4648efa59b
|
@ -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<String, ArtifactRepository> mirrors = new LinkedHashMap<String, ArtifactRepository>();
|
|
||||||
|
|
||||||
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 <id/> 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<String> 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<ArtifactRepository> getMirrors( List<ArtifactRepository> 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
|
@ -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<Mirror> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -85,6 +85,9 @@ public class LegacyRepositorySystem
|
||||||
@Requirement
|
@Requirement
|
||||||
private PlexusContainer plexus;
|
private PlexusContainer plexus;
|
||||||
|
|
||||||
|
@Requirement
|
||||||
|
private MirrorSelector mirrorSelector;
|
||||||
|
|
||||||
public Artifact createArtifact( String groupId, String artifactId, String version, String scope, String type )
|
public Artifact createArtifact( String groupId, String artifactId, String version, String scope, String type )
|
||||||
{
|
{
|
||||||
return artifactFactory.createArtifact( groupId, artifactId, version, scope, type );
|
return artifactFactory.createArtifact( groupId, artifactId, version, scope, type );
|
||||||
|
@ -456,28 +459,7 @@ public class LegacyRepositorySystem
|
||||||
|
|
||||||
public Mirror getMirror( ArtifactRepository repository, List<Mirror> mirrors )
|
public Mirror getMirror( ArtifactRepository repository, List<Mirror> mirrors )
|
||||||
{
|
{
|
||||||
String repoId = repository.getId();
|
return mirrorSelector.getMirror( repository, mirrors );
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void injectMirror( List<ArtifactRepository> repositories, List<Mirror> mirrors )
|
public void injectMirror( List<ArtifactRepository> repositories, List<Mirror> mirrors )
|
||||||
|
|
|
@ -22,15 +22,23 @@ package org.apache.maven.repository;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.apache.maven.artifact.repository.ArtifactRepository;
|
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<ArtifactRepository> getMirrors( List<ArtifactRepository> repositories );
|
* Determines the mirror for the specified repository.
|
||||||
|
*
|
||||||
void addMirror( String id, String mirrorOf, String url, Authentication auth );
|
* @param repository The repository to determine the mirror for, must not be {@code null}.
|
||||||
|
* @param mirrors The available mirrors, may be {@code null}.
|
||||||
void clearMirrors();
|
* @return The mirror specification for the repository or {@code null} if no mirror matched.
|
||||||
|
*/
|
||||||
|
Mirror getMirror( ArtifactRepository repository, List<Mirror> mirrors );
|
||||||
|
|
||||||
}
|
}
|
|
@ -19,195 +19,165 @@ package org.apache.maven.repository;
|
||||||
* under the License.
|
* under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.apache.maven.artifact.repository.ArtifactRepository;
|
import org.apache.maven.artifact.repository.ArtifactRepository;
|
||||||
import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
|
import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
|
||||||
import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
|
import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
|
||||||
|
import org.apache.maven.settings.Mirror;
|
||||||
import org.codehaus.plexus.PlexusTestCase;
|
import org.codehaus.plexus.PlexusTestCase;
|
||||||
|
|
||||||
public class MirrorProcessorTest
|
public class MirrorProcessorTest
|
||||||
extends PlexusTestCase
|
extends PlexusTestCase
|
||||||
{
|
{
|
||||||
private DefaultMirrorBuilder mirrorBuilder;
|
private DefaultMirrorSelector mirrorSelector;
|
||||||
private ArtifactRepositoryFactory repositorySystem;
|
private ArtifactRepositoryFactory repositorySystem;
|
||||||
|
|
||||||
protected void setUp()
|
protected void setUp()
|
||||||
throws Exception
|
throws Exception
|
||||||
{
|
{
|
||||||
mirrorBuilder = (DefaultMirrorBuilder) lookup( MirrorBuilder.class );
|
mirrorSelector = (DefaultMirrorSelector) lookup( MirrorSelector.class );
|
||||||
repositorySystem = lookup( ArtifactRepositoryFactory.class );
|
repositorySystem = lookup( ArtifactRepositoryFactory.class );
|
||||||
mirrorBuilder.clearMirrors();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void tearDown()
|
protected void tearDown()
|
||||||
throws Exception
|
throws Exception
|
||||||
{
|
{
|
||||||
mirrorBuilder = null;
|
mirrorSelector = null;
|
||||||
super.tearDown();
|
repositorySystem = null;
|
||||||
}
|
|
||||||
|
|
||||||
public void testAddMirrorWithNullRepositoryId()
|
super.tearDown();
|
||||||
{
|
|
||||||
mirrorBuilder.addMirror( null, "test", "http://www.nowhere.com/", null );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testExternalURL()
|
public void testExternalURL()
|
||||||
{
|
{
|
||||||
assertTrue( DefaultMirrorBuilder.isExternalRepo( getRepo( "foo", "http://somehost" ) ) );
|
assertTrue( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "http://somehost" ) ) );
|
||||||
assertTrue( DefaultMirrorBuilder.isExternalRepo( getRepo( "foo", "http://somehost:9090/somepath" ) ) );
|
assertTrue( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "http://somehost:9090/somepath" ) ) );
|
||||||
assertTrue( DefaultMirrorBuilder.isExternalRepo( getRepo( "foo", "ftp://somehost" ) ) );
|
assertTrue( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "ftp://somehost" ) ) );
|
||||||
assertTrue( DefaultMirrorBuilder.isExternalRepo( getRepo( "foo", "http://192.168.101.1" ) ) );
|
assertTrue( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "http://192.168.101.1" ) ) );
|
||||||
assertTrue( DefaultMirrorBuilder.isExternalRepo( getRepo( "foo", "http://" ) ) );
|
assertTrue( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "http://" ) ) );
|
||||||
// these are local
|
// these are local
|
||||||
assertFalse( DefaultMirrorBuilder.isExternalRepo( getRepo( "foo", "http://localhost:8080" ) ) );
|
assertFalse( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "http://localhost:8080" ) ) );
|
||||||
assertFalse( DefaultMirrorBuilder.isExternalRepo( getRepo( "foo", "http://127.0.0.1:9090" ) ) );
|
assertFalse( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "http://127.0.0.1:9090" ) ) );
|
||||||
assertFalse( DefaultMirrorBuilder.isExternalRepo( getRepo( "foo", "file://localhost/somepath" ) ) );
|
assertFalse( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "file://localhost/somepath" ) ) );
|
||||||
assertFalse( DefaultMirrorBuilder.isExternalRepo( getRepo( "foo", "file://localhost/D:/somepath" ) ) );
|
assertFalse( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "file://localhost/D:/somepath" ) ) );
|
||||||
assertFalse( DefaultMirrorBuilder.isExternalRepo( getRepo( "foo", "http://localhost" ) ) );
|
assertFalse( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "http://localhost" ) ) );
|
||||||
assertFalse( DefaultMirrorBuilder.isExternalRepo( getRepo( "foo", "http://127.0.0.1" ) ) );
|
assertFalse( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "http://127.0.0.1" ) ) );
|
||||||
assertFalse( DefaultMirrorBuilder.isExternalRepo( getRepo( "foo", "file:///somepath" ) ) );
|
assertFalse( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "file:///somepath" ) ) );
|
||||||
assertFalse( DefaultMirrorBuilder.isExternalRepo( getRepo( "foo", "file://D:/somepath" ) ) );
|
assertFalse( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "file://D:/somepath" ) ) );
|
||||||
|
|
||||||
// not a proper url so returns false;
|
// not a proper url so returns false;
|
||||||
assertFalse( DefaultMirrorBuilder.isExternalRepo( getRepo( "foo", "192.168.101.1" ) ) );
|
assertFalse( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "192.168.101.1" ) ) );
|
||||||
assertFalse( DefaultMirrorBuilder.isExternalRepo( getRepo( "foo", "" ) ) );
|
assertFalse( DefaultMirrorSelector.isExternalRepo( getRepo( "foo", "" ) ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testMirrorLookup()
|
public void testMirrorLookup()
|
||||||
{
|
{
|
||||||
mirrorBuilder.addMirror( "a", "a", "http://a", null );
|
Mirror mirrorA = newMirror( "a", "a", "http://a" );
|
||||||
mirrorBuilder.addMirror( "b", "b", "http://b", null );
|
Mirror mirrorB = newMirror( "b", "b", "http://b" );
|
||||||
|
|
||||||
ArtifactRepository repo = null;
|
List<Mirror> mirrors = Arrays.asList( mirrorA, mirrorB );
|
||||||
repo = mirrorBuilder.getMirrorRepository( getRepo( "a", "http://a.a" ) );
|
|
||||||
assertEquals( "http://a", repo.getUrl() );
|
|
||||||
|
|
||||||
repo = mirrorBuilder.getMirrorRepository( getRepo( "b", "http://a.a" ) );
|
assertSame( mirrorA, mirrorSelector.getMirror( getRepo( "a", "http://a.a" ), mirrors ) );
|
||||||
assertEquals( "http://b", repo.getUrl() );
|
|
||||||
|
|
||||||
repo = mirrorBuilder.getMirrorRepository( getRepo( "c", "http://c.c" ) );
|
assertSame( mirrorB, mirrorSelector.getMirror( getRepo( "b", "http://a.a" ), mirrors ) );
|
||||||
assertEquals( "http://c.c", repo.getUrl() );
|
|
||||||
|
|
||||||
|
assertNull( mirrorSelector.getMirror( getRepo( "c", "http://c.c" ), mirrors ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testMirrorWildcardLookup()
|
public void testMirrorWildcardLookup()
|
||||||
{
|
{
|
||||||
mirrorBuilder.addMirror( "a", "a", "http://a", null );
|
Mirror mirrorA = newMirror( "a", "a", "http://a" );
|
||||||
mirrorBuilder.addMirror( "b", "b", "http://b", null );
|
Mirror mirrorB = newMirror( "b", "b", "http://b" );
|
||||||
mirrorBuilder.addMirror( "c", "*", "http://wildcard", null );
|
Mirror mirrorC = newMirror( "c", "*", "http://wildcard" );
|
||||||
|
|
||||||
ArtifactRepository repo = null;
|
List<Mirror> mirrors = Arrays.asList( mirrorA, mirrorB, mirrorC );
|
||||||
repo = mirrorBuilder.getMirrorRepository( getRepo( "a", "http://a.a" ) );
|
|
||||||
assertEquals( "http://a", repo.getUrl() );
|
|
||||||
|
|
||||||
repo = mirrorBuilder.getMirrorRepository( getRepo( "b", "http://a.a" ) );
|
assertSame( mirrorA, mirrorSelector.getMirror( getRepo( "a", "http://a.a" ), mirrors ) );
|
||||||
assertEquals( "http://b", repo.getUrl() );
|
|
||||||
|
|
||||||
repo = mirrorBuilder.getMirrorRepository( getRepo( "c", "http://c.c" ) );
|
assertSame( mirrorB, mirrorSelector.getMirror( getRepo( "b", "http://a.a" ), mirrors ) );
|
||||||
assertEquals( "http://wildcard", repo.getUrl() );
|
|
||||||
|
|
||||||
|
assertSame( mirrorC, mirrorSelector.getMirror( getRepo( "c", "http://c.c" ), mirrors ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testMirrorStopOnFirstMatch()
|
public void testMirrorStopOnFirstMatch()
|
||||||
{
|
{
|
||||||
//exact matches win first
|
// exact matches win first
|
||||||
mirrorBuilder.addMirror( "a2", "a,b", "http://a2", null );
|
Mirror mirrorA2 = newMirror( "a2", "a,b", "http://a2" );
|
||||||
mirrorBuilder.addMirror( "a", "a", "http://a", null );
|
Mirror mirrorA = newMirror( "a", "a", "http://a" );
|
||||||
//make sure repeated entries are skipped
|
// make sure repeated entries are skipped
|
||||||
mirrorBuilder.addMirror( "a", "a", "http://a3", null );
|
Mirror mirrorA3 = newMirror( "a", "a", "http://a3" );
|
||||||
|
|
||||||
mirrorBuilder.addMirror( "b", "b", "http://b", null );
|
Mirror mirrorB = newMirror( "b", "b", "http://b" );
|
||||||
mirrorBuilder.addMirror( "c", "d,e", "http://de", null );
|
Mirror mirrorC = newMirror( "c", "d,e", "http://de" );
|
||||||
mirrorBuilder.addMirror( "c", "*", "http://wildcard", null );
|
Mirror mirrorC2 = newMirror( "c", "*", "http://wildcard" );
|
||||||
mirrorBuilder.addMirror( "c", "e,f", "http://ef", null );
|
Mirror mirrorC3 = newMirror( "c", "e,f", "http://ef" );
|
||||||
|
|
||||||
ArtifactRepository repo = null;
|
List<Mirror> mirrors = Arrays.asList( mirrorA2, mirrorA, mirrorA3, mirrorB, mirrorC, mirrorC2, mirrorC3 );
|
||||||
repo = mirrorBuilder.getMirrorRepository( getRepo( "a", "http://a.a" ) );
|
|
||||||
assertEquals( "http://a", repo.getUrl() );
|
|
||||||
|
|
||||||
repo = mirrorBuilder.getMirrorRepository( getRepo( "b", "http://a.a" ) );
|
assertSame( mirrorA, mirrorSelector.getMirror( getRepo( "a", "http://a.a" ), mirrors ) );
|
||||||
assertEquals( "http://b", repo.getUrl() );
|
|
||||||
|
|
||||||
repo = mirrorBuilder.getMirrorRepository( getRepo( "c", "http://c.c" ) );
|
assertSame( mirrorB, mirrorSelector.getMirror( getRepo( "b", "http://a.a" ), mirrors ) );
|
||||||
assertEquals( "http://wildcard", repo.getUrl() );
|
|
||||||
|
|
||||||
repo = mirrorBuilder.getMirrorRepository( getRepo( "d", "http://d" ) );
|
assertSame( mirrorC2, mirrorSelector.getMirror( getRepo( "c", "http://c.c" ), mirrors ) );
|
||||||
assertEquals( "http://de", repo.getUrl() );
|
|
||||||
|
|
||||||
repo = mirrorBuilder.getMirrorRepository( getRepo( "e", "http://e" ) );
|
assertSame( mirrorC, mirrorSelector.getMirror( getRepo( "d", "http://d" ), mirrors ) );
|
||||||
assertEquals( "http://de", repo.getUrl() );
|
|
||||||
|
|
||||||
repo = mirrorBuilder.getMirrorRepository( getRepo( "f", "http://f" ) );
|
assertSame( mirrorC, mirrorSelector.getMirror( getRepo( "e", "http://e" ), mirrors ) );
|
||||||
assertEquals( "http://wildcard", repo.getUrl() );
|
|
||||||
|
|
||||||
|
assertSame( mirrorC2, mirrorSelector.getMirror( getRepo( "f", "http://f" ), mirrors ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void testPatterns()
|
public void testPatterns()
|
||||||
{
|
{
|
||||||
assertTrue( DefaultMirrorBuilder.matchPattern( getRepo( "a" ), "*" ) );
|
assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a" ), "*" ) );
|
||||||
assertTrue( DefaultMirrorBuilder.matchPattern( getRepo( "a" ), "*," ) );
|
assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a" ), "*," ) );
|
||||||
assertTrue( DefaultMirrorBuilder.matchPattern( getRepo( "a" ), ",*," ) );
|
assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a" ), ",*," ) );
|
||||||
assertTrue( DefaultMirrorBuilder.matchPattern( getRepo( "a" ), "*," ) );
|
assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a" ), "*," ) );
|
||||||
|
|
||||||
assertTrue( DefaultMirrorBuilder.matchPattern( getRepo( "a" ), "a" ) );
|
assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a" ), "a" ) );
|
||||||
assertTrue( DefaultMirrorBuilder.matchPattern( getRepo( "a" ), "a," ) );
|
assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a" ), "a," ) );
|
||||||
assertTrue( DefaultMirrorBuilder.matchPattern( getRepo( "a" ), ",a," ) );
|
assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a" ), ",a," ) );
|
||||||
assertTrue( DefaultMirrorBuilder.matchPattern( getRepo( "a" ), "a," ) );
|
assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a" ), "a," ) );
|
||||||
|
|
||||||
assertFalse( DefaultMirrorBuilder.matchPattern( getRepo( "b" ), "a" ) );
|
assertFalse( DefaultMirrorSelector.matchPattern( getRepo( "b" ), "a" ) );
|
||||||
assertFalse( DefaultMirrorBuilder.matchPattern( getRepo( "b" ), "a," ) );
|
assertFalse( DefaultMirrorSelector.matchPattern( getRepo( "b" ), "a," ) );
|
||||||
assertFalse( DefaultMirrorBuilder.matchPattern( getRepo( "b" ), ",a" ) );
|
assertFalse( DefaultMirrorSelector.matchPattern( getRepo( "b" ), ",a" ) );
|
||||||
assertFalse( DefaultMirrorBuilder.matchPattern( getRepo( "b" ), ",a," ) );
|
assertFalse( DefaultMirrorSelector.matchPattern( getRepo( "b" ), ",a," ) );
|
||||||
|
|
||||||
assertTrue( DefaultMirrorBuilder.matchPattern( getRepo( "a" ), "a,b" ) );
|
assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a" ), "a,b" ) );
|
||||||
assertTrue( DefaultMirrorBuilder.matchPattern( getRepo( "b" ), "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( DefaultMirrorSelector.matchPattern( getRepo( "a" ), "*" ) );
|
||||||
assertTrue( DefaultMirrorBuilder.matchPattern( getRepo( "a" ), "*,b" ) );
|
assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a" ), "*,b" ) );
|
||||||
assertTrue( DefaultMirrorBuilder.matchPattern( getRepo( "a" ), "*,!b" ) );
|
assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a" ), "*,!b" ) );
|
||||||
|
|
||||||
assertFalse( DefaultMirrorBuilder.matchPattern( getRepo( "a" ), "*,!a" ) );
|
assertFalse( DefaultMirrorSelector.matchPattern( getRepo( "a" ), "*,!a" ) );
|
||||||
assertFalse( DefaultMirrorBuilder.matchPattern( getRepo( "a" ), "!a,*" ) );
|
assertFalse( DefaultMirrorSelector.matchPattern( getRepo( "a" ), "!a,*" ) );
|
||||||
|
|
||||||
assertTrue( DefaultMirrorBuilder.matchPattern( getRepo( "c" ), "*,!a" ) );
|
assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "c" ), "*,!a" ) );
|
||||||
assertTrue( DefaultMirrorBuilder.matchPattern( getRepo( "c" ), "!a,*" ) );
|
assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "c" ), "!a,*" ) );
|
||||||
|
|
||||||
assertFalse( DefaultMirrorBuilder.matchPattern( getRepo( "c" ), "!a,!c" ) );
|
assertFalse( DefaultMirrorSelector.matchPattern( getRepo( "c" ), "!a,!c" ) );
|
||||||
assertFalse( DefaultMirrorBuilder.matchPattern( getRepo( "d" ), "!a,!c*" ) );
|
assertFalse( DefaultMirrorSelector.matchPattern( getRepo( "d" ), "!a,!c*" ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testPatternsWithExternal()
|
public void testPatternsWithExternal()
|
||||||
{
|
{
|
||||||
assertTrue( DefaultMirrorBuilder.matchPattern( getRepo( "a", "http://localhost" ), "*" ) );
|
assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a", "http://localhost" ), "*" ) );
|
||||||
assertFalse( DefaultMirrorBuilder.matchPattern( getRepo( "a", "http://localhost" ), "external:*" ) );
|
assertFalse( DefaultMirrorSelector.matchPattern( getRepo( "a", "http://localhost" ), "external:*" ) );
|
||||||
|
|
||||||
assertTrue( DefaultMirrorBuilder.matchPattern( getRepo( "a", "http://localhost" ), "external:*,a" ) );
|
assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a", "http://localhost" ), "external:*,a" ) );
|
||||||
assertFalse( DefaultMirrorBuilder.matchPattern( getRepo( "a", "http://localhost" ), "external:*,!a" ) );
|
assertFalse( DefaultMirrorSelector.matchPattern( getRepo( "a", "http://localhost" ), "external:*,!a" ) );
|
||||||
assertTrue( DefaultMirrorBuilder.matchPattern( getRepo( "a", "http://localhost" ), "a,external:*" ) );
|
assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "a", "http://localhost" ), "a,external:*" ) );
|
||||||
assertFalse( DefaultMirrorBuilder.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:*" ) );
|
assertFalse( DefaultMirrorSelector.matchPattern( getRepo( "c", "http://localhost" ), "!a,external:*" ) );
|
||||||
assertTrue( DefaultMirrorBuilder.matchPattern( getRepo( "c", "http://somehost" ), "!a,external:*" ) );
|
assertTrue( DefaultMirrorSelector.matchPattern( getRepo( "c", "http://somehost" ), "!a,external:*" ) );
|
||||||
}
|
|
||||||
|
|
||||||
public void testMirrorProperUrlAndProtocolAndBasedir()
|
|
||||||
{
|
|
||||||
mirrorBuilder.addMirror( "mirror-id", "central", "file:///tmp", null );
|
|
||||||
|
|
||||||
List<ArtifactRepository> 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() );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -232,4 +202,16 @@ public class MirrorProcessorTest
|
||||||
{
|
{
|
||||||
return getRepo( id, "http://something" );
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue