From 12e8c4d60dff2833bd8da40028dd953d5511b693 Mon Sep 17 00:00:00 2001 From: "Edwin L. Punzalan" Date: Mon, 16 Jan 2006 08:53:09 +0000 Subject: [PATCH] PR: MEV-35 Submitted by: Maria Odea Ching Applied patch for indexing metadata and for searching the metadata index git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@369402 13f79535-47bb-0310-9956-ffa450edef68 --- maven-repository-indexer/pom.xml | 5 +- .../AbstractRepositoryIndexSearcher.java | 16 ++ .../DefaultRepositoryIndexingFactory.java | 10 + .../indexing/MetadataRepositoryIndex.java | 180 +++++++++++++ .../MetadataRepositoryIndexSearcher.java | 174 ++++++++++++ .../indexing/RepositoryIndexingFactory.java | 6 + .../repository/indexing/query/RangeQuery.java | 52 ++++ .../MetadataRepositoryIndexingTest.java | 249 ++++++++++++++++++ .../maven-artifact/2.0.1/maven-metadata.xml | 5 + .../maven/maven-artifact/maven-metadata.xml | 12 + .../org/apache/maven/maven-metadata.xml | 9 + 11 files changed, 717 insertions(+), 1 deletion(-) create mode 100644 maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/MetadataRepositoryIndex.java create mode 100644 maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/MetadataRepositoryIndexSearcher.java create mode 100644 maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/query/RangeQuery.java create mode 100644 maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/MetadataRepositoryIndexingTest.java create mode 100644 maven-repository-indexer/src/test/repository/org/apache/maven/maven-artifact/2.0.1/maven-metadata.xml create mode 100644 maven-repository-indexer/src/test/repository/org/apache/maven/maven-artifact/maven-metadata.xml create mode 100644 maven-repository-indexer/src/test/repository/org/apache/maven/maven-metadata.xml diff --git a/maven-repository-indexer/pom.xml b/maven-repository-indexer/pom.xml index a7afee6b0..4953d3243 100644 --- a/maven-repository-indexer/pom.xml +++ b/maven-repository-indexer/pom.xml @@ -32,7 +32,6 @@ org.apache.maven maven-artifact-manager - test org.apache.maven @@ -55,5 +54,9 @@ org.apache.maven.repository maven-repository-utils + + org.apache.maven + maven-repository-metadata + diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/AbstractRepositoryIndexSearcher.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/AbstractRepositoryIndexSearcher.java index f593b8c74..c67d7ef3d 100644 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/AbstractRepositoryIndexSearcher.java +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/AbstractRepositoryIndexSearcher.java @@ -28,6 +28,7 @@ import org.apache.maven.repository.indexing.query.CompoundQuery; import org.apache.maven.repository.indexing.query.CompoundQueryTerm; import org.apache.maven.repository.indexing.query.Query; import org.apache.maven.repository.indexing.query.SinglePhraseQuery; +import org.apache.maven.repository.indexing.query.RangeQuery; import org.codehaus.plexus.logging.AbstractLogEnabled; import java.io.IOException; @@ -158,6 +159,21 @@ public abstract class AbstractRepositoryIndexSearcher } retVal = booleanQuery; } + else if( query instanceof RangeQuery ) + { + RangeQuery rq = (RangeQuery) query; + List queries = rq.getQueries(); + Iterator iter = queries.iterator(); + Term begin = null, end = null; + if(queries.size() == 2) + { + SinglePhraseQuery qry = (SinglePhraseQuery) iter.next(); + begin = new Term( qry.getField(), qry.getValue() ); + qry = ( SinglePhraseQuery ) iter.next(); + end = new Term( qry.getField(), qry.getValue() ); + } + retVal = new org.apache.lucene.search.RangeQuery( begin, end, rq.isInclusive() ); + } else { SinglePhraseQuery singlePhraseQuery = (SinglePhraseQuery) query; 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 d999dc8b0..47298d3d6 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 @@ -71,4 +71,14 @@ public class DefaultRepositoryIndexingFactory { return new PomRepositoryIndexSearcher( index, artifactFactory ); } + + public MetadataRepositoryIndex createMetadataRepositoryIndex( String indexPath, ArtifactRepository repository) + throws RepositoryIndexException{ + return new MetadataRepositoryIndex(indexPath, repository); + } + + public MetadataRepositoryIndexSearcher createMetadataRepositoryIndexSearcher( MetadataRepositoryIndex index ) + { + return new MetadataRepositoryIndexSearcher( index, artifactFactory ); + } } diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/MetadataRepositoryIndex.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/MetadataRepositoryIndex.java new file mode 100644 index 000000000..5919acfc9 --- /dev/null +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/MetadataRepositoryIndex.java @@ -0,0 +1,180 @@ +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.analysis.Analyzer; +import org.apache.lucene.analysis.standard.StandardAnalyzer; +import org.apache.lucene.document.Document; +import org.apache.lucene.document.Field; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.repository.metadata.RepositoryMetadata; +import org.apache.maven.artifact.repository.metadata.Metadata; +import org.apache.maven.artifact.repository.metadata.Versioning; +import org.apache.maven.artifact.repository.metadata.Plugin; + +import java.util.List; +import java.util.Iterator; +import java.io.IOException; + +/** + * This class indexes the metadata in the repository. + */ +public class MetadataRepositoryIndex + extends AbstractRepositoryIndex +{ + private static final String FLD_LASTUPDATE = "lastUpdate"; + + private static final String FLD_PLUGINPREFIX = "pluginPrefix"; + + private static final String FLD_METADATAPATH = "path"; + + private static final String FLD_GROUPID = "groupId"; + + private static final String FLD_ARTIFACTID = "artifactId"; + + private static final String FLD_VERSION = "version"; + + private static final String[] FIELDS = {FLD_METADATAPATH, FLD_PLUGINPREFIX, FLD_LASTUPDATE, + FLD_GROUPID, FLD_ARTIFACTID, FLD_VERSION}; + + /** + * Constructor + * @param indexPath the path to the index + * @param repository the repository where the metadata to be indexed is located + * @throws RepositoryIndexException + */ + public MetadataRepositoryIndex( String indexPath, ArtifactRepository repository ) + throws RepositoryIndexException + { + super( indexPath, repository, FIELDS ); + } + + /** + * Get the field names to be used in the index + * @return array of strings + */ + public String[] getIndexFields() + { + return FIELDS; + } + + /** + * Returns the analyzer used for indexing + * @return Analyzer object + */ + public Analyzer getAnalyzer() + { + return new StandardAnalyzer(); + } + + /** + * Index the paramater object + * @param obj + * @throws RepositoryIndexException + */ + public void index( Object obj ) throws RepositoryIndexException + { + if ( obj instanceof RepositoryMetadata ) + { + indexMetadata( (RepositoryMetadata) obj ); + } + else + { + throw new RepositoryIndexException( + "This instance of indexer cannot index instances of " + obj.getClass().getName() ); + } + } + + /** + * Index the contents of the specified RepositoryMetadata paramter object + * @param repoMetadata the metadata object to be indexed + * @throws RepositoryIndexException + */ + private void indexMetadata( RepositoryMetadata repoMetadata ) throws RepositoryIndexException + { + if ( !isOpen() ) + { + throw new RepositoryIndexException( "Unable to add artifact index on a closed index" ); + } + + //get lastUpdated from Versioning (specified in Metadata object) + //get pluginPrefixes from Plugin (spcified in Metadata object) -----> concatenate/append??? + //get the metadatapath: check where metadata is located, then concatenate the groupId, + // artifactId, version based on its location + Document doc = new Document(); + String path = ""; + + if( repoMetadata.storedInGroupDirectory() && !repoMetadata.storedInArtifactVersionDirectory()) + { + path = repoMetadata.getGroupId() + "/"; + } + else if(!repoMetadata.storedInGroupDirectory() && !repoMetadata.storedInArtifactVersionDirectory()) + { + path = repoMetadata.getGroupId() + "/" + repoMetadata.getArtifactId() + "/"; + } + else if(!repoMetadata.storedInGroupDirectory() && repoMetadata.storedInArtifactVersionDirectory()) + { + path = repoMetadata.getGroupId() + "/" + repoMetadata.getArtifactId() + "/" + repoMetadata.getBaseVersion() + "/"; + } + + //@todo use localfilename or remotefilename to get the path??? + path = path + repoMetadata.getRemoteFilename(); + doc.add( Field.Text( FLD_METADATAPATH, path) ); + + Metadata metadata = repoMetadata.getMetadata(); + Versioning versioning = metadata.getVersioning(); + if( versioning != null ) + { + doc.add( Field.Text( FLD_LASTUPDATE, versioning.getLastUpdated() ) ); + } + + List plugins = metadata.getPlugins(); + String pluginAppended = ""; + for( Iterator iter = plugins.iterator(); iter.hasNext(); ) + { + Plugin plugin = (Plugin) iter.next(); + if( plugin.getPrefix() != null && !plugin.getPrefix().equals("") ) + { + pluginAppended = plugin.getPrefix() + " "; + } + } + doc.add( Field.Text( FLD_PLUGINPREFIX, pluginAppended ) ); + doc.add( Field.UnIndexed( FLD_GROUPID, metadata.getGroupId() ) ); + + if( metadata.getArtifactId() != null && !metadata.getArtifactId().equals("") ) + { + doc.add( Field.UnIndexed( FLD_ARTIFACTID, metadata.getArtifactId() ) ); + } + if( metadata.getVersion() != null && !metadata.getVersion().equals("") ) + { + doc.add( Field.UnIndexed( FLD_VERSION, metadata.getVersion() ) ); + } + + try + { + getIndexWriter().addDocument( doc ); + } + catch ( IOException e ) + { + throw new RepositoryIndexException( "Error opening index", e ); + } + } + + public boolean isKeywordField( String field ){ + return false; + } +} diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/MetadataRepositoryIndexSearcher.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/MetadataRepositoryIndexSearcher.java new file mode 100644 index 000000000..1f8dceb12 --- /dev/null +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/MetadataRepositoryIndexSearcher.java @@ -0,0 +1,174 @@ +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.maven.artifact.factory.ArtifactFactory; +import org.apache.maven.artifact.repository.metadata.RepositoryMetadata; +import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata; +import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata; +import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata; +import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader; +import org.apache.maven.artifact.Artifact; + +import java.net.URL; +import java.io.InputStream; +import java.io.File; +import java.io.InputStreamReader; +import java.util.*; + +/** + * This class searches the specified index given the search/query criteria. + * + */ +public class MetadataRepositoryIndexSearcher + extends AbstractRepositoryIndexSearcher +{ + private ArtifactFactory artifactFactory; + + private static final String FLD_METADATAPATH = "path"; + + private static final String FLD_GROUPID = "groupId"; + + private static final String FLD_ARTIFACTID = "artifactId"; + + private static final String FLD_VERSION = "version"; + + private static final String GROUP_TYPE = "GROUP"; + + private static final String ARTIFACT_TYPE = "ARTIFACT"; + + private static final String SNAPSHOT_TYPE = "SNAPSHOT"; + + /** + * Constructor + * @param index the index object to be set + * @param factory + */ + public MetadataRepositoryIndexSearcher( MetadataRepositoryIndex index, ArtifactFactory factory) + { + super(index); + artifactFactory = factory; + } + + /** + * Create object to be returned by the search based on the document + * @param doc + * @return Object + */ + protected Object createSearchedObjectFromIndexDocument( Document doc ) + { + List pathParts = new ArrayList(); + StringTokenizer st = new StringTokenizer( doc.get( FLD_METADATAPATH ), "/\\" ); + while ( st.hasMoreTokens() ) + { + pathParts.add( st.nextToken() ); + } + + Collections.reverse( pathParts ); + Iterator it = pathParts.iterator(); + String metadataFile = (String) it.next(); + String tmpDir = (String) it.next(); + + String metadataType = ""; + if( tmpDir.equals( doc.get( FLD_GROUPID ) ) ) + { + metadataType = GROUP_TYPE; + } + else if( tmpDir.equals( doc.get( FLD_ARTIFACTID ) ) ) + { + metadataType = ARTIFACT_TYPE; + } + else + { + metadataType = SNAPSHOT_TYPE; + } + + RepositoryMetadata repoMetadata = null; + + try{ + repoMetadata = getMetadata(doc.get( FLD_GROUPID ), doc.get( FLD_ARTIFACTID ), doc.get( FLD_VERSION ), metadataFile, metadataType ); + } + catch(Exception e) + { + //@todo + } + + return repoMetadata; + } + + /** + * Create RepositoryMetadata object. + * + * @param groupId the groupId to be set + * @param artifactId the artifactId to be set + * @param version the version to be set + * @param filename the name of the metadata file + * @param metadataType the type of RepositoryMetadata object to be created (GROUP, ARTIFACT or SNAPSHOT) + * @return RepositoryMetadata + * @throws Exception + */ + private RepositoryMetadata getMetadata( String groupId, String artifactId, String version, String filename, String metadataType) + throws Exception + { + RepositoryMetadata repoMetadata = null; + URL url; + InputStream is = null; + MetadataXpp3Reader metadataReader = new MetadataXpp3Reader(); + + //group metadata + if( metadataType.equals( GROUP_TYPE ) ) + { + url = new File( index.getRepository().getBasedir() + groupId.replace('.', '/') + "/" + filename ).toURL(); + is = url.openStream(); + repoMetadata = new GroupRepositoryMetadata(groupId); + repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) ); + } + //artifact metadata + else if( metadataType.equals( ARTIFACT_TYPE ) ) + { + url = new File( index.getRepository().getBasedir() + groupId.replace('.', '/') + "/" + artifactId + "/" + filename ).toURL(); + is = url.openStream(); + repoMetadata = new ArtifactRepositoryMetadata( getArtifact( groupId, artifactId, version ) ); + repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) ); + } + //snapshot/version metadata + else if( metadataType.equals( SNAPSHOT_TYPE ) ) + { + url = new File( index.getRepository().getBasedir() + groupId.replace('.', '/') + "/" + artifactId + "/" + version + "/" + filename ).toURL(); + is = url.openStream(); + repoMetadata = new SnapshotArtifactRepositoryMetadata( getArtifact( groupId, artifactId, version ) ); + repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) ); + } + + return repoMetadata; + } + + /** + * Create artifact object. + * @param groupId the groupId of the artifact + * @param artifactId the artifactId of the artifact + * @param version the version of the artifact + * @return Artifact + * @throws Exception + */ + private Artifact getArtifact( String groupId, String artifactId, String version ) + throws Exception + { + return artifactFactory.createBuildArtifact( groupId, artifactId, version, "jar" ); + } +} 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 53126c4c2..f4c7f5f1a 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 @@ -64,4 +64,10 @@ public interface RepositoryIndexingFactory * @return the PomRepositoryIndexSearcher instance */ PomRepositoryIndexSearcher createPomRepositoryIndexSearcher( PomRepositoryIndex index ); + + MetadataRepositoryIndex createMetadataRepositoryIndex( String indexPath, ArtifactRepository repository) + throws RepositoryIndexException; + + MetadataRepositoryIndexSearcher createMetadataRepositoryIndexSearcher( MetadataRepositoryIndex index ); + } diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/query/RangeQuery.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/query/RangeQuery.java new file mode 100644 index 000000000..db0b3887b --- /dev/null +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/query/RangeQuery.java @@ -0,0 +1,52 @@ +package org.apache.maven.repository.indexing.query; + +/* + * 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 java.util.List; +import java.util.ArrayList; + +/** + * Query object that handles range queries for dates. + * @author Maria Odea Ching + */ +public class RangeQuery + implements Query +{ + List queries = new ArrayList(); + + private boolean inclusive; + + public RangeQuery( boolean inclusive) + { + this.inclusive = inclusive; + } + + public void addQuery( Query qry ) + { + queries.add( qry ); + } + + public List getQueries() + { + return queries; + } + + public boolean isInclusive() + { + return inclusive; + } +} diff --git a/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/MetadataRepositoryIndexingTest.java b/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/MetadataRepositoryIndexingTest.java new file mode 100644 index 000000000..e2ad7aa16 --- /dev/null +++ b/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/MetadataRepositoryIndexingTest.java @@ -0,0 +1,249 @@ +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.artifact.repository.metadata.*; +import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader; +import org.apache.maven.repository.indexing.query.Query; +import org.apache.maven.repository.indexing.query.RangeQuery; +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.InputStream; +import java.io.InputStreamReader; +import java.net.URL; +import java.util.Iterator; +import java.util.List; + +/** + * This class tests the MetadataRepositoryIndex. + */ +public class MetadataRepositoryIndexingTest + extends PlexusTestCase +{ + private ArtifactRepository repository; + + private String indexPath; + + private static final String FLD_LASTUPDATE = "lastUpdate"; + + private static final String FLD_PLUGINPREFIX = "pluginPrefix"; + + private static final String GROUP_TYPE = "GROUP"; + + private static final String ARTIFACT_TYPE = "ARTIFACT"; + + private static final String SNAPSHOT_TYPE = "SNAPSHOT"; + + private MetadataRepositoryIndex indexer; + + private ArtifactFactory artifactFactory; + + /** + * Set up. + * @throws Exception + */ + public 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 ); + + indexPath = "target/index/metadata"; + FileUtils.deleteDirectory( indexPath ); + } + + /** + * Tear down. + * @throws Exception + */ + public void tearDown() throws Exception + { + repository = null; + super.tearDown(); + } + + /** + * Create the test index. + * @throws Exception + */ + private void createTestIndex() throws Exception + { + RepositoryIndexingFactory factory = ( RepositoryIndexingFactory ) lookup( RepositoryIndexingFactory.ROLE ); + indexer = factory.createMetadataRepositoryIndex( indexPath, repository ); + + RepositoryMetadata repoMetadata = getMetadata( "org.apache.maven", null, null, "maven-metadata.xml", GROUP_TYPE ); + indexer.index( repoMetadata ); + + repoMetadata = getMetadata( "org.apache.maven", "maven-artifact", "2.0.1", "maven-metadata.xml", ARTIFACT_TYPE ); + indexer.index( repoMetadata ); + + repoMetadata = getMetadata( "org.apache.maven", "maven-artifact", "2.0.1", "maven-metadata.xml", SNAPSHOT_TYPE ); + indexer.index( repoMetadata ); + + indexer.optimize(); + indexer.close(); + } + + /** + * Test the ArtifactRepositoryIndexSearcher using a single-phrase search. + * + * @throws Exception + */ + public void testSearchSingle() + throws Exception + { + createTestIndex(); + + RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE ); + MetadataRepositoryIndex indexer = factory.createMetadataRepositoryIndex( indexPath, repository ); + RepositoryIndexSearcher repoSearcher = factory.createMetadataRepositoryIndexSearcher( indexer ); + + // search last update + org.apache.maven.repository.indexing.query.Query qry = new SinglePhraseQuery( FLD_LASTUPDATE, "20051212044643" ); + List metadataList = repoSearcher.search( qry ); + assertEquals( 1, metadataList.size() ); + for ( Iterator iter = metadataList.iterator(); iter.hasNext(); ) + { + RepositoryMetadata repoMetadata = (RepositoryMetadata) iter.next(); + + Metadata metadata = repoMetadata.getMetadata(); + Versioning versioning = metadata.getVersioning(); + assertEquals( "20051212044643", versioning.getLastUpdated() ); + } + + // search plugin prefix + qry = new SinglePhraseQuery( FLD_PLUGINPREFIX, "org.apache.maven" ); + metadataList = repoSearcher.search( qry ); + assertEquals( 1, metadataList.size() ); + for ( Iterator iter = metadataList.iterator(); iter.hasNext(); ) + { + RepositoryMetadata repoMetadata = (RepositoryMetadata) iter.next(); + Metadata metadata = repoMetadata.getMetadata(); + List plugins = metadata.getPlugins(); + for( Iterator it = plugins.iterator(); it.hasNext(); ) + { + Plugin plugin = (Plugin) it.next(); + assertEquals( "org.apache.maven", plugin.getPrefix() ); + } + } + + // search last update using INCLUSIVE Range Query + Query qry1 = new SinglePhraseQuery( FLD_LASTUPDATE, "20051212000000" ); + Query qry2 = new SinglePhraseQuery( FLD_LASTUPDATE, "20051212235959"); + RangeQuery rQry = new RangeQuery( true ); + rQry.addQuery( qry1 ); + rQry.addQuery( qry2 ); + + metadataList = repoSearcher.search( rQry ); + for ( Iterator iter = metadataList.iterator(); iter.hasNext(); ) + { + RepositoryMetadata repoMetadata = (RepositoryMetadata) iter.next(); + Metadata metadata = repoMetadata.getMetadata(); + Versioning versioning = metadata.getVersioning(); + assertEquals( "20051212044643", versioning.getLastUpdated() ); + } + + // search last update using EXCLUSIVE Range Query + qry1 = new SinglePhraseQuery( FLD_LASTUPDATE, "20051212000000" ); + qry2 = new SinglePhraseQuery( FLD_LASTUPDATE, "20051212044643"); + rQry = new RangeQuery( false ); + rQry.addQuery( qry1 ); + rQry.addQuery( qry2 ); + + metadataList = repoSearcher.search( rQry ); + assertEquals( metadataList.size(), 0 ); + + indexer.close(); + } + + /** + * Create RepositoryMetadata object. + * + * @param groupId the groupId to be set + * @param artifactId the artifactId to be set + * @param version the version to be set + * @param filename the name of the metadata file + * @param metadataType the type of RepositoryMetadata object to be created (GROUP, ARTIFACT or SNAPSHOT) + * @return RepositoryMetadata + * @throws Exception + */ + private RepositoryMetadata getMetadata( String groupId, String artifactId, String version, String filename, String metadataType) + throws Exception + { + RepositoryMetadata repoMetadata = null; + URL url; + InputStream is = null; + MetadataXpp3Reader metadataReader = new MetadataXpp3Reader(); + + //group metadata + if( metadataType.equals( GROUP_TYPE ) ) + { + url = new File( repository.getBasedir() + groupId.replace('.', '/') + "/" + filename ).toURL(); + is = url.openStream(); + repoMetadata = new GroupRepositoryMetadata(groupId); + repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) ); + } + //artifact metadata + else if( metadataType.equals( ARTIFACT_TYPE ) ) + { + url = new File( repository.getBasedir() + groupId.replace('.', '/') + "/" + artifactId + "/" + filename ).toURL(); + is = url.openStream(); + repoMetadata = new ArtifactRepositoryMetadata( getArtifact( groupId, artifactId, version ) ); + repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) ); + } + //snapshot/version metadata + else if( metadataType.equals( SNAPSHOT_TYPE ) ) + { + url = new File( repository.getBasedir() + groupId.replace('.', '/') + "/" + artifactId + "/" + version + "/" + filename ).toURL(); + is = url.openStream(); + repoMetadata = new SnapshotArtifactRepositoryMetadata( getArtifact( groupId, artifactId, version ) ); + repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) ); + } + + return repoMetadata; + } + + + /** + * Create artifact object. + * @param groupId the groupId of the artifact + * @param artifactId the artifactId of the artifact + * @param version the version of the artifact + * @return Artifact + * @throws Exception + */ + 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, "jar" ); + } +} diff --git a/maven-repository-indexer/src/test/repository/org/apache/maven/maven-artifact/2.0.1/maven-metadata.xml b/maven-repository-indexer/src/test/repository/org/apache/maven/maven-artifact/2.0.1/maven-metadata.xml new file mode 100644 index 000000000..739da6b59 --- /dev/null +++ b/maven-repository-indexer/src/test/repository/org/apache/maven/maven-artifact/2.0.1/maven-metadata.xml @@ -0,0 +1,5 @@ + +org.apache.maven +maven-artifact +2.0.1 + diff --git a/maven-repository-indexer/src/test/repository/org/apache/maven/maven-artifact/maven-metadata.xml b/maven-repository-indexer/src/test/repository/org/apache/maven/maven-artifact/maven-metadata.xml new file mode 100644 index 000000000..fd40c7b01 --- /dev/null +++ b/maven-repository-indexer/src/test/repository/org/apache/maven/maven-artifact/maven-metadata.xml @@ -0,0 +1,12 @@ + +org.apache.maven +maven-artifact +2.0.1 + +2.0.1 + +2.0.1 + +20051212044643 + + diff --git a/maven-repository-indexer/src/test/repository/org/apache/maven/maven-metadata.xml b/maven-repository-indexer/src/test/repository/org/apache/maven/maven-metadata.xml new file mode 100644 index 000000000..11612dc7c --- /dev/null +++ b/maven-repository-indexer/src/test/repository/org/apache/maven/maven-metadata.xml @@ -0,0 +1,9 @@ + +org.apache.maven + + + org.apache.maven + org.apache.maven-maven-plugin + + + -- 2.39.5