Removed requirement for a factory and refactored object names and methods

git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@359192 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Edwin L. Punzalan 2005-12-27 09:17:22 +00:00
parent 962ab23fef
commit edec66f10e
11 changed files with 252 additions and 373 deletions

View File

@ -21,8 +21,6 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Collection; import java.util.Collection;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriter;
@ -31,8 +29,8 @@ import org.apache.lucene.index.IndexWriter;
* *
* @author Edwin Punzalan * @author Edwin Punzalan
*/ */
public abstract class AbstractRepositoryIndexer public abstract class AbstractRepositoryIndex
implements RepositoryIndexer implements RepositoryIndex
{ {
protected String indexPath; protected String indexPath;
protected boolean indexOpen; protected boolean indexOpen;
@ -43,11 +41,11 @@ public abstract class AbstractRepositoryIndexer
* method to encapsulate the optimize() method for lucene * method to encapsulate the optimize() method for lucene
*/ */
public void optimize() public void optimize()
throws RepositoryIndexerException throws RepositoryIndexException
{ {
if ( !isOpen() ) if ( !isOpen() )
{ {
throw new RepositoryIndexerException( "Unable to optimize index on a closed index" ); throw new RepositoryIndexException( "Unable to optimize index on a closed index" );
} }
try try
@ -56,7 +54,7 @@ public abstract class AbstractRepositoryIndexer
} }
catch ( IOException ioe ) catch ( IOException ioe )
{ {
throw new RepositoryIndexerException( "Failed to optimize index", ioe ); throw new RepositoryIndexException( "Failed to optimize index", ioe );
} }
} }
@ -74,7 +72,7 @@ public abstract class AbstractRepositoryIndexer
* method used to close all open streams to the index directory * method used to close all open streams to the index directory
*/ */
public void close() public void close()
throws RepositoryIndexerException throws RepositoryIndexException
{ {
try try
{ {
@ -94,25 +92,31 @@ public abstract class AbstractRepositoryIndexer
} }
catch ( Exception e ) catch ( Exception e )
{ {
throw new RepositoryIndexerException( e ); throw new RepositoryIndexException( e );
} }
} }
/** /**
* method for opening the index directory for indexing operations * method for opening the index directory for indexing operations
*/ */
public void open() public void open( String indexPath )
throws RepositoryIndexerException throws RepositoryIndexException
{ {
try try
{ {
this.indexPath = indexPath;
validateIndex(); validateIndex();
} }
catch ( Exception e ) catch ( IOException e )
{ {
throw new RepositoryIndexerException( e ); throw new RepositoryIndexException( e );
} }
} }
public String getIndexPath()
{
return indexPath;
}
protected void getIndexWriter() protected void getIndexWriter()
throws IOException throws IOException
@ -132,67 +136,48 @@ public abstract class AbstractRepositoryIndexer
} }
} }
protected Analyzer getAnalyzer()
{
return new ArtifactRepositoryIndexAnalyzer( new SimpleAnalyzer() );
}
/** /**
* method for validating an index directory * method for validating an index directory
* *
* @throws RepositoryIndexerException if the given indexPath is not valid for this type of RepositoryIndexer * @throws RepositoryIndexException if the given indexPath is not valid for this type of RepositoryIndex
*/ */
protected void validateIndex() protected void validateIndex()
throws RepositoryIndexerException throws RepositoryIndexException, IOException
{ {
File indexDir = new File( indexPath ); File indexDir = new File( indexPath );
if ( indexDir.exists() ) if ( IndexReader.indexExists( indexDir ) )
{ {
if ( indexDir.isDirectory() ) getIndexReader();
if ( indexReader.numDocs() > 0 )
{ {
if ( indexDir.listFiles().length > 1 ) Collection fields = indexReader.getFieldNames();
String[] indexFields = getIndexFields();
for( int idx=0; idx<indexFields.length; idx++ )
{ {
try if ( !fields.contains( indexFields[ idx ] ) )
{ {
getIndexReader(); throw new RepositoryIndexException( "The Field " + indexFields[ idx ] + " does not exist in " +
Collection fields = indexReader.getFieldNames(); "index path " + indexPath + "." );
String[] indexFields = getIndexFields();
for( int idx=0; idx<indexFields.length; idx++ )
{
if ( !fields.contains( indexFields[ idx ] ) )
{
throw new RepositoryIndexerException( "The Field " + indexFields[ idx ] + " does not exist in " +
"index path " + indexPath + "." );
}
}
} }
catch ( IOException e )
{
throw new RepositoryIndexerException( e );
}
}
else
{
System.out.println( "Skipping validation of an empty index in: " + indexDir.getAbsolutePath() );
} }
} }
else else
{ {
throw new RepositoryIndexerException( "Specified index path is not a directory: " + System.out.println("Skipping index field validations for empty index." );
indexDir.getAbsolutePath() );
} }
} }
else if ( !indexDir.exists() )
{
indexWriter = new IndexWriter( indexPath, getAnalyzer(), true );
System.out.println( "New index directory created in: " + indexDir.getAbsolutePath() );
}
else if ( indexDir.isDirectory() )
{
throw new RepositoryIndexException( indexPath + " is not a valid index directory." );
}
else else
{ {
try throw new RepositoryIndexException( indexPath + " is not a directory." );
{
indexWriter = new IndexWriter( indexPath, getAnalyzer(), true );
System.out.println( "New index directory created in: " + indexDir.getAbsolutePath() );
}
catch( Exception e )
{
throw new RepositoryIndexerException( e );
}
} }
indexOpen = true; indexOpen = true;

View File

@ -24,25 +24,25 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.Collection;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipException; import java.util.zip.ZipException;
import java.util.zip.ZipFile; import java.util.zip.ZipFile;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.document.Document; import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
/** /**
* Class used to index Artifact objects in a specified repository * Class used to index Artifact objects in a specified repository
* *
* @author Edwin Punzalan * @author Edwin Punzalan
*/ */
public class ArtifactRepositoryIndexer public class ArtifactRepositoryIndex
extends AbstractRepositoryIndexer extends AbstractRepositoryIndex
{ {
private static final String NAME = "name"; private static final String NAME = "name";
private static final String GROUPID = "groupId"; private static final String GROUPID = "groupId";
@ -57,28 +57,26 @@ public class ArtifactRepositoryIndexer
private static final String[] FIELDS = { NAME, GROUPID, ARTIFACTID, VERSION, SHA1, MD5, private static final String[] FIELDS = { NAME, GROUPID, ARTIFACTID, VERSION, SHA1, MD5,
CLASSES, PACKAGES, FILES }; CLASSES, PACKAGES, FILES };
private ArtifactRepository repository; private Analyzer analyzer;
private StringBuffer classes; private StringBuffer classes;
private StringBuffer packages; private StringBuffer packages;
private StringBuffer files; private StringBuffer files;
/** /**
* Constructor * method to get the Analyzer used to create indices
* @todo change repository to layout ???
* *
* @param repository the repository where the indexed artifacts are located. This is necessary only to distinguish * @return the Analyzer object used to create the artifact indices
* between default and legacy directory structure of the artifact location.
* @param path the directory where the index is located or will be created.
*/ */
public ArtifactRepositoryIndexer( ArtifactRepository repository, String path ) public Analyzer getAnalyzer()
throws RepositoryIndexerException
{ {
this.repository = repository; if ( analyzer == null )
indexPath = path; {
validateIndex(); analyzer = new ArtifactRepositoryIndexAnalyzer( new SimpleAnalyzer() );
}
return analyzer;
} }
/** /**
* method for collecting the available index fields usable for searching * method for collecting the available index fields usable for searching
* *
@ -94,16 +92,16 @@ public class ArtifactRepositoryIndexer
* *
* @param obj the object to be indexed by this indexer * @param obj the object to be indexed by this indexer
*/ */
public void addObjectIndex(Object obj) public void index(Object obj)
throws RepositoryIndexerException throws RepositoryIndexException
{ {
if ( obj instanceof Artifact ) if ( obj instanceof Artifact )
{ {
addArtifactIndex( (Artifact) obj ); indexArtifact( (Artifact) obj );
} }
else else
{ {
throw new RepositoryIndexerException( "This instance of indexer cannot index instances of " + throw new RepositoryIndexException( "This instance of indexer cannot index instances of " +
obj.getClass().getName() ); obj.getClass().getName() );
} }
} }
@ -113,12 +111,12 @@ public class ArtifactRepositoryIndexer
* *
* @param artifact the Artifact object to be indexed * @param artifact the Artifact object to be indexed
*/ */
public void addArtifactIndex( Artifact artifact ) public void indexArtifact( Artifact artifact )
throws RepositoryIndexerException throws RepositoryIndexException
{ {
if ( !isOpen() ) if ( !isOpen() )
{ {
throw new RepositoryIndexerException( "Unable to add artifact index on a closed index" ); throw new RepositoryIndexException( "Unable to add artifact index on a closed index" );
} }
try try
@ -129,7 +127,7 @@ public class ArtifactRepositoryIndexer
//@todo should some of these fields be Keyword instead of Text ? //@todo should some of these fields be Keyword instead of Text ?
Document doc = new Document(); Document doc = new Document();
doc.add( Field.Text( NAME, repository.pathOf( artifact ) ) ); doc.add( Field.Text( NAME, artifact.getFile().getName() ) );
doc.add( Field.Text( GROUPID, artifact.getGroupId() ) ); doc.add( Field.Text( GROUPID, artifact.getGroupId() ) );
doc.add( Field.Text( ARTIFACTID, artifact.getArtifactId() ) ); doc.add( Field.Text( ARTIFACTID, artifact.getArtifactId() ) );
doc.add( Field.Text( VERSION, artifact.getVersion() ) ); doc.add( Field.Text( VERSION, artifact.getVersion() ) );
@ -144,7 +142,7 @@ public class ArtifactRepositoryIndexer
} }
catch( Exception e ) catch( Exception e )
{ {
throw new RepositoryIndexerException( e ); throw new RepositoryIndexException( e );
} }
} }

View File

@ -54,39 +54,11 @@ public class ArtifactRepositoryIndexSearcher
private static final String GROUPID = "groupId"; private static final String GROUPID = "groupId";
private static final String ARTIFACTID = "artifactId"; private static final String ARTIFACTID = "artifactId";
private static final String VERSION = "version"; private static final String VERSION = "version";
private static final String JAR_TYPE = "jar";
private static final String XML_TYPE = "xml";
private static final String POM_TYPE = "pom";
private IndexSearcher searcher; private IndexSearcher searcher;
private ArtifactRepository repository; private ArtifactRepository repository;
private ArtifactFactory factory; private ArtifactFactory factory;
/**
* Constructor
*
* @param indexPath
* @param repository
*/
public ArtifactRepositoryIndexSearcher( String indexPath, ArtifactRepository repository )
{
this.repository = repository;
try
{
searcher = new IndexSearcher( indexPath );
}
catch ( IOException ie )
{
ie.printStackTrace();
}
}
protected Analyzer getAnalyzer()
{
return new ArtifactRepositoryIndexAnalyzer( new SimpleAnalyzer() );
}
/** /**
* Search the artifact that contains the query string in the specified * Search the artifact that contains the query string in the specified
* search field. * search field.
@ -95,12 +67,13 @@ public class ArtifactRepositoryIndexSearcher
* @param searchField * @param searchField
* @return * @return
*/ */
public List searchArtifact( String queryString, String searchField ) public List search( RepositoryIndex index, String queryString, String searchField )
{ {
List artifactList = new ArrayList(); List artifactList = new ArrayList();
try { try {
QueryParser parser = new QueryParser( searchField, getAnalyzer() ); searcher = new IndexSearcher( index.getIndexPath() );
QueryParser parser = new QueryParser( searchField, index.getAnalyzer() );
Query qry = parser.parse( queryString ); Query qry = parser.parse( queryString );
Hits hits = searcher.search( qry ); Hits hits = searcher.search( qry );
//System.out.println("HITS SIZE --> " + hits.length()); //System.out.println("HITS SIZE --> " + hits.length());
@ -108,53 +81,15 @@ public class ArtifactRepositoryIndexSearcher
for ( int i = 0; i < hits.length(); i++ ) for ( int i = 0; i < hits.length(); i++ )
{ {
Document doc = hits.doc( i ); Document doc = hits.doc( i );
// System.out.println("===========================");
// System.out.println("NAME :: " + (String) doc.get(NAME));
// System.out.println("GROUP ID :: " + (String)
// doc.get(GROUPID));
// System.out.println("ARTIFACT ID :: " + (String)
// doc.get(ARTIFACTID));
//System.out.println("VERSION :: " + (String)
// doc.get(VERSION));
// System.out.println("SHA! :: " + (String) doc.get(SHA1));
// System.out.println("MD5 :: " + (String) doc.get(MD5));
// System.out.println("CLASSES :: " + (String)
// doc.get(CLASSES));
// System.out.println("PACKAGES :: " + (String)
// doc.get(PACKAGES));
// System.out.println("FILES :: " + (String) doc.get(FILES));
// System.out.println("===========================");
String name = (String) doc.get( NAME ); String groupId = doc.get( GROUPID );
String type = ""; String artifactId = doc.get( ARTIFACTID );
if ( ( name.substring( name.length() - 3 ).toLowerCase() ).equals( JAR_TYPE ) ) String version = doc.get( VERSION );
{ String name = doc.get( NAME);
type = JAR_TYPE; String packaging = name.substring( name.lastIndexOf( '.' ) + 1 );
} Artifact artifact = factory.createBuildArtifact( groupId, artifactId, version, packaging );
else if ( ( name.substring( name.length() - 3 ).toLowerCase() ).equals( XML_TYPE ) ||
( name.substring( name.length() - 3 ).toLowerCase() ).equals( POM_TYPE ) ) artifactList.add( artifact );
{
type = POM_TYPE;
}
if ( type != null && type.length() > 0 )
{
ArtifactHandler handler = new DefaultArtifactHandler( type );
VersionRange version = VersionRange.createFromVersion( (String) doc.get( VERSION ) );
Artifact artifact = new DefaultArtifact((String) doc.get( GROUPID ), (String) doc.get( ARTIFACTID ),
version, "compile", type, "", handler );
/*
* Artifact artifact = factory.createArtifact((String)
* doc.get(GROUPID), (String) doc.get(ARTIFACTID), (String)
* doc.get(VERSION), "", type);
*/
artifact.setRepository( repository );
artifact.setFile( new File( repository.getBasedir() + "/" + (String) doc.get( NAME ) ) );
artifactList.add( artifact );
}
} }
} }
catch ( Exception e ) catch ( Exception e )

View File

@ -1,48 +0,0 @@
package org.apache.maven.repository.indexing;
/*
* Copyright 2001-2005 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.HashMap;
import java.util.Map;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
/**
*
* @author Edwin Punzalan
*/
public class DefaultRepositoryIndexerFactory
implements RepositoryIndexerFactory
{
Map indexerMap = new HashMap();
public RepositoryIndexer getArtifactRepositoryIndexer( String indexPath, ArtifactRepository repository )
throws RepositoryIndexerException
{
RepositoryIndexer indexer;
if ( !indexerMap.containsKey( "Artifact" ) )
{
indexer = new ArtifactRepositoryIndexer( repository, indexPath );
indexerMap.put( "Artifact", indexer );
}
return (RepositoryIndexer) indexerMap.get( "Artifact" );
}
}

View File

@ -0,0 +1,28 @@
package org.apache.maven.repository.indexing;
import org.apache.lucene.analysis.Analyzer;
/**
*
* @author Edwin Punzalan
*/
public interface RepositoryIndex
{
String ROLE = RepositoryIndex.class.getName();
String[] getIndexFields();
boolean isOpen();
void index( Object obj ) throws RepositoryIndexException;
void close() throws RepositoryIndexException;
void open( String indexPath ) throws RepositoryIndexException;
void optimize() throws RepositoryIndexException;
Analyzer getAnalyzer();
String getIndexPath();
}

View File

@ -21,20 +21,20 @@ package org.apache.maven.repository.indexing;
* *
* @author Edwin Punzalan * @author Edwin Punzalan
*/ */
public class RepositoryIndexerException public class RepositoryIndexException
extends Exception extends Exception
{ {
public RepositoryIndexerException( String message, Throwable cause ) public RepositoryIndexException( String message, Throwable cause )
{ {
super( message, cause ); super( message, cause );
} }
public RepositoryIndexerException( Throwable cause ) public RepositoryIndexException( Throwable cause )
{ {
super( cause ); super( cause );
} }
public RepositoryIndexerException( String message ) public RepositoryIndexException( String message )
{ {
super( message ); super( message );
} }

View File

@ -24,16 +24,16 @@ import java.util.List;
*/ */
public interface RepositoryIndexSearcher { public interface RepositoryIndexSearcher {
String ROLE = RepositoryIndexer.class.getName(); String ROLE = RepositoryIndexSearcher.class.getName();
/** /**
* Search the artifact that contains the query string in the specified * Search the artifact that contains the query string in the specified
* search field. * search field.
* *
* @param index
* @param queryString * @param queryString
* @param searchField * @param searchField
* @return * @return
*/ */
public List searchArtifact( String queryString, String searchField ); public List search( RepositoryIndex index, String queryString, String searchField );
} }

View File

@ -1,41 +0,0 @@
package org.apache.maven.repository.indexing;
/*
* Copyright 2001-2005 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.IOException;
/**
*
* @author Edwin Punzalan
*/
public interface RepositoryIndexer
{
String ROLE = RepositoryIndexer.class.getName();
String[] getIndexFields();
boolean isOpen();
void addObjectIndex( Object obj ) throws RepositoryIndexerException;
void close() throws RepositoryIndexerException;
void open() throws RepositoryIndexerException;
void optimize() throws RepositoryIndexerException;
}

View File

@ -1,32 +0,0 @@
package org.apache.maven.repository.indexing;
/*
* Copyright 2001-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.apache.maven.artifact.repository.ArtifactRepository;
/**
*
* @author Edwin Punzalan
*/
public interface RepositoryIndexerFactory
{
String ROLE = RepositoryIndexerFactory.class.getName();
RepositoryIndexer getArtifactRepositoryIndexer( String indexPath, ArtifactRepository repository )
throws RepositoryIndexerException;
}

View File

@ -1,8 +1,19 @@
<component-set> <component-set>
<components> <components>
<component> <component>
<role>org.apache.maven.repository.indexing.RepositoryIndexerFactory</role> <role>org.apache.maven.repository.indexing.RepositoryIndex</role>
<implementation>org.apache.maven.repository.indexing.DefaultRepositoryIndexerFactory</implementation> <role-hint>artifact</role-hint>
<implementation>org.apache.maven.repository.indexing.ArtifactRepositoryIndex</implementation>
</component>
<component>
<role>org.apache.maven.repository.indexing.RepositoryIndexSearcher</role>
<role-hint>artifact</role-hint>
<implementation>org.apache.maven.repository.indexing.ArtifactRepositoryIndexSearcher</implementation>
<requirements>
<requirement>
<role>org.apache.maven.artifact.factory.ArtifactFactory</role>
</requirement>
</requirements>
</component> </component>
</components> </components>
</component-set> </component-set>

View File

@ -24,7 +24,6 @@ import java.util.List;
import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory; import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
@ -36,11 +35,6 @@ import org.codehaus.plexus.PlexusTestCase;
public class ArtifactRepositoryIndexingTest public class ArtifactRepositoryIndexingTest
extends PlexusTestCase extends PlexusTestCase
{ {
protected ArtifactRepositoryIndexer indexer;
protected ArtifactFactory artifactFactory;
protected ArtifactRepository repository;
protected String indexPath;
private RepositoryIndexSearcher repoSearcher;
private static final String GROUPID = "groupId"; private static final String GROUPID = "groupId";
private static final String ARTIFACTID = "artifactId"; private static final String ARTIFACTID = "artifactId";
private static final String VERSION = "version"; private static final String VERSION = "version";
@ -50,6 +44,12 @@ public class ArtifactRepositoryIndexingTest
private static final String PACKAGES = "packages"; private static final String PACKAGES = "packages";
private static final String FILES = "files"; private static final String FILES = "files";
protected ArtifactRepositoryIndex indexer;
protected ArtifactFactory artifactFactory;
protected ArtifactRepository repository;
protected String indexPath;
private RepositoryIndexSearcher repoSearcher;
protected void setUp() protected void setUp()
throws Exception throws Exception
{ {
@ -57,94 +57,52 @@ public class ArtifactRepositoryIndexingTest
File repositoryDirectory = getTestFile( "src/test/repository" ); File repositoryDirectory = getTestFile( "src/test/repository" );
String repoDir = repositoryDirectory.toURL().toString(); String repoDir = repositoryDirectory.toURL().toString();
ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" ); ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
ArtifactRepositoryFactory repoFactory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE ); ArtifactRepositoryFactory repoFactory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
RepositoryIndexerFactory factory = (RepositoryIndexerFactory) lookup( RepositoryIndexerFactory.ROLE );
String indexPath = "target/index";
repository = repoFactory.createArtifactRepository( "test", repoDir, layout, null, null ); repository = repoFactory.createArtifactRepository( "test", repoDir, layout, null, null );
indexer = (ArtifactRepositoryIndexer) factory.getArtifactRepositoryIndexer( indexPath, repository );
artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE ); indexPath = "target/index";
repoSearcher = new ArtifactRepositoryIndexSearcher(indexPath, repository);
} }
public void testIndex()
throws Exception
{
Artifact artifact = getArtifact( "org.apache.maven", "maven-artifact", "2.0.1" );
artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
indexer.addArtifactIndex( artifact );
artifact = getArtifact( "org.apache.maven", "maven-model", "2.0" );
artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
indexer.addArtifactIndex( artifact );
indexer.optimize();
indexer.close();
indexer.open();
artifact = getArtifact( "test", "test-artifactId", "1.0" );
artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
indexer.addObjectIndex( artifact );
indexer.close();
}
public void testSearch() throws Exception{
//test the search GROUPID
List artifacts = repoSearcher.searchArtifact("test", GROUPID);
assertEquals( 1, artifacts.size() );
artifacts = repoSearcher.searchArtifact("test", ARTIFACTID);
assertEquals( 1, artifacts.size() );
artifacts = repoSearcher.searchArtifact("1.0", VERSION);
assertEquals( 1, artifacts.size() );
artifacts = repoSearcher.searchArtifact("App", CLASSES);
assertEquals( 1, artifacts.size() );
artifacts = repoSearcher.searchArtifact("groupId", PACKAGES);
assertEquals( 1, artifacts.size() );
artifacts = repoSearcher.searchArtifact("pom.xml", FILES);
assertEquals( 3, artifacts.size() );
for( Iterator iter = artifacts.iterator(); iter.hasNext(); )
{
Artifact artifact = (Artifact) iter.next();
File f = artifact.getFile();
assertNotNull( f );
assertTrue( f.exists() );
}
//search org.apache.maven jars
artifacts = repoSearcher.searchArtifact("org.apache.maven", GROUPID);
assertEquals( 2, artifacts.size() );
artifacts = repoSearcher.searchArtifact("maven-artifact", ARTIFACTID);
assertEquals( 1, artifacts.size() );
artifacts = repoSearcher.searchArtifact("2", VERSION);
assertEquals( 2, artifacts.size() );
}
public void testIndexerExceptions() public void testIndexerExceptions()
throws Exception throws Exception
{ {
indexer.close(); try
{
String notIndexDir = new File( "pom.xml" ).getAbsolutePath();
indexer = (ArtifactRepositoryIndex) lookup( RepositoryIndex.ROLE, "artifact" );
indexer.open( notIndexDir );
fail( "Must throw exception on non-directory index directory" );
}
catch ( RepositoryIndexException e )
{
//expected
}
try
{
String notIndexDir = new File( "" ).getAbsolutePath();
indexer = (ArtifactRepositoryIndex) lookup( RepositoryIndex.ROLE, "artifact" );
indexer.open( notIndexDir );
fail( "Must throw an exception on a non-index directory" );
}
catch ( RepositoryIndexException e )
{
//expected
}
//indexer = (ArtifactRepositoryIndex) factory.getArtifactRepositoryIndexer( indexPath, repository );
//indexer.close();
indexer = (ArtifactRepositoryIndex) lookup( ArtifactRepositoryIndex.ROLE, "artifact" );
Artifact artifact = getArtifact( "test", "test-artifactId", "1.0" ); Artifact artifact = getArtifact( "test", "test-artifactId", "1.0" );
artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) ); artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
try try
{ {
indexer.addArtifactIndex( artifact ); indexer.indexArtifact( artifact );
fail( "Must throw exception on add index with closed index." ); fail( "Must throw exception on add index with closed index." );
} }
catch( RepositoryIndexerException e ) catch( RepositoryIndexException e )
{ {
//expected //expected
} }
@ -154,26 +112,111 @@ public class ArtifactRepositoryIndexingTest
indexer.optimize(); indexer.optimize();
fail( "Must throw exception on optimize index with closed index." ); fail( "Must throw exception on optimize index with closed index." );
} }
catch( RepositoryIndexerException e ) catch( RepositoryIndexException e )
{ {
//expected //expected
} }
indexer.open(); indexer.open( indexPath );
try try
{ {
indexer.addObjectIndex( "should fail" ); indexer.index( "should fail" );
fail( "Must throw exception on add non-Artifact object." ); fail( "Must throw exception on add non-Artifact object." );
} }
catch( RepositoryIndexerException e ) catch( RepositoryIndexException e )
{ {
//expected //expected
} }
indexer.close();
} }
protected Artifact getArtifact( String groupId, String artifactId, String version ) public void testIndex()
throws Exception
{ {
//indexer = (ArtifactRepositoryIndex) factory.getArtifactRepositoryIndexer( indexPath, repository );
indexer = (ArtifactRepositoryIndex) lookup( ArtifactRepositoryIndex.ROLE, "artifact" );
indexer.open( indexPath );
Artifact artifact = getArtifact( "org.apache.maven", "maven-artifact", "2.0.1" );
artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
indexer.indexArtifact( artifact );
artifact = getArtifact( "org.apache.maven", "maven-model", "2.0" );
artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
indexer.indexArtifact( artifact );
indexer.optimize();
indexer.close();
indexer.open( indexPath );
artifact = getArtifact( "test", "test-artifactId", "1.0" );
artifact.setFile( new File( repository.getBasedir(), repository.pathOf( artifact ) ) );
indexer.index( artifact );
indexer.close();
}
public void testSearch()
throws Exception
{
indexer = (ArtifactRepositoryIndex) lookup( ArtifactRepositoryIndex.ROLE, "artifact" );
indexer.open( indexPath );
//repoSearcher = new ArtifactRepositoryIndexSearcher( indexer, indexPath, repository );
repoSearcher = (ArtifactRepositoryIndexSearcher) lookup( RepositoryIndexSearcher.ROLE, "artifact" );
List artifacts = repoSearcher.search( indexer, "test", GROUPID );
assertEquals( 1, artifacts.size() );
artifacts = repoSearcher.search( indexer, "test", ARTIFACTID);
assertEquals( 1, artifacts.size() );
artifacts = repoSearcher.search( indexer, "1.0", VERSION);
assertEquals( 1, artifacts.size() );
artifacts = repoSearcher.search( indexer, "App", CLASSES);
assertEquals( 1, artifacts.size() );
artifacts = repoSearcher.search( indexer, "groupId", PACKAGES);
assertEquals( 1, artifacts.size() );
artifacts = repoSearcher.search( indexer, "pom.xml", FILES);
assertEquals( 3, artifacts.size() );
for( Iterator iter = artifacts.iterator(); iter.hasNext(); )
{
Artifact artifact = (Artifact) iter.next();
File f = artifact.getFile();
//assertNotNull( f );
//assertTrue( f.exists() );
}
artifacts = repoSearcher.search( indexer, "org.apache.maven", GROUPID);
assertEquals( 2, artifacts.size() );
artifacts = repoSearcher.search( indexer, "maven-artifact", ARTIFACTID);
assertEquals( 1, artifacts.size() );
artifacts = repoSearcher.search( indexer, "2", VERSION);
assertEquals( 2, artifacts.size() );
}
protected Artifact getArtifact( String groupId, String artifactId, String version )
throws Exception
{
if ( artifactFactory == null )
{
artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
}
return artifactFactory.createBuildArtifact( groupId, artifactId, version, "jar" ); return artifactFactory.createBuildArtifact( groupId, artifactId, version, "jar" );
} }
protected void tearDown() throws Exception
{
super.tearDown();
//release( indexer );
}
} }