add some more tests

PR: MRM-9

git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@349600 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Porter 2005-11-29 03:11:23 +00:00
parent 7f0762951b
commit 8e757bd2a0
5 changed files with 89 additions and 44 deletions

View File

@ -18,6 +18,7 @@ package org.apache.maven.repository.discovery;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.util.DirectoryScanner;
import org.codehaus.plexus.util.FileUtils;
import java.io.File;
import java.util.ArrayList;
@ -55,24 +56,18 @@ public abstract class AbstractArtifactDiscoverer
*/
protected String[] scanForArtifactPaths( File repositoryBase, String blacklistedPatterns )
{
String[] blacklisted;
List allExcludes = new ArrayList();
allExcludes.addAll( FileUtils.getDefaultExcludesAsList() );
allExcludes.addAll( Arrays.asList( STANDARD_DISCOVERY_EXCLUDES ) );
if ( blacklistedPatterns != null && blacklistedPatterns.length() > 0 )
{
blacklisted = blacklistedPatterns.split( "," );
allExcludes.addAll( Arrays.asList( blacklistedPatterns.split( "," ) ) );
}
else
{
blacklisted = EMPTY_STRING_ARRAY;
}
String[] allExcludes = new String[STANDARD_DISCOVERY_EXCLUDES.length + blacklisted.length];
System.arraycopy( STANDARD_DISCOVERY_EXCLUDES, 0, allExcludes, 0, STANDARD_DISCOVERY_EXCLUDES.length );
System.arraycopy( blacklisted, 0, allExcludes, STANDARD_DISCOVERY_EXCLUDES.length, blacklisted.length );
DirectoryScanner scanner = new DirectoryScanner();
scanner.setBasedir( repositoryBase );
scanner.setExcludes( allExcludes );
scanner.setExcludes( (String[]) allExcludes.toArray( EMPTY_STRING_ARRAY ) );
scanner.scan();

View File

@ -18,11 +18,11 @@ package org.apache.maven.repository.discovery;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.codehaus.plexus.util.StringUtils;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
@ -93,27 +93,12 @@ public class DefaultArtifactDiscoverer
pathParts.remove( 0 );
// the remaining are the groupId.
StringBuffer groupBuffer = new StringBuffer();
Collections.reverse( pathParts );
String groupId = StringUtils.join( pathParts.iterator(), "." );
boolean firstPart = true;
for ( Iterator it = pathParts.iterator(); it.hasNext(); )
{
String part = (String) it.next();
result = artifactFactory.createArtifact( groupId, artifactId, version, Artifact.SCOPE_RUNTIME, "jar" );
groupBuffer.append( part );
if ( firstPart )
{
firstPart = false;
}
else if ( it.hasNext() )
{
groupBuffer.append( "." );
}
}
result = artifactFactory.createArtifact( groupBuffer.toString(), artifactId, version, Artifact.SCOPE_RUNTIME,
"jar" );
result.setFile( new File( path ) );
return result;
}

View File

@ -16,6 +16,8 @@ package org.apache.maven.repository.discovery;
* limitations under the License.
*/
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.codehaus.plexus.PlexusTestCase;
import java.io.File;
@ -33,6 +35,8 @@ public class DefaultArtifactDiscovererTest
{
private ArtifactDiscoverer discoverer;
private ArtifactFactory factory;
private File repositoryLocation;
protected void setUp()
@ -42,29 +46,90 @@ public class DefaultArtifactDiscovererTest
discoverer = (ArtifactDiscoverer) lookup( ArtifactDiscoverer.ROLE, "default" );
factory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
repositoryLocation = getTestFile( "src/test/repository" );
}
public void testDefaultExcludes()
{
List artifacts = discoverer.discoverArtifacts( repositoryLocation, null, false );
assertNotNull( "Check artifacts returned", artifacts );
assertTrue( "Check no artifacts returned", artifacts.isEmpty() );
assertNotNull( "Check artifacts not null", artifacts );
boolean found = false;
for ( Iterator i = discoverer.getExcludedPathsIterator(); i.hasNext(); )
for ( Iterator i = discoverer.getExcludedPathsIterator(); i.hasNext() && !found; )
{
String path = (String) i.next();
if ( !path.startsWith( ".svn" ) )
{
assertEquals( "Check the excluded path", "KEYS", path );
if ( found )
{
fail( "KEYS entry found twice" );
}
found = true;
}
found = path.indexOf( ".svn" ) >= 0;
}
assertTrue( "Check exclusion was found", found );
for ( Iterator i = artifacts.iterator(); i.hasNext(); )
{
Artifact a = (Artifact) i.next();
assertFalse( "Check not .svn", a.getFile().getPath().indexOf( ".svn" ) >= 0 );
}
}
public void testStandardExcludes()
{
List artifacts = discoverer.discoverArtifacts( repositoryLocation, null, false );
assertNotNull( "Check artifacts not null", artifacts );
boolean found = false;
for ( Iterator i = discoverer.getExcludedPathsIterator(); i.hasNext() && !found; )
{
String path = (String) i.next();
found = path.equals( "KEYS" );
}
assertTrue( "Check exclusion was found", found );
for ( Iterator i = artifacts.iterator(); i.hasNext(); )
{
Artifact a = (Artifact) i.next();
assertFalse( "Check not KEYS", a.getFile().getName().equals( "KEYS" ) );
}
}
public void testBlacklistedExclude()
{
List artifacts = discoverer.discoverArtifacts( repositoryLocation, "javax/**", false );
assertNotNull( "Check artifacts not null", artifacts );
boolean found = false;
for ( Iterator i = discoverer.getExcludedPathsIterator(); i.hasNext() && !found; )
{
String path = (String) i.next();
found = path.replace( '\\', '/' ).equals( "javax/sql/jdbc/2.0/jdbc-2.0.jar" );
}
assertTrue( "Check exclusion was found", found );
assertFalse( "Check jdbc not included", artifacts.contains( createArtifact( "javax.sql", "jdbc", "2.0" ) ) );
}
public void testSnapshotInclusion()
{
List artifacts = discoverer.discoverArtifacts( repositoryLocation, null, true );
assertNotNull( "Check artifacts not null", artifacts );
assertTrue( "Check normal included", artifacts.contains( createArtifact( "javax.sql", "jdbc", "2.0" ) ) );
assertTrue( "Check snapshot included",
artifacts.contains( createArtifact( "org.apache.maven", "test", "1.0-SNAPSHOT" ) ) );
}
public void testSnapshotExclusion()
{
List artifacts = discoverer.discoverArtifacts( repositoryLocation, null, false );
assertNotNull( "Check artifacts not null", artifacts );
assertTrue( "Check normal included", artifacts.contains( createArtifact( "javax.sql", "jdbc", "2.0" ) ) );
assertFalse( "Check snapshot included",
artifacts.contains( createArtifact( "org.apache.maven", "test", "1.0-SNAPSHOT" ) ) );
}
private Artifact createArtifact( String groupId, String artifactId, String version )
{
return factory.createArtifact( groupId, artifactId, version, null, "jar" );
}
}