]> source.dussan.org Git - archiva.git/blob
ebfcb5546a0e4c112979c4d70bcf584b3e231b4b
[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.admin.model.managed.ManagedRepositoryAdmin;
24 import org.apache.archiva.common.utils.PathUtil;
25 import org.apache.archiva.indexer.ArchivaIndexingContext;
26 import org.apache.archiva.indexer.UnsupportedBaseContextException;
27 import org.apache.archiva.repository.*;
28 import org.apache.archiva.repository.features.ArtifactCleanupFeature;
29 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
30 import org.apache.maven.index.ArtifactInfo;
31 import org.apache.maven.index.FlatSearchRequest;
32 import org.apache.maven.index.FlatSearchResponse;
33 import org.apache.maven.index.MAVEN;
34 import org.apache.maven.index.NexusIndexer;
35 import org.apache.maven.index.context.IndexCreator;
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.List;
59 import java.util.Locale;
60 import java.util.Map;
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     private BasicManagedRepository repositoryConfig;
75
76     @Inject
77     private NexusIndexer indexer;
78
79     @Inject
80     RepositoryRegistry repositoryRegistry;
81
82     @Inject
83     private IndexUpdater indexUpdater;
84
85     @Before
86     @Override
87     public void setUp()
88         throws Exception
89     {
90         super.setUp();
91
92         Path baseDir = Paths.get(System.getProperty("basedir"), "target/test-classes").toAbsolutePath();
93         repositoryConfig = new BasicManagedRepository( "test-repo", "Test Repository", baseDir);
94         Path repoLocation = baseDir.resolve("test-repo" );
95         repositoryConfig.setLocation(repoLocation.toUri() );
96         repositoryConfig.setLayout( "default" );
97         repositoryConfig.setScanned( true );
98         repositoryConfig.addActiveReleaseScheme( ReleaseScheme.RELEASE );
99         repositoryConfig.removeActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
100         repositoryRegistry.putRepository(repositoryConfig);
101     }
102
103     @After
104     @Override
105     public void tearDown()
106         throws Exception
107     {
108
109         for ( IndexingContext indexingContext : indexer.getIndexingContexts().values() )
110         {
111             indexer.removeIndexingContext( indexingContext, true );
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         Repository repo = repositoryRegistry.getRepository(repositoryConfig.getId());
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 = PathUtil.getPathFromUri( repositoryConfig.getLocation() );
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         ManagedRepository repo = repositoryRegistry.getManagedRepository(repositoryConfig.getId());
145         ArtifactIndexingTask task =
146             new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
147                                       repo.getIndexingContext());
148
149         indexingExecutor.executeTask( task );
150
151         Map<String, IndexingContext> ctxs = indexer.getIndexingContexts();
152         BooleanQuery q = new BooleanQuery();
153         q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
154                BooleanClause.Occur.SHOULD );
155         q.add(
156             indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
157             BooleanClause.Occur.SHOULD );
158
159         FlatSearchRequest request = new FlatSearchRequest( q );
160         FlatSearchResponse response = indexer.searchFlat( request );
161
162         assertTrue( Files.exists(basePath.resolve( ".indexer" )) );
163         assertFalse( Files.exists(basePath.resolve(".index" )) );
164         assertEquals( 1, response.getTotalHits() );
165
166         Set<ArtifactInfo> results = response.getResults();
167
168         ArtifactInfo artifactInfo = results.iterator().next();
169         assertEquals( "org.apache.archiva", artifactInfo.getGroupId() );
170         assertEquals( "archiva-index-methods-jar-test", artifactInfo.getArtifactId() );
171         assertEquals( "test-repo", artifactInfo.getRepository() );
172
173     }
174
175     @Test
176     public void testUpdateArtifactInIndex()
177         throws Exception
178     {
179         Path basePath = PathUtil.getPathFromUri( repositoryConfig.getLocation( ) );
180         Path artifactFile = basePath.resolve(
181                                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
182
183         ManagedRepository repo = repositoryRegistry.getManagedRepository(repositoryConfig.getId());
184         ArtifactIndexingTask task =
185             new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
186                                       repo.getIndexingContext() );
187
188         indexingExecutor.executeTask( task );
189         indexingExecutor.executeTask( task );
190
191         BooleanQuery q = new BooleanQuery();
192         q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
193                BooleanClause.Occur.SHOULD );
194         q.add(
195             indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
196             BooleanClause.Occur.SHOULD );
197
198         IndexingContext ctx = getIndexingContext();
199
200         IndexSearcher searcher = ctx.acquireIndexSearcher();
201         TopDocs topDocs = searcher.search( q, null, 10 );
202
203         //searcher.close();
204         ctx.releaseIndexSearcher( searcher );
205
206         assertTrue( Files.exists(basePath.resolve(".indexer" )) );
207         assertFalse( Files.exists(basePath.resolve(".index" )) );
208
209         // should only return 1 hit!
210         assertEquals( 1, topDocs.totalHits );
211     }
212
213     @Test
214     public void testRemoveArtifactFromIndex()
215         throws Exception
216     {
217         Path basePath = PathUtil.getPathFromUri( repositoryConfig.getLocation( ) );
218         Path artifactFile = basePath.resolve(
219                                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
220
221         ManagedRepository repo = repositoryRegistry.getManagedRepository(repositoryConfig.getId());
222         ArtifactIndexingTask task =
223             new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
224                                       repo.getIndexingContext() );
225
226         // add artifact to index
227         indexingExecutor.executeTask( task );
228
229         BooleanQuery q = new BooleanQuery();
230         q.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         FlatSearchRequest flatSearchRequest =
237             new FlatSearchRequest( q, indexer.getIndexingContexts().get( repositoryConfig.getId() ) );
238
239         FlatSearchResponse response = indexer.searchFlat( flatSearchRequest );
240
241         assertTrue( Files.exists(basePath.resolve(".indexer" )) );
242         assertFalse( Files.exists(basePath.resolve( ".index" )) );
243
244         // should return 1 hit
245         assertEquals( 1, response.getTotalHitsCount() );
246
247         // remove added artifact from index
248         task = new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.DELETE,
249                         repo.getIndexingContext());
250         indexingExecutor.executeTask( task );
251
252         task = new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.FINISH,
253                                          repo.getIndexingContext() );
254         indexingExecutor.executeTask( task );
255
256         q = new BooleanQuery();
257         q.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
258                BooleanClause.Occur.SHOULD );
259         q.add( indexer.constructQuery( MAVEN.ARTIFACT_ID,
260                                        new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
261                BooleanClause.Occur.SHOULD );
262
263         assertTrue( Files.exists(basePath.resolve( ".indexer" )) );
264         assertFalse( Files.exists(basePath.resolve(".index" )) );
265
266         flatSearchRequest = new FlatSearchRequest( q, getIndexingContext() );
267
268         response = indexer.searchFlat( flatSearchRequest );
269         // artifact should have been removed from the index!
270         assertEquals( 0, response.getTotalHitsCount() );//.totalHits );
271
272         // TODO: test it was removed from the packaged index also
273     }
274
275     @Test
276     public void testPackagedIndex()
277         throws Exception
278     {
279
280         Path basePath = PathUtil.getPathFromUri( repositoryConfig.getLocation());
281         Path indexerDirectory =basePath.resolve( ".indexer" );
282
283         Files.list(indexerDirectory).filter( path -> path.getFileName().toString().startsWith("nexus-maven-repository-index") )
284             .forEach( path ->
285             {
286                 try
287                 {
288                     Files.delete( path );
289                 }
290                 catch ( IOException e )
291                 {
292                     e.printStackTrace( );
293                 }
294             } );
295
296
297         Path artifactFile = basePath.resolve(
298                                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
299         ManagedRepository repo = repositoryRegistry.getManagedRepository(repositoryConfig.getId());
300         ArtifactIndexingTask task =
301             new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
302                                       repo.getIndexingContext() );
303         task.setExecuteOnEntireRepo( false );
304
305         indexingExecutor.executeTask( task );
306
307         task = new ArtifactIndexingTask( repositoryConfig, null, ArtifactIndexingTask.Action.FINISH,
308                                          repo.getIndexingContext() );
309
310         task.setExecuteOnEntireRepo( false );
311
312         indexingExecutor.executeTask( task );
313
314         assertTrue( Files.exists(indexerDirectory) );
315
316         // test packed index file creation
317         //no more zip
318         //Assertions.assertThat(new File( indexerDirectory, "nexus-maven-repository-index.zip" )).exists();
319         Assertions.assertThat( Files.exists(indexerDirectory.resolve("nexus-maven-repository-index.properties" ) ));
320         Assertions.assertThat( Files.exists(indexerDirectory.resolve("nexus-maven-repository-index.gz" ) ));
321
322         // unpack .zip index
323         Path destDir = basePath.resolve( ".indexer/tmp" );
324         //unzipIndex( indexerDirectory.getPath(), destDir.getPath() );
325
326         DefaultIndexUpdater.FileFetcher fetcher = new DefaultIndexUpdater.FileFetcher( indexerDirectory.toFile() );
327         IndexUpdateRequest updateRequest = new IndexUpdateRequest( getIndexingContext(), fetcher );
328         //updateRequest.setLocalIndexCacheDir( indexerDirectory );
329         indexUpdater.fetchAndUpdateIndex( updateRequest );
330
331         BooleanQuery q = new BooleanQuery();
332         q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
333                BooleanClause.Occur.SHOULD );
334         q.add(
335             indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
336             BooleanClause.Occur.SHOULD );
337
338         FlatSearchRequest request = new FlatSearchRequest( q, getIndexingContext() );
339         FlatSearchResponse response = indexer.searchFlat( request );
340
341         Set<ArtifactInfo> results = response.getResults();
342
343         ArtifactInfo artifactInfo = results.iterator().next();
344         assertEquals( "org.apache.archiva", artifactInfo.getGroupId() );
345         assertEquals( "archiva-index-methods-jar-test", artifactInfo.getArtifactId() );
346         assertEquals( "test-repo", artifactInfo.getRepository() );
347
348         assertEquals( 1, response.getTotalHits() );
349     }
350
351 }