From: Brett Porter Date: Sat, 26 Aug 2006 07:42:37 +0000 (+0000) Subject: rename package indexing -> indexer X-Git-Tag: archiva-0.9-alpha-1~652 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c5a3a9035f08f7f726113bd587e5648434bf2292;p=archiva.git rename package indexing -> indexer git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/trunk@437117 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/archiva-core/src/main/java/org/apache/maven/archiva/scheduler/IndexerTask.java b/archiva-core/src/main/java/org/apache/maven/archiva/scheduler/IndexerTask.java index a23f69f42..4468b2b1d 100644 --- a/archiva-core/src/main/java/org/apache/maven/archiva/scheduler/IndexerTask.java +++ b/archiva-core/src/main/java/org/apache/maven/archiva/scheduler/IndexerTask.java @@ -23,10 +23,10 @@ import org.apache.maven.archiva.configuration.ConfiguredRepositoryFactory; import org.apache.maven.archiva.configuration.RepositoryConfiguration; import org.apache.maven.archiva.discovery.ArtifactDiscoverer; import org.apache.maven.archiva.discovery.DiscovererException; -import org.apache.maven.archiva.indexing.RepositoryArtifactIndex; -import org.apache.maven.archiva.indexing.RepositoryArtifactIndexFactory; -import org.apache.maven.archiva.indexing.RepositoryIndexException; -import org.apache.maven.archiva.indexing.record.RepositoryIndexRecordFactory; +import org.apache.maven.archiva.indexer.RepositoryArtifactIndex; +import org.apache.maven.archiva.indexer.RepositoryArtifactIndexFactory; +import org.apache.maven.archiva.indexer.RepositoryIndexException; +import org.apache.maven.archiva.indexer.record.RepositoryIndexRecordFactory; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.repository.ArtifactRepository; import org.codehaus.plexus.logging.AbstractLogEnabled; diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/RepositoryArtifactIndex.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/RepositoryArtifactIndex.java new file mode 100644 index 000000000..a0476d80d --- /dev/null +++ b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/RepositoryArtifactIndex.java @@ -0,0 +1,69 @@ +package org.apache.maven.archiva.indexer; + +/* + * 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.archiva.indexer.query.Query; + +import java.util.Collection; +import java.util.List; + +/** + * Maintain an artifact index on the repository. + * + * @author Brett Porter + */ +public interface RepositoryArtifactIndex +{ + /** + * Indexes the artifacts found within the specified list of index records. If the artifacts are already in the + * repository they are updated. + * + * @param records the artifacts to index + * @throws RepositoryIndexException if there is a problem indexing the records + */ + void indexRecords( Collection records ) + throws RepositoryIndexException; + + /** + * Search the index based on the search criteria specified. Returns a list of index records. + * + * @param query The query that contains the search criteria + * @return the index records found + * @throws RepositoryIndexSearchException if there is a problem searching + * @todo should it return "SearchResult" instances that contain the index record and other search data (like score?) + */ + List search( Query query ) + throws RepositoryIndexSearchException; + + /** + * Check if the index already exists. + * + * @return true if the index already exists + * @throws RepositoryIndexException if the index location is not valid + */ + boolean exists() + throws RepositoryIndexException; + + /** + * Delete records from the index. Simply ignore the request any did not exist. + * + * @param records the records to delete + * @throws RepositoryIndexException if there is a problem removing the record + */ + void deleteRecords( Collection records ) + throws RepositoryIndexException; +} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/RepositoryArtifactIndexFactory.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/RepositoryArtifactIndexFactory.java new file mode 100644 index 000000000..83d8a2de3 --- /dev/null +++ b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/RepositoryArtifactIndexFactory.java @@ -0,0 +1,48 @@ +package org.apache.maven.archiva.indexer; + +/* + * Copyright 2005-2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.io.File; + +/** + * Obtain an index instance. + * + * @author Brett Porter + */ +public interface RepositoryArtifactIndexFactory +{ + /** + * Plexus role. + */ + String ROLE = RepositoryArtifactIndexFactory.class.getName(); + + /** + * Method to create an instance of the standard index. + * + * @param indexPath the path where the index will be created/updated + * @return the index instance + */ + RepositoryArtifactIndex createStandardIndex( File indexPath ); + + /** + * Method to create an instance of the minimal index. + * + * @param indexPath the path where the index will be created/updated + * @return the index instance + */ + RepositoryArtifactIndex createMinimalIndex( File indexPath ); +} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/RepositoryIndexException.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/RepositoryIndexException.java new file mode 100644 index 000000000..fb18f18fa --- /dev/null +++ b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/RepositoryIndexException.java @@ -0,0 +1,34 @@ +package org.apache.maven.archiva.indexer; + +/* + * Copyright 2005-2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @author Edwin Punzalan + */ +public class RepositoryIndexException + extends Exception +{ + public RepositoryIndexException( String message, Throwable cause ) + { + super( message, cause ); + } + + public RepositoryIndexException( String message ) + { + super( message ); + } +} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/RepositoryIndexSearchException.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/RepositoryIndexSearchException.java new file mode 100644 index 000000000..5088c4de7 --- /dev/null +++ b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/RepositoryIndexSearchException.java @@ -0,0 +1,29 @@ +package org.apache.maven.archiva.indexer; + +/* + * Copyright 2005-2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @author Brett Porter + */ +public class RepositoryIndexSearchException + extends Exception +{ + public RepositoryIndexSearchException( String message, Throwable cause ) + { + super( message, cause ); + } +} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/lucene/LuceneIndexRecordConverter.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/lucene/LuceneIndexRecordConverter.java new file mode 100644 index 000000000..6bd96dc7b --- /dev/null +++ b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/lucene/LuceneIndexRecordConverter.java @@ -0,0 +1,48 @@ +package org.apache.maven.archiva.indexer.lucene; + +/* + * Copyright 2005-2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.lucene.document.Document; +import org.apache.maven.archiva.indexer.record.RepositoryIndexRecord; + +import java.text.ParseException; + +/** + * Converts repository records to Lucene documents. + * + * @author Brett Porter + */ +public interface LuceneIndexRecordConverter +{ + /** + * Convert an index record to a Lucene document. + * + * @param record the record + * @return the document + */ + Document convert( RepositoryIndexRecord record ); + + /** + * Convert a Lucene document to an index record. + * + * @param document the document + * @return the record + * @throws java.text.ParseException if there is a problem parsing a field (specifically, dates) + */ + RepositoryIndexRecord convert( Document document ) + throws ParseException; +} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/lucene/LuceneMinimalIndexRecordConverter.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/lucene/LuceneMinimalIndexRecordConverter.java new file mode 100644 index 000000000..0e29e2ca3 --- /dev/null +++ b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/lucene/LuceneMinimalIndexRecordConverter.java @@ -0,0 +1,84 @@ +package org.apache.maven.archiva.indexer.lucene; + +/* + * Copyright 2005-2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.lucene.document.DateTools; +import org.apache.lucene.document.Document; +import org.apache.lucene.document.Field; +import org.apache.lucene.document.NumberTools; +import org.apache.maven.archiva.indexer.record.MinimalArtifactIndexRecord; +import org.apache.maven.archiva.indexer.record.MinimalIndexRecordFields; +import org.apache.maven.archiva.indexer.record.RepositoryIndexRecord; +import org.codehaus.plexus.util.StringUtils; + +import java.text.ParseException; +import java.util.Arrays; + +/** + * Convert the minimal index record to a Lucene document. + * + * @author Brett Porter + */ +public class LuceneMinimalIndexRecordConverter + implements LuceneIndexRecordConverter +{ + public Document convert( RepositoryIndexRecord record ) + { + MinimalArtifactIndexRecord rec = (MinimalArtifactIndexRecord) record; + + Document document = new Document(); + addTokenizedField( document, MinimalIndexRecordFields.FILENAME, rec.getFilename() ); + addUntokenizedField( document, MinimalIndexRecordFields.LAST_MODIFIED, + DateTools.timeToString( rec.getLastModified(), DateTools.Resolution.SECOND ) ); + addUntokenizedField( document, MinimalIndexRecordFields.FILE_SIZE, NumberTools.longToString( rec.getSize() ) ); + addUntokenizedField( document, MinimalIndexRecordFields.MD5, rec.getMd5Checksum() ); + addTokenizedField( document, MinimalIndexRecordFields.CLASSES, + StringUtils.join( rec.getClasses().iterator(), "\n" ) ); + + return document; + } + + public RepositoryIndexRecord convert( Document document ) + throws ParseException + { + MinimalArtifactIndexRecord record = new MinimalArtifactIndexRecord(); + + record.setFilename( document.get( MinimalIndexRecordFields.FILENAME ) ); + record.setLastModified( DateTools.stringToTime( document.get( MinimalIndexRecordFields.LAST_MODIFIED ) ) ); + record.setSize( NumberTools.stringToLong( document.get( MinimalIndexRecordFields.FILE_SIZE ) ) ); + record.setMd5Checksum( document.get( MinimalIndexRecordFields.MD5 ) ); + record.setClasses( Arrays.asList( document.get( MinimalIndexRecordFields.CLASSES ).split( "\n" ) ) ); + + return record; + } + + private static void addUntokenizedField( Document document, String name, String value ) + { + if ( value != null ) + { + document.add( new Field( name, value, Field.Store.YES, Field.Index.UN_TOKENIZED ) ); + } + } + + private static void addTokenizedField( Document document, String name, String value ) + { + if ( value != null ) + { + document.add( new Field( name, value, Field.Store.YES, Field.Index.TOKENIZED ) ); + } + } +} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/lucene/LuceneQuery.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/lucene/LuceneQuery.java new file mode 100644 index 000000000..0f43ec8e0 --- /dev/null +++ b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/lucene/LuceneQuery.java @@ -0,0 +1,40 @@ +package org.apache.maven.archiva.indexer.lucene; + +/* + * 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.archiva.indexer.query.Query; + +/** + * A holder for a lucene query to pass to the indexer API. + * + * @author Brett Porter + */ +public class LuceneQuery + implements Query +{ + private final org.apache.lucene.search.Query query; + + public LuceneQuery( org.apache.lucene.search.Query query ) + { + this.query = query; + } + + org.apache.lucene.search.Query getLuceneQuery() + { + return query; + } +} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/lucene/LuceneRepositoryArtifactIndex.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/lucene/LuceneRepositoryArtifactIndex.java new file mode 100644 index 000000000..c97b468cb --- /dev/null +++ b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/lucene/LuceneRepositoryArtifactIndex.java @@ -0,0 +1,276 @@ +package org.apache.maven.archiva.indexer.lucene; + +/* + * Copyright 2005-2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.lucene.analysis.Analyzer; +import org.apache.lucene.analysis.standard.StandardAnalyzer; +import org.apache.lucene.document.Document; +import org.apache.lucene.document.Field; +import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.IndexWriter; +import org.apache.lucene.index.Term; +import org.apache.lucene.search.Hits; +import org.apache.lucene.search.IndexSearcher; +import org.apache.maven.archiva.indexer.RepositoryArtifactIndex; +import org.apache.maven.archiva.indexer.RepositoryIndexException; +import org.apache.maven.archiva.indexer.RepositoryIndexSearchException; +import org.apache.maven.archiva.indexer.query.Query; +import org.apache.maven.archiva.indexer.record.RepositoryIndexRecord; + +import java.io.File; +import java.io.IOException; +import java.text.ParseException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; + +/** + * Lucene implementation of a repository index. + * + * @author Brett Porter + */ +public class LuceneRepositoryArtifactIndex + implements RepositoryArtifactIndex +{ + /** + * The location of the index on the file system. + */ + private File indexLocation; + + /** + * Convert repository records to Lucene documents. + */ + private LuceneIndexRecordConverter converter; + + private static final String FLD_PK = "pk"; + + public LuceneRepositoryArtifactIndex( File indexPath, LuceneIndexRecordConverter converter ) + { + this.indexLocation = indexPath; + this.converter = converter; + } + + public void indexRecords( Collection records ) + throws RepositoryIndexException + { + deleteRecords( records ); + + addRecords( records ); + } + + private void addRecords( Collection records ) + throws RepositoryIndexException + { + IndexWriter indexWriter; + try + { + indexWriter = new IndexWriter( indexLocation, getAnalyzer(), !exists() ); + } + catch ( IOException e ) + { + throw new RepositoryIndexException( "Unable to open index", e ); + } + + try + { + for ( Iterator i = records.iterator(); i.hasNext(); ) + { + RepositoryIndexRecord record = (RepositoryIndexRecord) i.next(); + + if ( record != null ) + { + Document document = converter.convert( record ); + document.add( + new Field( FLD_PK, record.getPrimaryKey(), Field.Store.NO, Field.Index.UN_TOKENIZED ) ); + + indexWriter.addDocument( document ); + } + } + + indexWriter.optimize(); + } + catch ( IOException e ) + { + throw new RepositoryIndexException( "Failed to add an index document", e ); + } + finally + { + close( indexWriter ); + } + } + + private void close( IndexWriter indexWriter ) + throws RepositoryIndexException + { + try + { + if ( indexWriter != null ) + { + indexWriter.close(); + } + } + catch ( IOException e ) + { + throw new RepositoryIndexException( e.getMessage(), e ); + } + } + + private Analyzer getAnalyzer() + { + // TODO: investigate why changed in original! Probably for MD5 and number querying. + return new StandardAnalyzer(); + } + + public void deleteRecords( Collection records ) + throws RepositoryIndexException + { + if ( exists() ) + { + IndexReader indexReader = null; + try + { + indexReader = IndexReader.open( indexLocation ); + + for ( Iterator artifacts = records.iterator(); artifacts.hasNext(); ) + { + RepositoryIndexRecord record = (RepositoryIndexRecord) artifacts.next(); + + if ( record != null ) + { + Term term = new Term( FLD_PK, record.getPrimaryKey() ); + + indexReader.deleteDocuments( term ); + } + } + } + catch ( IOException e ) + { + throw new RepositoryIndexException( "Error deleting document: " + e.getMessage(), e ); + } + finally + { + if ( indexReader != null ) + { + closeQuietly( indexReader ); + } + } + } + } + + public boolean exists() + throws RepositoryIndexException + { + if ( IndexReader.indexExists( indexLocation ) ) + { + return true; + } + else if ( !indexLocation.exists() ) + { + return false; + } + else if ( indexLocation.isDirectory() ) + { + if ( indexLocation.listFiles().length > 1 ) + { + throw new RepositoryIndexException( indexLocation + " is not a valid index directory." ); + } + else + { + return false; + } + } + else + { + throw new RepositoryIndexException( indexLocation + " is not a directory." ); + } + } + + public List search( Query query ) + throws RepositoryIndexSearchException + { + LuceneQuery lQuery = (LuceneQuery) query; + + org.apache.lucene.search.Query luceneQuery = lQuery.getLuceneQuery(); + + IndexSearcher searcher; + try + { + searcher = new IndexSearcher( indexLocation.getAbsolutePath() ); + } + catch ( IOException e ) + { + throw new RepositoryIndexSearchException( "Unable to open index: " + e.getMessage(), e ); + } + + List records = new ArrayList(); + try + { + Hits hits = searcher.search( luceneQuery ); + for ( int i = 0; i < hits.length(); i++ ) + { + Document doc = hits.doc( i ); + + records.add( converter.convert( doc ) ); + } + } + catch ( IOException e ) + { + throw new RepositoryIndexSearchException( "Unable to search index: " + e.getMessage(), e ); + } + catch ( ParseException e ) + { + throw new RepositoryIndexSearchException( "Unable to search index: " + e.getMessage(), e ); + } + finally + { + closeQuietly( searcher ); + } + + return records; + } + + private static void closeQuietly( IndexSearcher searcher ) + { + try + { + if ( searcher != null ) + { + searcher.close(); + } + } + catch ( IOException e ) + { + // ignore + } + } + + private static void closeQuietly( IndexReader reader ) + { + try + { + if ( reader != null ) + { + reader.close(); + } + } + catch ( IOException e ) + { + // ignore + } + } +} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/lucene/LuceneRepositoryArtifactIndexFactory.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/lucene/LuceneRepositoryArtifactIndexFactory.java new file mode 100644 index 000000000..0666a8dab --- /dev/null +++ b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/lucene/LuceneRepositoryArtifactIndexFactory.java @@ -0,0 +1,42 @@ +package org.apache.maven.archiva.indexer.lucene; + +/* + * 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.archiva.indexer.RepositoryArtifactIndex; +import org.apache.maven.archiva.indexer.RepositoryArtifactIndexFactory; + +import java.io.File; + +/** + * Factory for Lucene artifact index instances. + * + * @author Brett Porter + * @plexus.component role="org.apache.maven.archiva.indexer.RepositoryArtifactIndexFactory" role-hint="lucene" + */ +public class LuceneRepositoryArtifactIndexFactory + implements RepositoryArtifactIndexFactory +{ + public RepositoryArtifactIndex createStandardIndex( File indexPath ) + { + return new LuceneRepositoryArtifactIndex( indexPath, new LuceneStandardIndexRecordConverter() ); + } + + public RepositoryArtifactIndex createMinimalIndex( File indexPath ) + { + return new LuceneRepositoryArtifactIndex( indexPath, new LuceneMinimalIndexRecordConverter() ); + } +} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/lucene/LuceneStandardIndexRecordConverter.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/lucene/LuceneStandardIndexRecordConverter.java new file mode 100644 index 000000000..f64dd4a85 --- /dev/null +++ b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/lucene/LuceneStandardIndexRecordConverter.java @@ -0,0 +1,145 @@ +package org.apache.maven.archiva.indexer.lucene; + +/* + * Copyright 2005-2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.lucene.document.DateTools; +import org.apache.lucene.document.Document; +import org.apache.lucene.document.Field; +import org.apache.lucene.document.NumberTools; +import org.apache.maven.archiva.indexer.record.RepositoryIndexRecord; +import org.apache.maven.archiva.indexer.record.StandardArtifactIndexRecord; +import org.apache.maven.archiva.indexer.record.StandardIndexRecordFields; +import org.codehaus.plexus.util.StringUtils; + +import java.text.ParseException; +import java.util.Arrays; + +/** + * Convert the standard index record to a Lucene document. + * + * @author Brett Porter + */ +public class LuceneStandardIndexRecordConverter + implements LuceneIndexRecordConverter +{ + public Document convert( RepositoryIndexRecord record ) + { + StandardArtifactIndexRecord rec = (StandardArtifactIndexRecord) record; + + Document document = new Document(); + addTokenizedField( document, StandardIndexRecordFields.FILENAME, rec.getFilename() ); + addTokenizedField( document, StandardIndexRecordFields.GROUPID, rec.getGroupId() ); + addExactField( document, StandardIndexRecordFields.GROUPID_EXACT, rec.getGroupId() ); + addTokenizedField( document, StandardIndexRecordFields.ARTIFACTID, rec.getArtifactId() ); + addExactField( document, StandardIndexRecordFields.ARTIFACTID_EXACT, rec.getArtifactId() ); + addTokenizedField( document, StandardIndexRecordFields.VERSION, rec.getVersion() ); + addExactField( document, StandardIndexRecordFields.VERSION_EXACT, rec.getVersion() ); + addTokenizedField( document, StandardIndexRecordFields.BASE_VERSION, rec.getBaseVersion() ); + addExactField( document, StandardIndexRecordFields.BASE_VERSION_EXACT, rec.getBaseVersion() ); + addUntokenizedField( document, StandardIndexRecordFields.TYPE, rec.getType() ); + addTokenizedField( document, StandardIndexRecordFields.CLASSIFIER, rec.getClassifier() ); + addUntokenizedField( document, StandardIndexRecordFields.PACKAGING, rec.getPackaging() ); + addUntokenizedField( document, StandardIndexRecordFields.REPOSITORY, rec.getRepository() ); + addUntokenizedField( document, StandardIndexRecordFields.LAST_MODIFIED, + DateTools.timeToString( rec.getLastModified(), DateTools.Resolution.SECOND ) ); + addUntokenizedField( document, StandardIndexRecordFields.FILE_SIZE, NumberTools.longToString( rec.getSize() ) ); + addUntokenizedField( document, StandardIndexRecordFields.MD5, rec.getMd5Checksum() ); + addUntokenizedField( document, StandardIndexRecordFields.SHA1, rec.getSha1Checksum() ); + if ( rec.getClasses() != null ) + { + addTokenizedField( document, StandardIndexRecordFields.CLASSES, + StringUtils.join( rec.getClasses().iterator(), "\n" ) ); + } + if ( rec.getFiles() != null ) + { + addTokenizedField( document, StandardIndexRecordFields.FILES, + StringUtils.join( rec.getFiles().iterator(), "\n" ) ); + } + addUntokenizedField( document, StandardIndexRecordFields.PLUGIN_PREFIX, rec.getPluginPrefix() ); + addUntokenizedField( document, StandardIndexRecordFields.INCEPTION_YEAR, rec.getInceptionYear() ); + addTokenizedField( document, StandardIndexRecordFields.PROJECT_NAME, rec.getProjectName() ); + addTokenizedField( document, StandardIndexRecordFields.PROJECT_DESCRIPTION, rec.getProjectDescription() ); +/* TODO: add later + document.add( Field.Keyword( StandardIndexRecordFields.FLD_LICENSE_URLS, "" ) ); + document.add( Field.Keyword( StandardIndexRecordFields.FLD_DEPENDENCIES, "" ) ); + document.add( Field.Keyword( StandardIndexRecordFields.FLD_PLUGINS_REPORT, "" ) ); + document.add( Field.Keyword( StandardIndexRecordFields.FLD_PLUGINS_BUILD, "" ) ); +*/ + + return document; + } + + public RepositoryIndexRecord convert( Document document ) + throws ParseException + { + StandardArtifactIndexRecord record = new StandardArtifactIndexRecord(); + + record.setFilename( document.get( StandardIndexRecordFields.FILENAME ) ); + record.setGroupId( document.get( StandardIndexRecordFields.GROUPID ) ); + record.setArtifactId( document.get( StandardIndexRecordFields.ARTIFACTID ) ); + record.setVersion( document.get( StandardIndexRecordFields.VERSION ) ); + record.setBaseVersion( document.get( StandardIndexRecordFields.BASE_VERSION ) ); + record.setType( document.get( StandardIndexRecordFields.TYPE ) ); + record.setClassifier( document.get( StandardIndexRecordFields.CLASSIFIER ) ); + record.setPackaging( document.get( StandardIndexRecordFields.PACKAGING ) ); + record.setRepository( document.get( StandardIndexRecordFields.REPOSITORY ) ); + record.setLastModified( DateTools.stringToTime( document.get( StandardIndexRecordFields.LAST_MODIFIED ) ) ); + record.setSize( NumberTools.stringToLong( document.get( StandardIndexRecordFields.FILE_SIZE ) ) ); + record.setMd5Checksum( document.get( StandardIndexRecordFields.MD5 ) ); + record.setSha1Checksum( document.get( StandardIndexRecordFields.SHA1 ) ); + String classes = document.get( StandardIndexRecordFields.CLASSES ); + if ( classes != null ) + { + record.setClasses( Arrays.asList( classes.split( "\n" ) ) ); + } + String files = document.get( StandardIndexRecordFields.FILES ); + if ( files != null ) + { + record.setFiles( Arrays.asList( files.split( "\n" ) ) ); + } + record.setPluginPrefix( document.get( StandardIndexRecordFields.PLUGIN_PREFIX ) ); + record.setInceptionYear( document.get( StandardIndexRecordFields.INCEPTION_YEAR ) ); + record.setProjectName( document.get( StandardIndexRecordFields.PROJECT_NAME ) ); + record.setProjectDescription( document.get( StandardIndexRecordFields.PROJECT_DESCRIPTION ) ); + + return record; + } + + private static void addUntokenizedField( Document document, String name, String value ) + { + if ( value != null ) + { + document.add( new Field( name, value, Field.Store.YES, Field.Index.UN_TOKENIZED ) ); + } + } + + private static void addExactField( Document document, String name, String value ) + { + if ( value != null ) + { + document.add( new Field( name, value, Field.Store.NO, Field.Index.UN_TOKENIZED ) ); + } + } + + private static void addTokenizedField( Document document, String name, String value ) + { + if ( value != null ) + { + document.add( new Field( name, value, Field.Store.YES, Field.Index.TOKENIZED ) ); + } + } +} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/query/CompoundQuery.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/query/CompoundQuery.java new file mode 100644 index 000000000..538360d6f --- /dev/null +++ b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/query/CompoundQuery.java @@ -0,0 +1,105 @@ +package org.apache.maven.archiva.indexer.query; + +/* + * Copyright 2005-2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.util.ArrayList; +import java.util.List; + +/** + * Class to hold multiple SinglePhraseQueries and/or other CompoundQueries. + * + * @author Edwin Punzalan + */ +public class CompoundQuery + implements Query +{ + /** + * The query terms. + */ + private final List compoundQueryTerms = new ArrayList(); + + /** + * Appends a required term to this query. + * + * @param term the term to be appended to this query + */ + public void and( QueryTerm term ) + { + compoundQueryTerms.add( CompoundQueryTerm.and( new SingleTermQuery( term ) ) ); + } + + /** + * Appends an optional term to this query. + * + * @param term the term to be appended to this query + */ + public void or( QueryTerm term ) + { + compoundQueryTerms.add( CompoundQueryTerm.or( new SingleTermQuery( term ) ) ); + } + + /** + * Appends a prohibited term to this query. + * + * @param term the term to be appended to this query + */ + public void not( QueryTerm term ) + { + compoundQueryTerms.add( CompoundQueryTerm.not( new SingleTermQuery( term ) ) ); + } + + /** + * Appends a required subquery to this query. + * + * @param query the subquery to be appended to this query + */ + public void and( Query query ) + { + compoundQueryTerms.add( CompoundQueryTerm.and( query ) ); + } + + /** + * Appends an optional subquery to this query. + * + * @param query the subquery to be appended to this query + */ + public void or( Query query ) + { + compoundQueryTerms.add( CompoundQueryTerm.or( query ) ); + } + + /** + * Appends a prohibited subquery to this query. + * + * @param query the subquery to be appended to this query + */ + public void not( Query query ) + { + compoundQueryTerms.add( CompoundQueryTerm.not( query ) ); + } + + /** + * Method to get the List of Queries appended into this + * + * @return List of all Queries added to this Query + */ + public List getCompoundQueryTerms() + { + return compoundQueryTerms; + } + +} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/query/CompoundQueryTerm.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/query/CompoundQueryTerm.java new file mode 100644 index 000000000..06057d335 --- /dev/null +++ b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/query/CompoundQueryTerm.java @@ -0,0 +1,100 @@ +package org.apache.maven.archiva.indexer.query; + +/* + * Copyright 2005-2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Base of all query terms. + * + * @author Brett Porter + */ +public class CompoundQueryTerm +{ + /** + * The query to add to the compound query. + */ + private final Query query; + + /** + * Whether the term is required (an AND). + */ + private final boolean required; + + /** + * Whether the term is prohibited (a NOT). + */ + private final boolean prohibited; + + /** + * Class constructor + * + * @param query the subquery to add + * @param required whether the term is required (an AND) + * @param prohibited whether the term is prohibited (a NOT) + */ + private CompoundQueryTerm( Query query, boolean required, boolean prohibited ) + { + this.query = query; + this.prohibited = prohibited; + this.required = required; + } + + /** + * Method to test if the Query is a search requirement + * + * @return true if this Query is a search requirement, otherwise returns false + */ + public boolean isRequired() + { + return required; + } + + /** + * Method to test if the Query is prohibited in the search result + * + * @return true if this Query is prohibited in the search result + */ + public boolean isProhibited() + { + return prohibited; + } + + + /** + * The subquery to execute. + * + * @return the query + */ + public Query getQuery() + { + return query; + } + + static CompoundQueryTerm and( Query query ) + { + return new CompoundQueryTerm( query, true, false ); + } + + static CompoundQueryTerm or( Query query ) + { + return new CompoundQueryTerm( query, false, false ); + } + + static CompoundQueryTerm not( Query query ) + { + return new CompoundQueryTerm( query, false, true ); + } +} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/query/Query.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/query/Query.java new file mode 100644 index 000000000..e5709391b --- /dev/null +++ b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/query/Query.java @@ -0,0 +1,26 @@ +package org.apache.maven.archiva.indexer.query; + +/* + * Copyright 2005-2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Interface to label the query classes + * + * @author Edwin Punzalan + */ +public interface Query +{ +} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/query/QueryTerm.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/query/QueryTerm.java new file mode 100644 index 000000000..8309ec9d6 --- /dev/null +++ b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/query/QueryTerm.java @@ -0,0 +1,61 @@ +package org.apache.maven.archiva.indexer.query; + +/* + * Copyright 2005-2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Class to hold a single field search condition + * + * @author Edwin Punzalan + */ +public class QueryTerm +{ + private String field; + + private String value; + + /** + * Class constructor + * + * @param field the index field to search + * @param value the index value requirement + */ + public QueryTerm( String field, String value ) + { + this.field = field; + this.value = value; + } + + /** + * Method to retrieve the name of the index field searched + * + * @return the name of the index field + */ + public String getField() + { + return field; + } + + /** + * Method to retrieve the value used in searching the index field + * + * @return the value to corresspond the index field + */ + public String getValue() + { + return value; + } +} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/query/RangeQuery.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/query/RangeQuery.java new file mode 100644 index 000000000..82e0a7a2e --- /dev/null +++ b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/query/RangeQuery.java @@ -0,0 +1,150 @@ +package org.apache.maven.archiva.indexer.query; + +/* + * Copyright 2005-2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Query object that handles range queries (presently used for dates). + * + * @author Maria Odea Ching + * @author Brett Porter + */ +public class RangeQuery + implements Query +{ + /** + * Whether values equal to the boundaries are included in the query results. + */ + private final boolean inclusive; + + /** + * The lower bound. + */ + private final QueryTerm begin; + + /** + * The upper bound. + */ + private final QueryTerm end; + + /** + * Constructor. + * + * @param begin the lower bound + * @param end the upper bound + * @param inclusive whether to include the boundaries in the query + */ + private RangeQuery( QueryTerm begin, QueryTerm end, boolean inclusive ) + { + this.begin = begin; + this.end = end; + this.inclusive = inclusive; + } + + /** + * Create an open range, including all results. + * + * @return the query object + */ + public static RangeQuery createOpenRange() + { + return new RangeQuery( null, null, false ); + } + + /** + * Create a bounded range, excluding the endpoints. + * + * @param begin the lower bound value to compare to + * @param end the upper bound value to compare to + * @return the query object + */ + public static RangeQuery createExclusiveRange( QueryTerm begin, QueryTerm end ) + { + return new RangeQuery( begin, end, false ); + } + + /** + * Create a bounded range, including the endpoints. + * + * @param begin the lower bound value to compare to + * @param end the upper bound value to compare to + * @return the query object + */ + public static RangeQuery createInclusiveRange( QueryTerm begin, QueryTerm end ) + { + return new RangeQuery( begin, end, true ); + } + + /** + * Create a range that is greater than or equal to a given term. + * + * @param begin the value to compare to + * @return the query object + */ + public static RangeQuery createGreaterThanOrEqualToRange( QueryTerm begin ) + { + return new RangeQuery( begin, null, true ); + } + + /** + * Create a range that is greater than a given term. + * + * @param begin the value to compare to + * @return the query object + */ + public static RangeQuery createGreaterThanRange( QueryTerm begin ) + { + return new RangeQuery( begin, null, false ); + } + + /** + * Create a range that is less than or equal to a given term. + * + * @param end the value to compare to + * @return the query object + */ + public static RangeQuery createLessThanOrEqualToRange( QueryTerm end ) + { + return new RangeQuery( null, end, true ); + } + + /** + * Create a range that is less than a given term. + * + * @param end the value to compare to + * @return the query object + */ + public static RangeQuery createLessThanRange( QueryTerm end ) + { + return new RangeQuery( null, end, false ); + } + + public QueryTerm getBegin() + { + return begin; + } + + public QueryTerm getEnd() + { + return end; + } + + public boolean isInclusive() + { + return inclusive; + } + +} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/query/SingleTermQuery.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/query/SingleTermQuery.java new file mode 100644 index 000000000..e2effa83d --- /dev/null +++ b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/query/SingleTermQuery.java @@ -0,0 +1,62 @@ +package org.apache.maven.archiva.indexer.query; + +/* + * Copyright 2005-2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Query for a single term. + * + * @author Brett Porter + */ +public class SingleTermQuery + implements Query +{ + /** + * The term to query for. + */ + private final QueryTerm term; + + /** + * Constructor. + * + * @param term the term to query + */ + public SingleTermQuery( QueryTerm term ) + { + this.term = term; + } + + /** + * Shorthand constructor - create a single term query from a field and value + * + * @param field the field name + * @param value the value to check for + */ + public SingleTermQuery( String field, String value ) + { + this.term = new QueryTerm( field, value ); + } + + public String getField() + { + return term.getField(); + } + + public String getValue() + { + return term.getValue(); + } +} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/record/AbstractArtifactIndexRecordFactory.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/record/AbstractArtifactIndexRecordFactory.java new file mode 100644 index 000000000..4984bb745 --- /dev/null +++ b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/record/AbstractArtifactIndexRecordFactory.java @@ -0,0 +1,98 @@ +package org.apache.maven.archiva.indexer.record; + +/* + * 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.archiva.digest.Digester; +import org.apache.maven.archiva.digest.DigesterException; +import org.codehaus.plexus.logging.AbstractLogEnabled; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.List; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + +/** + * Base class for the index record factories. + * + * @author Brett Porter + */ +public abstract class AbstractArtifactIndexRecordFactory + extends AbstractLogEnabled + implements RepositoryIndexRecordFactory +{ + protected String readChecksum( File file, Digester digester ) + { + String checksum; + try + { + checksum = digester.calc( file ).toLowerCase(); + } + catch ( DigesterException e ) + { + getLogger().error( "Error getting checksum for artifact file, leaving empty in index: " + e.getMessage() ); + checksum = null; + } + return checksum; + } + + protected List readFilesInArchive( File file ) + throws IOException + { + ZipFile zipFile = new ZipFile( file ); + List files; + try + { + files = new ArrayList( zipFile.size() ); + + for ( Enumeration entries = zipFile.entries(); entries.hasMoreElements(); ) + { + ZipEntry entry = (ZipEntry) entries.nextElement(); + + files.add( entry.getName() ); + } + } + finally + { + closeQuietly( zipFile ); + } + return files; + } + + protected static boolean isClass( String name ) + { + // TODO: verify if class is public or protected (this might require the original ZipEntry) + return name.endsWith( ".class" ) && name.lastIndexOf( "$" ) < 0; + } + + protected static void closeQuietly( ZipFile zipFile ) + { + try + { + if ( zipFile != null ) + { + zipFile.close(); + } + } + catch ( IOException e ) + { + // ignored + } + } +} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/record/MinimalArtifactIndexRecord.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/record/MinimalArtifactIndexRecord.java new file mode 100644 index 000000000..8a1e49d39 --- /dev/null +++ b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/record/MinimalArtifactIndexRecord.java @@ -0,0 +1,170 @@ +package org.apache.maven.archiva.indexer.record; + +import java.util.Date; +import java.util.List; + +/* + * 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. + */ + +/** + * The a record with the fields in the minimal index. + * + * @author Brett Porter + */ +public class MinimalArtifactIndexRecord + implements RepositoryIndexRecord +{ + /** + * The classes in the archive for the artifact, if it is a JAR. + */ + private List classes; + + /** + * The MD5 checksum of the artifact file. + */ + private String md5Checksum; + + /** + * The filename of the artifact file (no path). + */ + private String filename; + + /** + * The timestamp that the artifact file was last modified. Granularity is seconds. + */ + private long lastModified; + + /** + * The size of the artifact file in bytes. + */ + private long size; + + private static final int MS_PER_SEC = 1000; + + public void setClasses( List classes ) + { + this.classes = classes; + } + + public void setMd5Checksum( String md5Checksum ) + { + this.md5Checksum = md5Checksum; + } + + public void setFilename( String filename ) + { + this.filename = filename; + } + + public void setLastModified( long lastModified ) + { + this.lastModified = lastModified - lastModified % MS_PER_SEC; + } + + public void setSize( long size ) + { + this.size = size; + } + + public List getClasses() + { + return classes; + } + + public String getMd5Checksum() + { + return md5Checksum; + } + + public String getFilename() + { + return filename; + } + + public long getLastModified() + { + return lastModified; + } + + public long getSize() + { + return size; + } + + /** + * @noinspection RedundantIfStatement + */ + public boolean equals( Object obj ) + { + if ( this == obj ) + { + return true; + } + if ( obj == null || getClass() != obj.getClass() ) + { + return false; + } + + MinimalArtifactIndexRecord that = (MinimalArtifactIndexRecord) obj; + + if ( lastModified != that.lastModified ) + { + return false; + } + if ( size != that.size ) + { + return false; + } + if ( classes != null ? !classes.equals( that.classes ) : that.classes != null ) + { + return false; + } + if ( !filename.equals( that.filename ) ) + { + return false; + } + if ( md5Checksum != null ? !md5Checksum.equals( that.md5Checksum ) : that.md5Checksum != null ) + { + return false; + } + + return true; + } + + /** + * @noinspection UnnecessaryParentheses + */ + public int hashCode() + { + int result = classes != null ? classes.hashCode() : 0; + result = 31 * result + ( md5Checksum != null ? md5Checksum.hashCode() : 0 ); + result = 31 * result + filename.hashCode(); + result = 31 * result + (int) ( lastModified ^ ( lastModified >>> 32 ) ); + result = 31 * result + (int) ( size ^ ( size >>> 32 ) ); + return result; + } + + public String toString() + { + return "Filename: " + filename + "; checksum: " + md5Checksum + "; size: " + size + "; lastModified: " + + new Date( lastModified ) + "; classes: " + classes; + } + + public String getPrimaryKey() + { + return filename; + } +} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/record/MinimalArtifactIndexRecordFactory.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/record/MinimalArtifactIndexRecordFactory.java new file mode 100644 index 000000000..1c0062357 --- /dev/null +++ b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/record/MinimalArtifactIndexRecordFactory.java @@ -0,0 +1,102 @@ +package org.apache.maven.archiva.indexer.record; + +/* + * 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.archiva.digest.Digester; +import org.apache.maven.artifact.Artifact; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +/** + * An index record type for the minimal index. + * + * @author Edwin Punzalan + * @author Brett Porter + * @plexus.component role="org.apache.maven.archiva.indexer.record.RepositoryIndexRecordFactory" role-hint="minimal" + */ +public class MinimalArtifactIndexRecordFactory + extends AbstractArtifactIndexRecordFactory +{ + /* List of types to index. */ + private static final Set INDEXED_TYPES = new HashSet( Arrays.asList( new String[]{"jar", "maven-plugin"} ) ); + + /** + * @plexus.requirement role-hint="sha1" + */ + protected Digester sha1Digester; + + /** + * @plexus.requirement role-hint="md5" + */ + protected Digester md5Digester; + + public RepositoryIndexRecord createRecord( Artifact artifact ) + { + MinimalArtifactIndexRecord record = null; + + File file = artifact.getFile(); + if ( file != null && INDEXED_TYPES.contains( artifact.getType() ) && file.exists() ) + { + String md5 = readChecksum( file, md5Digester ); + + List files = null; + try + { + files = readFilesInArchive( file ); + } + catch ( IOException e ) + { + getLogger().error( "Error reading artifact file, omitting from index: " + e.getMessage() ); + } + + if ( files != null ) + { + record = new MinimalArtifactIndexRecord(); + record.setMd5Checksum( md5 ); + record.setFilename( artifact.getRepository().pathOf( artifact ) ); + record.setLastModified( file.lastModified() ); + record.setSize( file.length() ); + record.setClasses( getClassesFromFiles( files ) ); + } + } + return record; + } + + private List getClassesFromFiles( List files ) + { + List classes = new ArrayList(); + + for ( Iterator i = files.iterator(); i.hasNext(); ) + { + String name = (String) i.next(); + + if ( isClass( name ) ) + { + classes.add( name.substring( 0, name.length() - 6 ).replace( '/', '.' ) ); + } + } + + return classes; + } +} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/record/MinimalIndexRecordFields.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/record/MinimalIndexRecordFields.java new file mode 100644 index 000000000..6c1249463 --- /dev/null +++ b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/record/MinimalIndexRecordFields.java @@ -0,0 +1,41 @@ +package org.apache.maven.archiva.indexer.record; + +/* + * 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. + */ + +/** + * The fields in a minimal artifact index record. + * + * @author Brett Porter + * @todo should be an enum + */ +public class MinimalIndexRecordFields +{ + public static final String FILENAME = "j"; + + public static final String LAST_MODIFIED = "d"; + + public static final String FILE_SIZE = "s"; + + public static final String MD5 = "m"; + + public static final String CLASSES = "c"; + + private MinimalIndexRecordFields() + { + // No touchy! + } +} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/record/RepositoryIndexRecord.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/record/RepositoryIndexRecord.java new file mode 100644 index 000000000..468c1ca00 --- /dev/null +++ b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/record/RepositoryIndexRecord.java @@ -0,0 +1,32 @@ +package org.apache.maven.archiva.indexer.record; + +/* + * 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. + */ + +/** + * A repository index record. + * + * @author Brett Porter + */ +public interface RepositoryIndexRecord +{ + /** + * Get the primary key used to identify the record uniquely in the index. + * + * @return the primary key + */ + String getPrimaryKey(); +} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/record/RepositoryIndexRecordFactory.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/record/RepositoryIndexRecordFactory.java new file mode 100644 index 000000000..aedeaef39 --- /dev/null +++ b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/record/RepositoryIndexRecordFactory.java @@ -0,0 +1,44 @@ +package org.apache.maven.archiva.indexer.record; + +/* + * 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.archiva.indexer.RepositoryIndexException; +import org.apache.maven.artifact.Artifact; + +/** + * The layout of a record in a repository index. + * + * @author Brett Porter + */ +public interface RepositoryIndexRecordFactory +{ + /** + * The Plexus role. + */ + String ROLE = RepositoryIndexRecordFactory.class.getName(); + + /** + * Create an index record from an artifact. + * + * @param artifact the artifact + * @return the index record + * @throws RepositoryIndexException if there is a problem constructing the record (due to not being able to read the artifact file as a POM) + */ + RepositoryIndexRecord createRecord( Artifact artifact ) + throws RepositoryIndexException; + +} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/record/StandardArtifactIndexRecord.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/record/StandardArtifactIndexRecord.java new file mode 100644 index 000000000..ae60fcfa2 --- /dev/null +++ b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/record/StandardArtifactIndexRecord.java @@ -0,0 +1,339 @@ +package org.apache.maven.archiva.indexer.record; + +import java.util.List; + +/* + * 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. + */ + +/** + * The a record with the fields in the standard index. + * + * @author Brett Porter + */ +public class StandardArtifactIndexRecord + extends MinimalArtifactIndexRecord +{ + /** + * The SHA-1 checksum of the artifact file. + */ + private String sha1Checksum; + + /** + * The artifact's group. + */ + private String groupId; + + /** + * The artifact's identifier within the group. + */ + private String artifactId; + + /** + * The artifact's version. + */ + private String version; + + /** + * The classifier, if there is one. + */ + private String classifier; + + /** + * The artifact type (from the file). + */ + private String type; + + /** + * A list of files (separated by '\n') in the artifact if it is an archive. + */ + private List files; + + /** + * The identifier of the repository that the artifact came from. + */ + private String repository; + + /** + * The packaging specified in the POM for this artifact. + */ + private String packaging; + + /** + * The plugin prefix specified in the metadata if the artifact is a plugin. + */ + private String pluginPrefix; + + /** + * The year the project was started. + */ + private String inceptionYear; + + /** + * The description of the project. + */ + private String projectDescription; + + /** + * The name of the project. + */ + private String projectName; + + /** + * The base version (before the snapshot is determined). + */ + private String baseVersion; + + public void setSha1Checksum( String sha1Checksum ) + { + this.sha1Checksum = sha1Checksum; + } + + public void setGroupId( String groupId ) + { + this.groupId = groupId; + } + + public void setArtifactId( String artifactId ) + { + this.artifactId = artifactId; + } + + public void setVersion( String version ) + { + this.version = version; + } + + public void setClassifier( String classifier ) + { + this.classifier = classifier; + } + + public void setType( String type ) + { + this.type = type; + } + + public void setFiles( List files ) + { + this.files = files; + } + + public void setRepository( String repository ) + { + this.repository = repository; + } + + /** + * @noinspection RedundantIfStatement + */ + public boolean equals( Object obj ) + { + if ( this == obj ) + { + return true; + } + if ( obj == null || getClass() != obj.getClass() ) + { + return false; + } + if ( !super.equals( obj ) ) + { + return false; + } + + StandardArtifactIndexRecord that = (StandardArtifactIndexRecord) obj; + + if ( !artifactId.equals( that.artifactId ) ) + { + return false; + } + if ( classifier != null ? !classifier.equals( that.classifier ) : that.classifier != null ) + { + return false; + } + if ( files != null ? !files.equals( that.files ) : that.files != null ) + { + return false; + } + if ( !groupId.equals( that.groupId ) ) + { + return false; + } + if ( repository != null ? !repository.equals( that.repository ) : that.repository != null ) + { + return false; + } + if ( sha1Checksum != null ? !sha1Checksum.equals( that.sha1Checksum ) : that.sha1Checksum != null ) + { + return false; + } + if ( type != null ? !type.equals( that.type ) : that.type != null ) + { + return false; + } + if ( !version.equals( that.version ) ) + { + return false; + } + if ( !baseVersion.equals( that.baseVersion ) ) + { + return false; + } + if ( packaging != null ? !packaging.equals( that.packaging ) : that.packaging != null ) + { + return false; + } + if ( pluginPrefix != null ? !pluginPrefix.equals( that.pluginPrefix ) : that.pluginPrefix != null ) + { + return false; + } + if ( projectName != null ? !projectName.equals( that.projectName ) : that.projectName != null ) + { + return false; + } + if ( inceptionYear != null ? !inceptionYear.equals( that.inceptionYear ) : that.inceptionYear != null ) + { + return false; + } + if ( projectDescription != null ? !projectDescription.equals( that.projectDescription ) + : that.projectDescription != null ) + { + return false; + } + + return true; + } + + public int hashCode() + { + int result = super.hashCode(); + result = 31 * result + ( sha1Checksum != null ? sha1Checksum.hashCode() : 0 ); + result = 31 * result + groupId.hashCode(); + result = 31 * result + artifactId.hashCode(); + result = 31 * result + version.hashCode(); + result = 31 * result + baseVersion.hashCode(); + result = 31 * result + ( classifier != null ? classifier.hashCode() : 0 ); + result = 31 * result + ( type != null ? type.hashCode() : 0 ); + result = 31 * result + ( files != null ? files.hashCode() : 0 ); + result = 31 * result + ( repository != null ? repository.hashCode() : 0 ); + result = 31 * result + ( packaging != null ? packaging.hashCode() : 0 ); + result = 31 * result + ( pluginPrefix != null ? pluginPrefix.hashCode() : 0 ); + result = 31 * result + ( inceptionYear != null ? inceptionYear.hashCode() : 0 ); + result = 31 * result + ( projectName != null ? projectName.hashCode() : 0 ); + result = 31 * result + ( projectDescription != null ? projectDescription.hashCode() : 0 ); + return result; + } + + public String getSha1Checksum() + { + return sha1Checksum; + } + + public String getGroupId() + { + return groupId; + } + + public String getArtifactId() + { + return artifactId; + } + + public String getVersion() + { + return version; + } + + public String getClassifier() + { + return classifier; + } + + public String getType() + { + return type; + } + + public List getFiles() + { + return files; + } + + public String getRepository() + { + return repository; + } + + public String getPackaging() + { + return packaging; + } + + public String getPluginPrefix() + { + return pluginPrefix; + } + + public void setPackaging( String packaging ) + { + this.packaging = packaging; + } + + public void setPluginPrefix( String pluginPrefix ) + { + this.pluginPrefix = pluginPrefix; + } + + public void setInceptionYear( String inceptionYear ) + { + this.inceptionYear = inceptionYear; + } + + public void setProjectDescription( String description ) + { + this.projectDescription = description; + } + + public void setProjectName( String projectName ) + { + this.projectName = projectName; + } + + public String getInceptionYear() + { + return inceptionYear; + } + + public String getProjectDescription() + { + return projectDescription; + } + + public String getProjectName() + { + return projectName; + } + + public void setBaseVersion( String baseVersion ) + { + this.baseVersion = baseVersion; + } + + public String getBaseVersion() + { + return baseVersion; + } +} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/record/StandardArtifactIndexRecordFactory.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/record/StandardArtifactIndexRecordFactory.java new file mode 100644 index 000000000..d97a1d6cf --- /dev/null +++ b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/record/StandardArtifactIndexRecordFactory.java @@ -0,0 +1,302 @@ +package org.apache.maven.archiva.indexer.record; + +/* + * 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.archiva.digest.Digester; +import org.apache.maven.archiva.indexer.RepositoryIndexException; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.factory.ArtifactFactory; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.model.Model; +import org.apache.maven.project.MavenProject; +import org.apache.maven.project.MavenProjectBuilder; +import org.apache.maven.project.ProjectBuildingException; +import org.codehaus.plexus.util.xml.Xpp3Dom; +import org.codehaus.plexus.util.xml.Xpp3DomBuilder; +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; + +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; +import java.util.zip.ZipEntry; +import java.util.zip.ZipException; +import java.util.zip.ZipFile; + +/** + * An index record type for the standard index. + * + * @author Edwin Punzalan + * @author Brett Porter + * @plexus.component role="org.apache.maven.archiva.indexer.record.RepositoryIndexRecordFactory" role-hint="standard" + */ +public class StandardArtifactIndexRecordFactory + extends AbstractArtifactIndexRecordFactory +{ + /** + * A list of artifact types to treat as a zip archive. + * + * @todo this should be smarter (perhaps use plexus archiver to look for an unarchiver, and make the ones for zip configurable since sar, par, etc can be added at random. + */ + private static final Set ARCHIVE_TYPES = + new HashSet( Arrays.asList( new String[]{"jar", "ejb", "par", "sar", "war", "ear", "rar"} ) ); + + /** + * @plexus.requirement + */ + private ArtifactFactory artifactFactory; + + /** + * @plexus.requirement + */ + private MavenProjectBuilder projectBuilder; + + /** + * @plexus.requirement role-hint="sha1" + */ + protected Digester sha1Digester; + + /** + * @plexus.requirement role-hint="md5" + */ + protected Digester md5Digester; + + private static final String PLUGIN_METADATA_NAME = "META-INF/maven/plugin.xml"; + + private static final String ARCHETYPE_METADATA_NAME = "META-INF/maven/archetype.xml"; + + // some current/old archetypes have the archetype.xml at different location. + private static final String ARCHETYPE_METADATA_NAME_OLD = "META-INF/archetype.xml"; + + public RepositoryIndexRecord createRecord( Artifact artifact ) + throws RepositoryIndexException + { + StandardArtifactIndexRecord record = null; + + File file = artifact.getFile(); + + // TODO: is this condition really a possibility? + if ( file != null && file.exists() ) + { + String md5 = readChecksum( file, md5Digester ); + String sha1 = readChecksum( file, sha1Digester ); + + List files = null; + boolean archive = ARCHIVE_TYPES.contains( artifact.getType() ); + try + { + if ( archive ) + { + files = readFilesInArchive( file ); + } + } + catch ( IOException e ) + { + getLogger().error( "Error reading artifact file, omitting from index: " + e.getMessage() ); + } + + // If it's an archive with no files, don't create a record + if ( !archive || files != null ) + { + record = new StandardArtifactIndexRecord(); + + record.setGroupId( artifact.getGroupId() ); + record.setArtifactId( artifact.getArtifactId() ); + record.setBaseVersion( artifact.getBaseVersion() ); + record.setVersion( artifact.getVersion() ); + record.setClassifier( artifact.getClassifier() ); + record.setType( artifact.getType() ); + record.setMd5Checksum( md5 ); + record.setSha1Checksum( sha1 ); + record.setFilename( artifact.getRepository().pathOf( artifact ) ); + record.setLastModified( file.lastModified() ); + record.setSize( file.length() ); + record.setRepository( artifact.getRepository().getId() ); + if ( files != null ) + { + populateArchiveEntries( files, record, artifact.getFile() ); + } + + if ( !"pom".equals( artifact.getType() ) ) + { + Artifact pomArtifact = artifactFactory.createProjectArtifact( artifact.getGroupId(), + artifact.getArtifactId(), + artifact.getVersion() ); + pomArtifact.isSnapshot(); // gross hack around bug in maven-artifact + File pomFile = new File( artifact.getRepository().getBasedir(), + artifact.getRepository().pathOf( pomArtifact ) ); + if ( pomFile.exists() ) + { + try + { + populatePomEntries( readPom( pomArtifact, artifact.getRepository() ), record ); + } + catch ( ProjectBuildingException e ) + { + getLogger().error( "Error reading POM file, not populating in index: " + e.getMessage() ); + } + } + } + else + { + Model model; + try + { + model = readPom( artifact, artifact.getRepository() ); + + if ( !"pom".equals( model.getPackaging() ) ) + { + // Don't return a record for a POM that is does not belong on its own + record = null; + } + else + { + populatePomEntries( model, record ); + } + } + catch ( ProjectBuildingException e ) + { + getLogger().error( "Error reading POM file, not populating in index: " + e.getMessage() ); + } + } + } + } + + return record; + } + + private void populatePomEntries( Model pom, StandardArtifactIndexRecord record ) + { + record.setPackaging( pom.getPackaging() ); + record.setProjectName( pom.getName() ); + record.setProjectDescription( pom.getDescription() ); + record.setInceptionYear( pom.getInceptionYear() ); + +/* TODO: fields for later + indexPlugins( doc, FLD_PLUGINS_BUILD, pom.getBuild().getPlugins().iterator() ); + indexReportPlugins( doc, FLD_PLUGINS_REPORT, pom.getReporting().getPlugins().iterator() ); + record.setDependencies( dependencies ); + record.setLicenses( licenses ); +*/ + } + + private Model readPom( Artifact artifact, ArtifactRepository repository ) + throws RepositoryIndexException, ProjectBuildingException + { + // TODO: this can create a -SNAPSHOT.pom when it didn't exist and a timestamped one did. This is harmless, but should be avoided + // TODO: will this pollute with local repo metadata? + MavenProject project = projectBuilder.buildFromRepository( artifact, Collections.EMPTY_LIST, repository ); + return project.getModel(); + } + + private void populateArchiveEntries( List files, StandardArtifactIndexRecord record, File artifactFile ) + throws RepositoryIndexException + { + List classes = new ArrayList(); + List fileList = new ArrayList(); + + for ( Iterator i = files.iterator(); i.hasNext(); ) + { + String name = (String) i.next(); + + // ignore directories + if ( !name.endsWith( "/" ) ) + { + fileList.add( name ); + + if ( isClass( name ) ) + { + classes.add( name.substring( 0, name.length() - 6 ).replace( '/', '.' ) ); + } + else if ( PLUGIN_METADATA_NAME.equals( name ) ) + { + populatePluginEntries( readXmlMetadataFileInJar( artifactFile, PLUGIN_METADATA_NAME ), record ); + } + else if ( ARCHETYPE_METADATA_NAME.equals( name ) || ARCHETYPE_METADATA_NAME_OLD.equals( name ) ) + { + populateArchetypeEntries( record ); + } + } + } + + if ( !classes.isEmpty() ) + { + record.setClasses( classes ); + } + if ( !fileList.isEmpty() ) + { + record.setFiles( fileList ); + } + } + + private void populateArchetypeEntries( StandardArtifactIndexRecord record ) + { + // Typically discovered as a JAR + record.setType( "maven-archetype" ); + } + + private Xpp3Dom readXmlMetadataFileInJar( File file, String name ) + throws RepositoryIndexException + { + // TODO: would be more efficient with original ZipEntry still around + + Xpp3Dom xpp3Dom; + ZipFile zipFile = null; + try + { + zipFile = new ZipFile( file ); + ZipEntry entry = zipFile.getEntry( name ); + xpp3Dom = Xpp3DomBuilder.build( new InputStreamReader( zipFile.getInputStream( entry ) ) ); + } + catch ( ZipException e ) + { + throw new RepositoryIndexException( "Unable to read plugin metadata: " + e.getMessage(), e ); + } + catch ( IOException e ) + { + throw new RepositoryIndexException( "Unable to read plugin metadata: " + e.getMessage(), e ); + } + catch ( XmlPullParserException e ) + { + throw new RepositoryIndexException( "Unable to read plugin metadata: " + e.getMessage(), e ); + } + finally + { + closeQuietly( zipFile ); + } + return xpp3Dom; + } + + public void populatePluginEntries( Xpp3Dom metadata, StandardArtifactIndexRecord record ) + { + // Typically discovered as a JAR + record.setType( "maven-plugin" ); + + Xpp3Dom prefix = metadata.getChild( "goalPrefix" ); + + if ( prefix != null ) + { + record.setPluginPrefix( prefix.getValue() ); + } + } +} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/record/StandardIndexRecordFields.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/record/StandardIndexRecordFields.java new file mode 100644 index 000000000..437bac350 --- /dev/null +++ b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexer/record/StandardIndexRecordFields.java @@ -0,0 +1,77 @@ +package org.apache.maven.archiva.indexer.record; + +/* + * 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. + */ + +/** + * The fields in a minimal artifact index record. + * + * @author Brett Porter + * @todo should be an enum + */ +public class StandardIndexRecordFields +{ + public static final String FILENAME = "filename"; + + public static final String GROUPID = "groupId"; + + public static final String GROUPID_EXACT = GROUPID + "_u"; + + public static final String ARTIFACTID = "artifactId"; + + public static final String ARTIFACTID_EXACT = ARTIFACTID + "_u"; + + public static final String VERSION = "version"; + + public static final String VERSION_EXACT = VERSION + "_u"; + + public static final String BASE_VERSION = "baseVersion"; + + public static final String BASE_VERSION_EXACT = BASE_VERSION + "_u"; + + public static final String TYPE = "type"; + + public static final String CLASSIFIER = "classifier"; + + public static final String PACKAGING = "packaging"; + + public static final String REPOSITORY = "repo"; + + public static final String LAST_MODIFIED = "lastModified"; + + public static final String FILE_SIZE = "fileSize"; + + public static final String MD5 = "md5"; + + public static final String SHA1 = "sha1"; + + public static final String CLASSES = "classes"; + + public static final String PLUGIN_PREFIX = "pluginPrefix"; + + public static final String FILES = "files"; + + public static final String INCEPTION_YEAR = "inceptionYear"; + + public static final String PROJECT_NAME = "projectName"; + + public static final String PROJECT_DESCRIPTION = "projectDesc"; + + private StandardIndexRecordFields() + { + // No touchy! + } +} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/RepositoryArtifactIndex.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/RepositoryArtifactIndex.java deleted file mode 100644 index b02ce2884..000000000 --- a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/RepositoryArtifactIndex.java +++ /dev/null @@ -1,69 +0,0 @@ -package org.apache.maven.archiva.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.archiva.indexing.query.Query; - -import java.util.Collection; -import java.util.List; - -/** - * Maintain an artifact index on the repository. - * - * @author Brett Porter - */ -public interface RepositoryArtifactIndex -{ - /** - * Indexes the artifacts found within the specified list of index records. If the artifacts are already in the - * repository they are updated. - * - * @param records the artifacts to index - * @throws RepositoryIndexException if there is a problem indexing the records - */ - void indexRecords( Collection records ) - throws RepositoryIndexException; - - /** - * Search the index based on the search criteria specified. Returns a list of index records. - * - * @param query The query that contains the search criteria - * @return the index records found - * @throws RepositoryIndexSearchException if there is a problem searching - * @todo should it return "SearchResult" instances that contain the index record and other search data (like score?) - */ - List search( Query query ) - throws RepositoryIndexSearchException; - - /** - * Check if the index already exists. - * - * @return true if the index already exists - * @throws RepositoryIndexException if the index location is not valid - */ - boolean exists() - throws RepositoryIndexException; - - /** - * Delete records from the index. Simply ignore the request any did not exist. - * - * @param records the records to delete - * @throws RepositoryIndexException if there is a problem removing the record - */ - void deleteRecords( Collection records ) - throws RepositoryIndexException; -} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/RepositoryArtifactIndexFactory.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/RepositoryArtifactIndexFactory.java deleted file mode 100644 index df9f08d55..000000000 --- a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/RepositoryArtifactIndexFactory.java +++ /dev/null @@ -1,48 +0,0 @@ -package org.apache.maven.archiva.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 java.io.File; - -/** - * Obtain an index instance. - * - * @author Brett Porter - */ -public interface RepositoryArtifactIndexFactory -{ - /** - * Plexus role. - */ - String ROLE = RepositoryArtifactIndexFactory.class.getName(); - - /** - * Method to create an instance of the standard index. - * - * @param indexPath the path where the index will be created/updated - * @return the index instance - */ - RepositoryArtifactIndex createStandardIndex( File indexPath ); - - /** - * Method to create an instance of the minimal index. - * - * @param indexPath the path where the index will be created/updated - * @return the index instance - */ - RepositoryArtifactIndex createMinimalIndex( File indexPath ); -} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/RepositoryIndexException.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/RepositoryIndexException.java deleted file mode 100644 index b44560777..000000000 --- a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/RepositoryIndexException.java +++ /dev/null @@ -1,34 +0,0 @@ -package org.apache.maven.archiva.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. - */ - -/** - * @author Edwin Punzalan - */ -public class RepositoryIndexException - extends Exception -{ - public RepositoryIndexException( String message, Throwable cause ) - { - super( message, cause ); - } - - public RepositoryIndexException( String message ) - { - super( message ); - } -} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/RepositoryIndexSearchException.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/RepositoryIndexSearchException.java deleted file mode 100644 index 590efb66e..000000000 --- a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/RepositoryIndexSearchException.java +++ /dev/null @@ -1,29 +0,0 @@ -package org.apache.maven.archiva.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. - */ - -/** - * @author Brett Porter - */ -public class RepositoryIndexSearchException - extends Exception -{ - public RepositoryIndexSearchException( String message, Throwable cause ) - { - super( message, cause ); - } -} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/lucene/LuceneIndexRecordConverter.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/lucene/LuceneIndexRecordConverter.java deleted file mode 100644 index 225862910..000000000 --- a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/lucene/LuceneIndexRecordConverter.java +++ /dev/null @@ -1,48 +0,0 @@ -package org.apache.maven.archiva.indexing.lucene; - -/* - * Copyright 2005-2006 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import org.apache.lucene.document.Document; -import org.apache.maven.archiva.indexing.record.RepositoryIndexRecord; - -import java.text.ParseException; - -/** - * Converts repository records to Lucene documents. - * - * @author Brett Porter - */ -public interface LuceneIndexRecordConverter -{ - /** - * Convert an index record to a Lucene document. - * - * @param record the record - * @return the document - */ - Document convert( RepositoryIndexRecord record ); - - /** - * Convert a Lucene document to an index record. - * - * @param document the document - * @return the record - * @throws java.text.ParseException if there is a problem parsing a field (specifically, dates) - */ - RepositoryIndexRecord convert( Document document ) - throws ParseException; -} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/lucene/LuceneMinimalIndexRecordConverter.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/lucene/LuceneMinimalIndexRecordConverter.java deleted file mode 100644 index d67b4bfaf..000000000 --- a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/lucene/LuceneMinimalIndexRecordConverter.java +++ /dev/null @@ -1,84 +0,0 @@ -package org.apache.maven.archiva.indexing.lucene; - -/* - * Copyright 2005-2006 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import org.apache.lucene.document.DateTools; -import org.apache.lucene.document.Document; -import org.apache.lucene.document.Field; -import org.apache.lucene.document.NumberTools; -import org.apache.maven.archiva.indexing.record.MinimalArtifactIndexRecord; -import org.apache.maven.archiva.indexing.record.MinimalIndexRecordFields; -import org.apache.maven.archiva.indexing.record.RepositoryIndexRecord; -import org.codehaus.plexus.util.StringUtils; - -import java.text.ParseException; -import java.util.Arrays; - -/** - * Convert the minimal index record to a Lucene document. - * - * @author Brett Porter - */ -public class LuceneMinimalIndexRecordConverter - implements LuceneIndexRecordConverter -{ - public Document convert( RepositoryIndexRecord record ) - { - MinimalArtifactIndexRecord rec = (MinimalArtifactIndexRecord) record; - - Document document = new Document(); - addTokenizedField( document, MinimalIndexRecordFields.FILENAME, rec.getFilename() ); - addUntokenizedField( document, MinimalIndexRecordFields.LAST_MODIFIED, - DateTools.timeToString( rec.getLastModified(), DateTools.Resolution.SECOND ) ); - addUntokenizedField( document, MinimalIndexRecordFields.FILE_SIZE, NumberTools.longToString( rec.getSize() ) ); - addUntokenizedField( document, MinimalIndexRecordFields.MD5, rec.getMd5Checksum() ); - addTokenizedField( document, MinimalIndexRecordFields.CLASSES, - StringUtils.join( rec.getClasses().iterator(), "\n" ) ); - - return document; - } - - public RepositoryIndexRecord convert( Document document ) - throws ParseException - { - MinimalArtifactIndexRecord record = new MinimalArtifactIndexRecord(); - - record.setFilename( document.get( MinimalIndexRecordFields.FILENAME ) ); - record.setLastModified( DateTools.stringToTime( document.get( MinimalIndexRecordFields.LAST_MODIFIED ) ) ); - record.setSize( NumberTools.stringToLong( document.get( MinimalIndexRecordFields.FILE_SIZE ) ) ); - record.setMd5Checksum( document.get( MinimalIndexRecordFields.MD5 ) ); - record.setClasses( Arrays.asList( document.get( MinimalIndexRecordFields.CLASSES ).split( "\n" ) ) ); - - return record; - } - - private static void addUntokenizedField( Document document, String name, String value ) - { - if ( value != null ) - { - document.add( new Field( name, value, Field.Store.YES, Field.Index.UN_TOKENIZED ) ); - } - } - - private static void addTokenizedField( Document document, String name, String value ) - { - if ( value != null ) - { - document.add( new Field( name, value, Field.Store.YES, Field.Index.TOKENIZED ) ); - } - } -} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/lucene/LuceneQuery.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/lucene/LuceneQuery.java deleted file mode 100644 index 63c665913..000000000 --- a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/lucene/LuceneQuery.java +++ /dev/null @@ -1,40 +0,0 @@ -package org.apache.maven.archiva.indexing.lucene; - -/* - * 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.archiva.indexing.query.Query; - -/** - * A holder for a lucene query to pass to the indexer API. - * - * @author Brett Porter - */ -public class LuceneQuery - implements Query -{ - private final org.apache.lucene.search.Query query; - - public LuceneQuery( org.apache.lucene.search.Query query ) - { - this.query = query; - } - - org.apache.lucene.search.Query getLuceneQuery() - { - return query; - } -} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/lucene/LuceneRepositoryArtifactIndex.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/lucene/LuceneRepositoryArtifactIndex.java deleted file mode 100644 index 12803681d..000000000 --- a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/lucene/LuceneRepositoryArtifactIndex.java +++ /dev/null @@ -1,276 +0,0 @@ -package org.apache.maven.archiva.indexing.lucene; - -/* - * Copyright 2005-2006 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import org.apache.lucene.analysis.Analyzer; -import org.apache.lucene.analysis.standard.StandardAnalyzer; -import org.apache.lucene.document.Document; -import org.apache.lucene.document.Field; -import org.apache.lucene.index.IndexReader; -import org.apache.lucene.index.IndexWriter; -import org.apache.lucene.index.Term; -import org.apache.lucene.search.Hits; -import org.apache.lucene.search.IndexSearcher; -import org.apache.maven.archiva.indexing.RepositoryArtifactIndex; -import org.apache.maven.archiva.indexing.RepositoryIndexException; -import org.apache.maven.archiva.indexing.RepositoryIndexSearchException; -import org.apache.maven.archiva.indexing.query.Query; -import org.apache.maven.archiva.indexing.record.RepositoryIndexRecord; - -import java.io.File; -import java.io.IOException; -import java.text.ParseException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; - -/** - * Lucene implementation of a repository index. - * - * @author Brett Porter - */ -public class LuceneRepositoryArtifactIndex - implements RepositoryArtifactIndex -{ - /** - * The location of the index on the file system. - */ - private File indexLocation; - - /** - * Convert repository records to Lucene documents. - */ - private LuceneIndexRecordConverter converter; - - private static final String FLD_PK = "pk"; - - public LuceneRepositoryArtifactIndex( File indexPath, LuceneIndexRecordConverter converter ) - { - this.indexLocation = indexPath; - this.converter = converter; - } - - public void indexRecords( Collection records ) - throws RepositoryIndexException - { - deleteRecords( records ); - - addRecords( records ); - } - - private void addRecords( Collection records ) - throws RepositoryIndexException - { - IndexWriter indexWriter; - try - { - indexWriter = new IndexWriter( indexLocation, getAnalyzer(), !exists() ); - } - catch ( IOException e ) - { - throw new RepositoryIndexException( "Unable to open index", e ); - } - - try - { - for ( Iterator i = records.iterator(); i.hasNext(); ) - { - RepositoryIndexRecord record = (RepositoryIndexRecord) i.next(); - - if ( record != null ) - { - Document document = converter.convert( record ); - document.add( - new Field( FLD_PK, record.getPrimaryKey(), Field.Store.NO, Field.Index.UN_TOKENIZED ) ); - - indexWriter.addDocument( document ); - } - } - - indexWriter.optimize(); - } - catch ( IOException e ) - { - throw new RepositoryIndexException( "Failed to add an index document", e ); - } - finally - { - close( indexWriter ); - } - } - - private void close( IndexWriter indexWriter ) - throws RepositoryIndexException - { - try - { - if ( indexWriter != null ) - { - indexWriter.close(); - } - } - catch ( IOException e ) - { - throw new RepositoryIndexException( e.getMessage(), e ); - } - } - - private Analyzer getAnalyzer() - { - // TODO: investigate why changed in original! Probably for MD5 and number querying. - return new StandardAnalyzer(); - } - - public void deleteRecords( Collection records ) - throws RepositoryIndexException - { - if ( exists() ) - { - IndexReader indexReader = null; - try - { - indexReader = IndexReader.open( indexLocation ); - - for ( Iterator artifacts = records.iterator(); artifacts.hasNext(); ) - { - RepositoryIndexRecord record = (RepositoryIndexRecord) artifacts.next(); - - if ( record != null ) - { - Term term = new Term( FLD_PK, record.getPrimaryKey() ); - - indexReader.deleteDocuments( term ); - } - } - } - catch ( IOException e ) - { - throw new RepositoryIndexException( "Error deleting document: " + e.getMessage(), e ); - } - finally - { - if ( indexReader != null ) - { - closeQuietly( indexReader ); - } - } - } - } - - public boolean exists() - throws RepositoryIndexException - { - if ( IndexReader.indexExists( indexLocation ) ) - { - return true; - } - else if ( !indexLocation.exists() ) - { - return false; - } - else if ( indexLocation.isDirectory() ) - { - if ( indexLocation.listFiles().length > 1 ) - { - throw new RepositoryIndexException( indexLocation + " is not a valid index directory." ); - } - else - { - return false; - } - } - else - { - throw new RepositoryIndexException( indexLocation + " is not a directory." ); - } - } - - public List search( Query query ) - throws RepositoryIndexSearchException - { - LuceneQuery lQuery = (LuceneQuery) query; - - org.apache.lucene.search.Query luceneQuery = lQuery.getLuceneQuery(); - - IndexSearcher searcher; - try - { - searcher = new IndexSearcher( indexLocation.getAbsolutePath() ); - } - catch ( IOException e ) - { - throw new RepositoryIndexSearchException( "Unable to open index: " + e.getMessage(), e ); - } - - List records = new ArrayList(); - try - { - Hits hits = searcher.search( luceneQuery ); - for ( int i = 0; i < hits.length(); i++ ) - { - Document doc = hits.doc( i ); - - records.add( converter.convert( doc ) ); - } - } - catch ( IOException e ) - { - throw new RepositoryIndexSearchException( "Unable to search index: " + e.getMessage(), e ); - } - catch ( ParseException e ) - { - throw new RepositoryIndexSearchException( "Unable to search index: " + e.getMessage(), e ); - } - finally - { - closeQuietly( searcher ); - } - - return records; - } - - private static void closeQuietly( IndexSearcher searcher ) - { - try - { - if ( searcher != null ) - { - searcher.close(); - } - } - catch ( IOException e ) - { - // ignore - } - } - - private static void closeQuietly( IndexReader reader ) - { - try - { - if ( reader != null ) - { - reader.close(); - } - } - catch ( IOException e ) - { - // ignore - } - } -} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/lucene/LuceneRepositoryArtifactIndexFactory.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/lucene/LuceneRepositoryArtifactIndexFactory.java deleted file mode 100644 index 42b23c475..000000000 --- a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/lucene/LuceneRepositoryArtifactIndexFactory.java +++ /dev/null @@ -1,42 +0,0 @@ -package org.apache.maven.archiva.indexing.lucene; - -/* - * 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.archiva.indexing.RepositoryArtifactIndex; -import org.apache.maven.archiva.indexing.RepositoryArtifactIndexFactory; - -import java.io.File; - -/** - * Factory for Lucene artifact index instances. - * - * @author Brett Porter - * @plexus.component role="org.apache.maven.archiva.indexing.RepositoryArtifactIndexFactory" role-hint="lucene" - */ -public class LuceneRepositoryArtifactIndexFactory - implements RepositoryArtifactIndexFactory -{ - public RepositoryArtifactIndex createStandardIndex( File indexPath ) - { - return new LuceneRepositoryArtifactIndex( indexPath, new LuceneStandardIndexRecordConverter() ); - } - - public RepositoryArtifactIndex createMinimalIndex( File indexPath ) - { - return new LuceneRepositoryArtifactIndex( indexPath, new LuceneMinimalIndexRecordConverter() ); - } -} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/lucene/LuceneStandardIndexRecordConverter.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/lucene/LuceneStandardIndexRecordConverter.java deleted file mode 100644 index dafc01500..000000000 --- a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/lucene/LuceneStandardIndexRecordConverter.java +++ /dev/null @@ -1,145 +0,0 @@ -package org.apache.maven.archiva.indexing.lucene; - -/* - * Copyright 2005-2006 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import org.apache.lucene.document.DateTools; -import org.apache.lucene.document.Document; -import org.apache.lucene.document.Field; -import org.apache.lucene.document.NumberTools; -import org.apache.maven.archiva.indexing.record.RepositoryIndexRecord; -import org.apache.maven.archiva.indexing.record.StandardArtifactIndexRecord; -import org.apache.maven.archiva.indexing.record.StandardIndexRecordFields; -import org.codehaus.plexus.util.StringUtils; - -import java.text.ParseException; -import java.util.Arrays; - -/** - * Convert the standard index record to a Lucene document. - * - * @author Brett Porter - */ -public class LuceneStandardIndexRecordConverter - implements LuceneIndexRecordConverter -{ - public Document convert( RepositoryIndexRecord record ) - { - StandardArtifactIndexRecord rec = (StandardArtifactIndexRecord) record; - - Document document = new Document(); - addTokenizedField( document, StandardIndexRecordFields.FILENAME, rec.getFilename() ); - addTokenizedField( document, StandardIndexRecordFields.GROUPID, rec.getGroupId() ); - addExactField( document, StandardIndexRecordFields.GROUPID_EXACT, rec.getGroupId() ); - addTokenizedField( document, StandardIndexRecordFields.ARTIFACTID, rec.getArtifactId() ); - addExactField( document, StandardIndexRecordFields.ARTIFACTID_EXACT, rec.getArtifactId() ); - addTokenizedField( document, StandardIndexRecordFields.VERSION, rec.getVersion() ); - addExactField( document, StandardIndexRecordFields.VERSION_EXACT, rec.getVersion() ); - addTokenizedField( document, StandardIndexRecordFields.BASE_VERSION, rec.getBaseVersion() ); - addExactField( document, StandardIndexRecordFields.BASE_VERSION_EXACT, rec.getBaseVersion() ); - addUntokenizedField( document, StandardIndexRecordFields.TYPE, rec.getType() ); - addTokenizedField( document, StandardIndexRecordFields.CLASSIFIER, rec.getClassifier() ); - addUntokenizedField( document, StandardIndexRecordFields.PACKAGING, rec.getPackaging() ); - addUntokenizedField( document, StandardIndexRecordFields.REPOSITORY, rec.getRepository() ); - addUntokenizedField( document, StandardIndexRecordFields.LAST_MODIFIED, - DateTools.timeToString( rec.getLastModified(), DateTools.Resolution.SECOND ) ); - addUntokenizedField( document, StandardIndexRecordFields.FILE_SIZE, NumberTools.longToString( rec.getSize() ) ); - addUntokenizedField( document, StandardIndexRecordFields.MD5, rec.getMd5Checksum() ); - addUntokenizedField( document, StandardIndexRecordFields.SHA1, rec.getSha1Checksum() ); - if ( rec.getClasses() != null ) - { - addTokenizedField( document, StandardIndexRecordFields.CLASSES, - StringUtils.join( rec.getClasses().iterator(), "\n" ) ); - } - if ( rec.getFiles() != null ) - { - addTokenizedField( document, StandardIndexRecordFields.FILES, - StringUtils.join( rec.getFiles().iterator(), "\n" ) ); - } - addUntokenizedField( document, StandardIndexRecordFields.PLUGIN_PREFIX, rec.getPluginPrefix() ); - addUntokenizedField( document, StandardIndexRecordFields.INCEPTION_YEAR, rec.getInceptionYear() ); - addTokenizedField( document, StandardIndexRecordFields.PROJECT_NAME, rec.getProjectName() ); - addTokenizedField( document, StandardIndexRecordFields.PROJECT_DESCRIPTION, rec.getProjectDescription() ); -/* TODO: add later - document.add( Field.Keyword( StandardIndexRecordFields.FLD_LICENSE_URLS, "" ) ); - document.add( Field.Keyword( StandardIndexRecordFields.FLD_DEPENDENCIES, "" ) ); - document.add( Field.Keyword( StandardIndexRecordFields.FLD_PLUGINS_REPORT, "" ) ); - document.add( Field.Keyword( StandardIndexRecordFields.FLD_PLUGINS_BUILD, "" ) ); -*/ - - return document; - } - - public RepositoryIndexRecord convert( Document document ) - throws ParseException - { - StandardArtifactIndexRecord record = new StandardArtifactIndexRecord(); - - record.setFilename( document.get( StandardIndexRecordFields.FILENAME ) ); - record.setGroupId( document.get( StandardIndexRecordFields.GROUPID ) ); - record.setArtifactId( document.get( StandardIndexRecordFields.ARTIFACTID ) ); - record.setVersion( document.get( StandardIndexRecordFields.VERSION ) ); - record.setBaseVersion( document.get( StandardIndexRecordFields.BASE_VERSION ) ); - record.setType( document.get( StandardIndexRecordFields.TYPE ) ); - record.setClassifier( document.get( StandardIndexRecordFields.CLASSIFIER ) ); - record.setPackaging( document.get( StandardIndexRecordFields.PACKAGING ) ); - record.setRepository( document.get( StandardIndexRecordFields.REPOSITORY ) ); - record.setLastModified( DateTools.stringToTime( document.get( StandardIndexRecordFields.LAST_MODIFIED ) ) ); - record.setSize( NumberTools.stringToLong( document.get( StandardIndexRecordFields.FILE_SIZE ) ) ); - record.setMd5Checksum( document.get( StandardIndexRecordFields.MD5 ) ); - record.setSha1Checksum( document.get( StandardIndexRecordFields.SHA1 ) ); - String classes = document.get( StandardIndexRecordFields.CLASSES ); - if ( classes != null ) - { - record.setClasses( Arrays.asList( classes.split( "\n" ) ) ); - } - String files = document.get( StandardIndexRecordFields.FILES ); - if ( files != null ) - { - record.setFiles( Arrays.asList( files.split( "\n" ) ) ); - } - record.setPluginPrefix( document.get( StandardIndexRecordFields.PLUGIN_PREFIX ) ); - record.setInceptionYear( document.get( StandardIndexRecordFields.INCEPTION_YEAR ) ); - record.setProjectName( document.get( StandardIndexRecordFields.PROJECT_NAME ) ); - record.setProjectDescription( document.get( StandardIndexRecordFields.PROJECT_DESCRIPTION ) ); - - return record; - } - - private static void addUntokenizedField( Document document, String name, String value ) - { - if ( value != null ) - { - document.add( new Field( name, value, Field.Store.YES, Field.Index.UN_TOKENIZED ) ); - } - } - - private static void addExactField( Document document, String name, String value ) - { - if ( value != null ) - { - document.add( new Field( name, value, Field.Store.NO, Field.Index.UN_TOKENIZED ) ); - } - } - - private static void addTokenizedField( Document document, String name, String value ) - { - if ( value != null ) - { - document.add( new Field( name, value, Field.Store.YES, Field.Index.TOKENIZED ) ); - } - } -} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/query/CompoundQuery.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/query/CompoundQuery.java deleted file mode 100644 index 54f68de0c..000000000 --- a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/query/CompoundQuery.java +++ /dev/null @@ -1,105 +0,0 @@ -package org.apache.maven.archiva.indexing.query; - -/* - * Copyright 2005-2006 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import java.util.ArrayList; -import java.util.List; - -/** - * Class to hold multiple SinglePhraseQueries and/or other CompoundQueries. - * - * @author Edwin Punzalan - */ -public class CompoundQuery - implements Query -{ - /** - * The query terms. - */ - private final List compoundQueryTerms = new ArrayList(); - - /** - * Appends a required term to this query. - * - * @param term the term to be appended to this query - */ - public void and( QueryTerm term ) - { - compoundQueryTerms.add( CompoundQueryTerm.and( new SingleTermQuery( term ) ) ); - } - - /** - * Appends an optional term to this query. - * - * @param term the term to be appended to this query - */ - public void or( QueryTerm term ) - { - compoundQueryTerms.add( CompoundQueryTerm.or( new SingleTermQuery( term ) ) ); - } - - /** - * Appends a prohibited term to this query. - * - * @param term the term to be appended to this query - */ - public void not( QueryTerm term ) - { - compoundQueryTerms.add( CompoundQueryTerm.not( new SingleTermQuery( term ) ) ); - } - - /** - * Appends a required subquery to this query. - * - * @param query the subquery to be appended to this query - */ - public void and( Query query ) - { - compoundQueryTerms.add( CompoundQueryTerm.and( query ) ); - } - - /** - * Appends an optional subquery to this query. - * - * @param query the subquery to be appended to this query - */ - public void or( Query query ) - { - compoundQueryTerms.add( CompoundQueryTerm.or( query ) ); - } - - /** - * Appends a prohibited subquery to this query. - * - * @param query the subquery to be appended to this query - */ - public void not( Query query ) - { - compoundQueryTerms.add( CompoundQueryTerm.not( query ) ); - } - - /** - * Method to get the List of Queries appended into this - * - * @return List of all Queries added to this Query - */ - public List getCompoundQueryTerms() - { - return compoundQueryTerms; - } - -} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/query/CompoundQueryTerm.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/query/CompoundQueryTerm.java deleted file mode 100644 index 2538bb01a..000000000 --- a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/query/CompoundQueryTerm.java +++ /dev/null @@ -1,100 +0,0 @@ -package org.apache.maven.archiva.indexing.query; - -/* - * Copyright 2005-2006 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * Base of all query terms. - * - * @author Brett Porter - */ -public class CompoundQueryTerm -{ - /** - * The query to add to the compound query. - */ - private final Query query; - - /** - * Whether the term is required (an AND). - */ - private final boolean required; - - /** - * Whether the term is prohibited (a NOT). - */ - private final boolean prohibited; - - /** - * Class constructor - * - * @param query the subquery to add - * @param required whether the term is required (an AND) - * @param prohibited whether the term is prohibited (a NOT) - */ - private CompoundQueryTerm( Query query, boolean required, boolean prohibited ) - { - this.query = query; - this.prohibited = prohibited; - this.required = required; - } - - /** - * Method to test if the Query is a search requirement - * - * @return true if this Query is a search requirement, otherwise returns false - */ - public boolean isRequired() - { - return required; - } - - /** - * Method to test if the Query is prohibited in the search result - * - * @return true if this Query is prohibited in the search result - */ - public boolean isProhibited() - { - return prohibited; - } - - - /** - * The subquery to execute. - * - * @return the query - */ - public Query getQuery() - { - return query; - } - - static CompoundQueryTerm and( Query query ) - { - return new CompoundQueryTerm( query, true, false ); - } - - static CompoundQueryTerm or( Query query ) - { - return new CompoundQueryTerm( query, false, false ); - } - - static CompoundQueryTerm not( Query query ) - { - return new CompoundQueryTerm( query, false, true ); - } -} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/query/Query.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/query/Query.java deleted file mode 100644 index c3d410b2a..000000000 --- a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/query/Query.java +++ /dev/null @@ -1,26 +0,0 @@ -package org.apache.maven.archiva.indexing.query; - -/* - * Copyright 2005-2006 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * Interface to label the query classes - * - * @author Edwin Punzalan - */ -public interface Query -{ -} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/query/QueryTerm.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/query/QueryTerm.java deleted file mode 100644 index bc949dcd2..000000000 --- a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/query/QueryTerm.java +++ /dev/null @@ -1,61 +0,0 @@ -package org.apache.maven.archiva.indexing.query; - -/* - * Copyright 2005-2006 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * Class to hold a single field search condition - * - * @author Edwin Punzalan - */ -public class QueryTerm -{ - private String field; - - private String value; - - /** - * Class constructor - * - * @param field the index field to search - * @param value the index value requirement - */ - public QueryTerm( String field, String value ) - { - this.field = field; - this.value = value; - } - - /** - * Method to retrieve the name of the index field searched - * - * @return the name of the index field - */ - public String getField() - { - return field; - } - - /** - * Method to retrieve the value used in searching the index field - * - * @return the value to corresspond the index field - */ - public String getValue() - { - return value; - } -} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/query/RangeQuery.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/query/RangeQuery.java deleted file mode 100644 index c44303b27..000000000 --- a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/query/RangeQuery.java +++ /dev/null @@ -1,150 +0,0 @@ -package org.apache.maven.archiva.indexing.query; - -/* - * Copyright 2005-2006 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * Query object that handles range queries (presently used for dates). - * - * @author Maria Odea Ching - * @author Brett Porter - */ -public class RangeQuery - implements Query -{ - /** - * Whether values equal to the boundaries are included in the query results. - */ - private final boolean inclusive; - - /** - * The lower bound. - */ - private final QueryTerm begin; - - /** - * The upper bound. - */ - private final QueryTerm end; - - /** - * Constructor. - * - * @param begin the lower bound - * @param end the upper bound - * @param inclusive whether to include the boundaries in the query - */ - private RangeQuery( QueryTerm begin, QueryTerm end, boolean inclusive ) - { - this.begin = begin; - this.end = end; - this.inclusive = inclusive; - } - - /** - * Create an open range, including all results. - * - * @return the query object - */ - public static RangeQuery createOpenRange() - { - return new RangeQuery( null, null, false ); - } - - /** - * Create a bounded range, excluding the endpoints. - * - * @param begin the lower bound value to compare to - * @param end the upper bound value to compare to - * @return the query object - */ - public static RangeQuery createExclusiveRange( QueryTerm begin, QueryTerm end ) - { - return new RangeQuery( begin, end, false ); - } - - /** - * Create a bounded range, including the endpoints. - * - * @param begin the lower bound value to compare to - * @param end the upper bound value to compare to - * @return the query object - */ - public static RangeQuery createInclusiveRange( QueryTerm begin, QueryTerm end ) - { - return new RangeQuery( begin, end, true ); - } - - /** - * Create a range that is greater than or equal to a given term. - * - * @param begin the value to compare to - * @return the query object - */ - public static RangeQuery createGreaterThanOrEqualToRange( QueryTerm begin ) - { - return new RangeQuery( begin, null, true ); - } - - /** - * Create a range that is greater than a given term. - * - * @param begin the value to compare to - * @return the query object - */ - public static RangeQuery createGreaterThanRange( QueryTerm begin ) - { - return new RangeQuery( begin, null, false ); - } - - /** - * Create a range that is less than or equal to a given term. - * - * @param end the value to compare to - * @return the query object - */ - public static RangeQuery createLessThanOrEqualToRange( QueryTerm end ) - { - return new RangeQuery( null, end, true ); - } - - /** - * Create a range that is less than a given term. - * - * @param end the value to compare to - * @return the query object - */ - public static RangeQuery createLessThanRange( QueryTerm end ) - { - return new RangeQuery( null, end, false ); - } - - public QueryTerm getBegin() - { - return begin; - } - - public QueryTerm getEnd() - { - return end; - } - - public boolean isInclusive() - { - return inclusive; - } - -} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/query/SingleTermQuery.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/query/SingleTermQuery.java deleted file mode 100644 index cb42007d5..000000000 --- a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/query/SingleTermQuery.java +++ /dev/null @@ -1,62 +0,0 @@ -package org.apache.maven.archiva.indexing.query; - -/* - * Copyright 2005-2006 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * Query for a single term. - * - * @author Brett Porter - */ -public class SingleTermQuery - implements Query -{ - /** - * The term to query for. - */ - private final QueryTerm term; - - /** - * Constructor. - * - * @param term the term to query - */ - public SingleTermQuery( QueryTerm term ) - { - this.term = term; - } - - /** - * Shorthand constructor - create a single term query from a field and value - * - * @param field the field name - * @param value the value to check for - */ - public SingleTermQuery( String field, String value ) - { - this.term = new QueryTerm( field, value ); - } - - public String getField() - { - return term.getField(); - } - - public String getValue() - { - return term.getValue(); - } -} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/record/AbstractArtifactIndexRecordFactory.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/record/AbstractArtifactIndexRecordFactory.java deleted file mode 100644 index 0325e2ebc..000000000 --- a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/record/AbstractArtifactIndexRecordFactory.java +++ /dev/null @@ -1,98 +0,0 @@ -package org.apache.maven.archiva.indexing.record; - -/* - * 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.archiva.digest.Digester; -import org.apache.maven.archiva.digest.DigesterException; -import org.codehaus.plexus.logging.AbstractLogEnabled; - -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Enumeration; -import java.util.List; -import java.util.zip.ZipEntry; -import java.util.zip.ZipFile; - -/** - * Base class for the index record factories. - * - * @author Brett Porter - */ -public abstract class AbstractArtifactIndexRecordFactory - extends AbstractLogEnabled - implements RepositoryIndexRecordFactory -{ - protected String readChecksum( File file, Digester digester ) - { - String checksum; - try - { - checksum = digester.calc( file ).toLowerCase(); - } - catch ( DigesterException e ) - { - getLogger().error( "Error getting checksum for artifact file, leaving empty in index: " + e.getMessage() ); - checksum = null; - } - return checksum; - } - - protected List readFilesInArchive( File file ) - throws IOException - { - ZipFile zipFile = new ZipFile( file ); - List files; - try - { - files = new ArrayList( zipFile.size() ); - - for ( Enumeration entries = zipFile.entries(); entries.hasMoreElements(); ) - { - ZipEntry entry = (ZipEntry) entries.nextElement(); - - files.add( entry.getName() ); - } - } - finally - { - closeQuietly( zipFile ); - } - return files; - } - - protected static boolean isClass( String name ) - { - // TODO: verify if class is public or protected (this might require the original ZipEntry) - return name.endsWith( ".class" ) && name.lastIndexOf( "$" ) < 0; - } - - protected static void closeQuietly( ZipFile zipFile ) - { - try - { - if ( zipFile != null ) - { - zipFile.close(); - } - } - catch ( IOException e ) - { - // ignored - } - } -} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/record/MinimalArtifactIndexRecord.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/record/MinimalArtifactIndexRecord.java deleted file mode 100644 index 747f7ab95..000000000 --- a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/record/MinimalArtifactIndexRecord.java +++ /dev/null @@ -1,170 +0,0 @@ -package org.apache.maven.archiva.indexing.record; - -import java.util.Date; -import java.util.List; - -/* - * 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. - */ - -/** - * The a record with the fields in the minimal index. - * - * @author Brett Porter - */ -public class MinimalArtifactIndexRecord - implements RepositoryIndexRecord -{ - /** - * The classes in the archive for the artifact, if it is a JAR. - */ - private List classes; - - /** - * The MD5 checksum of the artifact file. - */ - private String md5Checksum; - - /** - * The filename of the artifact file (no path). - */ - private String filename; - - /** - * The timestamp that the artifact file was last modified. Granularity is seconds. - */ - private long lastModified; - - /** - * The size of the artifact file in bytes. - */ - private long size; - - private static final int MS_PER_SEC = 1000; - - public void setClasses( List classes ) - { - this.classes = classes; - } - - public void setMd5Checksum( String md5Checksum ) - { - this.md5Checksum = md5Checksum; - } - - public void setFilename( String filename ) - { - this.filename = filename; - } - - public void setLastModified( long lastModified ) - { - this.lastModified = lastModified - lastModified % MS_PER_SEC; - } - - public void setSize( long size ) - { - this.size = size; - } - - public List getClasses() - { - return classes; - } - - public String getMd5Checksum() - { - return md5Checksum; - } - - public String getFilename() - { - return filename; - } - - public long getLastModified() - { - return lastModified; - } - - public long getSize() - { - return size; - } - - /** - * @noinspection RedundantIfStatement - */ - public boolean equals( Object obj ) - { - if ( this == obj ) - { - return true; - } - if ( obj == null || getClass() != obj.getClass() ) - { - return false; - } - - MinimalArtifactIndexRecord that = (MinimalArtifactIndexRecord) obj; - - if ( lastModified != that.lastModified ) - { - return false; - } - if ( size != that.size ) - { - return false; - } - if ( classes != null ? !classes.equals( that.classes ) : that.classes != null ) - { - return false; - } - if ( !filename.equals( that.filename ) ) - { - return false; - } - if ( md5Checksum != null ? !md5Checksum.equals( that.md5Checksum ) : that.md5Checksum != null ) - { - return false; - } - - return true; - } - - /** - * @noinspection UnnecessaryParentheses - */ - public int hashCode() - { - int result = classes != null ? classes.hashCode() : 0; - result = 31 * result + ( md5Checksum != null ? md5Checksum.hashCode() : 0 ); - result = 31 * result + filename.hashCode(); - result = 31 * result + (int) ( lastModified ^ ( lastModified >>> 32 ) ); - result = 31 * result + (int) ( size ^ ( size >>> 32 ) ); - return result; - } - - public String toString() - { - return "Filename: " + filename + "; checksum: " + md5Checksum + "; size: " + size + "; lastModified: " + - new Date( lastModified ) + "; classes: " + classes; - } - - public String getPrimaryKey() - { - return filename; - } -} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/record/MinimalArtifactIndexRecordFactory.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/record/MinimalArtifactIndexRecordFactory.java deleted file mode 100644 index 138094206..000000000 --- a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/record/MinimalArtifactIndexRecordFactory.java +++ /dev/null @@ -1,102 +0,0 @@ -package org.apache.maven.archiva.indexing.record; - -/* - * 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.archiva.digest.Digester; -import org.apache.maven.artifact.Artifact; - -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Set; - -/** - * An index record type for the minimal index. - * - * @author Edwin Punzalan - * @author Brett Porter - * @plexus.component role="org.apache.maven.archiva.indexing.record.RepositoryIndexRecordFactory" role-hint="minimal" - */ -public class MinimalArtifactIndexRecordFactory - extends AbstractArtifactIndexRecordFactory -{ - /* List of types to index. */ - private static final Set INDEXED_TYPES = new HashSet( Arrays.asList( new String[]{"jar", "maven-plugin"} ) ); - - /** - * @plexus.requirement role-hint="sha1" - */ - protected Digester sha1Digester; - - /** - * @plexus.requirement role-hint="md5" - */ - protected Digester md5Digester; - - public RepositoryIndexRecord createRecord( Artifact artifact ) - { - MinimalArtifactIndexRecord record = null; - - File file = artifact.getFile(); - if ( file != null && INDEXED_TYPES.contains( artifact.getType() ) && file.exists() ) - { - String md5 = readChecksum( file, md5Digester ); - - List files = null; - try - { - files = readFilesInArchive( file ); - } - catch ( IOException e ) - { - getLogger().error( "Error reading artifact file, omitting from index: " + e.getMessage() ); - } - - if ( files != null ) - { - record = new MinimalArtifactIndexRecord(); - record.setMd5Checksum( md5 ); - record.setFilename( artifact.getRepository().pathOf( artifact ) ); - record.setLastModified( file.lastModified() ); - record.setSize( file.length() ); - record.setClasses( getClassesFromFiles( files ) ); - } - } - return record; - } - - private List getClassesFromFiles( List files ) - { - List classes = new ArrayList(); - - for ( Iterator i = files.iterator(); i.hasNext(); ) - { - String name = (String) i.next(); - - if ( isClass( name ) ) - { - classes.add( name.substring( 0, name.length() - 6 ).replace( '/', '.' ) ); - } - } - - return classes; - } -} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/record/MinimalIndexRecordFields.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/record/MinimalIndexRecordFields.java deleted file mode 100644 index adb2d76a7..000000000 --- a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/record/MinimalIndexRecordFields.java +++ /dev/null @@ -1,41 +0,0 @@ -package org.apache.maven.archiva.indexing.record; - -/* - * 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. - */ - -/** - * The fields in a minimal artifact index record. - * - * @author Brett Porter - * @todo should be an enum - */ -public class MinimalIndexRecordFields -{ - public static final String FILENAME = "j"; - - public static final String LAST_MODIFIED = "d"; - - public static final String FILE_SIZE = "s"; - - public static final String MD5 = "m"; - - public static final String CLASSES = "c"; - - private MinimalIndexRecordFields() - { - // No touchy! - } -} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/record/RepositoryIndexRecord.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/record/RepositoryIndexRecord.java deleted file mode 100644 index 64c69ee92..000000000 --- a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/record/RepositoryIndexRecord.java +++ /dev/null @@ -1,32 +0,0 @@ -package org.apache.maven.archiva.indexing.record; - -/* - * 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. - */ - -/** - * A repository index record. - * - * @author Brett Porter - */ -public interface RepositoryIndexRecord -{ - /** - * Get the primary key used to identify the record uniquely in the index. - * - * @return the primary key - */ - String getPrimaryKey(); -} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/record/RepositoryIndexRecordFactory.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/record/RepositoryIndexRecordFactory.java deleted file mode 100644 index d3031439d..000000000 --- a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/record/RepositoryIndexRecordFactory.java +++ /dev/null @@ -1,44 +0,0 @@ -package org.apache.maven.archiva.indexing.record; - -/* - * 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.archiva.indexing.RepositoryIndexException; -import org.apache.maven.artifact.Artifact; - -/** - * The layout of a record in a repository index. - * - * @author Brett Porter - */ -public interface RepositoryIndexRecordFactory -{ - /** - * The Plexus role. - */ - String ROLE = RepositoryIndexRecordFactory.class.getName(); - - /** - * Create an index record from an artifact. - * - * @param artifact the artifact - * @return the index record - * @throws RepositoryIndexException if there is a problem constructing the record (due to not being able to read the artifact file as a POM) - */ - RepositoryIndexRecord createRecord( Artifact artifact ) - throws RepositoryIndexException; - -} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/record/StandardArtifactIndexRecord.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/record/StandardArtifactIndexRecord.java deleted file mode 100644 index 9b543f3f8..000000000 --- a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/record/StandardArtifactIndexRecord.java +++ /dev/null @@ -1,339 +0,0 @@ -package org.apache.maven.archiva.indexing.record; - -import java.util.List; - -/* - * 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. - */ - -/** - * The a record with the fields in the standard index. - * - * @author Brett Porter - */ -public class StandardArtifactIndexRecord - extends MinimalArtifactIndexRecord -{ - /** - * The SHA-1 checksum of the artifact file. - */ - private String sha1Checksum; - - /** - * The artifact's group. - */ - private String groupId; - - /** - * The artifact's identifier within the group. - */ - private String artifactId; - - /** - * The artifact's version. - */ - private String version; - - /** - * The classifier, if there is one. - */ - private String classifier; - - /** - * The artifact type (from the file). - */ - private String type; - - /** - * A list of files (separated by '\n') in the artifact if it is an archive. - */ - private List files; - - /** - * The identifier of the repository that the artifact came from. - */ - private String repository; - - /** - * The packaging specified in the POM for this artifact. - */ - private String packaging; - - /** - * The plugin prefix specified in the metadata if the artifact is a plugin. - */ - private String pluginPrefix; - - /** - * The year the project was started. - */ - private String inceptionYear; - - /** - * The description of the project. - */ - private String projectDescription; - - /** - * The name of the project. - */ - private String projectName; - - /** - * The base version (before the snapshot is determined). - */ - private String baseVersion; - - public void setSha1Checksum( String sha1Checksum ) - { - this.sha1Checksum = sha1Checksum; - } - - public void setGroupId( String groupId ) - { - this.groupId = groupId; - } - - public void setArtifactId( String artifactId ) - { - this.artifactId = artifactId; - } - - public void setVersion( String version ) - { - this.version = version; - } - - public void setClassifier( String classifier ) - { - this.classifier = classifier; - } - - public void setType( String type ) - { - this.type = type; - } - - public void setFiles( List files ) - { - this.files = files; - } - - public void setRepository( String repository ) - { - this.repository = repository; - } - - /** - * @noinspection RedundantIfStatement - */ - public boolean equals( Object obj ) - { - if ( this == obj ) - { - return true; - } - if ( obj == null || getClass() != obj.getClass() ) - { - return false; - } - if ( !super.equals( obj ) ) - { - return false; - } - - StandardArtifactIndexRecord that = (StandardArtifactIndexRecord) obj; - - if ( !artifactId.equals( that.artifactId ) ) - { - return false; - } - if ( classifier != null ? !classifier.equals( that.classifier ) : that.classifier != null ) - { - return false; - } - if ( files != null ? !files.equals( that.files ) : that.files != null ) - { - return false; - } - if ( !groupId.equals( that.groupId ) ) - { - return false; - } - if ( repository != null ? !repository.equals( that.repository ) : that.repository != null ) - { - return false; - } - if ( sha1Checksum != null ? !sha1Checksum.equals( that.sha1Checksum ) : that.sha1Checksum != null ) - { - return false; - } - if ( type != null ? !type.equals( that.type ) : that.type != null ) - { - return false; - } - if ( !version.equals( that.version ) ) - { - return false; - } - if ( !baseVersion.equals( that.baseVersion ) ) - { - return false; - } - if ( packaging != null ? !packaging.equals( that.packaging ) : that.packaging != null ) - { - return false; - } - if ( pluginPrefix != null ? !pluginPrefix.equals( that.pluginPrefix ) : that.pluginPrefix != null ) - { - return false; - } - if ( projectName != null ? !projectName.equals( that.projectName ) : that.projectName != null ) - { - return false; - } - if ( inceptionYear != null ? !inceptionYear.equals( that.inceptionYear ) : that.inceptionYear != null ) - { - return false; - } - if ( projectDescription != null ? !projectDescription.equals( that.projectDescription ) - : that.projectDescription != null ) - { - return false; - } - - return true; - } - - public int hashCode() - { - int result = super.hashCode(); - result = 31 * result + ( sha1Checksum != null ? sha1Checksum.hashCode() : 0 ); - result = 31 * result + groupId.hashCode(); - result = 31 * result + artifactId.hashCode(); - result = 31 * result + version.hashCode(); - result = 31 * result + baseVersion.hashCode(); - result = 31 * result + ( classifier != null ? classifier.hashCode() : 0 ); - result = 31 * result + ( type != null ? type.hashCode() : 0 ); - result = 31 * result + ( files != null ? files.hashCode() : 0 ); - result = 31 * result + ( repository != null ? repository.hashCode() : 0 ); - result = 31 * result + ( packaging != null ? packaging.hashCode() : 0 ); - result = 31 * result + ( pluginPrefix != null ? pluginPrefix.hashCode() : 0 ); - result = 31 * result + ( inceptionYear != null ? inceptionYear.hashCode() : 0 ); - result = 31 * result + ( projectName != null ? projectName.hashCode() : 0 ); - result = 31 * result + ( projectDescription != null ? projectDescription.hashCode() : 0 ); - return result; - } - - public String getSha1Checksum() - { - return sha1Checksum; - } - - public String getGroupId() - { - return groupId; - } - - public String getArtifactId() - { - return artifactId; - } - - public String getVersion() - { - return version; - } - - public String getClassifier() - { - return classifier; - } - - public String getType() - { - return type; - } - - public List getFiles() - { - return files; - } - - public String getRepository() - { - return repository; - } - - public String getPackaging() - { - return packaging; - } - - public String getPluginPrefix() - { - return pluginPrefix; - } - - public void setPackaging( String packaging ) - { - this.packaging = packaging; - } - - public void setPluginPrefix( String pluginPrefix ) - { - this.pluginPrefix = pluginPrefix; - } - - public void setInceptionYear( String inceptionYear ) - { - this.inceptionYear = inceptionYear; - } - - public void setProjectDescription( String description ) - { - this.projectDescription = description; - } - - public void setProjectName( String projectName ) - { - this.projectName = projectName; - } - - public String getInceptionYear() - { - return inceptionYear; - } - - public String getProjectDescription() - { - return projectDescription; - } - - public String getProjectName() - { - return projectName; - } - - public void setBaseVersion( String baseVersion ) - { - this.baseVersion = baseVersion; - } - - public String getBaseVersion() - { - return baseVersion; - } -} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/record/StandardArtifactIndexRecordFactory.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/record/StandardArtifactIndexRecordFactory.java deleted file mode 100644 index 27748050b..000000000 --- a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/record/StandardArtifactIndexRecordFactory.java +++ /dev/null @@ -1,302 +0,0 @@ -package org.apache.maven.archiva.indexing.record; - -/* - * 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.archiva.digest.Digester; -import org.apache.maven.archiva.indexing.RepositoryIndexException; -import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.factory.ArtifactFactory; -import org.apache.maven.artifact.repository.ArtifactRepository; -import org.apache.maven.model.Model; -import org.apache.maven.project.MavenProject; -import org.apache.maven.project.MavenProjectBuilder; -import org.apache.maven.project.ProjectBuildingException; -import org.codehaus.plexus.util.xml.Xpp3Dom; -import org.codehaus.plexus.util.xml.Xpp3DomBuilder; -import org.codehaus.plexus.util.xml.pull.XmlPullParserException; - -import java.io.File; -import java.io.IOException; -import java.io.InputStreamReader; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Set; -import java.util.zip.ZipEntry; -import java.util.zip.ZipException; -import java.util.zip.ZipFile; - -/** - * An index record type for the standard index. - * - * @author Edwin Punzalan - * @author Brett Porter - * @plexus.component role="org.apache.maven.archiva.indexing.record.RepositoryIndexRecordFactory" role-hint="standard" - */ -public class StandardArtifactIndexRecordFactory - extends AbstractArtifactIndexRecordFactory -{ - /** - * A list of artifact types to treat as a zip archive. - * - * @todo this should be smarter (perhaps use plexus archiver to look for an unarchiver, and make the ones for zip configurable since sar, par, etc can be added at random. - */ - private static final Set ARCHIVE_TYPES = - new HashSet( Arrays.asList( new String[]{"jar", "ejb", "par", "sar", "war", "ear", "rar"} ) ); - - /** - * @plexus.requirement - */ - private ArtifactFactory artifactFactory; - - /** - * @plexus.requirement - */ - private MavenProjectBuilder projectBuilder; - - /** - * @plexus.requirement role-hint="sha1" - */ - protected Digester sha1Digester; - - /** - * @plexus.requirement role-hint="md5" - */ - protected Digester md5Digester; - - private static final String PLUGIN_METADATA_NAME = "META-INF/maven/plugin.xml"; - - private static final String ARCHETYPE_METADATA_NAME = "META-INF/maven/archetype.xml"; - - // some current/old archetypes have the archetype.xml at different location. - private static final String ARCHETYPE_METADATA_NAME_OLD = "META-INF/archetype.xml"; - - public RepositoryIndexRecord createRecord( Artifact artifact ) - throws RepositoryIndexException - { - StandardArtifactIndexRecord record = null; - - File file = artifact.getFile(); - - // TODO: is this condition really a possibility? - if ( file != null && file.exists() ) - { - String md5 = readChecksum( file, md5Digester ); - String sha1 = readChecksum( file, sha1Digester ); - - List files = null; - boolean archive = ARCHIVE_TYPES.contains( artifact.getType() ); - try - { - if ( archive ) - { - files = readFilesInArchive( file ); - } - } - catch ( IOException e ) - { - getLogger().error( "Error reading artifact file, omitting from index: " + e.getMessage() ); - } - - // If it's an archive with no files, don't create a record - if ( !archive || files != null ) - { - record = new StandardArtifactIndexRecord(); - - record.setGroupId( artifact.getGroupId() ); - record.setArtifactId( artifact.getArtifactId() ); - record.setBaseVersion( artifact.getBaseVersion() ); - record.setVersion( artifact.getVersion() ); - record.setClassifier( artifact.getClassifier() ); - record.setType( artifact.getType() ); - record.setMd5Checksum( md5 ); - record.setSha1Checksum( sha1 ); - record.setFilename( artifact.getRepository().pathOf( artifact ) ); - record.setLastModified( file.lastModified() ); - record.setSize( file.length() ); - record.setRepository( artifact.getRepository().getId() ); - if ( files != null ) - { - populateArchiveEntries( files, record, artifact.getFile() ); - } - - if ( !"pom".equals( artifact.getType() ) ) - { - Artifact pomArtifact = artifactFactory.createProjectArtifact( artifact.getGroupId(), - artifact.getArtifactId(), - artifact.getVersion() ); - pomArtifact.isSnapshot(); // gross hack around bug in maven-artifact - File pomFile = new File( artifact.getRepository().getBasedir(), - artifact.getRepository().pathOf( pomArtifact ) ); - if ( pomFile.exists() ) - { - try - { - populatePomEntries( readPom( pomArtifact, artifact.getRepository() ), record ); - } - catch ( ProjectBuildingException e ) - { - getLogger().error( "Error reading POM file, not populating in index: " + e.getMessage() ); - } - } - } - else - { - Model model; - try - { - model = readPom( artifact, artifact.getRepository() ); - - if ( !"pom".equals( model.getPackaging() ) ) - { - // Don't return a record for a POM that is does not belong on its own - record = null; - } - else - { - populatePomEntries( model, record ); - } - } - catch ( ProjectBuildingException e ) - { - getLogger().error( "Error reading POM file, not populating in index: " + e.getMessage() ); - } - } - } - } - - return record; - } - - private void populatePomEntries( Model pom, StandardArtifactIndexRecord record ) - { - record.setPackaging( pom.getPackaging() ); - record.setProjectName( pom.getName() ); - record.setProjectDescription( pom.getDescription() ); - record.setInceptionYear( pom.getInceptionYear() ); - -/* TODO: fields for later - indexPlugins( doc, FLD_PLUGINS_BUILD, pom.getBuild().getPlugins().iterator() ); - indexReportPlugins( doc, FLD_PLUGINS_REPORT, pom.getReporting().getPlugins().iterator() ); - record.setDependencies( dependencies ); - record.setLicenses( licenses ); -*/ - } - - private Model readPom( Artifact artifact, ArtifactRepository repository ) - throws RepositoryIndexException, ProjectBuildingException - { - // TODO: this can create a -SNAPSHOT.pom when it didn't exist and a timestamped one did. This is harmless, but should be avoided - // TODO: will this pollute with local repo metadata? - MavenProject project = projectBuilder.buildFromRepository( artifact, Collections.EMPTY_LIST, repository ); - return project.getModel(); - } - - private void populateArchiveEntries( List files, StandardArtifactIndexRecord record, File artifactFile ) - throws RepositoryIndexException - { - List classes = new ArrayList(); - List fileList = new ArrayList(); - - for ( Iterator i = files.iterator(); i.hasNext(); ) - { - String name = (String) i.next(); - - // ignore directories - if ( !name.endsWith( "/" ) ) - { - fileList.add( name ); - - if ( isClass( name ) ) - { - classes.add( name.substring( 0, name.length() - 6 ).replace( '/', '.' ) ); - } - else if ( PLUGIN_METADATA_NAME.equals( name ) ) - { - populatePluginEntries( readXmlMetadataFileInJar( artifactFile, PLUGIN_METADATA_NAME ), record ); - } - else if ( ARCHETYPE_METADATA_NAME.equals( name ) || ARCHETYPE_METADATA_NAME_OLD.equals( name ) ) - { - populateArchetypeEntries( record ); - } - } - } - - if ( !classes.isEmpty() ) - { - record.setClasses( classes ); - } - if ( !fileList.isEmpty() ) - { - record.setFiles( fileList ); - } - } - - private void populateArchetypeEntries( StandardArtifactIndexRecord record ) - { - // Typically discovered as a JAR - record.setType( "maven-archetype" ); - } - - private Xpp3Dom readXmlMetadataFileInJar( File file, String name ) - throws RepositoryIndexException - { - // TODO: would be more efficient with original ZipEntry still around - - Xpp3Dom xpp3Dom; - ZipFile zipFile = null; - try - { - zipFile = new ZipFile( file ); - ZipEntry entry = zipFile.getEntry( name ); - xpp3Dom = Xpp3DomBuilder.build( new InputStreamReader( zipFile.getInputStream( entry ) ) ); - } - catch ( ZipException e ) - { - throw new RepositoryIndexException( "Unable to read plugin metadata: " + e.getMessage(), e ); - } - catch ( IOException e ) - { - throw new RepositoryIndexException( "Unable to read plugin metadata: " + e.getMessage(), e ); - } - catch ( XmlPullParserException e ) - { - throw new RepositoryIndexException( "Unable to read plugin metadata: " + e.getMessage(), e ); - } - finally - { - closeQuietly( zipFile ); - } - return xpp3Dom; - } - - public void populatePluginEntries( Xpp3Dom metadata, StandardArtifactIndexRecord record ) - { - // Typically discovered as a JAR - record.setType( "maven-plugin" ); - - Xpp3Dom prefix = metadata.getChild( "goalPrefix" ); - - if ( prefix != null ) - { - record.setPluginPrefix( prefix.getValue() ); - } - } -} diff --git a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/record/StandardIndexRecordFields.java b/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/record/StandardIndexRecordFields.java deleted file mode 100644 index 5515a8b62..000000000 --- a/archiva-indexer/src/main/java/org/apache/maven/archiva/indexing/record/StandardIndexRecordFields.java +++ /dev/null @@ -1,77 +0,0 @@ -package org.apache.maven.archiva.indexing.record; - -/* - * 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. - */ - -/** - * The fields in a minimal artifact index record. - * - * @author Brett Porter - * @todo should be an enum - */ -public class StandardIndexRecordFields -{ - public static final String FILENAME = "filename"; - - public static final String GROUPID = "groupId"; - - public static final String GROUPID_EXACT = GROUPID + "_u"; - - public static final String ARTIFACTID = "artifactId"; - - public static final String ARTIFACTID_EXACT = ARTIFACTID + "_u"; - - public static final String VERSION = "version"; - - public static final String VERSION_EXACT = VERSION + "_u"; - - public static final String BASE_VERSION = "baseVersion"; - - public static final String BASE_VERSION_EXACT = BASE_VERSION + "_u"; - - public static final String TYPE = "type"; - - public static final String CLASSIFIER = "classifier"; - - public static final String PACKAGING = "packaging"; - - public static final String REPOSITORY = "repo"; - - public static final String LAST_MODIFIED = "lastModified"; - - public static final String FILE_SIZE = "fileSize"; - - public static final String MD5 = "md5"; - - public static final String SHA1 = "sha1"; - - public static final String CLASSES = "classes"; - - public static final String PLUGIN_PREFIX = "pluginPrefix"; - - public static final String FILES = "files"; - - public static final String INCEPTION_YEAR = "inceptionYear"; - - public static final String PROJECT_NAME = "projectName"; - - public static final String PROJECT_DESCRIPTION = "projectDesc"; - - private StandardIndexRecordFields() - { - // No touchy! - } -} diff --git a/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/lucene/LuceneMinimalArtifactIndexSearchTest.java b/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/lucene/LuceneMinimalArtifactIndexSearchTest.java new file mode 100644 index 000000000..2183fd8aa --- /dev/null +++ b/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/lucene/LuceneMinimalArtifactIndexSearchTest.java @@ -0,0 +1,218 @@ +package org.apache.maven.archiva.indexer.lucene; + +/* + * Copyright 2005-2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.lucene.index.Term; +import org.apache.lucene.search.Query; +import org.apache.lucene.search.TermQuery; +import org.apache.maven.archiva.indexer.RepositoryArtifactIndex; +import org.apache.maven.archiva.indexer.RepositoryArtifactIndexFactory; +import org.apache.maven.archiva.indexer.RepositoryIndexSearchException; +import org.apache.maven.archiva.indexer.record.MinimalIndexRecordFields; +import org.apache.maven.archiva.indexer.record.RepositoryIndexRecordFactory; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.factory.ArtifactFactory; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; +import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; +import org.apache.maven.artifact.versioning.VersionRange; +import org.codehaus.plexus.PlexusTestCase; +import org.codehaus.plexus.util.FileUtils; + +import java.io.File; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Test the Lucene implementation of the artifact index search. + * + * @author Brett Porter + * @todo would be nice to abstract some of the query away, but for now passing in a Lucene query directly is good enough + */ +public class LuceneMinimalArtifactIndexSearchTest + extends PlexusTestCase +{ + private RepositoryArtifactIndex index; + + private ArtifactRepository repository; + + private ArtifactFactory artifactFactory; + + private File indexLocation; + + private RepositoryIndexRecordFactory recordFactory; + + private Map records = new HashMap(); + + protected void setUp() + throws Exception + { + super.setUp(); + + recordFactory = (RepositoryIndexRecordFactory) lookup( RepositoryIndexRecordFactory.ROLE, "minimal" ); + + artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE ); + + ArtifactRepositoryFactory repositoryFactory = + (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE ); + + ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" ); + + File file = getTestFile( "src/test/managed-repository" ); + repository = + repositoryFactory.createArtifactRepository( "test", file.toURI().toURL().toString(), layout, null, null ); + + RepositoryArtifactIndexFactory factory = + (RepositoryArtifactIndexFactory) lookup( RepositoryArtifactIndexFactory.ROLE, "lucene" ); + + indexLocation = getTestFile( "target/test-index" ); + + FileUtils.deleteDirectory( indexLocation ); + + index = factory.createMinimalIndex( indexLocation ); + + records.put( "test-jar", recordFactory.createRecord( createArtifact( "test-jar" ) ) ); + records.put( "test-jar-jdk14", + recordFactory.createRecord( createArtifact( "test-jar", "1.0", "jar", "jdk14" ) ) ); + records.put( "test-jar-and-pom", + recordFactory.createRecord( createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar" ) ) ); + records.put( "test-jar-and-pom-jdk14", recordFactory.createRecord( + createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar", "jdk14" ) ) ); + records.put( "test-child-pom", + recordFactory.createRecord( createArtifact( "test-child-pom", "1.0-20060728.121314-1", "jar" ) ) ); + records.put( "test-archetype", recordFactory.createRecord( createArtifact( "test-archetype" ) ) ); + records.put( "test-plugin", recordFactory.createRecord( createArtifact( "test-plugin" ) ) ); + records.put( "test-pom", recordFactory.createRecord( createArtifact( "test-pom", "1.0", "pom" ) ) ); + records.put( "parent-pom", recordFactory.createRecord( createArtifact( "parent-pom", "1", "pom" ) ) ); + records.put( "test-dll", recordFactory.createRecord( createArtifact( "test-dll", "1.0.1.34", "dll" ) ) ); + + index.indexRecords( records.values() ); + } + + public void testExactMatchMd5() + throws RepositoryIndexSearchException + { + Query query = new TermQuery( new Term( MinimalIndexRecordFields.MD5, "3a0adc365f849366cd8b633cad155cb7" ) ); + List results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) ); + assertEquals( "Check results size", 5, results.size() ); + + // test non-match fails + query = new TermQuery( new Term( MinimalIndexRecordFields.MD5, "foo" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check results size", results.isEmpty() ); + } + + public void testMatchFilename() + throws RepositoryIndexSearchException + { + Query query = new TermQuery( new Term( MinimalIndexRecordFields.FILENAME, "maven" ) ); + List results = index.search( new LuceneQuery( query ) ); + + assertFalse( "Check result", results.contains( records.get( "test-pom" ) ) ); + assertFalse( "Check result", results.contains( records.get( "parent-pom" ) ) ); + assertFalse( "Check result", results.contains( records.get( "test-dll" ) ) ); + assertEquals( "Check results size", 7, results.size() ); + +/* TODO: if this is a result we want, we need to change the analyzer. Currently, it is tokenizing it as plugin-1.0 and plugin/1.0 in the path + query = new TermQuery( new Term( MinimalIndexRecordFields.FILENAME, "plugin" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) ); + assertEquals( "Check results size", 1, results.size() ); +*/ + query = new TermQuery( new Term( MinimalIndexRecordFields.FILENAME, "test" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertFalse( "Check result", results.contains( records.get( "parent-pom" ) ) ); + assertFalse( "Check result", results.contains( records.get( "test-pom" ) ) ); + assertFalse( "Check result", results.contains( records.get( "test-dll" ) ) ); + assertEquals( "Check results size", 7, results.size() ); + + // test non-match fails + query = new TermQuery( new Term( MinimalIndexRecordFields.FILENAME, "foo" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check results size", results.isEmpty() ); + } + + public void testMatchClass() + throws RepositoryIndexSearchException + { + // TODO: should be preserving case! + Query query = new TermQuery( new Term( MinimalIndexRecordFields.CLASSES, "b.c.c" ) ); + List results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) ); + assertEquals( "Check results size", 5, results.size() ); + +/* TODO!: need to change the analyzer if we want partial classes (split on '.') + query = new TermQuery( new Term( MinimalIndexRecordFields.CLASSES, "C" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) ); + assertEquals( "Check results size", 4, results.size() ); + + query = new TermQuery( new Term( MinimalIndexRecordFields.CLASSES, "MyMojo" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) ); + assertEquals( "Check results size", 1, results.size() ); +*/ + + // test non-match fails + query = new TermQuery( new Term( MinimalIndexRecordFields.CLASSES, "foo" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check results size", results.isEmpty() ); + } + + private Artifact createArtifact( String artifactId ) + { + return createArtifact( artifactId, "1.0", "jar", null ); + } + + private Artifact createArtifact( String artifactId, String version, String type ) + { + return createArtifact( artifactId, version, type, null ); + } + + private Artifact createArtifact( String artifactId, String version, String type, String classifier ) + { + Artifact artifact = artifactFactory.createDependencyArtifact( "org.apache.maven.archiva.record", artifactId, + VersionRange.createFromVersion( version ), type, + classifier, Artifact.SCOPE_RUNTIME ); + artifact.isSnapshot(); + artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); + artifact.setRepository( repository ); + return artifact; + } +} diff --git a/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/lucene/LuceneMinimalArtifactIndexTest.java b/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/lucene/LuceneMinimalArtifactIndexTest.java new file mode 100644 index 000000000..f91ed0616 --- /dev/null +++ b/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/lucene/LuceneMinimalArtifactIndexTest.java @@ -0,0 +1,351 @@ +package org.apache.maven.archiva.indexer.lucene; + +/* + * Copyright 2005-2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.lucene.analysis.standard.StandardAnalyzer; +import org.apache.lucene.document.Document; +import org.apache.lucene.document.NumberTools; +import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.IndexWriter; +import org.apache.maven.archiva.indexer.RepositoryArtifactIndex; +import org.apache.maven.archiva.indexer.RepositoryArtifactIndexFactory; +import org.apache.maven.archiva.indexer.RepositoryIndexException; +import org.apache.maven.archiva.indexer.record.MinimalIndexRecordFields; +import org.apache.maven.archiva.indexer.record.RepositoryIndexRecord; +import org.apache.maven.archiva.indexer.record.RepositoryIndexRecordFactory; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.factory.ArtifactFactory; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; +import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; +import org.codehaus.plexus.PlexusTestCase; +import org.codehaus.plexus.util.FileUtils; +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; + +import java.io.File; +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.Collections; +import java.util.Date; +import java.util.Iterator; +import java.util.List; +import java.util.Locale; +import java.util.TimeZone; + +/** + * Test the Lucene implementation of the artifact index. + * + * @author Brett Porter + */ +public class LuceneMinimalArtifactIndexTest + extends PlexusTestCase +{ + private RepositoryArtifactIndex index; + + private ArtifactRepository repository; + + private ArtifactFactory artifactFactory; + + private File indexLocation; + + private RepositoryIndexRecordFactory recordFactory; + + protected void setUp() + throws Exception + { + super.setUp(); + + recordFactory = (RepositoryIndexRecordFactory) lookup( RepositoryIndexRecordFactory.ROLE, "minimal" ); + + artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE ); + + ArtifactRepositoryFactory repositoryFactory = + (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE ); + + ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" ); + + File file = getTestFile( "src/test/managed-repository" ); + repository = + repositoryFactory.createArtifactRepository( "test", file.toURI().toURL().toString(), layout, null, null ); + + RepositoryArtifactIndexFactory factory = + (RepositoryArtifactIndexFactory) lookup( RepositoryArtifactIndexFactory.ROLE, "lucene" ); + + indexLocation = getTestFile( "target/test-index" ); + + FileUtils.deleteDirectory( indexLocation ); + + index = factory.createMinimalIndex( indexLocation ); + } + + public void testIndexExists() + throws IOException, RepositoryIndexException + { + assertFalse( "check index doesn't exist", index.exists() ); + + // create empty directory + indexLocation.mkdirs(); + assertFalse( "check index doesn't exist even if directory does", index.exists() ); + + // create index, with no records + createEmptyIndex(); + assertTrue( "check index is considered to exist", index.exists() ); + + // Test non-directory + FileUtils.deleteDirectory( indexLocation ); + indexLocation.createNewFile(); + try + { + index.exists(); + fail( "Index operation should fail as the location is not valid" ); + } + catch ( RepositoryIndexException e ) + { + // great + } + finally + { + indexLocation.delete(); + } + } + + public void testAddRecordNoIndex() + throws IOException, RepositoryIndexException + { + Artifact artifact = createArtifact( "test-jar" ); + + RepositoryIndexRecord record = recordFactory.createRecord( artifact ); + index.indexRecords( Collections.singletonList( record ) ); + + IndexReader reader = IndexReader.open( indexLocation ); + try + { + Document document = reader.document( 0 ); + assertEquals( "Check document", repository.pathOf( artifact ), + document.get( MinimalIndexRecordFields.FILENAME ) ); + assertEquals( "Check index size", 1, reader.numDocs() ); + } + finally + { + reader.close(); + } + } + + public void testAddRecordExistingEmptyIndex() + throws IOException, RepositoryIndexException + { + createEmptyIndex(); + + Artifact artifact = createArtifact( "test-jar" ); + + RepositoryIndexRecord record = recordFactory.createRecord( artifact ); + index.indexRecords( Collections.singletonList( record ) ); + + IndexReader reader = IndexReader.open( indexLocation ); + try + { + Document document = reader.document( 0 ); + assertRecord( document, artifact, "3a0adc365f849366cd8b633cad155cb7", "A\nb.B\nb.c.C" ); + assertEquals( "Check index size", 1, reader.numDocs() ); + } + finally + { + reader.close(); + } + } + + public void testAddRecordInIndex() + throws IOException, RepositoryIndexException + { + createEmptyIndex(); + + Artifact artifact = createArtifact( "test-jar" ); + + RepositoryIndexRecord record = recordFactory.createRecord( artifact ); + index.indexRecords( Collections.singletonList( record ) ); + + // Do it again + record = recordFactory.createRecord( artifact ); + index.indexRecords( Collections.singletonList( record ) ); + + IndexReader reader = IndexReader.open( indexLocation ); + try + { + Document document = reader.document( 0 ); + assertRecord( document, artifact, "3a0adc365f849366cd8b633cad155cb7", "A\nb.B\nb.c.C" ); + assertEquals( "Check index size", 1, reader.numDocs() ); + } + finally + { + reader.close(); + } + } + + public void testDeleteRecordInIndex() + throws IOException, RepositoryIndexException + { + createEmptyIndex(); + + Artifact artifact = createArtifact( "test-jar" ); + + RepositoryIndexRecord record = recordFactory.createRecord( artifact ); + index.indexRecords( Collections.singletonList( record ) ); + + index.deleteRecords( Collections.singletonList( record ) ); + + IndexReader reader = IndexReader.open( indexLocation ); + try + { + assertEquals( "No documents", 0, reader.numDocs() ); + } + finally + { + reader.close(); + } + } + + public void testDeleteRecordNotInIndex() + throws IOException, RepositoryIndexException + { + createEmptyIndex(); + + Artifact artifact = createArtifact( "test-jar" ); + + RepositoryIndexRecord record = recordFactory.createRecord( artifact ); + + index.deleteRecords( Collections.singletonList( record ) ); + + IndexReader reader = IndexReader.open( indexLocation ); + try + { + assertEquals( "No documents", 0, reader.numDocs() ); + } + finally + { + reader.close(); + } + } + + public void testDeleteRecordNoIndex() + throws IOException, RepositoryIndexException + { + Artifact artifact = createArtifact( "test-jar" ); + + RepositoryIndexRecord record = recordFactory.createRecord( artifact ); + index.deleteRecords( Collections.singleton( record ) ); + + assertFalse( index.exists() ); + } + + public void testAddPomRecord() + throws IOException, RepositoryIndexException + { + createEmptyIndex(); + + Artifact artifact = createArtifact( "test-pom", "1.0", "pom" ); + + RepositoryIndexRecord record = recordFactory.createRecord( artifact ); + index.indexRecords( Collections.singletonList( record ) ); + + IndexReader reader = IndexReader.open( indexLocation ); + try + { + assertEquals( "No documents", 0, reader.numDocs() ); + } + finally + { + reader.close(); + } + } + + public void testAddPlugin() + throws IOException, RepositoryIndexException, XmlPullParserException + { + createEmptyIndex(); + + Artifact artifact = createArtifact( "test-plugin" ); + + RepositoryIndexRecord record = recordFactory.createRecord( artifact ); + + index.indexRecords( Collections.singletonList( record ) ); + + IndexReader reader = IndexReader.open( indexLocation ); + try + { + Document document = reader.document( 0 ); + assertRecord( document, artifact, "3530896791670ebb45e17708e5d52c40", + "org.apache.maven.archiva.record.MyMojo" ); + assertEquals( "Check index size", 1, reader.numDocs() ); + } + finally + { + reader.close(); + } + } + + private Artifact createArtifact( String artifactId ) + { + return createArtifact( artifactId, "1.0", "jar" ); + } + + private Artifact createArtifact( String artifactId, String version, String type ) + { + Artifact artifact = + artifactFactory.createBuildArtifact( "org.apache.maven.archiva.record", artifactId, version, type ); + artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); + artifact.setRepository( repository ); + return artifact; + } + + private void createEmptyIndex() + throws IOException + { + createIndex( Collections.EMPTY_LIST ); + } + + private void createIndex( List docments ) + throws IOException + { + IndexWriter writer = new IndexWriter( indexLocation, new StandardAnalyzer(), true ); + for ( Iterator i = docments.iterator(); i.hasNext(); ) + { + Document document = (Document) i.next(); + writer.addDocument( document ); + } + writer.optimize(); + writer.close(); + } + + private void assertRecord( Document document, Artifact artifact, String expectedChecksum, String expectedClasses ) + { + assertEquals( "Check document filename", repository.pathOf( artifact ), + document.get( MinimalIndexRecordFields.FILENAME ) ); + assertEquals( "Check document timestamp", getLastModified( artifact.getFile() ), + document.get( MinimalIndexRecordFields.LAST_MODIFIED ) ); + assertEquals( "Check document checksum", expectedChecksum, document.get( MinimalIndexRecordFields.MD5 ) ); + assertEquals( "Check document size", artifact.getFile().length(), + NumberTools.stringToLong( document.get( MinimalIndexRecordFields.FILE_SIZE ) ) ); + assertEquals( "Check document classes", expectedClasses, document.get( MinimalIndexRecordFields.CLASSES ) ); + } + + private String getLastModified( File file ) + { + SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyyMMddHHmmss", Locale.US ); + dateFormat.setTimeZone( TimeZone.getTimeZone( "UTC" ) ); + return dateFormat.format( new Date( file.lastModified() ) ); + } +} diff --git a/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/lucene/LuceneStandardArtifactIndexSearchTest.java b/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/lucene/LuceneStandardArtifactIndexSearchTest.java new file mode 100644 index 000000000..6f0625265 --- /dev/null +++ b/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/lucene/LuceneStandardArtifactIndexSearchTest.java @@ -0,0 +1,699 @@ +package org.apache.maven.archiva.indexer.lucene; + +/* + * Copyright 2005-2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.lucene.index.Term; +import org.apache.lucene.search.BooleanClause; +import org.apache.lucene.search.BooleanQuery; +import org.apache.lucene.search.MatchAllDocsQuery; +import org.apache.lucene.search.Query; +import org.apache.lucene.search.TermQuery; +import org.apache.maven.archiva.indexer.RepositoryArtifactIndex; +import org.apache.maven.archiva.indexer.RepositoryArtifactIndexFactory; +import org.apache.maven.archiva.indexer.RepositoryIndexSearchException; +import org.apache.maven.archiva.indexer.record.RepositoryIndexRecordFactory; +import org.apache.maven.archiva.indexer.record.StandardIndexRecordFields; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.factory.ArtifactFactory; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; +import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; +import org.apache.maven.artifact.versioning.VersionRange; +import org.codehaus.plexus.PlexusTestCase; +import org.codehaus.plexus.util.FileUtils; + +import java.io.File; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Test the Lucene implementation of the artifact index search. + * + * @author Brett Porter + * @todo would be nice to abstract some of the query away, but for now passing in a Lucene query directly is good enough + */ +public class LuceneStandardArtifactIndexSearchTest + extends PlexusTestCase +{ + private RepositoryArtifactIndex index; + + private ArtifactRepository repository; + + private ArtifactFactory artifactFactory; + + private File indexLocation; + + private RepositoryIndexRecordFactory recordFactory; + + private Map records = new HashMap(); + + protected void setUp() + throws Exception + { + super.setUp(); + + recordFactory = (RepositoryIndexRecordFactory) lookup( RepositoryIndexRecordFactory.ROLE, "standard" ); + + artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE ); + + ArtifactRepositoryFactory repositoryFactory = + (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE ); + + ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" ); + + File file = getTestFile( "src/test/managed-repository" ); + repository = + repositoryFactory.createArtifactRepository( "test", file.toURI().toURL().toString(), layout, null, null ); + + RepositoryArtifactIndexFactory factory = + (RepositoryArtifactIndexFactory) lookup( RepositoryArtifactIndexFactory.ROLE, "lucene" ); + + indexLocation = getTestFile( "target/test-index" ); + + FileUtils.deleteDirectory( indexLocation ); + + index = factory.createStandardIndex( indexLocation ); + + records.put( "test-jar", recordFactory.createRecord( createArtifact( "test-jar" ) ) ); + records.put( "test-jar-jdk14", + recordFactory.createRecord( createArtifact( "test-jar", "1.0", "jar", "jdk14" ) ) ); + records.put( "test-jar-and-pom", + recordFactory.createRecord( createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar" ) ) ); + records.put( "test-jar-and-pom-jdk14", recordFactory.createRecord( + createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar", "jdk14" ) ) ); + records.put( "test-child-pom", + recordFactory.createRecord( createArtifact( "test-child-pom", "1.0-20060728.121314-1", "jar" ) ) ); + records.put( "test-archetype", recordFactory.createRecord( createArtifact( "test-archetype" ) ) ); + records.put( "test-plugin", recordFactory.createRecord( createArtifact( "test-plugin" ) ) ); + records.put( "test-pom", recordFactory.createRecord( createArtifact( "test-pom", "1.0", "pom" ) ) ); + records.put( "parent-pom", recordFactory.createRecord( createArtifact( "parent-pom", "1", "pom" ) ) ); + records.put( "test-dll", recordFactory.createRecord( createArtifact( "test-dll", "1.0.1.34", "dll" ) ) ); + + index.indexRecords( records.values() ); + } + + public void testExactMatchVersion() + throws RepositoryIndexSearchException + { + Query query = new TermQuery( new Term( StandardIndexRecordFields.VERSION_EXACT, "1.0" ) ); + List results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-pom" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-archetype" ) ) ); + assertEquals( "Check results size", 5, results.size() ); + + query = new TermQuery( new Term( StandardIndexRecordFields.VERSION_EXACT, "1.0-SNAPSHOT" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check results size", results.isEmpty() ); + + query = new TermQuery( new Term( StandardIndexRecordFields.VERSION_EXACT, "1.0-20060728.121314-1" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) ); + assertEquals( "Check results size", 1, results.size() ); + + // test non-match fails + query = new TermQuery( new Term( StandardIndexRecordFields.VERSION_EXACT, "foo" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check results size", results.isEmpty() ); + } + + public void testExactMatchBaseVersion() + throws RepositoryIndexSearchException + { + Query query = new TermQuery( new Term( StandardIndexRecordFields.BASE_VERSION_EXACT, "1.0" ) ); + List results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-pom" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-archetype" ) ) ); + assertEquals( "Check results size", 5, results.size() ); + + query = new TermQuery( new Term( StandardIndexRecordFields.BASE_VERSION_EXACT, "1.0-SNAPSHOT" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) ); + assertEquals( "Check results size", 1, results.size() ); + + query = new TermQuery( new Term( StandardIndexRecordFields.BASE_VERSION_EXACT, "1.0-20060728.121314-1" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check results size", results.isEmpty() ); + + // test non-match fails + query = new TermQuery( new Term( StandardIndexRecordFields.BASE_VERSION_EXACT, "foo" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check results size", results.isEmpty() ); + } + + public void testExactMatchGroupId() + throws RepositoryIndexSearchException + { + Query query = + new TermQuery( new Term( StandardIndexRecordFields.GROUPID_EXACT, "org.apache.maven.archiva.record" ) ); + List results = index.search( new LuceneQuery( query ) ); + + assertEquals( "Check results size", 10, results.size() ); + + // test partial match fails + query = new TermQuery( new Term( StandardIndexRecordFields.GROUPID_EXACT, "org.apache.maven" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check results size", results.isEmpty() ); + + // test non-match fails + query = new TermQuery( new Term( StandardIndexRecordFields.GROUPID_EXACT, "foo" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check results size", results.isEmpty() ); + } + + public void testExactMatchArtifactId() + throws RepositoryIndexSearchException + { + Query query = new TermQuery( new Term( StandardIndexRecordFields.ARTIFACTID_EXACT, "test-jar" ) ); + List results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) ); + assertEquals( "Check results size", 2, results.size() ); + + // test partial match fails + query = new TermQuery( new Term( StandardIndexRecordFields.ARTIFACTID_EXACT, "test" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check results size", results.isEmpty() ); + + // test non-match fails + query = new TermQuery( new Term( StandardIndexRecordFields.ARTIFACTID_EXACT, "foo" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check results size", results.isEmpty() ); + } + + public void testExactMatchType() + throws RepositoryIndexSearchException + { + Query query = new TermQuery( new Term( StandardIndexRecordFields.TYPE, "maven-plugin" ) ); + List results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) ); + assertEquals( "Check results size", 1, results.size() ); + + query = new TermQuery( new Term( StandardIndexRecordFields.TYPE, "jar" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) ); + assertEquals( "Check results size", 5, results.size() ); + + query = new TermQuery( new Term( StandardIndexRecordFields.TYPE, "dll" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check result", results.contains( records.get( "test-dll" ) ) ); + assertEquals( "Check results size", 1, results.size() ); + + query = new TermQuery( new Term( StandardIndexRecordFields.TYPE, "maven-archetype" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check result", results.contains( records.get( "test-archetype" ) ) ); + assertEquals( "Check results size", 1, results.size() ); + + // test non-match fails + query = new TermQuery( new Term( StandardIndexRecordFields.TYPE, "foo" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check results size", results.isEmpty() ); + } + + public void testExactMatchPackaging() + throws RepositoryIndexSearchException + { + Query query = new TermQuery( new Term( StandardIndexRecordFields.PACKAGING, "maven-plugin" ) ); + List results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) ); + assertEquals( "Check results size", 1, results.size() ); + + query = new TermQuery( new Term( StandardIndexRecordFields.PACKAGING, "jar" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check result", results.contains( records.get( "test-archetype" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) ); + assertEquals( "Check results size", 4, results.size() ); + + query = new TermQuery( new Term( StandardIndexRecordFields.PACKAGING, "dll" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check results size", results.isEmpty() ); + + query = new TermQuery( new Term( StandardIndexRecordFields.PACKAGING, "maven-archetype" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check results size", results.isEmpty() ); + + // test non-match fails + query = new TermQuery( new Term( StandardIndexRecordFields.PACKAGING, "foo" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check results size", results.isEmpty() ); + } + + public void testExactMatchPluginPrefix() + throws RepositoryIndexSearchException + { + Query query = new TermQuery( new Term( StandardIndexRecordFields.PLUGIN_PREFIX, "test" ) ); + List results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) ); + assertEquals( "Check results size", 1, results.size() ); + + // test non-match fails + query = new TermQuery( new Term( StandardIndexRecordFields.PLUGIN_PREFIX, "foo" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check results size", results.isEmpty() ); + } + + public void testExactMatchRepository() + throws RepositoryIndexSearchException + { + Query query = new TermQuery( new Term( StandardIndexRecordFields.REPOSITORY, "test" ) ); + List results = index.search( new LuceneQuery( query ) ); + + assertEquals( "Check results size", 10, results.size() ); + + // test non-match fails + query = new TermQuery( new Term( StandardIndexRecordFields.REPOSITORY, "foo" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check results size", results.isEmpty() ); + } + + public void testExactMatchMd5() + throws RepositoryIndexSearchException + { + Query query = new TermQuery( new Term( StandardIndexRecordFields.MD5, "3a0adc365f849366cd8b633cad155cb7" ) ); + List results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) ); + assertEquals( "Check results size", 5, results.size() ); + + // test non-match fails + query = new TermQuery( new Term( StandardIndexRecordFields.MD5, "foo" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check results size", results.isEmpty() ); + } + + public void testExactMatchSha1() + throws RepositoryIndexSearchException + { + Query query = + new TermQuery( new Term( StandardIndexRecordFields.SHA1, "c66f18bf192cb613fc2febb4da541a34133eedc2" ) ); + List results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) ); + assertEquals( "Check results size", 5, results.size() ); + + // test non-match fails + query = new TermQuery( new Term( StandardIndexRecordFields.SHA1, "foo" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check results size", results.isEmpty() ); + } + + public void testExactMatchInceptionYear() + throws RepositoryIndexSearchException + { + Query query = new TermQuery( new Term( StandardIndexRecordFields.INCEPTION_YEAR, "2005" ) ); + List results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-pom" ) ) ); + assertTrue( "Check result", results.contains( records.get( "parent-pom" ) ) ); + assertEquals( "Check results size", 3, results.size() ); + + // test non-match fails + query = new TermQuery( new Term( StandardIndexRecordFields.INCEPTION_YEAR, "foo" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check results size", results.isEmpty() ); + } + + public void testMatchFilename() + throws RepositoryIndexSearchException + { + Query query = new TermQuery( new Term( StandardIndexRecordFields.FILENAME, "maven" ) ); + List results = index.search( new LuceneQuery( query ) ); + + assertEquals( "Check results size", 10, results.size() ); + +/* TODO: if this is a result we want, we need to change the analyzer. Currently, it is tokenizing it as plugin-1.0 and plugin/1.0 in the path + query = new TermQuery( new Term( StandardIndexRecordFields.FILENAME, "plugin" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) ); + assertEquals( "Check results size", 1, results.size() ); +*/ + query = new TermQuery( new Term( StandardIndexRecordFields.FILENAME, "test" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertFalse( "Check result", results.contains( records.get( "parent-pom" ) ) ); + assertEquals( "Check results size", 9, results.size() ); + + // test non-match fails + query = new TermQuery( new Term( StandardIndexRecordFields.FILENAME, "foo" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check results size", results.isEmpty() ); + } + + public void testMatchGroupId() + throws RepositoryIndexSearchException + { + Query query = new TermQuery( new Term( StandardIndexRecordFields.GROUPID, "org.apache.maven.archiva.record" ) ); + List results = index.search( new LuceneQuery( query ) ); + + assertEquals( "Check results size", 10, results.size() ); + +/* TODO: if we want this result, must change the analyzer to split on '.' + query = new TermQuery( new Term( StandardIndexRecordFields.GROUPID, "maven" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertEquals( "Check results size", 10, results.size() ); +*/ + + // test non-match fails + query = new TermQuery( new Term( StandardIndexRecordFields.GROUPID, "foo" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check results size", results.isEmpty() ); + } + + public void testMatchArtifactId() + throws RepositoryIndexSearchException + { + Query query = new TermQuery( new Term( StandardIndexRecordFields.ARTIFACTID, "plugin" ) ); + List results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) ); + assertEquals( "Check results size", 1, results.size() ); + + query = new TermQuery( new Term( StandardIndexRecordFields.ARTIFACTID, "test" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertFalse( "Check result", results.contains( records.get( "parent-pom" ) ) ); + assertEquals( "Check results size", 9, results.size() ); + + // test non-match fails + query = new TermQuery( new Term( StandardIndexRecordFields.ARTIFACTID, "maven" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check results size", results.isEmpty() ); + } + + public void testMatchVersion() + throws RepositoryIndexSearchException + { + // If partial matches are desired, need to change the analyzer for versions to split on '.' + Query query = new TermQuery( new Term( StandardIndexRecordFields.VERSION, "1" ) ); + List results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check result", results.contains( records.get( "parent-pom" ) ) ); + assertEquals( "Check results size", 1, results.size() ); + + query = new TermQuery( new Term( StandardIndexRecordFields.VERSION, "1.0" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-pom" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-archetype" ) ) ); + assertEquals( "Check results size", 5, results.size() ); + +/* TODO: need to change analyzer to split on - if we want this + query = new TermQuery( new Term( StandardIndexRecordFields.VERSION, "snapshot" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check results size", results.isEmpty() ); + + query = new TermQuery( new Term( StandardIndexRecordFields.VERSION, "alpha" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) ); + assertEquals( "Check results size", 2, results.size() ); +*/ + + // test non-match fails + query = new TermQuery( new Term( StandardIndexRecordFields.VERSION, "foo" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check results size", results.isEmpty() ); + } + + public void testMatchBaseVersion() + throws RepositoryIndexSearchException + { + // If partial matches are desired, need to change the analyzer for versions to split on '.' + Query query = new TermQuery( new Term( StandardIndexRecordFields.BASE_VERSION, "1" ) ); + List results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check result", results.contains( records.get( "parent-pom" ) ) ); + assertEquals( "Check results size", 1, results.size() ); + + query = new TermQuery( new Term( StandardIndexRecordFields.BASE_VERSION, "1.0" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-pom" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-archetype" ) ) ); + assertEquals( "Check results size", 5, results.size() ); + +/* TODO: need to change analyzer to split on - if we want this + query = new TermQuery( new Term( StandardIndexRecordFields.BASE_VERSION, "snapshot" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) ); + assertEquals( "Check results size", 1, results.size() ); + + query = new TermQuery( new Term( StandardIndexRecordFields.BASE_VERSION, "alpha" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) ); + assertEquals( "Check results size", 2, results.size() ); +*/ + + // test non-match fails + query = new TermQuery( new Term( StandardIndexRecordFields.BASE_VERSION, "foo" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check results size", results.isEmpty() ); + } + + public void testMatchClassifier() + throws RepositoryIndexSearchException + { + BooleanQuery bQuery = new BooleanQuery(); + bQuery.add( new MatchAllDocsQuery(), BooleanClause.Occur.MUST ); + bQuery.add( new TermQuery( new Term( StandardIndexRecordFields.CLASSIFIER, "jdk14" ) ), + BooleanClause.Occur.MUST_NOT ); + List results = index.search( new LuceneQuery( bQuery ) ); + + assertFalse( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) ); + assertFalse( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) ); + assertEquals( "Check results size", 8, results.size() ); + + // TODO: can we search for "anything with no classifier" ? + + Query query = new TermQuery( new Term( StandardIndexRecordFields.CLASSIFIER, "jdk14" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) ); + assertEquals( "Check results size", 2, results.size() ); + + // test non-match fails + query = new TermQuery( new Term( StandardIndexRecordFields.CLASSIFIER, "foo" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check results size", results.isEmpty() ); + } + + public void testMatchClass() + throws RepositoryIndexSearchException + { + // TODO: should be preserving case! + Query query = new TermQuery( new Term( StandardIndexRecordFields.CLASSES, "b.c.c" ) ); + List results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) ); + assertEquals( "Check results size", 5, results.size() ); + +/* TODO!: need to change the analyzer if we want partial classes (split on '.') + query = new TermQuery( new Term( StandardIndexRecordFields.CLASSES, "C" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) ); + assertEquals( "Check results size", 4, results.size() ); + + query = new TermQuery( new Term( StandardIndexRecordFields.CLASSES, "MyMojo" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) ); + assertEquals( "Check results size", 1, results.size() ); +*/ + + // test non-match fails + query = new TermQuery( new Term( StandardIndexRecordFields.CLASSES, "foo" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check results size", results.isEmpty() ); + } + + public void testMatchFiles() + throws RepositoryIndexSearchException + { + // TODO: should be preserving case! + Query query = new TermQuery( new Term( StandardIndexRecordFields.FILES, "manifest.mf" ) ); + List results = index.search( new LuceneQuery( query ) ); + + assertFalse( "Check result", results.contains( records.get( "test-pom" ) ) ); + assertFalse( "Check result", results.contains( records.get( "parent-pom" ) ) ); + assertFalse( "Check result", results.contains( records.get( "test-dll" ) ) ); + assertEquals( "Check results size", 7, results.size() ); + +/* + // TODO: should be preserving case, and '-inf'! + query = new TermQuery( new Term( StandardIndexRecordFields.FILES, "meta-inf" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertFalse( "Check result", results.contains( records.get( "test-pom" ) ) ); + assertFalse( "Check result", results.contains( records.get( "parent-pom" ) ) ); + assertFalse( "Check result", results.contains( records.get( "test-dll" ) ) ); + assertEquals( "Check results size", 7, results.size() ); +*/ + + query = new TermQuery( new Term( StandardIndexRecordFields.FILES, "plugin.xml" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) ); + assertEquals( "Check results size", 1, results.size() ); + + // test non-match fails + query = new TermQuery( new Term( StandardIndexRecordFields.FILES, "foo" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check results size", results.isEmpty() ); + } + + public void testMatchProjectName() + throws RepositoryIndexSearchException + { + Query query = new TermQuery( new Term( StandardIndexRecordFields.PROJECT_NAME, "mojo" ) ); + List results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) ); + assertEquals( "Check results size", 1, results.size() ); + + query = new TermQuery( new Term( StandardIndexRecordFields.PROJECT_NAME, "maven" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertFalse( "Check result", results.contains( records.get( "parent-pom" ) ) ); + assertFalse( "Check result", results.contains( records.get( "test-child-pom" ) ) ); + assertEquals( "Check results size", 2, results.size() ); + + // test non-match fails + query = new TermQuery( new Term( StandardIndexRecordFields.PROJECT_NAME, "foo" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check results size", results.isEmpty() ); + } + + public void testMatchProjectDescription() + throws RepositoryIndexSearchException + { + Query query = new TermQuery( new Term( StandardIndexRecordFields.PROJECT_DESCRIPTION, "description" ) ); + List results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) ); + assertTrue( "Check result", results.contains( records.get( "parent-pom" ) ) ); + assertTrue( "Check result", results.contains( records.get( "test-pom" ) ) ); + assertEquals( "Check results size", 3, results.size() ); + + // test non-match fails + query = new TermQuery( new Term( StandardIndexRecordFields.PROJECT_DESCRIPTION, "foo" ) ); + results = index.search( new LuceneQuery( query ) ); + + assertTrue( "Check results size", results.isEmpty() ); + } + + private Artifact createArtifact( String artifactId ) + { + return createArtifact( artifactId, "1.0", "jar", null ); + } + + private Artifact createArtifact( String artifactId, String version, String type ) + { + return createArtifact( artifactId, version, type, null ); + } + + private Artifact createArtifact( String artifactId, String version, String type, String classifier ) + { + Artifact artifact = artifactFactory.createDependencyArtifact( "org.apache.maven.archiva.record", artifactId, + VersionRange.createFromVersion( version ), type, + classifier, Artifact.SCOPE_RUNTIME ); + artifact.isSnapshot(); + artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); + artifact.setRepository( repository ); + return artifact; + } +} diff --git a/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/lucene/LuceneStandardArtifactIndexTest.java b/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/lucene/LuceneStandardArtifactIndexTest.java new file mode 100644 index 000000000..8b7a74e20 --- /dev/null +++ b/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/lucene/LuceneStandardArtifactIndexTest.java @@ -0,0 +1,407 @@ +package org.apache.maven.archiva.indexer.lucene; + +/* + * Copyright 2005-2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.lucene.analysis.standard.StandardAnalyzer; +import org.apache.lucene.document.Document; +import org.apache.lucene.document.NumberTools; +import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.IndexWriter; +import org.apache.maven.archiva.indexer.RepositoryArtifactIndex; +import org.apache.maven.archiva.indexer.RepositoryArtifactIndexFactory; +import org.apache.maven.archiva.indexer.RepositoryIndexException; +import org.apache.maven.archiva.indexer.record.RepositoryIndexRecord; +import org.apache.maven.archiva.indexer.record.RepositoryIndexRecordFactory; +import org.apache.maven.archiva.indexer.record.StandardIndexRecordFields; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.factory.ArtifactFactory; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; +import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; +import org.codehaus.plexus.PlexusTestCase; +import org.codehaus.plexus.util.FileUtils; +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; + +import java.io.File; +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.Collections; +import java.util.Date; +import java.util.Iterator; +import java.util.List; +import java.util.Locale; +import java.util.TimeZone; + +/** + * Test the Lucene implementation of the artifact index. + * + * @author Brett Porter + */ +public class LuceneStandardArtifactIndexTest + extends PlexusTestCase +{ + private RepositoryArtifactIndex index; + + private ArtifactRepository repository; + + private ArtifactFactory artifactFactory; + + private File indexLocation; + + private RepositoryIndexRecordFactory recordFactory; + + protected void setUp() + throws Exception + { + super.setUp(); + + recordFactory = (RepositoryIndexRecordFactory) lookup( RepositoryIndexRecordFactory.ROLE, "standard" ); + + artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE ); + + ArtifactRepositoryFactory repositoryFactory = + (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE ); + + ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" ); + + File file = getTestFile( "src/test/managed-repository" ); + repository = + repositoryFactory.createArtifactRepository( "test", file.toURI().toURL().toString(), layout, null, null ); + + RepositoryArtifactIndexFactory factory = + (RepositoryArtifactIndexFactory) lookup( RepositoryArtifactIndexFactory.ROLE, "lucene" ); + + indexLocation = getTestFile( "target/test-index" ); + + FileUtils.deleteDirectory( indexLocation ); + + index = factory.createStandardIndex( indexLocation ); + } + + public void testIndexExists() + throws IOException, RepositoryIndexException + { + assertFalse( "check index doesn't exist", index.exists() ); + + // create empty directory + indexLocation.mkdirs(); + assertFalse( "check index doesn't exist even if directory does", index.exists() ); + + // create index, with no records + createEmptyIndex(); + assertTrue( "check index is considered to exist", index.exists() ); + + // Test non-directory + FileUtils.deleteDirectory( indexLocation ); + indexLocation.createNewFile(); + try + { + index.exists(); + fail( "Index operation should fail as the location is not valid" ); + } + catch ( RepositoryIndexException e ) + { + // great + } + finally + { + indexLocation.delete(); + } + } + + public void testAddRecordNoIndex() + throws IOException, RepositoryIndexException + { + Artifact artifact = createArtifact( "test-jar" ); + + RepositoryIndexRecord record = recordFactory.createRecord( artifact ); + index.indexRecords( Collections.singletonList( record ) ); + + IndexReader reader = IndexReader.open( indexLocation ); + try + { + Document document = reader.document( 0 ); + assertJarRecord( artifact, document ); + assertEquals( "Check index size", 1, reader.numDocs() ); + } + finally + { + reader.close(); + } + } + + public void testAddRecordExistingEmptyIndex() + throws IOException, RepositoryIndexException + { + createEmptyIndex(); + + Artifact artifact = createArtifact( "test-jar" ); + + RepositoryIndexRecord record = recordFactory.createRecord( artifact ); + index.indexRecords( Collections.singletonList( record ) ); + + IndexReader reader = IndexReader.open( indexLocation ); + try + { + Document document = reader.document( 0 ); + assertJarRecord( artifact, document ); + assertEquals( "Check index size", 1, reader.numDocs() ); + } + finally + { + reader.close(); + } + } + + public void testAddRecordInIndex() + throws IOException, RepositoryIndexException + { + createEmptyIndex(); + + Artifact artifact = createArtifact( "test-jar" ); + + RepositoryIndexRecord record = recordFactory.createRecord( artifact ); + index.indexRecords( Collections.singletonList( record ) ); + + // Do it again + record = recordFactory.createRecord( artifact ); + index.indexRecords( Collections.singletonList( record ) ); + + IndexReader reader = IndexReader.open( indexLocation ); + try + { + Document document = reader.document( 0 ); + assertJarRecord( artifact, document ); + assertEquals( "Check index size", 1, reader.numDocs() ); + } + finally + { + reader.close(); + } + } + + public void testAddPomRecord() + throws IOException, RepositoryIndexException + { + createEmptyIndex(); + + Artifact artifact = createArtifact( "test-pom", "1.0", "pom" ); + + RepositoryIndexRecord record = recordFactory.createRecord( artifact ); + index.indexRecords( Collections.singletonList( record ) ); + + IndexReader reader = IndexReader.open( indexLocation ); + try + { + Document document = reader.document( 0 ); + assertPomRecord( artifact, document ); + assertEquals( "Check index size", 1, reader.numDocs() ); + } + finally + { + reader.close(); + } + } + + public void testAddPlugin() + throws IOException, RepositoryIndexException, XmlPullParserException + { + createEmptyIndex(); + + Artifact artifact = createArtifact( "test-plugin" ); + + RepositoryIndexRecord record = recordFactory.createRecord( artifact ); + + index.indexRecords( Collections.singletonList( record ) ); + + IndexReader reader = IndexReader.open( indexLocation ); + try + { + Document document = reader.document( 0 ); + assertPluginRecord( artifact, document ); + assertEquals( "Check index size", 1, reader.numDocs() ); + } + finally + { + reader.close(); + } + } + + public void testDeleteRecordInIndex() + throws IOException, RepositoryIndexException + { + createEmptyIndex(); + + Artifact artifact = createArtifact( "test-jar" ); + + RepositoryIndexRecord record = recordFactory.createRecord( artifact ); + index.indexRecords( Collections.singletonList( record ) ); + + index.deleteRecords( Collections.singletonList( record ) ); + + IndexReader reader = IndexReader.open( indexLocation ); + try + { + assertEquals( "No documents", 0, reader.numDocs() ); + } + finally + { + reader.close(); + } + } + + public void testDeleteRecordNotInIndex() + throws IOException, RepositoryIndexException + { + createEmptyIndex(); + + Artifact artifact = createArtifact( "test-jar" ); + + RepositoryIndexRecord record = recordFactory.createRecord( artifact ); + + index.deleteRecords( Collections.singletonList( record ) ); + + IndexReader reader = IndexReader.open( indexLocation ); + try + { + assertEquals( "No documents", 0, reader.numDocs() ); + } + finally + { + reader.close(); + } + } + + public void testDeleteRecordNoIndex() + throws IOException, RepositoryIndexException + { + Artifact artifact = createArtifact( "test-jar" ); + + RepositoryIndexRecord record = recordFactory.createRecord( artifact ); + index.deleteRecords( Collections.singleton( record ) ); + + assertFalse( index.exists() ); + } + + private Artifact createArtifact( String artifactId ) + { + return createArtifact( artifactId, "1.0", "jar" ); + } + + private Artifact createArtifact( String artifactId, String version, String type ) + { + Artifact artifact = + artifactFactory.createBuildArtifact( "org.apache.maven.archiva.record", artifactId, version, type ); + artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); + artifact.setRepository( repository ); + return artifact; + } + + private void createEmptyIndex() + throws IOException + { + createIndex( Collections.EMPTY_LIST ); + } + + private void createIndex( List docments ) + throws IOException + { + IndexWriter writer = new IndexWriter( indexLocation, new StandardAnalyzer(), true ); + for ( Iterator i = docments.iterator(); i.hasNext(); ) + { + Document document = (Document) i.next(); + writer.addDocument( document ); + } + writer.optimize(); + writer.close(); + } + + private void assertRecord( Artifact artifact, Document document, String expectedArtifactId, String expectedType, + String expectedMd5, String expectedSha1 ) + { + assertEquals( "Check document filename", repository.pathOf( artifact ), + document.get( StandardIndexRecordFields.FILENAME ) ); + assertEquals( "Check document groupId", "org.apache.maven.archiva.record", + document.get( StandardIndexRecordFields.GROUPID ) ); + assertEquals( "Check document artifactId", expectedArtifactId, + document.get( StandardIndexRecordFields.ARTIFACTID ) ); + assertEquals( "Check document version", "1.0", document.get( StandardIndexRecordFields.VERSION ) ); + assertEquals( "Check document type", expectedType, document.get( StandardIndexRecordFields.TYPE ) ); + assertEquals( "Check document repository", "test", document.get( StandardIndexRecordFields.REPOSITORY ) ); + assertEquals( "Check document timestamp", getLastModified( artifact.getFile() ), + document.get( StandardIndexRecordFields.LAST_MODIFIED ) ); + assertEquals( "Check document md5", expectedMd5, document.get( StandardIndexRecordFields.MD5 ) ); + assertEquals( "Check document sha1", expectedSha1, document.get( StandardIndexRecordFields.SHA1 ) ); + assertEquals( "Check document file size", artifact.getFile().length(), + NumberTools.stringToLong( document.get( StandardIndexRecordFields.FILE_SIZE ) ) ); + assertNull( "Check document classifier", document.get( StandardIndexRecordFields.CLASSIFIER ) ); + } + + private void assertPomRecord( Artifact artifact, Document document ) + { + assertRecord( artifact, document, "test-pom", "pom", "98b4a1b708a90a8637aaf541bef5094f", + "d95348bee1666a46511260696292bfa0519b61c1" ); + assertNull( "Check document classes", document.get( StandardIndexRecordFields.CLASSES ) ); + assertNull( "Check document files", document.get( StandardIndexRecordFields.FILES ) ); + assertNull( "Check document pluginPrefix", document.get( StandardIndexRecordFields.PLUGIN_PREFIX ) ); + assertEquals( "Check document year", "2005", document.get( StandardIndexRecordFields.INCEPTION_YEAR ) ); + assertEquals( "Check document project name", "Maven Repository Manager Test POM", + document.get( StandardIndexRecordFields.PROJECT_NAME ) ); + assertEquals( "Check document project description", "Description", + document.get( StandardIndexRecordFields.PROJECT_DESCRIPTION ) ); + assertEquals( "Check document packaging", "pom", document.get( StandardIndexRecordFields.PACKAGING ) ); + } + + private void assertJarRecord( Artifact artifact, Document document ) + { + assertRecord( artifact, document, "test-jar", "jar", "3a0adc365f849366cd8b633cad155cb7", + "c66f18bf192cb613fc2febb4da541a34133eedc2" ); + assertEquals( "Check document classes", "A\nb.B\nb.c.C", document.get( StandardIndexRecordFields.CLASSES ) ); + assertEquals( "Check document files", "META-INF/MANIFEST.MF\nA.class\nb/B.class\nb/c/C.class", + document.get( StandardIndexRecordFields.FILES ) ); + assertNull( "Check document inceptionYear", document.get( StandardIndexRecordFields.INCEPTION_YEAR ) ); + assertNull( "Check document projectName", document.get( StandardIndexRecordFields.PROJECT_NAME ) ); + assertNull( "Check document projectDesc", document.get( StandardIndexRecordFields.PROJECT_DESCRIPTION ) ); + assertNull( "Check document pluginPrefix", document.get( StandardIndexRecordFields.PLUGIN_PREFIX ) ); + assertNull( "Check document packaging", document.get( StandardIndexRecordFields.PACKAGING ) ); + } + + private void assertPluginRecord( Artifact artifact, Document document ) + { + assertRecord( artifact, document, "test-plugin", "maven-plugin", "3530896791670ebb45e17708e5d52c40", + "2cd2619d59a684e82e97471d2c2e004144c8f24e" ); + assertEquals( "Check document classes", "org.apache.maven.archiva.record.MyMojo", + document.get( StandardIndexRecordFields.CLASSES ) ); + assertEquals( "Check document files", "META-INF/MANIFEST.MF\n" + + "META-INF/maven/org.apache.maven.archiva.record/test-plugin/pom.properties\n" + + "META-INF/maven/org.apache.maven.archiva.record/test-plugin/pom.xml\n" + "META-INF/maven/plugin.xml\n" + + "org/apache/maven/archiva/record/MyMojo.class", document.get( StandardIndexRecordFields.FILES ) ); + assertEquals( "Check document pluginPrefix", "test", document.get( StandardIndexRecordFields.PLUGIN_PREFIX ) ); + assertEquals( "Check document packaging", "maven-plugin", document.get( StandardIndexRecordFields.PACKAGING ) ); + assertNull( "Check document inceptionYear", document.get( StandardIndexRecordFields.INCEPTION_YEAR ) ); + assertEquals( "Check document project name", "Maven Mojo Archetype", + document.get( StandardIndexRecordFields.PROJECT_NAME ) ); + assertNull( "Check document projectDesc", document.get( StandardIndexRecordFields.PROJECT_DESCRIPTION ) ); + } + + private String getLastModified( File file ) + { + SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyyMMddHHmmss", Locale.US ); + dateFormat.setTimeZone( TimeZone.getTimeZone( "UTC" ) ); + return dateFormat.format( new Date( file.lastModified() ) ); + } +} diff --git a/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/query/QueryTest.java b/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/query/QueryTest.java new file mode 100644 index 000000000..bcd237758 --- /dev/null +++ b/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/query/QueryTest.java @@ -0,0 +1,156 @@ +package org.apache.maven.archiva.indexer.query; + +/* + * Copyright 2005-2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import junit.framework.TestCase; + +import java.util.Iterator; + +/** + * @author Brett Porter + */ +public class QueryTest + extends TestCase +{ + private QueryTerm term1 = new QueryTerm( "field1", "value1" ); + + private QueryTerm term2 = new QueryTerm( "field2", "value2" ); + + private QueryTerm term3 = new QueryTerm( "field3", "value3" ); + + public void testQueryTerm() + { + QueryTerm query = new QueryTerm( "Field", "Value" ); + assertEquals( "check field setting", "Field", query.getField() ); + assertEquals( "check value setting", "Value", query.getValue() ); + } + + public void testSingleTermQuery() + { + SingleTermQuery query = new SingleTermQuery( "Field", "Value" ); + assertEquals( "check field setting", "Field", query.getField() ); + assertEquals( "check value setting", "Value", query.getValue() ); + + query = new SingleTermQuery( term1 ); + assertEquals( "check field setting", "field1", query.getField() ); + assertEquals( "check value setting", "value1", query.getValue() ); + } + + public void testRangeQueryOpen() + { + RangeQuery rangeQuery = RangeQuery.createOpenRange(); + assertNull( "Check range has no start", rangeQuery.getBegin() ); + assertNull( "Check range has no end", rangeQuery.getEnd() ); + } + + public void testRangeQueryExclusive() + { + RangeQuery rangeQuery = RangeQuery.createExclusiveRange( term1, term2 ); + assertEquals( "Check range start", term1, rangeQuery.getBegin() ); + assertEquals( "Check range end", term2, rangeQuery.getEnd() ); + assertFalse( "Check exclusive", rangeQuery.isInclusive() ); + } + + public void testRangeQueryInclusive() + { + RangeQuery rangeQuery = RangeQuery.createInclusiveRange( term1, term2 ); + assertEquals( "Check range start", term1, rangeQuery.getBegin() ); + assertEquals( "Check range end", term2, rangeQuery.getEnd() ); + assertTrue( "Check inclusive", rangeQuery.isInclusive() ); + } + + public void testRangeQueryOpenEnded() + { + RangeQuery rangeQuery = RangeQuery.createGreaterThanOrEqualToRange( term1 ); + assertEquals( "Check range start", term1, rangeQuery.getBegin() ); + assertNull( "Check range end", rangeQuery.getEnd() ); + assertTrue( "Check inclusive", rangeQuery.isInclusive() ); + + rangeQuery = RangeQuery.createGreaterThanRange( term1 ); + assertEquals( "Check range start", term1, rangeQuery.getBegin() ); + assertNull( "Check range end", rangeQuery.getEnd() ); + assertFalse( "Check exclusive", rangeQuery.isInclusive() ); + + rangeQuery = RangeQuery.createLessThanOrEqualToRange( term1 ); + assertNull( "Check range start", rangeQuery.getBegin() ); + assertEquals( "Check range end", term1, rangeQuery.getEnd() ); + assertTrue( "Check inclusive", rangeQuery.isInclusive() ); + + rangeQuery = RangeQuery.createLessThanRange( term1 ); + assertNull( "Check range start", rangeQuery.getBegin() ); + assertEquals( "Check range end", term1, rangeQuery.getEnd() ); + assertFalse( "Check exclusive", rangeQuery.isInclusive() ); + } + + public void testCompundQuery() + { + CompoundQuery query = new CompoundQuery(); + assertTrue( "check query is empty", query.getCompoundQueryTerms().isEmpty() ); + + query.and( term1 ); + query.or( term2 ); + query.not( term3 ); + + Iterator i = query.getCompoundQueryTerms().iterator(); + CompoundQueryTerm term = (CompoundQueryTerm) i.next(); + assertEquals( "Check first term", "field1", getQuery( term ).getField() ); + assertEquals( "Check first term", "value1", getQuery( term ).getValue() ); + assertTrue( "Check first term", term.isRequired() ); + assertFalse( "Check first term", term.isProhibited() ); + + term = (CompoundQueryTerm) i.next(); + assertEquals( "Check second term", "field2", getQuery( term ).getField() ); + assertEquals( "Check second term", "value2", getQuery( term ).getValue() ); + assertFalse( "Check second term", term.isRequired() ); + assertFalse( "Check second term", term.isProhibited() ); + + term = (CompoundQueryTerm) i.next(); + assertEquals( "Check third term", "field3", getQuery( term ).getField() ); + assertEquals( "Check third term", "value3", getQuery( term ).getValue() ); + assertFalse( "Check third term", term.isRequired() ); + assertTrue( "Check third term", term.isProhibited() ); + + CompoundQuery query2 = new CompoundQuery(); + query2.and( query ); + query2.or( new SingleTermQuery( term2 ) ); + query2.not( new SingleTermQuery( term3 ) ); + + i = query2.getCompoundQueryTerms().iterator(); + term = (CompoundQueryTerm) i.next(); + assertEquals( "Check first term", query, term.getQuery() ); + assertTrue( "Check first term", term.isRequired() ); + assertFalse( "Check first term", term.isProhibited() ); + + term = (CompoundQueryTerm) i.next(); + assertEquals( "Check second term", "field2", getQuery( term ).getField() ); + assertEquals( "Check second term", "value2", getQuery( term ).getValue() ); + assertFalse( "Check second term", term.isRequired() ); + assertFalse( "Check second term", term.isProhibited() ); + + term = (CompoundQueryTerm) i.next(); + assertEquals( "Check third term", "field3", getQuery( term ).getField() ); + assertEquals( "Check third term", "value3", getQuery( term ).getValue() ); + assertFalse( "Check third term", term.isRequired() ); + assertTrue( "Check third term", term.isProhibited() ); + } + + private static SingleTermQuery getQuery( CompoundQueryTerm term ) + { + return (SingleTermQuery) term.getQuery(); + } +} + diff --git a/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/record/MinimalArtifactIndexRecordFactoryTest.java b/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/record/MinimalArtifactIndexRecordFactoryTest.java new file mode 100644 index 000000000..a2aeba0b3 --- /dev/null +++ b/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/record/MinimalArtifactIndexRecordFactoryTest.java @@ -0,0 +1,240 @@ +package org.apache.maven.archiva.indexer.record; + +/* + * 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.archiva.indexer.RepositoryIndexException; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.factory.ArtifactFactory; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; +import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; +import org.apache.maven.artifact.versioning.VersionRange; +import org.codehaus.plexus.PlexusTestCase; +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; + +import java.io.File; +import java.io.IOException; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +/** + * Test the minimal artifact index record. + * + * @author Brett Porter + */ +public class MinimalArtifactIndexRecordFactoryTest + extends PlexusTestCase +{ + private RepositoryIndexRecordFactory factory; + + private ArtifactRepository repository; + + private ArtifactFactory artifactFactory; + + private static final String TEST_GROUP_ID = "org.apache.maven.archiva.record"; + + private static final List JAR_CLASS_LIST = Arrays.asList( new String[]{"A", "b.B", "b.c.C"} ); + + protected void setUp() + throws Exception + { + super.setUp(); + + factory = (RepositoryIndexRecordFactory) lookup( RepositoryIndexRecordFactory.ROLE, "minimal" ); + + artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE ); + + ArtifactRepositoryFactory repositoryFactory = + (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE ); + + ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" ); + + File file = getTestFile( "src/test/managed-repository" ); + repository = + repositoryFactory.createArtifactRepository( "test", file.toURI().toURL().toString(), layout, null, null ); + } + + public void testIndexedJar() + throws RepositoryIndexException + { + Artifact artifact = createArtifact( "test-jar" ); + + RepositoryIndexRecord record = factory.createRecord( artifact ); + + MinimalArtifactIndexRecord expectedRecord = new MinimalArtifactIndexRecord(); + expectedRecord.setMd5Checksum( "3a0adc365f849366cd8b633cad155cb7" ); + expectedRecord.setFilename( repository.pathOf( artifact ) ); + expectedRecord.setLastModified( artifact.getFile().lastModified() ); + expectedRecord.setSize( artifact.getFile().length() ); + expectedRecord.setClasses( JAR_CLASS_LIST ); + + assertEquals( "check record", expectedRecord, record ); + } + + public void testIndexedJarWithClassifier() + throws RepositoryIndexException + { + Artifact artifact = createArtifact( "test-jar", "1.0", "jar", "jdk14" ); + + RepositoryIndexRecord record = factory.createRecord( artifact ); + + MinimalArtifactIndexRecord expectedRecord = new MinimalArtifactIndexRecord(); + expectedRecord.setMd5Checksum( "3a0adc365f849366cd8b633cad155cb7" ); + expectedRecord.setFilename( repository.pathOf( artifact ) ); + expectedRecord.setLastModified( artifact.getFile().lastModified() ); + expectedRecord.setSize( artifact.getFile().length() ); + expectedRecord.setClasses( JAR_CLASS_LIST ); + + assertEquals( "check record", expectedRecord, record ); + } + + public void testIndexedJarAndPom() + throws RepositoryIndexException + { + Artifact artifact = createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar" ); + + RepositoryIndexRecord record = factory.createRecord( artifact ); + + MinimalArtifactIndexRecord expectedRecord = new MinimalArtifactIndexRecord(); + expectedRecord.setMd5Checksum( "3a0adc365f849366cd8b633cad155cb7" ); + expectedRecord.setFilename( repository.pathOf( artifact ) ); + expectedRecord.setLastModified( artifact.getFile().lastModified() ); + expectedRecord.setSize( artifact.getFile().length() ); + expectedRecord.setClasses( JAR_CLASS_LIST ); + + assertEquals( "check record", expectedRecord, record ); + } + + public void testIndexedJarAndPomWithClassifier() + throws RepositoryIndexException + { + Artifact artifact = createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar", "jdk14" ); + + RepositoryIndexRecord record = factory.createRecord( artifact ); + + MinimalArtifactIndexRecord expectedRecord = new MinimalArtifactIndexRecord(); + expectedRecord.setMd5Checksum( "3a0adc365f849366cd8b633cad155cb7" ); + expectedRecord.setFilename( repository.pathOf( artifact ) ); + expectedRecord.setLastModified( artifact.getFile().lastModified() ); + expectedRecord.setSize( artifact.getFile().length() ); + expectedRecord.setClasses( JAR_CLASS_LIST ); + + assertEquals( "check record", expectedRecord, record ); + } + + public void testIndexedPom() + throws RepositoryIndexException + { + Artifact artifact = createArtifact( "test-pom", "1.0", "pom" ); + + RepositoryIndexRecord record = factory.createRecord( artifact ); + + assertNull( "Check no record", record ); + } + + public void testNonIndexedPom() + throws RepositoryIndexException + { + // If we pass in only the POM that belongs to a JAR, then expect null not the POM + Artifact artifact = createArtifact( "test-jar-and-pom", "1.0-alpha-1", "pom" ); + + RepositoryIndexRecord record = factory.createRecord( artifact ); + + assertNull( "Check no record", record ); + + artifact = createArtifact( "test-plugin", "1.0", "pom" ); + + record = factory.createRecord( artifact ); + + assertNull( "Check no record", record ); + + artifact = createArtifact( "test-archetype", "1.0", "pom" ); + + record = factory.createRecord( artifact ); + + assertNull( "Check no record", record ); + } + + public void testIndexedPlugin() + throws RepositoryIndexException, IOException, XmlPullParserException + { + Artifact artifact = createArtifact( "test-plugin" ); + + RepositoryIndexRecord record = factory.createRecord( artifact ); + + MinimalArtifactIndexRecord expectedRecord = new MinimalArtifactIndexRecord(); + expectedRecord.setMd5Checksum( "3530896791670ebb45e17708e5d52c40" ); + expectedRecord.setFilename( repository.pathOf( artifact ) ); + expectedRecord.setLastModified( artifact.getFile().lastModified() ); + expectedRecord.setSize( artifact.getFile().length() ); + expectedRecord.setClasses( Collections.singletonList( "org.apache.maven.archiva.record.MyMojo" ) ); + + assertEquals( "check record", expectedRecord, record ); + } + + public void testCorruptJar() + throws RepositoryIndexException + { + Artifact artifact = createArtifact( "test-corrupt-jar" ); + + RepositoryIndexRecord record = factory.createRecord( artifact ); + + assertNull( "Confirm no record is returned", record ); + } + + public void testNonJar() + throws RepositoryIndexException + { + Artifact artifact = createArtifact( "test-dll", "1.0.1.34", "dll" ); + + RepositoryIndexRecord record = factory.createRecord( artifact ); + + assertNull( "Confirm no record is returned", record ); + } + + public void testMissingFile() + throws RepositoryIndexException + { + Artifact artifact = createArtifact( "test-foo" ); + + RepositoryIndexRecord record = factory.createRecord( artifact ); + + assertNull( "Confirm no record is returned", record ); + } + + private Artifact createArtifact( String artifactId ) + { + return createArtifact( artifactId, "1.0", "jar" ); + } + + private Artifact createArtifact( String artifactId, String version, String type ) + { + return createArtifact( artifactId, version, type, null ); + } + + private Artifact createArtifact( String artifactId, String version, String type, String classifier ) + { + Artifact artifact = artifactFactory.createDependencyArtifact( TEST_GROUP_ID, artifactId, + VersionRange.createFromVersion( version ), type, + classifier, Artifact.SCOPE_RUNTIME ); + artifact.isSnapshot(); + artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); + artifact.setRepository( repository ); + return artifact; + } +} diff --git a/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/record/StandardArtifactIndexRecordFactoryTest.java b/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/record/StandardArtifactIndexRecordFactoryTest.java new file mode 100644 index 000000000..42ea5756a --- /dev/null +++ b/archiva-indexer/src/test/java/org/apache/maven/archiva/indexer/record/StandardArtifactIndexRecordFactoryTest.java @@ -0,0 +1,382 @@ +package org.apache.maven.archiva.indexer.record; + +/* + * 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.archiva.indexer.RepositoryIndexException; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.factory.ArtifactFactory; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; +import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; +import org.apache.maven.artifact.versioning.VersionRange; +import org.codehaus.plexus.PlexusTestCase; +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; + +import java.io.File; +import java.io.IOException; +import java.util.Arrays; +import java.util.List; + +/** + * Test the minimal artifact index record. + * + * @author Brett Porter + */ +public class StandardArtifactIndexRecordFactoryTest + extends PlexusTestCase +{ + private RepositoryIndexRecordFactory factory; + + private ArtifactRepository repository; + + private ArtifactFactory artifactFactory; + + private static final String TEST_GROUP_ID = "org.apache.maven.archiva.record"; + + private static final List JAR_CLASS_LIST = Arrays.asList( new String[]{"A", "b.B", "b.c.C"} ); + + private static final List JAR_FILE_LIST = + Arrays.asList( new String[]{"META-INF/MANIFEST.MF", "A.class", "b/B.class", "b/c/C.class"} ); + + protected void setUp() + throws Exception + { + super.setUp(); + + factory = (RepositoryIndexRecordFactory) lookup( RepositoryIndexRecordFactory.ROLE, "standard" ); + + artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE ); + + ArtifactRepositoryFactory repositoryFactory = + (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE ); + + ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" ); + + File file = getTestFile( "src/test/managed-repository" ); + repository = + repositoryFactory.createArtifactRepository( "test", file.toURI().toURL().toString(), layout, null, null ); + } + + public void testIndexedJar() + throws RepositoryIndexException + { + Artifact artifact = createArtifact( "test-jar" ); + + RepositoryIndexRecord record = factory.createRecord( artifact ); + + StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord(); + expectedRecord.setMd5Checksum( "3a0adc365f849366cd8b633cad155cb7" ); + expectedRecord.setFilename( repository.pathOf( artifact ) ); + expectedRecord.setLastModified( artifact.getFile().lastModified() ); + expectedRecord.setSize( artifact.getFile().length() ); + expectedRecord.setClasses( JAR_CLASS_LIST ); + expectedRecord.setArtifactId( "test-jar" ); + expectedRecord.setGroupId( TEST_GROUP_ID ); + expectedRecord.setBaseVersion( "1.0" ); + expectedRecord.setVersion( "1.0" ); + expectedRecord.setFiles( JAR_FILE_LIST ); + expectedRecord.setSha1Checksum( "c66f18bf192cb613fc2febb4da541a34133eedc2" ); + expectedRecord.setType( "jar" ); + expectedRecord.setRepository( "test" ); + + assertEquals( "check record", expectedRecord, record ); + } + + public void testIndexedJarWithClassifier() + throws RepositoryIndexException + { + Artifact artifact = createArtifact( "test-jar", "1.0", "jar", "jdk14" ); + + RepositoryIndexRecord record = factory.createRecord( artifact ); + + StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord(); + expectedRecord.setMd5Checksum( "3a0adc365f849366cd8b633cad155cb7" ); + expectedRecord.setFilename( repository.pathOf( artifact ) ); + expectedRecord.setLastModified( artifact.getFile().lastModified() ); + expectedRecord.setSize( artifact.getFile().length() ); + expectedRecord.setClasses( JAR_CLASS_LIST ); + expectedRecord.setArtifactId( "test-jar" ); + expectedRecord.setGroupId( TEST_GROUP_ID ); + expectedRecord.setBaseVersion( "1.0" ); + expectedRecord.setVersion( "1.0" ); + expectedRecord.setFiles( JAR_FILE_LIST ); + expectedRecord.setSha1Checksum( "c66f18bf192cb613fc2febb4da541a34133eedc2" ); + expectedRecord.setType( "jar" ); + expectedRecord.setRepository( "test" ); + expectedRecord.setClassifier( "jdk14" ); + + assertEquals( "check record", expectedRecord, record ); + } + + public void testIndexedJarAndPom() + throws RepositoryIndexException + { + Artifact artifact = createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar" ); + + RepositoryIndexRecord record = factory.createRecord( artifact ); + + StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord(); + expectedRecord.setMd5Checksum( "3a0adc365f849366cd8b633cad155cb7" ); + expectedRecord.setFilename( repository.pathOf( artifact ) ); + expectedRecord.setLastModified( artifact.getFile().lastModified() ); + expectedRecord.setSize( artifact.getFile().length() ); + expectedRecord.setClasses( JAR_CLASS_LIST ); + expectedRecord.setArtifactId( "test-jar-and-pom" ); + expectedRecord.setGroupId( TEST_GROUP_ID ); + expectedRecord.setBaseVersion( "1.0-alpha-1" ); + expectedRecord.setVersion( "1.0-alpha-1" ); + expectedRecord.setFiles( JAR_FILE_LIST ); + expectedRecord.setSha1Checksum( "c66f18bf192cb613fc2febb4da541a34133eedc2" ); + expectedRecord.setType( "jar" ); + expectedRecord.setRepository( "test" ); + expectedRecord.setPackaging( "jar" ); + expectedRecord.setProjectName( "Test JAR and POM" ); + + assertEquals( "check record", expectedRecord, record ); + } + + public void testIndexedJarAndPomWithClassifier() + throws RepositoryIndexException + { + Artifact artifact = createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar", "jdk14" ); + + RepositoryIndexRecord record = factory.createRecord( artifact ); + + StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord(); + expectedRecord.setMd5Checksum( "3a0adc365f849366cd8b633cad155cb7" ); + expectedRecord.setFilename( repository.pathOf( artifact ) ); + expectedRecord.setLastModified( artifact.getFile().lastModified() ); + expectedRecord.setSize( artifact.getFile().length() ); + expectedRecord.setClasses( JAR_CLASS_LIST ); + expectedRecord.setArtifactId( "test-jar-and-pom" ); + expectedRecord.setGroupId( TEST_GROUP_ID ); + expectedRecord.setBaseVersion( "1.0-alpha-1" ); + expectedRecord.setVersion( "1.0-alpha-1" ); + expectedRecord.setFiles( JAR_FILE_LIST ); + expectedRecord.setSha1Checksum( "c66f18bf192cb613fc2febb4da541a34133eedc2" ); + expectedRecord.setType( "jar" ); + expectedRecord.setRepository( "test" ); + expectedRecord.setPackaging( "jar" ); + expectedRecord.setProjectName( "Test JAR and POM" ); + expectedRecord.setClassifier( "jdk14" ); + + assertEquals( "check record", expectedRecord, record ); + } + + public void testIndexedJarWithParentPom() + throws RepositoryIndexException + { + Artifact artifact = createArtifact( "test-child-pom", "1.0-20060728.121314-1", "jar" ); + + RepositoryIndexRecord record = factory.createRecord( artifact ); + + StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord(); + expectedRecord.setMd5Checksum( "3a0adc365f849366cd8b633cad155cb7" ); + expectedRecord.setFilename( repository.pathOf( artifact ) ); + expectedRecord.setLastModified( artifact.getFile().lastModified() ); + expectedRecord.setSize( artifact.getFile().length() ); + expectedRecord.setClasses( JAR_CLASS_LIST ); + expectedRecord.setArtifactId( "test-child-pom" ); + expectedRecord.setGroupId( TEST_GROUP_ID ); + expectedRecord.setBaseVersion( "1.0-SNAPSHOT" ); + expectedRecord.setVersion( "1.0-20060728.121314-1" ); + expectedRecord.setFiles( JAR_FILE_LIST ); + expectedRecord.setSha1Checksum( "c66f18bf192cb613fc2febb4da541a34133eedc2" ); + expectedRecord.setType( "jar" ); + expectedRecord.setRepository( "test" ); + expectedRecord.setPackaging( "jar" ); + expectedRecord.setProjectName( "Child Project" ); + expectedRecord.setProjectDescription( "Description" ); + expectedRecord.setInceptionYear( "2005" ); + + assertEquals( "check record", expectedRecord, record ); + } + + public void testIndexedPom() + throws RepositoryIndexException + { + Artifact artifact = createArtifact( "test-pom", "1.0", "pom" ); + + RepositoryIndexRecord record = factory.createRecord( artifact ); + + StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord(); + expectedRecord.setMd5Checksum( "98b4a1b708a90a8637aaf541bef5094f" ); + expectedRecord.setFilename( repository.pathOf( artifact ) ); + expectedRecord.setLastModified( artifact.getFile().lastModified() ); + expectedRecord.setSize( artifact.getFile().length() ); + expectedRecord.setArtifactId( "test-pom" ); + expectedRecord.setGroupId( TEST_GROUP_ID ); + expectedRecord.setBaseVersion( "1.0" ); + expectedRecord.setVersion( "1.0" ); + expectedRecord.setSha1Checksum( "d95348bee1666a46511260696292bfa0519b61c1" ); + expectedRecord.setType( "pom" ); + expectedRecord.setRepository( "test" ); + expectedRecord.setPackaging( "pom" ); + expectedRecord.setInceptionYear( "2005" ); + expectedRecord.setProjectName( "Maven Repository Manager Test POM" ); + expectedRecord.setProjectDescription( "Description" ); + + assertEquals( "check record", expectedRecord, record ); + } + + public void testNonIndexedPom() + throws RepositoryIndexException + { + // If we pass in only the POM that belongs to a JAR, then expect null not the POM + Artifact artifact = createArtifact( "test-jar-and-pom", "1.0", "pom" ); + + RepositoryIndexRecord record = factory.createRecord( artifact ); + + assertNull( "Check no record", record ); + + artifact = createArtifact( "test-plugin", "1.0", "pom" ); + + record = factory.createRecord( artifact ); + + assertNull( "Check no record", record ); + + artifact = createArtifact( "test-archetype", "1.0", "pom" ); + + record = factory.createRecord( artifact ); + + assertNull( "Check no record", record ); + } + + public void testIndexedPlugin() + throws RepositoryIndexException, IOException, XmlPullParserException + { + Artifact artifact = createArtifact( "test-plugin" ); + + RepositoryIndexRecord record = factory.createRecord( artifact ); + + StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord(); + expectedRecord.setMd5Checksum( "3530896791670ebb45e17708e5d52c40" ); + expectedRecord.setFilename( repository.pathOf( artifact ) ); + expectedRecord.setLastModified( artifact.getFile().lastModified() ); + expectedRecord.setSize( artifact.getFile().length() ); + expectedRecord.setArtifactId( "test-plugin" ); + expectedRecord.setGroupId( TEST_GROUP_ID ); + expectedRecord.setBaseVersion( "1.0" ); + expectedRecord.setVersion( "1.0" ); + expectedRecord.setSha1Checksum( "2cd2619d59a684e82e97471d2c2e004144c8f24e" ); + expectedRecord.setType( "maven-plugin" ); + expectedRecord.setRepository( "test" ); + expectedRecord.setClasses( Arrays.asList( new String[]{"org.apache.maven.archiva.record.MyMojo"} ) ); + expectedRecord.setFiles( Arrays.asList( new String[]{"META-INF/MANIFEST.MF", + "META-INF/maven/org.apache.maven.archiva.record/test-plugin/pom.properties", + "META-INF/maven/org.apache.maven.archiva.record/test-plugin/pom.xml", "META-INF/maven/plugin.xml", + "org/apache/maven/archiva/record/MyMojo.class"} ) ); + expectedRecord.setPackaging( "maven-plugin" ); + expectedRecord.setProjectName( "Maven Mojo Archetype" ); + expectedRecord.setPluginPrefix( "test" ); + + assertEquals( "check record", expectedRecord, record ); + } + + public void testIndexedArchetype() + throws RepositoryIndexException, IOException, XmlPullParserException + { + Artifact artifact = createArtifact( "test-archetype" ); + + RepositoryIndexRecord record = factory.createRecord( artifact ); + + StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord(); + expectedRecord.setMd5Checksum( "52b7ea4b53818b8a5f4c329d88fd60d9" ); + expectedRecord.setFilename( repository.pathOf( artifact ) ); + expectedRecord.setLastModified( artifact.getFile().lastModified() ); + expectedRecord.setSize( artifact.getFile().length() ); + expectedRecord.setArtifactId( "test-archetype" ); + expectedRecord.setGroupId( TEST_GROUP_ID ); + expectedRecord.setBaseVersion( "1.0" ); + expectedRecord.setVersion( "1.0" ); + expectedRecord.setSha1Checksum( "05841f5e51c124f1729d86c1687438c36b9255d9" ); + expectedRecord.setType( "maven-archetype" ); + expectedRecord.setRepository( "test" ); + expectedRecord.setFiles( Arrays.asList( new String[]{"META-INF/MANIFEST.MF", "META-INF/maven/archetype.xml", + "META-INF/maven/org.apache.maven.archiva.record/test-archetype/pom.properties", + "META-INF/maven/org.apache.maven.archiva.record/test-archetype/pom.xml", "archetype-resources/pom.xml", + "archetype-resources/src/main/java/App.java", "archetype-resources/src/test/java/AppTest.java"} ) ); + expectedRecord.setPackaging( "jar" ); + expectedRecord.setProjectName( "Archetype - test-archetype" ); + + assertEquals( "check record", expectedRecord, record ); + } + + public void testCorruptJar() + throws RepositoryIndexException + { + Artifact artifact = createArtifact( "test-corrupt-jar" ); + + RepositoryIndexRecord record = factory.createRecord( artifact ); + + assertNull( "Confirm no record is returned", record ); + } + + public void testDll() + throws RepositoryIndexException + { + Artifact artifact = createArtifact( "test-dll", "1.0.1.34", "dll" ); + + RepositoryIndexRecord record = factory.createRecord( artifact ); + + StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord(); + expectedRecord.setMd5Checksum( "d41d8cd98f00b204e9800998ecf8427e" ); + expectedRecord.setFilename( repository.pathOf( artifact ) ); + expectedRecord.setLastModified( artifact.getFile().lastModified() ); + expectedRecord.setSize( artifact.getFile().length() ); + expectedRecord.setArtifactId( "test-dll" ); + expectedRecord.setGroupId( TEST_GROUP_ID ); + expectedRecord.setBaseVersion( "1.0.1.34" ); + expectedRecord.setVersion( "1.0.1.34" ); + expectedRecord.setSha1Checksum( "da39a3ee5e6b4b0d3255bfef95601890afd80709" ); + expectedRecord.setType( "dll" ); + expectedRecord.setRepository( "test" ); + + assertEquals( "check record", expectedRecord, record ); + } + + public void testMissingFile() + throws RepositoryIndexException + { + Artifact artifact = createArtifact( "test-foo" ); + + RepositoryIndexRecord record = factory.createRecord( artifact ); + + assertNull( "Confirm no record is returned", record ); + } + + private Artifact createArtifact( String artifactId ) + { + return createArtifact( artifactId, "1.0", "jar" ); + } + + private Artifact createArtifact( String artifactId, String version, String type ) + { + return createArtifact( artifactId, version, type, null ); + } + + private Artifact createArtifact( String artifactId, String version, String type, String classifier ) + { + Artifact artifact = artifactFactory.createDependencyArtifact( TEST_GROUP_ID, artifactId, + VersionRange.createFromVersion( version ), type, + classifier, Artifact.SCOPE_RUNTIME ); + artifact.isSnapshot(); + artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); + artifact.setRepository( repository ); + return artifact; + } +} diff --git a/archiva-indexer/src/test/java/org/apache/maven/archiva/indexing/lucene/LuceneMinimalArtifactIndexSearchTest.java b/archiva-indexer/src/test/java/org/apache/maven/archiva/indexing/lucene/LuceneMinimalArtifactIndexSearchTest.java deleted file mode 100644 index caac994b7..000000000 --- a/archiva-indexer/src/test/java/org/apache/maven/archiva/indexing/lucene/LuceneMinimalArtifactIndexSearchTest.java +++ /dev/null @@ -1,218 +0,0 @@ -package org.apache.maven.archiva.indexing.lucene; - -/* - * Copyright 2005-2006 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import org.apache.lucene.index.Term; -import org.apache.lucene.search.Query; -import org.apache.lucene.search.TermQuery; -import org.apache.maven.archiva.indexing.RepositoryArtifactIndex; -import org.apache.maven.archiva.indexing.RepositoryArtifactIndexFactory; -import org.apache.maven.archiva.indexing.RepositoryIndexSearchException; -import org.apache.maven.archiva.indexing.record.MinimalIndexRecordFields; -import org.apache.maven.archiva.indexing.record.RepositoryIndexRecordFactory; -import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.factory.ArtifactFactory; -import org.apache.maven.artifact.repository.ArtifactRepository; -import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; -import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; -import org.apache.maven.artifact.versioning.VersionRange; -import org.codehaus.plexus.PlexusTestCase; -import org.codehaus.plexus.util.FileUtils; - -import java.io.File; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * Test the Lucene implementation of the artifact index search. - * - * @author Brett Porter - * @todo would be nice to abstract some of the query away, but for now passing in a Lucene query directly is good enough - */ -public class LuceneMinimalArtifactIndexSearchTest - extends PlexusTestCase -{ - private RepositoryArtifactIndex index; - - private ArtifactRepository repository; - - private ArtifactFactory artifactFactory; - - private File indexLocation; - - private RepositoryIndexRecordFactory recordFactory; - - private Map records = new HashMap(); - - protected void setUp() - throws Exception - { - super.setUp(); - - recordFactory = (RepositoryIndexRecordFactory) lookup( RepositoryIndexRecordFactory.ROLE, "minimal" ); - - artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE ); - - ArtifactRepositoryFactory repositoryFactory = - (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE ); - - ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" ); - - File file = getTestFile( "src/test/managed-repository" ); - repository = - repositoryFactory.createArtifactRepository( "test", file.toURI().toURL().toString(), layout, null, null ); - - RepositoryArtifactIndexFactory factory = - (RepositoryArtifactIndexFactory) lookup( RepositoryArtifactIndexFactory.ROLE, "lucene" ); - - indexLocation = getTestFile( "target/test-index" ); - - FileUtils.deleteDirectory( indexLocation ); - - index = factory.createMinimalIndex( indexLocation ); - - records.put( "test-jar", recordFactory.createRecord( createArtifact( "test-jar" ) ) ); - records.put( "test-jar-jdk14", - recordFactory.createRecord( createArtifact( "test-jar", "1.0", "jar", "jdk14" ) ) ); - records.put( "test-jar-and-pom", - recordFactory.createRecord( createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar" ) ) ); - records.put( "test-jar-and-pom-jdk14", recordFactory.createRecord( - createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar", "jdk14" ) ) ); - records.put( "test-child-pom", - recordFactory.createRecord( createArtifact( "test-child-pom", "1.0-20060728.121314-1", "jar" ) ) ); - records.put( "test-archetype", recordFactory.createRecord( createArtifact( "test-archetype" ) ) ); - records.put( "test-plugin", recordFactory.createRecord( createArtifact( "test-plugin" ) ) ); - records.put( "test-pom", recordFactory.createRecord( createArtifact( "test-pom", "1.0", "pom" ) ) ); - records.put( "parent-pom", recordFactory.createRecord( createArtifact( "parent-pom", "1", "pom" ) ) ); - records.put( "test-dll", recordFactory.createRecord( createArtifact( "test-dll", "1.0.1.34", "dll" ) ) ); - - index.indexRecords( records.values() ); - } - - public void testExactMatchMd5() - throws RepositoryIndexSearchException - { - Query query = new TermQuery( new Term( MinimalIndexRecordFields.MD5, "3a0adc365f849366cd8b633cad155cb7" ) ); - List results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) ); - assertEquals( "Check results size", 5, results.size() ); - - // test non-match fails - query = new TermQuery( new Term( MinimalIndexRecordFields.MD5, "foo" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check results size", results.isEmpty() ); - } - - public void testMatchFilename() - throws RepositoryIndexSearchException - { - Query query = new TermQuery( new Term( MinimalIndexRecordFields.FILENAME, "maven" ) ); - List results = index.search( new LuceneQuery( query ) ); - - assertFalse( "Check result", results.contains( records.get( "test-pom" ) ) ); - assertFalse( "Check result", results.contains( records.get( "parent-pom" ) ) ); - assertFalse( "Check result", results.contains( records.get( "test-dll" ) ) ); - assertEquals( "Check results size", 7, results.size() ); - -/* TODO: if this is a result we want, we need to change the analyzer. Currently, it is tokenizing it as plugin-1.0 and plugin/1.0 in the path - query = new TermQuery( new Term( MinimalIndexRecordFields.FILENAME, "plugin" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) ); - assertEquals( "Check results size", 1, results.size() ); -*/ - query = new TermQuery( new Term( MinimalIndexRecordFields.FILENAME, "test" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertFalse( "Check result", results.contains( records.get( "parent-pom" ) ) ); - assertFalse( "Check result", results.contains( records.get( "test-pom" ) ) ); - assertFalse( "Check result", results.contains( records.get( "test-dll" ) ) ); - assertEquals( "Check results size", 7, results.size() ); - - // test non-match fails - query = new TermQuery( new Term( MinimalIndexRecordFields.FILENAME, "foo" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check results size", results.isEmpty() ); - } - - public void testMatchClass() - throws RepositoryIndexSearchException - { - // TODO: should be preserving case! - Query query = new TermQuery( new Term( MinimalIndexRecordFields.CLASSES, "b.c.c" ) ); - List results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) ); - assertEquals( "Check results size", 5, results.size() ); - -/* TODO!: need to change the analyzer if we want partial classes (split on '.') - query = new TermQuery( new Term( MinimalIndexRecordFields.CLASSES, "C" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) ); - assertEquals( "Check results size", 4, results.size() ); - - query = new TermQuery( new Term( MinimalIndexRecordFields.CLASSES, "MyMojo" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) ); - assertEquals( "Check results size", 1, results.size() ); -*/ - - // test non-match fails - query = new TermQuery( new Term( MinimalIndexRecordFields.CLASSES, "foo" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check results size", results.isEmpty() ); - } - - private Artifact createArtifact( String artifactId ) - { - return createArtifact( artifactId, "1.0", "jar", null ); - } - - private Artifact createArtifact( String artifactId, String version, String type ) - { - return createArtifact( artifactId, version, type, null ); - } - - private Artifact createArtifact( String artifactId, String version, String type, String classifier ) - { - Artifact artifact = artifactFactory.createDependencyArtifact( "org.apache.maven.archiva.record", artifactId, - VersionRange.createFromVersion( version ), type, - classifier, Artifact.SCOPE_RUNTIME ); - artifact.isSnapshot(); - artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); - artifact.setRepository( repository ); - return artifact; - } -} diff --git a/archiva-indexer/src/test/java/org/apache/maven/archiva/indexing/lucene/LuceneMinimalArtifactIndexTest.java b/archiva-indexer/src/test/java/org/apache/maven/archiva/indexing/lucene/LuceneMinimalArtifactIndexTest.java deleted file mode 100644 index 5fddc326c..000000000 --- a/archiva-indexer/src/test/java/org/apache/maven/archiva/indexing/lucene/LuceneMinimalArtifactIndexTest.java +++ /dev/null @@ -1,351 +0,0 @@ -package org.apache.maven.archiva.indexing.lucene; - -/* - * Copyright 2005-2006 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import org.apache.lucene.analysis.standard.StandardAnalyzer; -import org.apache.lucene.document.Document; -import org.apache.lucene.document.NumberTools; -import org.apache.lucene.index.IndexReader; -import org.apache.lucene.index.IndexWriter; -import org.apache.maven.archiva.indexing.RepositoryArtifactIndex; -import org.apache.maven.archiva.indexing.RepositoryArtifactIndexFactory; -import org.apache.maven.archiva.indexing.RepositoryIndexException; -import org.apache.maven.archiva.indexing.record.MinimalIndexRecordFields; -import org.apache.maven.archiva.indexing.record.RepositoryIndexRecord; -import org.apache.maven.archiva.indexing.record.RepositoryIndexRecordFactory; -import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.factory.ArtifactFactory; -import org.apache.maven.artifact.repository.ArtifactRepository; -import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; -import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; -import org.codehaus.plexus.PlexusTestCase; -import org.codehaus.plexus.util.FileUtils; -import org.codehaus.plexus.util.xml.pull.XmlPullParserException; - -import java.io.File; -import java.io.IOException; -import java.text.SimpleDateFormat; -import java.util.Collections; -import java.util.Date; -import java.util.Iterator; -import java.util.List; -import java.util.Locale; -import java.util.TimeZone; - -/** - * Test the Lucene implementation of the artifact index. - * - * @author Brett Porter - */ -public class LuceneMinimalArtifactIndexTest - extends PlexusTestCase -{ - private RepositoryArtifactIndex index; - - private ArtifactRepository repository; - - private ArtifactFactory artifactFactory; - - private File indexLocation; - - private RepositoryIndexRecordFactory recordFactory; - - protected void setUp() - throws Exception - { - super.setUp(); - - recordFactory = (RepositoryIndexRecordFactory) lookup( RepositoryIndexRecordFactory.ROLE, "minimal" ); - - artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE ); - - ArtifactRepositoryFactory repositoryFactory = - (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE ); - - ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" ); - - File file = getTestFile( "src/test/managed-repository" ); - repository = - repositoryFactory.createArtifactRepository( "test", file.toURI().toURL().toString(), layout, null, null ); - - RepositoryArtifactIndexFactory factory = - (RepositoryArtifactIndexFactory) lookup( RepositoryArtifactIndexFactory.ROLE, "lucene" ); - - indexLocation = getTestFile( "target/test-index" ); - - FileUtils.deleteDirectory( indexLocation ); - - index = factory.createMinimalIndex( indexLocation ); - } - - public void testIndexExists() - throws IOException, RepositoryIndexException - { - assertFalse( "check index doesn't exist", index.exists() ); - - // create empty directory - indexLocation.mkdirs(); - assertFalse( "check index doesn't exist even if directory does", index.exists() ); - - // create index, with no records - createEmptyIndex(); - assertTrue( "check index is considered to exist", index.exists() ); - - // Test non-directory - FileUtils.deleteDirectory( indexLocation ); - indexLocation.createNewFile(); - try - { - index.exists(); - fail( "Index operation should fail as the location is not valid" ); - } - catch ( RepositoryIndexException e ) - { - // great - } - finally - { - indexLocation.delete(); - } - } - - public void testAddRecordNoIndex() - throws IOException, RepositoryIndexException - { - Artifact artifact = createArtifact( "test-jar" ); - - RepositoryIndexRecord record = recordFactory.createRecord( artifact ); - index.indexRecords( Collections.singletonList( record ) ); - - IndexReader reader = IndexReader.open( indexLocation ); - try - { - Document document = reader.document( 0 ); - assertEquals( "Check document", repository.pathOf( artifact ), - document.get( MinimalIndexRecordFields.FILENAME ) ); - assertEquals( "Check index size", 1, reader.numDocs() ); - } - finally - { - reader.close(); - } - } - - public void testAddRecordExistingEmptyIndex() - throws IOException, RepositoryIndexException - { - createEmptyIndex(); - - Artifact artifact = createArtifact( "test-jar" ); - - RepositoryIndexRecord record = recordFactory.createRecord( artifact ); - index.indexRecords( Collections.singletonList( record ) ); - - IndexReader reader = IndexReader.open( indexLocation ); - try - { - Document document = reader.document( 0 ); - assertRecord( document, artifact, "3a0adc365f849366cd8b633cad155cb7", "A\nb.B\nb.c.C" ); - assertEquals( "Check index size", 1, reader.numDocs() ); - } - finally - { - reader.close(); - } - } - - public void testAddRecordInIndex() - throws IOException, RepositoryIndexException - { - createEmptyIndex(); - - Artifact artifact = createArtifact( "test-jar" ); - - RepositoryIndexRecord record = recordFactory.createRecord( artifact ); - index.indexRecords( Collections.singletonList( record ) ); - - // Do it again - record = recordFactory.createRecord( artifact ); - index.indexRecords( Collections.singletonList( record ) ); - - IndexReader reader = IndexReader.open( indexLocation ); - try - { - Document document = reader.document( 0 ); - assertRecord( document, artifact, "3a0adc365f849366cd8b633cad155cb7", "A\nb.B\nb.c.C" ); - assertEquals( "Check index size", 1, reader.numDocs() ); - } - finally - { - reader.close(); - } - } - - public void testDeleteRecordInIndex() - throws IOException, RepositoryIndexException - { - createEmptyIndex(); - - Artifact artifact = createArtifact( "test-jar" ); - - RepositoryIndexRecord record = recordFactory.createRecord( artifact ); - index.indexRecords( Collections.singletonList( record ) ); - - index.deleteRecords( Collections.singletonList( record ) ); - - IndexReader reader = IndexReader.open( indexLocation ); - try - { - assertEquals( "No documents", 0, reader.numDocs() ); - } - finally - { - reader.close(); - } - } - - public void testDeleteRecordNotInIndex() - throws IOException, RepositoryIndexException - { - createEmptyIndex(); - - Artifact artifact = createArtifact( "test-jar" ); - - RepositoryIndexRecord record = recordFactory.createRecord( artifact ); - - index.deleteRecords( Collections.singletonList( record ) ); - - IndexReader reader = IndexReader.open( indexLocation ); - try - { - assertEquals( "No documents", 0, reader.numDocs() ); - } - finally - { - reader.close(); - } - } - - public void testDeleteRecordNoIndex() - throws IOException, RepositoryIndexException - { - Artifact artifact = createArtifact( "test-jar" ); - - RepositoryIndexRecord record = recordFactory.createRecord( artifact ); - index.deleteRecords( Collections.singleton( record ) ); - - assertFalse( index.exists() ); - } - - public void testAddPomRecord() - throws IOException, RepositoryIndexException - { - createEmptyIndex(); - - Artifact artifact = createArtifact( "test-pom", "1.0", "pom" ); - - RepositoryIndexRecord record = recordFactory.createRecord( artifact ); - index.indexRecords( Collections.singletonList( record ) ); - - IndexReader reader = IndexReader.open( indexLocation ); - try - { - assertEquals( "No documents", 0, reader.numDocs() ); - } - finally - { - reader.close(); - } - } - - public void testAddPlugin() - throws IOException, RepositoryIndexException, XmlPullParserException - { - createEmptyIndex(); - - Artifact artifact = createArtifact( "test-plugin" ); - - RepositoryIndexRecord record = recordFactory.createRecord( artifact ); - - index.indexRecords( Collections.singletonList( record ) ); - - IndexReader reader = IndexReader.open( indexLocation ); - try - { - Document document = reader.document( 0 ); - assertRecord( document, artifact, "3530896791670ebb45e17708e5d52c40", - "org.apache.maven.archiva.record.MyMojo" ); - assertEquals( "Check index size", 1, reader.numDocs() ); - } - finally - { - reader.close(); - } - } - - private Artifact createArtifact( String artifactId ) - { - return createArtifact( artifactId, "1.0", "jar" ); - } - - private Artifact createArtifact( String artifactId, String version, String type ) - { - Artifact artifact = - artifactFactory.createBuildArtifact( "org.apache.maven.archiva.record", artifactId, version, type ); - artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); - artifact.setRepository( repository ); - return artifact; - } - - private void createEmptyIndex() - throws IOException - { - createIndex( Collections.EMPTY_LIST ); - } - - private void createIndex( List docments ) - throws IOException - { - IndexWriter writer = new IndexWriter( indexLocation, new StandardAnalyzer(), true ); - for ( Iterator i = docments.iterator(); i.hasNext(); ) - { - Document document = (Document) i.next(); - writer.addDocument( document ); - } - writer.optimize(); - writer.close(); - } - - private void assertRecord( Document document, Artifact artifact, String expectedChecksum, String expectedClasses ) - { - assertEquals( "Check document filename", repository.pathOf( artifact ), - document.get( MinimalIndexRecordFields.FILENAME ) ); - assertEquals( "Check document timestamp", getLastModified( artifact.getFile() ), - document.get( MinimalIndexRecordFields.LAST_MODIFIED ) ); - assertEquals( "Check document checksum", expectedChecksum, document.get( MinimalIndexRecordFields.MD5 ) ); - assertEquals( "Check document size", artifact.getFile().length(), - NumberTools.stringToLong( document.get( MinimalIndexRecordFields.FILE_SIZE ) ) ); - assertEquals( "Check document classes", expectedClasses, document.get( MinimalIndexRecordFields.CLASSES ) ); - } - - private String getLastModified( File file ) - { - SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyyMMddHHmmss", Locale.US ); - dateFormat.setTimeZone( TimeZone.getTimeZone( "UTC" ) ); - return dateFormat.format( new Date( file.lastModified() ) ); - } -} diff --git a/archiva-indexer/src/test/java/org/apache/maven/archiva/indexing/lucene/LuceneStandardArtifactIndexSearchTest.java b/archiva-indexer/src/test/java/org/apache/maven/archiva/indexing/lucene/LuceneStandardArtifactIndexSearchTest.java deleted file mode 100644 index bac0e3d32..000000000 --- a/archiva-indexer/src/test/java/org/apache/maven/archiva/indexing/lucene/LuceneStandardArtifactIndexSearchTest.java +++ /dev/null @@ -1,699 +0,0 @@ -package org.apache.maven.archiva.indexing.lucene; - -/* - * Copyright 2005-2006 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import org.apache.lucene.index.Term; -import org.apache.lucene.search.BooleanClause; -import org.apache.lucene.search.BooleanQuery; -import org.apache.lucene.search.MatchAllDocsQuery; -import org.apache.lucene.search.Query; -import org.apache.lucene.search.TermQuery; -import org.apache.maven.archiva.indexing.RepositoryArtifactIndex; -import org.apache.maven.archiva.indexing.RepositoryArtifactIndexFactory; -import org.apache.maven.archiva.indexing.RepositoryIndexSearchException; -import org.apache.maven.archiva.indexing.record.RepositoryIndexRecordFactory; -import org.apache.maven.archiva.indexing.record.StandardIndexRecordFields; -import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.factory.ArtifactFactory; -import org.apache.maven.artifact.repository.ArtifactRepository; -import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; -import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; -import org.apache.maven.artifact.versioning.VersionRange; -import org.codehaus.plexus.PlexusTestCase; -import org.codehaus.plexus.util.FileUtils; - -import java.io.File; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * Test the Lucene implementation of the artifact index search. - * - * @author Brett Porter - * @todo would be nice to abstract some of the query away, but for now passing in a Lucene query directly is good enough - */ -public class LuceneStandardArtifactIndexSearchTest - extends PlexusTestCase -{ - private RepositoryArtifactIndex index; - - private ArtifactRepository repository; - - private ArtifactFactory artifactFactory; - - private File indexLocation; - - private RepositoryIndexRecordFactory recordFactory; - - private Map records = new HashMap(); - - protected void setUp() - throws Exception - { - super.setUp(); - - recordFactory = (RepositoryIndexRecordFactory) lookup( RepositoryIndexRecordFactory.ROLE, "standard" ); - - artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE ); - - ArtifactRepositoryFactory repositoryFactory = - (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE ); - - ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" ); - - File file = getTestFile( "src/test/managed-repository" ); - repository = - repositoryFactory.createArtifactRepository( "test", file.toURI().toURL().toString(), layout, null, null ); - - RepositoryArtifactIndexFactory factory = - (RepositoryArtifactIndexFactory) lookup( RepositoryArtifactIndexFactory.ROLE, "lucene" ); - - indexLocation = getTestFile( "target/test-index" ); - - FileUtils.deleteDirectory( indexLocation ); - - index = factory.createStandardIndex( indexLocation ); - - records.put( "test-jar", recordFactory.createRecord( createArtifact( "test-jar" ) ) ); - records.put( "test-jar-jdk14", - recordFactory.createRecord( createArtifact( "test-jar", "1.0", "jar", "jdk14" ) ) ); - records.put( "test-jar-and-pom", - recordFactory.createRecord( createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar" ) ) ); - records.put( "test-jar-and-pom-jdk14", recordFactory.createRecord( - createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar", "jdk14" ) ) ); - records.put( "test-child-pom", - recordFactory.createRecord( createArtifact( "test-child-pom", "1.0-20060728.121314-1", "jar" ) ) ); - records.put( "test-archetype", recordFactory.createRecord( createArtifact( "test-archetype" ) ) ); - records.put( "test-plugin", recordFactory.createRecord( createArtifact( "test-plugin" ) ) ); - records.put( "test-pom", recordFactory.createRecord( createArtifact( "test-pom", "1.0", "pom" ) ) ); - records.put( "parent-pom", recordFactory.createRecord( createArtifact( "parent-pom", "1", "pom" ) ) ); - records.put( "test-dll", recordFactory.createRecord( createArtifact( "test-dll", "1.0.1.34", "dll" ) ) ); - - index.indexRecords( records.values() ); - } - - public void testExactMatchVersion() - throws RepositoryIndexSearchException - { - Query query = new TermQuery( new Term( StandardIndexRecordFields.VERSION_EXACT, "1.0" ) ); - List results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-pom" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-archetype" ) ) ); - assertEquals( "Check results size", 5, results.size() ); - - query = new TermQuery( new Term( StandardIndexRecordFields.VERSION_EXACT, "1.0-SNAPSHOT" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check results size", results.isEmpty() ); - - query = new TermQuery( new Term( StandardIndexRecordFields.VERSION_EXACT, "1.0-20060728.121314-1" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) ); - assertEquals( "Check results size", 1, results.size() ); - - // test non-match fails - query = new TermQuery( new Term( StandardIndexRecordFields.VERSION_EXACT, "foo" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check results size", results.isEmpty() ); - } - - public void testExactMatchBaseVersion() - throws RepositoryIndexSearchException - { - Query query = new TermQuery( new Term( StandardIndexRecordFields.BASE_VERSION_EXACT, "1.0" ) ); - List results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-pom" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-archetype" ) ) ); - assertEquals( "Check results size", 5, results.size() ); - - query = new TermQuery( new Term( StandardIndexRecordFields.BASE_VERSION_EXACT, "1.0-SNAPSHOT" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) ); - assertEquals( "Check results size", 1, results.size() ); - - query = new TermQuery( new Term( StandardIndexRecordFields.BASE_VERSION_EXACT, "1.0-20060728.121314-1" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check results size", results.isEmpty() ); - - // test non-match fails - query = new TermQuery( new Term( StandardIndexRecordFields.BASE_VERSION_EXACT, "foo" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check results size", results.isEmpty() ); - } - - public void testExactMatchGroupId() - throws RepositoryIndexSearchException - { - Query query = - new TermQuery( new Term( StandardIndexRecordFields.GROUPID_EXACT, "org.apache.maven.archiva.record" ) ); - List results = index.search( new LuceneQuery( query ) ); - - assertEquals( "Check results size", 10, results.size() ); - - // test partial match fails - query = new TermQuery( new Term( StandardIndexRecordFields.GROUPID_EXACT, "org.apache.maven" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check results size", results.isEmpty() ); - - // test non-match fails - query = new TermQuery( new Term( StandardIndexRecordFields.GROUPID_EXACT, "foo" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check results size", results.isEmpty() ); - } - - public void testExactMatchArtifactId() - throws RepositoryIndexSearchException - { - Query query = new TermQuery( new Term( StandardIndexRecordFields.ARTIFACTID_EXACT, "test-jar" ) ); - List results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) ); - assertEquals( "Check results size", 2, results.size() ); - - // test partial match fails - query = new TermQuery( new Term( StandardIndexRecordFields.ARTIFACTID_EXACT, "test" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check results size", results.isEmpty() ); - - // test non-match fails - query = new TermQuery( new Term( StandardIndexRecordFields.ARTIFACTID_EXACT, "foo" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check results size", results.isEmpty() ); - } - - public void testExactMatchType() - throws RepositoryIndexSearchException - { - Query query = new TermQuery( new Term( StandardIndexRecordFields.TYPE, "maven-plugin" ) ); - List results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) ); - assertEquals( "Check results size", 1, results.size() ); - - query = new TermQuery( new Term( StandardIndexRecordFields.TYPE, "jar" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) ); - assertEquals( "Check results size", 5, results.size() ); - - query = new TermQuery( new Term( StandardIndexRecordFields.TYPE, "dll" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check result", results.contains( records.get( "test-dll" ) ) ); - assertEquals( "Check results size", 1, results.size() ); - - query = new TermQuery( new Term( StandardIndexRecordFields.TYPE, "maven-archetype" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check result", results.contains( records.get( "test-archetype" ) ) ); - assertEquals( "Check results size", 1, results.size() ); - - // test non-match fails - query = new TermQuery( new Term( StandardIndexRecordFields.TYPE, "foo" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check results size", results.isEmpty() ); - } - - public void testExactMatchPackaging() - throws RepositoryIndexSearchException - { - Query query = new TermQuery( new Term( StandardIndexRecordFields.PACKAGING, "maven-plugin" ) ); - List results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) ); - assertEquals( "Check results size", 1, results.size() ); - - query = new TermQuery( new Term( StandardIndexRecordFields.PACKAGING, "jar" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check result", results.contains( records.get( "test-archetype" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) ); - assertEquals( "Check results size", 4, results.size() ); - - query = new TermQuery( new Term( StandardIndexRecordFields.PACKAGING, "dll" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check results size", results.isEmpty() ); - - query = new TermQuery( new Term( StandardIndexRecordFields.PACKAGING, "maven-archetype" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check results size", results.isEmpty() ); - - // test non-match fails - query = new TermQuery( new Term( StandardIndexRecordFields.PACKAGING, "foo" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check results size", results.isEmpty() ); - } - - public void testExactMatchPluginPrefix() - throws RepositoryIndexSearchException - { - Query query = new TermQuery( new Term( StandardIndexRecordFields.PLUGIN_PREFIX, "test" ) ); - List results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) ); - assertEquals( "Check results size", 1, results.size() ); - - // test non-match fails - query = new TermQuery( new Term( StandardIndexRecordFields.PLUGIN_PREFIX, "foo" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check results size", results.isEmpty() ); - } - - public void testExactMatchRepository() - throws RepositoryIndexSearchException - { - Query query = new TermQuery( new Term( StandardIndexRecordFields.REPOSITORY, "test" ) ); - List results = index.search( new LuceneQuery( query ) ); - - assertEquals( "Check results size", 10, results.size() ); - - // test non-match fails - query = new TermQuery( new Term( StandardIndexRecordFields.REPOSITORY, "foo" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check results size", results.isEmpty() ); - } - - public void testExactMatchMd5() - throws RepositoryIndexSearchException - { - Query query = new TermQuery( new Term( StandardIndexRecordFields.MD5, "3a0adc365f849366cd8b633cad155cb7" ) ); - List results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) ); - assertEquals( "Check results size", 5, results.size() ); - - // test non-match fails - query = new TermQuery( new Term( StandardIndexRecordFields.MD5, "foo" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check results size", results.isEmpty() ); - } - - public void testExactMatchSha1() - throws RepositoryIndexSearchException - { - Query query = - new TermQuery( new Term( StandardIndexRecordFields.SHA1, "c66f18bf192cb613fc2febb4da541a34133eedc2" ) ); - List results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) ); - assertEquals( "Check results size", 5, results.size() ); - - // test non-match fails - query = new TermQuery( new Term( StandardIndexRecordFields.SHA1, "foo" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check results size", results.isEmpty() ); - } - - public void testExactMatchInceptionYear() - throws RepositoryIndexSearchException - { - Query query = new TermQuery( new Term( StandardIndexRecordFields.INCEPTION_YEAR, "2005" ) ); - List results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-pom" ) ) ); - assertTrue( "Check result", results.contains( records.get( "parent-pom" ) ) ); - assertEquals( "Check results size", 3, results.size() ); - - // test non-match fails - query = new TermQuery( new Term( StandardIndexRecordFields.INCEPTION_YEAR, "foo" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check results size", results.isEmpty() ); - } - - public void testMatchFilename() - throws RepositoryIndexSearchException - { - Query query = new TermQuery( new Term( StandardIndexRecordFields.FILENAME, "maven" ) ); - List results = index.search( new LuceneQuery( query ) ); - - assertEquals( "Check results size", 10, results.size() ); - -/* TODO: if this is a result we want, we need to change the analyzer. Currently, it is tokenizing it as plugin-1.0 and plugin/1.0 in the path - query = new TermQuery( new Term( StandardIndexRecordFields.FILENAME, "plugin" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) ); - assertEquals( "Check results size", 1, results.size() ); -*/ - query = new TermQuery( new Term( StandardIndexRecordFields.FILENAME, "test" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertFalse( "Check result", results.contains( records.get( "parent-pom" ) ) ); - assertEquals( "Check results size", 9, results.size() ); - - // test non-match fails - query = new TermQuery( new Term( StandardIndexRecordFields.FILENAME, "foo" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check results size", results.isEmpty() ); - } - - public void testMatchGroupId() - throws RepositoryIndexSearchException - { - Query query = new TermQuery( new Term( StandardIndexRecordFields.GROUPID, "org.apache.maven.archiva.record" ) ); - List results = index.search( new LuceneQuery( query ) ); - - assertEquals( "Check results size", 10, results.size() ); - -/* TODO: if we want this result, must change the analyzer to split on '.' - query = new TermQuery( new Term( StandardIndexRecordFields.GROUPID, "maven" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertEquals( "Check results size", 10, results.size() ); -*/ - - // test non-match fails - query = new TermQuery( new Term( StandardIndexRecordFields.GROUPID, "foo" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check results size", results.isEmpty() ); - } - - public void testMatchArtifactId() - throws RepositoryIndexSearchException - { - Query query = new TermQuery( new Term( StandardIndexRecordFields.ARTIFACTID, "plugin" ) ); - List results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) ); - assertEquals( "Check results size", 1, results.size() ); - - query = new TermQuery( new Term( StandardIndexRecordFields.ARTIFACTID, "test" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertFalse( "Check result", results.contains( records.get( "parent-pom" ) ) ); - assertEquals( "Check results size", 9, results.size() ); - - // test non-match fails - query = new TermQuery( new Term( StandardIndexRecordFields.ARTIFACTID, "maven" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check results size", results.isEmpty() ); - } - - public void testMatchVersion() - throws RepositoryIndexSearchException - { - // If partial matches are desired, need to change the analyzer for versions to split on '.' - Query query = new TermQuery( new Term( StandardIndexRecordFields.VERSION, "1" ) ); - List results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check result", results.contains( records.get( "parent-pom" ) ) ); - assertEquals( "Check results size", 1, results.size() ); - - query = new TermQuery( new Term( StandardIndexRecordFields.VERSION, "1.0" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-pom" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-archetype" ) ) ); - assertEquals( "Check results size", 5, results.size() ); - -/* TODO: need to change analyzer to split on - if we want this - query = new TermQuery( new Term( StandardIndexRecordFields.VERSION, "snapshot" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check results size", results.isEmpty() ); - - query = new TermQuery( new Term( StandardIndexRecordFields.VERSION, "alpha" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) ); - assertEquals( "Check results size", 2, results.size() ); -*/ - - // test non-match fails - query = new TermQuery( new Term( StandardIndexRecordFields.VERSION, "foo" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check results size", results.isEmpty() ); - } - - public void testMatchBaseVersion() - throws RepositoryIndexSearchException - { - // If partial matches are desired, need to change the analyzer for versions to split on '.' - Query query = new TermQuery( new Term( StandardIndexRecordFields.BASE_VERSION, "1" ) ); - List results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check result", results.contains( records.get( "parent-pom" ) ) ); - assertEquals( "Check results size", 1, results.size() ); - - query = new TermQuery( new Term( StandardIndexRecordFields.BASE_VERSION, "1.0" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-pom" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-archetype" ) ) ); - assertEquals( "Check results size", 5, results.size() ); - -/* TODO: need to change analyzer to split on - if we want this - query = new TermQuery( new Term( StandardIndexRecordFields.BASE_VERSION, "snapshot" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) ); - assertEquals( "Check results size", 1, results.size() ); - - query = new TermQuery( new Term( StandardIndexRecordFields.BASE_VERSION, "alpha" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) ); - assertEquals( "Check results size", 2, results.size() ); -*/ - - // test non-match fails - query = new TermQuery( new Term( StandardIndexRecordFields.BASE_VERSION, "foo" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check results size", results.isEmpty() ); - } - - public void testMatchClassifier() - throws RepositoryIndexSearchException - { - BooleanQuery bQuery = new BooleanQuery(); - bQuery.add( new MatchAllDocsQuery(), BooleanClause.Occur.MUST ); - bQuery.add( new TermQuery( new Term( StandardIndexRecordFields.CLASSIFIER, "jdk14" ) ), - BooleanClause.Occur.MUST_NOT ); - List results = index.search( new LuceneQuery( bQuery ) ); - - assertFalse( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) ); - assertFalse( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) ); - assertEquals( "Check results size", 8, results.size() ); - - // TODO: can we search for "anything with no classifier" ? - - Query query = new TermQuery( new Term( StandardIndexRecordFields.CLASSIFIER, "jdk14" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) ); - assertEquals( "Check results size", 2, results.size() ); - - // test non-match fails - query = new TermQuery( new Term( StandardIndexRecordFields.CLASSIFIER, "foo" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check results size", results.isEmpty() ); - } - - public void testMatchClass() - throws RepositoryIndexSearchException - { - // TODO: should be preserving case! - Query query = new TermQuery( new Term( StandardIndexRecordFields.CLASSES, "b.c.c" ) ); - List results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) ); - assertEquals( "Check results size", 5, results.size() ); - -/* TODO!: need to change the analyzer if we want partial classes (split on '.') - query = new TermQuery( new Term( StandardIndexRecordFields.CLASSES, "C" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check result", results.contains( records.get( "test-jar" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-jar-jdk14" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-jar-and-pom-jdk14" ) ) ); - assertEquals( "Check results size", 4, results.size() ); - - query = new TermQuery( new Term( StandardIndexRecordFields.CLASSES, "MyMojo" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) ); - assertEquals( "Check results size", 1, results.size() ); -*/ - - // test non-match fails - query = new TermQuery( new Term( StandardIndexRecordFields.CLASSES, "foo" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check results size", results.isEmpty() ); - } - - public void testMatchFiles() - throws RepositoryIndexSearchException - { - // TODO: should be preserving case! - Query query = new TermQuery( new Term( StandardIndexRecordFields.FILES, "manifest.mf" ) ); - List results = index.search( new LuceneQuery( query ) ); - - assertFalse( "Check result", results.contains( records.get( "test-pom" ) ) ); - assertFalse( "Check result", results.contains( records.get( "parent-pom" ) ) ); - assertFalse( "Check result", results.contains( records.get( "test-dll" ) ) ); - assertEquals( "Check results size", 7, results.size() ); - -/* - // TODO: should be preserving case, and '-inf'! - query = new TermQuery( new Term( StandardIndexRecordFields.FILES, "meta-inf" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertFalse( "Check result", results.contains( records.get( "test-pom" ) ) ); - assertFalse( "Check result", results.contains( records.get( "parent-pom" ) ) ); - assertFalse( "Check result", results.contains( records.get( "test-dll" ) ) ); - assertEquals( "Check results size", 7, results.size() ); -*/ - - query = new TermQuery( new Term( StandardIndexRecordFields.FILES, "plugin.xml" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) ); - assertEquals( "Check results size", 1, results.size() ); - - // test non-match fails - query = new TermQuery( new Term( StandardIndexRecordFields.FILES, "foo" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check results size", results.isEmpty() ); - } - - public void testMatchProjectName() - throws RepositoryIndexSearchException - { - Query query = new TermQuery( new Term( StandardIndexRecordFields.PROJECT_NAME, "mojo" ) ); - List results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check result", results.contains( records.get( "test-plugin" ) ) ); - assertEquals( "Check results size", 1, results.size() ); - - query = new TermQuery( new Term( StandardIndexRecordFields.PROJECT_NAME, "maven" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertFalse( "Check result", results.contains( records.get( "parent-pom" ) ) ); - assertFalse( "Check result", results.contains( records.get( "test-child-pom" ) ) ); - assertEquals( "Check results size", 2, results.size() ); - - // test non-match fails - query = new TermQuery( new Term( StandardIndexRecordFields.PROJECT_NAME, "foo" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check results size", results.isEmpty() ); - } - - public void testMatchProjectDescription() - throws RepositoryIndexSearchException - { - Query query = new TermQuery( new Term( StandardIndexRecordFields.PROJECT_DESCRIPTION, "description" ) ); - List results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check result", results.contains( records.get( "test-child-pom" ) ) ); - assertTrue( "Check result", results.contains( records.get( "parent-pom" ) ) ); - assertTrue( "Check result", results.contains( records.get( "test-pom" ) ) ); - assertEquals( "Check results size", 3, results.size() ); - - // test non-match fails - query = new TermQuery( new Term( StandardIndexRecordFields.PROJECT_DESCRIPTION, "foo" ) ); - results = index.search( new LuceneQuery( query ) ); - - assertTrue( "Check results size", results.isEmpty() ); - } - - private Artifact createArtifact( String artifactId ) - { - return createArtifact( artifactId, "1.0", "jar", null ); - } - - private Artifact createArtifact( String artifactId, String version, String type ) - { - return createArtifact( artifactId, version, type, null ); - } - - private Artifact createArtifact( String artifactId, String version, String type, String classifier ) - { - Artifact artifact = artifactFactory.createDependencyArtifact( "org.apache.maven.archiva.record", artifactId, - VersionRange.createFromVersion( version ), type, - classifier, Artifact.SCOPE_RUNTIME ); - artifact.isSnapshot(); - artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); - artifact.setRepository( repository ); - return artifact; - } -} diff --git a/archiva-indexer/src/test/java/org/apache/maven/archiva/indexing/lucene/LuceneStandardArtifactIndexTest.java b/archiva-indexer/src/test/java/org/apache/maven/archiva/indexing/lucene/LuceneStandardArtifactIndexTest.java deleted file mode 100644 index 8c25109a3..000000000 --- a/archiva-indexer/src/test/java/org/apache/maven/archiva/indexing/lucene/LuceneStandardArtifactIndexTest.java +++ /dev/null @@ -1,407 +0,0 @@ -package org.apache.maven.archiva.indexing.lucene; - -/* - * Copyright 2005-2006 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import org.apache.lucene.analysis.standard.StandardAnalyzer; -import org.apache.lucene.document.Document; -import org.apache.lucene.document.NumberTools; -import org.apache.lucene.index.IndexReader; -import org.apache.lucene.index.IndexWriter; -import org.apache.maven.archiva.indexing.RepositoryArtifactIndex; -import org.apache.maven.archiva.indexing.RepositoryArtifactIndexFactory; -import org.apache.maven.archiva.indexing.RepositoryIndexException; -import org.apache.maven.archiva.indexing.record.RepositoryIndexRecord; -import org.apache.maven.archiva.indexing.record.RepositoryIndexRecordFactory; -import org.apache.maven.archiva.indexing.record.StandardIndexRecordFields; -import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.factory.ArtifactFactory; -import org.apache.maven.artifact.repository.ArtifactRepository; -import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; -import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; -import org.codehaus.plexus.PlexusTestCase; -import org.codehaus.plexus.util.FileUtils; -import org.codehaus.plexus.util.xml.pull.XmlPullParserException; - -import java.io.File; -import java.io.IOException; -import java.text.SimpleDateFormat; -import java.util.Collections; -import java.util.Date; -import java.util.Iterator; -import java.util.List; -import java.util.Locale; -import java.util.TimeZone; - -/** - * Test the Lucene implementation of the artifact index. - * - * @author Brett Porter - */ -public class LuceneStandardArtifactIndexTest - extends PlexusTestCase -{ - private RepositoryArtifactIndex index; - - private ArtifactRepository repository; - - private ArtifactFactory artifactFactory; - - private File indexLocation; - - private RepositoryIndexRecordFactory recordFactory; - - protected void setUp() - throws Exception - { - super.setUp(); - - recordFactory = (RepositoryIndexRecordFactory) lookup( RepositoryIndexRecordFactory.ROLE, "standard" ); - - artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE ); - - ArtifactRepositoryFactory repositoryFactory = - (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE ); - - ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" ); - - File file = getTestFile( "src/test/managed-repository" ); - repository = - repositoryFactory.createArtifactRepository( "test", file.toURI().toURL().toString(), layout, null, null ); - - RepositoryArtifactIndexFactory factory = - (RepositoryArtifactIndexFactory) lookup( RepositoryArtifactIndexFactory.ROLE, "lucene" ); - - indexLocation = getTestFile( "target/test-index" ); - - FileUtils.deleteDirectory( indexLocation ); - - index = factory.createStandardIndex( indexLocation ); - } - - public void testIndexExists() - throws IOException, RepositoryIndexException - { - assertFalse( "check index doesn't exist", index.exists() ); - - // create empty directory - indexLocation.mkdirs(); - assertFalse( "check index doesn't exist even if directory does", index.exists() ); - - // create index, with no records - createEmptyIndex(); - assertTrue( "check index is considered to exist", index.exists() ); - - // Test non-directory - FileUtils.deleteDirectory( indexLocation ); - indexLocation.createNewFile(); - try - { - index.exists(); - fail( "Index operation should fail as the location is not valid" ); - } - catch ( RepositoryIndexException e ) - { - // great - } - finally - { - indexLocation.delete(); - } - } - - public void testAddRecordNoIndex() - throws IOException, RepositoryIndexException - { - Artifact artifact = createArtifact( "test-jar" ); - - RepositoryIndexRecord record = recordFactory.createRecord( artifact ); - index.indexRecords( Collections.singletonList( record ) ); - - IndexReader reader = IndexReader.open( indexLocation ); - try - { - Document document = reader.document( 0 ); - assertJarRecord( artifact, document ); - assertEquals( "Check index size", 1, reader.numDocs() ); - } - finally - { - reader.close(); - } - } - - public void testAddRecordExistingEmptyIndex() - throws IOException, RepositoryIndexException - { - createEmptyIndex(); - - Artifact artifact = createArtifact( "test-jar" ); - - RepositoryIndexRecord record = recordFactory.createRecord( artifact ); - index.indexRecords( Collections.singletonList( record ) ); - - IndexReader reader = IndexReader.open( indexLocation ); - try - { - Document document = reader.document( 0 ); - assertJarRecord( artifact, document ); - assertEquals( "Check index size", 1, reader.numDocs() ); - } - finally - { - reader.close(); - } - } - - public void testAddRecordInIndex() - throws IOException, RepositoryIndexException - { - createEmptyIndex(); - - Artifact artifact = createArtifact( "test-jar" ); - - RepositoryIndexRecord record = recordFactory.createRecord( artifact ); - index.indexRecords( Collections.singletonList( record ) ); - - // Do it again - record = recordFactory.createRecord( artifact ); - index.indexRecords( Collections.singletonList( record ) ); - - IndexReader reader = IndexReader.open( indexLocation ); - try - { - Document document = reader.document( 0 ); - assertJarRecord( artifact, document ); - assertEquals( "Check index size", 1, reader.numDocs() ); - } - finally - { - reader.close(); - } - } - - public void testAddPomRecord() - throws IOException, RepositoryIndexException - { - createEmptyIndex(); - - Artifact artifact = createArtifact( "test-pom", "1.0", "pom" ); - - RepositoryIndexRecord record = recordFactory.createRecord( artifact ); - index.indexRecords( Collections.singletonList( record ) ); - - IndexReader reader = IndexReader.open( indexLocation ); - try - { - Document document = reader.document( 0 ); - assertPomRecord( artifact, document ); - assertEquals( "Check index size", 1, reader.numDocs() ); - } - finally - { - reader.close(); - } - } - - public void testAddPlugin() - throws IOException, RepositoryIndexException, XmlPullParserException - { - createEmptyIndex(); - - Artifact artifact = createArtifact( "test-plugin" ); - - RepositoryIndexRecord record = recordFactory.createRecord( artifact ); - - index.indexRecords( Collections.singletonList( record ) ); - - IndexReader reader = IndexReader.open( indexLocation ); - try - { - Document document = reader.document( 0 ); - assertPluginRecord( artifact, document ); - assertEquals( "Check index size", 1, reader.numDocs() ); - } - finally - { - reader.close(); - } - } - - public void testDeleteRecordInIndex() - throws IOException, RepositoryIndexException - { - createEmptyIndex(); - - Artifact artifact = createArtifact( "test-jar" ); - - RepositoryIndexRecord record = recordFactory.createRecord( artifact ); - index.indexRecords( Collections.singletonList( record ) ); - - index.deleteRecords( Collections.singletonList( record ) ); - - IndexReader reader = IndexReader.open( indexLocation ); - try - { - assertEquals( "No documents", 0, reader.numDocs() ); - } - finally - { - reader.close(); - } - } - - public void testDeleteRecordNotInIndex() - throws IOException, RepositoryIndexException - { - createEmptyIndex(); - - Artifact artifact = createArtifact( "test-jar" ); - - RepositoryIndexRecord record = recordFactory.createRecord( artifact ); - - index.deleteRecords( Collections.singletonList( record ) ); - - IndexReader reader = IndexReader.open( indexLocation ); - try - { - assertEquals( "No documents", 0, reader.numDocs() ); - } - finally - { - reader.close(); - } - } - - public void testDeleteRecordNoIndex() - throws IOException, RepositoryIndexException - { - Artifact artifact = createArtifact( "test-jar" ); - - RepositoryIndexRecord record = recordFactory.createRecord( artifact ); - index.deleteRecords( Collections.singleton( record ) ); - - assertFalse( index.exists() ); - } - - private Artifact createArtifact( String artifactId ) - { - return createArtifact( artifactId, "1.0", "jar" ); - } - - private Artifact createArtifact( String artifactId, String version, String type ) - { - Artifact artifact = - artifactFactory.createBuildArtifact( "org.apache.maven.archiva.record", artifactId, version, type ); - artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); - artifact.setRepository( repository ); - return artifact; - } - - private void createEmptyIndex() - throws IOException - { - createIndex( Collections.EMPTY_LIST ); - } - - private void createIndex( List docments ) - throws IOException - { - IndexWriter writer = new IndexWriter( indexLocation, new StandardAnalyzer(), true ); - for ( Iterator i = docments.iterator(); i.hasNext(); ) - { - Document document = (Document) i.next(); - writer.addDocument( document ); - } - writer.optimize(); - writer.close(); - } - - private void assertRecord( Artifact artifact, Document document, String expectedArtifactId, String expectedType, - String expectedMd5, String expectedSha1 ) - { - assertEquals( "Check document filename", repository.pathOf( artifact ), - document.get( StandardIndexRecordFields.FILENAME ) ); - assertEquals( "Check document groupId", "org.apache.maven.archiva.record", - document.get( StandardIndexRecordFields.GROUPID ) ); - assertEquals( "Check document artifactId", expectedArtifactId, - document.get( StandardIndexRecordFields.ARTIFACTID ) ); - assertEquals( "Check document version", "1.0", document.get( StandardIndexRecordFields.VERSION ) ); - assertEquals( "Check document type", expectedType, document.get( StandardIndexRecordFields.TYPE ) ); - assertEquals( "Check document repository", "test", document.get( StandardIndexRecordFields.REPOSITORY ) ); - assertEquals( "Check document timestamp", getLastModified( artifact.getFile() ), - document.get( StandardIndexRecordFields.LAST_MODIFIED ) ); - assertEquals( "Check document md5", expectedMd5, document.get( StandardIndexRecordFields.MD5 ) ); - assertEquals( "Check document sha1", expectedSha1, document.get( StandardIndexRecordFields.SHA1 ) ); - assertEquals( "Check document file size", artifact.getFile().length(), - NumberTools.stringToLong( document.get( StandardIndexRecordFields.FILE_SIZE ) ) ); - assertNull( "Check document classifier", document.get( StandardIndexRecordFields.CLASSIFIER ) ); - } - - private void assertPomRecord( Artifact artifact, Document document ) - { - assertRecord( artifact, document, "test-pom", "pom", "103d11ac601a42ccf2a2ae54d308c362", - "4c4d237c5366df877c3a636d5b6241822d090355" ); - assertNull( "Check document classes", document.get( StandardIndexRecordFields.CLASSES ) ); - assertNull( "Check document files", document.get( StandardIndexRecordFields.FILES ) ); - assertNull( "Check document pluginPrefix", document.get( StandardIndexRecordFields.PLUGIN_PREFIX ) ); - assertEquals( "Check document year", "2005", document.get( StandardIndexRecordFields.INCEPTION_YEAR ) ); - assertEquals( "Check document project name", "Maven Repository Manager Test POM", - document.get( StandardIndexRecordFields.PROJECT_NAME ) ); - assertEquals( "Check document project description", "Description", - document.get( StandardIndexRecordFields.PROJECT_DESCRIPTION ) ); - assertEquals( "Check document packaging", "pom", document.get( StandardIndexRecordFields.PACKAGING ) ); - } - - private void assertJarRecord( Artifact artifact, Document document ) - { - assertRecord( artifact, document, "test-jar", "jar", "3a0adc365f849366cd8b633cad155cb7", - "c66f18bf192cb613fc2febb4da541a34133eedc2" ); - assertEquals( "Check document classes", "A\nb.B\nb.c.C", document.get( StandardIndexRecordFields.CLASSES ) ); - assertEquals( "Check document files", "META-INF/MANIFEST.MF\nA.class\nb/B.class\nb/c/C.class", - document.get( StandardIndexRecordFields.FILES ) ); - assertNull( "Check document inceptionYear", document.get( StandardIndexRecordFields.INCEPTION_YEAR ) ); - assertNull( "Check document projectName", document.get( StandardIndexRecordFields.PROJECT_NAME ) ); - assertNull( "Check document projectDesc", document.get( StandardIndexRecordFields.PROJECT_DESCRIPTION ) ); - assertNull( "Check document pluginPrefix", document.get( StandardIndexRecordFields.PLUGIN_PREFIX ) ); - assertNull( "Check document packaging", document.get( StandardIndexRecordFields.PACKAGING ) ); - } - - private void assertPluginRecord( Artifact artifact, Document document ) - { - assertRecord( artifact, document, "test-plugin", "maven-plugin", "3530896791670ebb45e17708e5d52c40", - "2cd2619d59a684e82e97471d2c2e004144c8f24e" ); - assertEquals( "Check document classes", "org.apache.maven.archiva.record.MyMojo", - document.get( StandardIndexRecordFields.CLASSES ) ); - assertEquals( "Check document files", "META-INF/MANIFEST.MF\n" + - "META-INF/maven/org.apache.maven.archiva.record/test-plugin/pom.properties\n" + - "META-INF/maven/org.apache.maven.archiva.record/test-plugin/pom.xml\n" + "META-INF/maven/plugin.xml\n" + - "org/apache/maven/archiva/record/MyMojo.class", document.get( StandardIndexRecordFields.FILES ) ); - assertEquals( "Check document pluginPrefix", "test", document.get( StandardIndexRecordFields.PLUGIN_PREFIX ) ); - assertEquals( "Check document packaging", "maven-plugin", document.get( StandardIndexRecordFields.PACKAGING ) ); - assertNull( "Check document inceptionYear", document.get( StandardIndexRecordFields.INCEPTION_YEAR ) ); - assertEquals( "Check document project name", "Maven Mojo Archetype", - document.get( StandardIndexRecordFields.PROJECT_NAME ) ); - assertNull( "Check document projectDesc", document.get( StandardIndexRecordFields.PROJECT_DESCRIPTION ) ); - } - - private String getLastModified( File file ) - { - SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyyMMddHHmmss", Locale.US ); - dateFormat.setTimeZone( TimeZone.getTimeZone( "UTC" ) ); - return dateFormat.format( new Date( file.lastModified() ) ); - } -} diff --git a/archiva-indexer/src/test/java/org/apache/maven/archiva/indexing/query/QueryTest.java b/archiva-indexer/src/test/java/org/apache/maven/archiva/indexing/query/QueryTest.java deleted file mode 100644 index 2e90f10e6..000000000 --- a/archiva-indexer/src/test/java/org/apache/maven/archiva/indexing/query/QueryTest.java +++ /dev/null @@ -1,156 +0,0 @@ -package org.apache.maven.archiva.indexing.query; - -/* - * Copyright 2005-2006 The Apache Software Foundation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import junit.framework.TestCase; - -import java.util.Iterator; - -/** - * @author Brett Porter - */ -public class QueryTest - extends TestCase -{ - private QueryTerm term1 = new QueryTerm( "field1", "value1" ); - - private QueryTerm term2 = new QueryTerm( "field2", "value2" ); - - private QueryTerm term3 = new QueryTerm( "field3", "value3" ); - - public void testQueryTerm() - { - QueryTerm query = new QueryTerm( "Field", "Value" ); - assertEquals( "check field setting", "Field", query.getField() ); - assertEquals( "check value setting", "Value", query.getValue() ); - } - - public void testSingleTermQuery() - { - SingleTermQuery query = new SingleTermQuery( "Field", "Value" ); - assertEquals( "check field setting", "Field", query.getField() ); - assertEquals( "check value setting", "Value", query.getValue() ); - - query = new SingleTermQuery( term1 ); - assertEquals( "check field setting", "field1", query.getField() ); - assertEquals( "check value setting", "value1", query.getValue() ); - } - - public void testRangeQueryOpen() - { - RangeQuery rangeQuery = RangeQuery.createOpenRange(); - assertNull( "Check range has no start", rangeQuery.getBegin() ); - assertNull( "Check range has no end", rangeQuery.getEnd() ); - } - - public void testRangeQueryExclusive() - { - RangeQuery rangeQuery = RangeQuery.createExclusiveRange( term1, term2 ); - assertEquals( "Check range start", term1, rangeQuery.getBegin() ); - assertEquals( "Check range end", term2, rangeQuery.getEnd() ); - assertFalse( "Check exclusive", rangeQuery.isInclusive() ); - } - - public void testRangeQueryInclusive() - { - RangeQuery rangeQuery = RangeQuery.createInclusiveRange( term1, term2 ); - assertEquals( "Check range start", term1, rangeQuery.getBegin() ); - assertEquals( "Check range end", term2, rangeQuery.getEnd() ); - assertTrue( "Check inclusive", rangeQuery.isInclusive() ); - } - - public void testRangeQueryOpenEnded() - { - RangeQuery rangeQuery = RangeQuery.createGreaterThanOrEqualToRange( term1 ); - assertEquals( "Check range start", term1, rangeQuery.getBegin() ); - assertNull( "Check range end", rangeQuery.getEnd() ); - assertTrue( "Check inclusive", rangeQuery.isInclusive() ); - - rangeQuery = RangeQuery.createGreaterThanRange( term1 ); - assertEquals( "Check range start", term1, rangeQuery.getBegin() ); - assertNull( "Check range end", rangeQuery.getEnd() ); - assertFalse( "Check exclusive", rangeQuery.isInclusive() ); - - rangeQuery = RangeQuery.createLessThanOrEqualToRange( term1 ); - assertNull( "Check range start", rangeQuery.getBegin() ); - assertEquals( "Check range end", term1, rangeQuery.getEnd() ); - assertTrue( "Check inclusive", rangeQuery.isInclusive() ); - - rangeQuery = RangeQuery.createLessThanRange( term1 ); - assertNull( "Check range start", rangeQuery.getBegin() ); - assertEquals( "Check range end", term1, rangeQuery.getEnd() ); - assertFalse( "Check exclusive", rangeQuery.isInclusive() ); - } - - public void testCompundQuery() - { - CompoundQuery query = new CompoundQuery(); - assertTrue( "check query is empty", query.getCompoundQueryTerms().isEmpty() ); - - query.and( term1 ); - query.or( term2 ); - query.not( term3 ); - - Iterator i = query.getCompoundQueryTerms().iterator(); - CompoundQueryTerm term = (CompoundQueryTerm) i.next(); - assertEquals( "Check first term", "field1", getQuery( term ).getField() ); - assertEquals( "Check first term", "value1", getQuery( term ).getValue() ); - assertTrue( "Check first term", term.isRequired() ); - assertFalse( "Check first term", term.isProhibited() ); - - term = (CompoundQueryTerm) i.next(); - assertEquals( "Check second term", "field2", getQuery( term ).getField() ); - assertEquals( "Check second term", "value2", getQuery( term ).getValue() ); - assertFalse( "Check second term", term.isRequired() ); - assertFalse( "Check second term", term.isProhibited() ); - - term = (CompoundQueryTerm) i.next(); - assertEquals( "Check third term", "field3", getQuery( term ).getField() ); - assertEquals( "Check third term", "value3", getQuery( term ).getValue() ); - assertFalse( "Check third term", term.isRequired() ); - assertTrue( "Check third term", term.isProhibited() ); - - CompoundQuery query2 = new CompoundQuery(); - query2.and( query ); - query2.or( new SingleTermQuery( term2 ) ); - query2.not( new SingleTermQuery( term3 ) ); - - i = query2.getCompoundQueryTerms().iterator(); - term = (CompoundQueryTerm) i.next(); - assertEquals( "Check first term", query, term.getQuery() ); - assertTrue( "Check first term", term.isRequired() ); - assertFalse( "Check first term", term.isProhibited() ); - - term = (CompoundQueryTerm) i.next(); - assertEquals( "Check second term", "field2", getQuery( term ).getField() ); - assertEquals( "Check second term", "value2", getQuery( term ).getValue() ); - assertFalse( "Check second term", term.isRequired() ); - assertFalse( "Check second term", term.isProhibited() ); - - term = (CompoundQueryTerm) i.next(); - assertEquals( "Check third term", "field3", getQuery( term ).getField() ); - assertEquals( "Check third term", "value3", getQuery( term ).getValue() ); - assertFalse( "Check third term", term.isRequired() ); - assertTrue( "Check third term", term.isProhibited() ); - } - - private static SingleTermQuery getQuery( CompoundQueryTerm term ) - { - return (SingleTermQuery) term.getQuery(); - } -} - diff --git a/archiva-indexer/src/test/java/org/apache/maven/archiva/indexing/record/MinimalArtifactIndexRecordFactoryTest.java b/archiva-indexer/src/test/java/org/apache/maven/archiva/indexing/record/MinimalArtifactIndexRecordFactoryTest.java deleted file mode 100644 index 9f35d9440..000000000 --- a/archiva-indexer/src/test/java/org/apache/maven/archiva/indexing/record/MinimalArtifactIndexRecordFactoryTest.java +++ /dev/null @@ -1,240 +0,0 @@ -package org.apache.maven.archiva.indexing.record; - -/* - * 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.archiva.indexing.RepositoryIndexException; -import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.factory.ArtifactFactory; -import org.apache.maven.artifact.repository.ArtifactRepository; -import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; -import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; -import org.apache.maven.artifact.versioning.VersionRange; -import org.codehaus.plexus.PlexusTestCase; -import org.codehaus.plexus.util.xml.pull.XmlPullParserException; - -import java.io.File; -import java.io.IOException; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -/** - * Test the minimal artifact index record. - * - * @author Brett Porter - */ -public class MinimalArtifactIndexRecordFactoryTest - extends PlexusTestCase -{ - private RepositoryIndexRecordFactory factory; - - private ArtifactRepository repository; - - private ArtifactFactory artifactFactory; - - private static final String TEST_GROUP_ID = "org.apache.maven.archiva.record"; - - private static final List JAR_CLASS_LIST = Arrays.asList( new String[]{"A", "b.B", "b.c.C"} ); - - protected void setUp() - throws Exception - { - super.setUp(); - - factory = (RepositoryIndexRecordFactory) lookup( RepositoryIndexRecordFactory.ROLE, "minimal" ); - - artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE ); - - ArtifactRepositoryFactory repositoryFactory = - (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE ); - - ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" ); - - File file = getTestFile( "src/test/managed-repository" ); - repository = - repositoryFactory.createArtifactRepository( "test", file.toURI().toURL().toString(), layout, null, null ); - } - - public void testIndexedJar() - throws RepositoryIndexException - { - Artifact artifact = createArtifact( "test-jar" ); - - RepositoryIndexRecord record = factory.createRecord( artifact ); - - MinimalArtifactIndexRecord expectedRecord = new MinimalArtifactIndexRecord(); - expectedRecord.setMd5Checksum( "3a0adc365f849366cd8b633cad155cb7" ); - expectedRecord.setFilename( repository.pathOf( artifact ) ); - expectedRecord.setLastModified( artifact.getFile().lastModified() ); - expectedRecord.setSize( artifact.getFile().length() ); - expectedRecord.setClasses( JAR_CLASS_LIST ); - - assertEquals( "check record", expectedRecord, record ); - } - - public void testIndexedJarWithClassifier() - throws RepositoryIndexException - { - Artifact artifact = createArtifact( "test-jar", "1.0", "jar", "jdk14" ); - - RepositoryIndexRecord record = factory.createRecord( artifact ); - - MinimalArtifactIndexRecord expectedRecord = new MinimalArtifactIndexRecord(); - expectedRecord.setMd5Checksum( "3a0adc365f849366cd8b633cad155cb7" ); - expectedRecord.setFilename( repository.pathOf( artifact ) ); - expectedRecord.setLastModified( artifact.getFile().lastModified() ); - expectedRecord.setSize( artifact.getFile().length() ); - expectedRecord.setClasses( JAR_CLASS_LIST ); - - assertEquals( "check record", expectedRecord, record ); - } - - public void testIndexedJarAndPom() - throws RepositoryIndexException - { - Artifact artifact = createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar" ); - - RepositoryIndexRecord record = factory.createRecord( artifact ); - - MinimalArtifactIndexRecord expectedRecord = new MinimalArtifactIndexRecord(); - expectedRecord.setMd5Checksum( "3a0adc365f849366cd8b633cad155cb7" ); - expectedRecord.setFilename( repository.pathOf( artifact ) ); - expectedRecord.setLastModified( artifact.getFile().lastModified() ); - expectedRecord.setSize( artifact.getFile().length() ); - expectedRecord.setClasses( JAR_CLASS_LIST ); - - assertEquals( "check record", expectedRecord, record ); - } - - public void testIndexedJarAndPomWithClassifier() - throws RepositoryIndexException - { - Artifact artifact = createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar", "jdk14" ); - - RepositoryIndexRecord record = factory.createRecord( artifact ); - - MinimalArtifactIndexRecord expectedRecord = new MinimalArtifactIndexRecord(); - expectedRecord.setMd5Checksum( "3a0adc365f849366cd8b633cad155cb7" ); - expectedRecord.setFilename( repository.pathOf( artifact ) ); - expectedRecord.setLastModified( artifact.getFile().lastModified() ); - expectedRecord.setSize( artifact.getFile().length() ); - expectedRecord.setClasses( JAR_CLASS_LIST ); - - assertEquals( "check record", expectedRecord, record ); - } - - public void testIndexedPom() - throws RepositoryIndexException - { - Artifact artifact = createArtifact( "test-pom", "1.0", "pom" ); - - RepositoryIndexRecord record = factory.createRecord( artifact ); - - assertNull( "Check no record", record ); - } - - public void testNonIndexedPom() - throws RepositoryIndexException - { - // If we pass in only the POM that belongs to a JAR, then expect null not the POM - Artifact artifact = createArtifact( "test-jar-and-pom", "1.0-alpha-1", "pom" ); - - RepositoryIndexRecord record = factory.createRecord( artifact ); - - assertNull( "Check no record", record ); - - artifact = createArtifact( "test-plugin", "1.0", "pom" ); - - record = factory.createRecord( artifact ); - - assertNull( "Check no record", record ); - - artifact = createArtifact( "test-archetype", "1.0", "pom" ); - - record = factory.createRecord( artifact ); - - assertNull( "Check no record", record ); - } - - public void testIndexedPlugin() - throws RepositoryIndexException, IOException, XmlPullParserException - { - Artifact artifact = createArtifact( "test-plugin" ); - - RepositoryIndexRecord record = factory.createRecord( artifact ); - - MinimalArtifactIndexRecord expectedRecord = new MinimalArtifactIndexRecord(); - expectedRecord.setMd5Checksum( "3530896791670ebb45e17708e5d52c40" ); - expectedRecord.setFilename( repository.pathOf( artifact ) ); - expectedRecord.setLastModified( artifact.getFile().lastModified() ); - expectedRecord.setSize( artifact.getFile().length() ); - expectedRecord.setClasses( Collections.singletonList( "org.apache.maven.archiva.record.MyMojo" ) ); - - assertEquals( "check record", expectedRecord, record ); - } - - public void testCorruptJar() - throws RepositoryIndexException - { - Artifact artifact = createArtifact( "test-corrupt-jar" ); - - RepositoryIndexRecord record = factory.createRecord( artifact ); - - assertNull( "Confirm no record is returned", record ); - } - - public void testNonJar() - throws RepositoryIndexException - { - Artifact artifact = createArtifact( "test-dll", "1.0.1.34", "dll" ); - - RepositoryIndexRecord record = factory.createRecord( artifact ); - - assertNull( "Confirm no record is returned", record ); - } - - public void testMissingFile() - throws RepositoryIndexException - { - Artifact artifact = createArtifact( "test-foo" ); - - RepositoryIndexRecord record = factory.createRecord( artifact ); - - assertNull( "Confirm no record is returned", record ); - } - - private Artifact createArtifact( String artifactId ) - { - return createArtifact( artifactId, "1.0", "jar" ); - } - - private Artifact createArtifact( String artifactId, String version, String type ) - { - return createArtifact( artifactId, version, type, null ); - } - - private Artifact createArtifact( String artifactId, String version, String type, String classifier ) - { - Artifact artifact = artifactFactory.createDependencyArtifact( TEST_GROUP_ID, artifactId, - VersionRange.createFromVersion( version ), type, - classifier, Artifact.SCOPE_RUNTIME ); - artifact.isSnapshot(); - artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); - artifact.setRepository( repository ); - return artifact; - } -} diff --git a/archiva-indexer/src/test/java/org/apache/maven/archiva/indexing/record/StandardArtifactIndexRecordFactoryTest.java b/archiva-indexer/src/test/java/org/apache/maven/archiva/indexing/record/StandardArtifactIndexRecordFactoryTest.java deleted file mode 100644 index 842228cb3..000000000 --- a/archiva-indexer/src/test/java/org/apache/maven/archiva/indexing/record/StandardArtifactIndexRecordFactoryTest.java +++ /dev/null @@ -1,382 +0,0 @@ -package org.apache.maven.archiva.indexing.record; - -/* - * 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.archiva.indexing.RepositoryIndexException; -import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.factory.ArtifactFactory; -import org.apache.maven.artifact.repository.ArtifactRepository; -import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; -import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; -import org.apache.maven.artifact.versioning.VersionRange; -import org.codehaus.plexus.PlexusTestCase; -import org.codehaus.plexus.util.xml.pull.XmlPullParserException; - -import java.io.File; -import java.io.IOException; -import java.util.Arrays; -import java.util.List; - -/** - * Test the minimal artifact index record. - * - * @author Brett Porter - */ -public class StandardArtifactIndexRecordFactoryTest - extends PlexusTestCase -{ - private RepositoryIndexRecordFactory factory; - - private ArtifactRepository repository; - - private ArtifactFactory artifactFactory; - - private static final String TEST_GROUP_ID = "org.apache.maven.archiva.record"; - - private static final List JAR_CLASS_LIST = Arrays.asList( new String[]{"A", "b.B", "b.c.C"} ); - - private static final List JAR_FILE_LIST = - Arrays.asList( new String[]{"META-INF/MANIFEST.MF", "A.class", "b/B.class", "b/c/C.class"} ); - - protected void setUp() - throws Exception - { - super.setUp(); - - factory = (RepositoryIndexRecordFactory) lookup( RepositoryIndexRecordFactory.ROLE, "standard" ); - - artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE ); - - ArtifactRepositoryFactory repositoryFactory = - (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE ); - - ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" ); - - File file = getTestFile( "src/test/managed-repository" ); - repository = - repositoryFactory.createArtifactRepository( "test", file.toURI().toURL().toString(), layout, null, null ); - } - - public void testIndexedJar() - throws RepositoryIndexException - { - Artifact artifact = createArtifact( "test-jar" ); - - RepositoryIndexRecord record = factory.createRecord( artifact ); - - StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord(); - expectedRecord.setMd5Checksum( "3a0adc365f849366cd8b633cad155cb7" ); - expectedRecord.setFilename( repository.pathOf( artifact ) ); - expectedRecord.setLastModified( artifact.getFile().lastModified() ); - expectedRecord.setSize( artifact.getFile().length() ); - expectedRecord.setClasses( JAR_CLASS_LIST ); - expectedRecord.setArtifactId( "test-jar" ); - expectedRecord.setGroupId( TEST_GROUP_ID ); - expectedRecord.setBaseVersion( "1.0" ); - expectedRecord.setVersion( "1.0" ); - expectedRecord.setFiles( JAR_FILE_LIST ); - expectedRecord.setSha1Checksum( "c66f18bf192cb613fc2febb4da541a34133eedc2" ); - expectedRecord.setType( "jar" ); - expectedRecord.setRepository( "test" ); - - assertEquals( "check record", expectedRecord, record ); - } - - public void testIndexedJarWithClassifier() - throws RepositoryIndexException - { - Artifact artifact = createArtifact( "test-jar", "1.0", "jar", "jdk14" ); - - RepositoryIndexRecord record = factory.createRecord( artifact ); - - StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord(); - expectedRecord.setMd5Checksum( "3a0adc365f849366cd8b633cad155cb7" ); - expectedRecord.setFilename( repository.pathOf( artifact ) ); - expectedRecord.setLastModified( artifact.getFile().lastModified() ); - expectedRecord.setSize( artifact.getFile().length() ); - expectedRecord.setClasses( JAR_CLASS_LIST ); - expectedRecord.setArtifactId( "test-jar" ); - expectedRecord.setGroupId( TEST_GROUP_ID ); - expectedRecord.setBaseVersion( "1.0" ); - expectedRecord.setVersion( "1.0" ); - expectedRecord.setFiles( JAR_FILE_LIST ); - expectedRecord.setSha1Checksum( "c66f18bf192cb613fc2febb4da541a34133eedc2" ); - expectedRecord.setType( "jar" ); - expectedRecord.setRepository( "test" ); - expectedRecord.setClassifier( "jdk14" ); - - assertEquals( "check record", expectedRecord, record ); - } - - public void testIndexedJarAndPom() - throws RepositoryIndexException - { - Artifact artifact = createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar" ); - - RepositoryIndexRecord record = factory.createRecord( artifact ); - - StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord(); - expectedRecord.setMd5Checksum( "3a0adc365f849366cd8b633cad155cb7" ); - expectedRecord.setFilename( repository.pathOf( artifact ) ); - expectedRecord.setLastModified( artifact.getFile().lastModified() ); - expectedRecord.setSize( artifact.getFile().length() ); - expectedRecord.setClasses( JAR_CLASS_LIST ); - expectedRecord.setArtifactId( "test-jar-and-pom" ); - expectedRecord.setGroupId( TEST_GROUP_ID ); - expectedRecord.setBaseVersion( "1.0-alpha-1" ); - expectedRecord.setVersion( "1.0-alpha-1" ); - expectedRecord.setFiles( JAR_FILE_LIST ); - expectedRecord.setSha1Checksum( "c66f18bf192cb613fc2febb4da541a34133eedc2" ); - expectedRecord.setType( "jar" ); - expectedRecord.setRepository( "test" ); - expectedRecord.setPackaging( "jar" ); - expectedRecord.setProjectName( "Test JAR and POM" ); - - assertEquals( "check record", expectedRecord, record ); - } - - public void testIndexedJarAndPomWithClassifier() - throws RepositoryIndexException - { - Artifact artifact = createArtifact( "test-jar-and-pom", "1.0-alpha-1", "jar", "jdk14" ); - - RepositoryIndexRecord record = factory.createRecord( artifact ); - - StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord(); - expectedRecord.setMd5Checksum( "3a0adc365f849366cd8b633cad155cb7" ); - expectedRecord.setFilename( repository.pathOf( artifact ) ); - expectedRecord.setLastModified( artifact.getFile().lastModified() ); - expectedRecord.setSize( artifact.getFile().length() ); - expectedRecord.setClasses( JAR_CLASS_LIST ); - expectedRecord.setArtifactId( "test-jar-and-pom" ); - expectedRecord.setGroupId( TEST_GROUP_ID ); - expectedRecord.setBaseVersion( "1.0-alpha-1" ); - expectedRecord.setVersion( "1.0-alpha-1" ); - expectedRecord.setFiles( JAR_FILE_LIST ); - expectedRecord.setSha1Checksum( "c66f18bf192cb613fc2febb4da541a34133eedc2" ); - expectedRecord.setType( "jar" ); - expectedRecord.setRepository( "test" ); - expectedRecord.setPackaging( "jar" ); - expectedRecord.setProjectName( "Test JAR and POM" ); - expectedRecord.setClassifier( "jdk14" ); - - assertEquals( "check record", expectedRecord, record ); - } - - public void testIndexedJarWithParentPom() - throws RepositoryIndexException - { - Artifact artifact = createArtifact( "test-child-pom", "1.0-20060728.121314-1", "jar" ); - - RepositoryIndexRecord record = factory.createRecord( artifact ); - - StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord(); - expectedRecord.setMd5Checksum( "3a0adc365f849366cd8b633cad155cb7" ); - expectedRecord.setFilename( repository.pathOf( artifact ) ); - expectedRecord.setLastModified( artifact.getFile().lastModified() ); - expectedRecord.setSize( artifact.getFile().length() ); - expectedRecord.setClasses( JAR_CLASS_LIST ); - expectedRecord.setArtifactId( "test-child-pom" ); - expectedRecord.setGroupId( TEST_GROUP_ID ); - expectedRecord.setBaseVersion( "1.0-SNAPSHOT" ); - expectedRecord.setVersion( "1.0-20060728.121314-1" ); - expectedRecord.setFiles( JAR_FILE_LIST ); - expectedRecord.setSha1Checksum( "c66f18bf192cb613fc2febb4da541a34133eedc2" ); - expectedRecord.setType( "jar" ); - expectedRecord.setRepository( "test" ); - expectedRecord.setPackaging( "jar" ); - expectedRecord.setProjectName( "Child Project" ); - expectedRecord.setProjectDescription( "Description" ); - expectedRecord.setInceptionYear( "2005" ); - - assertEquals( "check record", expectedRecord, record ); - } - - public void testIndexedPom() - throws RepositoryIndexException - { - Artifact artifact = createArtifact( "test-pom", "1.0", "pom" ); - - RepositoryIndexRecord record = factory.createRecord( artifact ); - - StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord(); - expectedRecord.setMd5Checksum( "103d11ac601a42ccf2a2ae54d308c362" ); - expectedRecord.setFilename( repository.pathOf( artifact ) ); - expectedRecord.setLastModified( artifact.getFile().lastModified() ); - expectedRecord.setSize( artifact.getFile().length() ); - expectedRecord.setArtifactId( "test-pom" ); - expectedRecord.setGroupId( TEST_GROUP_ID ); - expectedRecord.setBaseVersion( "1.0" ); - expectedRecord.setVersion( "1.0" ); - expectedRecord.setSha1Checksum( "4c4d237c5366df877c3a636d5b6241822d090355" ); - expectedRecord.setType( "pom" ); - expectedRecord.setRepository( "test" ); - expectedRecord.setPackaging( "pom" ); - expectedRecord.setInceptionYear( "2005" ); - expectedRecord.setProjectName( "Maven Repository Manager Test POM" ); - expectedRecord.setProjectDescription( "Description" ); - - assertEquals( "check record", expectedRecord, record ); - } - - public void testNonIndexedPom() - throws RepositoryIndexException - { - // If we pass in only the POM that belongs to a JAR, then expect null not the POM - Artifact artifact = createArtifact( "test-jar-and-pom", "1.0", "pom" ); - - RepositoryIndexRecord record = factory.createRecord( artifact ); - - assertNull( "Check no record", record ); - - artifact = createArtifact( "test-plugin", "1.0", "pom" ); - - record = factory.createRecord( artifact ); - - assertNull( "Check no record", record ); - - artifact = createArtifact( "test-archetype", "1.0", "pom" ); - - record = factory.createRecord( artifact ); - - assertNull( "Check no record", record ); - } - - public void testIndexedPlugin() - throws RepositoryIndexException, IOException, XmlPullParserException - { - Artifact artifact = createArtifact( "test-plugin" ); - - RepositoryIndexRecord record = factory.createRecord( artifact ); - - StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord(); - expectedRecord.setMd5Checksum( "3530896791670ebb45e17708e5d52c40" ); - expectedRecord.setFilename( repository.pathOf( artifact ) ); - expectedRecord.setLastModified( artifact.getFile().lastModified() ); - expectedRecord.setSize( artifact.getFile().length() ); - expectedRecord.setArtifactId( "test-plugin" ); - expectedRecord.setGroupId( TEST_GROUP_ID ); - expectedRecord.setBaseVersion( "1.0" ); - expectedRecord.setVersion( "1.0" ); - expectedRecord.setSha1Checksum( "2cd2619d59a684e82e97471d2c2e004144c8f24e" ); - expectedRecord.setType( "maven-plugin" ); - expectedRecord.setRepository( "test" ); - expectedRecord.setClasses( Arrays.asList( new String[]{"org.apache.maven.archiva.record.MyMojo"} ) ); - expectedRecord.setFiles( Arrays.asList( new String[]{"META-INF/MANIFEST.MF", - "META-INF/maven/org.apache.maven.archiva.record/test-plugin/pom.properties", - "META-INF/maven/org.apache.maven.archiva.record/test-plugin/pom.xml", "META-INF/maven/plugin.xml", - "org/apache/maven/archiva/record/MyMojo.class"} ) ); - expectedRecord.setPackaging( "maven-plugin" ); - expectedRecord.setProjectName( "Maven Mojo Archetype" ); - expectedRecord.setPluginPrefix( "test" ); - - assertEquals( "check record", expectedRecord, record ); - } - - public void testIndexedArchetype() - throws RepositoryIndexException, IOException, XmlPullParserException - { - Artifact artifact = createArtifact( "test-archetype" ); - - RepositoryIndexRecord record = factory.createRecord( artifact ); - - StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord(); - expectedRecord.setMd5Checksum( "52b7ea4b53818b8a5f4c329d88fd60d9" ); - expectedRecord.setFilename( repository.pathOf( artifact ) ); - expectedRecord.setLastModified( artifact.getFile().lastModified() ); - expectedRecord.setSize( artifact.getFile().length() ); - expectedRecord.setArtifactId( "test-archetype" ); - expectedRecord.setGroupId( TEST_GROUP_ID ); - expectedRecord.setBaseVersion( "1.0" ); - expectedRecord.setVersion( "1.0" ); - expectedRecord.setSha1Checksum( "05841f5e51c124f1729d86c1687438c36b9255d9" ); - expectedRecord.setType( "maven-archetype" ); - expectedRecord.setRepository( "test" ); - expectedRecord.setFiles( Arrays.asList( new String[]{"META-INF/MANIFEST.MF", "META-INF/maven/archetype.xml", - "META-INF/maven/org.apache.maven.archiva.record/test-archetype/pom.properties", - "META-INF/maven/org.apache.maven.archiva.record/test-archetype/pom.xml", "archetype-resources/pom.xml", - "archetype-resources/src/main/java/App.java", "archetype-resources/src/test/java/AppTest.java"} ) ); - expectedRecord.setPackaging( "jar" ); - expectedRecord.setProjectName( "Archetype - test-archetype" ); - - assertEquals( "check record", expectedRecord, record ); - } - - public void testCorruptJar() - throws RepositoryIndexException - { - Artifact artifact = createArtifact( "test-corrupt-jar" ); - - RepositoryIndexRecord record = factory.createRecord( artifact ); - - assertNull( "Confirm no record is returned", record ); - } - - public void testDll() - throws RepositoryIndexException - { - Artifact artifact = createArtifact( "test-dll", "1.0.1.34", "dll" ); - - RepositoryIndexRecord record = factory.createRecord( artifact ); - - StandardArtifactIndexRecord expectedRecord = new StandardArtifactIndexRecord(); - expectedRecord.setMd5Checksum( "d41d8cd98f00b204e9800998ecf8427e" ); - expectedRecord.setFilename( repository.pathOf( artifact ) ); - expectedRecord.setLastModified( artifact.getFile().lastModified() ); - expectedRecord.setSize( artifact.getFile().length() ); - expectedRecord.setArtifactId( "test-dll" ); - expectedRecord.setGroupId( TEST_GROUP_ID ); - expectedRecord.setBaseVersion( "1.0.1.34" ); - expectedRecord.setVersion( "1.0.1.34" ); - expectedRecord.setSha1Checksum( "da39a3ee5e6b4b0d3255bfef95601890afd80709" ); - expectedRecord.setType( "dll" ); - expectedRecord.setRepository( "test" ); - - assertEquals( "check record", expectedRecord, record ); - } - - public void testMissingFile() - throws RepositoryIndexException - { - Artifact artifact = createArtifact( "test-foo" ); - - RepositoryIndexRecord record = factory.createRecord( artifact ); - - assertNull( "Confirm no record is returned", record ); - } - - private Artifact createArtifact( String artifactId ) - { - return createArtifact( artifactId, "1.0", "jar" ); - } - - private Artifact createArtifact( String artifactId, String version, String type ) - { - return createArtifact( artifactId, version, type, null ); - } - - private Artifact createArtifact( String artifactId, String version, String type, String classifier ) - { - Artifact artifact = artifactFactory.createDependencyArtifact( TEST_GROUP_ID, artifactId, - VersionRange.createFromVersion( version ), type, - classifier, Artifact.SCOPE_RUNTIME ); - artifact.isSnapshot(); - artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); - artifact.setRepository( repository ); - return artifact; - } -} diff --git a/archiva-reports-standard/src/main/java/org/apache/maven/archiva/reporting/DuplicateArtifactFileReportProcessor.java b/archiva-reports-standard/src/main/java/org/apache/maven/archiva/reporting/DuplicateArtifactFileReportProcessor.java index 89f393d2b..3cf8aba9a 100644 --- a/archiva-reports-standard/src/main/java/org/apache/maven/archiva/reporting/DuplicateArtifactFileReportProcessor.java +++ b/archiva-reports-standard/src/main/java/org/apache/maven/archiva/reporting/DuplicateArtifactFileReportProcessor.java @@ -20,12 +20,12 @@ import org.apache.lucene.index.Term; import org.apache.lucene.search.TermQuery; import org.apache.maven.archiva.digest.Digester; import org.apache.maven.archiva.digest.DigesterException; -import org.apache.maven.archiva.indexing.RepositoryArtifactIndex; -import org.apache.maven.archiva.indexing.RepositoryArtifactIndexFactory; -import org.apache.maven.archiva.indexing.RepositoryIndexSearchException; -import org.apache.maven.archiva.indexing.lucene.LuceneQuery; -import org.apache.maven.archiva.indexing.record.StandardArtifactIndexRecord; -import org.apache.maven.archiva.indexing.record.StandardIndexRecordFields; +import org.apache.maven.archiva.indexer.RepositoryArtifactIndex; +import org.apache.maven.archiva.indexer.RepositoryArtifactIndexFactory; +import org.apache.maven.archiva.indexer.RepositoryIndexSearchException; +import org.apache.maven.archiva.indexer.lucene.LuceneQuery; +import org.apache.maven.archiva.indexer.record.StandardArtifactIndexRecord; +import org.apache.maven.archiva.indexer.record.StandardIndexRecordFields; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.model.Model; diff --git a/archiva-reports-standard/src/test/java/org/apache/maven/archiva/reporting/DuplicateArtifactFileReportProcessorTest.java b/archiva-reports-standard/src/test/java/org/apache/maven/archiva/reporting/DuplicateArtifactFileReportProcessorTest.java index 3491e5cc5..c1223137b 100644 --- a/archiva-reports-standard/src/test/java/org/apache/maven/archiva/reporting/DuplicateArtifactFileReportProcessorTest.java +++ b/archiva-reports-standard/src/test/java/org/apache/maven/archiva/reporting/DuplicateArtifactFileReportProcessorTest.java @@ -16,9 +16,9 @@ package org.apache.maven.archiva.reporting; * limitations under the License. */ -import org.apache.maven.archiva.indexing.RepositoryArtifactIndex; -import org.apache.maven.archiva.indexing.RepositoryArtifactIndexFactory; -import org.apache.maven.archiva.indexing.record.RepositoryIndexRecordFactory; +import org.apache.maven.archiva.indexer.RepositoryArtifactIndex; +import org.apache.maven.archiva.indexer.RepositoryArtifactIndexFactory; +import org.apache.maven.archiva.indexer.record.RepositoryIndexRecordFactory; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.factory.ArtifactFactory; import org.apache.maven.model.Model; diff --git a/archiva-reports-standard/src/test/resources/org/apache/maven/archiva/reporting/DuplicateArtifactFileReportProcessorTest.xml b/archiva-reports-standard/src/test/resources/org/apache/maven/archiva/reporting/DuplicateArtifactFileReportProcessorTest.xml index e1311bb8b..862124666 100644 --- a/archiva-reports-standard/src/test/resources/org/apache/maven/archiva/reporting/DuplicateArtifactFileReportProcessorTest.xml +++ b/archiva-reports-standard/src/test/resources/org/apache/maven/archiva/reporting/DuplicateArtifactFileReportProcessorTest.xml @@ -10,7 +10,7 @@ md5 - org.apache.maven.archiva.indexing.RepositoryArtifactIndexFactory + org.apache.maven.archiva.indexer.RepositoryArtifactIndexFactory diff --git a/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/BrowseAction.java b/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/BrowseAction.java index 29953ce25..45177171f 100644 --- a/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/BrowseAction.java +++ b/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/BrowseAction.java @@ -26,13 +26,13 @@ import org.apache.maven.archiva.configuration.Configuration; import org.apache.maven.archiva.configuration.ConfigurationStore; import org.apache.maven.archiva.configuration.ConfigurationStoreException; import org.apache.maven.archiva.configuration.ConfiguredRepositoryFactory; -import org.apache.maven.archiva.indexing.RepositoryArtifactIndex; -import org.apache.maven.archiva.indexing.RepositoryArtifactIndexFactory; -import org.apache.maven.archiva.indexing.RepositoryIndexException; -import org.apache.maven.archiva.indexing.RepositoryIndexSearchException; -import org.apache.maven.archiva.indexing.lucene.LuceneQuery; -import org.apache.maven.archiva.indexing.record.StandardArtifactIndexRecord; -import org.apache.maven.archiva.indexing.record.StandardIndexRecordFields; +import org.apache.maven.archiva.indexer.RepositoryArtifactIndex; +import org.apache.maven.archiva.indexer.RepositoryArtifactIndexFactory; +import org.apache.maven.archiva.indexer.RepositoryIndexException; +import org.apache.maven.archiva.indexer.RepositoryIndexSearchException; +import org.apache.maven.archiva.indexer.lucene.LuceneQuery; +import org.apache.maven.archiva.indexer.record.StandardArtifactIndexRecord; +import org.apache.maven.archiva.indexer.record.StandardIndexRecordFields; import org.codehaus.plexus.util.StringUtils; import java.io.File; diff --git a/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/SearchAction.java b/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/SearchAction.java index b4d50916f..38b9b248f 100644 --- a/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/SearchAction.java +++ b/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/SearchAction.java @@ -26,12 +26,12 @@ import org.apache.maven.archiva.configuration.Configuration; import org.apache.maven.archiva.configuration.ConfigurationStore; import org.apache.maven.archiva.configuration.ConfigurationStoreException; import org.apache.maven.archiva.configuration.ConfiguredRepositoryFactory; -import org.apache.maven.archiva.indexing.RepositoryArtifactIndex; -import org.apache.maven.archiva.indexing.RepositoryArtifactIndexFactory; -import org.apache.maven.archiva.indexing.RepositoryIndexException; -import org.apache.maven.archiva.indexing.RepositoryIndexSearchException; -import org.apache.maven.archiva.indexing.lucene.LuceneQuery; -import org.apache.maven.archiva.indexing.record.StandardIndexRecordFields; +import org.apache.maven.archiva.indexer.RepositoryArtifactIndex; +import org.apache.maven.archiva.indexer.RepositoryArtifactIndexFactory; +import org.apache.maven.archiva.indexer.RepositoryIndexException; +import org.apache.maven.archiva.indexer.RepositoryIndexSearchException; +import org.apache.maven.archiva.indexer.lucene.LuceneQuery; +import org.apache.maven.archiva.indexer.record.StandardIndexRecordFields; import java.io.File; import java.net.MalformedURLException; diff --git a/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/admin/ConfigureAction.java b/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/admin/ConfigureAction.java index b58aff5b9..1591e58ab 100644 --- a/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/admin/ConfigureAction.java +++ b/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/admin/ConfigureAction.java @@ -24,8 +24,8 @@ import org.apache.maven.archiva.configuration.ConfigurationChangeException; import org.apache.maven.archiva.configuration.ConfigurationStore; import org.apache.maven.archiva.configuration.ConfigurationStoreException; import org.apache.maven.archiva.configuration.InvalidConfigurationException; -import org.apache.maven.archiva.indexing.RepositoryIndexException; -import org.apache.maven.archiva.indexing.RepositoryIndexSearchException; +import org.apache.maven.archiva.indexer.RepositoryIndexException; +import org.apache.maven.archiva.indexer.RepositoryIndexSearchException; import java.io.File; import java.io.IOException;