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