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