]> source.dussan.org Git - archiva.git/blob
01215a39d74321d405243e6c95c860e18ea07900
[archiva.git] /
1 package org.apache.archiva.indexer.maven;
2
3 /*
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
11 *
12 *  http://www.apache.org/licenses/LICENSE-2.0
13 *
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
19 * under the License.
20 */
21
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;
45
46 import javax.inject.Inject;
47 import java.io.IOException;
48 import java.net.URI;
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;
56
57 import static org.junit.Assert.*;
58
59 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
60 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
61 public class MavenIndexManagerTest {
62
63     @Inject
64     ArchivaRepositoryRegistry repositoryRegistry;
65
66     @SuppressWarnings( "unused" )
67     @Inject
68     ManagedRepositoryHandler managedRepositoryHandler;
69
70     @SuppressWarnings( "unused" )
71     @Inject
72     RepositoryGroupHandler groupHandler;
73
74
75     private Path indexPath;
76     private MavenManagedRepository repository;
77     private ArchivaIndexingContext ctx;
78     private MavenRemoteRepository repositoryRemote;
79
80     @Inject
81     MavenIndexManager mavenIndexManager;
82
83     @Inject
84     QueryCreator queryCreator;
85
86
87     @After
88     public void tearDown() {
89         repositoryRegistry.destroy();
90         if (ctx!=null) {
91             try {
92                 ctx.close(true);
93             } catch (IOException e) {
94                 //
95             }
96         }
97         if (indexPath!=null && Files.exists(indexPath)) {
98             FileUtils.deleteQuietly(indexPath);
99         }
100
101     }
102
103     @Test
104     public void pack() throws Exception {
105         createTestContext();
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 -> {
112             try {
113                 return path.getFileName().toString().endsWith(".gz") && Files.size(path) > 0;
114             } catch (IOException e) {
115                 return false;
116             }
117         }).findAny().isPresent());
118     }
119
120     @Test
121     public void scan() throws Exception {
122         createTestContext();
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);
127
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));
133     }
134
135     /*
136      * Does only a index update via file uri, no HTTP uri
137      */
138     @Test
139     public void update() throws Exception {
140         createTestContext();
141         mavenIndexManager.pack(ctx);
142         ctx.close(false);
143         createTestContextForRemote();
144         mavenIndexManager.update(ctx, true);
145     }
146
147     @Test
148     public void addArtifactsToIndex() throws Exception {
149
150         ArchivaIndexingContext ctx = createTestContext();
151         try {
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);
159
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));
165         } finally {
166             try {
167                 ctx.close(true);
168             } catch (IOException e) {
169                 // Ignore
170             }
171         }
172     }
173
174     @Test
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);
184
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));
190         uriList.remove(0);
191         mavenIndexManager.removeArtifactsFromIndex(ctx, uriList);
192         assertEquals(1, mvnCtx.acquireIndexSearcher().count(q));
193     }
194
195     @Test
196     public void supportsRepository() throws Exception {
197         assertTrue(mavenIndexManager.supportsRepository(RepositoryType.MAVEN));
198         assertFalse(mavenIndexManager.supportsRepository(RepositoryType.NPM));
199     }
200
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)) {
205
206             try {
207                 FileUtils.deleteDirectory(indexPath);
208             } catch (IOException e) {
209                 String destName = indexPath.getFileName().toString() + "." + System.currentTimeMillis();
210                 Files.move(indexPath, indexPath.getParent().resolve(destName));
211             }
212         }
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);
218         return ctx;
219     }
220
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);
229         return ctx;
230     }
231
232     @Test
233     public void createContext() throws Exception {
234         ArchivaIndexingContext ctx = createTestContext();
235         assertNotNull(ctx);
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);
242
243     }
244
245 }