From: Martin Stockhammer Date: Mon, 20 Nov 2017 20:48:17 +0000 (+0100) Subject: Switching path in indexer api to uri adding additional tests X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=21f31d04c2c91f6d3fcb99d8026b73423ee3d06b;p=archiva.git Switching path in indexer api to uri adding additional tests --- diff --git a/archiva-modules/archiva-base/archiva-maven2-indexer/src/main/java/org/apache/archiva/indexer/maven/MavenIndexManager.java b/archiva-modules/archiva-base/archiva-maven2-indexer/src/main/java/org/apache/archiva/indexer/maven/MavenIndexManager.java index 60c2fdd3c..d54f868a0 100644 --- a/archiva-modules/archiva-base/archiva-maven2-indexer/src/main/java/org/apache/archiva/indexer/maven/MavenIndexManager.java +++ b/archiva-modules/archiva-base/archiva-maven2-indexer/src/main/java/org/apache/archiva/indexer/maven/MavenIndexManager.java @@ -29,7 +29,6 @@ import org.apache.archiva.indexer.ArchivaIndexingContext; 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; @@ -78,7 +77,6 @@ import java.net.URI; 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; @@ -134,7 +132,7 @@ public class MavenIndexManager implements ArchivaIndexManager 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 ) ) { @@ -226,10 +224,10 @@ public class MavenIndexManager implements ArchivaIndexManager } @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( ) ) @@ -383,10 +381,11 @@ public class MavenIndexManager implements ArchivaIndexManager } @Override - public void addArtifactsToIndex( final ArchivaIndexingContext context, final Collection artifactReference ) throws IndexUpdateFailedException + public void addArtifactsToIndex( final ArchivaIndexingContext context, final Collection artifactReference ) throws IndexUpdateFailedException { + final URI ctxUri = context.getPath(); executeUpdateFunction(context, indexingContext -> { - Collection artifacts = artifactReference.stream().map(r -> artifactContextProducer.getArtifactContext(indexingContext, r.toFile())).collect(Collectors.toList()); + Collection 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) { @@ -398,10 +397,11 @@ public class MavenIndexManager implements ArchivaIndexManager } @Override - public void removeArtifactsFromIndex( ArchivaIndexingContext context, Collection artifactReference ) throws IndexUpdateFailedException + public void removeArtifactsFromIndex( ArchivaIndexingContext context, Collection artifactReference ) throws IndexUpdateFailedException { + final URI ctxUri = context.getPath(); executeUpdateFunction(context, indexingContext -> { - Collection artifacts = artifactReference.stream().map(r -> artifactContextProducer.getArtifactContext(indexingContext, r.toFile())).collect(Collectors.toList()); + Collection 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) { diff --git a/archiva-modules/archiva-base/archiva-maven2-indexer/src/test/java/org/apache/archiva/indexer/maven/MavenIndexManagerTest.java b/archiva-modules/archiva-base/archiva-maven2-indexer/src/test/java/org/apache/archiva/indexer/maven/MavenIndexManagerTest.java index a70c584d3..96876ad95 100644 --- a/archiva-modules/archiva-base/archiva-maven2-indexer/src/test/java/org/apache/archiva/indexer/maven/MavenIndexManagerTest.java +++ b/archiva-modules/archiva-base/archiva-maven2-indexer/src/test/java/org/apache/archiva/indexer/maven/MavenIndexManagerTest.java @@ -19,21 +19,36 @@ package org.apache.archiva.indexer.maven; * 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.*; @@ -41,10 +56,31 @@ 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 { @@ -52,6 +88,17 @@ public class MavenIndexManagerTest { @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 @@ -60,29 +107,71 @@ public class MavenIndexManagerTest { @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 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 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 li = Files.list(indexPath).collect(Collectors.toList()); + assertTrue(li.size()>0); } diff --git a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/indexer/ArchivaIndexManager.java b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/indexer/ArchivaIndexManager.java index 96e578042..5e870d04f 100644 --- a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/indexer/ArchivaIndexManager.java +++ b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/indexer/ArchivaIndexManager.java @@ -19,13 +19,10 @@ package org.apache.archiva.indexer; * under the License. */ -import org.apache.archiva.model.ArtifactReference; import org.apache.archiva.repository.Repository; import org.apache.archiva.repository.RepositoryType; -import java.io.IOException; import java.net.URI; -import java.nio.file.Path; import java.util.Collection; public interface ArchivaIndexManager { @@ -39,9 +36,9 @@ public interface ArchivaIndexManager { /** * Rescans the whole repository, this index is associated to. * @param context - * @param update + * */ - void scan(ArchivaIndexingContext context, boolean update) throws IndexUpdateFailedException; + void scan(ArchivaIndexingContext context) throws IndexUpdateFailedException; /** * Updates the index from the remote url. @@ -56,14 +53,14 @@ public interface ArchivaIndexManager { * @param context * @param artifactReference */ - void addArtifactsToIndex(ArchivaIndexingContext context, Collection artifactReference) throws IndexUpdateFailedException; + void addArtifactsToIndex(ArchivaIndexingContext context, Collection artifactReference) throws IndexUpdateFailedException; /** * Removes a list of artifacts from the index. * @param context * @param artifactReference */ - void removeArtifactsFromIndex(ArchivaIndexingContext context, Collection artifactReference) throws IndexUpdateFailedException; + void removeArtifactsFromIndex(ArchivaIndexingContext context, Collection artifactReference) throws IndexUpdateFailedException; /** diff --git a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/indexer/GenericIndexManager.java b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/indexer/GenericIndexManager.java index 931c60ac6..02069773f 100644 --- a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/indexer/GenericIndexManager.java +++ b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/indexer/GenericIndexManager.java @@ -19,13 +19,11 @@ package org.apache.archiva.indexer; * under the License. */ -import org.apache.archiva.model.ArtifactReference; import org.apache.archiva.repository.Repository; import org.apache.archiva.repository.RepositoryType; import org.springframework.stereotype.Service; import java.net.URI; -import java.nio.file.Path; import java.util.Collection; @Service("indexManager#none") @@ -37,7 +35,7 @@ public class GenericIndexManager implements ArchivaIndexManager { } @Override - public void scan(ArchivaIndexingContext context, boolean update) { + public void scan(ArchivaIndexingContext context) { } @@ -47,12 +45,12 @@ public class GenericIndexManager implements ArchivaIndexManager { } @Override - public void addArtifactsToIndex(ArchivaIndexingContext context, Collection artifactReference) { + public void addArtifactsToIndex(ArchivaIndexingContext context, Collection artifactReference) { } @Override - public void removeArtifactsFromIndex(ArchivaIndexingContext context, Collection artifactReference) { + public void removeArtifactsFromIndex(ArchivaIndexingContext context, Collection artifactReference) { }