From bf826a50632e29f18f273084a64fc6668246803d Mon Sep 17 00:00:00 2001 From: Brett Porter Date: Mon, 24 Jul 2006 08:05:19 +0000 Subject: [PATCH] take some notes, merge the model indexing into the artifact indexing (when an artifact is a POM, add the model fields, otherwise treat it as normal). Remove the "Standalone POMs" concept as it was flawed. It would still be beneficial to merge the artifact records in the index when a POM and a JAR exist. git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@424957 13f79535-47bb-0310-9956-ffa450edef68 --- .../repository/DefaultRepositoryManager.java | 10 +- .../maven/repository/RepositoryManager.java | 7 +- .../repository/scheduler/IndexerTask.java | 34 +- .../discovery/AbstractArtifactDiscoverer.java | 80 +-- .../discovery/ArtifactDiscoverer.java | 15 +- .../DefaultArtifactDiscovererTest.java | 118 ++-- .../LegacyArtifactDiscovererTest.java | 55 +- .../indexing/AbstractRepositoryIndex.java | 2 + .../indexing/ArtifactRepositoryIndex.java | 244 ++++++++ .../DefaultRepositoryIndexingFactory.java | 6 - .../indexing/PomRepositoryIndex.java | 344 ----------- .../indexing/RepositoryIndexingFactory.java | 11 - .../ArtifactRepositoryIndexingTest.java | 347 ++++++++--- .../indexing/PomRepositoryIndexingTest.java | 551 ------------------ .../RepositoryIndexSearchLayerTest.java | 16 - .../main/webapp/WEB-INF/jsp/admin/index.jsp | 10 +- 16 files changed, 666 insertions(+), 1184 deletions(-) delete mode 100644 maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/PomRepositoryIndex.java delete mode 100644 maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/PomRepositoryIndexingTest.java diff --git a/maven-repository-core/src/main/java/org/apache/maven/repository/DefaultRepositoryManager.java b/maven-repository-core/src/main/java/org/apache/maven/repository/DefaultRepositoryManager.java index 741718902..c9676a486 100644 --- a/maven-repository-core/src/main/java/org/apache/maven/repository/DefaultRepositoryManager.java +++ b/maven-repository-core/src/main/java/org/apache/maven/repository/DefaultRepositoryManager.java @@ -6,6 +6,7 @@ import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; import org.apache.maven.repository.converter.RepositoryConversionException; import org.apache.maven.repository.converter.RepositoryConverter; import org.apache.maven.repository.discovery.ArtifactDiscoverer; +import org.apache.maven.repository.discovery.DiscovererException; import org.apache.maven.repository.reporting.ArtifactReporter; import java.io.File; @@ -51,7 +52,7 @@ public class DefaultRepositoryManager public void convertLegacyRepository( File legacyRepositoryDirectory, File repositoryDirectory, boolean includeSnapshots ) - throws RepositoryConversionException + throws RepositoryConversionException, DiscovererException { ArtifactRepository legacyRepository; @@ -60,11 +61,11 @@ public class DefaultRepositoryManager try { legacyRepository = artifactRepositoryFactory.createArtifactRepository( "legacy", - legacyRepositoryDirectory.toURL().toString(), + legacyRepositoryDirectory.toURI().toURL().toString(), legacyLayout, null, null ); repository = artifactRepositoryFactory.createArtifactRepository( "default", - repositoryDirectory.toURL().toString(), + repositoryDirectory.toURI().toURL().toString(), defaultLayout, null, null ); } catch ( MalformedURLException e ) @@ -72,7 +73,8 @@ public class DefaultRepositoryManager throw new RepositoryConversionException( "Error convering legacy repository.", e ); } - List legacyArtifacts = artifactDiscoverer.discoverArtifacts( legacyRepository, null, includeSnapshots ); + List legacyArtifacts = + artifactDiscoverer.discoverArtifacts( legacyRepository, "converter", null, includeSnapshots ); repositoryConverter.convert( legacyArtifacts, repository, reporter ); } diff --git a/maven-repository-core/src/main/java/org/apache/maven/repository/RepositoryManager.java b/maven-repository-core/src/main/java/org/apache/maven/repository/RepositoryManager.java index e3baccb12..40d176e83 100644 --- a/maven-repository-core/src/main/java/org/apache/maven/repository/RepositoryManager.java +++ b/maven-repository-core/src/main/java/org/apache/maven/repository/RepositoryManager.java @@ -1,6 +1,7 @@ package org.apache.maven.repository; import org.apache.maven.repository.converter.RepositoryConversionException; +import org.apache.maven.repository.discovery.DiscovererException; import java.io.File; @@ -22,8 +23,6 @@ public interface RepositoryManager * @param repositoryDirectory * @throws RepositoryConversionException */ - void convertLegacyRepository( File legacyRepositoryDirectory, - File repositoryDirectory, - boolean includeSnapshots ) - throws RepositoryConversionException; + void convertLegacyRepository( File legacyRepositoryDirectory, File repositoryDirectory, boolean includeSnapshots ) + throws RepositoryConversionException, DiscovererException; } diff --git a/maven-repository-core/src/main/java/org/apache/maven/repository/scheduler/IndexerTask.java b/maven-repository-core/src/main/java/org/apache/maven/repository/scheduler/IndexerTask.java index eaed3545f..17e28b02b 100644 --- a/maven-repository-core/src/main/java/org/apache/maven/repository/scheduler/IndexerTask.java +++ b/maven-repository-core/src/main/java/org/apache/maven/repository/scheduler/IndexerTask.java @@ -22,10 +22,10 @@ import org.apache.maven.repository.configuration.ConfigurationStore; import org.apache.maven.repository.configuration.ConfigurationStoreException; import org.apache.maven.repository.configuration.ConfiguredRepositoryFactory; import org.apache.maven.repository.discovery.ArtifactDiscoverer; +import org.apache.maven.repository.discovery.DiscovererException; import org.apache.maven.repository.discovery.MetadataDiscoverer; import org.apache.maven.repository.indexing.ArtifactRepositoryIndex; import org.apache.maven.repository.indexing.MetadataRepositoryIndex; -import org.apache.maven.repository.indexing.PomRepositoryIndex; import org.apache.maven.repository.indexing.RepositoryIndexException; import org.apache.maven.repository.indexing.RepositoryIndexingFactory; import org.codehaus.plexus.logging.AbstractLogEnabled; @@ -104,24 +104,17 @@ public class IndexerTask String layoutProperty = configuration.getRepositoryLayout(); ArtifactDiscoverer discoverer = (ArtifactDiscoverer) artifactDiscoverers.get( layoutProperty ); - List artifacts = discoverer.discoverArtifacts( defaultRepository, blacklistedPatterns, includeSnapshots ); + List artifacts = + discoverer.discoverArtifacts( defaultRepository, "indexer", blacklistedPatterns, includeSnapshots ); if ( !artifacts.isEmpty() ) { getLogger().info( "Indexing " + artifacts.size() + " new artifacts" ); indexArtifact( artifacts, indexPath, defaultRepository ); } - // TODO: I believe this is incorrect, since it only discovers standalone POMs, not the individual artifacts! - List models = discoverer.discoverStandalonePoms( defaultRepository, blacklistedPatterns, includeSnapshots ); - if ( !models.isEmpty() ) - { - getLogger().info( "Indexing " + models.size() + " new POMs" ); - indexPom( models, indexPath, defaultRepository ); - } - MetadataDiscoverer metadataDiscoverer = (MetadataDiscoverer) metadataDiscoverers.get( layoutProperty ); List metadataList = - metadataDiscoverer.discoverMetadata( new File( defaultRepository.getBasedir() ), blacklistedPatterns ); + metadataDiscoverer.discoverMetadata( defaultRepository, "indexer", blacklistedPatterns ); if ( !metadataList.isEmpty() ) { getLogger().info( "Indexing " + metadataList.size() + " new metadata files" ); @@ -132,6 +125,10 @@ public class IndexerTask { throw new TaskExecutionException( e.getMessage(), e ); } + catch ( DiscovererException e ) + { + throw new TaskExecutionException( e.getMessage(), e ); + } time = System.currentTimeMillis() - time; getLogger().info( "Finished repository indexing process in " + time + "ms" ); @@ -195,19 +192,4 @@ public class IndexerTask metadataIndex.indexMetadata( metadataList ); metadataIndex.optimize(); } - - /** - * Index the poms in the list - * - * @param models list of poms that will be indexed - * @param indexPath the path to the index - * @param repository the artifact repository where the poms were discovered - */ - protected void indexPom( List models, File indexPath, ArtifactRepository repository ) - throws RepositoryIndexException - { - PomRepositoryIndex pomIndex = indexFactory.createPomRepositoryIndex( indexPath, repository ); - pomIndex.indexPoms( models ); - pomIndex.optimize(); - } } 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 6394cb8ff..3fc21dac4 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 @@ -18,16 +18,11 @@ package org.apache.maven.repository.discovery; 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; @@ -73,15 +68,23 @@ public abstract class AbstractArtifactDiscoverer long comparisonTimestamp = readComparisonTimestamp( repository, operation ); + // 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, new Date() ); + setLastCheckedTime( repository, operation, newLastCheckedTime ); } catch ( IOException e ) { @@ -110,71 +113,6 @@ public abstract class AbstractArtifactDiscoverer return artifacts; } - /** - * Returns a list of pom packaging 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 pom artifacts - */ - public List discoverStandalonePoms( ArtifactRepository repository, String blacklistedPatterns, - boolean includeSnapshots ) - { - List artifacts = new ArrayList(); - - File repositoryBase = new File( repository.getBasedir() ); - - // TODO: if we keep this method, set comparison timestamp properly! - List artifactPaths = scanForArtifactPaths( repositoryBase, blacklistedPatterns, 0 ); - - for ( Iterator i = artifactPaths.iterator(); i.hasNext(); ) - { - String path = (String) i.next(); - - String filename = repositoryBase.getAbsolutePath() + "/" + path; - - if ( path.toLowerCase().endsWith( POM ) ) - { - try - { - Artifact pomArtifact = buildArtifactFromPath( path, repository ); - - MavenXpp3Reader mavenReader = new MavenXpp3Reader(); - - Model model = mavenReader.read( new FileReader( filename ) ); - if ( pomArtifact != null && "pom".equals( model.getPackaging() ) ) - { - if ( includeSnapshots || !pomArtifact.isSnapshot() ) - { - artifacts.add( model ); - } - } - } - catch ( FileNotFoundException e ) - { - // this should never happen - getLogger().error( "Error finding file during POM discovery: " + filename, e ); - } - catch ( IOException e ) - { - getLogger().error( "Error reading file during POM discovery: " + filename + ": " + e ); - } - catch ( XmlPullParserException e ) - { - getLogger().error( - "Parse error reading file during POM discovery: " + filename + ": " + e.getMessage() ); - } - catch ( DiscovererException e ) - { - getLogger().error( e.getMessage() ); - } - } - } - - return artifacts; - } - public void resetLastCheckedTime( ArtifactRepository repository, String operation ) throws IOException { 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 2afad6866..dfe4fab0a 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 @@ -38,7 +38,9 @@ public interface ArtifactDiscoverer String ROLE = ArtifactDiscoverer.class.getName(); /** - * Discover artifacts in the repository. + * 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 @@ -51,17 +53,6 @@ public interface ArtifactDiscoverer boolean includeSnapshots ) throws DiscovererException; - /** - * Discover standalone POM artifacts in the repository. - * - * @param repository the location of the repository - * @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 - * @todo why do we need this? shouldn't the discovered artifacts above link to the related POM, and include standalone POMs? Why would we need just this list? - */ - List discoverStandalonePoms( ArtifactRepository repository, String blacklistedPatterns, boolean includeSnapshots ); - /** * Build an artifact from a path in the repository * diff --git a/maven-repository-discovery/src/test/java/org/apache/maven/repository/discovery/DefaultArtifactDiscovererTest.java b/maven-repository-discovery/src/test/java/org/apache/maven/repository/discovery/DefaultArtifactDiscovererTest.java index 9dba1e40f..ef833d344 100644 --- a/maven-repository-discovery/src/test/java/org/apache/maven/repository/discovery/DefaultArtifactDiscovererTest.java +++ b/maven-repository-discovery/src/test/java/org/apache/maven/repository/discovery/DefaultArtifactDiscovererTest.java @@ -17,15 +17,16 @@ package org.apache.maven.repository.discovery; */ import org.apache.maven.artifact.Artifact; -import org.apache.maven.model.Model; 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.Comparator; +import java.util.HashMap; import java.util.Iterator; import java.util.List; +import java.util.Map; /** * Test the default artifact discoverer. @@ -37,6 +38,7 @@ import java.util.List; public class DefaultArtifactDiscovererTest extends AbstractArtifactDiscovererTest { + protected String getLayout() { return "default"; @@ -48,8 +50,9 @@ public class DefaultArtifactDiscovererTest } public void testDefaultExcludes() + throws DiscovererException { - List artifacts = discoverer.discoverArtifacts( repository, null, false ); + 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; ) @@ -75,8 +78,9 @@ public class DefaultArtifactDiscovererTest } public void testStandardExcludes() + throws DiscovererException { - List artifacts = discoverer.discoverArtifacts( repository, null, false ); + 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; ) @@ -101,8 +105,9 @@ public class DefaultArtifactDiscovererTest } public void testBlacklistedExclude() + throws DiscovererException { - List artifacts = discoverer.discoverArtifacts( repository, "javax/**", false ); + List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, "javax/**", false ); assertNotNull( "Check artifacts not null", artifacts ); boolean found = false; for ( Iterator i = discoverer.getExcludedPathsIterator(); i.hasNext() && !found; ) @@ -124,8 +129,9 @@ public class DefaultArtifactDiscovererTest } public void testKickoutWithShortPath() + throws DiscovererException { - List artifacts = discoverer.discoverArtifacts( repository, null, false ); + 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; ) @@ -152,8 +158,9 @@ public class DefaultArtifactDiscovererTest } public void testKickoutWithWrongArtifactId() + throws DiscovererException { - List artifacts = discoverer.discoverArtifacts( repository, null, false ); + 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; ) @@ -181,8 +188,9 @@ public class DefaultArtifactDiscovererTest } public void testKickoutWithNoType() + throws DiscovererException { - List artifacts = discoverer.discoverArtifacts( repository, null, false ); + 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; ) @@ -208,8 +216,9 @@ public class DefaultArtifactDiscovererTest } public void testKickoutWithWrongVersion() + throws DiscovererException { - List artifacts = discoverer.discoverArtifacts( repository, null, false ); + 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; ) @@ -235,8 +244,9 @@ public class DefaultArtifactDiscovererTest } public void testKickoutWithLongerVersion() + throws DiscovererException { - List artifacts = discoverer.discoverArtifacts( repository, null, false ); + 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; ) @@ -262,8 +272,9 @@ public class DefaultArtifactDiscovererTest } public void testKickoutWithWrongSnapshotVersion() + throws DiscovererException { - List artifacts = discoverer.discoverArtifacts( repository, null, false ); + 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; ) @@ -290,8 +301,9 @@ public class DefaultArtifactDiscovererTest } public void testKickoutWithSnapshotBaseVersion() + throws DiscovererException { - List artifacts = discoverer.discoverArtifacts( repository, null, false ); + 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; ) @@ -320,8 +332,9 @@ public class DefaultArtifactDiscovererTest } public void testInclusion() + throws DiscovererException { - List artifacts = discoverer.discoverArtifacts( repository, null, true ); + List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true ); assertNotNull( "Check artifacts not null", artifacts ); assertTrue( "Check normal included", @@ -329,8 +342,9 @@ public class DefaultArtifactDiscovererTest } public void testArtifactWithClassifier() + throws DiscovererException { - List artifacts = discoverer.discoverArtifacts( repository, null, true ); + List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true ); assertNotNull( "Check artifacts not null", artifacts ); assertTrue( "Check normal included", @@ -338,8 +352,9 @@ public class DefaultArtifactDiscovererTest } public void testJavaSourcesInclusion() + throws DiscovererException { - List artifacts = discoverer.discoverArtifacts( repository, null, true ); + List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true ); assertNotNull( "Check artifacts not null", artifacts ); assertTrue( "Check normal included", artifacts.contains( @@ -347,8 +362,9 @@ public class DefaultArtifactDiscovererTest } public void testDistributionInclusion() + throws DiscovererException { - List artifacts = discoverer.discoverArtifacts( repository, null, true ); + List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true ); assertNotNull( "Check artifacts not null", artifacts ); assertTrue( "Check zip included", @@ -359,8 +375,9 @@ public class DefaultArtifactDiscovererTest } public void testSnapshotInclusion() + throws DiscovererException { - List artifacts = discoverer.discoverArtifacts( repository, null, true ); + 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" ) ) ); @@ -369,8 +386,9 @@ public class DefaultArtifactDiscovererTest } public void testSnapshotInclusionWithClassifier() + throws DiscovererException { - List artifacts = discoverer.discoverArtifacts( repository, null, true ); + List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true ); assertNotNull( "Check artifacts not null", artifacts ); assertTrue( "Check snapshot included", artifacts.contains( @@ -378,8 +396,9 @@ public class DefaultArtifactDiscovererTest } public void testSnapshotExclusion() + throws DiscovererException { - List artifacts = discoverer.discoverArtifacts( repository, null, false ); + 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" ) ) ); @@ -388,8 +407,9 @@ public class DefaultArtifactDiscovererTest } public void testFileSet() + throws DiscovererException { - List artifacts = discoverer.discoverArtifacts( repository, null, true ); + List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true ); assertNotNull( "Check artifacts not null", artifacts ); for ( Iterator i = artifacts.iterator(); i.hasNext(); ) @@ -400,9 +420,9 @@ public class DefaultArtifactDiscovererTest } public void testRepositorySet() - throws MalformedURLException + throws MalformedURLException, DiscovererException { - List artifacts = discoverer.discoverArtifacts( repository, null, true ); + List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true ); assertNotNull( "Check artifacts not null", artifacts ); String url = repository.getUrl(); @@ -415,45 +435,53 @@ public class DefaultArtifactDiscovererTest } public void testStandalonePoms() + throws DiscovererException { - List models = discoverer.discoverStandalonePoms( repository, null, false ); - assertEquals( 4, models.size() ); + List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, false ); - // Define order we expect - Collections.sort( models, new Comparator() + // 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(); ) { - public int compare( Object o1, Object o2 ) + Artifact a = (Artifact) i.next(); + + if ( "pom".equals( a.getType() ) ) { - Model m1 = (Model) o1; - Model m2 = (Model) o2; - - int result = m1.getGroupId().compareTo( m2.getGroupId() ); - if ( result == 0 ) - { - result = m1.getArtifactId().compareTo( m2.getArtifactId() ); - } - if ( result == 0 ) - { - result = m1.getVersion().compareTo( m2.getVersion() ); - } - return result; + models.add( a ); } - } ); + } + + assertEquals( 4, models.size() ); + + // Define order we expect + Collections.sort( models ); Iterator itr = models.iterator(); - Model model = (Model) itr.next(); + Artifact model = (Artifact) itr.next(); assertEquals( "org.apache.maven", model.getGroupId() ); assertEquals( "B", model.getArtifactId() ); assertEquals( "1.0", model.getVersion() ); - model = (Model) itr.next(); + model = (Artifact) itr.next(); assertEquals( "org.apache.maven", model.getGroupId() ); assertEquals( "B", model.getArtifactId() ); assertEquals( "2.0", model.getVersion() ); - model = (Model) itr.next(); + model = (Artifact) itr.next(); assertEquals( "org.apache.maven", model.getGroupId() ); assertEquals( "discovery", model.getArtifactId() ); assertEquals( "1.0", model.getVersion() ); - model = (Model) itr.next(); + model = (Artifact) itr.next(); assertEquals( "org.apache.testgroup", model.getGroupId() ); assertEquals( "discovery", model.getArtifactId() ); assertEquals( "1.0", model.getVersion() ); diff --git a/maven-repository-discovery/src/test/java/org/apache/maven/repository/discovery/LegacyArtifactDiscovererTest.java b/maven-repository-discovery/src/test/java/org/apache/maven/repository/discovery/LegacyArtifactDiscovererTest.java index 274721d14..eee2f063a 100644 --- a/maven-repository-discovery/src/test/java/org/apache/maven/repository/discovery/LegacyArtifactDiscovererTest.java +++ b/maven-repository-discovery/src/test/java/org/apache/maven/repository/discovery/LegacyArtifactDiscovererTest.java @@ -44,8 +44,9 @@ public class LegacyArtifactDiscovererTest } public void testDefaultExcludes() + throws DiscovererException { - List artifacts = discoverer.discoverArtifacts( repository, null, false ); + 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; ) @@ -70,8 +71,9 @@ public class LegacyArtifactDiscovererTest } public void testStandardExcludes() + throws DiscovererException { - List artifacts = discoverer.discoverArtifacts( repository, null, false ); + 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; ) @@ -96,8 +98,9 @@ public class LegacyArtifactDiscovererTest } public void testBlacklistedExclude() + throws DiscovererException { - List artifacts = discoverer.discoverArtifacts( repository, "javax.sql/**", false ); + List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, "javax.sql/**", false ); assertNotNull( "Check artifacts not null", artifacts ); boolean found = false; for ( Iterator i = discoverer.getExcludedPathsIterator(); i.hasNext() && !found; ) @@ -119,8 +122,9 @@ public class LegacyArtifactDiscovererTest } public void testKickoutWithShortPath() + throws DiscovererException { - List artifacts = discoverer.discoverArtifacts( repository, null, false ); + 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; ) @@ -146,8 +150,9 @@ public class LegacyArtifactDiscovererTest } public void testKickoutWithLongPath() + throws DiscovererException { - List artifacts = discoverer.discoverArtifacts( repository, null, false ); + 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; ) @@ -173,8 +178,9 @@ public class LegacyArtifactDiscovererTest } public void testKickoutWithInvalidType() + throws DiscovererException { - List artifacts = discoverer.discoverArtifacts( repository, null, false ); + 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; ) @@ -200,8 +206,9 @@ public class LegacyArtifactDiscovererTest } public void testKickoutWithNoExtension() + throws DiscovererException { - List artifacts = discoverer.discoverArtifacts( repository, null, false ); + 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; ) @@ -227,8 +234,9 @@ public class LegacyArtifactDiscovererTest } public void testKickoutWithWrongExtension() + throws DiscovererException { - List artifacts = discoverer.discoverArtifacts( repository, null, false ); + 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; ) @@ -254,8 +262,9 @@ public class LegacyArtifactDiscovererTest } public void testKickoutWithNoVersion() + throws DiscovererException { - List artifacts = discoverer.discoverArtifacts( repository, null, false ); + 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; ) @@ -280,8 +289,9 @@ public class LegacyArtifactDiscovererTest } public void testInclusion() + throws DiscovererException { - List artifacts = discoverer.discoverArtifacts( repository, null, true ); + List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true ); assertNotNull( "Check artifacts not null", artifacts ); assertTrue( "Check normal included", @@ -289,8 +299,9 @@ public class LegacyArtifactDiscovererTest } public void testTextualVersion() + throws DiscovererException { - List artifacts = discoverer.discoverArtifacts( repository, null, true ); + List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true ); assertNotNull( "Check artifacts not null", artifacts ); assertTrue( "Check normal included", @@ -298,8 +309,9 @@ public class LegacyArtifactDiscovererTest } public void testArtifactWithClassifier() + throws DiscovererException { - List artifacts = discoverer.discoverArtifacts( repository, null, true ); + List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true ); assertNotNull( "Check artifacts not null", artifacts ); assertTrue( "Check normal included", @@ -307,8 +319,9 @@ public class LegacyArtifactDiscovererTest } public void testJavaSourcesInclusion() + throws DiscovererException { - List artifacts = discoverer.discoverArtifacts( repository, null, true ); + List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true ); assertNotNull( "Check artifacts not null", artifacts ); assertTrue( "Check normal included", artifacts.contains( @@ -316,8 +329,9 @@ public class LegacyArtifactDiscovererTest } public void testDistributionInclusion() + throws DiscovererException { - List artifacts = discoverer.discoverArtifacts( repository, null, true ); + List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true ); assertNotNull( "Check artifacts not null", artifacts ); assertTrue( "Check zip included", @@ -328,8 +342,9 @@ public class LegacyArtifactDiscovererTest } public void testSnapshotInclusion() + throws DiscovererException { - List artifacts = discoverer.discoverArtifacts( repository, null, true ); + 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" ) ) ); @@ -338,8 +353,9 @@ public class LegacyArtifactDiscovererTest } public void testSnapshotExclusion() + throws DiscovererException { - List artifacts = discoverer.discoverArtifacts( repository, null, false ); + 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" ) ) ); @@ -348,8 +364,9 @@ public class LegacyArtifactDiscovererTest } public void testFileSet() + throws DiscovererException { - List artifacts = discoverer.discoverArtifacts( repository, null, true ); + List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true ); assertNotNull( "Check artifacts not null", artifacts ); for ( Iterator i = artifacts.iterator(); i.hasNext(); ) @@ -360,9 +377,9 @@ public class LegacyArtifactDiscovererTest } public void testRepositorySet() - throws MalformedURLException + throws MalformedURLException, DiscovererException { - List artifacts = discoverer.discoverArtifacts( repository, null, true ); + List artifacts = discoverer.discoverArtifacts( repository, TEST_OPERATION, null, true ); assertNotNull( "Check artifacts not null", artifacts ); String url = repository.getUrl(); diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/AbstractRepositoryIndex.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/AbstractRepositoryIndex.java index 27f13afe3..b31620734 100644 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/AbstractRepositoryIndex.java +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/AbstractRepositoryIndex.java @@ -40,6 +40,8 @@ import java.util.zip.ZipEntry; * * @author Edwin Punzalan * @todo [BP] overall am not happy with the design of this class and subclasses, but will refactor over time based on how it is used and by assessing how this affects Lucene's performance + * @todo in particular - we should remove the reference to documents and instead just index artifacts, models and metadata. + * @todo these should be individual tasks, with the grouping and atomicity (closing of index after a whole unit) done separately */ public abstract class AbstractRepositoryIndex implements RepositoryIndex diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndex.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndex.java index 1913770bf..f5c615afc 100644 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndex.java +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndex.java @@ -23,10 +23,19 @@ import org.apache.lucene.index.Term; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.versioning.DefaultArtifactVersion; +import org.apache.maven.model.Dependency; +import org.apache.maven.model.License; +import org.apache.maven.model.Model; +import org.apache.maven.model.Plugin; +import org.apache.maven.model.ReportPlugin; +import org.apache.maven.model.io.xpp3.MavenXpp3Reader; import org.apache.maven.repository.digest.Digester; import org.apache.maven.repository.digest.DigesterException; +import org.codehaus.plexus.util.StringUtils; +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; import java.io.File; +import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; @@ -101,6 +110,11 @@ public class ArtifactRepositoryIndex Artifact artifact = (Artifact) artifacts.next(); list.add( new Term( FLD_ID, ARTIFACT + ":" + artifact.getId() ) ); + + if ( "pom".equals( artifact.getType() ) ) + { + list.add( new Term( FLD_ID, POM + ":" + artifact.getId() ) ); + } } return list; @@ -129,6 +143,31 @@ public class ArtifactRepositoryIndex // TODO: log the problem and record it as a repository error // We log the problem, but do not add the document to the list to be added to the index } + + if ( "pom".equals( artifact.getType() ) ) + { + try + { + Model model = new MavenXpp3Reader().read( new FileReader( artifact.getFile() ) ); + + list.add( createDocument( artifact, model ) ); + } + catch ( IOException e ) + { + // TODO: log the problem and record it as a repository error + // We log the problem, but do not add the document to the list to be added to the index + } + catch ( XmlPullParserException e ) + { + // TODO: log the problem and record it as a repository error + // We log the problem, but do not add the document to the list to be added to the index + } + catch ( RepositoryIndexException e ) + { + // TODO: log the problem and record it as a repository error + // We log the problem, but do not add the document to the list to be added to the index + } + } } return list; @@ -347,4 +386,209 @@ public class ArtifactRepositoryIndex Collections.sort( sortedVersions ); return sortedVersions; } + + /** + * Creates a Lucene Document from a Model; used for index additions + * + * @param pom + * @return + * @throws RepositoryIndexException + */ + private Document createDocument( Artifact artifact, Model pom ) + throws RepositoryIndexException + { + String version = pom.getVersion(); + if ( version == null ) + { + // It was inherited + version = pom.getParent().getVersion(); + // TODO: do we need to use the general inheritence mechanism or do we only want to search within those defined in this pom itself? + // I think searching just this one is adequate, and it is only necessary to inherit the version and group ID [BP] + } + + String groupId = pom.getGroupId(); + if ( groupId == null ) + { + groupId = pom.getParent().getGroupId(); + } + + Document doc = new Document(); + doc.add( Field.Keyword( FLD_ID, POM + ":" + artifact.getId() ) ); + doc.add( Field.Text( FLD_GROUPID, groupId ) ); + doc.add( Field.Text( FLD_ARTIFACTID, pom.getArtifactId() ) ); + doc.add( Field.Text( FLD_VERSION, version ) ); + doc.add( Field.Keyword( FLD_PACKAGING, pom.getPackaging() ) ); + + File pomFile = new File( repository.getBasedir(), repository.pathOf( artifact ) ); + doc.add( Field.Text( FLD_SHA1, getChecksum( Digester.SHA1, pomFile.getAbsolutePath() ) ) ); + doc.add( Field.Text( FLD_MD5, getChecksum( Digester.MD5, pomFile.getAbsolutePath() ) ) ); + + indexLicenseUrls( doc, pom ); + indexDependencies( doc, pom ); + + boolean hasPlugins = false; + if ( pom.getBuild() != null && pom.getBuild().getPlugins() != null && pom.getBuild().getPlugins().size() > 0 ) + { + hasPlugins = true; + indexPlugins( doc, FLD_PLUGINS_BUILD, pom.getBuild().getPlugins().iterator() ); + indexPlugins( doc, FLD_PLUGINS_ALL, pom.getBuild().getPlugins().iterator() ); + } + else + { + doc.add( Field.Text( FLD_PLUGINS_BUILD, "" ) ); + } + + if ( pom.getReporting() != null && pom.getReporting().getPlugins() != null && + pom.getReporting().getPlugins().size() > 0 ) + { + hasPlugins = true; + indexReportPlugins( doc, FLD_PLUGINS_REPORT, pom.getReporting().getPlugins().iterator() ); + indexReportPlugins( doc, FLD_PLUGINS_ALL, pom.getReporting().getPlugins().iterator() ); + } + else + { + doc.add( Field.Text( FLD_PLUGINS_REPORT, "" ) ); + } + + if ( !hasPlugins ) + { + doc.add( Field.Text( FLD_PLUGINS_ALL, "" ) ); + } + doc.add( Field.UnIndexed( FLD_DOCTYPE, POM ) ); + // TODO: do we need to add all these empty fields? + doc.add( Field.Text( FLD_PLUGINPREFIX, "" ) ); + doc.add( Field.Text( FLD_LASTUPDATE, "" ) ); + doc.add( Field.Text( FLD_NAME, "" ) ); + doc.add( Field.Text( FLD_CLASSES, "" ) ); + doc.add( Field.Keyword( FLD_PACKAGES, "" ) ); + doc.add( Field.Text( FLD_FILES, "" ) ); + return doc; + } + + /** + * Method to index license urls found inside the passed pom + * + * @param doc the index object to create the fields for the license urls + * @param pom the Model object to be indexed + */ + private void indexLicenseUrls( Document doc, Model pom ) + { + List licenseList = pom.getLicenses(); + if ( licenseList != null && licenseList.size() > 0 ) + { + Iterator licenses = licenseList.iterator(); + while ( licenses.hasNext() ) + { + License license = (License) licenses.next(); + String url = license.getUrl(); + if ( StringUtils.isNotEmpty( url ) ) + { + doc.add( Field.Keyword( FLD_LICENSE_URLS, url ) ); + } + } + } + else + { + doc.add( Field.Keyword( FLD_LICENSE_URLS, "" ) ); + } + } + + /** + * Method to index declared dependencies found inside the passed pom + * + * @param doc the index object to create the fields for the dependencies + * @param pom the Model object to be indexed + */ + private void indexDependencies( Document doc, Model pom ) + { + List dependencyList = pom.getDependencies(); + if ( dependencyList != null && dependencyList.size() > 0 ) + { + Iterator dependencies = dependencyList.iterator(); + while ( dependencies.hasNext() ) + { + Dependency dep = (Dependency) dependencies.next(); + String id = getId( dep.getGroupId(), dep.getArtifactId(), dep.getVersion() ); + doc.add( Field.Keyword( FLD_DEPENDENCIES, id ) ); + } + } + else + { + doc.add( Field.Keyword( FLD_DEPENDENCIES, "" ) ); + } + } + + /** + * Method to index plugins to a specified index field + * + * @param doc the index object to create the fields for the plugins + * @param field the index field to store the passed plugin + * @param plugins the iterator to the list of plugins to be indexed + */ + private void indexPlugins( Document doc, String field, Iterator plugins ) + { + while ( plugins.hasNext() ) + { + Plugin plugin = (Plugin) plugins.next(); + String id = getId( plugin.getGroupId(), plugin.getArtifactId(), plugin.getVersion() ); + doc.add( Field.Keyword( field, id ) ); + } + } + + /** + * Method to index report plugins to a specified index field + * + * @param doc the index object to create the fields for the report plugins + * @param field the index field to store the passed report plugin + * @param plugins the iterator to the list of report plugins to be indexed + */ + private void indexReportPlugins( Document doc, String field, Iterator plugins ) + { + while ( plugins.hasNext() ) + { + ReportPlugin plugin = (ReportPlugin) plugins.next(); + String id = getId( plugin.getGroupId(), plugin.getArtifactId(), plugin.getVersion() ); + doc.add( Field.Keyword( field, id ) ); + } + } + + /** + * Method to generate the computed checksum of an existing file using the specified algorithm. + * + * @param algorithm the algorithm to be used to generate the checksum + * @param file the file to match the generated checksum + * @return a string representing the checksum + * @throws RepositoryIndexException + */ + private String getChecksum( String algorithm, String file ) + throws RepositoryIndexException + { + try + { + return digester.createChecksum( new File( file ), algorithm ); + } + catch ( DigesterException e ) + { + throw new RepositoryIndexException( "Failed to create checksum", e ); + } + } + + /** + * Method to create the unique artifact id to represent the artifact in the repository + * + * @param groupId the artifact groupId + * @param artifactId the artifact artifactId + * @param version the artifact version + * @return the String id to uniquely represent the artifact + */ + private String getId( String groupId, String artifactId, String version ) + { + return groupId + ":" + artifactId + ":" + version; + } + + public void deleteArtifact( Artifact artifact ) + throws IOException, RepositoryIndexException + { + deleteDocuments( getTermList( Collections.singletonList( artifact ) ) ); + } } diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexingFactory.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexingFactory.java index b03f93209..45c08f134 100644 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexingFactory.java +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexingFactory.java @@ -45,12 +45,6 @@ public class DefaultRepositoryIndexingFactory return new ArtifactRepositoryIndex( indexPath, repository, digester ); } - public PomRepositoryIndex createPomRepositoryIndex( File indexPath, ArtifactRepository repository ) - throws RepositoryIndexException - { - return new PomRepositoryIndex( indexPath, repository, digester, artifactFactory ); - } - public MetadataRepositoryIndex createMetadataRepositoryIndex( File indexPath, ArtifactRepository repository ) throws RepositoryIndexException { diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/PomRepositoryIndex.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/PomRepositoryIndex.java deleted file mode 100644 index 81ff60492..000000000 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/PomRepositoryIndex.java +++ /dev/null @@ -1,344 +0,0 @@ -package org.apache.maven.repository.indexing; - -/* - * 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.lucene.document.Document; -import org.apache.lucene.document.Field; -import org.apache.lucene.index.Term; -import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.factory.ArtifactFactory; -import org.apache.maven.artifact.repository.ArtifactRepository; -import org.apache.maven.model.Dependency; -import org.apache.maven.model.License; -import org.apache.maven.model.Model; -import org.apache.maven.model.Plugin; -import org.apache.maven.model.ReportPlugin; -import org.apache.maven.repository.digest.Digester; -import org.apache.maven.repository.digest.DigesterException; -import org.codehaus.plexus.util.StringUtils; - -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; - -/** - * Class to create index entries for a given pom in a repository - * - * @author Edwin Punzalan - */ -public class PomRepositoryIndex - extends AbstractRepositoryIndex -{ - private Digester digester; - - private ArtifactFactory artifactFactory; - - /** - * Class Constructor - * - * @param indexPath the path where the index is available or will be made available - * @param repository the repository where objects indexed by this class resides - * @param digester the digester to be used for generating checksums - * @param artifactFactory the factory for building artifact objects - */ - public PomRepositoryIndex( File indexPath, ArtifactRepository repository, Digester digester, - ArtifactFactory artifactFactory ) - throws RepositoryIndexException - { - super( indexPath, repository ); - this.digester = digester; - this.artifactFactory = artifactFactory; - } - - /** - * Method to create the index fields for a Model object into the index - * - * @param pom the Model object to be indexed - * @throws RepositoryIndexException - */ - public void indexPom( Model pom ) - throws RepositoryIndexException - { - indexPoms( Collections.singletonList( pom ) ); - } - - /** - * Index the Models within the supplied List. Deletes existing index values before adding them to the list. - * - * @param pomList - * @throws RepositoryIndexException - */ - public void indexPoms( List pomList ) - throws RepositoryIndexException - { - try - { - deleteDocuments( getTermList( pomList ) ); - } - catch ( IOException e ) - { - throw new RepositoryIndexException( "Failed to delete an index document", e ); - } - - addDocuments( getDocumentList( pomList ) ); - } - - /** - * Creates a list of Lucene Term object used in index deletion - * - * @param pomList - * @return List of Term object - */ - private List getTermList( List pomList ) - { - List terms = new ArrayList(); - - for ( Iterator poms = pomList.iterator(); poms.hasNext(); ) - { - Model pom = (Model) poms.next(); - - terms.add( new Term( FLD_ID, POM + ":" + pom.getId() ) ); - } - - return terms; - } - - /** - * Creates a list of Lucene documents - * - * @param pomList - * @return - * @throws RepositoryIndexException - */ - private List getDocumentList( List pomList ) - throws RepositoryIndexException - { - List docs = new ArrayList(); - - for ( Iterator poms = pomList.iterator(); poms.hasNext(); ) - { - Model pom = (Model) poms.next(); - - docs.add( createDocument( pom ) ); - } - - return docs; - } - - /** - * Creates a Lucene Document from a Model; used for index additions - * - * @param pom - * @return - * @throws RepositoryIndexException - */ - private Document createDocument( Model pom ) - throws RepositoryIndexException - { - String version = pom.getVersion(); - if ( version == null ) - { - // It was inherited - version = pom.getParent().getVersion(); - // TODO: do we need to use the general inheritence mechanism or do we only want to search within those defined in this pom itself? - // I think searching just this one is adequate, and it is only necessary to inherit the version and group ID [BP] - } - - String groupId = pom.getGroupId(); - if ( groupId == null ) - { - groupId = pom.getParent().getGroupId(); - } - - Document doc = new Document(); - doc.add( Field.Keyword( FLD_ID, POM + ":" + pom.getId() ) ); - doc.add( Field.Text( FLD_GROUPID, groupId ) ); - doc.add( Field.Text( FLD_ARTIFACTID, pom.getArtifactId() ) ); - doc.add( Field.Text( FLD_VERSION, version ) ); - doc.add( Field.Keyword( FLD_PACKAGING, pom.getPackaging() ) ); - - Artifact artifact = artifactFactory.createBuildArtifact( groupId, pom.getArtifactId(), version, "pom" ); - File pomFile = new File( repository.getBasedir(), repository.pathOf( artifact ) ); - doc.add( Field.Text( FLD_SHA1, getChecksum( Digester.SHA1, pomFile.getAbsolutePath() ) ) ); - doc.add( Field.Text( FLD_MD5, getChecksum( Digester.MD5, pomFile.getAbsolutePath() ) ) ); - - indexLicenseUrls( doc, pom ); - indexDependencies( doc, pom ); - - boolean hasPlugins = false; - if ( pom.getBuild() != null && pom.getBuild().getPlugins() != null && pom.getBuild().getPlugins().size() > 0 ) - { - hasPlugins = true; - indexPlugins( doc, FLD_PLUGINS_BUILD, pom.getBuild().getPlugins().iterator() ); - indexPlugins( doc, FLD_PLUGINS_ALL, pom.getBuild().getPlugins().iterator() ); - } - else - { - doc.add( Field.Text( FLD_PLUGINS_BUILD, "" ) ); - } - - if ( pom.getReporting() != null && pom.getReporting().getPlugins() != null && - pom.getReporting().getPlugins().size() > 0 ) - { - hasPlugins = true; - indexReportPlugins( doc, FLD_PLUGINS_REPORT, pom.getReporting().getPlugins().iterator() ); - indexReportPlugins( doc, FLD_PLUGINS_ALL, pom.getReporting().getPlugins().iterator() ); - } - else - { - doc.add( Field.Text( FLD_PLUGINS_REPORT, "" ) ); - } - - if ( !hasPlugins ) - { - doc.add( Field.Text( FLD_PLUGINS_ALL, "" ) ); - } - doc.add( Field.UnIndexed( FLD_DOCTYPE, POM ) ); - // TODO: do we need to add all these empty fields? - doc.add( Field.Text( FLD_PLUGINPREFIX, "" ) ); - doc.add( Field.Text( FLD_LASTUPDATE, "" ) ); - doc.add( Field.Text( FLD_NAME, "" ) ); - doc.add( Field.Text( FLD_CLASSES, "" ) ); - doc.add( Field.Keyword( FLD_PACKAGES, "" ) ); - doc.add( Field.Text( FLD_FILES, "" ) ); - return doc; - } - - /** - * Method to index license urls found inside the passed pom - * - * @param doc the index object to create the fields for the license urls - * @param pom the Model object to be indexed - */ - private void indexLicenseUrls( Document doc, Model pom ) - { - List licenseList = pom.getLicenses(); - if ( licenseList != null && licenseList.size() > 0 ) - { - Iterator licenses = licenseList.iterator(); - while ( licenses.hasNext() ) - { - License license = (License) licenses.next(); - String url = license.getUrl(); - if ( StringUtils.isNotEmpty( url ) ) - { - doc.add( Field.Keyword( FLD_LICENSE_URLS, url ) ); - } - } - } - else - { - doc.add( Field.Keyword( FLD_LICENSE_URLS, "" ) ); - } - } - - /** - * Method to index declared dependencies found inside the passed pom - * - * @param doc the index object to create the fields for the dependencies - * @param pom the Model object to be indexed - */ - private void indexDependencies( Document doc, Model pom ) - { - List dependencyList = pom.getDependencies(); - if ( dependencyList != null && dependencyList.size() > 0 ) - { - Iterator dependencies = dependencyList.iterator(); - while ( dependencies.hasNext() ) - { - Dependency dep = (Dependency) dependencies.next(); - String id = getId( dep.getGroupId(), dep.getArtifactId(), dep.getVersion() ); - doc.add( Field.Keyword( FLD_DEPENDENCIES, id ) ); - } - } - else - { - doc.add( Field.Keyword( FLD_DEPENDENCIES, "" ) ); - } - } - - /** - * Method to index plugins to a specified index field - * - * @param doc the index object to create the fields for the plugins - * @param field the index field to store the passed plugin - * @param plugins the iterator to the list of plugins to be indexed - */ - private void indexPlugins( Document doc, String field, Iterator plugins ) - { - while ( plugins.hasNext() ) - { - Plugin plugin = (Plugin) plugins.next(); - String id = getId( plugin.getGroupId(), plugin.getArtifactId(), plugin.getVersion() ); - doc.add( Field.Keyword( field, id ) ); - } - } - - /** - * Method to index report plugins to a specified index field - * - * @param doc the index object to create the fields for the report plugins - * @param field the index field to store the passed report plugin - * @param plugins the iterator to the list of report plugins to be indexed - */ - private void indexReportPlugins( Document doc, String field, Iterator plugins ) - { - while ( plugins.hasNext() ) - { - ReportPlugin plugin = (ReportPlugin) plugins.next(); - String id = getId( plugin.getGroupId(), plugin.getArtifactId(), plugin.getVersion() ); - doc.add( Field.Keyword( field, id ) ); - } - } - - /** - * Method to generate the computed checksum of an existing file using the specified algorithm. - * - * @param algorithm the algorithm to be used to generate the checksum - * @param file the file to match the generated checksum - * @return a string representing the checksum - * @throws RepositoryIndexException - */ - private String getChecksum( String algorithm, String file ) - throws RepositoryIndexException - { - try - { - return digester.createChecksum( new File( file ), algorithm ); - } - catch ( DigesterException e ) - { - throw new RepositoryIndexException( "Failed to create checksum", e ); - } - } - - /** - * Method to create the unique artifact id to represent the artifact in the repository - * - * @param groupId the artifact groupId - * @param artifactId the artifact artifactId - * @param version the artifact version - * @return the String id to uniquely represent the artifact - */ - private String getId( String groupId, String artifactId, String version ) - { - return groupId + ":" + artifactId + ":" + version; - } -} diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexingFactory.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexingFactory.java index e51701cbb..26b76ed89 100644 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexingFactory.java +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexingFactory.java @@ -39,17 +39,6 @@ public interface RepositoryIndexingFactory ArtifactRepositoryIndex createArtifactRepositoryIndex( File indexPath, ArtifactRepository repository ) throws RepositoryIndexException; - /** - * Method to create an instance of the PomRepositoryIndex - * - * @param indexPath the path where the index will be created/updated - * @param repository the repository where the indexed poms are located - * @return the PomRepositoryIndex instance - * @throws RepositoryIndexException - */ - PomRepositoryIndex createPomRepositoryIndex( File indexPath, ArtifactRepository repository ) - throws RepositoryIndexException; - /** * Method to create instance of the MetadataRepositoryIndex * diff --git a/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndexingTest.java b/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndexingTest.java index 83c78b586..c4bd64413 100644 --- a/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndexingTest.java +++ b/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/ArtifactRepositoryIndexingTest.java @@ -23,6 +23,7 @@ import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; import org.apache.maven.repository.digest.DefaultDigester; import org.apache.maven.repository.digest.Digester; +import org.apache.maven.repository.digest.DigesterException; import org.apache.maven.repository.indexing.query.CompoundQuery; import org.apache.maven.repository.indexing.query.Query; import org.apache.maven.repository.indexing.query.SinglePhraseQuery; @@ -33,6 +34,7 @@ import java.io.File; import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import java.util.Map; /** * @author Edwin Punzalan @@ -54,16 +56,54 @@ public class ArtifactRepositoryIndexingTest super.setUp(); File repositoryDirectory = getTestFile( "src/test/repository" ); - String repoDir = repositoryDirectory.toURL().toString(); + String repoDir = repositoryDirectory.toURI().toURL().toString(); ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" ); ArtifactRepositoryFactory repoFactory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE ); repository = repoFactory.createArtifactRepository( "test", repoDir, layout, null, null ); digester = new DefaultDigester(); + artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE ); indexPath = getTestFile( "target/index" ); FileUtils.deleteDirectory( indexPath ); } + public void testInheritedFields() + throws Exception + { + RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE ); + Artifact artifact = createArtifact( "test.inherited", "test-inherited", "1.0.15", "pom" ); + + ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository ); + indexer.indexArtifact( artifact ); + + RepositoryIndexSearchLayer repoSearchLayer = + (RepositoryIndexSearchLayer) lookup( RepositoryIndexSearchLayer.ROLE ); + + // search version + Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "1.0.15" ); + List artifactList = repoSearchLayer.searchAdvanced( qry, indexer ); + assertEquals( 1, artifactList.size() ); + for ( Iterator iter = artifactList.iterator(); iter.hasNext(); ) + { + SearchResult result = (SearchResult) iter.next(); + Artifact a = result.getArtifact(); + assertEquals( "1.0.15", a.getVersion() ); + } + + // search group id + qry = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "test.inherited" ); + artifactList = repoSearchLayer.searchAdvanced( qry, indexer ); + assertEquals( 1, artifactList.size() ); + Iterator artifacts = artifactList.iterator(); + if ( artifacts.hasNext() ) + { + SearchResult result = (SearchResult) artifacts.next(); + Artifact a = result.getArtifact(); + assertEquals( "test.inherited", a.getGroupId() ); + } + + } + /** * Method for testing the exceptions thrown by ArtifactRepositoryIndex * @@ -73,8 +113,33 @@ public class ArtifactRepositoryIndexingTest throws Exception { RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE ); - Artifact artifact = getArtifact( "test", "test-artifactId", "1.0" ); - artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); + Artifact artifact = createArtifact( "test", "test-artifactId", "1.0" ); + + try + { + File notIndexDir = new File( "pom.xml" ); + ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( notIndexDir, repository ); + indexer.indexArtifact( artifact ); + fail( "Must throw exception on non-directory index directory" ); + } + catch ( RepositoryIndexException e ) + { + assertTrue( true ); + } + + try + { + File notIndexDir = new File( "" ); + ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( notIndexDir, repository ); + indexer.indexArtifact( artifact ); + fail( "Must throw an exception on a non-index directory" ); + } + catch ( RepositoryIndexException e ) + { + assertTrue( true ); + } + + artifact = createArtifact( "test", "test-artifactId", "1.0", "pom" ); try { @@ -116,25 +181,25 @@ public class ArtifactRepositoryIndexingTest List artifacts = new ArrayList(); - Artifact artifact = getArtifact( "org.apache.maven", "maven-artifact", "2.0.1" ); - artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); + Artifact artifact = createArtifact( "org.apache.maven", "maven-artifact", "2.0.1" ); artifacts.add( artifact ); - artifact = getArtifact( "org.apache.maven", "maven-model", "2.0" ); - artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); + artifact = createArtifact( "org.apache.maven", "maven-model", "2.0" ); artifacts.add( artifact ); - artifact = getArtifact( "test", "test-artifactId", "1.0" ); - artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); + artifact = createArtifact( "test", "test-artifactId", "1.0" ); artifacts.add( artifact ); - indexer.indexArtifacts( artifacts ); + artifact = createArtifact( "org.apache.maven", "maven-artifact", "2.0.1", "pom" ); + artifacts.add( artifact ); - artifact = getArtifact( "test", "test-artifactId", "1.0" ); - artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); - indexer.indexArtifact( artifact ); + artifact = createArtifact( "org.apache.maven", "maven-model", "2.0", "pom" ); + artifacts.add( artifact ); + + artifact = createArtifact( "test", "test-artifactId", "1.0", "pom" ); + artifacts.add( artifact ); - indexer.optimize(); + indexer.indexArtifacts( artifacts ); } /** @@ -156,7 +221,7 @@ public class ArtifactRepositoryIndexingTest // search version Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "1.0" ); List artifacts = repoSearchLayer.searchAdvanced( qry, indexer ); - assertEquals( 1, artifacts.size() ); + assertEquals( 2, artifacts.size() ); for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) { SearchResult result = (SearchResult) iter.next(); @@ -164,11 +229,45 @@ public class ArtifactRepositoryIndexingTest assertEquals( "1.0", artifact.getVersion() ); } + // search group id + qry = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "org.apache.maven" ); + artifacts = repoSearchLayer.searchAdvanced( qry, indexer ); + assertEquals( 4, artifacts.size() ); + Iterator iter = artifacts.iterator(); + if ( iter.hasNext() ) + { + SearchResult result = (SearchResult) iter.next(); + Artifact artifact = result.getArtifact(); + assertEquals( "org.apache.maven", artifact.getGroupId() ); + } + + // search artifact id + qry = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "maven-artifact" ); + artifacts = repoSearchLayer.searchAdvanced( qry, indexer ); + assertEquals( 2, artifacts.size() ); + for ( iter = artifacts.iterator(); iter.hasNext(); ) + { + SearchResult result = (SearchResult) iter.next(); + Artifact artifact = result.getArtifact(); + assertEquals( "maven-artifact", artifact.getArtifactId() ); + } + + // search version + qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "2" ); + artifacts = repoSearchLayer.searchAdvanced( qry, indexer ); + assertEquals( 4, artifacts.size() ); + for ( iter = artifacts.iterator(); iter.hasNext(); ) + { + SearchResult result = (SearchResult) iter.next(); + Artifact artifact = result.getArtifact(); + assertTrue( artifact.getVersion().indexOf( "2" ) != -1 ); + } + // search classes qry = new SinglePhraseQuery( RepositoryIndex.FLD_CLASSES, "App" ); artifacts = repoSearchLayer.searchAdvanced( qry, indexer ); assertEquals( 1, artifacts.size() ); - for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) + for ( iter = artifacts.iterator(); iter.hasNext(); ) { SearchResult result = (SearchResult) iter.next(); Artifact artifact = result.getArtifact(); @@ -179,7 +278,7 @@ public class ArtifactRepositoryIndexingTest qry = new SinglePhraseQuery( RepositoryIndex.FLD_PACKAGES, "groupId" ); artifacts = repoSearchLayer.searchAdvanced( qry, indexer ); assertEquals( 1, artifacts.size() ); - for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) + for ( iter = artifacts.iterator(); iter.hasNext(); ) { SearchResult result = (SearchResult) iter.next(); Artifact artifact = result.getArtifact(); @@ -190,7 +289,7 @@ public class ArtifactRepositoryIndexingTest qry = new SinglePhraseQuery( RepositoryIndex.FLD_FILES, "pom.xml" ); artifacts = repoSearchLayer.searchAdvanced( qry, indexer ); assertEquals( 3, artifacts.size() ); - Iterator iter = artifacts.iterator(); + iter = artifacts.iterator(); if ( iter.hasNext() ) { SearchResult result = (SearchResult) iter.next(); @@ -198,68 +297,130 @@ public class ArtifactRepositoryIndexingTest assertEquals( "test-artifactId", artifact.getArtifactId() ); } - // search group id - qry = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "org.apache.maven" ); + // search packaging + qry = new SinglePhraseQuery( RepositoryIndex.FLD_PACKAGING, "jar" ); artifacts = repoSearchLayer.searchAdvanced( qry, indexer ); - assertEquals( 2, artifacts.size() ); - iter = artifacts.iterator(); - if ( iter.hasNext() ) + assertEquals( 3, artifacts.size() ); + for ( iter = artifacts.iterator(); iter.hasNext(); ) { SearchResult result = (SearchResult) iter.next(); - Artifact artifact = result.getArtifact(); - assertEquals( "org.apache.maven", artifact.getGroupId() ); + Map map = result.getFieldMatches(); + assertEquals( "jar", (String) map.get( RepositoryIndex.FLD_PACKAGING ) ); } - // search artifact id - qry = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "maven-artifact" ); + //search license url + qry = + new SinglePhraseQuery( RepositoryIndex.FLD_LICENSE_URLS, "http://www.apache.org/licenses/LICENSE-2.0.txt" ); artifacts = repoSearchLayer.searchAdvanced( qry, indexer ); - assertEquals( 1, artifacts.size() ); + assertEquals( 2, artifacts.size() ); for ( iter = artifacts.iterator(); iter.hasNext(); ) { SearchResult result = (SearchResult) iter.next(); - Artifact artifact = result.getArtifact(); - assertEquals( "maven-artifact", artifact.getArtifactId() ); + Map map = result.getFieldMatches(); + List matches = (List) map.get( RepositoryIndex.FLD_LICENSE_URLS ); + for ( Iterator it = matches.iterator(); it.hasNext(); ) + { + assertEquals( "http://www.apache.org/licenses/LICENSE-2.0.txt", (String) it.next() ); + } } - // search version - qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "2" ); + //search dependencies + qry = new SinglePhraseQuery( RepositoryIndex.FLD_DEPENDENCIES, "org.codehaus.plexus:plexus-utils:1.0.5" ); artifacts = repoSearchLayer.searchAdvanced( qry, indexer ); assertEquals( 2, artifacts.size() ); for ( iter = artifacts.iterator(); iter.hasNext(); ) { SearchResult result = (SearchResult) iter.next(); - Artifact artifact = result.getArtifact(); - assertTrue( artifact.getVersion().indexOf( "2" ) != -1 ); + Map map = result.getFieldMatches(); + boolean depFound = false; + List list = (List) map.get( RepositoryIndex.FLD_DEPENDENCIES ); + Iterator dependencies = list.iterator(); + while ( dependencies.hasNext() ) + { + String dep = (String) dependencies.next(); + if ( "org.codehaus.plexus:plexus-utils:1.0.5".equals( dep ) ) + { + depFound = true; + break; + } + } + assertTrue( "Searched dependency not found.", depFound ); } - // search sha1 checksum - Artifact artifact = getArtifact( "org.apache.maven", "maven-model", "2.0" ); - artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); - - String sha1 = digester.createChecksum( artifact.getFile(), Digester.SHA1 ); - - qry = new SinglePhraseQuery( RepositoryIndex.FLD_SHA1, sha1.trim() ); + //search build plugin + qry = + new SinglePhraseQuery( RepositoryIndex.FLD_PLUGINS_BUILD, "org.codehaus.modello:modello-maven-plugin:2.0" ); artifacts = repoSearchLayer.searchAdvanced( qry, indexer ); assertEquals( 1, artifacts.size() ); for ( iter = artifacts.iterator(); iter.hasNext(); ) { SearchResult result = (SearchResult) iter.next(); - Artifact artifact2 = result.getArtifact(); - String sha1Tmp = digester.createChecksum( artifact2.getFile(), Digester.SHA1 ); - assertEquals( sha1, sha1Tmp ); + Map map = result.getFieldMatches(); + List list = (List) map.get( RepositoryIndex.FLD_PLUGINS_BUILD ); + Iterator plugins = list.iterator(); + boolean found = false; + while ( plugins.hasNext() ) + { + String plugin = (String) plugins.next(); + if ( "org.codehaus.modello:modello-maven-plugin:2.0".equals( plugin ) ) + { + found = true; + break; + } + } + assertTrue( "Searched plugin not found.", found ); } - // search md5 checksum - String md5 = digester.createChecksum( artifact.getFile(), Digester.MD5 ); - qry = new SinglePhraseQuery( RepositoryIndex.FLD_MD5, md5.trim() ); + //search reporting plugin + qry = new SinglePhraseQuery( RepositoryIndex.FLD_PLUGINS_REPORT, + "org.apache.maven.plugins:maven-checkstyle-plugin:2.0" ); artifacts = repoSearchLayer.searchAdvanced( qry, indexer ); assertEquals( 1, artifacts.size() ); for ( iter = artifacts.iterator(); iter.hasNext(); ) + { + SearchResult result = (SearchResult) iter.next(); + Map map = result.getFieldMatches(); + List list = (List) map.get( RepositoryIndex.FLD_PLUGINS_REPORT ); + Iterator plugins = list.iterator(); + boolean found = false; + while ( plugins.hasNext() ) + { + String plugin = (String) plugins.next(); + if ( "org.apache.maven.plugins:maven-checkstyle-plugin:2.0".equals( plugin ) ) + { + found = true; + break; + } + } + assertTrue( "Searched report plugin not found.", found ); + } + + Artifact artifact = createArtifact( "org.apache.maven", "maven-model", "2.0" ); + + confirmChecksum( artifact, Digester.SHA1, RepositoryIndex.FLD_SHA1, repoSearchLayer, indexer ); + confirmChecksum( artifact, Digester.MD5, RepositoryIndex.FLD_MD5, repoSearchLayer, indexer ); + + artifact = createArtifact( "org.apache.maven", "maven-model", "2.0", "pom" ); + + confirmChecksum( artifact, Digester.SHA1, RepositoryIndex.FLD_SHA1, repoSearchLayer, indexer ); + confirmChecksum( artifact, Digester.MD5, RepositoryIndex.FLD_MD5, repoSearchLayer, indexer ); + } + + private void confirmChecksum( Artifact artifact, String algorithm, String field, + RepositoryIndexSearchLayer repoSearchLayer, ArtifactRepositoryIndex indexer ) + throws DigesterException, RepositoryIndexSearchException + { + String sha1 = digester.createChecksum( artifact.getFile(), algorithm ); + + Query qry = new SinglePhraseQuery( field, sha1.trim() ); + List artifacts = repoSearchLayer.searchAdvanced( qry, indexer ); + assertEquals( 1, artifacts.size() ); + for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) { SearchResult result = (SearchResult) iter.next(); Artifact artifact2 = result.getArtifact(); - String md5Tmp = digester.createChecksum( artifact2.getFile(), Digester.MD5 ); - assertEquals( md5, md5Tmp ); + String sha1Tmp = digester.createChecksum( artifact2.getFile(), algorithm ); + assertEquals( sha1, sha1Tmp ); } } @@ -276,6 +437,7 @@ public class ArtifactRepositoryIndexingTest RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE ); RepositoryIndexSearchLayer repoSearchLayer = (RepositoryIndexSearchLayer) lookup( RepositoryIndexSearchLayer.ROLE ); + ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository ); // Criteria 1: required query @@ -330,7 +492,7 @@ public class ArtifactRepositoryIndexingTest CompoundQuery rQry2 = new CompoundQuery(); rQry2.or( oQry ); rQry2.and( rQry ); - rQry2.or( oQry5 ); + rQry2.and( oQry5 ); artifacts = repoSearchLayer.searchAdvanced( rQry2, indexer ); for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) @@ -342,19 +504,42 @@ public class ArtifactRepositoryIndexingTest assertEquals( "2.0.1", artifact.getVersion() ); } + CompoundQuery oQry6 = new CompoundQuery(); + Query qry11 = + new SinglePhraseQuery( RepositoryIndex.FLD_DEPENDENCIES, "org.codehaus.plexus:plexus-utils:1.0.5" ); + Query qry12 = new SinglePhraseQuery( RepositoryIndex.FLD_DEPENDENCIES, + "org.codehaus.plexus:plexus-container-defualt:1.0-alpha-9" ); + oQry6.or( qry11 ); + oQry6.or( qry12 ); + + CompoundQuery rQry3 = new CompoundQuery(); + rQry3.or( oQry ); + rQry3.and( rQry ); + rQry3.and( oQry6 ); + + artifacts = repoSearchLayer.searchAdvanced( rQry3, indexer ); + for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) + { + SearchResult result = (SearchResult) iter.next(); + Artifact artifact = result.getArtifact(); + assertEquals( "maven-artifact", artifact.getArtifactId() ); + assertEquals( "org.apache.maven", artifact.getGroupId() ); + assertEquals( "2.0.1", artifact.getVersion() ); + } + // Criteria 4: nested required query // ex. [(artifactId=maven-artifact AND groupId=org.apache.maven) AND // (version=2.0.3 OR version=2.0.1) // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)] // OR [(artifactId=sample AND groupId=test)] - CompoundQuery rQry3 = new CompoundQuery(); + CompoundQuery rQry4 = new CompoundQuery(); Query qry5 = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "sample" ); Query qry6 = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "test" ); - rQry3.and( qry5 ); - rQry3.and( qry6 ); + rQry4.and( qry5 ); + rQry4.and( qry6 ); CompoundQuery oQry2 = new CompoundQuery(); - oQry2.and( rQry2 ); - oQry2.and( rQry3 ); + oQry2.and( rQry4 ); + oQry2.and( rQry4 ); artifacts = repoSearchLayer.searchAdvanced( oQry2, indexer ); for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) @@ -372,12 +557,12 @@ public class ArtifactRepositoryIndexingTest // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)] OR // [(artifactId=sample AND groupId=test)] OR // [(artifactId=sample2 AND groupId=test)] - CompoundQuery rQry4 = new CompoundQuery(); + CompoundQuery rQry5 = new CompoundQuery(); Query qry7 = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "sample2" ); Query qry8 = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "test" ); - rQry4.and( qry7 ); - rQry4.and( qry8 ); - oQry2.and( rQry4 ); + rQry5.and( qry7 ); + rQry5.and( qry8 ); + oQry2.and( rQry5 ); artifacts = repoSearchLayer.searchAdvanced( oQry2, indexer ); for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) @@ -446,13 +631,28 @@ public class ArtifactRepositoryIndexingTest ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository ); - Artifact artifact = getArtifact( "org.apache.maven", "maven-artifact", "2.0.1" ); - artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); - indexer.deleteDocument( RepositoryIndex.FLD_ID, RepositoryIndex.ARTIFACT + artifact.getId() ); + Artifact artifact = createArtifact( "org.apache.maven", "maven-artifact", "2.0.1" ); + Artifact pomArtifact = + createArtifact( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), "pom" ); - Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_ID, RepositoryIndex.ARTIFACT + artifact.getId() ); - List artifacts = repoSearcher.search( qry, indexer ); - assertEquals( 0, artifacts.size() ); + Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_ID, RepositoryIndex.ARTIFACT + ":" + artifact.getId() ); + List results = repoSearcher.search( qry, indexer ); + assertFalse( results.isEmpty() ); + + qry = new SinglePhraseQuery( RepositoryIndex.FLD_ID, RepositoryIndex.POM + ":" + pomArtifact.getId() ); + results = repoSearcher.search( qry, indexer ); + assertFalse( results.isEmpty() ); + + indexer.deleteArtifact( artifact ); + indexer.deleteArtifact( pomArtifact ); + + qry = new SinglePhraseQuery( RepositoryIndex.FLD_ID, RepositoryIndex.ARTIFACT + ":" + artifact.getId() ); + results = repoSearcher.search( qry, indexer ); + assertTrue( results.isEmpty() ); + + qry = new SinglePhraseQuery( RepositoryIndex.FLD_ID, RepositoryIndex.POM + ":" + pomArtifact.getId() ); + results = repoSearcher.search( qry, indexer ); + assertTrue( results.isEmpty() ); } /** @@ -470,8 +670,7 @@ public class ArtifactRepositoryIndexingTest ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository ); - Artifact artifact = getArtifact( "org.apache.maven", "maven-corrupt-jar", "2.0" ); - artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); + Artifact artifact = createArtifact( "org.apache.maven", "maven-corrupt-jar", "2.0" ); indexer.indexArtifact( artifact ); Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_ID, RepositoryIndex.ARTIFACT + artifact.getId() ); @@ -488,15 +687,17 @@ public class ArtifactRepositoryIndexingTest * @return Artifact object * @throws Exception */ - private Artifact getArtifact( String groupId, String artifactId, String version ) + private Artifact createArtifact( String groupId, String artifactId, String version ) throws Exception { - if ( artifactFactory == null ) - { - artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE ); - } + return createArtifact( groupId, artifactId, version, "jar" ); + } - return artifactFactory.createBuildArtifact( groupId, artifactId, version, "jar" ); + private Artifact createArtifact( String groupId, String artifactId, String version, String type ) + { + Artifact artifact = artifactFactory.createBuildArtifact( groupId, artifactId, version, type ); + artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); + return artifact; } protected void tearDown() diff --git a/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/PomRepositoryIndexingTest.java b/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/PomRepositoryIndexingTest.java deleted file mode 100644 index e89d1c3bf..000000000 --- a/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/PomRepositoryIndexingTest.java +++ /dev/null @@ -1,551 +0,0 @@ -package org.apache.maven.repository.indexing; - -/* - * 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.model.Model; -import org.apache.maven.model.io.xpp3.MavenXpp3Reader; -import org.apache.maven.repository.digest.DefaultDigester; -import org.apache.maven.repository.digest.Digester; -import org.apache.maven.repository.indexing.query.CompoundQuery; -import org.apache.maven.repository.indexing.query.Query; -import org.apache.maven.repository.indexing.query.SinglePhraseQuery; -import org.codehaus.plexus.PlexusTestCase; -import org.codehaus.plexus.util.FileUtils; - -import java.io.File; -import java.io.FileReader; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Set; - -/** - * @author Edwin Punzalan - */ -public class PomRepositoryIndexingTest - extends PlexusTestCase -{ - private ArtifactRepository repository; - - private ArtifactFactory artifactFactory; - - private File indexPath; - - private Digester digester; - - protected void setUp() - throws Exception - { - super.setUp(); - - File repositoryDirectory = getTestFile( "src/test/repository" ); - String repoDir = repositoryDirectory.toURL().toString(); - ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" ); - ArtifactRepositoryFactory repoFactory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE ); - repository = repoFactory.createArtifactRepository( "test", repoDir, layout, null, null ); - digester = new DefaultDigester(); - - indexPath = getTestFile( "target/index" ); - FileUtils.deleteDirectory( indexPath ); - } - - - public void testIndexerExceptions() - throws Exception - { - RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE ); - Model pom = getPom( "test", "test-artifactId", "1.0" ); - - try - { - File notIndexDir = new File( "pom.xml" ); - PomRepositoryIndex indexer = factory.createPomRepositoryIndex( notIndexDir, repository ); - indexer.indexPom( pom ); - fail( "Must throw exception on non-directory index directory" ); - } - catch ( RepositoryIndexException e ) - { - assertTrue( true ); - } - - try - { - File notIndexDir = new File( "" ); - PomRepositoryIndex indexer = factory.createPomRepositoryIndex( notIndexDir, repository ); - indexer.indexPom( pom ); - fail( "Must throw an exception on a non-index directory" ); - } - catch ( RepositoryIndexException e ) - { - assertTrue( true ); - } - } - - public void testInheritedFields() - throws Exception - { - RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE ); - Model pom = getPom( "test.inherited", "test-inherited", "1.0.15" ); - - PomRepositoryIndex indexer = factory.createPomRepositoryIndex( indexPath, repository ); - indexer.indexPom( pom ); - - RepositoryIndexSearchLayer repoSearchLayer = - (RepositoryIndexSearchLayer) lookup( RepositoryIndexSearchLayer.ROLE ); - - // search version - Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "1.0.15" ); - List artifactList = repoSearchLayer.searchAdvanced( qry, indexer ); - assertEquals( 1, artifactList.size() ); - for ( Iterator iter = artifactList.iterator(); iter.hasNext(); ) - { - SearchResult result = (SearchResult) iter.next(); - Artifact artifact = result.getArtifact(); - assertEquals( "1.0.15", artifact.getVersion() ); - } - - // search group id - qry = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "test.inherited" ); - artifactList = repoSearchLayer.searchAdvanced( qry, indexer ); - assertEquals( 1, artifactList.size() ); - Iterator artifacts = artifactList.iterator(); - if ( artifacts.hasNext() ) - { - SearchResult result = (SearchResult) artifacts.next(); - Artifact artifact = result.getArtifact(); - assertEquals( "test.inherited", artifact.getGroupId() ); - } - - } - - /** - * Test the PomRepositoryIndex with DefaultRepositoryIndexSearcher using a single-phrase search. - * - * @throws Exception - */ - public void testSearchSingle() - throws Exception - { - createTestIndex(); - - RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE ); - RepositoryIndexSearchLayer repoSearchLayer = - (RepositoryIndexSearchLayer) lookup( RepositoryIndexSearchLayer.ROLE ); - - PomRepositoryIndex indexer = factory.createPomRepositoryIndex( indexPath, repository ); - - // search version - Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "1.0" ); - List artifactList = repoSearchLayer.searchAdvanced( qry, indexer ); - assertEquals( 1, artifactList.size() ); - for ( Iterator iter = artifactList.iterator(); iter.hasNext(); ) - { - SearchResult result = (SearchResult) iter.next(); - Artifact artifact = result.getArtifact(); - assertEquals( "1.0", artifact.getVersion() ); - } - - // search group id - qry = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "org.apache.maven" ); - artifactList = repoSearchLayer.searchAdvanced( qry, indexer ); - assertEquals( 2, artifactList.size() ); - Iterator artifacts = artifactList.iterator(); - if ( artifacts.hasNext() ) - { - SearchResult result = (SearchResult) artifacts.next(); - Artifact artifact = result.getArtifact(); - assertEquals( "org.apache.maven", artifact.getGroupId() ); - } - - // search artifact id - qry = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "maven-artifact" ); - artifactList = repoSearchLayer.searchAdvanced( qry, indexer ); - assertEquals( 1, artifactList.size() ); - for ( artifacts = artifactList.iterator(); artifacts.hasNext(); ) - { - SearchResult result = (SearchResult) artifacts.next(); - Artifact artifact = result.getArtifact(); - assertEquals( "maven-artifact", artifact.getArtifactId() ); - } - - // search version - qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "2" ); - artifactList = repoSearchLayer.searchAdvanced( qry, indexer ); - assertEquals( 2, artifactList.size() ); - for ( artifacts = artifactList.iterator(); artifacts.hasNext(); ) - { - SearchResult result = (SearchResult) artifacts.next(); - Artifact artifact = result.getArtifact(); - assertTrue( artifact.getVersion().indexOf( "2" ) != -1 ); - } - - // search packaging - qry = new SinglePhraseQuery( RepositoryIndex.FLD_PACKAGING, "jar" ); - artifactList = repoSearchLayer.searchAdvanced( qry, indexer ); - assertEquals( 3, artifactList.size() ); - for ( artifacts = artifactList.iterator(); artifacts.hasNext(); ) - { - SearchResult result = (SearchResult) artifacts.next(); - Map map = result.getFieldMatches(); - Set mapEntry = map.entrySet(); - assertEquals( "jar", (String) map.get( RepositoryIndex.FLD_PACKAGING ) ); - } - - //search license url - qry = - new SinglePhraseQuery( RepositoryIndex.FLD_LICENSE_URLS, "http://www.apache.org/licenses/LICENSE-2.0.txt" ); - artifactList = repoSearchLayer.searchAdvanced( qry, indexer ); - assertEquals( 2, artifactList.size() ); - for ( artifacts = artifactList.iterator(); artifacts.hasNext(); ) - { - SearchResult result = (SearchResult) artifacts.next(); - Map map = result.getFieldMatches(); - List matches = (List) map.get( RepositoryIndex.FLD_LICENSE_URLS ); - for ( Iterator it = matches.iterator(); it.hasNext(); ) - { - assertEquals( "http://www.apache.org/licenses/LICENSE-2.0.txt", (String) it.next() ); - } - } - - //search dependencies - qry = new SinglePhraseQuery( RepositoryIndex.FLD_DEPENDENCIES, "org.codehaus.plexus:plexus-utils:1.0.5" ); - artifactList = repoSearchLayer.searchAdvanced( qry, indexer ); - assertEquals( 2, artifactList.size() ); - for ( artifacts = artifactList.iterator(); artifacts.hasNext(); ) - { - SearchResult result = (SearchResult) artifacts.next(); - Map map = result.getFieldMatches(); - boolean depFound = false; - List list = (List) map.get( RepositoryIndex.FLD_DEPENDENCIES ); - Iterator dependencies = list.iterator(); - while ( dependencies.hasNext() ) - { - String dep = (String) dependencies.next(); - if ( "org.codehaus.plexus:plexus-utils:1.0.5".equals( dep ) ) - { - depFound = true; - break; - } - } - assertTrue( "Searched dependency not found.", depFound ); - } - - //search build plugin - qry = - new SinglePhraseQuery( RepositoryIndex.FLD_PLUGINS_BUILD, "org.codehaus.modello:modello-maven-plugin:2.0" ); - artifactList = repoSearchLayer.searchAdvanced( qry, indexer ); - assertEquals( 1, artifactList.size() ); - for ( artifacts = artifactList.iterator(); artifacts.hasNext(); ) - { - SearchResult result = (SearchResult) artifacts.next(); - Map map = result.getFieldMatches(); - List list = (List) map.get( RepositoryIndex.FLD_PLUGINS_BUILD ); - Iterator plugins = list.iterator(); - boolean found = false; - while ( plugins.hasNext() ) - { - String plugin = (String) plugins.next(); - if ( "org.codehaus.modello:modello-maven-plugin:2.0".equals( plugin ) ) - { - found = true; - break; - } - } - assertTrue( "Searched plugin not found.", found ); - } - - //search reporting plugin - qry = new SinglePhraseQuery( RepositoryIndex.FLD_PLUGINS_REPORT, - "org.apache.maven.plugins:maven-checkstyle-plugin:2.0" ); - artifactList = repoSearchLayer.searchAdvanced( qry, indexer ); - assertEquals( 1, artifactList.size() ); - for ( artifacts = artifactList.iterator(); artifacts.hasNext(); ) - { - SearchResult result = (SearchResult) artifacts.next(); - Map map = result.getFieldMatches(); - List list = (List) map.get( RepositoryIndex.FLD_PLUGINS_REPORT ); - Iterator plugins = list.iterator(); - boolean found = false; - while ( plugins.hasNext() ) - { - String plugin = (String) plugins.next(); - if ( "org.apache.maven.plugins:maven-checkstyle-plugin:2.0".equals( plugin ) ) - { - found = true; - break; - } - } - assertTrue( "Searched report plugin not found.", found ); - } - - // search sha1 checksum - Artifact artifact = getArtifact( "org.apache.maven", "maven-model", "2.0" ); - artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); - String sha1 = digester.createChecksum( artifact.getFile(), Digester.SHA1 ); - - qry = new SinglePhraseQuery( RepositoryIndex.FLD_SHA1, sha1.trim() ); - artifactList = repoSearchLayer.searchAdvanced( qry, indexer ); - assertEquals( 1, artifactList.size() ); - for ( artifacts = artifactList.iterator(); artifacts.hasNext(); ) - { - SearchResult result = (SearchResult) artifacts.next(); - Artifact artifact2 = result.getArtifact(); - String sha1Tmp = digester.createChecksum( getPomFile( artifact2 ), Digester.SHA1 ); - assertEquals( sha1, sha1Tmp ); - } - - // search md5 checksum - String md5 = digester.createChecksum( getPomFile( artifact ), Digester.MD5 ); - qry = new SinglePhraseQuery( RepositoryIndex.FLD_MD5, md5.trim() ); - artifactList = repoSearchLayer.searchAdvanced( qry, indexer ); - assertEquals( 1, artifactList.size() ); - for ( artifacts = artifactList.iterator(); artifacts.hasNext(); ) - { - SearchResult result = (SearchResult) artifacts.next(); - Artifact artifact2 = result.getArtifact(); - String md5Tmp = digester.createChecksum( getPomFile( artifact2 ), Digester.MD5 ); - assertEquals( md5, md5Tmp ); - } - } - - /** - * Test the PomRepositoryIndex with DefaultRepositoryIndexSearcher using compound search (AND, OR). - * - * @throws Exception - */ - public void testSearchCompound() - throws Exception - { - createTestIndex(); - - RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE ); - RepositoryIndexSearchLayer repoSearchLayer = - (RepositoryIndexSearchLayer) lookup( RepositoryIndexSearchLayer.ROLE ); - - PomRepositoryIndex indexer = factory.createPomRepositoryIndex( indexPath, repository ); - - // Criteria 1: required query - // ex. artifactId=maven-artifact AND groupId=org.apache.maven - Query qry1 = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "maven-artifact" ); - Query qry2 = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "org.apache.maven" ); - CompoundQuery rQry = new CompoundQuery(); - rQry.and( qry1 ); - rQry.and( qry2 ); - - List artifacts = repoSearchLayer.searchAdvanced( rQry, indexer ); - for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) - { - SearchResult result = (SearchResult) iter.next(); - Artifact artifact = result.getArtifact(); - assertEquals( "maven-artifact", artifact.getArtifactId() ); - assertEquals( "org.apache.maven", artifact.getGroupId() ); - } - - // Criteria 2: nested required query - // ex. (artifactId=maven-artifact AND groupId=org.apache.maven) OR - // version=2.0.3 - Query qry3 = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "2.0.3" ); - CompoundQuery oQry = new CompoundQuery(); - oQry.and( rQry ); - oQry.or( qry3 ); - - artifacts = repoSearchLayer.searchAdvanced( oQry, indexer ); - for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) - { - SearchResult result = (SearchResult) iter.next(); - Artifact artifact = result.getArtifact(); - assertEquals( "maven-artifact", artifact.getArtifactId() ); - assertEquals( "org.apache.maven", artifact.getGroupId() ); - } - - // Criteria 3: nested required query - // ex. (artifactId=maven-artifact AND groupId=org.apache.maven) AND - // (version=2.0.3 OR version=2.0.1) - // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact) - Query qry4 = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "2.0.1" ); - oQry = new CompoundQuery(); - oQry.or( qry3 ); - oQry.or( qry4 ); - - CompoundQuery oQry5 = new CompoundQuery(); - Query qry9 = - new SinglePhraseQuery( RepositoryIndex.FLD_DEPENDENCIES, "org.codehaus.plexus:plexus-utils:1.0.5" ); - Query qry10 = new SinglePhraseQuery( RepositoryIndex.FLD_DEPENDENCIES, - "org.codehaus.plexus:plexus-container-defualt:1.0-alpha-9" ); - oQry5.or( qry9 ); - oQry5.or( qry10 ); - - CompoundQuery rQry2 = new CompoundQuery(); - rQry2.or( oQry ); - rQry2.and( rQry ); - rQry2.and( oQry5 ); - - artifacts = repoSearchLayer.searchAdvanced( rQry2, indexer ); - for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) - { - SearchResult result = (SearchResult) iter.next(); - Artifact artifact = result.getArtifact(); - assertEquals( "maven-artifact", artifact.getArtifactId() ); - assertEquals( "org.apache.maven", artifact.getGroupId() ); - assertEquals( "2.0.1", artifact.getVersion() ); - } - - // Criteria 4: nested required query - // ex. [(artifactId=maven-artifact AND groupId=org.apache.maven) AND - // (version=2.0.3 OR version=2.0.1) - // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)] - // OR [(artifactId=sample AND groupId=test)] - CompoundQuery rQry3 = new CompoundQuery(); - Query qry5 = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "sample" ); - Query qry6 = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "test" ); - rQry3.and( qry5 ); - rQry3.and( qry6 ); - CompoundQuery oQry2 = new CompoundQuery(); - oQry2.and( rQry2 ); - oQry2.and( rQry3 ); - - artifacts = repoSearchLayer.searchAdvanced( oQry2, indexer ); - for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) - { - SearchResult result = (SearchResult) iter.next(); - Artifact artifact = result.getArtifact(); - assertEquals( "maven-artifact", artifact.getArtifactId() ); - assertEquals( "org.apache.maven", artifact.getGroupId() ); - assertEquals( "2.0.1", artifact.getVersion() ); - } - - // Criteria 4: nested required query - // ex. [(artifactId=maven-artifact AND groupId=org.apache.maven) AND - // (version=2.0.3 OR version=2.0.1) - // AND (name=maven-artifact-2.0.1.jar OR name=maven-artifact)] OR - // [(artifactId=sample AND groupId=test)] OR - // [(artifactId=sample2 AND groupId=test)] - CompoundQuery rQry4 = new CompoundQuery(); - Query qry7 = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "sample2" ); - Query qry8 = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "test" ); - rQry4.and( qry7 ); - rQry4.and( qry8 ); - oQry2.and( rQry4 ); - - artifacts = repoSearchLayer.searchAdvanced( oQry2, indexer ); - for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) - { - SearchResult result = (SearchResult) iter.next(); - Artifact artifact = result.getArtifact(); - assertEquals( "maven-artifact", artifact.getArtifactId() ); - assertEquals( "org.apache.maven", artifact.getGroupId() ); - } - } - - /** - * Create an index that will be used for testing. - * Indexing process: check if the object was already indexed [ checkIfIndexed(Object) ], open the index [ open() ], - * index the object [ index(Object) ], optimize the index [ optimize() ] and close the index [ close() ]. - * - * @throws Exception - */ - private void createTestIndex() - throws Exception - { - RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE ); - PomRepositoryIndex indexer = factory.createPomRepositoryIndex( indexPath, repository ); - - Model pom = getPom( "org.apache.maven", "maven-artifact", "2.0.1" ); - indexer.indexPom( pom ); - - pom = getPom( "org.apache.maven", "maven-model", "2.0" ); - indexer.indexPom( pom ); - - pom = getPom( "test", "test-artifactId", "1.0" ); - indexer.indexPom( pom ); - - pom = getPom( "test", "test-artifactId", "1.0" ); - indexer.indexPom( pom ); - - indexer.optimize(); - } - - /** - * Test delete of pom document from index. - * - * @throws Exception - */ - public void testDeletePomDocument() - throws Exception - { - createTestIndex(); - - RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE ); - RepositoryIndexSearcher repoSearcher = (RepositoryIndexSearcher) lookup( RepositoryIndexSearcher.ROLE ); - - PomRepositoryIndex indexer = factory.createPomRepositoryIndex( indexPath, repository ); - - Model pom = getPom( "org.apache.maven", "maven-artifact", "2.0.1" ); - indexer.deleteDocument( RepositoryIndex.FLD_ID, RepositoryIndex.POM + pom.getId() ); - - Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_ID, RepositoryIndex.POM + pom.getId() ); - List artifactList = repoSearcher.search( qry, indexer ); - assertEquals( 0, artifactList.size() ); - } - - private Model getPom( String groupId, String artifactId, String version ) - throws Exception - { - Artifact artifact = getArtifact( groupId, artifactId, version ); - - return getPom( artifact ); - } - - private Model getPom( Artifact artifact ) - throws Exception - { - File pomFile = getPomFile( artifact ); - - MavenXpp3Reader pomReader = new MavenXpp3Reader(); - return pomReader.read( new FileReader( pomFile ) ); - } - - private File getPomFile( Artifact artifact ) - { - String path = new File( repository.getBasedir(), repository.pathOf( artifact ) ).getAbsolutePath(); - return new File( path.substring( 0, path.lastIndexOf( '.' ) ) + ".pom" ); - } - - private Artifact getArtifact( String groupId, String artifactId, String version ) - throws Exception - { - if ( artifactFactory == null ) - { - artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE ); - } - - return artifactFactory.createBuildArtifact( groupId, artifactId, version, "pom" ); - } - - protected void tearDown() - throws Exception - { - repository = null; - - super.tearDown(); - } -} diff --git a/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/RepositoryIndexSearchLayerTest.java b/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/RepositoryIndexSearchLayerTest.java index 035239bdb..770bc45cb 100644 --- a/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/RepositoryIndexSearchLayerTest.java +++ b/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/RepositoryIndexSearchLayerTest.java @@ -121,22 +121,6 @@ public class RepositoryIndexSearchLayerTest metaIndexer.indexMetadata( repoMetadata ); metaIndexer.optimize(); - - PomRepositoryIndex pomIndexer = factory.createPomRepositoryIndex( indexPath, repository ); - - Model pom = getPom( "org.apache.maven", "maven-artifact", "2.0.1" ); - pomIndexer.indexPom( pom ); - - pom = getPom( "org.apache.maven", "maven-model", "2.0" ); - pomIndexer.indexPom( pom ); - - pom = getPom( "test", "test-artifactId", "1.0" ); - pomIndexer.indexPom( pom ); - - pom = getPom( "test", "test-artifactId", "1.0" ); - pomIndexer.indexPom( pom ); - - pomIndexer.optimize(); } /** diff --git a/maven-repository-webapp/src/main/webapp/WEB-INF/jsp/admin/index.jsp b/maven-repository-webapp/src/main/webapp/WEB-INF/jsp/admin/index.jsp index 3501bd352..e5a6ff118 100644 --- a/maven-repository-webapp/src/main/webapp/WEB-INF/jsp/admin/index.jsp +++ b/maven-repository-webapp/src/main/webapp/WEB-INF/jsp/admin/index.jsp @@ -31,12 +31,18 @@ - + - + + <%-- TODO: a "run now without timestamp checking" operation should be here too, to pick up any stragglers (in the event of a bug) --%> + <%-- TODO: a "delete index and run now" operation should be here too (really clean, remove deletions that didn't get picked up) --%> -- 2.39.5
Repository Directory + +
Indexing Schedule + + ">Run Now