]> source.dussan.org Git - archiva.git/blob
c7e5cd55e29fe9f0475ae963e696f83b94df480a
[archiva.git] /
1 package org.apache.archiva.maven.indexer;
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  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  */
20
21 import org.apache.archiva.common.utils.FileUtils;
22 import org.apache.archiva.indexer.ArchivaIndexingContext;
23 import org.apache.archiva.indexer.IndexCreationFailedException;
24 import org.apache.archiva.repository.RepositoryType;
25 import org.apache.archiva.repository.base.ArchivaRepositoryRegistry;
26 import org.apache.archiva.repository.base.RepositoryHandlerDependencies;
27 import org.apache.archiva.repository.features.IndexCreationFeature;
28 import org.apache.archiva.repository.features.RemoteIndexFeature;
29 import org.apache.archiva.maven.repository.MavenManagedRepository;
30 import org.apache.archiva.maven.repository.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     @SuppressWarnings( "unused" )
65     @Inject
66     RepositoryHandlerDependencies repositoryHandlerDependencies;
67
68     private Path indexPath;
69     private MavenManagedRepository repository;
70     private ArchivaIndexingContext ctx;
71     private MavenRemoteRepository repositoryRemote;
72
73     @Inject
74     MavenIndexManager mavenIndexManager;
75
76     @Inject
77     QueryCreator queryCreator;
78
79
80     @After
81     public void tearDown() {
82         repositoryRegistry.destroy();
83         if (ctx!=null) {
84             try {
85                 ctx.close(true);
86             } catch (IOException e) {
87                 //
88             }
89         }
90         if (indexPath!=null && Files.exists(indexPath)) {
91             FileUtils.deleteQuietly(indexPath);
92         }
93
94     }
95
96     @Test
97     public void pack() throws Exception {
98         createTestContext();
99         Path destDir = repository.getRoot().getFilePath().resolve("org/apache/archiva/archiva-webapp/1.0");
100         Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-webapp/1.0");
101         org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(),destDir.toFile());
102         mavenIndexManager.scan(ctx);
103         mavenIndexManager.pack(ctx);
104         assertTrue(Files.list(indexPath).filter(path -> {
105             try {
106                 return path.getFileName().toString().endsWith(".gz") && Files.size(path) > 0;
107             } catch (IOException e) {
108                 return false;
109             }
110         }).findAny().isPresent());
111     }
112
113     @Test
114     public void scan() throws Exception {
115         createTestContext();
116         Path destDir = repository.getRoot().getFilePath().resolve("org/apache/archiva/archiva-webapp/1.0");
117         Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-webapp/1.0");
118         org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(),destDir.toFile());
119         mavenIndexManager.scan(ctx);
120
121         IndexingContext mvnCtx = mavenIndexManager.getMvnContext(ctx);
122         String term = "org.apache.archiva";
123         Query q = new BooleanQuery.Builder().add( queryCreator.constructQuery( MAVEN.GROUP_ID, new UserInputSearchExpression( term ) ),
124                 BooleanClause.Occur.SHOULD ).build();
125         assertEquals(4, mvnCtx.acquireIndexSearcher().count(q));
126     }
127
128     /*
129      * Does only a index update via file uri, no HTTP uri
130      */
131     @Test
132     public void update() throws Exception {
133         createTestContext();
134         mavenIndexManager.pack(ctx);
135         ctx.close(false);
136         createTestContextForRemote();
137         mavenIndexManager.update(ctx, true);
138     }
139
140     @Test
141     public void addArtifactsToIndex() throws Exception {
142
143         ArchivaIndexingContext ctx = createTestContext();
144         try {
145             Path destDir = repository.getRoot().getFilePath().resolve("org/apache/archiva/archiva-search/1.0");
146             Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-search/1.0");
147             org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(), destDir.toFile());
148             List<URI> uriList = new ArrayList<>();
149             uriList.add(destDir.resolve("archiva-search-1.0.jar").toUri());
150             uriList.add(destDir.resolve("archiva-search-1.0-sources.jar").toUri());
151             mavenIndexManager.addArtifactsToIndex(ctx, uriList);
152
153             IndexingContext mvnCtx = mavenIndexManager.getMvnContext(ctx);
154             String term = "org.apache.archiva";
155             Query q = new BooleanQuery.Builder().add(queryCreator.constructQuery(MAVEN.GROUP_ID, new UserInputSearchExpression(term)),
156                     BooleanClause.Occur.SHOULD).build();
157             assertEquals(2, mvnCtx.acquireIndexSearcher().count(q));
158         } finally {
159             try {
160                 ctx.close(true);
161             } catch (IOException e) {
162                 // Ignore
163             }
164         }
165     }
166
167     @Test
168     public void removeArtifactsFromIndex() throws Exception {
169         ArchivaIndexingContext ctx = createTestContext();
170         Path destDir = repository.getRoot().getFilePath().resolve("org/apache/archiva/archiva-search/1.0");
171         Path srcDir = Paths.get("src/test/maven-search-test-repo/org/apache/archiva/archiva-search/1.0");
172         org.apache.commons.io.FileUtils.copyDirectory(srcDir.toFile(), destDir.toFile());
173         List<URI> uriList = new ArrayList<>();
174         uriList.add(destDir.resolve("archiva-search-1.0.jar").toUri());
175         uriList.add(destDir.resolve("archiva-search-1.0-sources.jar").toUri());
176         mavenIndexManager.addArtifactsToIndex(ctx, uriList);
177
178         IndexingContext mvnCtx = mavenIndexManager.getMvnContext(ctx);
179         String term = "org.apache.archiva";
180         Query q = new BooleanQuery.Builder().add( queryCreator.constructQuery( MAVEN.GROUP_ID, new UserInputSearchExpression( term ) ),
181                 BooleanClause.Occur.SHOULD ).build();
182         assertEquals(2, mvnCtx.acquireIndexSearcher().count(q));
183         uriList.remove(0);
184         mavenIndexManager.removeArtifactsFromIndex(ctx, uriList);
185         assertEquals(1, mvnCtx.acquireIndexSearcher().count(q));
186     }
187
188     @Test
189     public void supportsRepository() throws Exception {
190         assertTrue(mavenIndexManager.supportsRepository(RepositoryType.MAVEN));
191         assertFalse(mavenIndexManager.supportsRepository(RepositoryType.NPM));
192     }
193
194     private ArchivaIndexingContext createTestContext() throws URISyntaxException, IndexCreationFailedException, IOException {
195         String indexPathName = ".index-test." + System.nanoTime();
196         indexPath = Paths.get("target/repositories/test-repo" ).resolve(indexPathName);
197         if (Files.exists(indexPath)) {
198
199             try {
200                 FileUtils.deleteDirectory(indexPath);
201             } catch (IOException e) {
202                 String destName = indexPath.getFileName().toString() + "." + System.currentTimeMillis();
203                 Files.move(indexPath, indexPath.getParent().resolve(destName));
204             }
205         }
206         repository = MavenManagedRepository.newLocalInstance("test-repo", "Test Repo", Paths.get("target/repositories"));
207         // repository.setLocation(new URI("test-repo"));
208         IndexCreationFeature icf = repository.getFeature(IndexCreationFeature.class).get();
209         icf.setIndexPath(new URI(indexPathName));
210         ctx = mavenIndexManager.createContext(repository);
211         return ctx;
212     }
213
214     private ArchivaIndexingContext createTestContextForRemote() throws URISyntaxException, IndexCreationFailedException, IOException {
215         // indexPath = Paths.get("target/repositories/test-repo/.index-test");
216         Path repoPath = Paths.get("target/repositories").toAbsolutePath();
217         repositoryRemote = MavenRemoteRepository.newLocalInstance("test-repo", "Test Repo", repoPath);
218         repositoryRemote.setLocation(repoPath.resolve("test-repo").toUri());
219         RemoteIndexFeature icf = repositoryRemote.getFeature(RemoteIndexFeature.class).get();
220         icf.setIndexUri(new URI(indexPath.getFileName().toString()));
221         ctx = mavenIndexManager.createContext(repositoryRemote);
222         return ctx;
223     }
224
225     @Test
226     public void createContext() throws Exception {
227         ArchivaIndexingContext ctx = createTestContext();
228         assertNotNull(ctx);
229         assertEquals(repository, ctx.getRepository());
230         assertEquals("test-repo", ctx.getId());
231         assertEquals(indexPath.toAbsolutePath(), ctx.getPath().getFilePath().toAbsolutePath());
232         assertTrue(Files.exists(indexPath));
233         List<Path> li = Files.list(indexPath).collect(Collectors.toList());
234         assertTrue(li.size()>0);
235
236     }
237
238 }