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.base.RepositoryGroupHandler;
28 import org.apache.archiva.repository.features.IndexCreationFeature;
29 import org.apache.archiva.repository.features.RemoteIndexFeature;
30 import org.apache.archiva.repository.maven.MavenManagedRepository;
31 import org.apache.archiva.repository.maven.MavenRemoteRepository;
32 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
33 import org.apache.maven.index.MAVEN;
34 import org.apache.maven.index.QueryCreator;
35 import org.apache.maven.index.context.IndexingContext;
36 import org.apache.maven.index.expr.UserInputSearchExpression;
37 import org.apache.maven.index_shaded.lucene.search.BooleanClause;
38 import org.apache.maven.index_shaded.lucene.search.BooleanQuery;
39 import org.apache.maven.index_shaded.lucene.search.Query;
40 import org.junit.After;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.springframework.test.context.ContextConfiguration;
45 import javax.inject.Inject;
46 import java.io.IOException;
48 import java.net.URISyntaxException;
49 import java.nio.file.Files;
50 import java.nio.file.Path;
51 import java.nio.file.Paths;
52 import java.util.ArrayList;
53 import java.util.List;
54 import java.util.stream.Collectors;
56 import static org.junit.Assert.*;
58 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
59 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
60 public class MavenIndexManagerTest {
63 ArchivaRepositoryRegistry repositoryRegistry;
66 RepositoryGroupHandler groupHandler;
69 private Path indexPath;
70 private MavenManagedRepository repository;
71 private ArchivaIndexingContext ctx;
72 private MavenRemoteRepository repositoryRemote;
75 MavenIndexManager mavenIndexManager;
78 QueryCreator queryCreator;
82 public void tearDown() {
83 repositoryRegistry.destroy();
87 } catch (IOException e) {
91 if (indexPath!=null && Files.exists(indexPath)) {
92 FileUtils.deleteQuietly(indexPath);
98 public void pack() throws Exception {
100 Path destDir = repository.getRoot().getFilePath().resolve("org/apache/archiva/archiva-webapp/1.0");
101 Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-webapp/1.0");
102 org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(),destDir.toFile());
103 mavenIndexManager.scan(ctx);
104 mavenIndexManager.pack(ctx);
105 assertTrue(Files.list(indexPath).filter(path -> {
107 return path.getFileName().toString().endsWith(".gz") && Files.size(path) > 0;
108 } catch (IOException e) {
111 }).findAny().isPresent());
115 public void scan() throws Exception {
117 Path destDir = repository.getRoot().getFilePath().resolve("org/apache/archiva/archiva-webapp/1.0");
118 Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-webapp/1.0");
119 org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(),destDir.toFile());
120 mavenIndexManager.scan(ctx);
122 IndexingContext mvnCtx = mavenIndexManager.getMvnContext(ctx);
123 String term = "org.apache.archiva";
124 Query q = new BooleanQuery.Builder().add( queryCreator.constructQuery( MAVEN.GROUP_ID, new UserInputSearchExpression( term ) ),
125 BooleanClause.Occur.SHOULD ).build();
126 assertEquals(4, mvnCtx.acquireIndexSearcher().count(q));
130 * Does only a index update via file uri, no HTTP uri
133 public void update() throws Exception {
135 mavenIndexManager.pack(ctx);
137 createTestContextForRemote();
138 mavenIndexManager.update(ctx, true);
142 public void addArtifactsToIndex() throws Exception {
144 ArchivaIndexingContext ctx = createTestContext();
146 Path destDir = repository.getRoot().getFilePath().resolve("org/apache/archiva/archiva-search/1.0");
147 Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-search/1.0");
148 org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(), destDir.toFile());
149 List<URI> uriList = new ArrayList<>();
150 uriList.add(destDir.resolve("archiva-search-1.0.jar").toUri());
151 uriList.add(destDir.resolve("archiva-search-1.0-sources.jar").toUri());
152 mavenIndexManager.addArtifactsToIndex(ctx, uriList);
154 IndexingContext mvnCtx = mavenIndexManager.getMvnContext(ctx);
155 String term = "org.apache.archiva";
156 Query q = new BooleanQuery.Builder().add(queryCreator.constructQuery(MAVEN.GROUP_ID, new UserInputSearchExpression(term)),
157 BooleanClause.Occur.SHOULD).build();
158 assertEquals(2, mvnCtx.acquireIndexSearcher().count(q));
162 } catch (IOException e) {
169 public void removeArtifactsFromIndex() throws Exception {
170 ArchivaIndexingContext ctx = createTestContext();
171 Path destDir = repository.getRoot().getFilePath().resolve("org/apache/archiva/archiva-search/1.0");
172 Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-search/1.0");
173 org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(), destDir.toFile());
174 List<URI> uriList = new ArrayList<>();
175 uriList.add(destDir.resolve("archiva-search-1.0.jar").toUri());
176 uriList.add(destDir.resolve("archiva-search-1.0-sources.jar").toUri());
177 mavenIndexManager.addArtifactsToIndex(ctx, uriList);
179 IndexingContext mvnCtx = mavenIndexManager.getMvnContext(ctx);
180 String term = "org.apache.archiva";
181 Query q = new BooleanQuery.Builder().add( queryCreator.constructQuery( MAVEN.GROUP_ID, new UserInputSearchExpression( term ) ),
182 BooleanClause.Occur.SHOULD ).build();
183 assertEquals(2, mvnCtx.acquireIndexSearcher().count(q));
185 mavenIndexManager.removeArtifactsFromIndex(ctx, uriList);
186 assertEquals(1, mvnCtx.acquireIndexSearcher().count(q));
190 public void supportsRepository() throws Exception {
191 assertTrue(mavenIndexManager.supportsRepository(RepositoryType.MAVEN));
192 assertFalse(mavenIndexManager.supportsRepository(RepositoryType.NPM));
195 private ArchivaIndexingContext createTestContext() throws URISyntaxException, IndexCreationFailedException, IOException {
196 String indexPathName = ".index-test." + System.nanoTime();
197 indexPath = Paths.get("target/repositories/test-repo" ).resolve(indexPathName);
198 if (Files.exists(indexPath)) {
201 FileUtils.deleteDirectory(indexPath);
202 } catch (IOException e) {
203 String destName = indexPath.getFileName().toString() + "." + System.currentTimeMillis();
204 Files.move(indexPath, indexPath.getParent().resolve(destName));
207 repository = MavenManagedRepository.newLocalInstance("test-repo", "Test Repo", Paths.get("target/repositories"));
208 // repository.setLocation(new URI("test-repo"));
209 IndexCreationFeature icf = repository.getFeature(IndexCreationFeature.class).get();
210 icf.setIndexPath(new URI(indexPathName));
211 ctx = mavenIndexManager.createContext(repository);
215 private ArchivaIndexingContext createTestContextForRemote() throws URISyntaxException, IndexCreationFailedException, IOException {
216 // indexPath = Paths.get("target/repositories/test-repo/.index-test");
217 Path repoPath = Paths.get("target/repositories").toAbsolutePath();
218 repositoryRemote = MavenRemoteRepository.newLocalInstance("test-repo", "Test Repo", repoPath);
219 repositoryRemote.setLocation(repoPath.resolve("test-repo").toUri());
220 RemoteIndexFeature icf = repositoryRemote.getFeature(RemoteIndexFeature.class).get();
221 icf.setIndexUri(new URI(indexPath.getFileName().toString()));
222 ctx = mavenIndexManager.createContext(repositoryRemote);
227 public void createContext() throws Exception {
228 ArchivaIndexingContext ctx = createTestContext();
230 assertEquals(repository, ctx.getRepository());
231 assertEquals("test-repo", ctx.getId());
232 assertEquals(indexPath.toAbsolutePath(), ctx.getPath().getFilePath().toAbsolutePath());
233 assertTrue(Files.exists(indexPath));
234 List<Path> li = Files.list(indexPath).collect(Collectors.toList());
235 assertTrue(li.size()>0);