From: Martin Stockhammer Date: Tue, 25 Feb 2020 20:38:17 +0000 (+0100) Subject: [MRM-2011] Using unique index directories for maven indexer X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=3a4b72c2e73298e98e7b24a54b4bcf66d4abf55d;p=archiva.git [MRM-2011] Using unique index directories for maven indexer --- diff --git a/archiva-modules/archiva-maven/archiva-maven-indexer/src/test/java/org/apache/archiva/indexer/maven/MavenIndexManagerTest.java b/archiva-modules/archiva-maven/archiva-maven-indexer/src/test/java/org/apache/archiva/indexer/maven/MavenIndexManagerTest.java index e2e609143..1f8b2ddb3 100644 --- a/archiva-modules/archiva-maven/archiva-maven-indexer/src/test/java/org/apache/archiva/indexer/maven/MavenIndexManagerTest.java +++ b/archiva-modules/archiva-maven/archiva-maven-indexer/src/test/java/org/apache/archiva/indexer/maven/MavenIndexManagerTest.java @@ -45,12 +45,17 @@ import javax.inject.Inject; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; +import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.nio.file.attribute.*; import java.util.ArrayList; +import java.util.Collections; +import java.util.EnumSet; import java.util.List; import java.util.stream.Collectors; +import java.util.stream.Stream; import static org.junit.Assert.*; @@ -138,19 +143,27 @@ public class MavenIndexManagerTest { public void addArtifactsToIndex() throws Exception { ArchivaIndexingContext ctx = createTestContext(); - Path destDir = repository.getAsset( "" ).getFilePath().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)); + try { + Path destDir = repository.getAsset("").getFilePath().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)); + } finally { + try { + ctx.close(true); + } catch (IOException e) { + // Ignore + } + } } @Test @@ -181,23 +194,32 @@ public class MavenIndexManagerTest { } private ArchivaIndexingContext createTestContext() throws URISyntaxException, IndexCreationFailedException, IOException { - indexPath = Paths.get("target/repositories/test-repo/.index-test"); - FileUtils.deleteDirectory(indexPath); + String indexPathName = ".index-test." + System.nanoTime(); + indexPath = Paths.get("target/repositories/test-repo" ).resolve(indexPathName); + if (Files.exists(indexPath)) { + + try { + FileUtils.deleteDirectory(indexPath); + } catch (IOException e) { + String destName = indexPath.getFileName().toString() + "." + System.currentTimeMillis(); + Files.move(indexPath, indexPath.getParent().resolve(destName)); + } + } repository = MavenManagedRepository.newLocalInstance("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")); + icf.setIndexPath(new URI(indexPathName)); ctx = mavenIndexManager.createContext(repository); return ctx; } private ArchivaIndexingContext createTestContextForRemote() throws URISyntaxException, IndexCreationFailedException, IOException { - indexPath = Paths.get("target/repositories/test-repo/.index-test"); + // indexPath = Paths.get("target/repositories/test-repo/.index-test"); Path repoPath = Paths.get("target/repositories").toAbsolutePath(); repositoryRemote = MavenRemoteRepository.newLocalInstance("test-repo", "Test Repo", repoPath); repositoryRemote.setLocation(repoPath.resolve("test-repo").toUri()); RemoteIndexFeature icf = repositoryRemote.getFeature(RemoteIndexFeature.class).get(); - icf.setIndexUri(new URI(".index-test")); + icf.setIndexUri(new URI(indexPath.getFileName().toString())); ctx = mavenIndexManager.createContext(repositoryRemote); return ctx; }