]> source.dussan.org Git - archiva.git/blob
ed4f5458192b2b767957788ea3bb0d9c8ced3790
[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.features.IndexCreationFeature;
28 import org.apache.archiva.repository.features.RemoteIndexFeature;
29 import org.apache.archiva.repository.maven.MavenManagedRepository;
30 import org.apache.archiva.repository.maven.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;
43
44 import javax.inject.Inject;
45 import java.io.IOException;
46 import java.net.URI;
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;
54
55 import static org.junit.Assert.*;
56
57 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
58 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
59 public class MavenIndexManagerTest {
60
61     @Inject
62     ArchivaRepositoryRegistry repositoryRegistry;
63
64
65     private Path indexPath;
66     private MavenManagedRepository repository;
67     private ArchivaIndexingContext ctx;
68     private MavenRemoteRepository repositoryRemote;
69
70     @Inject
71     MavenIndexManager mavenIndexManager;
72
73     @Inject
74     QueryCreator queryCreator;
75
76
77     @After
78     public void tearDown() {
79         repositoryRegistry.destroy();
80         if (ctx!=null) {
81             try {
82                 ctx.close(true);
83             } catch (IOException e) {
84                 //
85             }
86         }
87         if (indexPath!=null && Files.exists(indexPath)) {
88             FileUtils.deleteQuietly(indexPath);
89         }
90
91     }
92
93     @Test
94     public void pack() throws Exception {
95         createTestContext();
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 -> {
102             try {
103                 return path.getFileName().toString().endsWith(".gz") && Files.size(path) > 0;
104             } catch (IOException e) {
105                 return false;
106             }
107         }).findAny().isPresent());
108     }
109
110     @Test
111     public void scan() throws Exception {
112         createTestContext();
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);
117
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));
123     }
124
125     /*
126      * Does only a index update via file uri, no HTTP uri
127      */
128     @Test
129     public void update() throws Exception {
130         createTestContext();
131         mavenIndexManager.pack(ctx);
132         ctx.close(false);
133         createTestContextForRemote();
134         mavenIndexManager.update(ctx, true);
135     }
136
137     @Test
138     public void addArtifactsToIndex() throws Exception {
139
140         ArchivaIndexingContext ctx = createTestContext();
141         try {
142             Path destDir = repository.getAsset("").getFilePath().resolve("org/apache/archiva/archiva-search/1.0");
143             Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-search/1.0");
144             org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(), destDir.toFile());
145             List<URI> uriList = new ArrayList<>();
146             uriList.add(destDir.resolve("archiva-search-1.0.jar").toUri());
147             uriList.add(destDir.resolve("archiva-search-1.0-sources.jar").toUri());
148             mavenIndexManager.addArtifactsToIndex(ctx, uriList);
149
150             IndexingContext mvnCtx = mavenIndexManager.getMvnContext(ctx);
151             String term = "org.apache.archiva";
152             Query q = new BooleanQuery.Builder().add(queryCreator.constructQuery(MAVEN.GROUP_ID, new UserInputSearchExpression(term)),
153                     BooleanClause.Occur.SHOULD).build();
154             assertEquals(2, mvnCtx.acquireIndexSearcher().count(q));
155         } finally {
156             try {
157                 ctx.close(true);
158             } catch (IOException e) {
159                 // Ignore
160             }
161         }
162     }
163
164     @Test
165     public void removeArtifactsFromIndex() throws Exception {
166         ArchivaIndexingContext ctx = createTestContext();
167         Path destDir = repository.getAsset( "" ).getFilePath().resolve("org/apache/archiva/archiva-search/1.0");
168         Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-search/1.0");
169         org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(), destDir.toFile());
170         List<URI> uriList = new ArrayList<>();
171         uriList.add(destDir.resolve("archiva-search-1.0.jar").toUri());
172         uriList.add(destDir.resolve("archiva-search-1.0-sources.jar").toUri());
173         mavenIndexManager.addArtifactsToIndex(ctx, uriList);
174
175         IndexingContext mvnCtx = mavenIndexManager.getMvnContext(ctx);
176         String term = "org.apache.archiva";
177         Query q = new BooleanQuery.Builder().add( queryCreator.constructQuery( MAVEN.GROUP_ID, new UserInputSearchExpression( term ) ),
178                 BooleanClause.Occur.SHOULD ).build();
179         assertEquals(2, mvnCtx.acquireIndexSearcher().count(q));
180         uriList.remove(0);
181         mavenIndexManager.removeArtifactsFromIndex(ctx, uriList);
182         assertEquals(1, mvnCtx.acquireIndexSearcher().count(q));
183     }
184
185     @Test
186     public void supportsRepository() throws Exception {
187         assertTrue(mavenIndexManager.supportsRepository(RepositoryType.MAVEN));
188         assertFalse(mavenIndexManager.supportsRepository(RepositoryType.NPM));
189     }
190
191     private ArchivaIndexingContext createTestContext() throws URISyntaxException, IndexCreationFailedException, IOException {
192         String indexPathName = ".index-test." + System.nanoTime();
193         indexPath = Paths.get("target/repositories/test-repo" ).resolve(indexPathName);
194         if (Files.exists(indexPath)) {
195
196             try {
197                 FileUtils.deleteDirectory(indexPath);
198             } catch (IOException e) {
199                 String destName = indexPath.getFileName().toString() + "." + System.currentTimeMillis();
200                 Files.move(indexPath, indexPath.getParent().resolve(destName));
201             }
202         }
203         repository = MavenManagedRepository.newLocalInstance("test-repo", "Test Repo", Paths.get("target/repositories"));
204         // repository.setLocation(new URI("test-repo"));
205         IndexCreationFeature icf = repository.getFeature(IndexCreationFeature.class).get();
206         icf.setIndexPath(new URI(indexPathName));
207         ctx = mavenIndexManager.createContext(repository);
208         return ctx;
209     }
210
211     private ArchivaIndexingContext createTestContextForRemote() throws URISyntaxException, IndexCreationFailedException, IOException {
212         // indexPath = Paths.get("target/repositories/test-repo/.index-test");
213         Path repoPath = Paths.get("target/repositories").toAbsolutePath();
214         repositoryRemote = MavenRemoteRepository.newLocalInstance("test-repo", "Test Repo", repoPath);
215         repositoryRemote.setLocation(repoPath.resolve("test-repo").toUri());
216         RemoteIndexFeature icf = repositoryRemote.getFeature(RemoteIndexFeature.class).get();
217         icf.setIndexUri(new URI(indexPath.getFileName().toString()));
218         ctx = mavenIndexManager.createContext(repositoryRemote);
219         return ctx;
220     }
221
222     @Test
223     public void createContext() throws Exception {
224         ArchivaIndexingContext ctx = createTestContext();
225         assertNotNull(ctx);
226         assertEquals(repository, ctx.getRepository());
227         assertEquals("test-repo", ctx.getId());
228         assertEquals(indexPath.toAbsolutePath(), ctx.getPath().getFilePath().toAbsolutePath());
229         assertTrue(Files.exists(indexPath));
230         List<Path> li = Files.list(indexPath).collect(Collectors.toList());
231         assertTrue(li.size()>0);
232
233     }
234
235 }