import org.apache.archiva.indexer.IndexCreationFailedException;
import org.apache.archiva.indexer.IndexUpdateFailedException;
import org.apache.archiva.indexer.UnsupportedBaseContextException;
-import org.apache.archiva.model.ArtifactReference;
import org.apache.archiva.proxy.common.WagonFactory;
import org.apache.archiva.proxy.common.WagonFactoryException;
import org.apache.archiva.proxy.common.WagonFactoryRequest;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
-import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
private static final int MAX_WAIT = 10;
- IndexingContext getMvnContext( ArchivaIndexingContext context ) throws UnsupportedBaseContextException
+ public static IndexingContext getMvnContext( ArchivaIndexingContext context ) throws UnsupportedBaseContextException
{
if ( !context.supports( IndexingContext.class ) )
{
}
@Override
- public void scan( final ArchivaIndexingContext context, final boolean update ) throws IndexUpdateFailedException
+ public void scan(final ArchivaIndexingContext context) throws IndexUpdateFailedException
{
executeUpdateFunction( context, indexingContext -> {
- DefaultScannerListener listener = new DefaultScannerListener( indexingContext, indexerEngine, update, null );
+ DefaultScannerListener listener = new DefaultScannerListener( indexingContext, indexerEngine, true, null );
ScanningRequest request = new ScanningRequest( indexingContext, listener );
ScanningResult result = scanner.scan( request );
if ( result.hasExceptions( ) )
}
@Override
- public void addArtifactsToIndex( final ArchivaIndexingContext context, final Collection<Path> artifactReference ) throws IndexUpdateFailedException
+ public void addArtifactsToIndex( final ArchivaIndexingContext context, final Collection<URI> artifactReference ) throws IndexUpdateFailedException
{
+ final URI ctxUri = context.getPath();
executeUpdateFunction(context, indexingContext -> {
- Collection<ArtifactContext> artifacts = artifactReference.stream().map(r -> artifactContextProducer.getArtifactContext(indexingContext, r.toFile())).collect(Collectors.toList());
+ Collection<ArtifactContext> artifacts = artifactReference.stream().map(r -> artifactContextProducer.getArtifactContext(indexingContext, Paths.get(ctxUri.resolve(r)).toFile())).collect(Collectors.toList());
try {
indexer.addArtifactsToIndex(artifacts, indexingContext);
} catch (IOException e) {
}
@Override
- public void removeArtifactsFromIndex( ArchivaIndexingContext context, Collection<Path> artifactReference ) throws IndexUpdateFailedException
+ public void removeArtifactsFromIndex( ArchivaIndexingContext context, Collection<URI> artifactReference ) throws IndexUpdateFailedException
{
+ final URI ctxUri = context.getPath();
executeUpdateFunction(context, indexingContext -> {
- Collection<ArtifactContext> artifacts = artifactReference.stream().map(r -> artifactContextProducer.getArtifactContext(indexingContext, r.toFile())).collect(Collectors.toList());
+ Collection<ArtifactContext> artifacts = artifactReference.stream().map(r -> artifactContextProducer.getArtifactContext(indexingContext, Paths.get(ctxUri.resolve(r)).toFile())).collect(Collectors.toList());
try {
indexer.deleteArtifactsFromIndex(artifacts, indexingContext);
} catch (IOException e) {
* under the License.
*/
+import org.apache.archiva.common.utils.FileUtils;
import org.apache.archiva.indexer.ArchivaIndexingContext;
-import org.apache.archiva.repository.ManagedRepository;
+import org.apache.archiva.indexer.IndexCreationFailedException;
+import org.apache.archiva.repository.RepositoryType;
import org.apache.archiva.repository.features.IndexCreationFeature;
import org.apache.archiva.repository.maven2.MavenManagedRepository;
import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
+import org.apache.maven.index.MAVEN;
+import org.apache.maven.index.QueryCreator;
+import org.apache.maven.index.context.IndexingContext;
+import org.apache.maven.index.expr.UserInputSearchExpression;
+import org.apache.maven.index_shaded.lucene.search.BooleanClause;
+import org.apache.maven.index_shaded.lucene.search.BooleanQuery;
+import org.apache.maven.index_shaded.lucene.search.Query;
+import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import javax.inject.Inject;
+import java.io.IOException;
import java.net.URI;
+import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
import static org.junit.Assert.*;
@ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
public class MavenIndexManagerTest {
+ private Path indexPath;
+ private MavenManagedRepository repository;
+ private ArchivaIndexingContext ctx;
+
@Inject
MavenIndexManager mavenIndexManager;
+ @Inject
+ QueryCreator queryCreator;
+
+
+ @After
+ public void tearDown() {
+ if (ctx!=null) {
+ try {
+ ctx.close(true);
+ } catch (IOException e) {
+ //
+ }
+ }
+ if (indexPath!=null && Files.exists(indexPath)) {
+ FileUtils.deleteQuietly(indexPath);
+ }
+ }
@Test
public void pack() throws Exception {
@Test
public void scan() throws Exception {
+ createTestContext();
+ Path destDir = repository.getLocalPath().resolve("org/apache/archiva/archiva-webapp/1.0");
+ Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-webapp/1.0");
+ org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(),destDir.toFile());
+ mavenIndexManager.scan(ctx);
+
+ IndexingContext mvnCtx = mavenIndexManager.getMvnContext(ctx);
+ String term = "org.apache.archiva";
+ Query q = new BooleanQuery.Builder().add( queryCreator.constructQuery( MAVEN.GROUP_ID, new UserInputSearchExpression( term ) ),
+ BooleanClause.Occur.SHOULD ).build();
+ assertEquals(4, mvnCtx.acquireIndexSearcher().count(q));
}
@Test
@Test
public void addArtifactsToIndex() throws Exception {
+
+ ArchivaIndexingContext ctx = createTestContext();
+ Path destDir = repository.getLocalPath().resolve("org/apache/archiva/archiva-search/1.0");
+ Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-search/1.0");
+ org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(), destDir.toFile());
+ List<URI> uriList = new ArrayList<>();
+ uriList.add(destDir.resolve("archiva-search-1.0.jar").toUri());
+ uriList.add(destDir.resolve("archiva-search-1.0-sources.jar").toUri());
+ mavenIndexManager.addArtifactsToIndex(ctx, uriList);
+
+ IndexingContext mvnCtx = mavenIndexManager.getMvnContext(ctx);
+ String term = "org.apache.archiva";
+ Query q = new BooleanQuery.Builder().add( queryCreator.constructQuery( MAVEN.GROUP_ID, new UserInputSearchExpression( term ) ),
+ BooleanClause.Occur.SHOULD ).build();
+ assertEquals(2, mvnCtx.acquireIndexSearcher().count(q));
}
@Test
public void removeArtifactsFromIndex() throws Exception {
+ ArchivaIndexingContext ctx = createTestContext();
+ Path destDir = repository.getLocalPath().resolve("org/apache/archiva/archiva-search/1.0");
+ Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-search/1.0");
+ org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(), destDir.toFile());
+ List<URI> uriList = new ArrayList<>();
+ uriList.add(destDir.resolve("archiva-search-1.0.jar").toUri());
+ uriList.add(destDir.resolve("archiva-search-1.0-sources.jar").toUri());
+ mavenIndexManager.addArtifactsToIndex(ctx, uriList);
+
+ IndexingContext mvnCtx = mavenIndexManager.getMvnContext(ctx);
+ String term = "org.apache.archiva";
+ Query q = new BooleanQuery.Builder().add( queryCreator.constructQuery( MAVEN.GROUP_ID, new UserInputSearchExpression( term ) ),
+ BooleanClause.Occur.SHOULD ).build();
+ assertEquals(2, mvnCtx.acquireIndexSearcher().count(q));
+ uriList.remove(0);
+ mavenIndexManager.removeArtifactsFromIndex(ctx, uriList);
+ assertEquals(1, mvnCtx.acquireIndexSearcher().count(q));
}
@Test
public void supportsRepository() throws Exception {
+ assertTrue(mavenIndexManager.supportsRepository(RepositoryType.MAVEN));
+ assertFalse(mavenIndexManager.supportsRepository(RepositoryType.NPM));
}
- @Test
- public void createContext() throws Exception {
- MavenManagedRepository repository = new MavenManagedRepository("test-repo", "Test Repo", Paths.get("target/repositories"));
+ private ArchivaIndexingContext createTestContext() throws URISyntaxException, IndexCreationFailedException, IOException {
+ indexPath = Paths.get("target/repositories/test-repo/.index-test");
+ FileUtils.deleteDirectory(indexPath);
+ repository = new MavenManagedRepository("test-repo", "Test Repo", Paths.get("target/repositories"));
repository.setLocation(new URI("test-repo"));
IndexCreationFeature icf = repository.getFeature(IndexCreationFeature.class).get();
icf.setIndexPath(new URI(".index-test"));
- ArchivaIndexingContext ctx = mavenIndexManager.createContext(repository);
+ ctx = mavenIndexManager.createContext(repository);
+ return ctx;
+ }
+
+ @Test
+ public void createContext() throws Exception {
+ ArchivaIndexingContext ctx = createTestContext();
assertNotNull(ctx);
assertEquals(repository, ctx.getRepository());
assertEquals("test-repo", ctx.getId());
- Path indexPath = Paths.get("target/repositories/test-repo/.index-test");
assertEquals(indexPath.toAbsolutePath(), Paths.get(ctx.getPath()).toAbsolutePath());
assertTrue(Files.exists(indexPath));
+ List<Path> li = Files.list(indexPath).collect(Collectors.toList());
+ assertTrue(li.size()>0);
}