]> source.dussan.org Git - archiva.git/blob
0f89678974afe03e849353aef64a348a1f8fd6b0
[archiva.git] /
1 package org.apache.archiva.scheduler.indexing.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 junit.framework.TestCase;
23 import org.apache.archiva.indexer.ArchivaIndexingContext;
24 import org.apache.archiva.indexer.UnsupportedBaseContextException;
25 import org.apache.archiva.repository.ManagedRepository;
26 import org.apache.archiva.repository.ReleaseScheme;
27 import org.apache.archiva.repository.base.ArchivaRepositoryRegistry;
28 import org.apache.archiva.repository.base.RepositoryHandlerDependencies;
29 import org.apache.archiva.repository.base.managed.BasicManagedRepository;
30 import org.apache.archiva.repository.features.IndexCreationFeature;
31 import org.apache.archiva.repository.storage.StorageAsset;
32 import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask;
33 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
34 import org.apache.maven.index.ArtifactInfo;
35 import org.apache.maven.index.FlatSearchRequest;
36 import org.apache.maven.index.FlatSearchResponse;
37 import org.apache.maven.index.Indexer;
38 import org.apache.maven.index.MAVEN;
39 import org.apache.maven.index.context.IndexingContext;
40 import org.apache.maven.index.expr.SourcedSearchExpression;
41 import org.apache.maven.index.expr.StringSearchExpression;
42 import org.apache.maven.index.updater.DefaultIndexUpdater;
43 import org.apache.maven.index.updater.IndexUpdateRequest;
44 import org.apache.maven.index.updater.IndexUpdater;
45 import org.apache.maven.index_shaded.lucene.search.BooleanClause;
46 import org.apache.maven.index_shaded.lucene.search.BooleanQuery;
47 import org.apache.maven.index_shaded.lucene.search.IndexSearcher;
48 import org.apache.maven.index_shaded.lucene.search.TopDocs;
49 import org.assertj.core.api.Assertions;
50 import org.junit.After;
51 import org.junit.Before;
52 import org.junit.Test;
53 import org.junit.runner.RunWith;
54 import org.springframework.test.context.ContextConfiguration;
55
56 import javax.inject.Inject;
57 import java.io.IOException;
58 import java.nio.file.Files;
59 import java.nio.file.Path;
60 import java.nio.file.Paths;
61 import java.util.Set;
62
63 /**
64  * ArchivaIndexingTaskExecutorTest
65  */
66 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
67 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
68 public class ArchivaIndexingTaskExecutorTest
69     extends TestCase
70 {
71     @Inject
72     private ArchivaIndexingTaskExecutor indexingExecutor;
73
74     @Inject
75     ArchivaRepositoryRegistry repositoryRegistry;
76
77     @SuppressWarnings( "unused" )
78     @Inject
79     RepositoryHandlerDependencies repositoryHandlerDependencies;
80
81     @Inject
82     private IndexUpdater indexUpdater;
83
84     private ManagedRepository repo;
85
86     @Inject
87     private Indexer indexer;
88
89     @Before
90     @Override
91     public void setUp()
92         throws Exception
93     {
94         super.setUp();
95
96         Path baseDir = Paths.get(System.getProperty("basedir"), "target/test-classes").toAbsolutePath();
97         BasicManagedRepository repositoryConfig = BasicManagedRepository.newFilesystemInstance("test-repo", "Test Repository", baseDir.resolve("test-repo"));
98         Path repoLocation = baseDir.resolve("test-repo" );
99         repositoryConfig.setLocation(repoLocation.toUri() );
100         repositoryConfig.setLayout( "default" );
101         repositoryConfig.setScanned( true );
102         repositoryConfig.addActiveReleaseScheme( ReleaseScheme.RELEASE );
103         repositoryConfig.removeActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
104         repositoryRegistry.putRepository(repositoryConfig);
105         repo = repositoryRegistry.getManagedRepository( repositoryConfig.getId() );
106     }
107
108     @After
109     @Override
110     public void tearDown()
111         throws Exception
112     {
113
114         repositoryRegistry.destroy();
115         /*
116         removeIndexingContext with true cleanup files.
117         // delete created index in the repository
118         File indexDir = new File( repositoryConfig.getLocation(), ".indexer" );
119         FileUtils.deleteDirectory( indexDir );
120         assertFalse( indexDir.exists() );
121
122         indexDir = new File( repositoryConfig.getLocation(), ".index" );
123         FileUtils.deleteDirectory( indexDir );
124         assertFalse( indexDir.exists() );
125         */
126         super.tearDown();
127     }
128
129     protected IndexingContext getIndexingContext() throws UnsupportedBaseContextException {
130         assert repo != null;
131         ArchivaIndexingContext ctx = repo.getIndexingContext();
132         assert ctx != null;
133         return ctx.getBaseContext(IndexingContext.class);
134     }
135
136     @Test
137     public void testAddArtifactToIndex()
138         throws Exception
139     {
140         Path basePath = repo.getRoot().getFilePath();
141         Path artifactFile = basePath.resolve(
142                                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
143
144         ArtifactIndexingTask task =
145             new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.ADD,
146                                       repo.getIndexingContext());
147
148         indexingExecutor.executeTask( task );
149
150         task = new ArtifactIndexingTask( repo, null, ArtifactIndexingTask.Action.FINISH,
151             repo.getIndexingContext() );
152         indexingExecutor.executeTask( task );
153
154         BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder( );
155         queryBuilder.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
156                BooleanClause.Occur.SHOULD );
157         queryBuilder.add(
158             indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
159             BooleanClause.Occur.SHOULD );
160         BooleanQuery q = queryBuilder.build();
161
162         FlatSearchRequest request = new FlatSearchRequest( q , getIndexingContext());
163         FlatSearchResponse response = indexer.searchFlat( request );
164
165         assertTrue( Files.exists(basePath.resolve( ".indexer" )) );
166         assertTrue( Files.exists(basePath.resolve(".index" )) );
167         assertEquals( 1, response.getTotalHitsCount());
168
169         Set<ArtifactInfo> results = response.getResults();
170
171         ArtifactInfo artifactInfo = results.iterator().next();
172         assertEquals( "org.apache.archiva", artifactInfo.getGroupId() );
173         assertEquals( "archiva-index-methods-jar-test", artifactInfo.getArtifactId() );
174         assertEquals( "test-repo", artifactInfo.getRepository() );
175
176     }
177
178     @Test
179     public void testUpdateArtifactInIndex()
180         throws Exception
181     {
182         Path basePath = repo.getRoot().getFilePath();
183         Path artifactFile = basePath.resolve(
184                                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
185
186         ArtifactIndexingTask task =
187             new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.ADD,
188                                       repo.getIndexingContext() );
189
190         indexingExecutor.executeTask( task );
191         indexingExecutor.executeTask( task );
192
193         BooleanQuery.Builder qb = new BooleanQuery.Builder();
194         qb.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
195                BooleanClause.Occur.SHOULD );
196         qb.add(
197             indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
198             BooleanClause.Occur.SHOULD );
199
200         IndexingContext ctx = getIndexingContext();
201
202         IndexSearcher searcher = ctx.acquireIndexSearcher();
203         TopDocs topDocs = searcher.search( qb.build(), 10 );
204
205         //searcher.close();
206         ctx.releaseIndexSearcher( searcher );
207
208         assertTrue( Files.exists(basePath.resolve(".indexer" )) );
209         assertTrue( Files.exists(basePath.resolve(".index" )) );
210
211         // should only return 1 hit!
212         assertEquals( 1, topDocs.totalHits );
213     }
214
215     @Test
216     public void testRemoveArtifactFromIndex()
217         throws Exception
218     {
219         Path basePath = repo.getRoot().getFilePath();
220         Path artifactFile = basePath.resolve(
221                                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
222
223         ArtifactIndexingTask task =
224             new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.ADD,
225                                       repo.getIndexingContext() );
226
227         // add artifact to index
228         indexingExecutor.executeTask( task );
229
230         BooleanQuery.Builder qb = new BooleanQuery.Builder();
231         qb.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
232                BooleanClause.Occur.SHOULD );
233         //q.add(
234         //    indexer.constructQuery( MAVEN.ARTIFACT_ID, new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
235         //    Occur.SHOULD );
236
237         IndexingContext ctx = repo.getIndexingContext( ).getBaseContext( IndexingContext.class );
238         FlatSearchRequest flatSearchRequest =
239             new FlatSearchRequest( qb.build(), ctx );
240
241         FlatSearchResponse response = indexer.searchFlat( flatSearchRequest );
242
243         assertTrue( Files.exists(basePath.resolve(".indexer" )) );
244         assertTrue( Files.exists(basePath.resolve( ".index" )) );
245
246         // should return 1 hit
247         assertEquals( 1, response.getTotalHitsCount() );
248
249         // remove added artifact from index
250         task = new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.DELETE,
251                         repo.getIndexingContext());
252         indexingExecutor.executeTask( task );
253
254         task = new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.FINISH,
255                                          repo.getIndexingContext() );
256         indexingExecutor.executeTask( task );
257
258         qb = new BooleanQuery.Builder();
259         qb.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
260                BooleanClause.Occur.SHOULD );
261         qb.add( indexer.constructQuery( MAVEN.ARTIFACT_ID,
262                                        new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
263                BooleanClause.Occur.SHOULD );
264
265         assertTrue( Files.exists(basePath.resolve( ".indexer" )) );
266         assertTrue( Files.exists(basePath.resolve(".index" )) );
267
268         flatSearchRequest = new FlatSearchRequest( qb.build(), getIndexingContext() );
269
270         response = indexer.searchFlat( flatSearchRequest );
271         // artifact should have been removed from the index!
272         assertEquals( 0, response.getTotalHitsCount() );//.totalHits );
273
274         // TODO: test it was removed from the packaged index also
275     }
276
277     @Test
278     public void testPackagedIndex()
279         throws Exception
280     {
281
282         Path basePath = repo.getRoot().getFilePath();
283         IndexCreationFeature icf = repo.getFeature( IndexCreationFeature.class ).get();
284         StorageAsset packedIndexDirectory = icf.getLocalPackedIndexPath();
285         StorageAsset indexerDirectory = icf.getLocalIndexPath();
286
287         for (StorageAsset dir : new StorageAsset[] { packedIndexDirectory, indexerDirectory }) {
288             if (dir.getFilePath()!=null)
289             {
290                 Path localDirPath = dir.getFilePath();
291                 Files.list( localDirPath ).filter( path -> path.getFileName( ).toString( ).startsWith( "nexus-maven-repository-index" ) )
292                     .forEach( path ->
293                     {
294                         try
295                         {
296                             System.err.println( "Deleting " + path );
297                             Files.delete( path );
298                         }
299                         catch ( IOException e )
300                         {
301                             e.printStackTrace( );
302                         }
303                     } );
304             }
305         }
306
307
308
309
310         Path artifactFile = basePath.resolve(
311                                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
312         ArtifactIndexingTask task =
313             new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.ADD,
314                                       repo.getIndexingContext() );
315         task.setExecuteOnEntireRepo( false );
316
317         indexingExecutor.executeTask( task );
318
319         task = new ArtifactIndexingTask( repo, null, ArtifactIndexingTask.Action.FINISH,
320                                          repo.getIndexingContext() );
321
322         task.setExecuteOnEntireRepo( false );
323
324         indexingExecutor.executeTask( task );
325
326         assertTrue( Files.exists(packedIndexDirectory.getFilePath()) );
327         assertTrue( Files.exists(indexerDirectory.getFilePath()) );
328
329         // test packed index file creation
330         //no more zip
331         //Assertions.assertThat(new File( indexerDirectory, "nexus-maven-repository-index.zip" )).exists();
332         Assertions.assertThat( Files.exists(packedIndexDirectory.getFilePath().resolve("nexus-maven-repository-index.properties" ) ));
333         Assertions.assertThat( Files.exists(packedIndexDirectory.getFilePath().resolve("nexus-maven-repository-index.gz" ) ));
334         assertFalse( Files.exists(packedIndexDirectory.getFilePath().resolve("nexus-maven-repository-index.1.gz" )  ));
335
336         // unpack .zip index
337         //unzipIndex( indexerDirectory.getPath(), destDir.getPath() );
338
339         DefaultIndexUpdater.FileFetcher fetcher = new DefaultIndexUpdater.FileFetcher( packedIndexDirectory.getFilePath().toFile() );
340         IndexUpdateRequest updateRequest = new IndexUpdateRequest( getIndexingContext(), fetcher );
341         //updateRequest.setLocalIndexCacheDir( indexerDirectory );
342         indexUpdater.fetchAndUpdateIndex( updateRequest );
343
344         BooleanQuery.Builder qb = new BooleanQuery.Builder();
345         qb.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
346                BooleanClause.Occur.SHOULD );
347         qb.add(
348             indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
349             BooleanClause.Occur.SHOULD );
350
351         FlatSearchRequest request = new FlatSearchRequest( qb.build(), getIndexingContext() );
352         FlatSearchResponse response = indexer.searchFlat( request );
353
354         assertEquals( 1, response.getTotalHitsCount() );
355         Set<ArtifactInfo> results = response.getResults();
356
357         ArtifactInfo artifactInfo = results.iterator().next();
358         assertEquals( "org.apache.archiva", artifactInfo.getGroupId() );
359         assertEquals( "archiva-index-methods-jar-test", artifactInfo.getArtifactId() );
360         assertEquals( "test-repo", artifactInfo.getRepository() );
361
362
363     }
364
365 }