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.maven2.MavenManagedRepository;
30 import org.apache.archiva.repository.maven2.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.getAsset( "" ).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.getAsset("").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();
141 Path destDir = repository.getAsset( "" ).getFilePath().resolve("org/apache/archiva/archiva-search/1.0");
142 Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-search/1.0");
143 org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(), destDir.toFile());
144 List<URI> uriList = new ArrayList<>();
145 uriList.add(destDir.resolve("archiva-search-1.0.jar").toUri());
146 uriList.add(destDir.resolve("archiva-search-1.0-sources.jar").toUri());
147 mavenIndexManager.addArtifactsToIndex(ctx, uriList);
149 IndexingContext mvnCtx = mavenIndexManager.getMvnContext(ctx);
150 String term = "org.apache.archiva";
151 Query q = new BooleanQuery.Builder().add( queryCreator.constructQuery( MAVEN.GROUP_ID, new UserInputSearchExpression( term ) ),
152 BooleanClause.Occur.SHOULD ).build();
153 assertEquals(2, mvnCtx.acquireIndexSearcher().count(q));
157 public void removeArtifactsFromIndex() throws Exception {
158 ArchivaIndexingContext ctx = createTestContext();
159 Path destDir = repository.getAsset( "" ).getFilePath().resolve("org/apache/archiva/archiva-search/1.0");
160 Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-search/1.0");
161 org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(), destDir.toFile());
162 List<URI> uriList = new ArrayList<>();
163 uriList.add(destDir.resolve("archiva-search-1.0.jar").toUri());
164 uriList.add(destDir.resolve("archiva-search-1.0-sources.jar").toUri());
165 mavenIndexManager.addArtifactsToIndex(ctx, uriList);
167 IndexingContext mvnCtx = mavenIndexManager.getMvnContext(ctx);
168 String term = "org.apache.archiva";
169 Query q = new BooleanQuery.Builder().add( queryCreator.constructQuery( MAVEN.GROUP_ID, new UserInputSearchExpression( term ) ),
170 BooleanClause.Occur.SHOULD ).build();
171 assertEquals(2, mvnCtx.acquireIndexSearcher().count(q));
173 mavenIndexManager.removeArtifactsFromIndex(ctx, uriList);
174 assertEquals(1, mvnCtx.acquireIndexSearcher().count(q));
178 public void supportsRepository() throws Exception {
179 assertTrue(mavenIndexManager.supportsRepository(RepositoryType.MAVEN));
180 assertFalse(mavenIndexManager.supportsRepository(RepositoryType.NPM));
183 private ArchivaIndexingContext createTestContext() throws URISyntaxException, IndexCreationFailedException, IOException {
184 indexPath = Paths.get("target/repositories/test-repo/.index-test");
185 FileUtils.deleteDirectory(indexPath);
186 repository = MavenManagedRepository.newLocalInstance("test-repo", "Test Repo", Paths.get("target/repositories"));
187 // repository.setLocation(new URI("test-repo"));
188 IndexCreationFeature icf = repository.getFeature(IndexCreationFeature.class).get();
189 icf.setIndexPath(new URI(".index-test"));
190 ctx = mavenIndexManager.createContext(repository);
194 private ArchivaIndexingContext createTestContextForRemote() throws URISyntaxException, IndexCreationFailedException, IOException {
195 indexPath = Paths.get("target/repositories/test-repo/.index-test");
196 Path repoPath = Paths.get("target/repositories").toAbsolutePath();
197 repositoryRemote = MavenRemoteRepository.newLocalInstance("test-repo", "Test Repo", repoPath);
198 repositoryRemote.setLocation(repoPath.resolve("test-repo").toUri());
199 RemoteIndexFeature icf = repositoryRemote.getFeature(RemoteIndexFeature.class).get();
200 icf.setIndexUri(new URI(".index-test"));
201 ctx = mavenIndexManager.createContext(repositoryRemote);
206 public void createContext() throws Exception {
207 ArchivaIndexingContext ctx = createTestContext();
209 assertEquals(repository, ctx.getRepository());
210 assertEquals("test-repo", ctx.getId());
211 assertEquals(indexPath.toAbsolutePath(), ctx.getPath().getFilePath().toAbsolutePath());
212 assertTrue(Files.exists(indexPath));
213 List<Path> li = Files.list(indexPath).collect(Collectors.toList());
214 assertTrue(li.size()>0);