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.RepositoryType;
26 import org.apache.archiva.repository.features.IndexCreationFeature;
27 import org.apache.archiva.repository.maven2.MavenManagedRepository;
28 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
29 import org.apache.maven.index.MAVEN;
30 import org.apache.maven.index.QueryCreator;
31 import org.apache.maven.index.context.IndexingContext;
32 import org.apache.maven.index.expr.UserInputSearchExpression;
33 import org.apache.maven.index_shaded.lucene.search.BooleanClause;
34 import org.apache.maven.index_shaded.lucene.search.BooleanQuery;
35 import org.apache.maven.index_shaded.lucene.search.Query;
36 import org.junit.After;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.springframework.test.context.ContextConfiguration;
41 import javax.inject.Inject;
43 import java.io.IOException;
45 import java.net.URISyntaxException;
46 import java.nio.file.Files;
47 import java.nio.file.Path;
48 import java.nio.file.Paths;
49 import java.util.ArrayList;
50 import java.util.List;
51 import java.util.stream.Collectors;
53 import static org.junit.Assert.*;
55 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
56 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
57 public class MavenIndexManagerTest {
59 private Path indexPath;
60 private MavenManagedRepository repository;
61 private ArchivaIndexingContext ctx;
64 MavenIndexManager mavenIndexManager;
67 QueryCreator queryCreator;
71 public void tearDown() {
75 } catch (IOException e) {
79 if (indexPath!=null && Files.exists(indexPath)) {
80 FileUtils.deleteQuietly(indexPath);
86 public void pack() throws Exception {
90 public void scan() throws Exception {
92 Path destDir = repository.getLocalPath().resolve("org/apache/archiva/archiva-webapp/1.0");
93 Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-webapp/1.0");
94 org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(),destDir.toFile());
95 mavenIndexManager.scan(ctx);
97 IndexingContext mvnCtx = mavenIndexManager.getMvnContext(ctx);
98 String term = "org.apache.archiva";
99 Query q = new BooleanQuery.Builder().add( queryCreator.constructQuery( MAVEN.GROUP_ID, new UserInputSearchExpression( term ) ),
100 BooleanClause.Occur.SHOULD ).build();
101 assertEquals(4, mvnCtx.acquireIndexSearcher().count(q));
105 public void update() throws Exception {
109 public void addArtifactsToIndex() throws Exception {
111 ArchivaIndexingContext ctx = createTestContext();
112 Path destDir = repository.getLocalPath().resolve("org/apache/archiva/archiva-search/1.0");
113 Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-search/1.0");
114 org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(), destDir.toFile());
115 List<URI> uriList = new ArrayList<>();
116 uriList.add(destDir.resolve("archiva-search-1.0.jar").toUri());
117 uriList.add(destDir.resolve("archiva-search-1.0-sources.jar").toUri());
118 mavenIndexManager.addArtifactsToIndex(ctx, uriList);
120 IndexingContext mvnCtx = mavenIndexManager.getMvnContext(ctx);
121 String term = "org.apache.archiva";
122 Query q = new BooleanQuery.Builder().add( queryCreator.constructQuery( MAVEN.GROUP_ID, new UserInputSearchExpression( term ) ),
123 BooleanClause.Occur.SHOULD ).build();
124 assertEquals(2, mvnCtx.acquireIndexSearcher().count(q));
128 public void removeArtifactsFromIndex() throws Exception {
129 ArchivaIndexingContext ctx = createTestContext();
130 Path destDir = repository.getLocalPath().resolve("org/apache/archiva/archiva-search/1.0");
131 Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-search/1.0");
132 org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(), destDir.toFile());
133 List<URI> uriList = new ArrayList<>();
134 uriList.add(destDir.resolve("archiva-search-1.0.jar").toUri());
135 uriList.add(destDir.resolve("archiva-search-1.0-sources.jar").toUri());
136 mavenIndexManager.addArtifactsToIndex(ctx, uriList);
138 IndexingContext mvnCtx = mavenIndexManager.getMvnContext(ctx);
139 String term = "org.apache.archiva";
140 Query q = new BooleanQuery.Builder().add( queryCreator.constructQuery( MAVEN.GROUP_ID, new UserInputSearchExpression( term ) ),
141 BooleanClause.Occur.SHOULD ).build();
142 assertEquals(2, mvnCtx.acquireIndexSearcher().count(q));
144 mavenIndexManager.removeArtifactsFromIndex(ctx, uriList);
145 assertEquals(1, mvnCtx.acquireIndexSearcher().count(q));
149 public void supportsRepository() throws Exception {
150 assertTrue(mavenIndexManager.supportsRepository(RepositoryType.MAVEN));
151 assertFalse(mavenIndexManager.supportsRepository(RepositoryType.NPM));
154 private ArchivaIndexingContext createTestContext() throws URISyntaxException, IndexCreationFailedException, IOException {
155 indexPath = Paths.get("target/repositories/test-repo/.index-test");
156 FileUtils.deleteDirectory(indexPath);
157 repository = new MavenManagedRepository("test-repo", "Test Repo", Paths.get("target/repositories"));
158 repository.setLocation(new URI("test-repo"));
159 IndexCreationFeature icf = repository.getFeature(IndexCreationFeature.class).get();
160 icf.setIndexPath(new URI(".index-test"));
161 ctx = mavenIndexManager.createContext(repository);
166 public void createContext() throws Exception {
167 ArchivaIndexingContext ctx = createTestContext();
169 assertEquals(repository, ctx.getRepository());
170 assertEquals("test-repo", ctx.getId());
171 assertEquals(indexPath.toAbsolutePath(), Paths.get(ctx.getPath()).toAbsolutePath());
172 assertTrue(Files.exists(indexPath));
173 List<Path> li = Files.list(indexPath).collect(Collectors.toList());
174 assertTrue(li.size()>0);