Using unique index directories for maven indexer
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,6 +143,7 @@ public void update() throws Exception {
public void addArtifactsToIndex() throws Exception {
ArchivaIndexingContext ctx = createTestContext();
+ 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());
@@ -151,6 +157,13 @@ public void addArtifactsToIndex() throws Exception {
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 void supportsRepository() throws Exception {
}
private ArchivaIndexingContext createTestContext() throws URISyntaxException, IndexCreationFailedException, IOException {
- indexPath = Paths.get("target/repositories/test-repo/.index-test");
+ 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;
}
