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