1 package org.apache.archiva.maven.indexer;
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
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import org.apache.archiva.common.utils.FileUtils;
22 import org.apache.archiva.indexer.ArchivaIndexingContext;
23 import org.apache.archiva.indexer.IndexCreationFailedException;
24 import org.apache.archiva.repository.RepositoryType;
25 import org.apache.archiva.repository.base.ArchivaRepositoryRegistry;
26 import org.apache.archiva.repository.base.RepositoryHandlerDependencies;
27 import org.apache.archiva.repository.features.IndexCreationFeature;
28 import org.apache.archiva.repository.features.RemoteIndexFeature;
29 import org.apache.archiva.maven.repository.MavenManagedRepository;
30 import org.apache.archiva.maven.repository.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;
64 @SuppressWarnings( "unused" )
66 RepositoryHandlerDependencies repositoryHandlerDependencies;
68 private Path indexPath;
69 private MavenManagedRepository repository;
70 private ArchivaIndexingContext ctx;
71 private MavenRemoteRepository repositoryRemote;
74 MavenIndexManager mavenIndexManager;
77 QueryCreator queryCreator;
81 public void tearDown() {
82 repositoryRegistry.destroy();
86 } catch (IOException e) {
90 if (indexPath!=null && Files.exists(indexPath)) {
91 FileUtils.deleteQuietly(indexPath);
97 public void pack() throws Exception {
99 Path destDir = repository.getRoot().getFilePath().resolve("org/apache/archiva/archiva-webapp/1.0");
100 Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-webapp/1.0");
101 org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(),destDir.toFile());
102 mavenIndexManager.scan(ctx);
103 mavenIndexManager.pack(ctx);
104 assertTrue(Files.list(indexPath).filter(path -> {
106 return path.getFileName().toString().endsWith(".gz") && Files.size(path) > 0;
107 } catch (IOException e) {
110 }).findAny().isPresent());
114 public void scan() throws Exception {
116 Path destDir = repository.getRoot().getFilePath().resolve("org/apache/archiva/archiva-webapp/1.0");
117 Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-webapp/1.0");
118 org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(),destDir.toFile());
119 mavenIndexManager.scan(ctx);
121 IndexingContext mvnCtx = mavenIndexManager.getMvnContext(ctx);
122 String term = "org.apache.archiva";
123 Query q = new BooleanQuery.Builder().add( queryCreator.constructQuery( MAVEN.GROUP_ID, new UserInputSearchExpression( term ) ),
124 BooleanClause.Occur.SHOULD ).build();
125 assertEquals(4, mvnCtx.acquireIndexSearcher().count(q));
129 * Does only a index update via file uri, no HTTP uri
132 public void update() throws Exception {
134 mavenIndexManager.pack(ctx);
136 createTestContextForRemote();
137 mavenIndexManager.update(ctx, true);
141 public void addArtifactsToIndex() throws Exception {
143 ArchivaIndexingContext ctx = createTestContext();
145 Path destDir = repository.getRoot().getFilePath().resolve("org/apache/archiva/archiva-search/1.0");
146 Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-search/1.0");
147 org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(), destDir.toFile());
148 List<URI> uriList = new ArrayList<>();
149 uriList.add(destDir.resolve("archiva-search-1.0.jar").toUri());
150 uriList.add(destDir.resolve("archiva-search-1.0-sources.jar").toUri());
151 mavenIndexManager.addArtifactsToIndex(ctx, uriList);
153 IndexingContext mvnCtx = mavenIndexManager.getMvnContext(ctx);
154 String term = "org.apache.archiva";
155 Query q = new BooleanQuery.Builder().add(queryCreator.constructQuery(MAVEN.GROUP_ID, new UserInputSearchExpression(term)),
156 BooleanClause.Occur.SHOULD).build();
157 assertEquals(2, mvnCtx.acquireIndexSearcher().count(q));
161 } catch (IOException e) {
168 public void removeArtifactsFromIndex() throws Exception {
169 ArchivaIndexingContext ctx = createTestContext();
170 Path destDir = repository.getRoot().getFilePath().resolve("org/apache/archiva/archiva-search/1.0");
171 Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-search/1.0");
172 org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(), destDir.toFile());
173 List<URI> uriList = new ArrayList<>();
174 uriList.add(destDir.resolve("archiva-search-1.0.jar").toUri());
175 uriList.add(destDir.resolve("archiva-search-1.0-sources.jar").toUri());
176 mavenIndexManager.addArtifactsToIndex(ctx, uriList);
178 IndexingContext mvnCtx = mavenIndexManager.getMvnContext(ctx);
179 String term = "org.apache.archiva";
180 Query q = new BooleanQuery.Builder().add( queryCreator.constructQuery( MAVEN.GROUP_ID, new UserInputSearchExpression( term ) ),
181 BooleanClause.Occur.SHOULD ).build();
182 assertEquals(2, mvnCtx.acquireIndexSearcher().count(q));
184 mavenIndexManager.removeArtifactsFromIndex(ctx, uriList);
185 assertEquals(1, mvnCtx.acquireIndexSearcher().count(q));
189 public void supportsRepository() throws Exception {
190 assertTrue(mavenIndexManager.supportsRepository(RepositoryType.MAVEN));
191 assertFalse(mavenIndexManager.supportsRepository(RepositoryType.NPM));
194 private ArchivaIndexingContext createTestContext() throws URISyntaxException, IndexCreationFailedException, IOException {
195 String indexPathName = ".index-test." + System.nanoTime();
196 indexPath = Paths.get("target/repositories/test-repo" ).resolve(indexPathName);
197 if (Files.exists(indexPath)) {
200 FileUtils.deleteDirectory(indexPath);
201 } catch (IOException e) {
202 String destName = indexPath.getFileName().toString() + "." + System.currentTimeMillis();
203 Files.move(indexPath, indexPath.getParent().resolve(destName));
206 repository = MavenManagedRepository.newLocalInstance("test-repo", "Test Repo", Paths.get("target/repositories"));
207 // repository.setLocation(new URI("test-repo"));
208 IndexCreationFeature icf = repository.getFeature(IndexCreationFeature.class).get();
209 icf.setIndexPath(new URI(indexPathName));
210 ctx = mavenIndexManager.createContext(repository);
214 private ArchivaIndexingContext createTestContextForRemote() throws URISyntaxException, IndexCreationFailedException, IOException {
215 // indexPath = Paths.get("target/repositories/test-repo/.index-test");
216 Path repoPath = Paths.get("target/repositories").toAbsolutePath();
217 repositoryRemote = MavenRemoteRepository.newLocalInstance("test-repo", "Test Repo", repoPath);
218 repositoryRemote.setLocation(repoPath.resolve("test-repo").toUri());
219 RemoteIndexFeature icf = repositoryRemote.getFeature(RemoteIndexFeature.class).get();
220 icf.setIndexUri(new URI(indexPath.getFileName().toString()));
221 ctx = mavenIndexManager.createContext(repositoryRemote);
226 public void createContext() throws Exception {
227 ArchivaIndexingContext ctx = createTestContext();
229 assertEquals(repository, ctx.getRepository());
230 assertEquals("test-repo", ctx.getId());
231 assertEquals(indexPath.toAbsolutePath(), ctx.getPath().getFilePath().toAbsolutePath());
232 assertTrue(Files.exists(indexPath));
233 List<Path> li = Files.list(indexPath).collect(Collectors.toList());
234 assertTrue(li.size()>0);