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