From: Edwin L. Punzalan Date: Wed, 22 Feb 2006 02:39:04 +0000 (+0000) Subject: PR: MRM-57, MRM-58 X-Git-Tag: archiva-0.9-alpha-1~928 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=edd8a998aa6006fddaf4d4a404bbdd6ddfd246d7;p=archiva.git PR: MRM-57, MRM-58 Submitted by: Maria Odea Ching patch fix for general search git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@379654 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexSearcher.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexSearcher.java index 4ca2ca4a6..e1a119382 100644 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexSearcher.java +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/DefaultRepositoryIndexSearcher.java @@ -24,35 +24,38 @@ import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.Hits; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.TermQuery; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.factory.ArtifactFactory; +import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata; +import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata; +import org.apache.maven.artifact.repository.metadata.RepositoryMetadata; +import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata; +import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader; +import org.apache.maven.model.io.xpp3.MavenXpp3Reader; 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.RangeQuery; import org.apache.maven.repository.indexing.query.SinglePhraseQuery; -import org.apache.maven.artifact.Artifact; -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.factory.ArtifactFactory; import org.codehaus.plexus.logging.AbstractLogEnabled; import org.codehaus.plexus.util.xml.pull.XmlPullParserException; -import java.io.IOException; import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.net.MalformedURLException; import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.StringTokenizer; -import java.util.Collections; -import java.net.MalformedURLException; -import java.net.URL; /** - * Abstract Class to hold common codes for the different RepositoryIndexSearcher + * Implementation Class for searching through the index */ public class DefaultRepositoryIndexSearcher extends AbstractLogEnabled @@ -62,6 +65,8 @@ public class DefaultRepositoryIndexSearcher private ArtifactFactory factory; + private List artifactList; + /** * Constructor * @@ -79,7 +84,7 @@ public class DefaultRepositoryIndexSearcher public List search( Query query ) throws RepositoryIndexSearchException { - + artifactList = new ArrayList(); org.apache.lucene.search.Query luceneQuery; try { @@ -215,12 +220,9 @@ public class DefaultRepositoryIndexSearcher private List buildList( Hits hits ) throws MalformedURLException, IOException, XmlPullParserException { - List artifactList = new ArrayList(); - for ( int i = 0; i < hits.length(); i++ ) { Document doc = hits.doc( i ); - artifactList.add( createSearchedObjectFromIndexDocument( doc ) ); } @@ -233,38 +235,55 @@ public class DefaultRepositoryIndexSearcher * @param doc the index document where the object field values will be retrieved from * @return Object */ - protected Object createSearchedObjectFromIndexDocument( Document doc ) + protected RepositoryIndexSearchHit createSearchedObjectFromIndexDocument( Document doc ) throws MalformedURLException, IOException, XmlPullParserException { String groupId, artifactId, version, name, packaging; + RepositoryIndexSearchHit searchHit = null; - if ( doc.get( index.FLD_DOCTYPE ).equals( index.ARTIFACT ) ) + // the document is of type artifact + if ( doc.get( RepositoryIndex.FLD_DOCTYPE ).equals( RepositoryIndex.ARTIFACT ) ) { - groupId = doc.get( ArtifactRepositoryIndex.FLD_GROUPID ); - artifactId = doc.get( ArtifactRepositoryIndex.FLD_ARTIFACTID ); - version = doc.get( ArtifactRepositoryIndex.FLD_VERSION ); - name = doc.get( ArtifactRepositoryIndex.FLD_NAME ); - packaging = name.substring( name.lastIndexOf( '.' ) + 1 ); + groupId = doc.get( RepositoryIndex.FLD_GROUPID ); + artifactId = doc.get( RepositoryIndex.FLD_ARTIFACTID ); + version = doc.get( RepositoryIndex.FLD_VERSION ); + name = doc.get( RepositoryIndex.FLD_NAME ); + packaging = doc.get( RepositoryIndex.FLD_PACKAGING ); Artifact artifact = factory.createBuildArtifact( groupId, artifactId, version, packaging ); - String groupIdTemp = groupId.replace( '.', '/' ); - artifact.setFile( new File( - index.getRepository().getBasedir() + groupIdTemp + "/" + artifactId + "/" + version + "/" + name ) ); - return artifact; + artifact.setFile( + new File( index.getRepository().getBasedir(), index.getRepository().pathOf( artifact ) ) ); + + Map map = new HashMap(); + map.put( RepositoryIndex.ARTIFACT, artifact ); + map.put( RepositoryIndex.FLD_CLASSES, doc.get( RepositoryIndex.FLD_CLASSES ) ); + map.put( RepositoryIndex.FLD_PACKAGES, doc.get( RepositoryIndex.FLD_PACKAGES ) ); + map.put( RepositoryIndex.FLD_FILES, doc.get( RepositoryIndex.FLD_FILES ) ); + map.put( RepositoryIndex.FLD_MD5, doc.get( RepositoryIndex.FLD_MD5 ) ); + map.put( RepositoryIndex.FLD_SHA1, doc.get( RepositoryIndex.FLD_SHA1 ) ); + map.put( RepositoryIndex.FLD_PACKAGING, doc.get( RepositoryIndex.FLD_PACKAGING ) ); + + searchHit = new RepositoryIndexSearchHit( true, false, false ); + searchHit.setObject( map ); } - else if ( doc.get( index.FLD_DOCTYPE ).equals( index.POM ) ) + // the document is of type model + else if ( doc.get( RepositoryIndex.FLD_DOCTYPE ).equals( RepositoryIndex.POM ) ) { - groupId = doc.get( PomRepositoryIndex.FLD_GROUPID ); - artifactId = doc.get( PomRepositoryIndex.FLD_ARTIFACTID ); - version = doc.get( PomRepositoryIndex.FLD_VERSION ); - packaging = doc.get( PomRepositoryIndex.FLD_PACKAGING ); + InputStream is = new FileInputStream( new File( index.getRepository().getBasedir() + + doc.get( RepositoryIndex.FLD_GROUPID ).replace( '.', '/' ) + "/" + + doc.get( RepositoryIndex.FLD_ARTIFACTID ) + "/" + doc.get( RepositoryIndex.FLD_VERSION ) + "/" + + doc.get( RepositoryIndex.FLD_ARTIFACTID ) + "-" + doc.get( RepositoryIndex.FLD_VERSION ) + ".pom" ) ); + MavenXpp3Reader reader = new MavenXpp3Reader(); + + searchHit = new RepositoryIndexSearchHit( false, false, true ); + searchHit.setObject( reader.read( new InputStreamReader( is ) ) ); - return factory.createBuildArtifact( groupId, artifactId, version, packaging ); } - else if ( doc.get( index.FLD_DOCTYPE ).equals( index.METADATA ) ) + // the document is of type metadata + else if ( doc.get( RepositoryIndex.FLD_DOCTYPE ).equals( RepositoryIndex.METADATA ) ) { List pathParts = new ArrayList(); - StringTokenizer st = new StringTokenizer( doc.get( MetadataRepositoryIndex.FLD_NAME ), "/\\" ); + StringTokenizer st = new StringTokenizer( doc.get( RepositoryIndex.FLD_NAME ), "/\\" ); while ( st.hasMoreTokens() ) { pathParts.add( st.nextToken() ); @@ -276,28 +295,28 @@ public class DefaultRepositoryIndexSearcher String tmpDir = (String) it.next(); String metadataType = ""; - if ( tmpDir.equals( doc.get( MetadataRepositoryIndex.FLD_GROUPID ) ) ) + if ( tmpDir.equals( doc.get( RepositoryIndex.FLD_VERSION ) ) ) { - metadataType = MetadataRepositoryIndex.GROUP_METADATA; + metadataType = MetadataRepositoryIndex.SNAPSHOT_METADATA; } - else if ( tmpDir.equals( doc.get( MetadataRepositoryIndex.FLD_ARTIFACTID ) ) ) + else if ( tmpDir.equals( doc.get( RepositoryIndex.FLD_ARTIFACTID ) ) ) { metadataType = MetadataRepositoryIndex.ARTIFACT_METADATA; } else { - metadataType = MetadataRepositoryIndex.SNAPSHOT_METADATA; + metadataType = MetadataRepositoryIndex.GROUP_METADATA; } - RepositoryMetadata repoMetadata = null; - repoMetadata = getMetadata( doc.get( MetadataRepositoryIndex.FLD_GROUPID ), - doc.get( MetadataRepositoryIndex.FLD_ARTIFACTID ), - doc.get( MetadataRepositoryIndex.FLD_VERSION ), metadataFile, metadataType ); - - return repoMetadata; + RepositoryMetadata repoMetadata = getMetadata( doc.get( RepositoryIndex.FLD_GROUPID ), + doc.get( RepositoryIndex.FLD_ARTIFACTID ), + doc.get( RepositoryIndex.FLD_VERSION ), metadataFile, + metadataType ); + searchHit = new RepositoryIndexSearchHit( false, true, false ); + searchHit.setObject( repoMetadata ); } - return null; + return searchHit; } /** @@ -318,24 +337,22 @@ public class DefaultRepositoryIndexSearcher throws MalformedURLException, IOException, XmlPullParserException { RepositoryMetadata repoMetadata = null; - URL url; InputStream is = null; MetadataXpp3Reader metadataReader = new MetadataXpp3Reader(); //group metadata if ( metadataType.equals( MetadataRepositoryIndex.GROUP_METADATA ) ) { - url = new File( index.getRepository().getBasedir() + groupId.replace( '.', '/' ) + "/" + filename ).toURL(); - is = url.openStream(); + is = new FileInputStream( + new File( index.getRepository().getBasedir() + groupId.replace( '.', '/' ) + "/" + filename ) ); repoMetadata = new GroupRepositoryMetadata( groupId ); repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) ); } //artifact metadata else if ( metadataType.equals( MetadataRepositoryIndex.ARTIFACT_METADATA ) ) { - url = new File( index.getRepository().getBasedir() + groupId.replace( '.', '/' ) + "/" + artifactId + "/" + - filename ).toURL(); - is = url.openStream(); + is = new FileInputStream( new File( index.getRepository().getBasedir() + groupId.replace( '.', '/' ) + "/" + + artifactId + "/" + filename ) ); repoMetadata = new ArtifactRepositoryMetadata( factory.createBuildArtifact( groupId, artifactId, version, "jar" ) ); repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) ); @@ -343,9 +360,8 @@ public class DefaultRepositoryIndexSearcher //snapshot/version metadata else if ( metadataType.equals( MetadataRepositoryIndex.SNAPSHOT_METADATA ) ) { - url = new File( index.getRepository().getBasedir() + groupId.replace( '.', '/' ) + "/" + artifactId + "/" + - version + "/" + filename ).toURL(); - is = url.openStream(); + is = new FileInputStream( new File( index.getRepository().getBasedir() + groupId.replace( '.', '/' ) + "/" + + artifactId + "/" + version + "/" + filename ) ); repoMetadata = new SnapshotArtifactRepositoryMetadata( factory.createBuildArtifact( groupId, artifactId, version, "jar" ) ); repoMetadata.setMetadata( metadataReader.read( new InputStreamReader( is ) ) ); 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 14aba23e5..da25294aa 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 @@ -66,11 +66,11 @@ public class DefaultRepositoryIndexingFactory } /* - * @see RepositoryIndexingFactory#createGeneralRepositoryIndexSearcher(RepositoryIndex) + * @see RepositoryIndexingFactory#createRepositoryIndexSearchLayer(RepositoryIndex) */ - public GeneralRepositoryIndexSearcher createGeneralRepositoryIndexSearcher( RepositoryIndex index ) + public RepositoryIndexSearchLayer createRepositoryIndexSearchLayer( RepositoryIndex index ) { - return new GeneralRepositoryIndexSearcher( index, artifactFactory ); + return new RepositoryIndexSearchLayer( index, artifactFactory ); } /** diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/GeneralRepositoryIndexSearcher.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/GeneralRepositoryIndexSearcher.java deleted file mode 100644 index e87a4a2bb..000000000 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/GeneralRepositoryIndexSearcher.java +++ /dev/null @@ -1,80 +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.factory.ArtifactFactory; -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 java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -/** - * @author Maria Odea Ching - *

- * This class is for "query everything" search in the repository index. - * It creates the Query object that will be passed to the DefaultRepositoryIndexSearcher - * for searching through all the fields in the index. - */ -public class GeneralRepositoryIndexSearcher -{ - private RepositoryIndex index; - - private ArtifactFactory factory; - - /** - * Class constructor - * - * @param index - */ - public GeneralRepositoryIndexSearcher( RepositoryIndex index, ArtifactFactory factory ) - { - this.index = index; - this.factory = factory; - } - - /** - * Method for searching the keyword in all the fields in the index. The index fields will be retrieved - * and query objects will be constructed using the optional (OR) CompoundQuery. - * - * @param keyword - * @return - * @throws RepositoryIndexSearchException - */ - public List search( String keyword ) - throws RepositoryIndexSearchException - { - List qryList = new ArrayList(); - for ( int i = 0; i < index.FIELDS.length; i++ ) - { - Query qry = new SinglePhraseQuery( index.FIELDS[i], keyword ); - qryList.add( qry ); - } - - CompoundQuery cQry = new CompoundQuery(); - for ( Iterator iter = qryList.iterator(); iter.hasNext(); ) - { - cQry.or( (Query) iter.next() ); - } - RepositoryIndexSearcher searcher = new DefaultRepositoryIndexSearcher( index, factory ); - - return searcher.search( cQry ); - } - -} 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 index 484f328f2..9a3e4114c 100644 --- 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 @@ -132,7 +132,7 @@ public class MetadataRepositoryIndex Plugin plugin = (Plugin) iter.next(); if ( plugin.getPrefix() != null && !plugin.getPrefix().equals( "" ) ) { - pluginAppended = plugin.getPrefix() + " "; + pluginAppended = plugin.getPrefix() + "\n"; } } doc.add( Field.Text( FLD_PLUGINPREFIX, pluginAppended ) ); diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndex.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndex.java index 3801bc51a..4c2eba68f 100644 --- a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndex.java +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndex.java @@ -33,43 +33,43 @@ public interface RepositoryIndex static final String ARTIFACT = "ARTIFACT"; - static final String FLD_ID = "ID"; + static final String FLD_ID = "id"; - static final String FLD_NAME = "NAME"; + static final String FLD_NAME = "name"; - static final String FLD_DOCTYPE = "DOCTYPE"; + static final String FLD_DOCTYPE = "doctype"; - static final String FLD_GROUPID = "GROUPID"; + static final String FLD_GROUPID = "groupId"; - static final String FLD_ARTIFACTID = "ARTIFACTID"; + static final String FLD_ARTIFACTID = "artifactId"; - static final String FLD_VERSION = "VERSION"; + static final String FLD_VERSION = "version"; - static final String FLD_PACKAGING = "PACKAGING"; + static final String FLD_PACKAGING = "packaging"; - static final String FLD_SHA1 = "SHA1"; + static final String FLD_SHA1 = "sha1"; - static final String FLD_MD5 = "MD5"; + static final String FLD_MD5 = "md5"; - static final String FLD_LASTUPDATE = "LASTUPDATE"; + static final String FLD_LASTUPDATE = "last update"; - static final String FLD_PLUGINPREFIX = "PLUGINPREFIX"; + static final String FLD_PLUGINPREFIX = "plugin prefix"; - static final String FLD_CLASSES = "CLASSES"; + static final String FLD_CLASSES = "class"; - static final String FLD_PACKAGES = "PACKAGES"; + static final String FLD_PACKAGES = "package"; - static final String FLD_FILES = "FILES"; + static final String FLD_FILES = "file"; - static final String FLD_LICENSE_URLS = "LICENSE_URLS"; + static final String FLD_LICENSE_URLS = "license url"; - static final String FLD_DEPENDENCIES = "DEPENDENCIES"; + static final String FLD_DEPENDENCIES = "dependency"; - static final String FLD_PLUGINS_BUILD = "PLUGINS_BUILD"; + static final String FLD_PLUGINS_BUILD = "build plugin"; - static final String FLD_PLUGINS_REPORT = "PLUGINS_REPORT"; + static final String FLD_PLUGINS_REPORT = "report plugin"; - static final String FLD_PLUGINS_ALL = "PLUGINS_ALL"; + static final String FLD_PLUGINS_ALL = "plugins_all"; static final String[] FIELDS = {FLD_ID, FLD_NAME, FLD_DOCTYPE, FLD_GROUPID, FLD_ARTIFACTID, FLD_VERSION, FLD_PACKAGING, FLD_SHA1, FLD_MD5, FLD_LASTUPDATE, FLD_PLUGINPREFIX, FLD_CLASSES, FLD_PACKAGES, FLD_FILES, @@ -78,6 +78,9 @@ public interface RepositoryIndex static final List KEYWORD_FIELDS = Arrays.asList( new String[]{FLD_ID, FLD_PACKAGING, FLD_LICENSE_URLS, FLD_DEPENDENCIES, FLD_PLUGINS_BUILD, FLD_PLUGINS_REPORT, FLD_PLUGINS_ALL} ); + static final String[] MODEL_FIELDS = + {FLD_PACKAGING, FLD_LICENSE_URLS, FLD_DEPENDENCIES, FLD_PLUGINS_BUILD, FLD_PLUGINS_REPORT}; + /** * Method used to query the index status * diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexSearchHit.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexSearchHit.java new file mode 100644 index 000000000..f28aa1885 --- /dev/null +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexSearchHit.java @@ -0,0 +1,96 @@ +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. + */ + +/** + * This class is the object type contained in the list returned by the DefaultRepositoryIndexSearcher + */ +public class RepositoryIndexSearchHit +{ + private Object obj; + + private boolean isHashMap = false; + + private boolean isMetadata = false; + + private boolean isModel = false; + + /** + * Class constructor + * + * @param isHashMap indicates whether the object is a HashMap object + * @param isMetadata indicates whether the object is a RepositoryMetadata object + * @param isModel indicates whether the object is a Model object + */ + public RepositoryIndexSearchHit( boolean isHashMap, boolean isMetadata, boolean isModel ) + { + this.isHashMap = isHashMap; + this.isMetadata = isMetadata; + this.isModel = isModel; + } + + /** + * Getter method for obj variable + * + * @return the Object + */ + public Object getObject() + { + return obj; + } + + /** + * Setter method for obj variable + * + * @param obj + */ + public void setObject( Object obj ) + { + this.obj = obj; + } + + /** + * Method that indicates if the object is a HashMap + * + * @return boolean + */ + public boolean isHashMap() + { + return isHashMap; + } + + /** + * Method that indicates if the object is a RepositoryMetadata + * + * @return boolean + */ + public boolean isMetadata() + { + return isMetadata; + } + + /** + * Method that indicates if the object is a Model + * + * @return boolean + */ + public boolean isModel() + { + return isModel; + } + +} diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexSearchLayer.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexSearchLayer.java new file mode 100644 index 000000000..1327724fe --- /dev/null +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/RepositoryIndexSearchLayer.java @@ -0,0 +1,438 @@ +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.metadata.RepositoryMetadata; +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.indexing.query.Query; +import org.apache.maven.repository.indexing.query.SinglePhraseQuery; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.StringTokenizer; +import java.util.Set; + +/** + *

+ * This class is to be invoked or called by the action class for + * general and advanced searching. It uses the DefaultRepositoryIndexSearcher + * to perform the search and constructs the search result objects to be + * returned to tha webapp action class. + */ +public class RepositoryIndexSearchLayer +{ + private RepositoryIndex index; + + private ArtifactFactory factory; + + private List searchResults; + + private List generalSearchResults; + + /** + * Class constructor + * + * @param index + */ + public RepositoryIndexSearchLayer( RepositoryIndex index, ArtifactFactory factory ) + { + this.index = index; + this.factory = factory; + } + + /** + * Method for searching the keyword in all the fields in the index. "Query everything" search. + * The index fields will be retrieved and query objects will be constructed using the + * optional (OR) CompoundQuery. + * + * @param keyword + * @return + * @throws RepositoryIndexSearchException + */ + public List searchGeneral( String keyword ) + throws RepositoryIndexSearchException + { + generalSearchResults = new ArrayList(); + for ( int i = 0; i < RepositoryIndex.FIELDS.length; i++ ) + { + Query qry = new SinglePhraseQuery( RepositoryIndex.FIELDS[i], keyword ); + List results = searchAdvanced( qry ); + for ( Iterator iter = results.iterator(); iter.hasNext(); ) + { + SearchResult result = (SearchResult) iter.next(); + Map map = result.getFieldMatches(); + Set entrySet = map.entrySet(); + for ( Iterator it = entrySet.iterator(); it.hasNext(); ) + { + Map.Entry entry = (Map.Entry) it.next(); + SearchResult result2 = + createSearchResult( result.getArtifact(), map, keyword, (String) entry.getKey() ); + generalSearchResults.add( result2 ); + } + } + } + + return generalSearchResults; + } + + /** + * Method for "advanced search" of the index + * + * @param qry the query object that will be used for searching the index + * @return + * @throws RepositoryIndexSearchException + */ + public List searchAdvanced( Query qry ) + throws RepositoryIndexSearchException + { + RepositoryIndexSearcher searcher = new DefaultRepositoryIndexSearcher( index, factory ); + searchResults = new ArrayList(); + + List hits = searcher.search( qry ); + for ( Iterator it = hits.iterator(); it.hasNext(); ) + { + RepositoryIndexSearchHit hit = (RepositoryIndexSearchHit) it.next(); + SearchResult result = new SearchResult(); + if ( hit.isHashMap() ) + { + Map map = (Map) hit.getObject(); + result.setArtifact( (Artifact) map.get( RepositoryIndex.ARTIFACT ) ); + + Map fields = new HashMap(); + fields.put( RepositoryIndex.FLD_CLASSES, map.get( RepositoryIndex.FLD_CLASSES ) ); + fields.put( RepositoryIndex.FLD_PACKAGES, map.get( RepositoryIndex.FLD_PACKAGES ) ); + fields.put( RepositoryIndex.FLD_FILES, map.get( RepositoryIndex.FLD_FILES ) ); + fields.put( RepositoryIndex.FLD_PACKAGING, map.get( RepositoryIndex.FLD_PACKAGING ) ); + fields.put( RepositoryIndex.FLD_SHA1, map.get( RepositoryIndex.FLD_SHA1 ) ); + fields.put( RepositoryIndex.FLD_MD5, map.get( RepositoryIndex.FLD_MD5 ) ); + + result.setFieldMatches( fields ); + searchResults.add( result ); + } + else if ( hit.isModel() ) + { + Model model = (Model) hit.getObject(); + for ( int i = 0; i < RepositoryIndex.MODEL_FIELDS.length; i++ ) + { + result = createSearchResult( model, RepositoryIndex.MODEL_FIELDS[i] ); + searchResults.add( result ); + } + } + else if ( hit.isMetadata() ) + { + //@todo what about metadata objects? + RepositoryMetadata metadata = (RepositoryMetadata) hit.getObject(); + } + } + + return searchResults; + } + + /** + * Method for checking if the artifact already exists in the search result list. + * + * @param groupId the group id of the artifact + * @param artifactId the artifact id of the artifact + * @param version the version of the artifact + * @return the int index number of the artifact in the search result + */ + private int getListIndex( String groupId, String artifactId, String version, List list ) + { + int index = 0; + for ( Iterator iter = list.iterator(); iter.hasNext(); ) + { + SearchResult result = (SearchResult) iter.next(); + Artifact artifact = result.getArtifact(); + if ( artifact.getGroupId().equals( groupId ) && artifact.getArtifactId().equals( artifactId ) && + artifact.getVersion().equals( version ) ) + { + return index; + } + index++; + } + return -1; + } + + /** + * 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; + } + + /** + * Method to get the matching values (packages, classes and files) in the + * given string to be tokenized. + * + * @param tokenizeStr the string to be tokenized + * @param key the map key + * @param resultMap the map to be populated + * @param keyword the value to be matched + * @return the map that contains the matched values + */ + private Map getArtifactHits( String tokenizeStr, String key, Map resultMap, String keyword ) + { + List values = new ArrayList(); + StringTokenizer st = new StringTokenizer( tokenizeStr, "\n" ); + while ( st.hasMoreTokens() ) + { + String str = st.nextToken(); + if ( str.toLowerCase().indexOf( keyword.toLowerCase() ) != -1 ) + { + values.add( str ); + } + } + + if ( values != null && values.size() > 0 ) + { + resultMap.put( key, values ); + } + + return resultMap; + } + + /** + * Method to create SearchResult object from a given HashMap. Used for general search results + * + * @param artifact the retrieved artifact from the index + * @param map the HashMap object that contains the values for the search result + * @param keyword the query term + * @return the SearchResult object + */ + private SearchResult createSearchResult( Artifact artifact, Map map, String keyword, String field ) + { + int index = getListIndex( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), + generalSearchResults ); + SearchResult result; + Map resultMap; + + if ( index > -1 ) + { + result = (SearchResult) generalSearchResults.get( index ); + generalSearchResults.remove( index ); + resultMap = result.getFieldMatches(); + } + else + { + result = new SearchResult(); + result.setArtifact( artifact ); + resultMap = new HashMap(); + } + + // the searched field is either the class, package or file field + if ( field.equals( RepositoryIndex.FLD_CLASSES ) || field.equals( RepositoryIndex.FLD_PACKAGES ) || + field.equals( RepositoryIndex.FLD_FILES ) ) + { + resultMap = getArtifactHits( (String) map.get( field ), field, resultMap, keyword ); + } + else if ( field.equals( RepositoryIndex.FLD_SHA1 ) || + ( field.equals( RepositoryIndex.FLD_MD5 ) || field.equals( RepositoryIndex.FLD_PACKAGING ) ) ) + { + if ( map.get( field ) != null ) + { + // the searched field is either the md5, sha1 or packaging field + if ( ( (String) map.get( field ) ).toLowerCase().equals( keyword.toLowerCase() ) || + ( (String) map.get( field ) ).toLowerCase().indexOf( keyword.toLowerCase() ) != -1 ) + { + resultMap.put( field, map.get( field ) ); + } + } + } + else if ( field.equals( RepositoryIndex.FLD_DEPENDENCIES ) || + field.equals( RepositoryIndex.FLD_PLUGINS_BUILD ) || field.equals( RepositoryIndex.FLD_PLUGINS_REPORT ) || + field.equals( RepositoryIndex.FLD_LICENSE_URLS ) ) + { + List contents = (List) map.get( field ); + List values = new ArrayList(); + for ( Iterator it = contents.iterator(); it.hasNext(); ) + { + String str = (String) it.next(); + if ( str.toLowerCase().equals( keyword.toLowerCase() ) ) + { + values.add( str ); + } + } + if ( values.size() > 0 ) + { + resultMap.put( field, values ); + } + } + result.setFieldMatches( resultMap ); + + return result; + } + + /** + * Method to create a SearchResult object from the given model. Used for advanced search results + * + * @param model the Model object that contains the values for the search result + * @param field the field whose value is to be retrieved + * @return a SearchResult object + */ + private SearchResult createSearchResult( Model model, String field ) + { + int index = getListIndex( model.getGroupId(), model.getArtifactId(), model.getVersion(), searchResults ); + SearchResult result; + Map map; + + // the object already exists in the search result list + if ( index > -1 ) + { + result = (SearchResult) searchResults.get( index ); + searchResults.remove( index ); + map = result.getFieldMatches(); + } + else + { + result = new SearchResult(); + result.setArtifact( factory.createBuildArtifact( model.getGroupId(), model.getArtifactId(), + model.getVersion(), model.getPackaging() ) ); + map = new HashMap(); + } + + // get the matched value with the query term + List values = new ArrayList(); + if ( field.equals( RepositoryIndex.FLD_LICENSE_URLS ) ) + { + values = getLicenseUrls( model ); + } + else if ( field.equals( RepositoryIndex.FLD_DEPENDENCIES ) ) + { + values = getDependencies( model ); + } + else if ( field.equals( RepositoryIndex.FLD_PLUGINS_BUILD ) ) + { + if ( model.getBuild() != null && model.getBuild().getPlugins() != null ) + { + values = getBuildPlugins( model ); + } + } + else if ( field.equals( RepositoryIndex.FLD_PLUGINS_REPORT ) ) + { + if ( model.getReporting() != null && model.getReporting().getPlugins() != null ) + { + values = getReportPlugins( model ); + } + } + else if ( field.equals( RepositoryIndex.FLD_PACKAGING ) ) + { + if ( model.getPackaging() != null ) + { + map.put( RepositoryIndex.FLD_PACKAGING, model.getPackaging() ); + } + } + + if ( values.size() > 0 && values != null ) + { + map.put( field, values ); + } + result.setFieldMatches( map ); + + return result; + } + + /** + * Method for getting the query term hits or matches in the pom's license urls. + * + * @param model the Model object that contains the pom values + * @return a List of matched license urls + */ + private List getLicenseUrls( Model model ) + { + List licenseUrls = new ArrayList(); + List licenseList = model.getLicenses(); + for ( Iterator it = licenseList.iterator(); it.hasNext(); ) + { + License license = (License) it.next(); + licenseUrls.add( license.getUrl() ); + } + return licenseUrls; + } + + /** + * Method for getting the hits or matches in the dependencies specified in the pom + * + * @param model the Model object that contains the pom values + * @return a List of matched dependencies + */ + private List getDependencies( Model model ) + { + List dependencies = new ArrayList(); + List dependencyList = model.getDependencies(); + for ( Iterator it = dependencyList.iterator(); it.hasNext(); ) + { + Dependency dep = (Dependency) it.next(); + dependencies.add( getId( dep.getGroupId(), dep.getArtifactId(), dep.getVersion() ) ); + } + + return dependencies; + } + + /** + * Method for getting the hits or matches in the build plugins specified in the pom + * + * @param model the Model object that contains the pom values + * @return a List of matched build plugins + */ + private List getBuildPlugins( Model model ) + { + List values = new ArrayList(); + List plugins = model.getBuild().getPlugins(); + for ( Iterator it = plugins.iterator(); it.hasNext(); ) + { + Plugin plugin = (Plugin) it.next(); + values.add( getId( plugin.getGroupId(), plugin.getArtifactId(), plugin.getVersion() ) ); + } + + return values; + } + + /** + * Method for getting the hits or matches in the reporting plugins specified in the pom + * + * @param model the Model object that contains the pom values + * @return a List of matched reporting plugins + */ + private List getReportPlugins( Model model ) + { + List values = new ArrayList(); + List plugins = model.getReporting().getPlugins(); + for ( Iterator it = plugins.iterator(); it.hasNext(); ) + { + ReportPlugin plugin = (ReportPlugin) it.next(); + values.add( getId( plugin.getGroupId(), plugin.getArtifactId(), plugin.getVersion() ) ); + } + + return values; + } + +} 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 20b913d07..a1b3981cc 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 @@ -61,12 +61,12 @@ public interface RepositoryIndexingFactory throws RepositoryIndexException; /** - * Method to create an instance of GeneralRepositoryIndexSearcher + * Method to create an instance of RepositoryIndexSearchLayer * * @param index the RepositoryIndex object where the query string will be searched - * @return the GeneralRepositoryIndexSearcher instance + * @return the RepositoryIndexSearchLayer instance */ - GeneralRepositoryIndexSearcher createGeneralRepositoryIndexSearcher( RepositoryIndex index ); + RepositoryIndexSearchLayer createRepositoryIndexSearchLayer( RepositoryIndex index ); /** * Method to create an instance of DefaultRepositoryIndexSearcher diff --git a/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/SearchResult.java b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/SearchResult.java new file mode 100644 index 000000000..ba8daf160 --- /dev/null +++ b/maven-repository-indexer/src/main/java/org/apache/maven/repository/indexing/SearchResult.java @@ -0,0 +1,81 @@ +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 java.util.Map; +import java.util.HashMap; + +/** + * This is the object type contained in the list that will be returned by the + * RepositoryIndexSearchLayer to the action class + */ +public class SearchResult +{ + private Artifact artifact; + + private Map fieldMatches; + + /** + * Class constructor + */ + public SearchResult() + { + fieldMatches = new HashMap(); + } + + /** + * Getter method for artifact + * + * @return Artifact + */ + public Artifact getArtifact() + { + return artifact; + } + + /** + * Setter method for artifact + * + * @param artifact + */ + public void setArtifact( Artifact artifact ) + { + this.artifact = artifact; + } + + /** + * Getter method for fieldMatches + * + * @return Map + */ + public Map getFieldMatches() + { + return fieldMatches; + } + + /** + * Setter method for fieldMatches + * + * @param fieldMatches + */ + public void setFieldMatches( Map fieldMatches ) + { + this.fieldMatches = fieldMatches; + } +} 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 e9c0dc9ba..6361ab276 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,15 +23,15 @@ 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.indexing.query.SinglePhraseQuery; -import org.apache.maven.repository.indexing.query.Query; 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.util.List; import java.util.Iterator; +import java.util.List; /** * @author Edwin Punzalan @@ -162,77 +162,84 @@ public class ArtifactRepositoryIndexingTest RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE ); ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository ); - RepositoryIndexSearcher repoSearcher = factory.createDefaultRepositoryIndexSearcher( indexer ); + RepositoryIndexSearchLayer repoSearchLayer = factory.createRepositoryIndexSearchLayer( indexer ); // search version Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "1.0" ); - List artifacts = repoSearcher.search( qry ); + List artifacts = repoSearchLayer.searchAdvanced( qry ); assertEquals( 1, artifacts.size() ); for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) { - Artifact artifact = (Artifact) iter.next(); + SearchResult result = (SearchResult) iter.next(); + Artifact artifact = result.getArtifact(); assertEquals( "1.0", artifact.getVersion() ); } // search classes qry = new SinglePhraseQuery( RepositoryIndex.FLD_CLASSES, "App" ); - artifacts = repoSearcher.search( qry ); + artifacts = repoSearchLayer.searchAdvanced( qry ); assertEquals( 1, artifacts.size() ); for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) { - Artifact artifact = (Artifact) iter.next(); + SearchResult result = (SearchResult) iter.next(); + Artifact artifact = result.getArtifact(); assertEquals( "test-artifactId", artifact.getArtifactId() ); } // search packages qry = new SinglePhraseQuery( RepositoryIndex.FLD_PACKAGES, "groupId" ); - artifacts = repoSearcher.search( qry ); + artifacts = repoSearchLayer.searchAdvanced( qry ); assertEquals( 1, artifacts.size() ); for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) { - Artifact artifact = (Artifact) iter.next(); + SearchResult result = (SearchResult) iter.next(); + Artifact artifact = result.getArtifact(); assertEquals( "test-artifactId", artifact.getArtifactId() ); } // search files qry = new SinglePhraseQuery( RepositoryIndex.FLD_FILES, "pom.xml" ); - artifacts = repoSearcher.search( qry ); + artifacts = repoSearchLayer.searchAdvanced( qry ); assertEquals( 3, artifacts.size() ); Iterator iter = artifacts.iterator(); if ( iter.hasNext() ) { - Artifact artifact = (Artifact) iter.next(); + SearchResult result = (SearchResult) iter.next(); + Artifact artifact = result.getArtifact(); assertEquals( "test-artifactId", artifact.getArtifactId() ); } // search group id qry = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "org.apache.maven" ); - artifacts = repoSearcher.search( qry ); + artifacts = repoSearchLayer.searchAdvanced( qry ); assertEquals( 2, artifacts.size() ); iter = artifacts.iterator(); if ( iter.hasNext() ) { - Artifact artifact = (Artifact) iter.next(); + 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 = repoSearcher.search( qry ); + artifacts = repoSearchLayer.searchAdvanced( qry ); assertEquals( 1, artifacts.size() ); for ( iter = artifacts.iterator(); iter.hasNext(); ) { - Artifact artifact = (Artifact) iter.next(); + SearchResult result = (SearchResult) iter.next(); + Artifact artifact = result.getArtifact(); assertEquals( "maven-artifact", artifact.getArtifactId() ); } // search version qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "2" ); - artifacts = repoSearcher.search( qry ); + artifacts = repoSearchLayer.searchAdvanced( qry ); assertEquals( 2, artifacts.size() ); for ( iter = artifacts.iterator(); iter.hasNext(); ) { - Artifact artifact = (Artifact) iter.next(); + SearchResult result = (SearchResult) iter.next(); + Artifact artifact = result.getArtifact(); assertTrue( artifact.getVersion().indexOf( "2" ) != -1 ); } @@ -243,11 +250,12 @@ public class ArtifactRepositoryIndexingTest String sha1 = digester.createChecksum( artifact.getFile(), Digester.SHA1 ); qry = new SinglePhraseQuery( RepositoryIndex.FLD_SHA1, sha1.trim() ); - artifacts = repoSearcher.search( qry ); + artifacts = repoSearchLayer.searchAdvanced( qry ); assertEquals( 1, artifacts.size() ); for ( iter = artifacts.iterator(); iter.hasNext(); ) { - Artifact artifact2 = (Artifact) iter.next(); + SearchResult result = (SearchResult) iter.next(); + Artifact artifact2 = result.getArtifact(); String sha1Tmp = digester.createChecksum( artifact2.getFile(), Digester.SHA1 ); assertEquals( sha1, sha1Tmp ); } @@ -255,11 +263,12 @@ public class ArtifactRepositoryIndexingTest // search md5 checksum String md5 = digester.createChecksum( artifact.getFile(), Digester.MD5 ); qry = new SinglePhraseQuery( RepositoryIndex.FLD_MD5, md5.trim() ); - artifacts = repoSearcher.search( qry ); + artifacts = repoSearchLayer.searchAdvanced( qry ); assertEquals( 1, artifacts.size() ); for ( iter = artifacts.iterator(); iter.hasNext(); ) { - Artifact artifact2 = (Artifact) iter.next(); + SearchResult result = (SearchResult) iter.next(); + Artifact artifact2 = result.getArtifact(); String md5Tmp = digester.createChecksum( artifact2.getFile(), Digester.MD5 ); assertEquals( md5, md5Tmp ); } @@ -279,7 +288,8 @@ public class ArtifactRepositoryIndexingTest RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE ); ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository ); - RepositoryIndexSearcher repoSearcher = factory.createDefaultRepositoryIndexSearcher( indexer ); + //RepositoryIndexSearcher repoSearchLayer = factory.createDefaultRepositoryIndexSearcher( indexer ); + RepositoryIndexSearchLayer repoSearchLayer = factory.createRepositoryIndexSearchLayer( indexer ); // Criteria 1: required query // ex. artifactId=maven-artifact AND groupId=org.apache.maven @@ -289,10 +299,11 @@ public class ArtifactRepositoryIndexingTest rQry.and( qry1 ); rQry.and( qry2 ); - List artifacts = repoSearcher.search( rQry ); + List artifacts = repoSearchLayer.searchAdvanced( rQry ); for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) { - Artifact artifact = (Artifact) iter.next(); + SearchResult result = (SearchResult) iter.next(); + Artifact artifact = result.getArtifact(); assertEquals( "maven-artifact", artifact.getArtifactId() ); assertEquals( "org.apache.maven", artifact.getGroupId() ); } @@ -305,10 +316,11 @@ public class ArtifactRepositoryIndexingTest oQry.or( rQry ); oQry.or( qry3 ); - artifacts = repoSearcher.search( oQry ); + artifacts = repoSearchLayer.searchAdvanced( oQry ); for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) { - Artifact artifact = (Artifact) iter.next(); + SearchResult result = (SearchResult) iter.next(); + Artifact artifact = result.getArtifact(); assertEquals( "maven-artifact", artifact.getArtifactId() ); assertEquals( "org.apache.maven", artifact.getGroupId() ); } @@ -333,10 +345,11 @@ public class ArtifactRepositoryIndexingTest rQry2.and( rQry ); rQry2.or( oQry5 ); - artifacts = repoSearcher.search( rQry2 ); + artifacts = repoSearchLayer.searchAdvanced( rQry2 ); for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) { - Artifact artifact = (Artifact) iter.next(); + 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() ); @@ -356,10 +369,11 @@ public class ArtifactRepositoryIndexingTest oQry2.and( rQry2 ); oQry2.and( rQry3 ); - artifacts = repoSearcher.search( oQry2 ); + artifacts = repoSearchLayer.searchAdvanced( oQry2 ); for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) { - Artifact artifact = (Artifact) iter.next(); + 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() ); @@ -378,10 +392,11 @@ public class ArtifactRepositoryIndexingTest rQry4.and( qry8 ); oQry2.and( rQry4 ); - artifacts = repoSearcher.search( oQry2 ); + artifacts = repoSearchLayer.searchAdvanced( oQry2 ); for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) { - Artifact artifact = (Artifact) iter.next(); + SearchResult result = (SearchResult) iter.next(); + Artifact artifact = result.getArtifact(); assertEquals( "maven-artifact", artifact.getArtifactId() ); assertEquals( "org.apache.maven", artifact.getGroupId() ); } @@ -401,12 +416,13 @@ public class ArtifactRepositoryIndexingTest RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE ); ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository ); - RepositoryIndexSearcher repoSearcher = factory.createDefaultRepositoryIndexSearcher( indexer ); + // RepositoryIndexSearcher repoSearchLayer = factory.createDefaultRepositoryIndexSearcher( indexer ); + RepositoryIndexSearchLayer repoSearchLayer = factory.createRepositoryIndexSearchLayer( indexer ); try { Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "~~~~~" ); - List artifacts = repoSearcher.search( qry ); + List artifacts = repoSearchLayer.searchAdvanced( qry ); fail( "Must throw an exception on unparseable query." ); } catch ( RepositoryIndexSearchException re ) @@ -415,12 +431,12 @@ public class ArtifactRepositoryIndexingTest } indexer = factory.createArtifactRepositoryIndex( "target/index/sample", repository ); - repoSearcher = factory.createDefaultRepositoryIndexSearcher( indexer ); + repoSearchLayer = factory.createRepositoryIndexSearchLayer( indexer ); try { Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "1.0" ); - List artifacts = repoSearcher.search( qry ); + List artifacts = repoSearchLayer.searchAdvanced( qry ); fail( "Must throw an exception on invalid index location." ); } catch ( RepositoryIndexSearchException re ) diff --git a/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/GeneralRepositoryIndexSearcherTest.java b/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/GeneralRepositoryIndexSearcherTest.java deleted file mode 100644 index d488ec723..000000000 --- a/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/GeneralRepositoryIndexSearcherTest.java +++ /dev/null @@ -1,353 +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.codehaus.plexus.PlexusTestCase; -import org.codehaus.plexus.util.FileUtils; -import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; -import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; -import org.apache.maven.artifact.repository.ArtifactRepository; -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 org.apache.maven.artifact.factory.ArtifactFactory; -import org.apache.maven.repository.digest.DefaultDigester; -import org.apache.maven.repository.digest.Digester; -import org.apache.maven.model.Model; -import org.apache.maven.model.io.xpp3.MavenXpp3Reader; - -import java.io.File; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.FileReader; -import java.util.List; -import java.util.Iterator; -import java.net.URL; - -/** - * @author Maria Odea Ching - *

- * This class tests the GeneralRepositoryIndexSearcher. - */ -public class GeneralRepositoryIndexSearcherTest - extends PlexusTestCase -{ - private ArtifactRepository repository; - - private ArtifactFactory artifactFactory; - - private Digester digester; - - private String indexPath; - - /** - * Setup method - * - * @throws Exception - */ - 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 = "target/index"; - FileUtils.deleteDirectory( indexPath ); - } - - /** - * Tear down method - * - * @throws Exception - */ - protected void tearDown() - throws Exception - { - super.tearDown(); - } - - /** - * Method for creating the index used for testing - * - * @throws Exception - */ - private void createTestIndex() - throws Exception - { - RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE ); - 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.indexArtifact( artifact ); - indexer.optimize(); - indexer.close(); - - artifact = getArtifact( "org.apache.maven", "maven-model", "2.0" ); - artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); - indexer.indexArtifact( artifact ); - indexer.optimize(); - indexer.close(); - - artifact = getArtifact( "test", "test-artifactId", "1.0" ); - artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); - indexer.indexArtifact( artifact ); - indexer.optimize(); - indexer.close(); - - artifact = getArtifact( "test", "test-artifactId", "1.0" ); - artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); - indexer.indexArtifact( artifact ); - indexer.optimize(); - indexer.close(); - - MetadataRepositoryIndex metaIndexer = factory.createMetadataRepositoryIndex( indexPath, repository ); - RepositoryMetadata repoMetadata = - getMetadata( "org.apache.maven", null, null, "maven-metadata.xml", metaIndexer.GROUP_METADATA ); - metaIndexer.index( repoMetadata ); - metaIndexer.optimize(); - metaIndexer.close(); - - repoMetadata = getMetadata( "org.apache.maven", "maven-artifact", "2.0.1", "maven-metadata.xml", - metaIndexer.ARTIFACT_METADATA ); - metaIndexer.index( repoMetadata ); - metaIndexer.optimize(); - metaIndexer.close(); - - repoMetadata = getMetadata( "org.apache.maven", "maven-artifact", "2.0.1", "maven-metadata.xml", - metaIndexer.SNAPSHOT_METADATA ); - metaIndexer.index( repoMetadata ); - metaIndexer.optimize(); - metaIndexer.close(); - - repoMetadata = getMetadata( "test", null, null, "maven-metadata.xml", metaIndexer.GROUP_METADATA ); - metaIndexer.index( repoMetadata ); - metaIndexer.optimize(); - metaIndexer.close(); - - PomRepositoryIndex pomIndexer = factory.createPomRepositoryIndex( indexPath, repository ); - - Model pom = getPom( "org.apache.maven", "maven-artifact", "2.0.1" ); - pomIndexer.indexPom( pom ); - pomIndexer.optimize(); - pomIndexer.close(); - - pom = getPom( "org.apache.maven", "maven-model", "2.0" ); - pomIndexer.indexPom( pom ); - pomIndexer.optimize(); - pomIndexer.close(); - - pom = getPom( "test", "test-artifactId", "1.0" ); - pomIndexer.indexPom( pom ); - pomIndexer.optimize(); - pomIndexer.close(); - - pom = getPom( "test", "test-artifactId", "1.0" ); - pomIndexer.indexPom( pom ); - pomIndexer.optimize(); - pomIndexer.close(); - } - - /** - * Method for testing the "query everything" searcher - * - * @throws Exception - */ - public void testGeneralSearcher() - throws Exception - { - createTestIndex(); - RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE ); - ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository ); - GeneralRepositoryIndexSearcher searcher = factory.createGeneralRepositoryIndexSearcher( indexer ); - - List returnList = searcher.search( "org.apache.maven" ); - assertEquals( returnList.size(), 7 ); - for ( Iterator iter = returnList.iterator(); iter.hasNext(); ) - { - Object obj = (Object) iter.next(); - if ( obj instanceof Artifact ) - { - Artifact artifact = (Artifact) obj; - assertEquals( artifact.getGroupId(), "org.apache.maven" ); - } - else if ( obj instanceof RepositoryMetadata ) - { - RepositoryMetadata repoMetadata = (RepositoryMetadata) obj; - assertEquals( repoMetadata.getGroupId(), "org.apache.maven" ); - } - } - - returnList = searcher.search( "test" ); - assertEquals( returnList.size(), 3 ); - for ( Iterator iter = returnList.iterator(); iter.hasNext(); ) - { - Object obj = (Object) iter.next(); - if ( obj instanceof Artifact ) - { - Artifact artifact = (Artifact) obj; - assertEquals( artifact.getGroupId(), "test" ); - } - else if ( obj instanceof RepositoryMetadata ) - { - RepositoryMetadata repoMetadata = (RepositoryMetadata) obj; - assertEquals( repoMetadata.getGroupId(), "test" ); - } - } - - returnList = searcher.search( "artifact" ); - assertEquals( returnList.size(), 4 ); - for ( Iterator iter = returnList.iterator(); iter.hasNext(); ) - { - Object obj = (Object) iter.next(); - if ( obj instanceof Artifact ) - { - Artifact artifact = (Artifact) obj; - assertEquals( artifact.getArtifactId(), "maven-artifact" ); - } - else if ( obj instanceof RepositoryMetadata ) - { - RepositoryMetadata repoMetadata = (RepositoryMetadata) obj; - assertEquals( repoMetadata.getArtifactId(), "maven-artifact" ); - } - } - } - - /** - * Method for creating 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( MetadataRepositoryIndex.GROUP_METADATA ) ) - { - 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( MetadataRepositoryIndex.ARTIFACT_METADATA ) ) - { - 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( MetadataRepositoryIndex.SNAPSHOT_METADATA ) ) - { - 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; - } - - /** - * Method for creating Artifact object - * - * @param groupId the groupId of the artifact to be created - * @param artifactId the artifactId of the artifact to be created - * @param version the version of the artifact to be created - * @return Artifact object - * @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" ); - } - - /** - * Method for creating a Model object given the groupId, artifactId and version - * - * @param groupId the groupId of the model to be created - * @param artifactId the artifactId of the model to be created - * @param version the version of the model to be created - * @return Model object - * @throws Exception - */ - private Model getPom( String groupId, String artifactId, String version ) - throws Exception - { - Artifact artifact = getArtifact( groupId, artifactId, version ); - - return getPom( artifact ); - } - - /** - * Method for creating a Model object given an artifact - * - * @param artifact the artifact to be created a Model object for - * @return Model object - * @throws Exception - */ - private Model getPom( Artifact artifact ) - throws Exception - { - File pomFile = getPomFile( artifact ); - - MavenXpp3Reader pomReader = new MavenXpp3Reader(); - return pomReader.read( new FileReader( pomFile ) ); - } - - /** - * Method for creating a pom file - * - * @param artifact - * @return File - */ - private File getPomFile( Artifact artifact ) - { - String path = new File( repository.getBasedir(), repository.pathOf( artifact ) ).getAbsolutePath(); - return new File( path.substring( 0, path.lastIndexOf( '.' ) ) + ".pom" ); - } - -} 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 index f74acc1da..0d1f5f8f7 100644 --- 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 @@ -136,35 +136,43 @@ public class MetadataRepositoryIndexingTest RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE ); MetadataRepositoryIndex indexer = factory.createMetadataRepositoryIndex( indexPath, repository ); - RepositoryIndexSearcher repoSearcher = factory.createDefaultRepositoryIndexSearcher( indexer ); + //RepositoryIndexSearcher repoSearchLayer = factory.createDefaultRepositoryIndexSearcher( indexer ); + RepositoryIndexSearchLayer repoSearchLayer = factory.createRepositoryIndexSearchLayer( indexer ); // search last update org.apache.maven.repository.indexing.query.Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_LASTUPDATE, "20051212044643" ); - List metadataList = repoSearcher.search( qry ); - assertEquals( 1, metadataList.size() ); + List metadataList = repoSearchLayer.searchAdvanced( 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() ); + RepositoryIndexSearchHit hit = (RepositoryIndexSearchHit) iter.next(); + if ( hit.isMetadata() ) + { + RepositoryMetadata repoMetadata = (RepositoryMetadata) hit.getObject(); + Metadata metadata = repoMetadata.getMetadata(); + Versioning versioning = metadata.getVersioning(); + assertEquals( "20051212044643", versioning.getLastUpdated() ); + } } // search plugin prefix qry = new SinglePhraseQuery( RepositoryIndex.FLD_PLUGINPREFIX, "org.apache.maven" ); - metadataList = repoSearcher.search( qry ); - assertEquals( 1, metadataList.size() ); + metadataList = repoSearchLayer.searchAdvanced( 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(); ) + RepositoryIndexSearchHit hit = (RepositoryIndexSearchHit) iter.next(); + if ( hit.isMetadata() ) { - Plugin plugin = (Plugin) it.next(); - assertEquals( "org.apache.maven", plugin.getPrefix() ); + RepositoryMetadata repoMetadata = (RepositoryMetadata) hit.getObject(); + 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() ); + } } } @@ -175,13 +183,17 @@ public class MetadataRepositoryIndexingTest rQry.addQuery( qry1 ); rQry.addQuery( qry2 ); - metadataList = repoSearcher.search( rQry ); + metadataList = repoSearchLayer.searchAdvanced( 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() ); + RepositoryIndexSearchHit hit = (RepositoryIndexSearchHit) iter.next(); + if ( hit.isMetadata() ) + { + RepositoryMetadata repoMetadata = (RepositoryMetadata) hit.getObject(); + Metadata metadata = repoMetadata.getMetadata(); + Versioning versioning = metadata.getVersioning(); + assertEquals( "20051212044643", versioning.getLastUpdated() ); + } } // search last update using EXCLUSIVE Range Query @@ -191,7 +203,7 @@ public class MetadataRepositoryIndexingTest rQry.addQuery( qry1 ); rQry.addQuery( qry2 ); - metadataList = repoSearcher.search( rQry ); + metadataList = repoSearchLayer.searchAdvanced( rQry ); assertEquals( metadataList.size(), 0 ); indexer.close(); 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 index 85aa32d31..71f32c037 100644 --- 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 @@ -22,23 +22,21 @@ 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.License; -import org.apache.maven.model.Dependency; -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.DefaultDigester; import org.apache.maven.repository.digest.Digester; -import org.apache.maven.repository.indexing.query.SinglePhraseQuery; -import org.apache.maven.repository.indexing.query.Query; 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.List; import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; /** * @author Edwin Punzalan @@ -125,85 +123,100 @@ public class PomRepositoryIndexingTest RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE ); PomRepositoryIndex indexer = factory.createPomRepositoryIndex( indexPath, repository ); - RepositoryIndexSearcher repoSearcher = factory.createDefaultRepositoryIndexSearcher( indexer ); + //RepositoryIndexSearcher repoSearchLayer = factory.createDefaultRepositoryIndexSearcher( indexer ); + RepositoryIndexSearchLayer repoSearchLayer = factory.createRepositoryIndexSearchLayer( indexer ); // search version - Query qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_VERSION, "1.0" ); - List artifactList = repoSearcher.search( qry ); - //assertEquals( 1, artifactList.size() ); + Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "1.0" ); + List artifactList = repoSearchLayer.searchAdvanced( qry ); + assertEquals( 1, artifactList.size() ); for ( Iterator iter = artifactList.iterator(); iter.hasNext(); ) { - Artifact artifact = (Artifact) iter.next(); + SearchResult result = (SearchResult) iter.next(); + Artifact artifact = result.getArtifact(); assertEquals( "1.0", artifact.getVersion() ); } // search group id - qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_GROUPID, "org.apache.maven" ); - artifactList = repoSearcher.search( qry ); + qry = new SinglePhraseQuery( RepositoryIndex.FLD_GROUPID, "org.apache.maven" ); + artifactList = repoSearchLayer.searchAdvanced( qry ); assertEquals( 2, artifactList.size() ); Iterator artifacts = artifactList.iterator(); if ( artifacts.hasNext() ) { - Artifact artifact = (Artifact) artifacts.next(); + SearchResult result = (SearchResult) artifacts.next(); + Artifact artifact = result.getArtifact(); assertEquals( "org.apache.maven", artifact.getGroupId() ); } // search artifact id - qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_ARTIFACTID, "maven-artifact" ); - artifactList = repoSearcher.search( qry ); + qry = new SinglePhraseQuery( RepositoryIndex.FLD_ARTIFACTID, "maven-artifact" ); + artifactList = repoSearchLayer.searchAdvanced( qry ); assertEquals( 1, artifactList.size() ); for ( artifacts = artifactList.iterator(); artifacts.hasNext(); ) { - Artifact artifact = (Artifact) artifacts.next(); + SearchResult result = (SearchResult) artifacts.next(); + Artifact artifact = result.getArtifact(); assertEquals( "maven-artifact", artifact.getArtifactId() ); } // search version - qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_VERSION, "2" ); - artifactList = repoSearcher.search( qry ); + qry = new SinglePhraseQuery( RepositoryIndex.FLD_VERSION, "2" ); + artifactList = repoSearchLayer.searchAdvanced( qry ); assertEquals( 2, artifactList.size() ); for ( artifacts = artifactList.iterator(); artifacts.hasNext(); ) { - Artifact artifact = (Artifact) artifacts.next(); + SearchResult result = (SearchResult) artifacts.next(); + Artifact artifact = result.getArtifact(); assertTrue( artifact.getVersion().indexOf( "2" ) != -1 ); } // search packaging - qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_PACKAGING, "jar" ); - artifactList = repoSearcher.search( qry ); + qry = new SinglePhraseQuery( RepositoryIndex.FLD_PACKAGING, "jar" ); + artifactList = repoSearchLayer.searchAdvanced( qry ); assertEquals( 3, artifactList.size() ); for ( artifacts = artifactList.iterator(); artifacts.hasNext(); ) { - Artifact artifact = (Artifact) artifacts.next(); - assertEquals( "jar", artifact.getType() ); + SearchResult result = (SearchResult) artifacts.next(); + Map map = result.getFieldMatches(); + Set mapEntry = map.entrySet(); + for ( Iterator it = mapEntry.iterator(); it.hasNext(); ) + { + Map.Entry entry = (Map.Entry) it.next(); + } + assertEquals( "jar", (String) map.get( RepositoryIndex.FLD_PACKAGING ) ); } //search license url - qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_LICENSE_URLS, - "http://www.apache.org/licenses/LICENSE-2.0.txt" ); - artifactList = repoSearcher.search( qry ); + qry = + new SinglePhraseQuery( RepositoryIndex.FLD_LICENSE_URLS, "http://www.apache.org/licenses/LICENSE-2.0.txt" ); + artifactList = repoSearchLayer.searchAdvanced( qry ); assertEquals( 2, artifactList.size() ); for ( artifacts = artifactList.iterator(); artifacts.hasNext(); ) { - Artifact artifact = (Artifact) artifacts.next(); - License license = (License) getPom( artifact ).getLicenses().get( 0 ); - assertEquals( "http://www.apache.org/licenses/LICENSE-2.0.txt", license.getUrl() ); + 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( PomRepositoryIndex.FLD_DEPENDENCIES, "org.codehaus.plexus:plexus-utils:1.0.5" ); - artifactList = repoSearcher.search( qry ); + qry = new SinglePhraseQuery( RepositoryIndex.FLD_DEPENDENCIES, "org.codehaus.plexus:plexus-utils:1.0.5" ); + artifactList = repoSearchLayer.searchAdvanced( qry ); assertEquals( 2, artifactList.size() ); for ( artifacts = artifactList.iterator(); artifacts.hasNext(); ) { - Artifact artifact = (Artifact) artifacts.next(); - Iterator dependencies = getPom( artifact ).getDependencies().iterator(); + SearchResult result = (SearchResult) artifacts.next(); + Map map = result.getFieldMatches(); boolean depFound = false; + Iterator dependencies = ( (List) map.get( RepositoryIndex.FLD_DEPENDENCIES ) ).iterator(); while ( dependencies.hasNext() ) { - Dependency dep = (Dependency) dependencies.next(); - if ( "org.codehaus.plexus:plexus-utils:1.0.5".equals( - dep.getGroupId() + ":" + dep.getArtifactId() + ":" + dep.getVersion() ) ) + String dep = (String) dependencies.next(); + if ( "org.codehaus.plexus:plexus-utils:1.0.5".equals( dep ) ) { depFound = true; break; @@ -213,20 +226,20 @@ public class PomRepositoryIndexingTest } //search build plugin - qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_PLUGINS_BUILD, - "org.codehaus.modello:modello-maven-plugin:2.0" ); - artifactList = repoSearcher.search( qry ); + qry = + new SinglePhraseQuery( RepositoryIndex.FLD_PLUGINS_BUILD, "org.codehaus.modello:modello-maven-plugin:2.0" ); + artifactList = repoSearchLayer.searchAdvanced( qry ); assertEquals( 1, artifactList.size() ); for ( artifacts = artifactList.iterator(); artifacts.hasNext(); ) { - Artifact artifact = (Artifact) artifacts.next(); - Iterator plugins = getPom( artifact ).getBuild().getPlugins().iterator(); + SearchResult result = (SearchResult) artifacts.next(); + Map map = result.getFieldMatches(); + Iterator plugins = ( (List) map.get( RepositoryIndex.FLD_PLUGINS_BUILD ) ).iterator(); boolean found = false; while ( plugins.hasNext() ) { - Plugin plugin = (Plugin) plugins.next(); - if ( "org.codehaus.modello:modello-maven-plugin:2.0".equals( - plugin.getKey() + ":" + plugin.getVersion() ) ) + String plugin = (String) plugins.next(); + if ( "org.codehaus.modello:modello-maven-plugin:2.0".equals( plugin ) ) { found = true; break; @@ -236,20 +249,20 @@ public class PomRepositoryIndexingTest } //search reporting plugin - qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_PLUGINS_REPORT, + qry = new SinglePhraseQuery( RepositoryIndex.FLD_PLUGINS_REPORT, "org.apache.maven.plugins:maven-checkstyle-plugin:2.0" ); - artifactList = repoSearcher.search( qry ); + artifactList = repoSearchLayer.searchAdvanced( qry ); assertEquals( 1, artifactList.size() ); for ( artifacts = artifactList.iterator(); artifacts.hasNext(); ) { - Artifact artifact = (Artifact) artifacts.next(); - Iterator plugins = getPom( artifact ).getReporting().getPlugins().iterator(); + SearchResult result = (SearchResult) artifacts.next(); + Map map = result.getFieldMatches(); + Iterator plugins = ( (List) map.get( RepositoryIndex.FLD_PLUGINS_REPORT ) ).iterator(); boolean found = false; while ( plugins.hasNext() ) { - ReportPlugin plugin = (ReportPlugin) plugins.next(); - if ( "org.apache.maven.plugins:maven-checkstyle-plugin:2.0".equals( - plugin.getKey() + ":" + plugin.getVersion() ) ) + String plugin = (String) plugins.next(); + if ( "org.apache.maven.plugins:maven-checkstyle-plugin:2.0".equals( plugin ) ) { found = true; break; @@ -263,24 +276,26 @@ public class PomRepositoryIndexingTest artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); String sha1 = digester.createChecksum( artifact.getFile(), Digester.SHA1 ); - qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_SHA1, sha1.trim() ); - artifactList = repoSearcher.search( qry ); + qry = new SinglePhraseQuery( RepositoryIndex.FLD_SHA1, sha1.trim() ); + artifactList = repoSearchLayer.searchAdvanced( qry ); assertEquals( 1, artifactList.size() ); for ( artifacts = artifactList.iterator(); artifacts.hasNext(); ) { - Artifact artifact2 = (Artifact) artifacts.next(); + 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( PomRepositoryIndex.FLD_MD5, md5.trim() ); - artifactList = repoSearcher.search( qry ); + qry = new SinglePhraseQuery( RepositoryIndex.FLD_MD5, md5.trim() ); + artifactList = repoSearchLayer.searchAdvanced( qry ); assertEquals( 1, artifactList.size() ); for ( artifacts = artifactList.iterator(); artifacts.hasNext(); ) { - Artifact artifact2 = (Artifact) artifacts.next(); + SearchResult result = (SearchResult) artifacts.next(); + Artifact artifact2 = result.getArtifact(); String md5Tmp = digester.createChecksum( getPomFile( artifact2 ), Digester.MD5 ); assertEquals( md5, md5Tmp ); } @@ -300,20 +315,22 @@ public class PomRepositoryIndexingTest RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE ); PomRepositoryIndex indexer = factory.createPomRepositoryIndex( indexPath, repository ); - RepositoryIndexSearcher repoSearcher = factory.createDefaultRepositoryIndexSearcher( indexer ); + //RepositoryIndexSearcher repoSearchLayer = factory.createDefaultRepositoryIndexSearcher( indexer ); + RepositoryIndexSearchLayer repoSearchLayer = factory.createRepositoryIndexSearchLayer( indexer ); // Criteria 1: required query // ex. artifactId=maven-artifact AND groupId=org.apache.maven - Query qry1 = new SinglePhraseQuery( PomRepositoryIndex.FLD_ARTIFACTID, "maven-artifact" ); - Query qry2 = new SinglePhraseQuery( PomRepositoryIndex.FLD_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 = repoSearcher.search( rQry ); + List artifacts = repoSearchLayer.searchAdvanced( rQry ); for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) { - Artifact artifact = (Artifact) iter.next(); + SearchResult result = (SearchResult) iter.next(); + Artifact artifact = result.getArtifact(); assertEquals( "maven-artifact", artifact.getArtifactId() ); assertEquals( "org.apache.maven", artifact.getGroupId() ); } @@ -321,15 +338,16 @@ public class PomRepositoryIndexingTest // Criteria 2: nested required query // ex. (artifactId=maven-artifact AND groupId=org.apache.maven) OR // version=2.0.3 - Query qry3 = new SinglePhraseQuery( PomRepositoryIndex.FLD_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 = repoSearcher.search( oQry ); + artifacts = repoSearchLayer.searchAdvanced( oQry ); for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) { - Artifact artifact = (Artifact) iter.next(); + SearchResult result = (SearchResult) iter.next(); + Artifact artifact = result.getArtifact(); assertEquals( "maven-artifact", artifact.getArtifactId() ); assertEquals( "org.apache.maven", artifact.getGroupId() ); } @@ -338,15 +356,15 @@ public class PomRepositoryIndexingTest // 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( PomRepositoryIndex.FLD_VERSION, "2.0.1" ); + 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( PomRepositoryIndex.FLD_DEPENDENCIES, "org.codehaus.plexus:plexus-utils:1.0.5" ); - Query qry10 = new SinglePhraseQuery( PomRepositoryIndex.FLD_DEPENDENCIES, + 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 ); @@ -356,10 +374,11 @@ public class PomRepositoryIndexingTest rQry2.and( rQry ); rQry2.and( oQry5 ); - artifacts = repoSearcher.search( rQry2 ); + artifacts = repoSearchLayer.searchAdvanced( rQry2 ); for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) { - Artifact artifact = (Artifact) iter.next(); + 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() ); @@ -371,18 +390,19 @@ public class PomRepositoryIndexingTest // 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( PomRepositoryIndex.FLD_ARTIFACTID, "sample" ); - Query qry6 = new SinglePhraseQuery( PomRepositoryIndex.FLD_GROUPID, "test" ); + 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 = repoSearcher.search( oQry2 ); + artifacts = repoSearchLayer.searchAdvanced( oQry2 ); for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) { - Artifact artifact = (Artifact) iter.next(); + 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() ); @@ -395,16 +415,17 @@ public class PomRepositoryIndexingTest // [(artifactId=sample AND groupId=test)] OR // [(artifactId=sample2 AND groupId=test)] CompoundQuery rQry4 = new CompoundQuery(); - Query qry7 = new SinglePhraseQuery( PomRepositoryIndex.FLD_ARTIFACTID, "sample2" ); - Query qry8 = new SinglePhraseQuery( PomRepositoryIndex.FLD_GROUPID, "test" ); + 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 = repoSearcher.search( oQry2 ); + artifacts = repoSearchLayer.searchAdvanced( oQry2 ); for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) { - Artifact artifact = (Artifact) iter.next(); + SearchResult result = (SearchResult) iter.next(); + Artifact artifact = result.getArtifact(); assertEquals( "maven-artifact", artifact.getArtifactId() ); assertEquals( "org.apache.maven", artifact.getGroupId() ); } @@ -459,10 +480,10 @@ public class PomRepositoryIndexingTest RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE ); PomRepositoryIndex indexer = factory.createPomRepositoryIndex( indexPath, repository ); Model pom = getPom( "org.apache.maven", "maven-artifact", "2.0.1" ); - indexer.deleteDocument( PomRepositoryIndex.FLD_ID, PomRepositoryIndex.POM + pom.getId() ); + indexer.deleteDocument( RepositoryIndex.FLD_ID, RepositoryIndex.POM + pom.getId() ); RepositoryIndexSearcher repoSearcher = factory.createDefaultRepositoryIndexSearcher( indexer ); - Query qry = new SinglePhraseQuery( PomRepositoryIndex.FLD_ID, PomRepositoryIndex.POM + pom.getId() ); + Query qry = new SinglePhraseQuery( RepositoryIndex.FLD_ID, RepositoryIndex.POM + pom.getId() ); List artifactList = repoSearcher.search( qry ); assertEquals( artifactList.size(), 0 ); } 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 new file mode 100644 index 000000000..41827e798 --- /dev/null +++ b/maven-repository-indexer/src/test/java/org/apache/maven/repository/indexing/RepositoryIndexSearchLayerTest.java @@ -0,0 +1,476 @@ +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.codehaus.plexus.PlexusTestCase; +import org.codehaus.plexus.util.FileUtils; +import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; +import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; +import org.apache.maven.artifact.repository.ArtifactRepository; +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 org.apache.maven.artifact.factory.ArtifactFactory; +import org.apache.maven.repository.digest.DefaultDigester; +import org.apache.maven.repository.digest.Digester; +import org.apache.maven.model.Model; +import org.apache.maven.model.io.xpp3.MavenXpp3Reader; + +import java.io.File; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.FileReader; +import java.util.List; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; +import java.net.URL; + +/** + *

