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.group.RepositoryGroupHandler;
28 import org.apache.archiva.repository.base.managed.ManagedRepositoryHandler;
29 import org.apache.archiva.repository.features.IndexCreationFeature;
30 import org.apache.archiva.repository.features.RemoteIndexFeature;
31 import org.apache.archiva.repository.maven.MavenManagedRepository;
32 import org.apache.archiva.repository.maven.MavenRemoteRepository;
33 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
34 import org.apache.maven.index.MAVEN;
35 import org.apache.maven.index.QueryCreator;
36 import org.apache.maven.index.context.IndexingContext;
37 import org.apache.maven.index.expr.UserInputSearchExpression;
38 import org.apache.maven.index_shaded.lucene.search.BooleanClause;
39 import org.apache.maven.index_shaded.lucene.search.BooleanQuery;
40 import org.apache.maven.index_shaded.lucene.search.Query;
41 import org.junit.After;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.springframework.test.context.ContextConfiguration;
46 import javax.inject.Inject;
47 import java.io.IOException;
49 import java.net.URISyntaxException;
50 import java.nio.file.Files;
51 import java.nio.file.Path;
52 import java.nio.file.Paths;
53 import java.util.ArrayList;
54 import java.util.List;
55 import java.util.stream.Collectors;
57 import static org.junit.Assert.*;
59 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
60 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
61 public class MavenIndexManagerTest {
64 ArchivaRepositoryRegistry repositoryRegistry;
66 @SuppressWarnings( "unused" )
68 ManagedRepositoryHandler managedRepositoryHandler;
70 @SuppressWarnings( "unused" )
72 RepositoryGroupHandler groupHandler;
75 private Path indexPath;
76 private MavenManagedRepository repository;
77 private ArchivaIndexingContext ctx;
78 private MavenRemoteRepository repositoryRemote;
81 MavenIndexManager mavenIndexManager;
84 QueryCreator queryCreator;
88 public void tearDown() {
89 repositoryRegistry.destroy();
93 } catch (IOException e) {
97 if (indexPath!=null && Files.exists(indexPath)) {
98 FileUtils.deleteQuietly(indexPath);
104 public void pack() throws Exception {
106 Path destDir = repository.getRoot().getFilePath().resolve("org/apache/archiva/archiva-webapp/1.0");
107 Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-webapp/1.0");
108 org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(),destDir.toFile());
109 mavenIndexManager.scan(ctx);
110 mavenIndexManager.pack(ctx);
111 assertTrue(Files.list(indexPath).filter(path -> {
113 return path.getFileName().toString().endsWith(".gz") && Files.size(path) > 0;
114 } catch (IOException e) {
117 }).findAny().isPresent());
121 public void scan() throws Exception {
123 Path destDir = repository.getRoot().getFilePath().resolve("org/apache/archiva/archiva-webapp/1.0");
124 Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-webapp/1.0");
125 org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(),destDir.toFile());
126 mavenIndexManager.scan(ctx);
128 IndexingContext mvnCtx = mavenIndexManager.getMvnContext(ctx);
129 String term = "org.apache.archiva";
130 Query q = new BooleanQuery.Builder().add( queryCreator.constructQuery( MAVEN.GROUP_ID, new UserInputSearchExpression( term ) ),
131 BooleanClause.Occur.SHOULD ).build();
132 assertEquals(4, mvnCtx.acquireIndexSearcher().count(q));
136 * Does only a index update via file uri, no HTTP uri
139 public void update() throws Exception {
141 mavenIndexManager.pack(ctx);
143 createTestContextForRemote();
144 mavenIndexManager.update(ctx, true);
148 public void addArtifactsToIndex() throws Exception {
150 ArchivaIndexingContext ctx = createTestContext();
152 Path destDir = repository.getRoot().getFilePath().resolve("org/apache/archiva/archiva-search/1.0");
153 Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-search/1.0");
154 org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(), destDir.toFile());
155 List<URI> uriList = new ArrayList<>();
156 uriList.add(destDir.resolve("archiva-search-1.0.jar").toUri());
157 uriList.add(destDir.resolve("archiva-search-1.0-sources.jar").toUri());
158 mavenIndexManager.addArtifactsToIndex(ctx, uriList);
160 IndexingContext mvnCtx = mavenIndexManager.getMvnContext(ctx);
161 String term = "org.apache.archiva";
162 Query q = new BooleanQuery.Builder().add(queryCreator.constructQuery(MAVEN.GROUP_ID, new UserInputSearchExpression(term)),
163 BooleanClause.Occur.SHOULD).build();
164 assertEquals(2, mvnCtx.acquireIndexSearcher().count(q));
168 } catch (IOException e) {
175 public void removeArtifactsFromIndex() throws Exception {
176 ArchivaIndexingContext ctx = createTestContext();
177 Path destDir = repository.getRoot().getFilePath().resolve("org/apache/archiva/archiva-search/1.0");
178 Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-search/1.0");
179 org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(), destDir.toFile());
180 List<URI> uriList = new ArrayList<>();
181 uriList.add(destDir.resolve("archiva-search-1.0.jar").toUri());
182 uriList.add(destDir.resolve("archiva-search-1.0-sources.jar").toUri());
183 mavenIndexManager.addArtifactsToIndex(ctx, uriList);
185 IndexingContext mvnCtx = mavenIndexManager.getMvnContext(ctx);
186 String term = "org.apache.archiva";
187 Query q = new BooleanQuery.Builder().add( queryCreator.constructQuery( MAVEN.GROUP_ID, new UserInputSearchExpression( term ) ),
188 BooleanClause.Occur.SHOULD ).build();
189 assertEquals(2, mvnCtx.acquireIndexSearcher().count(q));
191 mavenIndexManager.removeArtifactsFromIndex(ctx, uriList);
192 assertEquals(1, mvnCtx.acquireIndexSearcher().count(q));
196 public void supportsRepository() throws Exception {
197 assertTrue(mavenIndexManager.supportsRepository(RepositoryType.MAVEN));
198 assertFalse(mavenIndexManager.supportsRepository(RepositoryType.NPM));
201 private ArchivaIndexingContext createTestContext() throws URISyntaxException, IndexCreationFailedException, IOException {
202 String indexPathName = ".index-test." + System.nanoTime();
203 indexPath = Paths.get("target/repositories/test-repo" ).resolve(indexPathName);
204 if (Files.exists(indexPath)) {
207 FileUtils.deleteDirectory(indexPath);
208 } catch (IOException e) {
209 String destName = indexPath.getFileName().toString() + "." + System.currentTimeMillis();
210 Files.move(indexPath, indexPath.getParent().resolve(destName));
213 repository = MavenManagedRepository.newLocalInstance("test-repo", "Test Repo", Paths.get("target/repositories"));
214 // repository.setLocation(new URI("test-repo"));
215 IndexCreationFeature icf = repository.getFeature(IndexCreationFeature.class).get();
216 icf.setIndexPath(new URI(indexPathName));
217 ctx = mavenIndexManager.createContext(repository);
221 private ArchivaIndexingContext createTestContextForRemote() throws URISyntaxException, IndexCreationFailedException, IOException {
222 // indexPath = Paths.get("target/repositories/test-repo/.index-test");
223 Path repoPath = Paths.get("target/repositories").toAbsolutePath();
224 repositoryRemote = MavenRemoteRepository.newLocalInstance("test-repo", "Test Repo", repoPath);
225 repositoryRemote.setLocation(repoPath.resolve("test-repo").toUri());
226 RemoteIndexFeature icf = repositoryRemote.getFeature(RemoteIndexFeature.class).get();
227 icf.setIndexUri(new URI(indexPath.getFileName().toString()));
228 ctx = mavenIndexManager.createContext(repositoryRemote);
233 public void createContext() throws Exception {
234 ArchivaIndexingContext ctx = createTestContext();
236 assertEquals(repository, ctx.getRepository());
237 assertEquals("test-repo", ctx.getId());
238 assertEquals(indexPath.toAbsolutePath(), ctx.getPath().getFilePath().toAbsolutePath());
239 assertTrue(Files.exists(indexPath));
240 List<Path> li = Files.list(indexPath).collect(Collectors.toList());
241 assertTrue(li.size()>0);