1 package org.apache.archiva.indexer.maven;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.common.utils.FileUtils;
23 import org.apache.archiva.indexer.ArchivaIndexingContext;
24 import org.apache.archiva.indexer.IndexCreationFailedException;
25 import org.apache.archiva.repository.base.ArchivaRepositoryRegistry;
26 import org.apache.archiva.repository.RepositoryType;
27 import org.apache.archiva.repository.features.IndexCreationFeature;
28 import org.apache.archiva.repository.features.RemoteIndexFeature;
29 import org.apache.archiva.repository.maven.MavenManagedRepository;
30 import org.apache.archiva.repository.maven.MavenRemoteRepository;
31 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
32 import org.apache.maven.index.MAVEN;
33 import org.apache.maven.index.QueryCreator;
34 import org.apache.maven.index.context.IndexingContext;
35 import org.apache.maven.index.expr.UserInputSearchExpression;
36 import org.apache.maven.index_shaded.lucene.search.BooleanClause;
37 import org.apache.maven.index_shaded.lucene.search.BooleanQuery;
38 import org.apache.maven.index_shaded.lucene.search.Query;
39 import org.junit.After;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.springframework.test.context.ContextConfiguration;
44 import javax.inject.Inject;
45 import java.io.IOException;
47 import java.net.URISyntaxException;
48 import java.nio.file.Files;
49 import java.nio.file.Path;
50 import java.nio.file.Paths;
51 import java.util.ArrayList;
52 import java.util.List;
53 import java.util.stream.Collectors;
55 import static org.junit.Assert.*;
57 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
58 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
59 public class MavenIndexManagerTest {
62 ArchivaRepositoryRegistry repositoryRegistry;
65 private Path indexPath;
66 private MavenManagedRepository repository;
67 private ArchivaIndexingContext ctx;
68 private MavenRemoteRepository repositoryRemote;
71 MavenIndexManager mavenIndexManager;
74 QueryCreator queryCreator;
78 public void tearDown() {
79 repositoryRegistry.destroy();
83 } catch (IOException e) {
87 if (indexPath!=null && Files.exists(indexPath)) {
88 FileUtils.deleteQuietly(indexPath);
94 public void pack() throws Exception {
96 Path destDir = repository.getRoot().getFilePath().resolve("org/apache/archiva/archiva-webapp/1.0");
97 Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-webapp/1.0");
98 org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(),destDir.toFile());
99 mavenIndexManager.scan(ctx);
100 mavenIndexManager.pack(ctx);
101 assertTrue(Files.list(indexPath).filter(path -> {
103 return path.getFileName().toString().endsWith(".gz") && Files.size(path) > 0;
104 } catch (IOException e) {
107 }).findAny().isPresent());
111 public void scan() throws Exception {
113 Path destDir = repository.getRoot().getFilePath().resolve("org/apache/archiva/archiva-webapp/1.0");
114 Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-webapp/1.0");
115 org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(),destDir.toFile());
116 mavenIndexManager.scan(ctx);
118 IndexingContext mvnCtx = mavenIndexManager.getMvnContext(ctx);
119 String term = "org.apache.archiva";
120 Query q = new BooleanQuery.Builder().add( queryCreator.constructQuery( MAVEN.GROUP_ID, new UserInputSearchExpression( term ) ),
121 BooleanClause.Occur.SHOULD ).build();
122 assertEquals(4, mvnCtx.acquireIndexSearcher().count(q));
126 * Does only a index update via file uri, no HTTP uri
129 public void update() throws Exception {
131 mavenIndexManager.pack(ctx);
133 createTestContextForRemote();
134 mavenIndexManager.update(ctx, true);
138 public void addArtifactsToIndex() throws Exception {
140 ArchivaIndexingContext ctx = createTestContext();
142 Path destDir = repository.getRoot().getFilePath().resolve("org/apache/archiva/archiva-search/1.0");
143 Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-search/1.0");
144 org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(), destDir.toFile());
145 List<URI> uriList = new ArrayList<>();
146 uriList.add(destDir.resolve("archiva-search-1.0.jar").toUri());
147 uriList.add(destDir.resolve("archiva-search-1.0-sources.jar").toUri());
148 mavenIndexManager.addArtifactsToIndex(ctx, uriList);
150 IndexingContext mvnCtx = mavenIndexManager.getMvnContext(ctx);
151 String term = "org.apache.archiva";
152 Query q = new BooleanQuery.Builder().add(queryCreator.constructQuery(MAVEN.GROUP_ID, new UserInputSearchExpression(term)),
153 BooleanClause.Occur.SHOULD).build();
154 assertEquals(2, mvnCtx.acquireIndexSearcher().count(q));
158 } catch (IOException e) {
165 public void removeArtifactsFromIndex() throws Exception {
166 ArchivaIndexingContext ctx = createTestContext();
167 Path destDir = repository.getRoot().getFilePath().resolve("org/apache/archiva/archiva-search/1.0");
168 Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-search/1.0");
169 org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(), destDir.toFile());
170 List<URI> uriList = new ArrayList<>();
171 uriList.add(destDir.resolve("archiva-search-1.0.jar").toUri());
172 uriList.add(destDir.resolve("archiva-search-1.0-sources.jar").toUri());
173 mavenIndexManager.addArtifactsToIndex(ctx, uriList);
175 IndexingContext mvnCtx = mavenIndexManager.getMvnContext(ctx);
176 String term = "org.apache.archiva";
177 Query q = new BooleanQuery.Builder().add( queryCreator.constructQuery( MAVEN.GROUP_ID, new UserInputSearchExpression( term ) ),
178 BooleanClause.Occur.SHOULD ).build();
179 assertEquals(2, mvnCtx.acquireIndexSearcher().count(q));
181 mavenIndexManager.removeArtifactsFromIndex(ctx, uriList);
182 assertEquals(1, mvnCtx.acquireIndexSearcher().count(q));
186 public void supportsRepository() throws Exception {
187 assertTrue(mavenIndexManager.supportsRepository(RepositoryType.MAVEN));
188 assertFalse(mavenIndexManager.supportsRepository(RepositoryType.NPM));
191 private ArchivaIndexingContext createTestContext() throws URISyntaxException, IndexCreationFailedException, IOException {
192 String indexPathName = ".index-test." + System.nanoTime();
193 indexPath = Paths.get("target/repositories/test-repo" ).resolve(indexPathName);
194 if (Files.exists(indexPath)) {
197 FileUtils.deleteDirectory(indexPath);
198 } catch (IOException e) {
199 String destName = indexPath.getFileName().toString() + "." + System.currentTimeMillis();
200 Files.move(indexPath, indexPath.getParent().resolve(destName));
203 repository = MavenManagedRepository.newLocalInstance("test-repo", "Test Repo", Paths.get("target/repositories"));
204 // repository.setLocation(new URI("test-repo"));
205 IndexCreationFeature icf = repository.getFeature(IndexCreationFeature.class).get();
206 icf.setIndexPath(new URI(indexPathName));
207 ctx = mavenIndexManager.createContext(repository);
211 private ArchivaIndexingContext createTestContextForRemote() throws URISyntaxException, IndexCreationFailedException, IOException {
212 // indexPath = Paths.get("target/repositories/test-repo/.index-test");
213 Path repoPath = Paths.get("target/repositories").toAbsolutePath();
214 repositoryRemote = MavenRemoteRepository.newLocalInstance("test-repo", "Test Repo", repoPath);
215 repositoryRemote.setLocation(repoPath.resolve("test-repo").toUri());
216 RemoteIndexFeature icf = repositoryRemote.getFeature(RemoteIndexFeature.class).get();
217 icf.setIndexUri(new URI(indexPath.getFileName().toString()));
218 ctx = mavenIndexManager.createContext(repositoryRemote);
223 public void createContext() throws Exception {
224 ArchivaIndexingContext ctx = createTestContext();
226 assertEquals(repository, ctx.getRepository());
227 assertEquals("test-repo", ctx.getId());
228 assertEquals(indexPath.toAbsolutePath(), ctx.getPath().getFilePath().toAbsolutePath());
229 assertTrue(Files.exists(indexPath));
230 List<Path> li = Files.list(indexPath).collect(Collectors.toList());
231 assertTrue(li.size()>0);