+ * This class tests the RepositoryIndexSearchLayer. + */ +public class RepositoryIndexSearchLayerTest + extends PlexusTestCase +{ + private ArtifactRepository repository; + + private ArtifactFactory artifactFactory; + + private Digester digester; + + private String indexPath; + + /** + * Setup method + * + * @throws Exception + */ + 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 = "target/index"; + FileUtils.deleteDirectory( indexPath ); + } + + /** + * Tear down method + * + * @throws Exception + */ + protected void tearDown() + throws Exception + { + super.tearDown(); + } + + /** + * Method for creating the index used for testing + * + * @throws Exception + */ + private void createTestIndex() + throws Exception + { + RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE ); + 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.indexArtifact( artifact ); + indexer.optimize(); + indexer.close(); + + artifact = getArtifact( "org.apache.maven", "maven-model", "2.0" ); + artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); + indexer.indexArtifact( artifact ); + indexer.optimize(); + indexer.close(); + + artifact = getArtifact( "test", "test-artifactId", "1.0" ); + artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); + indexer.indexArtifact( artifact ); + indexer.optimize(); + indexer.close(); + + artifact = getArtifact( "test", "test-artifactId", "1.0" ); + artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); + indexer.indexArtifact( artifact ); + indexer.optimize(); + indexer.close(); + + MetadataRepositoryIndex metaIndexer = factory.createMetadataRepositoryIndex( indexPath, repository ); + RepositoryMetadata repoMetadata = + getMetadata( "org.apache.maven", null, null, "maven-metadata.xml", metaIndexer.GROUP_METADATA ); + metaIndexer.index( repoMetadata ); + metaIndexer.optimize(); + metaIndexer.close(); + + repoMetadata = getMetadata( "org.apache.maven", "maven-artifact", "2.0.1", "maven-metadata.xml", + metaIndexer.ARTIFACT_METADATA ); + metaIndexer.index( repoMetadata ); + metaIndexer.optimize(); + metaIndexer.close(); + + repoMetadata = getMetadata( "org.apache.maven", "maven-artifact", "2.0.1", "maven-metadata.xml", + metaIndexer.SNAPSHOT_METADATA ); + metaIndexer.index( repoMetadata ); + metaIndexer.optimize(); + metaIndexer.close(); + + repoMetadata = getMetadata( "test", null, null, "maven-metadata.xml", metaIndexer.GROUP_METADATA ); + metaIndexer.index( repoMetadata ); + metaIndexer.optimize(); + metaIndexer.close(); + + PomRepositoryIndex pomIndexer = factory.createPomRepositoryIndex( indexPath, repository ); + + Model pom = getPom( "org.apache.maven", "maven-artifact", "2.0.1" ); + pomIndexer.indexPom( pom ); + pomIndexer.optimize(); + pomIndexer.close(); + + pom = getPom( "org.apache.maven", "maven-model", "2.0" ); + pomIndexer.indexPom( pom ); + pomIndexer.optimize(); + pomIndexer.close(); + + pom = getPom( "test", "test-artifactId", "1.0" ); + pomIndexer.indexPom( pom ); + pomIndexer.optimize(); + pomIndexer.close(); + + pom = getPom( "test", "test-artifactId", "1.0" ); + pomIndexer.indexPom( pom ); + pomIndexer.optimize(); + pomIndexer.close(); + } + + /** + * Method for testing the "query everything" searcher + * + * @throws Exception + */ + public void testGeneralSearcher() + throws Exception + { + createTestIndex(); + RepositoryIndexingFactory factory = (RepositoryIndexingFactory) lookup( RepositoryIndexingFactory.ROLE ); + ArtifactRepositoryIndex indexer = factory.createArtifactRepositoryIndex( indexPath, repository ); + RepositoryIndexSearchLayer searchLayer = factory.createRepositoryIndexSearchLayer( indexer ); + + List returnList = searchLayer.searchGeneral( "org.apache.maven" ); + for ( Iterator iter = returnList.iterator(); iter.hasNext(); ) + { + SearchResult result = (SearchResult) iter.next(); + Map map = result.getFieldMatches(); + Set entries = map.entrySet(); + for ( Iterator it = entries.iterator(); it.hasNext(); ) + { + Map.Entry entry = (Map.Entry) it.next(); + if ( entry.getKey().equals( RepositoryIndex.FLD_PACKAGES ) ) + { + List packages = (List) entry.getValue(); + for ( Iterator iterator = packages.iterator(); iterator.hasNext(); ) + { + assertTrue( ( (String) iterator.next() ).indexOf( "org.apache.maven" ) != -1 ); + } + } + } + } + + //POM license urls + returnList = searchLayer.searchGeneral( "http://www.apache.org/licenses/LICENSE-2.0.txt" ); + for ( Iterator iter = returnList.iterator(); iter.hasNext(); ) + { + SearchResult result = (SearchResult) iter.next(); + Map map = result.getFieldMatches(); + Set entries = map.entrySet(); + for ( Iterator it = entries.iterator(); it.hasNext(); ) + { + Map.Entry entry = (Map.Entry) it.next(); + if ( entry.getKey().equals( RepositoryIndex.FLD_LICENSE_URLS ) ) + { + List packages = (List) entry.getValue(); + for ( Iterator iterator = packages.iterator(); iterator.hasNext(); ) + { + assertEquals( "http://www.apache.org/licenses/LICENSE-2.0.txt", (String) iterator.next() ); + } + } + } + } + + //POM dependency + returnList = searchLayer.searchGeneral( "org.codehaus.plexus:plexus-utils:1.0.5" ); + for ( Iterator iter = returnList.iterator(); iter.hasNext(); ) + { + SearchResult result = (SearchResult) iter.next(); + Map map = result.getFieldMatches(); + Set entries = map.entrySet(); + for ( Iterator it = entries.iterator(); it.hasNext(); ) + { + Map.Entry entry = (Map.Entry) it.next(); + if ( entry.getKey().equals( RepositoryIndex.FLD_DEPENDENCIES ) ) + { + List packages = (List) entry.getValue(); + for ( Iterator iterator = packages.iterator(); iterator.hasNext(); ) + { + assertEquals( "org.codehaus.plexus:plexus-utils:1.0.5", (String) iterator.next() ); + } + } + } + } + + // POM reporting plugin + returnList = searchLayer.searchGeneral( "org.apache.maven.plugins:maven-checkstyle-plugin:2.0" ); + for ( Iterator iter = returnList.iterator(); iter.hasNext(); ) + { + SearchResult result = (SearchResult) iter.next(); + Map map = result.getFieldMatches(); + Set entries = map.entrySet(); + for ( Iterator it = entries.iterator(); it.hasNext(); ) + { + Map.Entry entry = (Map.Entry) it.next(); + if ( entry.getKey().equals( RepositoryIndex.FLD_PLUGINS_REPORT ) ) + { + List packages = (List) entry.getValue(); + for ( Iterator iterator = packages.iterator(); iterator.hasNext(); ) + { + assertEquals( "org.apache.maven.plugins:maven-checkstyle-plugin:2.0", + (String) iterator.next() ); + } + } + } + } + + // POM build plugin + returnList = searchLayer.searchGeneral( "org.codehaus.modello:modello-maven-plugin:2.0" ); + for ( Iterator iter = returnList.iterator(); iter.hasNext(); ) + { + SearchResult result = (SearchResult) iter.next(); + Map map = result.getFieldMatches(); + Set entries = map.entrySet(); + for ( Iterator it = entries.iterator(); it.hasNext(); ) + { + Map.Entry entry = (Map.Entry) it.next(); + if ( entry.getKey().equals( RepositoryIndex.FLD_PLUGINS_BUILD ) ) + { + List packages = (List) entry.getValue(); + for ( Iterator iterator = packages.iterator(); iterator.hasNext(); ) + { + assertEquals( "org.codehaus.modello:modello-maven-plugin:2.0", (String) iterator.next() ); + } + } + } + } + + //maven-artifact-2.0.1.jar MD5 checksum + returnList = searchLayer.searchGeneral( "F5A934ABBBC70A33136D89A996B9D5C09F652766" ); + for ( Iterator iter = returnList.iterator(); iter.hasNext(); ) + { + SearchResult result = (SearchResult) iter.next(); + Map map = result.getFieldMatches(); + Set entries = map.entrySet(); + for ( Iterator it = entries.iterator(); it.hasNext(); ) + { + Map.Entry entry = (Map.Entry) it.next(); + if ( entry.getKey().equals( RepositoryIndex.FLD_MD5 ) ) + { + assertTrue( + ( (String) entry.getValue() ).indexOf( "F5A934ABBBC70A33136D89A996B9D5C09F652766" ) != -1 ); + } + } + } + + //maven-artifact-2.0.1.jar SHA1 checksum + returnList = searchLayer.searchGeneral( "AE55D9B5720E11B6CF19FE1E31A42E51" ); + for ( Iterator iter = returnList.iterator(); iter.hasNext(); ) + { + SearchResult result = (SearchResult) iter.next(); + Map map = result.getFieldMatches(); + Set entries = map.entrySet(); + for ( Iterator it = entries.iterator(); it.hasNext(); ) + { + Map.Entry entry = (Map.Entry) it.next(); + if ( entry.getKey().equals( RepositoryIndex.FLD_SHA1 ) ) + { + assertTrue( ( (String) entry.getValue() ).indexOf( "AE55D9B5720E11B6CF19FE1E31A42E516" ) != -1 ); + } + } + } + + //packaging jar + returnList = searchLayer.searchGeneral( "jar" ); + for ( Iterator iter = returnList.iterator(); iter.hasNext(); ) + { + SearchResult result = (SearchResult) iter.next(); + Map map = result.getFieldMatches(); + Set entries = map.entrySet(); + for ( Iterator it = entries.iterator(); it.hasNext(); ) + { + Map.Entry entry = (Map.Entry) it.next(); + if ( entry.getKey().equals( RepositoryIndex.FLD_PACKAGING ) ) + { + assertEquals( "jar", (String) entry.getValue() ); + } + } + } + + returnList = searchLayer.searchGeneral( "test" ); + for ( Iterator iter = returnList.iterator(); iter.hasNext(); ) + { + SearchResult result = (SearchResult) iter.next(); + assertEquals( result.getArtifact().getGroupId(), "test" ); + } + + returnList = searchLayer.searchGeneral( "test-artifactId" ); + for ( Iterator iter = returnList.iterator(); iter.hasNext(); ) + { + SearchResult result = (SearchResult) iter.next(); + assertEquals( result.getArtifact().getArtifactId(), "test-artifactId" ); + } + + } + + + /** + * Method for creating 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( MetadataRepositoryIndex.GROUP_METADATA ) ) + { + 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( MetadataRepositoryIndex.ARTIFACT_METADATA ) ) + { + 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( MetadataRepositoryIndex.SNAPSHOT_METADATA ) ) + { + 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; + } + + /** + * Method for creating Artifact object + * + * @param groupId the groupId of the artifact to be created + * @param artifactId the artifactId of the artifact to be created + * @param version the version of the artifact to be created + * @return Artifact object + * @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" ); + } + + /** + * Method for creating a Model object given the groupId, artifactId and version + * + * @param groupId the groupId of the model to be created + * @param artifactId the artifactId of the model to be created + * @param version the version of the model to be created + * @return Model object + * @throws Exception + */ + private Model getPom( String groupId, String artifactId, String version ) + throws Exception + { + Artifact artifact = getArtifact( groupId, artifactId, version ); + + return getPom( artifact ); + } + + /** + * Method for creating a Model object given an artifact + * + * @param artifact the artifact to be created a Model object for + * @return Model object + * @throws Exception + */ + private Model getPom( Artifact artifact ) + throws Exception + { + File pomFile = getPomFile( artifact ); + + MavenXpp3Reader pomReader = new MavenXpp3Reader(); + return pomReader.read( new FileReader( pomFile ) ); + } + + /** + * Method for creating a pom file + * + * @param artifact + * @return File + */ + private File getPomFile( Artifact artifact ) + { + String path = new File( repository.getBasedir(), repository.pathOf( artifact ) ).getAbsolutePath(); + return new File( path.substring( 0, path.lastIndexOf( '.' ) ) + ".pom" ); + } + +}