From a96bd2dc9322b5e31bbfd47a8566e1659dd8a697 Mon Sep 17 00:00:00 2001 From: Brett Porter Date: Mon, 24 Jul 2006 05:57:01 +0000 Subject: [PATCH] [MRM-125] implement timestamp based discovery git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@424918 13f79535-47bb-0310-9956-ffa450edef68 --- .../discovery/AbstractArtifactDiscoverer.java | 124 ++++++++++++--- .../discovery/AbstractDiscoverer.java | 97 +++++++++++- .../discovery/ArtifactDiscoverer.java | 29 +++- .../discovery/DefaultMetadataDiscoverer.java | 32 ++-- .../discovery/MetadataDiscoverer.java | 9 +- .../AbstractArtifactDiscovererTest.java | 143 +++++++++++++++++- .../DefaultMetadataDiscovererTest.java | 37 ++++- .../jars/test-not-updated-1.0.jar | 1 + .../jars/test-updated-1.0.jar | 1 + .../1.0/test-not-updated-1.0.jar | 1 + .../1.0/test-not-updated-1.0.pom | 10 ++ .../test-updated/1.0/test-updated-1.0.jar | 1 + .../test-updated/1.0/test-updated-1.0.pom | 10 ++ 13 files changed, 435 insertions(+), 60 deletions(-) create mode 100644 maven-repository-discovery/src/test/legacy-repository/org.apache.maven.update/jars/test-not-updated-1.0.jar create mode 100644 maven-repository-discovery/src/test/legacy-repository/org.apache.maven.update/jars/test-updated-1.0.jar create mode 100644 maven-repository-discovery/src/test/repository/org/apache/maven/update/test-not-updated/1.0/test-not-updated-1.0.jar create mode 100644 maven-repository-discovery/src/test/repository/org/apache/maven/update/test-not-updated/1.0/test-not-updated-1.0.pom create mode 100644 maven-repository-discovery/src/test/repository/org/apache/maven/update/test-updated/1.0/test-updated-1.0.jar create mode 100644 maven-repository-discovery/src/test/repository/org/apache/maven/update/test-updated/1.0/test-updated-1.0.pom diff --git a/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/AbstractArtifactDiscoverer.java b/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/AbstractArtifactDiscoverer.java index 0da2fabce..6394cb8ff 100644 --- a/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/AbstractArtifactDiscoverer.java +++ b/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/AbstractArtifactDiscoverer.java @@ -20,14 +20,22 @@ import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.model.Model; import org.apache.maven.model.io.xpp3.MavenXpp3Reader; +import org.codehaus.plexus.util.IOUtil; +import org.codehaus.plexus.util.xml.Xpp3Dom; +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.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. @@ -48,38 +56,41 @@ public abstract class AbstractArtifactDiscoverer private static final String POM = ".pom"; - /** - * Scan the repository for artifact paths. - */ - private String[] scanForArtifactPaths( File repositoryBase, String blacklistedPatterns ) + private List scanForArtifactPaths( File repositoryBase, String blacklistedPatterns, long comparisonTimestamp ) { - return scanForArtifactPaths( repositoryBase, blacklistedPatterns, null, STANDARD_DISCOVERY_EXCLUDES ); + return scanForArtifactPaths( repositoryBase, blacklistedPatterns, null, STANDARD_DISCOVERY_EXCLUDES, + comparisonTimestamp ); } - /** - * Return a list of artifacts found in a specified repository - * - * @param repository The ArtifactRepository to discover artifacts - * @param blacklistedPatterns Comma-delimited list of string paths that will be excluded in the discovery - * @param includeSnapshots if the repository contains snapshots which should also be included - * @return list of artifacts - */ - public List discoverArtifacts( ArtifactRepository repository, String blacklistedPatterns, boolean includeSnapshots ) + public List discoverArtifacts( ArtifactRepository repository, String operation, String blacklistedPatterns, + boolean includeSnapshots ) + throws DiscovererException { if ( !"file".equals( repository.getProtocol() ) ) { throw new UnsupportedOperationException( "Only filesystem repositories are supported" ); } + long comparisonTimestamp = readComparisonTimestamp( repository, operation ); + File repositoryBase = new File( repository.getBasedir() ); List artifacts = new ArrayList(); - String[] artifactPaths = scanForArtifactPaths( repositoryBase, blacklistedPatterns ); + List artifactPaths = scanForArtifactPaths( repositoryBase, blacklistedPatterns, comparisonTimestamp ); - for ( int i = 0; i < artifactPaths.length; i++ ) + try { - String path = artifactPaths[i]; + setLastCheckedTime( repository, operation, new Date() ); + } + 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 { @@ -114,11 +125,12 @@ public abstract class AbstractArtifactDiscoverer File repositoryBase = new File( repository.getBasedir() ); - String[] artifactPaths = scanForArtifactPaths( repositoryBase, blacklistedPatterns ); + // TODO: if we keep this method, set comparison timestamp properly! + List artifactPaths = scanForArtifactPaths( repositoryBase, blacklistedPatterns, 0 ); - for ( int i = 0; i < artifactPaths.length; i++ ) + for ( Iterator i = artifactPaths.iterator(); i.hasNext(); ) { - String path = artifactPaths[i]; + String path = (String) i.next(); String filename = repositoryBase.getAbsolutePath() + "/" + path; @@ -163,6 +175,78 @@ public abstract class AbstractArtifactDiscoverer return artifacts; } + 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: yyyyMMddHHmmss (ie, flat properties) + + File file = new File( repository.getBasedir(), "maven-metadata.xml" ); + + Xpp3Dom dom = readDom( file ); + + Xpp3Dom lastDiscoveryDom = getLastDiscoveryDom( dom ); + + 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 ); + } + } + + if ( changed ) + { + saveDom( file, dom ); + } + } + + private void saveDom( File file, Xpp3Dom dom ) + throws IOException + { + FileWriter writer = new FileWriter( file ); + + // save metadata + try + { + Xpp3DomWriter.write( writer, dom ); + } + finally + { + IOUtil.close( writer ); + } + } + + 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 ); + + Xpp3Dom lastDiscoveryDom = getLastDiscoveryDom( dom ); + + Xpp3Dom entry = lastDiscoveryDom.getChild( operation ); + if ( entry == null ) + { + entry = new Xpp3Dom( operation ); + lastDiscoveryDom.addChild( entry ); + } + entry.setValue( new SimpleDateFormat( DATE_FMT, Locale.US ).format( date ) ); + + saveDom( file, dom ); + } + /** * Returns an artifact object that is represented by the specified path in a repository * diff --git a/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/AbstractDiscoverer.java b/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/AbstractDiscoverer.java index 90762b53e..052eb821d 100644 --- a/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/AbstractDiscoverer.java +++ b/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/AbstractDiscoverer.java @@ -17,16 +17,27 @@ package org.apache.maven.repository.discovery; */ 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.StringUtils; +import org.codehaus.plexus.util.xml.Xpp3Dom; +import org.codehaus.plexus.util.xml.Xpp3DomBuilder; +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +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. @@ -48,6 +59,8 @@ public abstract class AbstractDiscoverer private List excludedPaths = new ArrayList(); + protected static final String DATE_FMT = "yyyyMMddHHmmss"; + /** * Add a path to the list of files that were kicked out due to being invalid. * @@ -69,11 +82,8 @@ public abstract class AbstractDiscoverer return kickedOutPaths.iterator(); } - /** - * Scan the repository for artifact paths. - */ - protected String[] scanForArtifactPaths( File repositoryBase, String blacklistedPatterns, String[] includes, - String[] excludes ) + protected List scanForArtifactPaths( File repositoryBase, String blacklistedPatterns, String[] includes, + String[] excludes, long comparisonTimestamp ) { List allExcludes = new ArrayList(); allExcludes.addAll( FileUtils.getDefaultExcludesAsList() ); @@ -104,7 +114,19 @@ public abstract class AbstractDiscoverer excludedPaths.add( new DiscovererPath( path, "Artifact was in the specified list of exclusions" ) ); } - return scanner.getIncludedFiles(); + // 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(); + + if ( comparisonTimestamp == 0 || new File( repositoryBase, path ).lastModified() > comparisonTimestamp ) + { + includedPaths.add( path ); + } + } + + return includedPaths; } /** @@ -116,4 +138,67 @@ public abstract class AbstractDiscoverer { return excludedPaths.iterator(); } + + protected long readComparisonTimestamp( ArtifactRepository repository, String operation ) + { + File file = new File( repository.getBasedir(), "maven-metadata.xml" ); + Xpp3Dom dom = readDom( file ); + dom = getLastDiscoveryDom( 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 getLastDiscoveryDom( Xpp3Dom dom ) + { + Xpp3Dom lastDiscoveryDom = dom.getChild( "lastDiscovery" ); + if ( lastDiscoveryDom == null ) + { + dom.addChild( new Xpp3Dom( "lastDiscovery" ) ); + lastDiscoveryDom = dom.getChild( "lastDiscovery" ); + } + return lastDiscoveryDom; + } } diff --git a/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/ArtifactDiscoverer.java b/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/ArtifactDiscoverer.java index 4b317ead5..2afad6866 100644 --- a/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/ArtifactDiscoverer.java +++ b/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/ArtifactDiscoverer.java @@ -19,6 +19,8 @@ package org.apache.maven.repository.discovery; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.repository.ArtifactRepository; +import java.io.IOException; +import java.util.Date; import java.util.List; /** @@ -39,11 +41,15 @@ public interface ArtifactDiscoverer * Discover artifacts in the repository. * * @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 blacklistedPatterns, boolean includeSnapshots ); + List discoverArtifacts( ArtifactRepository repository, String operation, String blacklistedPatterns, + boolean includeSnapshots ) + throws DiscovererException; /** * Discover standalone POM artifacts in the repository. @@ -66,4 +72,25 @@ public interface ArtifactDiscoverer */ Artifact buildArtifact( String path ) throws DiscovererException; + + /** + * 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/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/DefaultMetadataDiscoverer.java b/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/DefaultMetadataDiscoverer.java index 30b3f2c0e..3ec0463d7 100644 --- a/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/DefaultMetadataDiscoverer.java +++ b/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/DefaultMetadataDiscoverer.java @@ -17,6 +17,7 @@ package org.apache.maven.repository.discovery; */ 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; @@ -56,38 +57,33 @@ public class DefaultMetadataDiscoverer private static final String[] STANDARD_DISCOVERY_INCLUDES = {"**/*-metadata.xml", "**/*/*-metadata.xml", "**/*/*/*-metadata.xml", "**/*-metadata-*.xml", "**/*/*-metadata-*.xml", "**/*/*/*-metadata-*.xml"}; - /** - * @see org.apache.maven.repository.discovery.MetadataDiscoverer#discoverMetadata(java.io.File,String) - */ - public List discoverMetadata( File repositoryBase, String blacklistedPatterns ) + public List discoverMetadata( ArtifactRepository repository, String operation, String blacklistedPatterns ) { - List metadataFiles = new ArrayList(); - String[] metadataPaths = - scanForArtifactPaths( repositoryBase, blacklistedPatterns, STANDARD_DISCOVERY_INCLUDES, null ); + long comparisonTimestamp = readComparisonTimestamp( repository, operation ); - for ( int i = 0; i < metadataPaths.length; i++ ) + List metadataFiles = new ArrayList(); + List metadataPaths = scanForArtifactPaths( new File( repository.getBasedir() ), blacklistedPatterns, + STANDARD_DISCOVERY_INCLUDES, null, comparisonTimestamp ); + + // TODO: save! should we be using a different entry for metadata? + + for ( Iterator i = metadataPaths.iterator(); i.hasNext(); ) { + String metadataPath = (String) i.next(); try { - RepositoryMetadata metadata = buildMetadata( repositoryBase.getPath(), metadataPaths[i] ); + RepositoryMetadata metadata = buildMetadata( repository.getBasedir(), metadataPath ); metadataFiles.add( metadata ); } catch ( DiscovererException e ) { - addKickedOutPath( metadataPaths[i], e.getMessage() ); + addKickedOutPath( metadataPath, e.getMessage() ); } } return metadataFiles; } - /** - * Create RepositoryMetadata object. - * - * @param repo The path to the repository. - * @param metadataPath The path to the metadata file. - * @return the metadata - */ private RepositoryMetadata buildMetadata( String repo, String metadataPath ) throws DiscovererException { @@ -95,7 +91,7 @@ public class DefaultMetadataDiscoverer String repoPath = repo + "/" + metadataPath; try { - URL url = new File( repoPath ).toURL(); + URL url = new File( repoPath ).toURI().toURL(); InputStream is = url.openStream(); Reader reader = new InputStreamReader( is ); MetadataXpp3Reader metadataReader = new MetadataXpp3Reader(); diff --git a/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/MetadataDiscoverer.java b/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/MetadataDiscoverer.java index 01cedac66..cfd966a8e 100644 --- a/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/MetadataDiscoverer.java +++ b/maven-repository-discovery/src/main/java/org/apache/maven/repository/discovery/MetadataDiscoverer.java @@ -16,7 +16,8 @@ package org.apache.maven.repository.discovery; * limitations under the License. */ -import java.io.File; +import org.apache.maven.artifact.repository.ArtifactRepository; + import java.util.List; /** @@ -30,8 +31,10 @@ public interface MetadataDiscoverer /** * Search for metadata files in the repository. * - * @param repositoryBase The repository directory. + * @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 */ - List discoverMetadata( File repositoryBase, String blacklistedPatterns ); + List discoverMetadata( ArtifactRepository repository, String operation, String blacklistedPatterns ); } diff --git a/maven-repository-discovery/src/test/java/org/apache/maven/repository/discovery/AbstractArtifactDiscovererTest.java b/maven-repository-discovery/src/test/java/org/apache/maven/repository/discovery/AbstractArtifactDiscovererTest.java index 8a50c4ebb..965cdd4c2 100644 --- a/maven-repository-discovery/src/test/java/org/apache/maven/repository/discovery/AbstractArtifactDiscovererTest.java +++ b/maven-repository-discovery/src/test/java/org/apache/maven/repository/discovery/AbstractArtifactDiscovererTest.java @@ -16,14 +16,21 @@ package org.apache.maven.repository.discovery; * limitations under the License. */ -import org.codehaus.plexus.PlexusTestCase; -import org.apache.maven.artifact.factory.ArtifactFactory; -import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; -import org.apache.maven.artifact.repository.ArtifactRepository; -import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; 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 @@ -37,6 +44,8 @@ public abstract class AbstractArtifactDiscovererTest protected ArtifactRepository repository; + protected static final String TEST_OPERATION = "test"; + protected abstract String getLayout(); protected abstract File getRepositoryFile(); @@ -51,6 +60,8 @@ public abstract class AbstractArtifactDiscovererTest factory = (ArtifactFactory) lookup( ArtifactFactory.ROLE ); repository = getRepository(); + + removeTimestampMetadata(); } protected ArtifactRepository getRepository() @@ -60,7 +71,8 @@ public abstract class AbstractArtifactDiscovererTest ArtifactRepositoryFactory factory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE ); - ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, getLayout() ); + ArtifactRepositoryLayout layout = + (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, getLayout() ); return factory.createArtifactRepository( "discoveryRepo", "file://" + basedir, layout, null, null ); } @@ -75,8 +87,125 @@ public abstract class AbstractArtifactDiscovererTest return factory.createArtifact( groupId, artifactId, version, null, type ); } - protected Artifact createArtifact( String groupId, String artifactId, String version, String type, String classifier ) + 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-not-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 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/maven-repository-discovery/src/test/java/org/apache/maven/repository/discovery/DefaultMetadataDiscovererTest.java b/maven-repository-discovery/src/test/java/org/apache/maven/repository/discovery/DefaultMetadataDiscovererTest.java index 223b2a902..2b3a77f73 100644 --- a/maven-repository-discovery/src/test/java/org/apache/maven/repository/discovery/DefaultMetadataDiscovererTest.java +++ b/maven-repository-discovery/src/test/java/org/apache/maven/repository/discovery/DefaultMetadataDiscovererTest.java @@ -16,6 +16,9 @@ package org.apache.maven.repository.discovery; * limitations under the License. */ +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 java.io.File; @@ -30,7 +33,9 @@ public class DefaultMetadataDiscovererTest { private MetadataDiscoverer discoverer; - private File repositoryLocation; + private static final String TEST_OPERATION = "test"; + + private ArtifactRepository repository; /** * @@ -41,7 +46,22 @@ public class DefaultMetadataDiscovererTest super.setUp(); discoverer = (MetadataDiscoverer) lookup( MetadataDiscoverer.ROLE, "default" ); - repositoryLocation = getTestFile( "src/test/repository" ); + + 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 ); } /** @@ -59,7 +79,7 @@ public class DefaultMetadataDiscovererTest */ public void testMetadataDiscovererSuccess() { - List metadataPaths = discoverer.discoverMetadata( repositoryLocation, null ); + List metadataPaths = discoverer.discoverMetadata( repository, TEST_OPERATION, null ); assertNotNull( "Check metadata not null", metadataPaths ); assertEquals( 3, metadataPaths.size() ); } @@ -69,7 +89,7 @@ public class DefaultMetadataDiscovererTest */ public void testKickoutWrongDirectory() { - discoverer.discoverMetadata( repositoryLocation, null ); + discoverer.discoverMetadata( repository, TEST_OPERATION, null ); Iterator iter = discoverer.getKickedOutPathsIterator(); boolean found = false; while ( iter.hasNext() && !found ) @@ -93,7 +113,7 @@ public class DefaultMetadataDiscovererTest */ public void testKickoutBlankMetadata() { - discoverer.discoverMetadata( repositoryLocation, null ); + discoverer.discoverMetadata( repository, TEST_OPERATION, null ); Iterator iter = discoverer.getKickedOutPathsIterator(); boolean found = false; while ( iter.hasNext() && !found ) @@ -112,4 +132,11 @@ public class DefaultMetadataDiscovererTest assertTrue( found ); } + 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/maven-repository-discovery/src/test/legacy-repository/org.apache.maven.update/jars/test-not-updated-1.0.jar b/maven-repository-discovery/src/test/legacy-repository/org.apache.maven.update/jars/test-not-updated-1.0.jar new file mode 100644 index 000000000..54d190b23 --- /dev/null +++ b/maven-repository-discovery/src/test/legacy-repository/org.apache.maven.update/jars/test-not-updated-1.0.jar @@ -0,0 +1 @@ +dummy content. sample file only. diff --git a/maven-repository-discovery/src/test/legacy-repository/org.apache.maven.update/jars/test-updated-1.0.jar b/maven-repository-discovery/src/test/legacy-repository/org.apache.maven.update/jars/test-updated-1.0.jar new file mode 100644 index 000000000..54d190b23 --- /dev/null +++ b/maven-repository-discovery/src/test/legacy-repository/org.apache.maven.update/jars/test-updated-1.0.jar @@ -0,0 +1 @@ +dummy content. sample file only. diff --git a/maven-repository-discovery/src/test/repository/org/apache/maven/update/test-not-updated/1.0/test-not-updated-1.0.jar b/maven-repository-discovery/src/test/repository/org/apache/maven/update/test-not-updated/1.0/test-not-updated-1.0.jar new file mode 100644 index 000000000..54d190b23 --- /dev/null +++ b/maven-repository-discovery/src/test/repository/org/apache/maven/update/test-not-updated/1.0/test-not-updated-1.0.jar @@ -0,0 +1 @@ +dummy content. sample file only. diff --git a/maven-repository-discovery/src/test/repository/org/apache/maven/update/test-not-updated/1.0/test-not-updated-1.0.pom b/maven-repository-discovery/src/test/repository/org/apache/maven/update/test-not-updated/1.0/test-not-updated-1.0.pom new file mode 100644 index 000000000..21bef0276 --- /dev/null +++ b/maven-repository-discovery/src/test/repository/org/apache/maven/update/test-not-updated/1.0/test-not-updated-1.0.pom @@ -0,0 +1,10 @@ + + 4.0.0 + org.apache.maven.update + test-not-updated + 1.0 + Maven Test Repository Artifact Discovery + + + diff --git a/maven-repository-discovery/src/test/repository/org/apache/maven/update/test-updated/1.0/test-updated-1.0.jar b/maven-repository-discovery/src/test/repository/org/apache/maven/update/test-updated/1.0/test-updated-1.0.jar new file mode 100644 index 000000000..54d190b23 --- /dev/null +++ b/maven-repository-discovery/src/test/repository/org/apache/maven/update/test-updated/1.0/test-updated-1.0.jar @@ -0,0 +1 @@ +dummy content. sample file only. diff --git a/maven-repository-discovery/src/test/repository/org/apache/maven/update/test-updated/1.0/test-updated-1.0.pom b/maven-repository-discovery/src/test/repository/org/apache/maven/update/test-updated/1.0/test-updated-1.0.pom new file mode 100644 index 000000000..5f88c44df --- /dev/null +++ b/maven-repository-discovery/src/test/repository/org/apache/maven/update/test-updated/1.0/test-updated-1.0.pom @@ -0,0 +1,10 @@ + + 4.0.0 + org.apache.maven.update + test-updated + 1.0 + Maven Test Repository Artifact Discovery + + +