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