]> source.dussan.org Git - archiva.git/commitdiff
rename discovery package for consistency
authorBrett Porter <brett@apache.org>
Fri, 1 Sep 2006 05:52:21 +0000 (05:52 +0000)
committerBrett Porter <brett@apache.org>
Fri, 1 Sep 2006 05:52:21 +0000 (05:52 +0000)
git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/trunk@439194 13f79535-47bb-0310-9956-ffa450edef68

32 files changed:
archiva-core/src/main/java/org/apache/maven/archiva/DefaultRepositoryManager.java
archiva-core/src/main/java/org/apache/maven/archiva/RepositoryManager.java
archiva-core/src/main/java/org/apache/maven/archiva/scheduler/IndexerTask.java
archiva-discoverer/src/main/java/org/apache/maven/archiva/discoverer/AbstractArtifactDiscoverer.java [new file with mode: 0644]
archiva-discoverer/src/main/java/org/apache/maven/archiva/discoverer/AbstractDiscoverer.java [new file with mode: 0644]
archiva-discoverer/src/main/java/org/apache/maven/archiva/discoverer/ArtifactDiscoverer.java [new file with mode: 0644]
archiva-discoverer/src/main/java/org/apache/maven/archiva/discoverer/DefaultArtifactDiscoverer.java [new file with mode: 0644]
archiva-discoverer/src/main/java/org/apache/maven/archiva/discoverer/DefaultMetadataDiscoverer.java [new file with mode: 0644]
archiva-discoverer/src/main/java/org/apache/maven/archiva/discoverer/Discoverer.java [new file with mode: 0644]
archiva-discoverer/src/main/java/org/apache/maven/archiva/discoverer/DiscovererException.java [new file with mode: 0644]
archiva-discoverer/src/main/java/org/apache/maven/archiva/discoverer/DiscovererPath.java [new file with mode: 0644]
archiva-discoverer/src/main/java/org/apache/maven/archiva/discoverer/LegacyArtifactDiscoverer.java [new file with mode: 0644]
archiva-discoverer/src/main/java/org/apache/maven/archiva/discoverer/MetadataDiscoverer.java [new file with mode: 0644]
archiva-discoverer/src/main/java/org/apache/maven/archiva/discovery/AbstractArtifactDiscoverer.java [deleted file]
archiva-discoverer/src/main/java/org/apache/maven/archiva/discovery/AbstractDiscoverer.java [deleted file]
archiva-discoverer/src/main/java/org/apache/maven/archiva/discovery/ArtifactDiscoverer.java [deleted file]
archiva-discoverer/src/main/java/org/apache/maven/archiva/discovery/DefaultArtifactDiscoverer.java [deleted file]
archiva-discoverer/src/main/java/org/apache/maven/archiva/discovery/DefaultMetadataDiscoverer.java [deleted file]
archiva-discoverer/src/main/java/org/apache/maven/archiva/discovery/Discoverer.java [deleted file]
archiva-discoverer/src/main/java/org/apache/maven/archiva/discovery/DiscovererException.java [deleted file]
archiva-discoverer/src/main/java/org/apache/maven/archiva/discovery/DiscovererPath.java [deleted file]
archiva-discoverer/src/main/java/org/apache/maven/archiva/discovery/LegacyArtifactDiscoverer.java [deleted file]
archiva-discoverer/src/main/java/org/apache/maven/archiva/discovery/MetadataDiscoverer.java [deleted file]
archiva-discoverer/src/test/java/org/apache/maven/archiva/discoverer/AbstractArtifactDiscovererTest.java [new file with mode: 0644]
archiva-discoverer/src/test/java/org/apache/maven/archiva/discoverer/DefaultArtifactDiscovererTest.java [new file with mode: 0644]
archiva-discoverer/src/test/java/org/apache/maven/archiva/discoverer/DefaultMetadataDiscovererTest.java [new file with mode: 0644]
archiva-discoverer/src/test/java/org/apache/maven/archiva/discoverer/LegacyArtifactDiscovererTest.java [new file with mode: 0644]
archiva-discoverer/src/test/java/org/apache/maven/archiva/discovery/AbstractArtifactDiscovererTest.java [deleted file]
archiva-discoverer/src/test/java/org/apache/maven/archiva/discovery/DefaultArtifactDiscovererTest.java [deleted file]
archiva-discoverer/src/test/java/org/apache/maven/archiva/discovery/DefaultMetadataDiscovererTest.java [deleted file]
archiva-discoverer/src/test/java/org/apache/maven/archiva/discovery/LegacyArtifactDiscovererTest.java [deleted file]
archiva-proxy/src/main/java/org/apache/maven/archiva/proxy/DefaultProxyRequestHandler.java

index 2e789c75885b8367542d1f50c9d5bf55dc686f51..39f3232b1d8f533e72b1bb2d66916668e7269c40 100644 (file)
@@ -2,8 +2,8 @@ package org.apache.maven.archiva;
 
 import org.apache.maven.archiva.converter.RepositoryConversionException;
 import org.apache.maven.archiva.converter.RepositoryConverter;
-import org.apache.maven.archiva.discovery.ArtifactDiscoverer;
-import org.apache.maven.archiva.discovery.DiscovererException;
+import org.apache.maven.archiva.discoverer.ArtifactDiscoverer;
+import org.apache.maven.archiva.discoverer.DiscovererException;
 import org.apache.maven.archiva.reporting.ArtifactReporter;
 import org.apache.maven.artifact.repository.ArtifactRepository;
 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
index 52f88bf5c1a960f9b43eb4ab2eca87fe777736b2..7e7076350efa0d477df8b1214685dce5b2a0ff2d 100644 (file)
@@ -1,7 +1,7 @@
 package org.apache.maven.archiva;
 
 import org.apache.maven.archiva.converter.RepositoryConversionException;
-import org.apache.maven.archiva.discovery.DiscovererException;
+import org.apache.maven.archiva.discoverer.DiscovererException;
 
 import java.io.File;
 
index 52487d4f0df76fcbd68cdac4ee863ce69b7df8fd..b95fc7cc91cdb8c0635dffc25c0b41014f5586f0 100644 (file)
@@ -21,8 +21,8 @@ import org.apache.maven.archiva.configuration.ConfigurationStore;
 import org.apache.maven.archiva.configuration.ConfigurationStoreException;
 import org.apache.maven.archiva.configuration.ConfiguredRepositoryFactory;
 import org.apache.maven.archiva.configuration.RepositoryConfiguration;
-import org.apache.maven.archiva.discovery.ArtifactDiscoverer;
-import org.apache.maven.archiva.discovery.DiscovererException;
+import org.apache.maven.archiva.discoverer.ArtifactDiscoverer;
+import org.apache.maven.archiva.discoverer.DiscovererException;
 import org.apache.maven.archiva.indexer.RepositoryArtifactIndex;
 import org.apache.maven.archiva.indexer.RepositoryArtifactIndexFactory;
 import org.apache.maven.archiva.indexer.RepositoryIndexException;
@@ -65,7 +65,7 @@ public class IndexerTask
     private ConfiguredRepositoryFactory repoFactory;
 
     /**
-     * @plexus.requirement role="org.apache.maven.archiva.discovery.ArtifactDiscoverer"
+     * @plexus.requirement role="org.apache.maven.archiva.discoverer.ArtifactDiscoverer"
      */
     private Map artifactDiscoverers;
 
diff --git a/archiva-discoverer/src/main/java/org/apache/maven/archiva/discoverer/AbstractArtifactDiscoverer.java b/archiva-discoverer/src/main/java/org/apache/maven/archiva/discoverer/AbstractArtifactDiscoverer.java
new file mode 100644 (file)
index 0000000..b7841fc
--- /dev/null
@@ -0,0 +1,149 @@
+package org.apache.maven.archiva.discoverer;
+
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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 org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.codehaus.plexus.util.xml.Xpp3Dom;
+
+import java.io.File;
+import java.io.IOException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Locale;
+
+/**
+ * Base class for artifact discoverers.
+ *
+ * @author John Casey
+ * @author Brett Porter
+ */
+public abstract class AbstractArtifactDiscoverer
+    extends AbstractDiscoverer
+    implements ArtifactDiscoverer
+{
+    /**
+     * Standard patterns to exclude from discovery as they are not artifacts.
+     */
+    private static final String[] STANDARD_DISCOVERY_EXCLUDES = {"bin/**", "reports/**", ".maven/**", "**/*.md5",
+        "**/*.MD5", "**/*.sha1", "**/*.SHA1", "**/*snapshot-version", "*/website/**", "*/licenses/**", "*/licences/**",
+        "**/.htaccess", "**/*.html", "**/*.asc", "**/*.txt", "**/*.xml", "**/README*", "**/CHANGELOG*", "**/KEYS*"};
+
+    private List scanForArtifactPaths( File repositoryBase, List blacklistedPatterns, long comparisonTimestamp )
+    {
+        return scanForArtifactPaths( repositoryBase, blacklistedPatterns, null, STANDARD_DISCOVERY_EXCLUDES,
+                                     comparisonTimestamp );
+    }
+
+    public List discoverArtifacts( ArtifactRepository repository, String operation, List blacklistedPatterns,
+                                   boolean includeSnapshots )
+        throws DiscovererException
+    {
+        if ( !"file".equals( repository.getProtocol() ) )
+        {
+            throw new UnsupportedOperationException( "Only filesystem repositories are supported" );
+        }
+
+        Xpp3Dom dom = getLastArtifactDiscoveryDom( readRepositoryMetadataDom( repository ) );
+        long comparisonTimestamp = readComparisonTimestamp( repository, operation, dom );
+
+        // Note that last checked time is deliberately set to the start of the process so that anything added
+        // mid-discoverer and missed by the scanner will get checked next time.
+        // Due to this, there must be no negative side-effects of discovering something twice.
+        Date newLastCheckedTime = new Date();
+
+        File repositoryBase = new File( repository.getBasedir() );
+
+        List artifacts = new ArrayList();
+
+        List artifactPaths = scanForArtifactPaths( repositoryBase, blacklistedPatterns, comparisonTimestamp );
+
+        // Also note that the last check time, while set at the start, is saved at the end, so that if any exceptions
+        // occur, then the timestamp is not updated so that the discoverer is attempted again
+        // TODO: under the list-return behaviour we have now, exceptions might occur later and the timestamp will not be reset - see MRM-83
+        try
+        {
+            setLastCheckedTime( repository, operation, newLastCheckedTime );
+        }
+        catch ( IOException e )
+        {
+            throw new DiscovererException( "Error writing metadata: " + e.getMessage(), e );
+        }
+
+        for ( Iterator i = artifactPaths.iterator(); i.hasNext(); )
+        {
+            String path = (String) i.next();
+
+            try
+            {
+                Artifact artifact = buildArtifactFromPath( path, repository );
+
+                if ( includeSnapshots || !artifact.isSnapshot() )
+                {
+                    artifacts.add( artifact );
+                }
+            }
+            catch ( DiscovererException e )
+            {
+                addKickedOutPath( path, e.getMessage() );
+            }
+        }
+
+        return artifacts;
+    }
+
+    /**
+     * Returns an artifact object that is represented by the specified path in a repository
+     *
+     * @param path       The path that is pointing to an artifact
+     * @param repository The repository of the artifact
+     * @return Artifact
+     * @throws DiscovererException when the specified path does correspond to an artifact
+     */
+    public Artifact buildArtifactFromPath( String path, ArtifactRepository repository )
+        throws DiscovererException
+    {
+        Artifact artifact = buildArtifact( path );
+
+        if ( artifact != null )
+        {
+            artifact.setRepository( repository );
+            artifact.setFile( new File( repository.getBasedir(), path ) );
+        }
+
+        return artifact;
+    }
+
+    public void setLastCheckedTime( ArtifactRepository repository, String operation, Date date )
+        throws IOException
+    {
+        // see notes in resetLastCheckedTime
+
+        File file = new File( repository.getBasedir(), "maven-metadata.xml" );
+
+        Xpp3Dom dom = readDom( file );
+
+        String dateString = new SimpleDateFormat( DATE_FMT, Locale.US ).format( date );
+
+        setEntry( getLastArtifactDiscoveryDom( dom ), operation, dateString );
+
+        saveDom( file, dom );
+    }
+}
diff --git a/archiva-discoverer/src/main/java/org/apache/maven/archiva/discoverer/AbstractDiscoverer.java b/archiva-discoverer/src/main/java/org/apache/maven/archiva/discoverer/AbstractDiscoverer.java
new file mode 100644 (file)
index 0000000..783a8c7
--- /dev/null
@@ -0,0 +1,302 @@
+package org.apache.maven.archiva.discoverer;
+
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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 org.apache.maven.artifact.factory.ArtifactFactory;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.codehaus.plexus.logging.AbstractLogEnabled;
+import org.codehaus.plexus.util.DirectoryScanner;
+import org.codehaus.plexus.util.FileUtils;
+import org.codehaus.plexus.util.IOUtil;
+import org.codehaus.plexus.util.xml.Xpp3Dom;
+import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
+import org.codehaus.plexus.util.xml.Xpp3DomWriter;
+import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Locale;
+
+/**
+ * Base class for the artifact and metadata discoverers.
+ *
+ * @author <a href="mailto:brett@apache.org">Brett Porter</a>
+ */
+public abstract class AbstractDiscoverer
+    extends AbstractLogEnabled
+    implements Discoverer
+{
+    private List kickedOutPaths = new ArrayList();
+
+    /**
+     * @plexus.requirement
+     */
+    protected ArtifactFactory artifactFactory;
+
+    private static final String[] EMPTY_STRING_ARRAY = new String[0];
+
+    private List excludedPaths = new ArrayList();
+
+    /**
+     * @plexus.configuration default-value="60000"
+     */
+    private int blackoutPeriod;
+
+    protected static final String DATE_FMT = "yyyyMMddHHmmss";
+
+    /**
+     * Add a path to the list of files that were kicked out due to being invalid.
+     *
+     * @param path   the path to add
+     * @param reason the reason why the path is being kicked out
+     */
+    protected void addKickedOutPath( String path, String reason )
+    {
+        kickedOutPaths.add( new DiscovererPath( path, reason ) );
+    }
+
+    /**
+     * Returns an iterator for the list if DiscovererPaths that were found to not represent a searched object
+     *
+     * @return Iterator for the DiscovererPath List
+     */
+    public Iterator getKickedOutPathsIterator()
+    {
+        return kickedOutPaths.iterator();
+    }
+
+    protected List scanForArtifactPaths( File repositoryBase, List blacklistedPatterns, String[] includes,
+                                         String[] excludes, long comparisonTimestamp )
+    {
+        List allExcludes = new ArrayList();
+        allExcludes.addAll( FileUtils.getDefaultExcludesAsList() );
+        if ( excludes != null )
+        {
+            allExcludes.addAll( Arrays.asList( excludes ) );
+        }
+        if ( blacklistedPatterns != null )
+        {
+            allExcludes.addAll( blacklistedPatterns );
+        }
+
+        DirectoryScanner scanner = new DirectoryScanner();
+        scanner.setBasedir( repositoryBase );
+        if ( includes != null )
+        {
+            scanner.setIncludes( includes );
+        }
+        scanner.setExcludes( (String[]) allExcludes.toArray( EMPTY_STRING_ARRAY ) );
+
+        // TODO: Correct for extremely large repositories (artifact counts over 200,000 entries)
+        scanner.scan();
+
+        for ( Iterator files = Arrays.asList( scanner.getExcludedFiles() ).iterator(); files.hasNext(); )
+        {
+            String path = files.next().toString();
+
+            excludedPaths.add( new DiscovererPath( path, "Artifact was in the specified list of exclusions" ) );
+        }
+
+        // TODO: this could be a part of the scanner
+        List includedPaths = new ArrayList();
+        for ( Iterator files = Arrays.asList( scanner.getIncludedFiles() ).iterator(); files.hasNext(); )
+        {
+            String path = files.next().toString();
+
+            long modTime = new File( repositoryBase, path ).lastModified();
+            if ( modTime < System.currentTimeMillis() - blackoutPeriod )
+            {
+                if ( modTime > comparisonTimestamp )
+                {
+                    includedPaths.add( path );
+                }
+            }
+        }
+
+        return includedPaths;
+    }
+
+    /**
+     * Returns an iterator for the list if DiscovererPaths that were not processed because they are explicitly excluded
+     *
+     * @return Iterator for the DiscovererPath List
+     */
+    public Iterator getExcludedPathsIterator()
+    {
+        return excludedPaths.iterator();
+    }
+
+    protected long readComparisonTimestamp( ArtifactRepository repository, String operation, Xpp3Dom dom )
+    {
+        Xpp3Dom entry = dom.getChild( operation );
+        long comparisonTimestamp = 0;
+        if ( entry != null )
+        {
+            try
+            {
+                comparisonTimestamp = new SimpleDateFormat( DATE_FMT, Locale.US ).parse( entry.getValue() ).getTime();
+            }
+            catch ( ParseException e )
+            {
+                getLogger().error( "Timestamp was invalid: " + entry.getValue() + "; ignoring" );
+            }
+        }
+        return comparisonTimestamp;
+    }
+
+    protected Xpp3Dom readDom( File file )
+    {
+        Xpp3Dom dom;
+        FileReader fileReader = null;
+        try
+        {
+            fileReader = new FileReader( file );
+            dom = Xpp3DomBuilder.build( fileReader );
+        }
+        catch ( FileNotFoundException e )
+        {
+            // Safe to ignore
+            dom = new Xpp3Dom( "metadata" );
+        }
+        catch ( XmlPullParserException e )
+        {
+            getLogger().error( "Error reading metadata (ignoring and recreating): " + e.getMessage() );
+            dom = new Xpp3Dom( "metadata" );
+        }
+        catch ( IOException e )
+        {
+            getLogger().error( "Error reading metadata (ignoring and recreating): " + e.getMessage() );
+            dom = new Xpp3Dom( "metadata" );
+        }
+        finally
+        {
+            IOUtil.close( fileReader );
+        }
+        return dom;
+    }
+
+    protected Xpp3Dom getLastArtifactDiscoveryDom( Xpp3Dom dom )
+    {
+        Xpp3Dom lastDiscoveryDom = dom.getChild( "lastArtifactDiscovery" );
+        if ( lastDiscoveryDom == null )
+        {
+            dom.addChild( new Xpp3Dom( "lastArtifactDiscovery" ) );
+            lastDiscoveryDom = dom.getChild( "lastArtifactDiscovery" );
+        }
+        return lastDiscoveryDom;
+    }
+
+    protected Xpp3Dom getLastMetadataDiscoveryDom( Xpp3Dom dom )
+    {
+        Xpp3Dom lastDiscoveryDom = dom.getChild( "lastMetadataDiscovery" );
+        if ( lastDiscoveryDom == null )
+        {
+            dom.addChild( new Xpp3Dom( "lastMetadataDiscovery" ) );
+            lastDiscoveryDom = dom.getChild( "lastMetadataDiscovery" );
+        }
+        return lastDiscoveryDom;
+    }
+
+    public void resetLastCheckedTime( ArtifactRepository repository, String operation )
+        throws IOException
+    {
+        // TODO: get these changes into maven-metadata.xml and migrate towards that. The model is further diverging to a different layout at each level so submodels might be a good idea.
+        // TODO: maven-artifact probably needs an improved pathOfMetadata to cope with top level metadata
+        // TODO: might we need to write this as maven-metadata-local in some circumstances? merge others? Probably best to keep it simple and just use this format at the root. No need to merge anything that I can see
+        // TODO: since this metadata isn't meant to be shared, perhaps another file is called for after all.
+        // Format is: <repository><lastDiscovery><KEY>yyyyMMddHHmmss</KEY></lastDiscovery></repository> (ie, flat properties)
+
+        File file = new File( repository.getBasedir(), "maven-metadata.xml" );
+
+        Xpp3Dom dom = readDom( file );
+
+        boolean changed = false;
+
+        if ( removeEntry( getLastArtifactDiscoveryDom( dom ), operation ) )
+        {
+            changed = true;
+        }
+
+        if ( removeEntry( getLastMetadataDiscoveryDom( dom ), operation ) )
+        {
+            changed = true;
+        }
+
+        if ( changed )
+        {
+            saveDom( file, dom );
+        }
+    }
+
+    private boolean removeEntry( Xpp3Dom lastDiscoveryDom, String operation )
+    {
+        boolean changed = false;
+
+        // do this in reverse so that removing doesn't affect counter
+        Xpp3Dom[] children = lastDiscoveryDom.getChildren();
+        for ( int i = lastDiscoveryDom.getChildCount() - 1; i >= 0; i-- )
+        {
+            if ( children[i].getName().equals( operation ) )
+            {
+                changed = true;
+                lastDiscoveryDom.removeChild( i );
+            }
+        }
+        return changed;
+    }
+
+    protected void saveDom( File file, Xpp3Dom dom )
+        throws IOException
+    {
+        FileWriter writer = new FileWriter( file );
+
+        // save metadata
+        try
+        {
+            Xpp3DomWriter.write( writer, dom );
+        }
+        finally
+        {
+            IOUtil.close( writer );
+        }
+    }
+
+    protected void setEntry( Xpp3Dom lastDiscoveryDom, String operation, String dateString )
+    {
+        Xpp3Dom entry = lastDiscoveryDom.getChild( operation );
+        if ( entry == null )
+        {
+            entry = new Xpp3Dom( operation );
+            lastDiscoveryDom.addChild( entry );
+        }
+        entry.setValue( dateString );
+    }
+
+    protected Xpp3Dom readRepositoryMetadataDom( ArtifactRepository repository )
+    {
+        return readDom( new File( repository.getBasedir(), "maven-metadata.xml" ) );
+    }
+}
diff --git a/archiva-discoverer/src/main/java/org/apache/maven/archiva/discoverer/ArtifactDiscoverer.java b/archiva-discoverer/src/main/java/org/apache/maven/archiva/discoverer/ArtifactDiscoverer.java
new file mode 100644 (file)
index 0000000..c5c79c9
--- /dev/null
@@ -0,0 +1,64 @@
+package org.apache.maven.archiva.discoverer;
+
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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 org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+
+import java.util.List;
+
+/**
+ * Interface for implementation that can discover artifacts within a repository.
+ *
+ * @author John Casey
+ * @author Brett Porter
+ * @todo do we want blacklisted patterns in another form? Part of the object construction?
+ * @todo should includeSnapshots be configuration on the component? If not, should the methods be changed to include alternates for both possibilities (discoverReleaseArtifacts, discoverReleaseAndSnapshotArtifacts)?
+ * @todo instead of a returned list, should a listener be passed in?
+ */
+public interface ArtifactDiscoverer
+    extends Discoverer
+{
+    String ROLE = ArtifactDiscoverer.class.getName();
+
+    /**
+     * Discover artifacts in the repository. Only artifacts added since the last attempt at discovery will be found.
+     * This process guarantees never to miss an artifact, however it is possible that an artifact will be received twice
+     * consecutively even if unchanged, so any users of this list must handle such a situation gracefully.
+     *
+     * @param repository          the location of the repository
+     * @param operation           the operation being used to discover for timestamp checking
+     * @param blacklistedPatterns pattern that lists any files to prevent from being included when scanning
+     * @param includeSnapshots    whether to discover snapshots
+     * @return the list of artifacts discovered
+     * @throws DiscovererException if there was an unrecoverable problem discovering artifacts or recording progress
+     */
+    List discoverArtifacts( ArtifactRepository repository, String operation, List blacklistedPatterns,
+                            boolean includeSnapshots )
+        throws DiscovererException;
+
+    /**
+     * Build an artifact from a path in the repository
+     *
+     * @param path the path
+     * @return the artifact
+     * @throws DiscovererException if the file is not a valid artifact
+     * @todo this should be in maven-artifact
+     */
+    Artifact buildArtifact( String path )
+        throws DiscovererException;
+}
diff --git a/archiva-discoverer/src/main/java/org/apache/maven/archiva/discoverer/DefaultArtifactDiscoverer.java b/archiva-discoverer/src/main/java/org/apache/maven/archiva/discoverer/DefaultArtifactDiscoverer.java
new file mode 100644 (file)
index 0000000..f72f82e
--- /dev/null
@@ -0,0 +1,190 @@
+package org.apache.maven.archiva.discoverer;
+
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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 org.apache.maven.artifact.Artifact;
+import org.codehaus.plexus.util.StringUtils;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.StringTokenizer;
+
+/**
+ * Artifact discoverer for the new repository layout (Maven 2.0+).
+ *
+ * @author John Casey
+ * @author Brett Porter
+ * @plexus.component role="org.apache.maven.archiva.discoverer.ArtifactDiscoverer" role-hint="default"
+ */
+public class DefaultArtifactDiscoverer
+    extends AbstractArtifactDiscoverer
+{
+    /**
+     * @see org.apache.maven.archiva.discoverer.ArtifactDiscoverer#buildArtifact(String)
+     */
+    public Artifact buildArtifact( String path )
+        throws DiscovererException
+    {
+        List pathParts = new ArrayList();
+        StringTokenizer st = new StringTokenizer( path, "/\\" );
+        while ( st.hasMoreTokens() )
+        {
+            pathParts.add( st.nextToken() );
+        }
+
+        Collections.reverse( pathParts );
+
+        Artifact artifact;
+        if ( pathParts.size() >= 4 )
+        {
+            // maven 2.x path
+
+            // the actual artifact filename.
+            String filename = (String) pathParts.remove( 0 );
+
+            // the next one is the version.
+            String version = (String) pathParts.remove( 0 );
+
+            // the next one is the artifactId.
+            String artifactId = (String) pathParts.remove( 0 );
+
+            // the remaining are the groupId.
+            Collections.reverse( pathParts );
+            String groupId = StringUtils.join( pathParts.iterator(), "." );
+
+            String remainingFilename = filename;
+            if ( remainingFilename.startsWith( artifactId + "-" ) )
+            {
+                remainingFilename = remainingFilename.substring( artifactId.length() + 1 );
+
+                String classifier = null;
+
+                // TODO: use artifact handler, share with legacy discoverer
+                String type;
+                if ( remainingFilename.endsWith( ".tar.gz" ) )
+                {
+                    type = "distribution-tgz";
+                    remainingFilename =
+                        remainingFilename.substring( 0, remainingFilename.length() - ".tar.gz".length() );
+                }
+                else if ( remainingFilename.endsWith( ".zip" ) )
+                {
+                    type = "distribution-zip";
+                    remainingFilename = remainingFilename.substring( 0, remainingFilename.length() - ".zip".length() );
+                }
+                else if ( remainingFilename.endsWith( "-sources.jar" ) )
+                {
+                    type = "java-source";
+                    classifier = "sources";
+                    remainingFilename =
+                        remainingFilename.substring( 0, remainingFilename.length() - "-sources.jar".length() );
+                }
+                else
+                {
+                    int index = remainingFilename.lastIndexOf( "." );
+                    if ( index >= 0 )
+                    {
+                        type = remainingFilename.substring( index + 1 );
+                        remainingFilename = remainingFilename.substring( 0, index );
+                    }
+                    else
+                    {
+                        throw new DiscovererException( "Path filename does not have an extension" );
+                    }
+                }
+
+                Artifact result;
+                if ( classifier == null )
+                {
+                    result =
+                        artifactFactory.createArtifact( groupId, artifactId, version, Artifact.SCOPE_RUNTIME, type );
+                }
+                else
+                {
+                    result =
+                        artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, type, classifier );
+                }
+
+                if ( result.isSnapshot() )
+                {
+                    // version is *-SNAPSHOT, filename is *-yyyyMMdd.hhmmss-b
+                    int classifierIndex = remainingFilename.indexOf( '-', version.length() + 8 );
+                    if ( classifierIndex >= 0 )
+                    {
+                        classifier = remainingFilename.substring( classifierIndex + 1 );
+                        remainingFilename = remainingFilename.substring( 0, classifierIndex );
+                        result = artifactFactory.createArtifactWithClassifier( groupId, artifactId, remainingFilename,
+                                                                               type, classifier );
+                    }
+                    else
+                    {
+                        result = artifactFactory.createArtifact( groupId, artifactId, remainingFilename,
+                                                                 Artifact.SCOPE_RUNTIME, type );
+                    }
+
+                    // poor encapsulation requires we do this to populate base version
+                    if ( !result.isSnapshot() )
+                    {
+                        throw new DiscovererException( "Failed to create a snapshot artifact: " + result );
+                    }
+                    else if ( !result.getBaseVersion().equals( version ) )
+                    {
+                        throw new DiscovererException(
+                            "Built snapshot artifact base version does not match path version: " + result +
+                                "; should have been version: " + version );
+                    }
+                    else
+                    {
+                        artifact = result;
+                    }
+                }
+                else if ( !remainingFilename.startsWith( version ) )
+                {
+                    throw new DiscovererException( "Built artifact version does not match path version" );
+                }
+                else if ( !remainingFilename.equals( version ) )
+                {
+                    if ( remainingFilename.charAt( version.length() ) == '-' )
+                    {
+                        classifier = remainingFilename.substring( version.length() + 1 );
+                        artifact = artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, type,
+                                                                                 classifier );
+                    }
+                    else
+                    {
+                        throw new DiscovererException( "Path version does not corresspond to an artifact version" );
+                    }
+                }
+                else
+                {
+                    artifact = result;
+                }
+            }
+            else
+            {
+                throw new DiscovererException( "Path filename does not correspond to an artifact" );
+            }
+        }
+        else
+        {
+            throw new DiscovererException( "Path is too short to build an artifact from" );
+        }
+
+        return artifact;
+    }
+}
diff --git a/archiva-discoverer/src/main/java/org/apache/maven/archiva/discoverer/DefaultMetadataDiscoverer.java b/archiva-discoverer/src/main/java/org/apache/maven/archiva/discoverer/DefaultMetadataDiscoverer.java
new file mode 100644 (file)
index 0000000..1d03d9d
--- /dev/null
@@ -0,0 +1,254 @@
+package org.apache.maven.archiva.discoverer;
+
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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 org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
+import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata;
+import org.apache.maven.artifact.repository.metadata.Metadata;
+import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
+import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
+import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
+import org.codehaus.plexus.util.StringUtils;
+import org.codehaus.plexus.util.xml.Xpp3Dom;
+import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Locale;
+import java.util.StringTokenizer;
+
+/**
+ * This class gets all the paths that contain the metadata files.
+ *
+ * @plexus.component role="org.apache.maven.archiva.discoverer.MetadataDiscoverer" role-hint="default"
+ */
+public class DefaultMetadataDiscoverer
+    extends AbstractDiscoverer
+    implements MetadataDiscoverer
+{
+    /**
+     * Standard patterns to include in discovery of metadata files.
+     *
+     * @todo Note that only the remote format is supported at this time: you cannot search local repository metadata due
+     * to the way it is later loaded in the searchers. Review code using pathOfRemoteMetadata. IS there any value in
+     * searching the local metadata in the first place though?
+     */
+    private static final String[] STANDARD_DISCOVERY_INCLUDES = {"**/maven-metadata.xml"};
+
+    public List discoverMetadata( ArtifactRepository repository, String operation, List blacklistedPatterns )
+        throws DiscovererException
+    {
+        if ( !"file".equals( repository.getProtocol() ) )
+        {
+            throw new UnsupportedOperationException( "Only filesystem repositories are supported" );
+        }
+
+        Xpp3Dom dom = getLastMetadataDiscoveryDom( readRepositoryMetadataDom( repository ) );
+        long comparisonTimestamp = readComparisonTimestamp( repository, operation, dom );
+
+        // Note that last checked time is deliberately set to the start of the process so that anything added
+        // mid-discoverer and missed by the scanner will get checked next time.
+        // Due to this, there must be no negative side-effects of discovering something twice.
+        Date newLastCheckedTime = new Date();
+
+        List metadataFiles = new ArrayList();
+        List metadataPaths = scanForArtifactPaths( new File( repository.getBasedir() ), blacklistedPatterns,
+                                                   STANDARD_DISCOVERY_INCLUDES, null, comparisonTimestamp );
+
+        // Also note that the last check time, while set at the start, is saved at the end, so that if any exceptions
+        // occur, then the timestamp is not updated so that the discoverer is attempted again
+        // TODO: under the list-return behaviour we have now, exceptions might occur later and the timestamp will not be reset - see MRM-83
+        try
+        {
+            setLastCheckedTime( repository, operation, newLastCheckedTime );
+        }
+        catch ( IOException e )
+        {
+            throw new DiscovererException( "Error writing metadata: " + e.getMessage(), e );
+        }
+
+        for ( Iterator i = metadataPaths.iterator(); i.hasNext(); )
+        {
+            String metadataPath = (String) i.next();
+            try
+            {
+                RepositoryMetadata metadata = buildMetadata( repository.getBasedir(), metadataPath );
+                metadataFiles.add( metadata );
+            }
+            catch ( DiscovererException e )
+            {
+                addKickedOutPath( metadataPath, e.getMessage() );
+            }
+        }
+
+        return metadataFiles;
+    }
+
+    private RepositoryMetadata buildMetadata( String repo, String metadataPath )
+        throws DiscovererException
+    {
+        Metadata m;
+        String repoPath = repo + "/" + metadataPath;
+        try
+        {
+            URL url = new File( repoPath ).toURI().toURL();
+            InputStream is = url.openStream();
+            Reader reader = new InputStreamReader( is );
+            MetadataXpp3Reader metadataReader = new MetadataXpp3Reader();
+
+            m = metadataReader.read( reader );
+        }
+        catch ( XmlPullParserException e )
+        {
+            throw new DiscovererException( "Error parsing metadata file '" + repoPath + "': " + e.getMessage(), e );
+        }
+        catch ( MalformedURLException e )
+        {
+            // shouldn't happen
+            throw new DiscovererException( "Error constructing metadata file '" + repoPath + "': " + e.getMessage(),
+                                           e );
+        }
+        catch ( IOException e )
+        {
+            throw new DiscovererException( "Error reading metadata file '" + repoPath + "': " + e.getMessage(), e );
+        }
+
+        RepositoryMetadata repositoryMetadata = buildMetadata( m, metadataPath );
+
+        if ( repositoryMetadata == null )
+        {
+            throw new DiscovererException( "Unable to build a repository metadata from path" );
+        }
+
+        return repositoryMetadata;
+    }
+
+    /**
+     * Builds a RepositoryMetadata object from a Metadata object and its path.
+     *
+     * @param m            Metadata
+     * @param metadataPath path
+     * @return RepositoryMetadata if the parameters represent one; null if not
+     * @todo should we just be using the path information, and loading it later when it is needed? (for reporting, etc)
+     */
+    private RepositoryMetadata buildMetadata( Metadata m, String metadataPath )
+    {
+        String metaGroupId = m.getGroupId();
+        String metaArtifactId = m.getArtifactId();
+        String metaVersion = m.getVersion();
+
+        // check if the groupId, artifactId and version is in the
+        // metadataPath
+        // parse the path, in reverse order
+        List pathParts = new ArrayList();
+        StringTokenizer st = new StringTokenizer( metadataPath, "/\\" );
+        while ( st.hasMoreTokens() )
+        {
+            pathParts.add( st.nextToken() );
+        }
+
+        Collections.reverse( pathParts );
+        // remove the metadata file
+        pathParts.remove( 0 );
+        Iterator it = pathParts.iterator();
+        String tmpDir = (String) it.next();
+
+        Artifact artifact = null;
+        if ( !StringUtils.isEmpty( metaVersion ) )
+        {
+            artifact = artifactFactory.createProjectArtifact( metaGroupId, metaArtifactId, metaVersion );
+        }
+
+        // snapshotMetadata
+        RepositoryMetadata metadata = null;
+        if ( tmpDir != null && tmpDir.equals( metaVersion ) )
+        {
+            if ( artifact != null )
+            {
+                metadata = new SnapshotArtifactRepositoryMetadata( artifact );
+            }
+        }
+        else if ( tmpDir != null && tmpDir.equals( metaArtifactId ) )
+        {
+            // artifactMetadata
+            if ( artifact != null )
+            {
+                metadata = new ArtifactRepositoryMetadata( artifact );
+            }
+            else
+            {
+                artifact = artifactFactory.createProjectArtifact( metaGroupId, metaArtifactId, "1.0" );
+                metadata = new ArtifactRepositoryMetadata( artifact );
+            }
+        }
+        else
+        {
+            String groupDir = "";
+            int ctr = 0;
+            for ( it = pathParts.iterator(); it.hasNext(); )
+            {
+                String path = (String) it.next();
+                if ( ctr == 0 )
+                {
+                    groupDir = path;
+                }
+                else
+                {
+                    groupDir = path + "." + groupDir;
+                }
+                ctr++;
+            }
+
+            // groupMetadata
+            if ( metaGroupId != null && metaGroupId.equals( groupDir ) )
+            {
+                metadata = new GroupRepositoryMetadata( metaGroupId );
+            }
+        }
+
+        return metadata;
+    }
+
+    public void setLastCheckedTime( ArtifactRepository repository, String operation, Date date )
+        throws IOException
+    {
+        // see notes in resetLastCheckedTime
+
+        File file = new File( repository.getBasedir(), "maven-metadata.xml" );
+
+        Xpp3Dom dom = readDom( file );
+
+        String dateString = new SimpleDateFormat( DATE_FMT, Locale.US ).format( date );
+
+        setEntry( getLastMetadataDiscoveryDom( dom ), operation, dateString );
+
+        saveDom( file, dom );
+    }
+}
diff --git a/archiva-discoverer/src/main/java/org/apache/maven/archiva/discoverer/Discoverer.java b/archiva-discoverer/src/main/java/org/apache/maven/archiva/discoverer/Discoverer.java
new file mode 100644 (file)
index 0000000..54bb55e
--- /dev/null
@@ -0,0 +1,64 @@
+package org.apache.maven.archiva.discoverer;
+
+import org.apache.maven.artifact.repository.ArtifactRepository;
+
+import java.io.IOException;
+import java.util.Date;
+import java.util.Iterator;
+
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+
+/**
+ * @author Edwin Punzalan
+ */
+public interface Discoverer
+{
+    /**
+     * Get the list of paths kicked out during the discovery process.
+     *
+     * @return the paths as Strings.
+     */
+    Iterator getKickedOutPathsIterator();
+
+    /**
+     * Get the list of paths excluded during the discovery process.
+     *
+     * @return the paths as Strings.
+     */
+    Iterator getExcludedPathsIterator();
+
+    /**
+     * Reset the time in the repository that indicates the last time a check was performed.
+     *
+     * @param repository the location of the repository
+     * @param operation  the operation to record the timestamp for
+     * @throws java.io.IOException if there is a non-recoverable problem reading or writing the metadata
+     */
+    void resetLastCheckedTime( ArtifactRepository repository, String operation )
+        throws IOException;
+
+    /**
+     * Set the time in the repository that indicates the last time a check was performed.
+     *
+     * @param repository the location of the repository
+     * @param operation  the operation to record the timestamp for
+     * @param date       the date to set the last check to
+     * @throws java.io.IOException if there is a non-recoverable problem reading or writing the metadata
+     */
+    void setLastCheckedTime( ArtifactRepository repository, String operation, Date date )
+        throws IOException;
+}
diff --git a/archiva-discoverer/src/main/java/org/apache/maven/archiva/discoverer/DiscovererException.java b/archiva-discoverer/src/main/java/org/apache/maven/archiva/discoverer/DiscovererException.java
new file mode 100644 (file)
index 0000000..393ec5c
--- /dev/null
@@ -0,0 +1,34 @@
+package org.apache.maven.archiva.discoverer;
+
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+
+/**
+ * @author Edwin Punzalan
+ */
+public class DiscovererException
+    extends Exception
+{
+    public DiscovererException( String message )
+    {
+        super( message );
+    }
+
+    public DiscovererException( String message, Throwable cause )
+    {
+        super( message, cause );
+    }
+}
diff --git a/archiva-discoverer/src/main/java/org/apache/maven/archiva/discoverer/DiscovererPath.java b/archiva-discoverer/src/main/java/org/apache/maven/archiva/discoverer/DiscovererPath.java
new file mode 100644 (file)
index 0000000..c300e9e
--- /dev/null
@@ -0,0 +1,49 @@
+package org.apache.maven.archiva.discoverer;
+
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+
+/**
+ * @author Edwin Punzalan
+ */
+public class DiscovererPath
+{
+    /**
+     * The path discovered.
+     */
+    private final String path;
+
+    /**
+     * A comment about why the path is being processed.
+     */
+    private final String comment;
+
+    public DiscovererPath( String path, String comment )
+    {
+        this.path = path;
+        this.comment = comment;
+    }
+
+    public String getPath()
+    {
+        return path;
+    }
+
+    public String getComment()
+    {
+        return comment;
+    }
+}
diff --git a/archiva-discoverer/src/main/java/org/apache/maven/archiva/discoverer/LegacyArtifactDiscoverer.java b/archiva-discoverer/src/main/java/org/apache/maven/archiva/discoverer/LegacyArtifactDiscoverer.java
new file mode 100644 (file)
index 0000000..361a6f9
--- /dev/null
@@ -0,0 +1,280 @@
+package org.apache.maven.archiva.discoverer;
+
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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 org.apache.maven.artifact.Artifact;
+
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.StringTokenizer;
+
+/**
+ * Artifact discoverer for the legacy repository layout (Maven 1.x).
+ * Method used to build an artifact object using a relative path from a repository base directory.  An artifactId
+ * having the words "DEV", "PRE", "RC", "ALPHA", "BETA", "DEBUG", "UNOFFICIAL", "CURRENT", "LATEST", "FCS",
+ * "RELEASE", "NIGHTLY", "SNAPSHOT" and "TEST" (not case-sensitive) will most likely make this method fail as
+ * they are reserved for version usage.
+ *
+ * @author John Casey
+ * @author Brett Porter
+ * @plexus.component role="org.apache.maven.archiva.discoverer.ArtifactDiscoverer" role-hint="legacy"
+ */
+public class LegacyArtifactDiscoverer
+    extends AbstractArtifactDiscoverer
+{
+    /**
+     * @see org.apache.maven.archiva.discoverer.ArtifactDiscoverer#buildArtifact(String)
+     */
+    public Artifact buildArtifact( String path )
+        throws DiscovererException
+    {
+        StringTokenizer tokens = new StringTokenizer( path, "/\\" );
+
+        Artifact result;
+
+        int numberOfTokens = tokens.countTokens();
+
+        if ( numberOfTokens == 3 )
+        {
+            String groupId = tokens.nextToken();
+
+            String type = tokens.nextToken();
+
+            if ( type.endsWith( "s" ) )
+            {
+                type = type.substring( 0, type.length() - 1 );
+
+                // contains artifactId, version, classifier, and extension.
+                String avceGlob = tokens.nextToken();
+
+                //noinspection CollectionDeclaredAsConcreteClass
+                LinkedList avceTokenList = new LinkedList();
+
+                StringTokenizer avceTokenizer = new StringTokenizer( avceGlob, "-" );
+                while ( avceTokenizer.hasMoreTokens() )
+                {
+                    avceTokenList.addLast( avceTokenizer.nextToken() );
+                }
+
+                String lastAvceToken = (String) avceTokenList.removeLast();
+
+                // TODO: share with other discoverer, use artifact handlers instead
+                if ( lastAvceToken.endsWith( ".tar.gz" ) )
+                {
+                    type = "distribution-tgz";
+
+                    lastAvceToken = lastAvceToken.substring( 0, lastAvceToken.length() - ".tar.gz".length() );
+
+                    avceTokenList.addLast( lastAvceToken );
+                }
+                else if ( lastAvceToken.endsWith( "sources.jar" ) )
+                {
+                    type = "java-source";
+
+                    lastAvceToken = lastAvceToken.substring( 0, lastAvceToken.length() - ".jar".length() );
+
+                    avceTokenList.addLast( lastAvceToken );
+                }
+                else if ( lastAvceToken.endsWith( ".zip" ) )
+                {
+                    type = "distribution-zip";
+
+                    lastAvceToken = lastAvceToken.substring( 0, lastAvceToken.length() - ".zip".length() );
+
+                    avceTokenList.addLast( lastAvceToken );
+                }
+                else
+                {
+                    int extPos = lastAvceToken.lastIndexOf( '.' );
+
+                    if ( extPos > 0 )
+                    {
+                        String ext = lastAvceToken.substring( extPos + 1 );
+                        if ( type.equals( ext ) )
+                        {
+                            lastAvceToken = lastAvceToken.substring( 0, extPos );
+
+                            avceTokenList.addLast( lastAvceToken );
+                        }
+                        else
+                        {
+                            throw new DiscovererException( "Path type does not match the extension" );
+                        }
+                    }
+                    else
+                    {
+                        throw new DiscovererException( "Path filename does not have an extension" );
+                    }
+                }
+
+                // let's discover the version, and whatever's leftover will be either
+                // a classifier, or part of the artifactId, depending on position.
+                // Since version is at the end, we have to move in from the back.
+                Collections.reverse( avceTokenList );
+
+                // TODO: this is obscene - surely a better way?
+                String validVersionParts = "([Dd][Ee][Vv][_.0-9]*)|" + "([Ss][Nn][Aa][Pp][Ss][Hh][Oo][Tt])|" +
+                    "([0-9][_.0-9a-zA-Z]*)|" + "([Gg]?[_.0-9ab]*([Pp][Rr][Ee]|[Rr][Cc]|[Gg]|[Mm])[_.0-9]*)|" +
+                    "([Aa][Ll][Pp][Hh][Aa][_.0-9]*)|" + "([Bb][Ee][Tt][Aa][_.0-9]*)|" + "([Rr][Cc][_.0-9]*)|" +
+                    "([Tt][Ee][Ss][Tt][_.0-9]*)|" + "([Dd][Ee][Bb][Uu][Gg][_.0-9]*)|" +
+                    "([Uu][Nn][Oo][Ff][Ff][Ii][Cc][Ii][Aa][Ll][_.0-9]*)|" + "([Cc][Uu][Rr][Rr][Ee][Nn][Tt])|" +
+                    "([Ll][Aa][Tt][Ee][Ss][Tt])|" + "([Ff][Cc][Ss])|" + "([Rr][Ee][Ll][Ee][Aa][Ss][Ee][_.0-9]*)|" +
+                    "([Nn][Ii][Gg][Hh][Tt][Ll][Yy])|" + "[Ff][Ii][Nn][Aa][Ll]|" + "([AaBb][_.0-9]*)";
+
+                StringBuffer classifierBuffer = new StringBuffer();
+                StringBuffer versionBuffer = new StringBuffer();
+
+                boolean firstVersionTokenEncountered = false;
+                boolean firstToken = true;
+
+                int tokensIterated = 0;
+                for ( Iterator it = avceTokenList.iterator(); it.hasNext(); )
+                {
+                    String token = (String) it.next();
+
+                    boolean tokenIsVersionPart = token.matches( validVersionParts );
+
+                    StringBuffer bufferToUpdate;
+
+                    // NOTE: logic in code is reversed, since we're peeling off the back
+                    // Any token after the last versionPart will be in the classifier.
+                    // Any token UP TO first non-versionPart is part of the version.
+                    if ( !tokenIsVersionPart )
+                    {
+                        if ( firstVersionTokenEncountered )
+                        {
+                            //noinspection BreakStatement
+                            break;
+                        }
+                        else
+                        {
+                            bufferToUpdate = classifierBuffer;
+                        }
+                    }
+                    else
+                    {
+                        firstVersionTokenEncountered = true;
+
+                        bufferToUpdate = versionBuffer;
+                    }
+
+                    if ( firstToken )
+                    {
+                        firstToken = false;
+                    }
+                    else
+                    {
+                        bufferToUpdate.insert( 0, '-' );
+                    }
+
+                    bufferToUpdate.insert( 0, token );
+
+                    tokensIterated++;
+                }
+
+                // Now, restore the proper ordering so we can build the artifactId.
+                Collections.reverse( avceTokenList );
+
+                // if we didn't find a version, then punt. Use the last token
+                // as the version, and set the classifier empty.
+                if ( versionBuffer.length() < 1 )
+                {
+                    if ( avceTokenList.size() > 1 )
+                    {
+                        int lastIdx = avceTokenList.size() - 1;
+
+                        versionBuffer.append( avceTokenList.get( lastIdx ) );
+                        avceTokenList.remove( lastIdx );
+                    }
+
+                    classifierBuffer.setLength( 0 );
+                }
+                else
+                {
+                    // if everything is kosher, then pop off all the classifier and
+                    // version tokens, leaving the naked artifact id in the list.
+                    avceTokenList = new LinkedList( avceTokenList.subList( 0, avceTokenList.size() - tokensIterated ) );
+                }
+
+                StringBuffer artifactIdBuffer = new StringBuffer();
+
+                firstToken = true;
+                for ( Iterator it = avceTokenList.iterator(); it.hasNext(); )
+                {
+                    String token = (String) it.next();
+
+                    if ( firstToken )
+                    {
+                        firstToken = false;
+                    }
+                    else
+                    {
+                        artifactIdBuffer.append( '-' );
+                    }
+
+                    artifactIdBuffer.append( token );
+                }
+
+                String artifactId = artifactIdBuffer.toString();
+
+                if ( artifactId.length() > 0 )
+                {
+                    int lastVersionCharIdx = versionBuffer.length() - 1;
+                    if ( lastVersionCharIdx > -1 && versionBuffer.charAt( lastVersionCharIdx ) == '-' )
+                    {
+                        versionBuffer.setLength( lastVersionCharIdx );
+                    }
+
+                    String version = versionBuffer.toString();
+
+                    if ( version.length() > 0 )
+                    {
+                        if ( classifierBuffer.length() > 0 )
+                        {
+                            result = artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, type,
+                                                                                   classifierBuffer.toString() );
+                        }
+                        else
+                        {
+                            result = artifactFactory.createArtifact( groupId, artifactId, version,
+                                                                     Artifact.SCOPE_RUNTIME, type );
+                        }
+                    }
+                    else
+                    {
+                        throw new DiscovererException( "Path filename version is empty" );
+                    }
+                }
+                else
+                {
+                    throw new DiscovererException( "Path filename artifactId is empty" );
+                }
+            }
+            else
+            {
+                throw new DiscovererException( "Path artifact type does not corresspond to an artifact type" );
+            }
+        }
+        else
+        {
+            throw new DiscovererException( "Path does not match a legacy repository path for an artifact" );
+        }
+
+        return result;
+    }
+}
diff --git a/archiva-discoverer/src/main/java/org/apache/maven/archiva/discoverer/MetadataDiscoverer.java b/archiva-discoverer/src/main/java/org/apache/maven/archiva/discoverer/MetadataDiscoverer.java
new file mode 100644 (file)
index 0000000..2c470a3
--- /dev/null
@@ -0,0 +1,42 @@
+package org.apache.maven.archiva.discoverer;
+
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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 org.apache.maven.artifact.repository.ArtifactRepository;
+
+import java.util.List;
+
+/**
+ * Interface for discovering metadata files.
+ */
+public interface MetadataDiscoverer
+    extends Discoverer
+{
+    String ROLE = MetadataDiscoverer.class.getName();
+
+    /**
+     * Search for metadata files in the repository.
+     *
+     * @param repository          The repository.
+     * @param operation           the operation being performed (used for timestamp comparison)
+     * @param blacklistedPatterns Patterns that are to be excluded from the discovery process.
+     * @return the list of artifacts found
+     * @throws DiscovererException if there is a problem during the discovery process
+     */
+    List discoverMetadata( ArtifactRepository repository, String operation, List blacklistedPatterns )
+        throws DiscovererException;
+}
diff --git a/archiva-discoverer/src/main/java/org/apache/maven/archiva/discovery/AbstractArtifactDiscoverer.java b/archiva-discoverer/src/main/java/org/apache/maven/archiva/discovery/AbstractArtifactDiscoverer.java
deleted file mode 100644 (file)
index 9b3a8b9..0000000
+++ /dev/null
@@ -1,149 +0,0 @@
-package org.apache.maven.archiva.discovery;
-
-/*
- * Copyright 2005-2006 The Apache Software Foundation.
- *
- * Licensed 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 org.apache.maven.artifact.Artifact;
-import org.apache.maven.artifact.repository.ArtifactRepository;
-import org.codehaus.plexus.util.xml.Xpp3Dom;
-
-import java.io.File;
-import java.io.IOException;
-import java.text.SimpleDateFormat;
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Locale;
-
-/**
- * Base class for artifact discoverers.
- *
- * @author John Casey
- * @author Brett Porter
- */
-public abstract class AbstractArtifactDiscoverer
-    extends AbstractDiscoverer
-    implements ArtifactDiscoverer
-{
-    /**
-     * Standard patterns to exclude from discovery as they are not artifacts.
-     */
-    private static final String[] STANDARD_DISCOVERY_EXCLUDES = {"bin/**", "reports/**", ".maven/**", "**/*.md5",
-        "**/*.MD5", "**/*.sha1", "**/*.SHA1", "**/*snapshot-version", "*/website/**", "*/licenses/**", "*/licences/**",
-        "**/.htaccess", "**/*.html", "**/*.asc", "**/*.txt", "**/*.xml", "**/README*", "**/CHANGELOG*", "**/KEYS*"};
-
-    private List scanForArtifactPaths( File repositoryBase, List blacklistedPatterns, long comparisonTimestamp )
-    {
-        return scanForArtifactPaths( repositoryBase, blacklistedPatterns, null, STANDARD_DISCOVERY_EXCLUDES,
-                                     comparisonTimestamp );
-    }
-
-    public List discoverArtifacts( ArtifactRepository repository, String operation, List blacklistedPatterns,
-                                   boolean includeSnapshots )
-        throws DiscovererException
-    {
-        if ( !"file".equals( repository.getProtocol() ) )
-        {
-            throw new UnsupportedOperationException( "Only filesystem repositories are supported" );
-        }
-
-        Xpp3Dom dom = getLastArtifactDiscoveryDom( readRepositoryMetadataDom( repository ) );
-        long comparisonTimestamp = readComparisonTimestamp( repository, operation, dom );
-
-        // Note that last checked time is deliberately set to the start of the process so that anything added
-        // mid-discovery and missed by the scanner will get checked next time.
-        // Due to this, there must be no negative side-effects of discovering something twice.
-        Date newLastCheckedTime = new Date();
-
-        File repositoryBase = new File( repository.getBasedir() );
-
-        List artifacts = new ArrayList();
-
-        List artifactPaths = scanForArtifactPaths( repositoryBase, blacklistedPatterns, comparisonTimestamp );
-
-        // Also note that the last check time, while set at the start, is saved at the end, so that if any exceptions
-        // occur, then the timestamp is not updated so that the discovery is attempted again
-        // TODO: under the list-return behaviour we have now, exceptions might occur later and the timestamp will not be reset - see MRM-83
-        try
-        {
-            setLastCheckedTime( repository, operation, newLastCheckedTime );
-        }
-        catch ( IOException e )
-        {
-            throw new DiscovererException( "Error writing metadata: " + e.getMessage(), e );
-        }
-
-        for ( Iterator i = artifactPaths.iterator(); i.hasNext(); )
-        {
-            String path = (String) i.next();
-
-            try
-            {
-                Artifact artifact = buildArtifactFromPath( path, repository );
-
-                if ( includeSnapshots || !artifact.isSnapshot() )
-                {
-                    artifacts.add( artifact );
-                }
-            }
-            catch ( DiscovererException e )
-            {
-                addKickedOutPath( path, e.getMessage() );
-            }
-        }
-
-        return artifacts;
-    }
-
-    /**
-     * Returns an artifact object that is represented by the specified path in a repository
-     *
-     * @param path       The path that is pointing to an artifact
-     * @param repository The repository of the artifact
-     * @return Artifact
-     * @throws DiscovererException when the specified path does correspond to an artifact
-     */
-    public Artifact buildArtifactFromPath( String path, ArtifactRepository repository )
-        throws DiscovererException
-    {
-        Artifact artifact = buildArtifact( path );
-
-        if ( artifact != null )
-        {
-            artifact.setRepository( repository );
-            artifact.setFile( new File( repository.getBasedir(), path ) );
-        }
-
-        return artifact;
-    }
-
-    public void setLastCheckedTime( ArtifactRepository repository, String operation, Date date )
-        throws IOException
-    {
-        // see notes in resetLastCheckedTime
-
-        File file = new File( repository.getBasedir(), "maven-metadata.xml" );
-
-        Xpp3Dom dom = readDom( file );
-
-        String dateString = new SimpleDateFormat( DATE_FMT, Locale.US ).format( date );
-
-        setEntry( getLastArtifactDiscoveryDom( dom ), operation, dateString );
-
-        saveDom( file, dom );
-    }
-}
diff --git a/archiva-discoverer/src/main/java/org/apache/maven/archiva/discovery/AbstractDiscoverer.java b/archiva-discoverer/src/main/java/org/apache/maven/archiva/discovery/AbstractDiscoverer.java
deleted file mode 100644 (file)
index dc90ef0..0000000
+++ /dev/null
@@ -1,302 +0,0 @@
-package org.apache.maven.archiva.discovery;
-
-/*
- * Copyright 2005-2006 The Apache Software Foundation.
- *
- * Licensed 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 org.apache.maven.artifact.factory.ArtifactFactory;
-import org.apache.maven.artifact.repository.ArtifactRepository;
-import org.codehaus.plexus.logging.AbstractLogEnabled;
-import org.codehaus.plexus.util.DirectoryScanner;
-import org.codehaus.plexus.util.FileUtils;
-import org.codehaus.plexus.util.IOUtil;
-import org.codehaus.plexus.util.xml.Xpp3Dom;
-import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
-import org.codehaus.plexus.util.xml.Xpp3DomWriter;
-import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
-
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileReader;
-import java.io.FileWriter;
-import java.io.IOException;
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Locale;
-
-/**
- * Base class for the artifact and metadata discoverers.
- *
- * @author <a href="mailto:brett@apache.org">Brett Porter</a>
- */
-public abstract class AbstractDiscoverer
-    extends AbstractLogEnabled
-    implements Discoverer
-{
-    private List kickedOutPaths = new ArrayList();
-
-    /**
-     * @plexus.requirement
-     */
-    protected ArtifactFactory artifactFactory;
-
-    private static final String[] EMPTY_STRING_ARRAY = new String[0];
-
-    private List excludedPaths = new ArrayList();
-
-    /**
-     * @plexus.configuration default-value="60000"
-     */
-    private int blackoutPeriod;
-
-    protected static final String DATE_FMT = "yyyyMMddHHmmss";
-
-    /**
-     * Add a path to the list of files that were kicked out due to being invalid.
-     *
-     * @param path   the path to add
-     * @param reason the reason why the path is being kicked out
-     */
-    protected void addKickedOutPath( String path, String reason )
-    {
-        kickedOutPaths.add( new DiscovererPath( path, reason ) );
-    }
-
-    /**
-     * Returns an iterator for the list if DiscovererPaths that were found to not represent a searched object
-     *
-     * @return Iterator for the DiscovererPath List
-     */
-    public Iterator getKickedOutPathsIterator()
-    {
-        return kickedOutPaths.iterator();
-    }
-
-    protected List scanForArtifactPaths( File repositoryBase, List blacklistedPatterns, String[] includes,
-                                         String[] excludes, long comparisonTimestamp )
-    {
-        List allExcludes = new ArrayList();
-        allExcludes.addAll( FileUtils.getDefaultExcludesAsList() );
-        if ( excludes != null )
-        {
-            allExcludes.addAll( Arrays.asList( excludes ) );
-        }
-        if ( blacklistedPatterns != null )
-        {
-            allExcludes.addAll( blacklistedPatterns );
-        }
-
-        DirectoryScanner scanner = new DirectoryScanner();
-        scanner.setBasedir( repositoryBase );
-        if ( includes != null )
-        {
-            scanner.setIncludes( includes );
-        }
-        scanner.setExcludes( (String[]) allExcludes.toArray( EMPTY_STRING_ARRAY ) );
-
-        // TODO: Correct for extremely large repositories (artifact counts over 200,000 entries)
-        scanner.scan();
-
-        for ( Iterator files = Arrays.asList( scanner.getExcludedFiles() ).iterator(); files.hasNext(); )
-        {
-            String path = files.next().toString();
-
-            excludedPaths.add( new DiscovererPath( path, "Artifact was in the specified list of exclusions" ) );
-        }
-
-        // TODO: this could be a part of the scanner
-        List includedPaths = new ArrayList();
-        for ( Iterator files = Arrays.asList( scanner.getIncludedFiles() ).iterator(); files.hasNext(); )
-        {
-            String path = files.next().toString();
-
-            long modTime = new File( repositoryBase, path ).lastModified();
-            if ( modTime < System.currentTimeMillis() - blackoutPeriod )
-            {
-                if ( modTime > comparisonTimestamp )
-                {
-                    includedPaths.add( path );
-                }
-            }
-        }
-
-        return includedPaths;
-    }
-
-    /**
-     * Returns an iterator for the list if DiscovererPaths that were not processed because they are explicitly excluded
-     *
-     * @return Iterator for the DiscovererPath List
-     */
-    public Iterator getExcludedPathsIterator()
-    {
-        return excludedPaths.iterator();
-    }
-
-    protected long readComparisonTimestamp( ArtifactRepository repository, String operation, Xpp3Dom dom )
-    {
-        Xpp3Dom entry = dom.getChild( operation );
-        long comparisonTimestamp = 0;
-        if ( entry != null )
-        {
-            try
-            {
-                comparisonTimestamp = new SimpleDateFormat( DATE_FMT, Locale.US ).parse( entry.getValue() ).getTime();
-            }
-            catch ( ParseException e )
-            {
-                getLogger().error( "Timestamp was invalid: " + entry.getValue() + "; ignoring" );
-            }
-        }
-        return comparisonTimestamp;
-    }
-
-    protected Xpp3Dom readDom( File file )
-    {
-        Xpp3Dom dom;
-        FileReader fileReader = null;
-        try
-        {
-            fileReader = new FileReader( file );
-            dom = Xpp3DomBuilder.build( fileReader );
-        }
-        catch ( FileNotFoundException e )
-        {
-            // Safe to ignore
-            dom = new Xpp3Dom( "metadata" );
-        }
-        catch ( XmlPullParserException e )
-        {
-            getLogger().error( "Error reading metadata (ignoring and recreating): " + e.getMessage() );
-            dom = new Xpp3Dom( "metadata" );
-        }
-        catch ( IOException e )
-        {
-            getLogger().error( "Error reading metadata (ignoring and recreating): " + e.getMessage() );
-            dom = new Xpp3Dom( "metadata" );
-        }
-        finally
-        {
-            IOUtil.close( fileReader );
-        }
-        return dom;
-    }
-
-    protected Xpp3Dom getLastArtifactDiscoveryDom( Xpp3Dom dom )
-    {
-        Xpp3Dom lastDiscoveryDom = dom.getChild( "lastArtifactDiscovery" );
-        if ( lastDiscoveryDom == null )
-        {
-            dom.addChild( new Xpp3Dom( "lastArtifactDiscovery" ) );
-            lastDiscoveryDom = dom.getChild( "lastArtifactDiscovery" );
-        }
-        return lastDiscoveryDom;
-    }
-
-    protected Xpp3Dom getLastMetadataDiscoveryDom( Xpp3Dom dom )
-    {
-        Xpp3Dom lastDiscoveryDom = dom.getChild( "lastMetadataDiscovery" );
-        if ( lastDiscoveryDom == null )
-        {
-            dom.addChild( new Xpp3Dom( "lastMetadataDiscovery" ) );
-            lastDiscoveryDom = dom.getChild( "lastMetadataDiscovery" );
-        }
-        return lastDiscoveryDom;
-    }
-
-    public void resetLastCheckedTime( ArtifactRepository repository, String operation )
-        throws IOException
-    {
-        // TODO: get these changes into maven-metadata.xml and migrate towards that. The model is further diverging to a different layout at each level so submodels might be a good idea.
-        // TODO: maven-artifact probably needs an improved pathOfMetadata to cope with top level metadata
-        // TODO: might we need to write this as maven-metadata-local in some circumstances? merge others? Probably best to keep it simple and just use this format at the root. No need to merge anything that I can see
-        // TODO: since this metadata isn't meant to be shared, perhaps another file is called for after all.
-        // Format is: <repository><lastDiscovery><KEY>yyyyMMddHHmmss</KEY></lastDiscovery></repository> (ie, flat properties)
-
-        File file = new File( repository.getBasedir(), "maven-metadata.xml" );
-
-        Xpp3Dom dom = readDom( file );
-
-        boolean changed = false;
-
-        if ( removeEntry( getLastArtifactDiscoveryDom( dom ), operation ) )
-        {
-            changed = true;
-        }
-
-        if ( removeEntry( getLastMetadataDiscoveryDom( dom ), operation ) )
-        {
-            changed = true;
-        }
-
-        if ( changed )
-        {
-            saveDom( file, dom );
-        }
-    }
-
-    private boolean removeEntry( Xpp3Dom lastDiscoveryDom, String operation )
-    {
-        boolean changed = false;
-
-        // do this in reverse so that removing doesn't affect counter
-        Xpp3Dom[] children = lastDiscoveryDom.getChildren();
-        for ( int i = lastDiscoveryDom.getChildCount() - 1; i >= 0; i-- )
-        {
-            if ( children[i].getName().equals( operation ) )
-            {
-                changed = true;
-                lastDiscoveryDom.removeChild( i );
-            }
-        }
-        return changed;
-    }
-
-    protected void saveDom( File file, Xpp3Dom dom )
-        throws IOException
-    {
-        FileWriter writer = new FileWriter( file );
-
-        // save metadata
-        try
-        {
-            Xpp3DomWriter.write( writer, dom );
-        }
-        finally
-        {
-            IOUtil.close( writer );
-        }
-    }
-
-    protected void setEntry( Xpp3Dom lastDiscoveryDom, String operation, String dateString )
-    {
-        Xpp3Dom entry = lastDiscoveryDom.getChild( operation );
-        if ( entry == null )
-        {
-            entry = new Xpp3Dom( operation );
-            lastDiscoveryDom.addChild( entry );
-        }
-        entry.setValue( dateString );
-    }
-
-    protected Xpp3Dom readRepositoryMetadataDom( ArtifactRepository repository )
-    {
-        return readDom( new File( repository.getBasedir(), "maven-metadata.xml" ) );
-    }
-}
diff --git a/archiva-discoverer/src/main/java/org/apache/maven/archiva/discovery/ArtifactDiscoverer.java b/archiva-discoverer/src/main/java/org/apache/maven/archiva/discovery/ArtifactDiscoverer.java
deleted file mode 100644 (file)
index 353a72f..0000000
+++ /dev/null
@@ -1,64 +0,0 @@
-package org.apache.maven.archiva.discovery;
-
-/*
- * Copyright 2005-2006 The Apache Software Foundation.
- *
- * Licensed 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 org.apache.maven.artifact.Artifact;
-import org.apache.maven.artifact.repository.ArtifactRepository;
-
-import java.util.List;
-
-/**
- * Interface for implementation that can discover artifacts within a repository.
- *
- * @author John Casey
- * @author Brett Porter
- * @todo do we want blacklisted patterns in another form? Part of the object construction?
- * @todo should includeSnapshots be configuration on the component? If not, should the methods be changed to include alternates for both possibilities (discoverReleaseArtifacts, discoverReleaseAndSnapshotArtifacts)?
- * @todo instead of a returned list, should a listener be passed in?
- */
-public interface ArtifactDiscoverer
-    extends Discoverer
-{
-    String ROLE = ArtifactDiscoverer.class.getName();
-
-    /**
-     * Discover artifacts in the repository. Only artifacts added since the last attempt at discovery will be found.
-     * This process guarantees never to miss an artifact, however it is possible that an artifact will be received twice
-     * consecutively even if unchanged, so any users of this list must handle such a situation gracefully.
-     *
-     * @param repository          the location of the repository
-     * @param operation           the operation being used to discover for timestamp checking
-     * @param blacklistedPatterns pattern that lists any files to prevent from being included when scanning
-     * @param includeSnapshots    whether to discover snapshots
-     * @return the list of artifacts discovered
-     * @throws DiscovererException if there was an unrecoverable problem discovering artifacts or recording progress
-     */
-    List discoverArtifacts( ArtifactRepository repository, String operation, List blacklistedPatterns,
-                            boolean includeSnapshots )
-        throws DiscovererException;
-
-    /**
-     * Build an artifact from a path in the repository
-     *
-     * @param path the path
-     * @return the artifact
-     * @throws DiscovererException if the file is not a valid artifact
-     * @todo this should be in maven-artifact
-     */
-    Artifact buildArtifact( String path )
-        throws DiscovererException;
-}
diff --git a/archiva-discoverer/src/main/java/org/apache/maven/archiva/discovery/DefaultArtifactDiscoverer.java b/archiva-discoverer/src/main/java/org/apache/maven/archiva/discovery/DefaultArtifactDiscoverer.java
deleted file mode 100644 (file)
index 9cde7cf..0000000
+++ /dev/null
@@ -1,190 +0,0 @@
-package org.apache.maven.archiva.discovery;
-
-/*
- * Copyright 2005-2006 The Apache Software Foundation.
- *
- * Licensed 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 org.apache.maven.artifact.Artifact;
-import org.codehaus.plexus.util.StringUtils;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-import java.util.StringTokenizer;
-
-/**
- * Artifact discoverer for the new repository layout (Maven 2.0+).
- *
- * @author John Casey
- * @author Brett Porter
- * @plexus.component role="org.apache.maven.archiva.discovery.ArtifactDiscoverer" role-hint="default"
- */
-public class DefaultArtifactDiscoverer
-    extends AbstractArtifactDiscoverer
-{
-    /**
-     * @see org.apache.maven.archiva.discovery.ArtifactDiscoverer#buildArtifact(String)
-     */
-    public Artifact buildArtifact( String path )
-        throws DiscovererException
-    {
-        List pathParts = new ArrayList();
-        StringTokenizer st = new StringTokenizer( path, "/\\" );
-        while ( st.hasMoreTokens() )
-        {
-            pathParts.add( st.nextToken() );
-        }
-
-        Collections.reverse( pathParts );
-
-        Artifact artifact;
-        if ( pathParts.size() >= 4 )
-        {
-            // maven 2.x path
-
-            // the actual artifact filename.
-            String filename = (String) pathParts.remove( 0 );
-
-            // the next one is the version.
-            String version = (String) pathParts.remove( 0 );
-
-            // the next one is the artifactId.
-            String artifactId = (String) pathParts.remove( 0 );
-
-            // the remaining are the groupId.
-            Collections.reverse( pathParts );
-            String groupId = StringUtils.join( pathParts.iterator(), "." );
-
-            String remainingFilename = filename;
-            if ( remainingFilename.startsWith( artifactId + "-" ) )
-            {
-                remainingFilename = remainingFilename.substring( artifactId.length() + 1 );
-
-                String classifier = null;
-
-                // TODO: use artifact handler, share with legacy discoverer
-                String type;
-                if ( remainingFilename.endsWith( ".tar.gz" ) )
-                {
-                    type = "distribution-tgz";
-                    remainingFilename =
-                        remainingFilename.substring( 0, remainingFilename.length() - ".tar.gz".length() );
-                }
-                else if ( remainingFilename.endsWith( ".zip" ) )
-                {
-                    type = "distribution-zip";
-                    remainingFilename = remainingFilename.substring( 0, remainingFilename.length() - ".zip".length() );
-                }
-                else if ( remainingFilename.endsWith( "-sources.jar" ) )
-                {
-                    type = "java-source";
-                    classifier = "sources";
-                    remainingFilename =
-                        remainingFilename.substring( 0, remainingFilename.length() - "-sources.jar".length() );
-                }
-                else
-                {
-                    int index = remainingFilename.lastIndexOf( "." );
-                    if ( index >= 0 )
-                    {
-                        type = remainingFilename.substring( index + 1 );
-                        remainingFilename = remainingFilename.substring( 0, index );
-                    }
-                    else
-                    {
-                        throw new DiscovererException( "Path filename does not have an extension" );
-                    }
-                }
-
-                Artifact result;
-                if ( classifier == null )
-                {
-                    result =
-                        artifactFactory.createArtifact( groupId, artifactId, version, Artifact.SCOPE_RUNTIME, type );
-                }
-                else
-                {
-                    result =
-                        artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, type, classifier );
-                }
-
-                if ( result.isSnapshot() )
-                {
-                    // version is *-SNAPSHOT, filename is *-yyyyMMdd.hhmmss-b
-                    int classifierIndex = remainingFilename.indexOf( '-', version.length() + 8 );
-                    if ( classifierIndex >= 0 )
-                    {
-                        classifier = remainingFilename.substring( classifierIndex + 1 );
-                        remainingFilename = remainingFilename.substring( 0, classifierIndex );
-                        result = artifactFactory.createArtifactWithClassifier( groupId, artifactId, remainingFilename,
-                                                                               type, classifier );
-                    }
-                    else
-                    {
-                        result = artifactFactory.createArtifact( groupId, artifactId, remainingFilename,
-                                                                 Artifact.SCOPE_RUNTIME, type );
-                    }
-
-                    // poor encapsulation requires we do this to populate base version
-                    if ( !result.isSnapshot() )
-                    {
-                        throw new DiscovererException( "Failed to create a snapshot artifact: " + result );
-                    }
-                    else if ( !result.getBaseVersion().equals( version ) )
-                    {
-                        throw new DiscovererException(
-                            "Built snapshot artifact base version does not match path version: " + result +
-                                "; should have been version: " + version );
-                    }
-                    else
-                    {
-                        artifact = result;
-                    }
-                }
-                else if ( !remainingFilename.startsWith( version ) )
-                {
-                    throw new DiscovererException( "Built artifact version does not match path version" );
-                }
-                else if ( !remainingFilename.equals( version ) )
-                {
-                    if ( remainingFilename.charAt( version.length() ) == '-' )
-                    {
-                        classifier = remainingFilename.substring( version.length() + 1 );
-                        artifact = artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, type,
-                                                                                 classifier );
-                    }
-                    else
-                    {
-                        throw new DiscovererException( "Path version does not corresspond to an artifact version" );
-                    }
-                }
-                else
-                {
-                    artifact = result;
-                }
-            }
-            else
-            {
-                throw new DiscovererException( "Path filename does not correspond to an artifact" );
-            }
-        }
-        else
-        {
-            throw new DiscovererException( "Path is too short to build an artifact from" );
-        }
-
-        return artifact;
-    }
-}
diff --git a/archiva-discoverer/src/main/java/org/apache/maven/archiva/discovery/DefaultMetadataDiscoverer.java b/archiva-discoverer/src/main/java/org/apache/maven/archiva/discovery/DefaultMetadataDiscoverer.java
deleted file mode 100644 (file)
index 7074774..0000000
+++ /dev/null
@@ -1,254 +0,0 @@
-package org.apache.maven.archiva.discovery;
-
-/*
- * Copyright 2005-2006 The Apache Software Foundation.
- *
- * Licensed 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 org.apache.maven.artifact.Artifact;
-import org.apache.maven.artifact.repository.ArtifactRepository;
-import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
-import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata;
-import org.apache.maven.artifact.repository.metadata.Metadata;
-import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
-import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
-import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
-import org.codehaus.plexus.util.StringUtils;
-import org.codehaus.plexus.util.xml.Xpp3Dom;
-import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
-
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.Reader;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.text.SimpleDateFormat;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Date;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Locale;
-import java.util.StringTokenizer;
-
-/**
- * This class gets all the paths that contain the metadata files.
- *
- * @plexus.component role="org.apache.maven.archiva.discovery.MetadataDiscoverer" role-hint="default"
- */
-public class DefaultMetadataDiscoverer
-    extends AbstractDiscoverer
-    implements MetadataDiscoverer
-{
-    /**
-     * Standard patterns to include in discovery of metadata files.
-     *
-     * @todo Note that only the remote format is supported at this time: you cannot search local repository metadata due
-     * to the way it is later loaded in the searchers. Review code using pathOfRemoteMetadata. IS there any value in
-     * searching the local metadata in the first place though?
-     */
-    private static final String[] STANDARD_DISCOVERY_INCLUDES = {"**/maven-metadata.xml"};
-
-    public List discoverMetadata( ArtifactRepository repository, String operation, List blacklistedPatterns )
-        throws DiscovererException
-    {
-        if ( !"file".equals( repository.getProtocol() ) )
-        {
-            throw new UnsupportedOperationException( "Only filesystem repositories are supported" );
-        }
-
-        Xpp3Dom dom = getLastMetadataDiscoveryDom( readRepositoryMetadataDom( repository ) );
-        long comparisonTimestamp = readComparisonTimestamp( repository, operation, dom );
-
-        // Note that last checked time is deliberately set to the start of the process so that anything added
-        // mid-discovery and missed by the scanner will get checked next time.
-        // Due to this, there must be no negative side-effects of discovering something twice.
-        Date newLastCheckedTime = new Date();
-
-        List metadataFiles = new ArrayList();
-        List metadataPaths = scanForArtifactPaths( new File( repository.getBasedir() ), blacklistedPatterns,
-                                                   STANDARD_DISCOVERY_INCLUDES, null, comparisonTimestamp );
-
-        // Also note that the last check time, while set at the start, is saved at the end, so that if any exceptions
-        // occur, then the timestamp is not updated so that the discovery is attempted again
-        // TODO: under the list-return behaviour we have now, exceptions might occur later and the timestamp will not be reset - see MRM-83
-        try
-        {
-            setLastCheckedTime( repository, operation, newLastCheckedTime );
-        }
-        catch ( IOException e )
-        {
-            throw new DiscovererException( "Error writing metadata: " + e.getMessage(), e );
-        }
-
-        for ( Iterator i = metadataPaths.iterator(); i.hasNext(); )
-        {
-            String metadataPath = (String) i.next();
-            try
-            {
-                RepositoryMetadata metadata = buildMetadata( repository.getBasedir(), metadataPath );
-                metadataFiles.add( metadata );
-            }
-            catch ( DiscovererException e )
-            {
-                addKickedOutPath( metadataPath, e.getMessage() );
-            }
-        }
-
-        return metadataFiles;
-    }
-
-    private RepositoryMetadata buildMetadata( String repo, String metadataPath )
-        throws DiscovererException
-    {
-        Metadata m;
-        String repoPath = repo + "/" + metadataPath;
-        try
-        {
-            URL url = new File( repoPath ).toURI().toURL();
-            InputStream is = url.openStream();
-            Reader reader = new InputStreamReader( is );
-            MetadataXpp3Reader metadataReader = new MetadataXpp3Reader();
-
-            m = metadataReader.read( reader );
-        }
-        catch ( XmlPullParserException e )
-        {
-            throw new DiscovererException( "Error parsing metadata file '" + repoPath + "': " + e.getMessage(), e );
-        }
-        catch ( MalformedURLException e )
-        {
-            // shouldn't happen
-            throw new DiscovererException( "Error constructing metadata file '" + repoPath + "': " + e.getMessage(),
-                                           e );
-        }
-        catch ( IOException e )
-        {
-            throw new DiscovererException( "Error reading metadata file '" + repoPath + "': " + e.getMessage(), e );
-        }
-
-        RepositoryMetadata repositoryMetadata = buildMetadata( m, metadataPath );
-
-        if ( repositoryMetadata == null )
-        {
-            throw new DiscovererException( "Unable to build a repository metadata from path" );
-        }
-
-        return repositoryMetadata;
-    }
-
-    /**
-     * Builds a RepositoryMetadata object from a Metadata object and its path.
-     *
-     * @param m            Metadata
-     * @param metadataPath path
-     * @return RepositoryMetadata if the parameters represent one; null if not
-     * @todo should we just be using the path information, and loading it later when it is needed? (for reporting, etc)
-     */
-    private RepositoryMetadata buildMetadata( Metadata m, String metadataPath )
-    {
-        String metaGroupId = m.getGroupId();
-        String metaArtifactId = m.getArtifactId();
-        String metaVersion = m.getVersion();
-
-        // check if the groupId, artifactId and version is in the
-        // metadataPath
-        // parse the path, in reverse order
-        List pathParts = new ArrayList();
-        StringTokenizer st = new StringTokenizer( metadataPath, "/\\" );
-        while ( st.hasMoreTokens() )
-        {
-            pathParts.add( st.nextToken() );
-        }
-
-        Collections.reverse( pathParts );
-        // remove the metadata file
-        pathParts.remove( 0 );
-        Iterator it = pathParts.iterator();
-        String tmpDir = (String) it.next();
-
-        Artifact artifact = null;
-        if ( !StringUtils.isEmpty( metaVersion ) )
-        {
-            artifact = artifactFactory.createProjectArtifact( metaGroupId, metaArtifactId, metaVersion );
-        }
-
-        // snapshotMetadata
-        RepositoryMetadata metadata = null;
-        if ( tmpDir != null && tmpDir.equals( metaVersion ) )
-        {
-            if ( artifact != null )
-            {
-                metadata = new SnapshotArtifactRepositoryMetadata( artifact );
-            }
-        }
-        else if ( tmpDir != null && tmpDir.equals( metaArtifactId ) )
-        {
-            // artifactMetadata
-            if ( artifact != null )
-            {
-                metadata = new ArtifactRepositoryMetadata( artifact );
-            }
-            else
-            {
-                artifact = artifactFactory.createProjectArtifact( metaGroupId, metaArtifactId, "1.0" );
-                metadata = new ArtifactRepositoryMetadata( artifact );
-            }
-        }
-        else
-        {
-            String groupDir = "";
-            int ctr = 0;
-            for ( it = pathParts.iterator(); it.hasNext(); )
-            {
-                String path = (String) it.next();
-                if ( ctr == 0 )
-                {
-                    groupDir = path;
-                }
-                else
-                {
-                    groupDir = path + "." + groupDir;
-                }
-                ctr++;
-            }
-
-            // groupMetadata
-            if ( metaGroupId != null && metaGroupId.equals( groupDir ) )
-            {
-                metadata = new GroupRepositoryMetadata( metaGroupId );
-            }
-        }
-
-        return metadata;
-    }
-
-    public void setLastCheckedTime( ArtifactRepository repository, String operation, Date date )
-        throws IOException
-    {
-        // see notes in resetLastCheckedTime
-
-        File file = new File( repository.getBasedir(), "maven-metadata.xml" );
-
-        Xpp3Dom dom = readDom( file );
-
-        String dateString = new SimpleDateFormat( DATE_FMT, Locale.US ).format( date );
-
-        setEntry( getLastMetadataDiscoveryDom( dom ), operation, dateString );
-
-        saveDom( file, dom );
-    }
-}
diff --git a/archiva-discoverer/src/main/java/org/apache/maven/archiva/discovery/Discoverer.java b/archiva-discoverer/src/main/java/org/apache/maven/archiva/discovery/Discoverer.java
deleted file mode 100644 (file)
index 8b7d171..0000000
+++ /dev/null
@@ -1,64 +0,0 @@
-package org.apache.maven.archiva.discovery;
-
-import org.apache.maven.artifact.repository.ArtifactRepository;
-
-import java.io.IOException;
-import java.util.Date;
-import java.util.Iterator;
-
-/*
- * Copyright 2005-2006 The Apache Software Foundation.
- *
- * Licensed 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.
- */
-
-/**
- * @author Edwin Punzalan
- */
-public interface Discoverer
-{
-    /**
-     * Get the list of paths kicked out during the discovery process.
-     *
-     * @return the paths as Strings.
-     */
-    Iterator getKickedOutPathsIterator();
-
-    /**
-     * Get the list of paths excluded during the discovery process.
-     *
-     * @return the paths as Strings.
-     */
-    Iterator getExcludedPathsIterator();
-
-    /**
-     * Reset the time in the repository that indicates the last time a check was performed.
-     *
-     * @param repository the location of the repository
-     * @param operation  the operation to record the timestamp for
-     * @throws java.io.IOException if there is a non-recoverable problem reading or writing the metadata
-     */
-    void resetLastCheckedTime( ArtifactRepository repository, String operation )
-        throws IOException;
-
-    /**
-     * Set the time in the repository that indicates the last time a check was performed.
-     *
-     * @param repository the location of the repository
-     * @param operation  the operation to record the timestamp for
-     * @param date       the date to set the last check to
-     * @throws java.io.IOException if there is a non-recoverable problem reading or writing the metadata
-     */
-    void setLastCheckedTime( ArtifactRepository repository, String operation, Date date )
-        throws IOException;
-}
diff --git a/archiva-discoverer/src/main/java/org/apache/maven/archiva/discovery/DiscovererException.java b/archiva-discoverer/src/main/java/org/apache/maven/archiva/discovery/DiscovererException.java
deleted file mode 100644 (file)
index 104f03e..0000000
+++ /dev/null
@@ -1,34 +0,0 @@
-package org.apache.maven.archiva.discovery;
-
-/*
- * Copyright 2005-2006 The Apache Software Foundation.
- *
- * Licensed 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.
- */
-
-/**
- * @author Edwin Punzalan
- */
-public class DiscovererException
-    extends Exception
-{
-    public DiscovererException( String message )
-    {
-        super( message );
-    }
-
-    public DiscovererException( String message, Throwable cause )
-    {
-        super( message, cause );
-    }
-}
diff --git a/archiva-discoverer/src/main/java/org/apache/maven/archiva/discovery/DiscovererPath.java b/archiva-discoverer/src/main/java/org/apache/maven/archiva/discovery/DiscovererPath.java
deleted file mode 100644 (file)
index 0af7dd6..0000000
+++ /dev/null
@@ -1,49 +0,0 @@
-package org.apache.maven.archiva.discovery;
-
-/*
- * Copyright 2005-2006 The Apache Software Foundation.
- *
- * Licensed 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.
- */
-
-/**
- * @author Edwin Punzalan
- */
-public class DiscovererPath
-{
-    /**
-     * The path discovered.
-     */
-    private final String path;
-
-    /**
-     * A comment about why the path is being processed.
-     */
-    private final String comment;
-
-    public DiscovererPath( String path, String comment )
-    {
-        this.path = path;
-        this.comment = comment;
-    }
-
-    public String getPath()
-    {
-        return path;
-    }
-
-    public String getComment()
-    {
-        return comment;
-    }
-}
diff --git a/archiva-discoverer/src/main/java/org/apache/maven/archiva/discovery/LegacyArtifactDiscoverer.java b/archiva-discoverer/src/main/java/org/apache/maven/archiva/discovery/LegacyArtifactDiscoverer.java
deleted file mode 100644 (file)
index 1ccadbe..0000000
+++ /dev/null
@@ -1,280 +0,0 @@
-package org.apache.maven.archiva.discovery;
-
-/*
- * Copyright 2005-2006 The Apache Software Foundation.
- *
- * Licensed 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 org.apache.maven.artifact.Artifact;
-
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.StringTokenizer;
-
-/**
- * Artifact discoverer for the legacy repository layout (Maven 1.x).
- * Method used to build an artifact object using a relative path from a repository base directory.  An artifactId
- * having the words "DEV", "PRE", "RC", "ALPHA", "BETA", "DEBUG", "UNOFFICIAL", "CURRENT", "LATEST", "FCS",
- * "RELEASE", "NIGHTLY", "SNAPSHOT" and "TEST" (not case-sensitive) will most likely make this method fail as
- * they are reserved for version usage.
- *
- * @author John Casey
- * @author Brett Porter
- * @plexus.component role="org.apache.maven.archiva.discovery.ArtifactDiscoverer" role-hint="legacy"
- */
-public class LegacyArtifactDiscoverer
-    extends AbstractArtifactDiscoverer
-{
-    /**
-     * @see org.apache.maven.archiva.discovery.ArtifactDiscoverer#buildArtifact(String)
-     */
-    public Artifact buildArtifact( String path )
-        throws DiscovererException
-    {
-        StringTokenizer tokens = new StringTokenizer( path, "/\\" );
-
-        Artifact result;
-
-        int numberOfTokens = tokens.countTokens();
-
-        if ( numberOfTokens == 3 )
-        {
-            String groupId = tokens.nextToken();
-
-            String type = tokens.nextToken();
-
-            if ( type.endsWith( "s" ) )
-            {
-                type = type.substring( 0, type.length() - 1 );
-
-                // contains artifactId, version, classifier, and extension.
-                String avceGlob = tokens.nextToken();
-
-                //noinspection CollectionDeclaredAsConcreteClass
-                LinkedList avceTokenList = new LinkedList();
-
-                StringTokenizer avceTokenizer = new StringTokenizer( avceGlob, "-" );
-                while ( avceTokenizer.hasMoreTokens() )
-                {
-                    avceTokenList.addLast( avceTokenizer.nextToken() );
-                }
-
-                String lastAvceToken = (String) avceTokenList.removeLast();
-
-                // TODO: share with other discoverer, use artifact handlers instead
-                if ( lastAvceToken.endsWith( ".tar.gz" ) )
-                {
-                    type = "distribution-tgz";
-
-                    lastAvceToken = lastAvceToken.substring( 0, lastAvceToken.length() - ".tar.gz".length() );
-
-                    avceTokenList.addLast( lastAvceToken );
-                }
-                else if ( lastAvceToken.endsWith( "sources.jar" ) )
-                {
-                    type = "java-source";
-
-                    lastAvceToken = lastAvceToken.substring( 0, lastAvceToken.length() - ".jar".length() );
-
-                    avceTokenList.addLast( lastAvceToken );
-                }
-                else if ( lastAvceToken.endsWith( ".zip" ) )
-                {
-                    type = "distribution-zip";
-
-                    lastAvceToken = lastAvceToken.substring( 0, lastAvceToken.length() - ".zip".length() );
-
-                    avceTokenList.addLast( lastAvceToken );
-                }
-                else
-                {
-                    int extPos = lastAvceToken.lastIndexOf( '.' );
-
-                    if ( extPos > 0 )
-                    {
-                        String ext = lastAvceToken.substring( extPos + 1 );
-                        if ( type.equals( ext ) )
-                        {
-                            lastAvceToken = lastAvceToken.substring( 0, extPos );
-
-                            avceTokenList.addLast( lastAvceToken );
-                        }
-                        else
-                        {
-                            throw new DiscovererException( "Path type does not match the extension" );
-                        }
-                    }
-                    else
-                    {
-                        throw new DiscovererException( "Path filename does not have an extension" );
-                    }
-                }
-
-                // let's discover the version, and whatever's leftover will be either
-                // a classifier, or part of the artifactId, depending on position.
-                // Since version is at the end, we have to move in from the back.
-                Collections.reverse( avceTokenList );
-
-                // TODO: this is obscene - surely a better way?
-                String validVersionParts = "([Dd][Ee][Vv][_.0-9]*)|" + "([Ss][Nn][Aa][Pp][Ss][Hh][Oo][Tt])|" +
-                    "([0-9][_.0-9a-zA-Z]*)|" + "([Gg]?[_.0-9ab]*([Pp][Rr][Ee]|[Rr][Cc]|[Gg]|[Mm])[_.0-9]*)|" +
-                    "([Aa][Ll][Pp][Hh][Aa][_.0-9]*)|" + "([Bb][Ee][Tt][Aa][_.0-9]*)|" + "([Rr][Cc][_.0-9]*)|" +
-                    "([Tt][Ee][Ss][Tt][_.0-9]*)|" + "([Dd][Ee][Bb][Uu][Gg][_.0-9]*)|" +
-                    "([Uu][Nn][Oo][Ff][Ff][Ii][Cc][Ii][Aa][Ll][_.0-9]*)|" + "([Cc][Uu][Rr][Rr][Ee][Nn][Tt])|" +
-                    "([Ll][Aa][Tt][Ee][Ss][Tt])|" + "([Ff][Cc][Ss])|" + "([Rr][Ee][Ll][Ee][Aa][Ss][Ee][_.0-9]*)|" +
-                    "([Nn][Ii][Gg][Hh][Tt][Ll][Yy])|" + "[Ff][Ii][Nn][Aa][Ll]|" + "([AaBb][_.0-9]*)";
-
-                StringBuffer classifierBuffer = new StringBuffer();
-                StringBuffer versionBuffer = new StringBuffer();
-
-                boolean firstVersionTokenEncountered = false;
-                boolean firstToken = true;
-
-                int tokensIterated = 0;
-                for ( Iterator it = avceTokenList.iterator(); it.hasNext(); )
-                {
-                    String token = (String) it.next();
-
-                    boolean tokenIsVersionPart = token.matches( validVersionParts );
-
-                    StringBuffer bufferToUpdate;
-
-                    // NOTE: logic in code is reversed, since we're peeling off the back
-                    // Any token after the last versionPart will be in the classifier.
-                    // Any token UP TO first non-versionPart is part of the version.
-                    if ( !tokenIsVersionPart )
-                    {
-                        if ( firstVersionTokenEncountered )
-                        {
-                            //noinspection BreakStatement
-                            break;
-                        }
-                        else
-                        {
-                            bufferToUpdate = classifierBuffer;
-                        }
-                    }
-                    else
-                    {
-                        firstVersionTokenEncountered = true;
-
-                        bufferToUpdate = versionBuffer;
-                    }
-
-                    if ( firstToken )
-                    {
-                        firstToken = false;
-                    }
-                    else
-                    {
-                        bufferToUpdate.insert( 0, '-' );
-                    }
-
-                    bufferToUpdate.insert( 0, token );
-
-                    tokensIterated++;
-                }
-
-                // Now, restore the proper ordering so we can build the artifactId.
-                Collections.reverse( avceTokenList );
-
-                // if we didn't find a version, then punt. Use the last token
-                // as the version, and set the classifier empty.
-                if ( versionBuffer.length() < 1 )
-                {
-                    if ( avceTokenList.size() > 1 )
-                    {
-                        int lastIdx = avceTokenList.size() - 1;
-
-                        versionBuffer.append( avceTokenList.get( lastIdx ) );
-                        avceTokenList.remove( lastIdx );
-                    }
-
-                    classifierBuffer.setLength( 0 );
-                }
-                else
-                {
-                    // if everything is kosher, then pop off all the classifier and
-                    // version tokens, leaving the naked artifact id in the list.
-                    avceTokenList = new LinkedList( avceTokenList.subList( 0, avceTokenList.size() - tokensIterated ) );
-                }
-
-                StringBuffer artifactIdBuffer = new StringBuffer();
-
-                firstToken = true;
-                for ( Iterator it = avceTokenList.iterator(); it.hasNext(); )
-                {
-                    String token = (String) it.next();
-
-                    if ( firstToken )
-                    {
-                        firstToken = false;
-                    }
-                    else
-                    {
-                        artifactIdBuffer.append( '-' );
-                    }
-
-                    artifactIdBuffer.append( token );
-                }
-
-                String artifactId = artifactIdBuffer.toString();
-
-                if ( artifactId.length() > 0 )
-                {
-                    int lastVersionCharIdx = versionBuffer.length() - 1;
-                    if ( lastVersionCharIdx > -1 && versionBuffer.charAt( lastVersionCharIdx ) == '-' )
-                    {
-                        versionBuffer.setLength( lastVersionCharIdx );
-                    }
-
-                    String version = versionBuffer.toString();
-
-                    if ( version.length() > 0 )
-                    {
-                        if ( classifierBuffer.length() > 0 )
-                        {
-                            result = artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, type,
-                                                                                   classifierBuffer.toString() );
-                        }
-                        else
-                        {
-                            result = artifactFactory.createArtifact( groupId, artifactId, version,
-                                                                     Artifact.SCOPE_RUNTIME, type );
-                        }
-                    }
-                    else
-                    {
-                        throw new DiscovererException( "Path filename version is empty" );
-                    }
-                }
-                else
-                {
-                    throw new DiscovererException( "Path filename artifactId is empty" );
-                }
-            }
-            else
-            {
-                throw new DiscovererException( "Path artifact type does not corresspond to an artifact type" );
-            }
-        }
-        else
-        {
-            throw new DiscovererException( "Path does not match a legacy repository path for an artifact" );
-        }
-
-        return result;
-    }
-}
diff --git a/archiva-discoverer/src/main/java/org/apache/maven/archiva/discovery/MetadataDiscoverer.java b/archiva-discoverer/src/main/java/org/apache/maven/archiva/discovery/MetadataDiscoverer.java
deleted file mode 100644 (file)
index 0499080..0000000
+++ /dev/null
@@ -1,42 +0,0 @@
-package org.apache.maven.archiva.discovery;
-
-/*
- * Copyright 2005-2006 The Apache Software Foundation.
- *
- * Licensed 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 org.apache.maven.artifact.repository.ArtifactRepository;
-
-import java.util.List;
-
-/**
- * Interface for discovering metadata files.
- */
-public interface MetadataDiscoverer
-    extends Discoverer
-{
-    String ROLE = MetadataDiscoverer.class.getName();
-
-    /**
-     * Search for metadata files in the repository.
-     *
-     * @param repository          The repository.
-     * @param operation           the operation being performed (used for timestamp comparison)
-     * @param blacklistedPatterns Patterns that are to be excluded from the discovery process.
-     * @return the list of artifacts found
-     * @throws DiscovererException if there is a problem during the discovery process
-     */
-    List discoverMetadata( ArtifactRepository repository, String operation, List blacklistedPatterns )
-        throws DiscovererException;
-}
diff --git a/archiva-discoverer/src/test/java/org/apache/maven/archiva/discoverer/AbstractArtifactDiscovererTest.java b/archiva-discoverer/src/test/java/org/apache/maven/archiva/discoverer/AbstractArtifactDiscovererTest.java
new file mode 100644 (file)
index 0000000..2bd4227
--- /dev/null
@@ -0,0 +1,254 @@
+package org.apache.maven.archiva.discoverer;
+
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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 org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.factory.ArtifactFactory;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
+import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
+import org.codehaus.plexus.PlexusTestCase;
+import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
+
+import java.io.File;
+import java.io.IOException;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.List;
+import java.util.Locale;
+
+/**
+ * @author Edwin Punzalan
+ */
+public abstract class AbstractArtifactDiscovererTest
+    extends PlexusTestCase
+{
+    protected ArtifactDiscoverer discoverer;
+
+    private ArtifactFactory factory;
+
+    protected ArtifactRepository repository;
+
+    protected static final String TEST_OPERATION = "test";
+
+    protected abstract String getLayout();
+
+    protected abstract File getRepositoryFile();
+
+    protected void setUp()
+        throws Exception
+    {
+        super.setUp();
+
+        discoverer = (ArtifactDiscoverer) lookup( ArtifactDiscoverer.ROLE, getLayout() );
+
+        factory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
+
+        repository = getRepository();
+
+        removeTimestampMetadata();
+    }
+
+    protected ArtifactRepository getRepository()
+        throws Exception
+    {
+        File basedir = getRepositoryFile();
+
+        ArtifactRepositoryFactory factory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
+
+        ArtifactRepositoryLayout layout =
+            (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, getLayout() );
+
+        return factory.createArtifactRepository( "discoveryRepo", "file://" + basedir, layout, null, null );
+    }
+
+    protected Artifact createArtifact( String groupId, String artifactId, String version )
+    {
+        Artifact artifact = factory.createArtifact( groupId, artifactId, version, null, "jar" );
+        artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
+        artifact.setRepository( repository );
+        return artifact;
+    }
+
+    protected Artifact createArtifact( String groupId, String artifactId, String version, String type )
+    {
+        return factory.createArtifact( groupId, artifactId, version, null, type );
+    }
+
+    protected Artifact createArtifact( String groupId, String artifactId, String version, String type,
+                                       String classifier )
+    {
+        return factory.createArtifactWithClassifier( groupId, artifactId, version, type, classifier );
+    }
+
+    public void testUpdatedInRepository()
+        throws ComponentLookupException, DiscovererException, ParseException, IOException
+    {
+        // Set repository time to 1-1-2000, a time in the distant past so definitely updated
+        discoverer.setLastCheckedTime( repository, "update",
+                                       new SimpleDateFormat( "yyyy-MM-dd", Locale.US ).parse( "2000-01-01" ) );
+
+        List artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
+        assertNotNull( "Check artifacts not null", artifacts );
+
+        assertTrue( "Check included",
+                    artifacts.contains( createArtifact( "org.apache.maven.update", "test-updated", "1.0" ) ) );
+
+        // try again with the updated timestamp
+        artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
+        assertNotNull( "Check artifacts not null", artifacts );
+
+        assertFalse( "Check not included",
+                     artifacts.contains( createArtifact( "org.apache.maven.update", "test-updated", "1.0" ) ) );
+    }
+
+    public void testNotUpdatedInRepository()
+        throws ComponentLookupException, DiscovererException, IOException
+    {
+        // Set repository time to now, which is after any artifacts, so definitely not updated
+        discoverer.setLastCheckedTime( repository, "update", new Date() );
+
+        List artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
+        assertNotNull( "Check artifacts not null", artifacts );
+
+        assertFalse( "Check not included",
+                     artifacts.contains( createArtifact( "org.apache.maven.update", "test-not-updated", "1.0" ) ) );
+    }
+
+    public void testNotUpdatedInRepositoryForcedDiscovery()
+        throws ComponentLookupException, DiscovererException, IOException
+    {
+        discoverer.resetLastCheckedTime( repository, "update" );
+
+        List artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
+        assertNotNull( "Check artifacts not null", artifacts );
+
+        assertTrue( "Check included",
+                    artifacts.contains( createArtifact( "org.apache.maven.update", "test-not-updated", "1.0" ) ) );
+
+        // try again with the updated timestamp
+        artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
+        assertNotNull( "Check artifacts not null", artifacts );
+
+        assertFalse( "Check not included",
+                     artifacts.contains( createArtifact( "org.apache.maven.update", "test-not-updated", "1.0" ) ) );
+    }
+
+    public void testUpdatedInRepositoryBlackout()
+        throws ComponentLookupException, DiscovererException, IOException
+    {
+        discoverer.resetLastCheckedTime( repository, "update" );
+
+        Artifact artifact = createArtifact( "org.apache.maven.update", "test-not-updated", "1.0" );
+        artifact.getFile().setLastModified( System.currentTimeMillis() );
+
+        List artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
+        assertNotNull( "Check artifacts not null", artifacts );
+
+        assertFalse( "Check not included", artifacts.contains( artifact ) );
+
+        // try again with the updated timestamp
+        artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
+        assertNotNull( "Check artifacts not null", artifacts );
+
+        assertFalse( "Check not included", artifacts.contains( artifact ) );
+    }
+
+    public void testUpdatedInRepositoryNotBlackout()
+        throws ComponentLookupException, DiscovererException, IOException
+    {
+        discoverer.resetLastCheckedTime( repository, "update" );
+
+        Artifact artifact = createArtifact( "org.apache.maven.update", "test-not-updated", "1.0" );
+        artifact.getFile().setLastModified( System.currentTimeMillis() - 61000 );
+
+        List artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
+        assertNotNull( "Check artifacts not null", artifacts );
+
+        assertTrue( "Check included", artifacts.contains( artifact ) );
+
+        // try again with the updated timestamp
+        artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
+        assertNotNull( "Check artifacts not null", artifacts );
+
+        assertFalse( "Check not included", artifacts.contains( artifact ) );
+    }
+
+    public void testNotUpdatedInRepositoryForcedDiscoveryMetadataAlreadyExists()
+        throws ComponentLookupException, DiscovererException, IOException
+    {
+        discoverer.setLastCheckedTime( repository, "update", new Date() );
+
+        discoverer.resetLastCheckedTime( repository, "update" );
+
+        List artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
+        assertNotNull( "Check artifacts not null", artifacts );
+
+        assertTrue( "Check included",
+                    artifacts.contains( createArtifact( "org.apache.maven.update", "test-not-updated", "1.0" ) ) );
+
+        // try again with the updated timestamp
+        artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
+        assertNotNull( "Check artifacts not null", artifacts );
+
+        assertFalse( "Check not included",
+                     artifacts.contains( createArtifact( "org.apache.maven.update", "test-not-updated", "1.0" ) ) );
+    }
+
+    public void testNotUpdatedInRepositoryForcedDiscoveryOtherMetadataAlreadyExists()
+        throws ComponentLookupException, DiscovererException, IOException
+    {
+        discoverer.setLastCheckedTime( repository, "test", new Date() );
+
+        discoverer.resetLastCheckedTime( repository, "update" );
+
+        List artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
+        assertNotNull( "Check artifacts not null", artifacts );
+
+        assertTrue( "Check included",
+                    artifacts.contains( createArtifact( "org.apache.maven.update", "test-not-updated", "1.0" ) ) );
+
+        // try again with the updated timestamp
+        artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
+        assertNotNull( "Check artifacts not null", artifacts );
+
+        assertFalse( "Check not included",
+                     artifacts.contains( createArtifact( "org.apache.maven.update", "test-not-updated", "1.0" ) ) );
+    }
+
+    public void testNoRepositoryMetadata()
+        throws ComponentLookupException, DiscovererException, ParseException, IOException
+    {
+        removeTimestampMetadata();
+
+        // should find all
+        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true );
+        assertNotNull( "Check artifacts not null", artifacts );
+
+        assertTrue( "Check included",
+                    artifacts.contains( createArtifact( "org.apache.maven.update", "test-updated", "1.0" ) ) );
+    }
+
+    private void removeTimestampMetadata()
+    {
+        // remove the metadata that tracks time
+        File file = new File( repository.getBasedir(), "maven-metadata.xml" );
+        file.delete();
+        assertFalse( file.exists() );
+    }
+}
diff --git a/archiva-discoverer/src/test/java/org/apache/maven/archiva/discoverer/DefaultArtifactDiscovererTest.java b/archiva-discoverer/src/test/java/org/apache/maven/archiva/discoverer/DefaultArtifactDiscovererTest.java
new file mode 100644 (file)
index 0000000..a8a8a21
--- /dev/null
@@ -0,0 +1,668 @@
+package org.apache.maven.archiva.discoverer;
+
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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 org.apache.maven.artifact.Artifact;
+import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
+
+import java.io.File;
+import java.net.MalformedURLException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Test the default artifact discoverer.
+ *
+ * @author <a href="mailto:brett@apache.org">Brett Porter</a>
+ * @version $Id:DefaultArtifactDiscovererTest.java 437105 2006-08-26 17:22:22 +1000 (Sat, 26 Aug 2006) brett $
+ */
+public class DefaultArtifactDiscovererTest
+    extends AbstractArtifactDiscovererTest
+{
+    private static final List JAVAX_BLACKLIST = Collections.singletonList( "javax/**" );
+
+    protected String getLayout()
+    {
+        return "default";
+    }
+
+    protected File getRepositoryFile()
+    {
+        return getTestFile( "src/test/repository" );
+    }
+
+    public void testDefaultExcludes()
+        throws DiscovererException
+    {
+        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
+        assertNotNull( "Check artifacts not null", artifacts );
+        boolean found = false;
+        for ( Iterator i = discoverer.getExcludedPathsIterator(); i.hasNext() && !found; )
+        {
+            DiscovererPath dPath = (DiscovererPath) i.next();
+
+            String path = dPath.getPath();
+
+            boolean b = path.indexOf( ".svn" ) >= 0;
+            if ( b )
+            {
+                found = true;
+                assertEquals( "Check comment", "Artifact was in the specified list of exclusions", dPath.getComment() );
+            }
+        }
+        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()
+        throws DiscovererException
+    {
+        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
+        assertNotNull( "Check artifacts not null", artifacts );
+        boolean found = false;
+        for ( Iterator i = discoverer.getExcludedPathsIterator(); i.hasNext() && !found; )
+        {
+            DiscovererPath dPath = (DiscovererPath) i.next();
+
+            String path = dPath.getPath();
+
+            if ( "KEYS".equals( path ) )
+            {
+                found = true;
+                assertEquals( "Check comment", "Artifact was in the specified list of exclusions", dPath.getComment() );
+            }
+        }
+        assertTrue( "Check exclusion was found", found );
+
+        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
+        {
+            Artifact a = (Artifact) i.next();
+            assertFalse( "Check not KEYS", "KEYS".equals( a.getFile().getName() ) );
+        }
+    }
+
+    public void testBlacklistedExclude()
+        throws DiscovererException
+    {
+        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, JAVAX_BLACKLIST, false );
+        assertNotNull( "Check artifacts not null", artifacts );
+        boolean found = false;
+        for ( Iterator i = discoverer.getExcludedPathsIterator(); i.hasNext() && !found; )
+        {
+            DiscovererPath dPath = (DiscovererPath) i.next();
+
+            String path = dPath.getPath();
+
+            if ( "javax/sql/jdbc/2.0/jdbc-2.0.jar".equals( path.replace( '\\', '/' ) ) )
+            {
+                found = true;
+                assertEquals( "Check comment is about blacklisting", "Artifact was in the specified list of exclusions",
+                              dPath.getComment() );
+            }
+        }
+        assertTrue( "Check exclusion was found", found );
+
+        assertFalse( "Check jdbc not included", artifacts.contains( createArtifact( "javax.sql", "jdbc", "2.0" ) ) );
+    }
+
+    public void testKickoutWithShortPath()
+        throws DiscovererException
+    {
+        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
+        assertNotNull( "Check artifacts not null", artifacts );
+        boolean found = false;
+        for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
+        {
+            DiscovererPath dPath = (DiscovererPath) i.next();
+
+            String path = dPath.getPath();
+
+            if ( "invalid/invalid-1.0.jar".equals( path.replace( '\\', '/' ) ) )
+            {
+                found = true;
+                assertEquals( "Check reason for kickout", "Path is too short to build an artifact from",
+                              dPath.getComment() );
+
+            }
+        }
+        assertTrue( "Check kickout was found", found );
+
+        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
+        {
+            Artifact a = (Artifact) i.next();
+            assertFalse( "Check not invalid-1.0.jar", "invalid-1.0.jar".equals( a.getFile().getName() ) );
+        }
+    }
+
+    public void testKickoutWithWrongArtifactId()
+        throws DiscovererException
+    {
+        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
+        assertNotNull( "Check artifacts not null", artifacts );
+        boolean found = false;
+        for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
+        {
+            DiscovererPath dPath = (DiscovererPath) i.next();
+
+            String path = dPath.getPath();
+
+            if ( "org/apache/maven/test/1.0-SNAPSHOT/wrong-artifactId-1.0-20050611.112233-1.jar".equals(
+                path.replace( '\\', '/' ) ) )
+            {
+                found = true;
+                assertEquals( "Check reason for kickout", "Path filename does not correspond to an artifact",
+                              dPath.getComment() );
+            }
+        }
+        assertTrue( "Check kickout was found", found );
+
+        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
+        {
+            Artifact a = (Artifact) i.next();
+            assertFalse( "Check not wrong jar",
+                         "wrong-artifactId-1.0-20050611.112233-1.jar".equals( a.getFile().getName() ) );
+        }
+    }
+
+    public void testKickoutWithNoType()
+        throws DiscovererException
+    {
+        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
+        assertNotNull( "Check artifacts not null", artifacts );
+        boolean found = false;
+        for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
+        {
+            DiscovererPath dPath = (DiscovererPath) i.next();
+
+            String path = dPath.getPath();
+
+            if ( "invalid/invalid/1/invalid-1".equals( path.replace( '\\', '/' ) ) )
+            {
+                found = true;
+                assertEquals( "Check reason for kickout", "Path filename does not have an extension",
+                              dPath.getComment() );
+            }
+        }
+        assertTrue( "Check kickout was found", found );
+
+        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
+        {
+            Artifact a = (Artifact) i.next();
+            assertFalse( "Check not 'invalid-1'", "invalid-1".equals( a.getFile().getName() ) );
+        }
+    }
+
+    public void testKickoutWithWrongVersion()
+        throws DiscovererException
+    {
+        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
+        assertNotNull( "Check artifacts not null", artifacts );
+        boolean found = false;
+        for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
+        {
+            DiscovererPath dPath = (DiscovererPath) i.next();
+
+            String path = dPath.getPath();
+
+            if ( "invalid/invalid/1.0/invalid-2.0.jar".equals( path.replace( '\\', '/' ) ) )
+            {
+                found = true;
+                assertEquals( "Check reason for kickout", "Built artifact version does not match path version",
+                              dPath.getComment() );
+            }
+        }
+        assertTrue( "Check kickout was found", found );
+
+        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
+        {
+            Artifact a = (Artifact) i.next();
+            assertFalse( "Check not 'invalid-2.0.jar'", "invalid-2.0.jar".equals( a.getFile().getName() ) );
+        }
+    }
+
+    public void testKickoutWithLongerVersion()
+        throws DiscovererException
+    {
+        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
+        assertNotNull( "Check artifacts not null", artifacts );
+        boolean found = false;
+        for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
+        {
+            DiscovererPath dPath = (DiscovererPath) i.next();
+
+            String path = dPath.getPath();
+
+            if ( "invalid/invalid/1.0/invalid-1.0b.jar".equals( path.replace( '\\', '/' ) ) )
+            {
+                found = true;
+                assertEquals( "Check reason for kickout", "Path version does not corresspond to an artifact version",
+                              dPath.getComment() );
+            }
+        }
+        assertTrue( "Check kickout was found", found );
+
+        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
+        {
+            Artifact a = (Artifact) i.next();
+            assertFalse( "Check not 'invalid-1.0b.jar'", "invalid-1.0b.jar".equals( a.getFile().getName() ) );
+        }
+    }
+
+    public void testKickoutWithWrongSnapshotVersion()
+        throws DiscovererException
+    {
+        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
+        assertNotNull( "Check artifacts not null", artifacts );
+        boolean found = false;
+        for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
+        {
+            DiscovererPath dPath = (DiscovererPath) i.next();
+
+            String path = dPath.getPath();
+
+            if ( "invalid/invalid/1.0-SNAPSHOT/invalid-1.0.jar".equals( path.replace( '\\', '/' ) ) )
+            {
+                found = true;
+                assertEquals( "Check reason for kickout",
+                              "Failed to create a snapshot artifact: invalid:invalid:jar:1.0:runtime",
+                              dPath.getComment() );
+            }
+        }
+        assertTrue( "Check kickout was found", found );
+
+        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
+        {
+            Artifact a = (Artifact) i.next();
+            assertFalse( "Check not 'invalid-1.0.jar'", "invalid-1.0.jar".equals( a.getFile().getName() ) );
+        }
+    }
+
+    public void testKickoutWithSnapshotBaseVersion()
+        throws DiscovererException
+    {
+        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
+        assertNotNull( "Check artifacts not null", artifacts );
+        boolean found = false;
+        for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
+        {
+            DiscovererPath dPath = (DiscovererPath) i.next();
+
+            String path = dPath.getPath();
+
+            if ( "invalid/invalid/1.0-20050611.123456-1/invalid-1.0-20050611.123456-1.jar".equals(
+                path.replace( '\\', '/' ) ) )
+            {
+                found = true;
+                assertEquals( "Check reason for kickout",
+                              "Built snapshot artifact base version does not match path version: invalid:invalid:jar:1.0-SNAPSHOT:runtime; should have been version: 1.0-20050611.123456-1",
+                              dPath.getComment() );
+            }
+        }
+        assertTrue( "Check kickout was found", found );
+
+        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
+        {
+            Artifact a = (Artifact) i.next();
+            assertFalse( "Check not 'invalid-1.0-20050611-123456-1.jar'",
+                         "invalid-1.0-20050611.123456-1.jar".equals( a.getFile().getName() ) );
+        }
+    }
+
+    public void testInclusion()
+        throws DiscovererException
+    {
+        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true );
+        assertNotNull( "Check artifacts not null", artifacts );
+
+        assertTrue( "Check normal included",
+                    artifacts.contains( createArtifact( "org.apache.maven", "testing", "1.0" ) ) );
+    }
+
+    public void testArtifactWithClassifier()
+        throws DiscovererException
+    {
+        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true );
+        assertNotNull( "Check artifacts not null", artifacts );
+
+        assertTrue( "Check normal included",
+                    artifacts.contains( createArtifact( "org.apache.maven", "some-ejb", "1.0", "jar", "client" ) ) );
+    }
+
+    public void testJavaSourcesInclusion()
+        throws DiscovererException
+    {
+        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true );
+        assertNotNull( "Check artifacts not null", artifacts );
+
+        assertTrue( "Check normal included", artifacts.contains(
+            createArtifact( "org.apache.maven", "testing", "1.0", "java-source", "sources" ) ) );
+    }
+
+    public void testDistributionInclusion()
+        throws DiscovererException
+    {
+        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true );
+        assertNotNull( "Check artifacts not null", artifacts );
+
+        assertTrue( "Check zip included",
+                    artifacts.contains( createArtifact( "org.apache.maven", "testing", "1.0", "distribution-zip" ) ) );
+
+        assertTrue( "Check tar.gz included",
+                    artifacts.contains( createArtifact( "org.apache.maven", "testing", "1.0", "distribution-tgz" ) ) );
+    }
+
+    public void testSnapshotInclusion()
+        throws DiscovererException
+    {
+        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, 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-20050611.112233-1" ) ) );
+    }
+
+    public void testSnapshotInclusionWithClassifier()
+        throws DiscovererException
+    {
+        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true );
+        assertNotNull( "Check artifacts not null", artifacts );
+
+        assertTrue( "Check snapshot included", artifacts.contains(
+            createArtifact( "org.apache.maven", "test", "1.0-20050611.112233-1", "jar", "javadoc" ) ) );
+    }
+
+    public void testSnapshotExclusion()
+        throws DiscovererException
+    {
+        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, 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" ) ) );
+    }
+
+    public void testFileSet()
+        throws DiscovererException
+    {
+        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true );
+        assertNotNull( "Check artifacts not null", artifacts );
+
+        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
+        {
+            Artifact artifact = (Artifact) i.next();
+            assertNotNull( "Check file is set", artifact.getFile() );
+        }
+    }
+
+    public void testRepositorySet()
+        throws MalformedURLException, DiscovererException
+    {
+        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true );
+        assertNotNull( "Check artifacts not null", artifacts );
+
+        String url = repository.getUrl();
+        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
+        {
+            Artifact artifact = (Artifact) i.next();
+            assertNotNull( "Check repository set", artifact.getRepository() );
+            assertEquals( "Check repository url is correct", url, artifact.getRepository().getUrl() );
+        }
+    }
+
+    public void testStandalonePoms()
+        throws DiscovererException
+    {
+        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
+
+        // cull down to actual artifacts (only standalone poms will have type = pom)
+        Map keyedArtifacts = new HashMap();
+        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
+        {
+            Artifact a = (Artifact) i.next();
+            String key = a.getGroupId() + ":" + a.getArtifactId() + ":" + a.getVersion();
+            if ( !"pom".equals( a.getType() ) || !keyedArtifacts.containsKey( key ) )
+            {
+                keyedArtifacts.put( key, a );
+            }
+        }
+
+        List models = new ArrayList();
+
+        for ( Iterator i = keyedArtifacts.values().iterator(); i.hasNext(); )
+        {
+            Artifact a = (Artifact) i.next();
+
+            if ( "pom".equals( a.getType() ) )
+            {
+                models.add( a );
+            }
+        }
+
+        assertEquals( 4, models.size() );
+
+        // Define order we expect
+        Collections.sort( models );
+
+        Iterator itr = models.iterator();
+        Artifact model = (Artifact) itr.next();
+        assertEquals( "org.apache.maven", model.getGroupId() );
+        assertEquals( "B", model.getArtifactId() );
+        assertEquals( "1.0", model.getVersion() );
+        model = (Artifact) itr.next();
+        assertEquals( "org.apache.maven", model.getGroupId() );
+        assertEquals( "B", model.getArtifactId() );
+        assertEquals( "2.0", model.getVersion() );
+        model = (Artifact) itr.next();
+        assertEquals( "org.apache.maven", model.getGroupId() );
+        assertEquals( "discovery", model.getArtifactId() );
+        assertEquals( "1.0", model.getVersion() );
+        model = (Artifact) itr.next();
+        assertEquals( "org.apache.testgroup", model.getGroupId() );
+        assertEquals( "discovery", model.getArtifactId() );
+        assertEquals( "1.0", model.getVersion() );
+    }
+
+    public void testShortPath()
+        throws ComponentLookupException
+    {
+        try
+        {
+            discoverer.buildArtifact( "invalid/invalid-1.0.jar" );
+
+            fail( "Artifact should be null for short paths" );
+        }
+        catch ( DiscovererException e )
+        {
+            // excellent
+        }
+    }
+
+    public void testWrongArtifactId()
+        throws ComponentLookupException
+    {
+
+        try
+        {
+            discoverer.buildArtifact( "org/apache/maven/test/1.0-SNAPSHOT/wrong-artifactId-1.0-20050611.112233-1.jar" );
+
+            fail( "Artifact should be null for wrong ArtifactId" );
+        }
+        catch ( DiscovererException e )
+        {
+            // excellent
+        }
+    }
+
+    public void testNoType()
+        throws ComponentLookupException
+    {
+        try
+        {
+            discoverer.buildArtifact( "invalid/invalid/1/invalid-1" );
+
+            fail( "Artifact should be null for no type" );
+        }
+        catch ( DiscovererException e )
+        {
+            // excellent
+        }
+    }
+
+    public void testWrongVersion()
+        throws ComponentLookupException
+    {
+        try
+        {
+            discoverer.buildArtifact( "invalid/invalid/1.0/invalid-2.0.jar" );
+
+            fail( "Artifact should be null for wrong version" );
+        }
+        catch ( DiscovererException e )
+        {
+            // excellent
+        }
+    }
+
+    public void testLongVersion()
+        throws ComponentLookupException
+    {
+        try
+        {
+            discoverer.buildArtifact( "invalid/invalid/1.0/invalid-1.0b.jar" );
+
+            fail( "Artifact should be null for long version" );
+        }
+        catch ( DiscovererException e )
+        {
+            // excellent
+        }
+    }
+
+    public void testWrongSnapshotVersion()
+        throws ComponentLookupException
+    {
+        try
+        {
+            discoverer.buildArtifact( "invalid/invalid/1.0-SNAPSHOT/invalid-1.0.jar" );
+
+            fail( "Artifact should be null for wrong snapshot version" );
+        }
+        catch ( DiscovererException e )
+        {
+            // excellent
+        }
+    }
+
+    public void testSnapshotBaseVersion()
+        throws ComponentLookupException
+    {
+        try
+        {
+            discoverer.buildArtifact( "invalid/invalid/1.0-20050611.123456-1/invalid-1.0-20050611.123456-1.jar" );
+
+            fail( "Artifact should be null for snapshot base version" );
+        }
+        catch ( DiscovererException e )
+        {
+            // excellent
+        }
+    }
+
+    public void testPathWithClassifier()
+        throws ComponentLookupException, DiscovererException
+    {
+        String testPath = "org/apache/maven/some-ejb/1.0/some-ejb-1.0-client.jar";
+
+        Artifact artifact = discoverer.buildArtifact( testPath );
+
+        assertEquals( createArtifact( "org.apache.maven", "some-ejb", "1.0", "jar", "client" ), artifact );
+    }
+
+    public void testWithJavaSourceInclusion()
+        throws ComponentLookupException, DiscovererException
+    {
+        String testPath = "org/apache/maven/testing/1.0/testing-1.0-sources.jar";
+
+        Artifact artifact = discoverer.buildArtifact( testPath );
+
+        assertEquals( createArtifact( "org.apache.maven", "testing", "1.0", "java-source", "sources" ), artifact );
+    }
+
+    public void testDistributionArtifacts()
+        throws ComponentLookupException, DiscovererException
+    {
+        String testPath = "org/apache/maven/testing/1.0/testing-1.0.tar.gz";
+
+        Artifact artifact = discoverer.buildArtifact( testPath );
+
+        assertEquals( createArtifact( "org.apache.maven", "testing", "1.0", "distribution-tgz" ), artifact );
+
+        testPath = "org/apache/maven/testing/1.0/testing-1.0.zip";
+
+        artifact = discoverer.buildArtifact( testPath );
+
+        assertEquals( createArtifact( "org.apache.maven", "testing", "1.0", "distribution-zip" ), artifact );
+    }
+
+    public void testSnapshot()
+        throws ComponentLookupException, DiscovererException
+    {
+        String testPath = "org/apache/maven/test/1.0-SNAPSHOT/test-1.0-SNAPSHOT.jar";
+
+        Artifact artifact = discoverer.buildArtifact( testPath );
+
+        assertEquals( createArtifact( "org.apache.maven", "test", "1.0-SNAPSHOT" ), artifact );
+
+        testPath = "org/apache/maven/test/1.0-SNAPSHOT/test-1.0-20050611.112233-1.jar";
+
+        artifact = discoverer.buildArtifact( testPath );
+
+        assertEquals( createArtifact( "org.apache.maven", "test", "1.0-20050611.112233-1" ), artifact );
+    }
+
+    public void testNormal()
+        throws ComponentLookupException, DiscovererException
+    {
+        String testPath = "javax/sql/jdbc/2.0/jdbc-2.0.jar";
+
+        Artifact artifact = discoverer.buildArtifact( testPath );
+
+        assertEquals( createArtifact( "javax.sql", "jdbc", "2.0" ), artifact );
+    }
+
+    public void testSnapshotWithClassifier()
+        throws ComponentLookupException, DiscovererException
+    {
+        String testPath = "org/apache/maven/test/1.0-SNAPSHOT/test-1.0-20050611.112233-1-javadoc.jar";
+
+        Artifact artifact = discoverer.buildArtifact( testPath );
+
+        assertEquals( createArtifact( "org.apache.maven", "test", "1.0-20050611.112233-1", "jar", "javadoc" ),
+                      artifact );
+    }
+}
diff --git a/archiva-discoverer/src/test/java/org/apache/maven/archiva/discoverer/DefaultMetadataDiscovererTest.java b/archiva-discoverer/src/test/java/org/apache/maven/archiva/discoverer/DefaultMetadataDiscovererTest.java
new file mode 100644 (file)
index 0000000..9be924c
--- /dev/null
@@ -0,0 +1,311 @@
+package org.apache.maven.archiva.discoverer;
+
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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 org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.factory.ArtifactFactory;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
+import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
+import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
+import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata;
+import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
+import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
+import org.codehaus.plexus.PlexusTestCase;
+import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
+
+import java.io.File;
+import java.io.IOException;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Locale;
+
+/**
+ * This class tests the DefaultMetadataDiscoverer class.
+ */
+public class DefaultMetadataDiscovererTest
+    extends PlexusTestCase
+{
+    private MetadataDiscoverer discoverer;
+
+    private static final String TEST_OPERATION = "test";
+
+    private ArtifactRepository repository;
+
+    private ArtifactFactory factory;
+
+    /**
+     *
+     */
+    public void setUp()
+        throws Exception
+    {
+        super.setUp();
+
+        discoverer = (MetadataDiscoverer) lookup( MetadataDiscoverer.ROLE, "default" );
+
+        factory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
+
+        repository = getRepository();
+
+        removeTimestampMetadata();
+    }
+
+    protected ArtifactRepository getRepository()
+        throws Exception
+    {
+        File basedir = getTestFile( "src/test/repository" );
+
+        ArtifactRepositoryFactory factory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
+
+        ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
+
+        return factory.createArtifactRepository( "discoveryRepo", "file://" + basedir, layout, null, null );
+    }
+
+    /**
+     *
+     */
+    public void tearDown()
+        throws Exception
+    {
+        super.tearDown();
+        discoverer = null;
+    }
+
+    /**
+     * Test if metadata file in wrong directory was added to the kickedOutPaths.
+     */
+    public void testKickoutWrongDirectory()
+        throws DiscovererException
+    {
+        discoverer.discoverMetadata( repository, TEST_OPERATION, null );
+        Iterator iter = discoverer.getKickedOutPathsIterator();
+        boolean found = false;
+        while ( iter.hasNext() && !found )
+        {
+            DiscovererPath dPath = (DiscovererPath) iter.next();
+            String dir = dPath.getPath();
+
+            String normalizedDir = dir.replace( '\\', '/' );
+            if ( "javax/maven-metadata.xml".equals( normalizedDir ) )
+            {
+                found = true;
+                assertEquals( "Check reason for kickout", "Unable to build a repository metadata from path",
+                              dPath.getComment() );
+            }
+        }
+        assertTrue( found );
+    }
+
+    /**
+     * Test if blank metadata file was added to the kickedOutPaths.
+     */
+    public void testKickoutBlankMetadata()
+        throws DiscovererException
+    {
+        discoverer.discoverMetadata( repository, TEST_OPERATION, null );
+        Iterator iter = discoverer.getKickedOutPathsIterator();
+        boolean found = false;
+        while ( iter.hasNext() && !found )
+        {
+            DiscovererPath dPath = (DiscovererPath) iter.next();
+            String dir = dPath.getPath();
+
+            String normalizedDir = dir.replace( '\\', '/' );
+            if ( "org/apache/maven/some-ejb/1.0/maven-metadata.xml".equals( normalizedDir ) )
+            {
+                found = true;
+                assertTrue( "Check reason for kickout", dPath.getComment().matches(
+                    "Error reading metadata file '(.*)': input contained no data" ) );
+            }
+        }
+        assertTrue( found );
+    }
+
+    private void removeTimestampMetadata()
+        throws IOException
+    {
+        // remove the metadata that tracks time
+        File file = new File( repository.getBasedir(), "maven-metadata.xml" );
+        System.gc(); // for Windows
+        file.delete();
+        assertFalse( file.exists() );
+    }
+
+    public void testDiscoverMetadata()
+        throws DiscovererException
+    {
+        List metadataPaths = discoverer.discoverMetadata( repository, TEST_OPERATION, null );
+        assertNotNull( "Check metadata not null", metadataPaths );
+
+        RepositoryMetadata metadata =
+            new ArtifactRepositoryMetadata( createArtifact( "org.apache.testgroup", "discovery" ) );
+        assertTrue( "Check included", containsMetadata( metadataPaths, metadata ) );
+
+        metadata =
+            new SnapshotArtifactRepositoryMetadata( createArtifact( "org.apache.testgroup", "discovery", "1.0" ) );
+        assertTrue( "Check included", containsMetadata( metadataPaths, metadata ) );
+
+        metadata = new GroupRepositoryMetadata( "org.apache.maven" );
+        assertTrue( "Check included", containsMetadata( metadataPaths, metadata ) );
+    }
+
+    public void testUpdatedInRepository()
+        throws ComponentLookupException, DiscovererException, ParseException, IOException
+    {
+        // Set repository time to 1-1-2000, a time in the distant past so definitely updated
+        discoverer.setLastCheckedTime( repository, "update",
+                                       new SimpleDateFormat( "yyyy-MM-dd", Locale.US ).parse( "2000-01-01" ) );
+
+        List metadataPaths = discoverer.discoverMetadata( repository, "update", null );
+        assertNotNull( "Check metadata not null", metadataPaths );
+
+        RepositoryMetadata metadata =
+            new ArtifactRepositoryMetadata( createArtifact( "org.apache.maven.update", "test-updated" ) );
+        assertTrue( "Check included", containsMetadata( metadataPaths, metadata ) );
+
+        // try again with the updated timestamp
+        metadataPaths = discoverer.discoverMetadata( repository, "update", null );
+        assertNotNull( "Check metadata not null", metadataPaths );
+
+        assertFalse( "Check not included", containsMetadata( metadataPaths, metadata ) );
+    }
+
+    private boolean containsMetadata( List metadataPaths, RepositoryMetadata metadata )
+    {
+        for ( Iterator i = metadataPaths.iterator(); i.hasNext(); )
+        {
+            RepositoryMetadata m = (RepositoryMetadata) i.next();
+
+            if ( m.getGroupId().equals( metadata.getGroupId() ) )
+            {
+                if ( m.getArtifactId() == null && metadata.getArtifactId() == null )
+                {
+                    return true;
+                }
+                else if ( m.getArtifactId() != null && m.getArtifactId().equals( metadata.getArtifactId() ) )
+                {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+
+    public void testNotUpdatedInRepository()
+        throws ComponentLookupException, DiscovererException, IOException
+    {
+        // Set repository time to now, which is after any artifacts, so definitely not updated
+        discoverer.setLastCheckedTime( repository, "update", new Date() );
+
+        List metadataPaths = discoverer.discoverMetadata( repository, "update", null );
+        assertNotNull( "Check metadata not null", metadataPaths );
+
+        RepositoryMetadata metadata =
+            new ArtifactRepositoryMetadata( createArtifact( "org.apache.maven.update", "test-not-updated" ) );
+        assertFalse( "Check not included", containsMetadata( metadataPaths, metadata ) );
+    }
+
+    public void testNotUpdatedInRepositoryForcedDiscovery()
+        throws ComponentLookupException, DiscovererException, IOException
+    {
+        discoverer.resetLastCheckedTime( repository, "update" );
+
+        List metadataPaths = discoverer.discoverMetadata( repository, "update", null );
+        assertNotNull( "Check metadata not null", metadataPaths );
+
+        RepositoryMetadata metadata =
+            new ArtifactRepositoryMetadata( createArtifact( "org.apache.maven.update", "test-not-updated" ) );
+        assertTrue( "Check included", containsMetadata( metadataPaths, metadata ) );
+
+        // try again with the updated timestamp
+        metadataPaths = discoverer.discoverMetadata( repository, "update", null );
+        assertNotNull( "Check metadata not null", metadataPaths );
+
+        assertFalse( "Check not included", containsMetadata( metadataPaths, metadata ) );
+    }
+
+    public void testNotUpdatedInRepositoryForcedDiscoveryMetadataAlreadyExists()
+        throws ComponentLookupException, DiscovererException, IOException
+    {
+        discoverer.setLastCheckedTime( repository, "update", new Date() );
+
+        discoverer.resetLastCheckedTime( repository, "update" );
+
+        List metadataPaths = discoverer.discoverMetadata( repository, "update", null );
+        assertNotNull( "Check metadata not null", metadataPaths );
+
+        RepositoryMetadata metadata =
+            new ArtifactRepositoryMetadata( createArtifact( "org.apache.maven.update", "test-not-updated" ) );
+        assertTrue( "Check included", containsMetadata( metadataPaths, metadata ) );
+
+        // try again with the updated timestamp
+        metadataPaths = discoverer.discoverMetadata( repository, "update", null );
+        assertNotNull( "Check metadata not null", metadataPaths );
+
+        assertFalse( "Check not included", containsMetadata( metadataPaths, metadata ) );
+    }
+
+    public void testNotUpdatedInRepositoryForcedDiscoveryOtherMetadataAlreadyExists()
+        throws ComponentLookupException, DiscovererException, IOException
+    {
+        discoverer.setLastCheckedTime( repository, "test", new Date() );
+
+        discoverer.resetLastCheckedTime( repository, "update" );
+
+        List metadataPaths = discoverer.discoverMetadata( repository, "update", null );
+        assertNotNull( "Check metadata not null", metadataPaths );
+
+        RepositoryMetadata metadata =
+            new ArtifactRepositoryMetadata( createArtifact( "org.apache.maven.update", "test-not-updated" ) );
+        assertTrue( "Check included", containsMetadata( metadataPaths, metadata ) );
+
+        // try again with the updated timestamp
+        metadataPaths = discoverer.discoverMetadata( repository, "update", null );
+        assertNotNull( "Check metadata not null", metadataPaths );
+
+        assertFalse( "Check not included", containsMetadata( metadataPaths, metadata ) );
+    }
+
+    public void testNoRepositoryMetadata()
+        throws ComponentLookupException, DiscovererException, ParseException, IOException
+    {
+        removeTimestampMetadata();
+
+        // should find all
+        List metadataPaths = discoverer.discoverMetadata( repository, TEST_OPERATION, null );
+        assertNotNull( "Check metadata not null", metadataPaths );
+
+        RepositoryMetadata metadata =
+            new ArtifactRepositoryMetadata( createArtifact( "org.apache.maven.update", "test-updated" ) );
+        assertTrue( "Check included", containsMetadata( metadataPaths, metadata ) );
+    }
+
+    protected Artifact createArtifact( String groupId, String artifactId )
+    {
+        return createArtifact( groupId, artifactId, "1.0" );
+    }
+
+    private Artifact createArtifact( String groupId, String artifactId, String version )
+    {
+        return factory.createArtifact( groupId, artifactId, version, null, "jar" );
+    }
+}
diff --git a/archiva-discoverer/src/test/java/org/apache/maven/archiva/discoverer/LegacyArtifactDiscovererTest.java b/archiva-discoverer/src/test/java/org/apache/maven/archiva/discoverer/LegacyArtifactDiscovererTest.java
new file mode 100644 (file)
index 0000000..72d0f8f
--- /dev/null
@@ -0,0 +1,482 @@
+package org.apache.maven.archiva.discoverer;
+
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed 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 org.apache.maven.artifact.Artifact;
+import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
+
+import java.io.File;
+import java.net.MalformedURLException;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * Test the legacy artifact discoverer.
+ *
+ * @author <a href="mailto:brett@apache.org">Brett Porter</a>
+ * @version $Id:LegacyArtifactDiscovererTest.java 437105 2006-08-26 17:22:22 +1000 (Sat, 26 Aug 2006) brett $
+ */
+public class LegacyArtifactDiscovererTest
+    extends AbstractArtifactDiscovererTest
+{
+    private static final List JAVAX_SQL_BLACKLIST = Collections.singletonList( "javax.sql/**" );
+
+    protected String getLayout()
+    {
+        return "legacy";
+    }
+
+    protected File getRepositoryFile()
+    {
+        return getTestFile( "src/test/legacy-repository" );
+    }
+
+    public void testDefaultExcludes()
+        throws DiscovererException
+    {
+        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
+        assertNotNull( "Check artifacts not null", artifacts );
+        boolean found = false;
+        for ( Iterator i = discoverer.getExcludedPathsIterator(); i.hasNext() && !found; )
+        {
+            DiscovererPath dPath = (DiscovererPath) i.next();
+
+            String path = dPath.getPath();
+
+            if ( path.indexOf( ".svn" ) >= 0 )
+            {
+                found = true;
+                assertEquals( "Check comment", "Artifact was in the specified list of exclusions", dPath.getComment() );
+            }
+        }
+        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()
+        throws DiscovererException
+    {
+        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
+        assertNotNull( "Check artifacts not null", artifacts );
+        boolean found = false;
+        for ( Iterator i = discoverer.getExcludedPathsIterator(); i.hasNext() && !found; )
+        {
+            DiscovererPath dPath = (DiscovererPath) i.next();
+
+            String path = dPath.getPath();
+
+            if ( "KEYS".equals( path ) )
+            {
+                found = true;
+                assertEquals( "Check comment", "Artifact was in the specified list of exclusions", dPath.getComment() );
+            }
+        }
+        assertTrue( "Check exclusion was found", found );
+
+        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
+        {
+            Artifact a = (Artifact) i.next();
+            assertFalse( "Check not KEYS", "KEYS".equals( a.getFile().getName() ) );
+        }
+    }
+
+    public void testBlacklistedExclude()
+        throws DiscovererException
+    {
+        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, JAVAX_SQL_BLACKLIST, false );
+        assertNotNull( "Check artifacts not null", artifacts );
+        boolean found = false;
+        for ( Iterator i = discoverer.getExcludedPathsIterator(); i.hasNext() && !found; )
+        {
+            DiscovererPath dPath = (DiscovererPath) i.next();
+
+            String path = dPath.getPath();
+
+            if ( "javax.sql/jars/jdbc-2.0.jar".equals( path.replace( '\\', '/' ) ) )
+            {
+                found = true;
+                assertEquals( "Check comment is about blacklisting", "Artifact was in the specified list of exclusions",
+                              dPath.getComment() );
+            }
+        }
+        assertTrue( "Check exclusion was found", found );
+
+        assertFalse( "Check jdbc not included", artifacts.contains( createArtifact( "javax.sql", "jdbc", "2.0" ) ) );
+    }
+
+    public void testKickoutWithShortPath()
+        throws DiscovererException
+    {
+        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
+        assertNotNull( "Check artifacts not null", artifacts );
+        boolean found = false;
+        for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
+        {
+            DiscovererPath dPath = (DiscovererPath) i.next();
+
+            String path = dPath.getPath();
+
+            if ( "invalid/invalid-1.0.jar".equals( path.replace( '\\', '/' ) ) )
+            {
+                found = true;
+                assertEquals( "Check reason for kickout",
+                              "Path does not match a legacy repository path for an artifact", dPath.getComment() );
+            }
+        }
+        assertTrue( "Check kickout was found", found );
+
+        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
+        {
+            Artifact a = (Artifact) i.next();
+            assertFalse( "Check not invalid-1.0.jar", "invalid-1.0.jar".equals( a.getFile().getName() ) );
+        }
+    }
+
+    public void testKickoutWithLongPath()
+        throws DiscovererException
+    {
+        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
+        assertNotNull( "Check artifacts not null", artifacts );
+        boolean found = false;
+        for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
+        {
+            DiscovererPath dPath = (DiscovererPath) i.next();
+
+            String path = dPath.getPath();
+
+            if ( "invalid/jars/1.0/invalid-1.0.jar".equals( path.replace( '\\', '/' ) ) )
+            {
+                found = true;
+                assertEquals( "Check reason for kickout",
+                              "Path does not match a legacy repository path for an artifact", dPath.getComment() );
+            }
+        }
+        assertTrue( "Check kickout was found", found );
+
+        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
+        {
+            Artifact a = (Artifact) i.next();
+            assertFalse( "Check not invalid-1.0.jar", "invalid-1.0.jar".equals( a.getFile().getName() ) );
+        }
+    }
+
+    public void testKickoutWithInvalidType()
+        throws DiscovererException
+    {
+        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
+        assertNotNull( "Check artifacts not null", artifacts );
+        boolean found = false;
+        for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
+        {
+            DiscovererPath dPath = (DiscovererPath) i.next();
+
+            String path = dPath.getPath();
+
+            if ( "invalid/foo/invalid-1.0.foo".equals( path.replace( '\\', '/' ) ) )
+            {
+                found = true;
+                assertEquals( "Check reason for kickout", "Path artifact type does not corresspond to an artifact type",
+                              dPath.getComment() );
+            }
+        }
+        assertTrue( "Check kickout was found", found );
+
+        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
+        {
+            Artifact a = (Artifact) i.next();
+            assertFalse( "Check not invalid-1.0.foo", "invalid-1.0.foo".equals( a.getFile().getName() ) );
+        }
+    }
+
+    public void testKickoutWithNoExtension()
+        throws DiscovererException
+    {
+        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
+        assertNotNull( "Check artifacts not null", artifacts );
+        boolean found = false;
+        for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
+        {
+            DiscovererPath dPath = (DiscovererPath) i.next();
+
+            String path = dPath.getPath();
+
+            if ( "invalid/jars/no-extension".equals( path.replace( '\\', '/' ) ) )
+            {
+                found = true;
+                assertEquals( "Check reason for kickout", "Path filename does not have an extension",
+                              dPath.getComment() );
+            }
+        }
+        assertTrue( "Check kickout was found", found );
+
+        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
+        {
+            Artifact a = (Artifact) i.next();
+            assertFalse( "Check not 'no-extension'", "no-extension".equals( a.getFile().getName() ) );
+        }
+    }
+
+    public void testKickoutWithWrongExtension()
+        throws DiscovererException
+    {
+        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
+        assertNotNull( "Check artifacts not null", artifacts );
+        boolean found = false;
+        for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
+        {
+            DiscovererPath dPath = (DiscovererPath) i.next();
+
+            String path = dPath.getPath();
+
+            if ( "invalid/jars/invalid-1.0.rar".equals( path.replace( '\\', '/' ) ) )
+            {
+                found = true;
+                assertEquals( "Check reason for kickout", "Path type does not match the extension",
+                              dPath.getComment() );
+            }
+        }
+        assertTrue( "Check kickout was found", found );
+
+        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
+        {
+            Artifact a = (Artifact) i.next();
+            assertFalse( "Check not 'invalid-1.0.rar'", "invalid-1.0.rar".equals( a.getFile().getName() ) );
+        }
+    }
+
+    public void testKickoutWithNoVersion()
+        throws DiscovererException
+    {
+        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
+        assertNotNull( "Check artifacts not null", artifacts );
+        boolean found = false;
+        for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
+        {
+            DiscovererPath dPath = (DiscovererPath) i.next();
+
+            String path = dPath.getPath();
+
+            if ( "invalid/jars/invalid.jar".equals( path.replace( '\\', '/' ) ) )
+            {
+                found = true;
+                assertEquals( "Check reason for kickout", "Path filename version is empty", dPath.getComment() );
+            }
+        }
+        assertTrue( "Check kickout was found", found );
+
+        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
+        {
+            Artifact a = (Artifact) i.next();
+            assertFalse( "Check not 'invalid.jar'", "invalid.jar".equals( a.getFile().getName() ) );
+        }
+    }
+
+    public void testInclusion()
+        throws DiscovererException
+    {
+        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true );
+        assertNotNull( "Check artifacts not null", artifacts );
+
+        assertTrue( "Check normal included",
+                    artifacts.contains( createArtifact( "org.apache.maven", "testing", "1.0" ) ) );
+    }
+
+    public void testTextualVersion()
+        throws DiscovererException
+    {
+        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true );
+        assertNotNull( "Check artifacts not null", artifacts );
+
+        assertTrue( "Check normal included",
+                    artifacts.contains( createArtifact( "org.apache.maven", "testing", "UNKNOWN" ) ) );
+    }
+
+    public void testArtifactWithClassifier()
+        throws DiscovererException
+    {
+        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true );
+        assertNotNull( "Check artifacts not null", artifacts );
+
+        assertTrue( "Check normal included",
+                    artifacts.contains( createArtifact( "org.apache.maven", "some-ejb", "1.0", "jar", "client" ) ) );
+    }
+
+    public void testJavaSourcesInclusion()
+        throws DiscovererException
+    {
+        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true );
+        assertNotNull( "Check artifacts not null", artifacts );
+
+        assertTrue( "Check normal included", artifacts.contains(
+            createArtifact( "org.apache.maven", "testing", "1.0", "java-source", "sources" ) ) );
+    }
+
+    public void testDistributionInclusion()
+        throws DiscovererException
+    {
+        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true );
+        assertNotNull( "Check artifacts not null", artifacts );
+
+        assertTrue( "Check zip included",
+                    artifacts.contains( createArtifact( "org.apache.maven", "testing", "1.0", "distribution-zip" ) ) );
+
+        assertTrue( "Check tar.gz included",
+                    artifacts.contains( createArtifact( "org.apache.maven", "testing", "1.0", "distribution-tgz" ) ) );
+    }
+
+    public void testSnapshotInclusion()
+        throws DiscovererException
+    {
+        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, 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", "testing", "1.0-20050611.112233-1" ) ) );
+    }
+
+    public void testSnapshotExclusion()
+        throws DiscovererException
+    {
+        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, 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", "testing", "1.0-20050611.112233-1" ) ) );
+    }
+
+    public void testFileSet()
+        throws DiscovererException
+    {
+        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true );
+        assertNotNull( "Check artifacts not null", artifacts );
+
+        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
+        {
+            Artifact artifact = (Artifact) i.next();
+            assertNotNull( "Check file is set", artifact.getFile() );
+        }
+    }
+
+    public void testRepositorySet()
+        throws MalformedURLException, DiscovererException
+    {
+        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true );
+        assertNotNull( "Check artifacts not null", artifacts );
+
+        String url = repository.getUrl();
+        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
+        {
+            Artifact artifact = (Artifact) i.next();
+            assertNotNull( "Check repository set", artifact.getRepository() );
+            assertEquals( "Check repository url is correct", url, artifact.getRepository().getUrl() );
+        }
+    }
+
+    public void testWrongArtifactPackaging()
+        throws ComponentLookupException, DiscovererException
+    {
+        try
+        {
+            discoverer.buildArtifact( "org.apache.maven.test/jars/artifactId-1.0.jar.md5" );
+
+            fail( "Artifact should be null for wrong package extension" );
+        }
+        catch ( DiscovererException e )
+        {
+            // excellent
+        }
+    }
+
+    public void testNoArtifactId()
+        throws DiscovererException
+    {
+        try
+        {
+            discoverer.buildArtifact( "groupId/jars/-1.0.jar" );
+
+            fail( "Artifact should be null when artifactId is missing" );
+        }
+        catch ( DiscovererException e )
+        {
+            // excellent
+        }
+
+        try
+        {
+            discoverer.buildArtifact( "groupId/jars/1.0.jar" );
+
+            fail( "Artifact should be null when artifactId is missing" );
+        }
+        catch ( DiscovererException e )
+        {
+            // excellent
+        }
+    }
+
+    public void testNoType()
+        throws ComponentLookupException, DiscovererException
+    {
+        try
+        {
+            discoverer.buildArtifact( "invalid/invalid/1/invalid-1" );
+
+            fail( "Artifact should be null for no type" );
+        }
+        catch ( DiscovererException e )
+        {
+            // excellent
+        }
+    }
+
+    public void testSnapshot()
+        throws ComponentLookupException, DiscovererException
+    {
+        String testPath = "org.apache.maven.test/jars/maven-model-1.0-SNAPSHOT.jar";
+
+        Artifact artifact = discoverer.buildArtifact( testPath );
+
+        assertEquals( createArtifact( "org.apache.maven.test", "maven-model", "1.0-SNAPSHOT" ), artifact );
+    }
+
+    public void testFinal()
+        throws ComponentLookupException, DiscovererException
+    {
+        String testPath = "org.apache.maven.test/jars/maven-model-1.0-final-20060606.jar";
+
+        Artifact artifact = discoverer.buildArtifact( testPath );
+
+        assertEquals( createArtifact( "org.apache.maven.test", "maven-model", "1.0-final-20060606" ), artifact );
+    }
+
+    public void testNormal()
+        throws ComponentLookupException, DiscovererException
+    {
+        String testPath = "javax.sql/jars/jdbc-2.0.jar";
+
+        Artifact artifact = discoverer.buildArtifact( testPath );
+
+        assertEquals( createArtifact( "javax.sql", "jdbc", "2.0" ), artifact );
+    }
+}
diff --git a/archiva-discoverer/src/test/java/org/apache/maven/archiva/discovery/AbstractArtifactDiscovererTest.java b/archiva-discoverer/src/test/java/org/apache/maven/archiva/discovery/AbstractArtifactDiscovererTest.java
deleted file mode 100644 (file)
index c448b3d..0000000
+++ /dev/null
@@ -1,254 +0,0 @@
-package org.apache.maven.archiva.discovery;
-
-/*
- * Copyright 2005-2006 The Apache Software Foundation.
- *
- * Licensed 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 org.apache.maven.artifact.Artifact;
-import org.apache.maven.artifact.factory.ArtifactFactory;
-import org.apache.maven.artifact.repository.ArtifactRepository;
-import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
-import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
-import org.codehaus.plexus.PlexusTestCase;
-import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
-
-import java.io.File;
-import java.io.IOException;
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
-import java.util.Date;
-import java.util.List;
-import java.util.Locale;
-
-/**
- * @author Edwin Punzalan
- */
-public abstract class AbstractArtifactDiscovererTest
-    extends PlexusTestCase
-{
-    protected ArtifactDiscoverer discoverer;
-
-    private ArtifactFactory factory;
-
-    protected ArtifactRepository repository;
-
-    protected static final String TEST_OPERATION = "test";
-
-    protected abstract String getLayout();
-
-    protected abstract File getRepositoryFile();
-
-    protected void setUp()
-        throws Exception
-    {
-        super.setUp();
-
-        discoverer = (ArtifactDiscoverer) lookup( ArtifactDiscoverer.ROLE, getLayout() );
-
-        factory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
-
-        repository = getRepository();
-
-        removeTimestampMetadata();
-    }
-
-    protected ArtifactRepository getRepository()
-        throws Exception
-    {
-        File basedir = getRepositoryFile();
-
-        ArtifactRepositoryFactory factory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
-
-        ArtifactRepositoryLayout layout =
-            (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, getLayout() );
-
-        return factory.createArtifactRepository( "discoveryRepo", "file://" + basedir, layout, null, null );
-    }
-
-    protected Artifact createArtifact( String groupId, String artifactId, String version )
-    {
-        Artifact artifact = factory.createArtifact( groupId, artifactId, version, null, "jar" );
-        artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
-        artifact.setRepository( repository );
-        return artifact;
-    }
-
-    protected Artifact createArtifact( String groupId, String artifactId, String version, String type )
-    {
-        return factory.createArtifact( groupId, artifactId, version, null, type );
-    }
-
-    protected Artifact createArtifact( String groupId, String artifactId, String version, String type,
-                                       String classifier )
-    {
-        return factory.createArtifactWithClassifier( groupId, artifactId, version, type, classifier );
-    }
-
-    public void testUpdatedInRepository()
-        throws ComponentLookupException, DiscovererException, ParseException, IOException
-    {
-        // Set repository time to 1-1-2000, a time in the distant past so definitely updated
-        discoverer.setLastCheckedTime( repository, "update",
-                                       new SimpleDateFormat( "yyyy-MM-dd", Locale.US ).parse( "2000-01-01" ) );
-
-        List artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
-        assertNotNull( "Check artifacts not null", artifacts );
-
-        assertTrue( "Check included",
-                    artifacts.contains( createArtifact( "org.apache.maven.update", "test-updated", "1.0" ) ) );
-
-        // try again with the updated timestamp
-        artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
-        assertNotNull( "Check artifacts not null", artifacts );
-
-        assertFalse( "Check not included",
-                     artifacts.contains( createArtifact( "org.apache.maven.update", "test-updated", "1.0" ) ) );
-    }
-
-    public void testNotUpdatedInRepository()
-        throws ComponentLookupException, DiscovererException, IOException
-    {
-        // Set repository time to now, which is after any artifacts, so definitely not updated
-        discoverer.setLastCheckedTime( repository, "update", new Date() );
-
-        List artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
-        assertNotNull( "Check artifacts not null", artifacts );
-
-        assertFalse( "Check not included",
-                     artifacts.contains( createArtifact( "org.apache.maven.update", "test-not-updated", "1.0" ) ) );
-    }
-
-    public void testNotUpdatedInRepositoryForcedDiscovery()
-        throws ComponentLookupException, DiscovererException, IOException
-    {
-        discoverer.resetLastCheckedTime( repository, "update" );
-
-        List artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
-        assertNotNull( "Check artifacts not null", artifacts );
-
-        assertTrue( "Check included",
-                    artifacts.contains( createArtifact( "org.apache.maven.update", "test-not-updated", "1.0" ) ) );
-
-        // try again with the updated timestamp
-        artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
-        assertNotNull( "Check artifacts not null", artifacts );
-
-        assertFalse( "Check not included",
-                     artifacts.contains( createArtifact( "org.apache.maven.update", "test-not-updated", "1.0" ) ) );
-    }
-
-    public void testUpdatedInRepositoryBlackout()
-        throws ComponentLookupException, DiscovererException, IOException
-    {
-        discoverer.resetLastCheckedTime( repository, "update" );
-
-        Artifact artifact = createArtifact( "org.apache.maven.update", "test-not-updated", "1.0" );
-        artifact.getFile().setLastModified( System.currentTimeMillis() );
-
-        List artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
-        assertNotNull( "Check artifacts not null", artifacts );
-
-        assertFalse( "Check not included", artifacts.contains( artifact ) );
-
-        // try again with the updated timestamp
-        artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
-        assertNotNull( "Check artifacts not null", artifacts );
-
-        assertFalse( "Check not included", artifacts.contains( artifact ) );
-    }
-
-    public void testUpdatedInRepositoryNotBlackout()
-        throws ComponentLookupException, DiscovererException, IOException
-    {
-        discoverer.resetLastCheckedTime( repository, "update" );
-
-        Artifact artifact = createArtifact( "org.apache.maven.update", "test-not-updated", "1.0" );
-        artifact.getFile().setLastModified( System.currentTimeMillis() - 61000 );
-
-        List artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
-        assertNotNull( "Check artifacts not null", artifacts );
-
-        assertTrue( "Check included", artifacts.contains( artifact ) );
-
-        // try again with the updated timestamp
-        artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
-        assertNotNull( "Check artifacts not null", artifacts );
-
-        assertFalse( "Check not included", artifacts.contains( artifact ) );
-    }
-
-    public void testNotUpdatedInRepositoryForcedDiscoveryMetadataAlreadyExists()
-        throws ComponentLookupException, DiscovererException, IOException
-    {
-        discoverer.setLastCheckedTime( repository, "update", new Date() );
-
-        discoverer.resetLastCheckedTime( repository, "update" );
-
-        List artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
-        assertNotNull( "Check artifacts not null", artifacts );
-
-        assertTrue( "Check included",
-                    artifacts.contains( createArtifact( "org.apache.maven.update", "test-not-updated", "1.0" ) ) );
-
-        // try again with the updated timestamp
-        artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
-        assertNotNull( "Check artifacts not null", artifacts );
-
-        assertFalse( "Check not included",
-                     artifacts.contains( createArtifact( "org.apache.maven.update", "test-not-updated", "1.0" ) ) );
-    }
-
-    public void testNotUpdatedInRepositoryForcedDiscoveryOtherMetadataAlreadyExists()
-        throws ComponentLookupException, DiscovererException, IOException
-    {
-        discoverer.setLastCheckedTime( repository, "test", new Date() );
-
-        discoverer.resetLastCheckedTime( repository, "update" );
-
-        List artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
-        assertNotNull( "Check artifacts not null", artifacts );
-
-        assertTrue( "Check included",
-                    artifacts.contains( createArtifact( "org.apache.maven.update", "test-not-updated", "1.0" ) ) );
-
-        // try again with the updated timestamp
-        artifacts = discoverer.discoverArtifacts( repository, "update", null, true );
-        assertNotNull( "Check artifacts not null", artifacts );
-
-        assertFalse( "Check not included",
-                     artifacts.contains( createArtifact( "org.apache.maven.update", "test-not-updated", "1.0" ) ) );
-    }
-
-    public void testNoRepositoryMetadata()
-        throws ComponentLookupException, DiscovererException, ParseException, IOException
-    {
-        removeTimestampMetadata();
-
-        // should find all
-        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true );
-        assertNotNull( "Check artifacts not null", artifacts );
-
-        assertTrue( "Check included",
-                    artifacts.contains( createArtifact( "org.apache.maven.update", "test-updated", "1.0" ) ) );
-    }
-
-    private void removeTimestampMetadata()
-    {
-        // remove the metadata that tracks time
-        File file = new File( repository.getBasedir(), "maven-metadata.xml" );
-        file.delete();
-        assertFalse( file.exists() );
-    }
-}
diff --git a/archiva-discoverer/src/test/java/org/apache/maven/archiva/discovery/DefaultArtifactDiscovererTest.java b/archiva-discoverer/src/test/java/org/apache/maven/archiva/discovery/DefaultArtifactDiscovererTest.java
deleted file mode 100644 (file)
index 9c68b02..0000000
+++ /dev/null
@@ -1,668 +0,0 @@
-package org.apache.maven.archiva.discovery;
-
-/*
- * Copyright 2005-2006 The Apache Software Foundation.
- *
- * Licensed 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 org.apache.maven.artifact.Artifact;
-import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
-
-import java.io.File;
-import java.net.MalformedURLException;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-
-/**
- * Test the default artifact discoverer.
- *
- * @author <a href="mailto:brett@apache.org">Brett Porter</a>
- * @version $Id:DefaultArtifactDiscovererTest.java 437105 2006-08-26 17:22:22 +1000 (Sat, 26 Aug 2006) brett $
- */
-public class DefaultArtifactDiscovererTest
-    extends AbstractArtifactDiscovererTest
-{
-    private static final List JAVAX_BLACKLIST = Collections.singletonList( "javax/**" );
-
-    protected String getLayout()
-    {
-        return "default";
-    }
-
-    protected File getRepositoryFile()
-    {
-        return getTestFile( "src/test/repository" );
-    }
-
-    public void testDefaultExcludes()
-        throws DiscovererException
-    {
-        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
-        assertNotNull( "Check artifacts not null", artifacts );
-        boolean found = false;
-        for ( Iterator i = discoverer.getExcludedPathsIterator(); i.hasNext() && !found; )
-        {
-            DiscovererPath dPath = (DiscovererPath) i.next();
-
-            String path = dPath.getPath();
-
-            boolean b = path.indexOf( ".svn" ) >= 0;
-            if ( b )
-            {
-                found = true;
-                assertEquals( "Check comment", "Artifact was in the specified list of exclusions", dPath.getComment() );
-            }
-        }
-        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()
-        throws DiscovererException
-    {
-        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
-        assertNotNull( "Check artifacts not null", artifacts );
-        boolean found = false;
-        for ( Iterator i = discoverer.getExcludedPathsIterator(); i.hasNext() && !found; )
-        {
-            DiscovererPath dPath = (DiscovererPath) i.next();
-
-            String path = dPath.getPath();
-
-            if ( "KEYS".equals( path ) )
-            {
-                found = true;
-                assertEquals( "Check comment", "Artifact was in the specified list of exclusions", dPath.getComment() );
-            }
-        }
-        assertTrue( "Check exclusion was found", found );
-
-        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
-        {
-            Artifact a = (Artifact) i.next();
-            assertFalse( "Check not KEYS", "KEYS".equals( a.getFile().getName() ) );
-        }
-    }
-
-    public void testBlacklistedExclude()
-        throws DiscovererException
-    {
-        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, JAVAX_BLACKLIST, false );
-        assertNotNull( "Check artifacts not null", artifacts );
-        boolean found = false;
-        for ( Iterator i = discoverer.getExcludedPathsIterator(); i.hasNext() && !found; )
-        {
-            DiscovererPath dPath = (DiscovererPath) i.next();
-
-            String path = dPath.getPath();
-
-            if ( "javax/sql/jdbc/2.0/jdbc-2.0.jar".equals( path.replace( '\\', '/' ) ) )
-            {
-                found = true;
-                assertEquals( "Check comment is about blacklisting", "Artifact was in the specified list of exclusions",
-                              dPath.getComment() );
-            }
-        }
-        assertTrue( "Check exclusion was found", found );
-
-        assertFalse( "Check jdbc not included", artifacts.contains( createArtifact( "javax.sql", "jdbc", "2.0" ) ) );
-    }
-
-    public void testKickoutWithShortPath()
-        throws DiscovererException
-    {
-        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
-        assertNotNull( "Check artifacts not null", artifacts );
-        boolean found = false;
-        for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
-        {
-            DiscovererPath dPath = (DiscovererPath) i.next();
-
-            String path = dPath.getPath();
-
-            if ( "invalid/invalid-1.0.jar".equals( path.replace( '\\', '/' ) ) )
-            {
-                found = true;
-                assertEquals( "Check reason for kickout", "Path is too short to build an artifact from",
-                              dPath.getComment() );
-
-            }
-        }
-        assertTrue( "Check kickout was found", found );
-
-        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
-        {
-            Artifact a = (Artifact) i.next();
-            assertFalse( "Check not invalid-1.0.jar", "invalid-1.0.jar".equals( a.getFile().getName() ) );
-        }
-    }
-
-    public void testKickoutWithWrongArtifactId()
-        throws DiscovererException
-    {
-        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
-        assertNotNull( "Check artifacts not null", artifacts );
-        boolean found = false;
-        for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
-        {
-            DiscovererPath dPath = (DiscovererPath) i.next();
-
-            String path = dPath.getPath();
-
-            if ( "org/apache/maven/test/1.0-SNAPSHOT/wrong-artifactId-1.0-20050611.112233-1.jar".equals(
-                path.replace( '\\', '/' ) ) )
-            {
-                found = true;
-                assertEquals( "Check reason for kickout", "Path filename does not correspond to an artifact",
-                              dPath.getComment() );
-            }
-        }
-        assertTrue( "Check kickout was found", found );
-
-        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
-        {
-            Artifact a = (Artifact) i.next();
-            assertFalse( "Check not wrong jar",
-                         "wrong-artifactId-1.0-20050611.112233-1.jar".equals( a.getFile().getName() ) );
-        }
-    }
-
-    public void testKickoutWithNoType()
-        throws DiscovererException
-    {
-        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
-        assertNotNull( "Check artifacts not null", artifacts );
-        boolean found = false;
-        for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
-        {
-            DiscovererPath dPath = (DiscovererPath) i.next();
-
-            String path = dPath.getPath();
-
-            if ( "invalid/invalid/1/invalid-1".equals( path.replace( '\\', '/' ) ) )
-            {
-                found = true;
-                assertEquals( "Check reason for kickout", "Path filename does not have an extension",
-                              dPath.getComment() );
-            }
-        }
-        assertTrue( "Check kickout was found", found );
-
-        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
-        {
-            Artifact a = (Artifact) i.next();
-            assertFalse( "Check not 'invalid-1'", "invalid-1".equals( a.getFile().getName() ) );
-        }
-    }
-
-    public void testKickoutWithWrongVersion()
-        throws DiscovererException
-    {
-        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
-        assertNotNull( "Check artifacts not null", artifacts );
-        boolean found = false;
-        for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
-        {
-            DiscovererPath dPath = (DiscovererPath) i.next();
-
-            String path = dPath.getPath();
-
-            if ( "invalid/invalid/1.0/invalid-2.0.jar".equals( path.replace( '\\', '/' ) ) )
-            {
-                found = true;
-                assertEquals( "Check reason for kickout", "Built artifact version does not match path version",
-                              dPath.getComment() );
-            }
-        }
-        assertTrue( "Check kickout was found", found );
-
-        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
-        {
-            Artifact a = (Artifact) i.next();
-            assertFalse( "Check not 'invalid-2.0.jar'", "invalid-2.0.jar".equals( a.getFile().getName() ) );
-        }
-    }
-
-    public void testKickoutWithLongerVersion()
-        throws DiscovererException
-    {
-        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
-        assertNotNull( "Check artifacts not null", artifacts );
-        boolean found = false;
-        for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
-        {
-            DiscovererPath dPath = (DiscovererPath) i.next();
-
-            String path = dPath.getPath();
-
-            if ( "invalid/invalid/1.0/invalid-1.0b.jar".equals( path.replace( '\\', '/' ) ) )
-            {
-                found = true;
-                assertEquals( "Check reason for kickout", "Path version does not corresspond to an artifact version",
-                              dPath.getComment() );
-            }
-        }
-        assertTrue( "Check kickout was found", found );
-
-        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
-        {
-            Artifact a = (Artifact) i.next();
-            assertFalse( "Check not 'invalid-1.0b.jar'", "invalid-1.0b.jar".equals( a.getFile().getName() ) );
-        }
-    }
-
-    public void testKickoutWithWrongSnapshotVersion()
-        throws DiscovererException
-    {
-        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
-        assertNotNull( "Check artifacts not null", artifacts );
-        boolean found = false;
-        for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
-        {
-            DiscovererPath dPath = (DiscovererPath) i.next();
-
-            String path = dPath.getPath();
-
-            if ( "invalid/invalid/1.0-SNAPSHOT/invalid-1.0.jar".equals( path.replace( '\\', '/' ) ) )
-            {
-                found = true;
-                assertEquals( "Check reason for kickout",
-                              "Failed to create a snapshot artifact: invalid:invalid:jar:1.0:runtime",
-                              dPath.getComment() );
-            }
-        }
-        assertTrue( "Check kickout was found", found );
-
-        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
-        {
-            Artifact a = (Artifact) i.next();
-            assertFalse( "Check not 'invalid-1.0.jar'", "invalid-1.0.jar".equals( a.getFile().getName() ) );
-        }
-    }
-
-    public void testKickoutWithSnapshotBaseVersion()
-        throws DiscovererException
-    {
-        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
-        assertNotNull( "Check artifacts not null", artifacts );
-        boolean found = false;
-        for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
-        {
-            DiscovererPath dPath = (DiscovererPath) i.next();
-
-            String path = dPath.getPath();
-
-            if ( "invalid/invalid/1.0-20050611.123456-1/invalid-1.0-20050611.123456-1.jar".equals(
-                path.replace( '\\', '/' ) ) )
-            {
-                found = true;
-                assertEquals( "Check reason for kickout",
-                              "Built snapshot artifact base version does not match path version: invalid:invalid:jar:1.0-SNAPSHOT:runtime; should have been version: 1.0-20050611.123456-1",
-                              dPath.getComment() );
-            }
-        }
-        assertTrue( "Check kickout was found", found );
-
-        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
-        {
-            Artifact a = (Artifact) i.next();
-            assertFalse( "Check not 'invalid-1.0-20050611-123456-1.jar'",
-                         "invalid-1.0-20050611.123456-1.jar".equals( a.getFile().getName() ) );
-        }
-    }
-
-    public void testInclusion()
-        throws DiscovererException
-    {
-        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true );
-        assertNotNull( "Check artifacts not null", artifacts );
-
-        assertTrue( "Check normal included",
-                    artifacts.contains( createArtifact( "org.apache.maven", "testing", "1.0" ) ) );
-    }
-
-    public void testArtifactWithClassifier()
-        throws DiscovererException
-    {
-        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true );
-        assertNotNull( "Check artifacts not null", artifacts );
-
-        assertTrue( "Check normal included",
-                    artifacts.contains( createArtifact( "org.apache.maven", "some-ejb", "1.0", "jar", "client" ) ) );
-    }
-
-    public void testJavaSourcesInclusion()
-        throws DiscovererException
-    {
-        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true );
-        assertNotNull( "Check artifacts not null", artifacts );
-
-        assertTrue( "Check normal included", artifacts.contains(
-            createArtifact( "org.apache.maven", "testing", "1.0", "java-source", "sources" ) ) );
-    }
-
-    public void testDistributionInclusion()
-        throws DiscovererException
-    {
-        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true );
-        assertNotNull( "Check artifacts not null", artifacts );
-
-        assertTrue( "Check zip included",
-                    artifacts.contains( createArtifact( "org.apache.maven", "testing", "1.0", "distribution-zip" ) ) );
-
-        assertTrue( "Check tar.gz included",
-                    artifacts.contains( createArtifact( "org.apache.maven", "testing", "1.0", "distribution-tgz" ) ) );
-    }
-
-    public void testSnapshotInclusion()
-        throws DiscovererException
-    {
-        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, 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-20050611.112233-1" ) ) );
-    }
-
-    public void testSnapshotInclusionWithClassifier()
-        throws DiscovererException
-    {
-        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true );
-        assertNotNull( "Check artifacts not null", artifacts );
-
-        assertTrue( "Check snapshot included", artifacts.contains(
-            createArtifact( "org.apache.maven", "test", "1.0-20050611.112233-1", "jar", "javadoc" ) ) );
-    }
-
-    public void testSnapshotExclusion()
-        throws DiscovererException
-    {
-        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, 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" ) ) );
-    }
-
-    public void testFileSet()
-        throws DiscovererException
-    {
-        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true );
-        assertNotNull( "Check artifacts not null", artifacts );
-
-        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
-        {
-            Artifact artifact = (Artifact) i.next();
-            assertNotNull( "Check file is set", artifact.getFile() );
-        }
-    }
-
-    public void testRepositorySet()
-        throws MalformedURLException, DiscovererException
-    {
-        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true );
-        assertNotNull( "Check artifacts not null", artifacts );
-
-        String url = repository.getUrl();
-        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
-        {
-            Artifact artifact = (Artifact) i.next();
-            assertNotNull( "Check repository set", artifact.getRepository() );
-            assertEquals( "Check repository url is correct", url, artifact.getRepository().getUrl() );
-        }
-    }
-
-    public void testStandalonePoms()
-        throws DiscovererException
-    {
-        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
-
-        // cull down to actual artifacts (only standalone poms will have type = pom)
-        Map keyedArtifacts = new HashMap();
-        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
-        {
-            Artifact a = (Artifact) i.next();
-            String key = a.getGroupId() + ":" + a.getArtifactId() + ":" + a.getVersion();
-            if ( !"pom".equals( a.getType() ) || !keyedArtifacts.containsKey( key ) )
-            {
-                keyedArtifacts.put( key, a );
-            }
-        }
-
-        List models = new ArrayList();
-
-        for ( Iterator i = keyedArtifacts.values().iterator(); i.hasNext(); )
-        {
-            Artifact a = (Artifact) i.next();
-
-            if ( "pom".equals( a.getType() ) )
-            {
-                models.add( a );
-            }
-        }
-
-        assertEquals( 4, models.size() );
-
-        // Define order we expect
-        Collections.sort( models );
-
-        Iterator itr = models.iterator();
-        Artifact model = (Artifact) itr.next();
-        assertEquals( "org.apache.maven", model.getGroupId() );
-        assertEquals( "B", model.getArtifactId() );
-        assertEquals( "1.0", model.getVersion() );
-        model = (Artifact) itr.next();
-        assertEquals( "org.apache.maven", model.getGroupId() );
-        assertEquals( "B", model.getArtifactId() );
-        assertEquals( "2.0", model.getVersion() );
-        model = (Artifact) itr.next();
-        assertEquals( "org.apache.maven", model.getGroupId() );
-        assertEquals( "discovery", model.getArtifactId() );
-        assertEquals( "1.0", model.getVersion() );
-        model = (Artifact) itr.next();
-        assertEquals( "org.apache.testgroup", model.getGroupId() );
-        assertEquals( "discovery", model.getArtifactId() );
-        assertEquals( "1.0", model.getVersion() );
-    }
-
-    public void testShortPath()
-        throws ComponentLookupException
-    {
-        try
-        {
-            discoverer.buildArtifact( "invalid/invalid-1.0.jar" );
-
-            fail( "Artifact should be null for short paths" );
-        }
-        catch ( DiscovererException e )
-        {
-            // excellent
-        }
-    }
-
-    public void testWrongArtifactId()
-        throws ComponentLookupException
-    {
-
-        try
-        {
-            discoverer.buildArtifact( "org/apache/maven/test/1.0-SNAPSHOT/wrong-artifactId-1.0-20050611.112233-1.jar" );
-
-            fail( "Artifact should be null for wrong ArtifactId" );
-        }
-        catch ( DiscovererException e )
-        {
-            // excellent
-        }
-    }
-
-    public void testNoType()
-        throws ComponentLookupException
-    {
-        try
-        {
-            discoverer.buildArtifact( "invalid/invalid/1/invalid-1" );
-
-            fail( "Artifact should be null for no type" );
-        }
-        catch ( DiscovererException e )
-        {
-            // excellent
-        }
-    }
-
-    public void testWrongVersion()
-        throws ComponentLookupException
-    {
-        try
-        {
-            discoverer.buildArtifact( "invalid/invalid/1.0/invalid-2.0.jar" );
-
-            fail( "Artifact should be null for wrong version" );
-        }
-        catch ( DiscovererException e )
-        {
-            // excellent
-        }
-    }
-
-    public void testLongVersion()
-        throws ComponentLookupException
-    {
-        try
-        {
-            discoverer.buildArtifact( "invalid/invalid/1.0/invalid-1.0b.jar" );
-
-            fail( "Artifact should be null for long version" );
-        }
-        catch ( DiscovererException e )
-        {
-            // excellent
-        }
-    }
-
-    public void testWrongSnapshotVersion()
-        throws ComponentLookupException
-    {
-        try
-        {
-            discoverer.buildArtifact( "invalid/invalid/1.0-SNAPSHOT/invalid-1.0.jar" );
-
-            fail( "Artifact should be null for wrong snapshot version" );
-        }
-        catch ( DiscovererException e )
-        {
-            // excellent
-        }
-    }
-
-    public void testSnapshotBaseVersion()
-        throws ComponentLookupException
-    {
-        try
-        {
-            discoverer.buildArtifact( "invalid/invalid/1.0-20050611.123456-1/invalid-1.0-20050611.123456-1.jar" );
-
-            fail( "Artifact should be null for snapshot base version" );
-        }
-        catch ( DiscovererException e )
-        {
-            // excellent
-        }
-    }
-
-    public void testPathWithClassifier()
-        throws ComponentLookupException, DiscovererException
-    {
-        String testPath = "org/apache/maven/some-ejb/1.0/some-ejb-1.0-client.jar";
-
-        Artifact artifact = discoverer.buildArtifact( testPath );
-
-        assertEquals( createArtifact( "org.apache.maven", "some-ejb", "1.0", "jar", "client" ), artifact );
-    }
-
-    public void testWithJavaSourceInclusion()
-        throws ComponentLookupException, DiscovererException
-    {
-        String testPath = "org/apache/maven/testing/1.0/testing-1.0-sources.jar";
-
-        Artifact artifact = discoverer.buildArtifact( testPath );
-
-        assertEquals( createArtifact( "org.apache.maven", "testing", "1.0", "java-source", "sources" ), artifact );
-    }
-
-    public void testDistributionArtifacts()
-        throws ComponentLookupException, DiscovererException
-    {
-        String testPath = "org/apache/maven/testing/1.0/testing-1.0.tar.gz";
-
-        Artifact artifact = discoverer.buildArtifact( testPath );
-
-        assertEquals( createArtifact( "org.apache.maven", "testing", "1.0", "distribution-tgz" ), artifact );
-
-        testPath = "org/apache/maven/testing/1.0/testing-1.0.zip";
-
-        artifact = discoverer.buildArtifact( testPath );
-
-        assertEquals( createArtifact( "org.apache.maven", "testing", "1.0", "distribution-zip" ), artifact );
-    }
-
-    public void testSnapshot()
-        throws ComponentLookupException, DiscovererException
-    {
-        String testPath = "org/apache/maven/test/1.0-SNAPSHOT/test-1.0-SNAPSHOT.jar";
-
-        Artifact artifact = discoverer.buildArtifact( testPath );
-
-        assertEquals( createArtifact( "org.apache.maven", "test", "1.0-SNAPSHOT" ), artifact );
-
-        testPath = "org/apache/maven/test/1.0-SNAPSHOT/test-1.0-20050611.112233-1.jar";
-
-        artifact = discoverer.buildArtifact( testPath );
-
-        assertEquals( createArtifact( "org.apache.maven", "test", "1.0-20050611.112233-1" ), artifact );
-    }
-
-    public void testNormal()
-        throws ComponentLookupException, DiscovererException
-    {
-        String testPath = "javax/sql/jdbc/2.0/jdbc-2.0.jar";
-
-        Artifact artifact = discoverer.buildArtifact( testPath );
-
-        assertEquals( createArtifact( "javax.sql", "jdbc", "2.0" ), artifact );
-    }
-
-    public void testSnapshotWithClassifier()
-        throws ComponentLookupException, DiscovererException
-    {
-        String testPath = "org/apache/maven/test/1.0-SNAPSHOT/test-1.0-20050611.112233-1-javadoc.jar";
-
-        Artifact artifact = discoverer.buildArtifact( testPath );
-
-        assertEquals( createArtifact( "org.apache.maven", "test", "1.0-20050611.112233-1", "jar", "javadoc" ),
-                      artifact );
-    }
-}
diff --git a/archiva-discoverer/src/test/java/org/apache/maven/archiva/discovery/DefaultMetadataDiscovererTest.java b/archiva-discoverer/src/test/java/org/apache/maven/archiva/discovery/DefaultMetadataDiscovererTest.java
deleted file mode 100644 (file)
index cdc5596..0000000
+++ /dev/null
@@ -1,311 +0,0 @@
-package org.apache.maven.archiva.discovery;
-
-/*
- * Copyright 2005-2006 The Apache Software Foundation.
- *
- * Licensed 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 org.apache.maven.artifact.Artifact;
-import org.apache.maven.artifact.factory.ArtifactFactory;
-import org.apache.maven.artifact.repository.ArtifactRepository;
-import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
-import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
-import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
-import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata;
-import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
-import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
-import org.codehaus.plexus.PlexusTestCase;
-import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
-
-import java.io.File;
-import java.io.IOException;
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
-import java.util.Date;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Locale;
-
-/**
- * This class tests the DefaultMetadataDiscoverer class.
- */
-public class DefaultMetadataDiscovererTest
-    extends PlexusTestCase
-{
-    private MetadataDiscoverer discoverer;
-
-    private static final String TEST_OPERATION = "test";
-
-    private ArtifactRepository repository;
-
-    private ArtifactFactory factory;
-
-    /**
-     *
-     */
-    public void setUp()
-        throws Exception
-    {
-        super.setUp();
-
-        discoverer = (MetadataDiscoverer) lookup( MetadataDiscoverer.ROLE, "default" );
-
-        factory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
-
-        repository = getRepository();
-
-        removeTimestampMetadata();
-    }
-
-    protected ArtifactRepository getRepository()
-        throws Exception
-    {
-        File basedir = getTestFile( "src/test/repository" );
-
-        ArtifactRepositoryFactory factory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
-
-        ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
-
-        return factory.createArtifactRepository( "discoveryRepo", "file://" + basedir, layout, null, null );
-    }
-
-    /**
-     *
-     */
-    public void tearDown()
-        throws Exception
-    {
-        super.tearDown();
-        discoverer = null;
-    }
-
-    /**
-     * Test if metadata file in wrong directory was added to the kickedOutPaths.
-     */
-    public void testKickoutWrongDirectory()
-        throws DiscovererException
-    {
-        discoverer.discoverMetadata( repository, TEST_OPERATION, null );
-        Iterator iter = discoverer.getKickedOutPathsIterator();
-        boolean found = false;
-        while ( iter.hasNext() && !found )
-        {
-            DiscovererPath dPath = (DiscovererPath) iter.next();
-            String dir = dPath.getPath();
-
-            String normalizedDir = dir.replace( '\\', '/' );
-            if ( "javax/maven-metadata.xml".equals( normalizedDir ) )
-            {
-                found = true;
-                assertEquals( "Check reason for kickout", "Unable to build a repository metadata from path",
-                              dPath.getComment() );
-            }
-        }
-        assertTrue( found );
-    }
-
-    /**
-     * Test if blank metadata file was added to the kickedOutPaths.
-     */
-    public void testKickoutBlankMetadata()
-        throws DiscovererException
-    {
-        discoverer.discoverMetadata( repository, TEST_OPERATION, null );
-        Iterator iter = discoverer.getKickedOutPathsIterator();
-        boolean found = false;
-        while ( iter.hasNext() && !found )
-        {
-            DiscovererPath dPath = (DiscovererPath) iter.next();
-            String dir = dPath.getPath();
-
-            String normalizedDir = dir.replace( '\\', '/' );
-            if ( "org/apache/maven/some-ejb/1.0/maven-metadata.xml".equals( normalizedDir ) )
-            {
-                found = true;
-                assertTrue( "Check reason for kickout", dPath.getComment().matches(
-                    "Error reading metadata file '(.*)': input contained no data" ) );
-            }
-        }
-        assertTrue( found );
-    }
-
-    private void removeTimestampMetadata()
-        throws IOException
-    {
-        // remove the metadata that tracks time
-        File file = new File( repository.getBasedir(), "maven-metadata.xml" );
-        System.gc(); // for Windows
-        file.delete();
-        assertFalse( file.exists() );
-    }
-
-    public void testDiscoverMetadata()
-        throws DiscovererException
-    {
-        List metadataPaths = discoverer.discoverMetadata( repository, TEST_OPERATION, null );
-        assertNotNull( "Check metadata not null", metadataPaths );
-
-        RepositoryMetadata metadata =
-            new ArtifactRepositoryMetadata( createArtifact( "org.apache.testgroup", "discovery" ) );
-        assertTrue( "Check included", containsMetadata( metadataPaths, metadata ) );
-
-        metadata =
-            new SnapshotArtifactRepositoryMetadata( createArtifact( "org.apache.testgroup", "discovery", "1.0" ) );
-        assertTrue( "Check included", containsMetadata( metadataPaths, metadata ) );
-
-        metadata = new GroupRepositoryMetadata( "org.apache.maven" );
-        assertTrue( "Check included", containsMetadata( metadataPaths, metadata ) );
-    }
-
-    public void testUpdatedInRepository()
-        throws ComponentLookupException, DiscovererException, ParseException, IOException
-    {
-        // Set repository time to 1-1-2000, a time in the distant past so definitely updated
-        discoverer.setLastCheckedTime( repository, "update",
-                                       new SimpleDateFormat( "yyyy-MM-dd", Locale.US ).parse( "2000-01-01" ) );
-
-        List metadataPaths = discoverer.discoverMetadata( repository, "update", null );
-        assertNotNull( "Check metadata not null", metadataPaths );
-
-        RepositoryMetadata metadata =
-            new ArtifactRepositoryMetadata( createArtifact( "org.apache.maven.update", "test-updated" ) );
-        assertTrue( "Check included", containsMetadata( metadataPaths, metadata ) );
-
-        // try again with the updated timestamp
-        metadataPaths = discoverer.discoverMetadata( repository, "update", null );
-        assertNotNull( "Check metadata not null", metadataPaths );
-
-        assertFalse( "Check not included", containsMetadata( metadataPaths, metadata ) );
-    }
-
-    private boolean containsMetadata( List metadataPaths, RepositoryMetadata metadata )
-    {
-        for ( Iterator i = metadataPaths.iterator(); i.hasNext(); )
-        {
-            RepositoryMetadata m = (RepositoryMetadata) i.next();
-
-            if ( m.getGroupId().equals( metadata.getGroupId() ) )
-            {
-                if ( m.getArtifactId() == null && metadata.getArtifactId() == null )
-                {
-                    return true;
-                }
-                else if ( m.getArtifactId() != null && m.getArtifactId().equals( metadata.getArtifactId() ) )
-                {
-                    return true;
-                }
-            }
-        }
-        return false;
-    }
-
-    public void testNotUpdatedInRepository()
-        throws ComponentLookupException, DiscovererException, IOException
-    {
-        // Set repository time to now, which is after any artifacts, so definitely not updated
-        discoverer.setLastCheckedTime( repository, "update", new Date() );
-
-        List metadataPaths = discoverer.discoverMetadata( repository, "update", null );
-        assertNotNull( "Check metadata not null", metadataPaths );
-
-        RepositoryMetadata metadata =
-            new ArtifactRepositoryMetadata( createArtifact( "org.apache.maven.update", "test-not-updated" ) );
-        assertFalse( "Check not included", containsMetadata( metadataPaths, metadata ) );
-    }
-
-    public void testNotUpdatedInRepositoryForcedDiscovery()
-        throws ComponentLookupException, DiscovererException, IOException
-    {
-        discoverer.resetLastCheckedTime( repository, "update" );
-
-        List metadataPaths = discoverer.discoverMetadata( repository, "update", null );
-        assertNotNull( "Check metadata not null", metadataPaths );
-
-        RepositoryMetadata metadata =
-            new ArtifactRepositoryMetadata( createArtifact( "org.apache.maven.update", "test-not-updated" ) );
-        assertTrue( "Check included", containsMetadata( metadataPaths, metadata ) );
-
-        // try again with the updated timestamp
-        metadataPaths = discoverer.discoverMetadata( repository, "update", null );
-        assertNotNull( "Check metadata not null", metadataPaths );
-
-        assertFalse( "Check not included", containsMetadata( metadataPaths, metadata ) );
-    }
-
-    public void testNotUpdatedInRepositoryForcedDiscoveryMetadataAlreadyExists()
-        throws ComponentLookupException, DiscovererException, IOException
-    {
-        discoverer.setLastCheckedTime( repository, "update", new Date() );
-
-        discoverer.resetLastCheckedTime( repository, "update" );
-
-        List metadataPaths = discoverer.discoverMetadata( repository, "update", null );
-        assertNotNull( "Check metadata not null", metadataPaths );
-
-        RepositoryMetadata metadata =
-            new ArtifactRepositoryMetadata( createArtifact( "org.apache.maven.update", "test-not-updated" ) );
-        assertTrue( "Check included", containsMetadata( metadataPaths, metadata ) );
-
-        // try again with the updated timestamp
-        metadataPaths = discoverer.discoverMetadata( repository, "update", null );
-        assertNotNull( "Check metadata not null", metadataPaths );
-
-        assertFalse( "Check not included", containsMetadata( metadataPaths, metadata ) );
-    }
-
-    public void testNotUpdatedInRepositoryForcedDiscoveryOtherMetadataAlreadyExists()
-        throws ComponentLookupException, DiscovererException, IOException
-    {
-        discoverer.setLastCheckedTime( repository, "test", new Date() );
-
-        discoverer.resetLastCheckedTime( repository, "update" );
-
-        List metadataPaths = discoverer.discoverMetadata( repository, "update", null );
-        assertNotNull( "Check metadata not null", metadataPaths );
-
-        RepositoryMetadata metadata =
-            new ArtifactRepositoryMetadata( createArtifact( "org.apache.maven.update", "test-not-updated" ) );
-        assertTrue( "Check included", containsMetadata( metadataPaths, metadata ) );
-
-        // try again with the updated timestamp
-        metadataPaths = discoverer.discoverMetadata( repository, "update", null );
-        assertNotNull( "Check metadata not null", metadataPaths );
-
-        assertFalse( "Check not included", containsMetadata( metadataPaths, metadata ) );
-    }
-
-    public void testNoRepositoryMetadata()
-        throws ComponentLookupException, DiscovererException, ParseException, IOException
-    {
-        removeTimestampMetadata();
-
-        // should find all
-        List metadataPaths = discoverer.discoverMetadata( repository, TEST_OPERATION, null );
-        assertNotNull( "Check metadata not null", metadataPaths );
-
-        RepositoryMetadata metadata =
-            new ArtifactRepositoryMetadata( createArtifact( "org.apache.maven.update", "test-updated" ) );
-        assertTrue( "Check included", containsMetadata( metadataPaths, metadata ) );
-    }
-
-    protected Artifact createArtifact( String groupId, String artifactId )
-    {
-        return createArtifact( groupId, artifactId, "1.0" );
-    }
-
-    private Artifact createArtifact( String groupId, String artifactId, String version )
-    {
-        return factory.createArtifact( groupId, artifactId, version, null, "jar" );
-    }
-}
diff --git a/archiva-discoverer/src/test/java/org/apache/maven/archiva/discovery/LegacyArtifactDiscovererTest.java b/archiva-discoverer/src/test/java/org/apache/maven/archiva/discovery/LegacyArtifactDiscovererTest.java
deleted file mode 100644 (file)
index d514da9..0000000
+++ /dev/null
@@ -1,482 +0,0 @@
-package org.apache.maven.archiva.discovery;
-
-/*
- * Copyright 2005-2006 The Apache Software Foundation.
- *
- * Licensed 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 org.apache.maven.artifact.Artifact;
-import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
-
-import java.io.File;
-import java.net.MalformedURLException;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.List;
-
-/**
- * Test the legacy artifact discoverer.
- *
- * @author <a href="mailto:brett@apache.org">Brett Porter</a>
- * @version $Id:LegacyArtifactDiscovererTest.java 437105 2006-08-26 17:22:22 +1000 (Sat, 26 Aug 2006) brett $
- */
-public class LegacyArtifactDiscovererTest
-    extends AbstractArtifactDiscovererTest
-{
-    private static final List JAVAX_SQL_BLACKLIST = Collections.singletonList( "javax.sql/**" );
-
-    protected String getLayout()
-    {
-        return "legacy";
-    }
-
-    protected File getRepositoryFile()
-    {
-        return getTestFile( "src/test/legacy-repository" );
-    }
-
-    public void testDefaultExcludes()
-        throws DiscovererException
-    {
-        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
-        assertNotNull( "Check artifacts not null", artifacts );
-        boolean found = false;
-        for ( Iterator i = discoverer.getExcludedPathsIterator(); i.hasNext() && !found; )
-        {
-            DiscovererPath dPath = (DiscovererPath) i.next();
-
-            String path = dPath.getPath();
-
-            if ( path.indexOf( ".svn" ) >= 0 )
-            {
-                found = true;
-                assertEquals( "Check comment", "Artifact was in the specified list of exclusions", dPath.getComment() );
-            }
-        }
-        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()
-        throws DiscovererException
-    {
-        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
-        assertNotNull( "Check artifacts not null", artifacts );
-        boolean found = false;
-        for ( Iterator i = discoverer.getExcludedPathsIterator(); i.hasNext() && !found; )
-        {
-            DiscovererPath dPath = (DiscovererPath) i.next();
-
-            String path = dPath.getPath();
-
-            if ( "KEYS".equals( path ) )
-            {
-                found = true;
-                assertEquals( "Check comment", "Artifact was in the specified list of exclusions", dPath.getComment() );
-            }
-        }
-        assertTrue( "Check exclusion was found", found );
-
-        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
-        {
-            Artifact a = (Artifact) i.next();
-            assertFalse( "Check not KEYS", "KEYS".equals( a.getFile().getName() ) );
-        }
-    }
-
-    public void testBlacklistedExclude()
-        throws DiscovererException
-    {
-        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, JAVAX_SQL_BLACKLIST, false );
-        assertNotNull( "Check artifacts not null", artifacts );
-        boolean found = false;
-        for ( Iterator i = discoverer.getExcludedPathsIterator(); i.hasNext() && !found; )
-        {
-            DiscovererPath dPath = (DiscovererPath) i.next();
-
-            String path = dPath.getPath();
-
-            if ( "javax.sql/jars/jdbc-2.0.jar".equals( path.replace( '\\', '/' ) ) )
-            {
-                found = true;
-                assertEquals( "Check comment is about blacklisting", "Artifact was in the specified list of exclusions",
-                              dPath.getComment() );
-            }
-        }
-        assertTrue( "Check exclusion was found", found );
-
-        assertFalse( "Check jdbc not included", artifacts.contains( createArtifact( "javax.sql", "jdbc", "2.0" ) ) );
-    }
-
-    public void testKickoutWithShortPath()
-        throws DiscovererException
-    {
-        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
-        assertNotNull( "Check artifacts not null", artifacts );
-        boolean found = false;
-        for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
-        {
-            DiscovererPath dPath = (DiscovererPath) i.next();
-
-            String path = dPath.getPath();
-
-            if ( "invalid/invalid-1.0.jar".equals( path.replace( '\\', '/' ) ) )
-            {
-                found = true;
-                assertEquals( "Check reason for kickout",
-                              "Path does not match a legacy repository path for an artifact", dPath.getComment() );
-            }
-        }
-        assertTrue( "Check kickout was found", found );
-
-        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
-        {
-            Artifact a = (Artifact) i.next();
-            assertFalse( "Check not invalid-1.0.jar", "invalid-1.0.jar".equals( a.getFile().getName() ) );
-        }
-    }
-
-    public void testKickoutWithLongPath()
-        throws DiscovererException
-    {
-        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
-        assertNotNull( "Check artifacts not null", artifacts );
-        boolean found = false;
-        for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
-        {
-            DiscovererPath dPath = (DiscovererPath) i.next();
-
-            String path = dPath.getPath();
-
-            if ( "invalid/jars/1.0/invalid-1.0.jar".equals( path.replace( '\\', '/' ) ) )
-            {
-                found = true;
-                assertEquals( "Check reason for kickout",
-                              "Path does not match a legacy repository path for an artifact", dPath.getComment() );
-            }
-        }
-        assertTrue( "Check kickout was found", found );
-
-        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
-        {
-            Artifact a = (Artifact) i.next();
-            assertFalse( "Check not invalid-1.0.jar", "invalid-1.0.jar".equals( a.getFile().getName() ) );
-        }
-    }
-
-    public void testKickoutWithInvalidType()
-        throws DiscovererException
-    {
-        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
-        assertNotNull( "Check artifacts not null", artifacts );
-        boolean found = false;
-        for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
-        {
-            DiscovererPath dPath = (DiscovererPath) i.next();
-
-            String path = dPath.getPath();
-
-            if ( "invalid/foo/invalid-1.0.foo".equals( path.replace( '\\', '/' ) ) )
-            {
-                found = true;
-                assertEquals( "Check reason for kickout", "Path artifact type does not corresspond to an artifact type",
-                              dPath.getComment() );
-            }
-        }
-        assertTrue( "Check kickout was found", found );
-
-        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
-        {
-            Artifact a = (Artifact) i.next();
-            assertFalse( "Check not invalid-1.0.foo", "invalid-1.0.foo".equals( a.getFile().getName() ) );
-        }
-    }
-
-    public void testKickoutWithNoExtension()
-        throws DiscovererException
-    {
-        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
-        assertNotNull( "Check artifacts not null", artifacts );
-        boolean found = false;
-        for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
-        {
-            DiscovererPath dPath = (DiscovererPath) i.next();
-
-            String path = dPath.getPath();
-
-            if ( "invalid/jars/no-extension".equals( path.replace( '\\', '/' ) ) )
-            {
-                found = true;
-                assertEquals( "Check reason for kickout", "Path filename does not have an extension",
-                              dPath.getComment() );
-            }
-        }
-        assertTrue( "Check kickout was found", found );
-
-        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
-        {
-            Artifact a = (Artifact) i.next();
-            assertFalse( "Check not 'no-extension'", "no-extension".equals( a.getFile().getName() ) );
-        }
-    }
-
-    public void testKickoutWithWrongExtension()
-        throws DiscovererException
-    {
-        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
-        assertNotNull( "Check artifacts not null", artifacts );
-        boolean found = false;
-        for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
-        {
-            DiscovererPath dPath = (DiscovererPath) i.next();
-
-            String path = dPath.getPath();
-
-            if ( "invalid/jars/invalid-1.0.rar".equals( path.replace( '\\', '/' ) ) )
-            {
-                found = true;
-                assertEquals( "Check reason for kickout", "Path type does not match the extension",
-                              dPath.getComment() );
-            }
-        }
-        assertTrue( "Check kickout was found", found );
-
-        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
-        {
-            Artifact a = (Artifact) i.next();
-            assertFalse( "Check not 'invalid-1.0.rar'", "invalid-1.0.rar".equals( a.getFile().getName() ) );
-        }
-    }
-
-    public void testKickoutWithNoVersion()
-        throws DiscovererException
-    {
-        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false );
-        assertNotNull( "Check artifacts not null", artifacts );
-        boolean found = false;
-        for ( Iterator i = discoverer.getKickedOutPathsIterator(); i.hasNext() && !found; )
-        {
-            DiscovererPath dPath = (DiscovererPath) i.next();
-
-            String path = dPath.getPath();
-
-            if ( "invalid/jars/invalid.jar".equals( path.replace( '\\', '/' ) ) )
-            {
-                found = true;
-                assertEquals( "Check reason for kickout", "Path filename version is empty", dPath.getComment() );
-            }
-        }
-        assertTrue( "Check kickout was found", found );
-
-        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
-        {
-            Artifact a = (Artifact) i.next();
-            assertFalse( "Check not 'invalid.jar'", "invalid.jar".equals( a.getFile().getName() ) );
-        }
-    }
-
-    public void testInclusion()
-        throws DiscovererException
-    {
-        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true );
-        assertNotNull( "Check artifacts not null", artifacts );
-
-        assertTrue( "Check normal included",
-                    artifacts.contains( createArtifact( "org.apache.maven", "testing", "1.0" ) ) );
-    }
-
-    public void testTextualVersion()
-        throws DiscovererException
-    {
-        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true );
-        assertNotNull( "Check artifacts not null", artifacts );
-
-        assertTrue( "Check normal included",
-                    artifacts.contains( createArtifact( "org.apache.maven", "testing", "UNKNOWN" ) ) );
-    }
-
-    public void testArtifactWithClassifier()
-        throws DiscovererException
-    {
-        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true );
-        assertNotNull( "Check artifacts not null", artifacts );
-
-        assertTrue( "Check normal included",
-                    artifacts.contains( createArtifact( "org.apache.maven", "some-ejb", "1.0", "jar", "client" ) ) );
-    }
-
-    public void testJavaSourcesInclusion()
-        throws DiscovererException
-    {
-        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true );
-        assertNotNull( "Check artifacts not null", artifacts );
-
-        assertTrue( "Check normal included", artifacts.contains(
-            createArtifact( "org.apache.maven", "testing", "1.0", "java-source", "sources" ) ) );
-    }
-
-    public void testDistributionInclusion()
-        throws DiscovererException
-    {
-        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true );
-        assertNotNull( "Check artifacts not null", artifacts );
-
-        assertTrue( "Check zip included",
-                    artifacts.contains( createArtifact( "org.apache.maven", "testing", "1.0", "distribution-zip" ) ) );
-
-        assertTrue( "Check tar.gz included",
-                    artifacts.contains( createArtifact( "org.apache.maven", "testing", "1.0", "distribution-tgz" ) ) );
-    }
-
-    public void testSnapshotInclusion()
-        throws DiscovererException
-    {
-        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, 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", "testing", "1.0-20050611.112233-1" ) ) );
-    }
-
-    public void testSnapshotExclusion()
-        throws DiscovererException
-    {
-        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, 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", "testing", "1.0-20050611.112233-1" ) ) );
-    }
-
-    public void testFileSet()
-        throws DiscovererException
-    {
-        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true );
-        assertNotNull( "Check artifacts not null", artifacts );
-
-        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
-        {
-            Artifact artifact = (Artifact) i.next();
-            assertNotNull( "Check file is set", artifact.getFile() );
-        }
-    }
-
-    public void testRepositorySet()
-        throws MalformedURLException, DiscovererException
-    {
-        List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true );
-        assertNotNull( "Check artifacts not null", artifacts );
-
-        String url = repository.getUrl();
-        for ( Iterator i = artifacts.iterator(); i.hasNext(); )
-        {
-            Artifact artifact = (Artifact) i.next();
-            assertNotNull( "Check repository set", artifact.getRepository() );
-            assertEquals( "Check repository url is correct", url, artifact.getRepository().getUrl() );
-        }
-    }
-
-    public void testWrongArtifactPackaging()
-        throws ComponentLookupException, DiscovererException
-    {
-        try
-        {
-            discoverer.buildArtifact( "org.apache.maven.test/jars/artifactId-1.0.jar.md5" );
-
-            fail( "Artifact should be null for wrong package extension" );
-        }
-        catch ( DiscovererException e )
-        {
-            // excellent
-        }
-    }
-
-    public void testNoArtifactId()
-        throws DiscovererException
-    {
-        try
-        {
-            discoverer.buildArtifact( "groupId/jars/-1.0.jar" );
-
-            fail( "Artifact should be null when artifactId is missing" );
-        }
-        catch ( DiscovererException e )
-        {
-            // excellent
-        }
-
-        try
-        {
-            discoverer.buildArtifact( "groupId/jars/1.0.jar" );
-
-            fail( "Artifact should be null when artifactId is missing" );
-        }
-        catch ( DiscovererException e )
-        {
-            // excellent
-        }
-    }
-
-    public void testNoType()
-        throws ComponentLookupException, DiscovererException
-    {
-        try
-        {
-            discoverer.buildArtifact( "invalid/invalid/1/invalid-1" );
-
-            fail( "Artifact should be null for no type" );
-        }
-        catch ( DiscovererException e )
-        {
-            // excellent
-        }
-    }
-
-    public void testSnapshot()
-        throws ComponentLookupException, DiscovererException
-    {
-        String testPath = "org.apache.maven.test/jars/maven-model-1.0-SNAPSHOT.jar";
-
-        Artifact artifact = discoverer.buildArtifact( testPath );
-
-        assertEquals( createArtifact( "org.apache.maven.test", "maven-model", "1.0-SNAPSHOT" ), artifact );
-    }
-
-    public void testFinal()
-        throws ComponentLookupException, DiscovererException
-    {
-        String testPath = "org.apache.maven.test/jars/maven-model-1.0-final-20060606.jar";
-
-        Artifact artifact = discoverer.buildArtifact( testPath );
-
-        assertEquals( createArtifact( "org.apache.maven.test", "maven-model", "1.0-final-20060606" ), artifact );
-    }
-
-    public void testNormal()
-        throws ComponentLookupException, DiscovererException
-    {
-        String testPath = "javax.sql/jars/jdbc-2.0.jar";
-
-        Artifact artifact = discoverer.buildArtifact( testPath );
-
-        assertEquals( createArtifact( "javax.sql", "jdbc", "2.0" ), artifact );
-    }
-}
index 74b0cc1f130253038fb4cacbbf18ac3ee86bb897..e4fe87156688f48e0c5aa103f5669d90ca97b748 100644 (file)
@@ -18,8 +18,8 @@ package org.apache.maven.archiva.proxy;
 
 import org.apache.maven.archiva.digest.DigestUtils;
 import org.apache.maven.archiva.digest.DigesterException;
-import org.apache.maven.archiva.discovery.ArtifactDiscoverer;
-import org.apache.maven.archiva.discovery.DiscovererException;
+import org.apache.maven.archiva.discoverer.ArtifactDiscoverer;
+import org.apache.maven.archiva.discoverer.DiscovererException;
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.factory.ArtifactFactory;
 import org.apache.maven.artifact.repository.ArtifactRepository;