]> source.dussan.org Git - archiva.git/blob
b9ad48c6815726174d7600993d14cbe45c2f30f5
[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.beans.ManagedRepository;
24 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
25 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
26 import org.apache.maven.index.ArtifactInfo;
27 import org.apache.maven.index.FlatSearchRequest;
28 import org.apache.maven.index.FlatSearchResponse;
29 import org.apache.maven.index.MAVEN;
30 import org.apache.maven.index.NexusIndexer;
31 import org.apache.maven.index.context.IndexCreator;
32 import org.apache.maven.index.context.IndexingContext;
33 import org.apache.maven.index.expr.SourcedSearchExpression;
34 import org.apache.maven.index.expr.StringSearchExpression;
35 import org.apache.maven.index.shaded.lucene.search.BooleanClause;
36 import org.apache.maven.index.shaded.lucene.search.BooleanQuery;
37 import org.apache.maven.index.shaded.lucene.search.IndexSearcher;
38 import org.apache.maven.index.shaded.lucene.search.TopDocs;
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.assertj.core.api.Assertions;
43 import org.junit.After;
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47 import org.springframework.test.context.ContextConfiguration;
48
49 import javax.inject.Inject;
50 import java.io.File;
51 import java.io.FilenameFilter;
52 import java.util.List;
53 import java.util.Set;
54
55 /**
56  * ArchivaIndexingTaskExecutorTest
57  */
58 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
59 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
60 public class ArchivaIndexingTaskExecutorTest
61     extends TestCase
62 {
63     @Inject
64     private ArchivaIndexingTaskExecutor indexingExecutor;
65
66     private ManagedRepository repositoryConfig;
67
68     @Inject
69     private NexusIndexer indexer;
70
71     @Inject
72     List<IndexCreator> indexCreators;
73
74     @Inject
75     ManagedRepositoryAdmin managedRepositoryAdmin;
76
77     @Inject
78     private IndexUpdater indexUpdater;
79
80     @Before
81     @Override
82     public void setUp()
83         throws Exception
84     {
85         super.setUp();
86
87         repositoryConfig = new ManagedRepository();
88         repositoryConfig.setId( "test-repo" );
89         repositoryConfig.setLocation(
90             new File( System.getProperty( "basedir" ), "target/test-classes/test-repo" ).getAbsolutePath() );
91         repositoryConfig.setLayout( "default" );
92         repositoryConfig.setName( "Test Repository" );
93         repositoryConfig.setScanned( true );
94         repositoryConfig.setSnapshots( false );
95         repositoryConfig.setReleases( true );
96
97         managedRepositoryAdmin.createIndexContext( repositoryConfig );
98     }
99
100     @After
101     @Override
102     public void tearDown()
103         throws Exception
104     {
105
106         for ( IndexingContext indexingContext : indexer.getIndexingContexts().values() )
107         {
108             indexer.removeIndexingContext( indexingContext, true );
109         }
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()
125     {
126         return indexer.getIndexingContexts().get( repositoryConfig.getId() );
127     }
128
129     @Test
130     public void testAddArtifactToIndex()
131         throws Exception
132     {
133         File artifactFile = new File( repositoryConfig.getLocation(),
134                                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
135
136         ArtifactIndexingTask task =
137             new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
138                                       getIndexingContext() );
139
140         indexingExecutor.executeTask( task );
141
142         BooleanQuery q = new BooleanQuery();
143         q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
144                BooleanClause.Occur.SHOULD );
145         q.add(
146             indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
147             BooleanClause.Occur.SHOULD );
148
149         if ( !indexer.getIndexingContexts().containsKey( repositoryConfig.getId() ) )
150         {
151             IndexingContext context = indexer.addIndexingContext( repositoryConfig.getId(), //
152                                                                   repositoryConfig.getId(), //
153                                                                   new File( repositoryConfig.getLocation() ), //
154                                                                   new File( repositoryConfig.getLocation(), ".indexer" )
155                                                                   //
156                 , null, null, indexCreators );
157             context.setSearchable( true );
158         }
159
160         FlatSearchRequest request = new FlatSearchRequest( q );
161         FlatSearchResponse response = indexer.searchFlat( request );
162
163         assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
164         assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
165         assertEquals( 1, response.getTotalHits() );
166
167         Set<ArtifactInfo> results = response.getResults();
168
169         ArtifactInfo artifactInfo = results.iterator().next();
170         assertEquals( "org.apache.archiva", artifactInfo.getGroupId() );
171         assertEquals( "archiva-index-methods-jar-test", artifactInfo.getArtifactId() );
172         assertEquals( "test-repo", artifactInfo.getRepository() );
173
174     }
175
176     @Test
177     public void testUpdateArtifactInIndex()
178         throws Exception
179     {
180         File artifactFile = new File( repositoryConfig.getLocation(),
181                                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
182
183         ArtifactIndexingTask task =
184             new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
185                                       getIndexingContext() );
186
187         indexingExecutor.executeTask( task );
188         indexingExecutor.executeTask( task );
189
190         BooleanQuery q = new BooleanQuery();
191         q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
192                BooleanClause.Occur.SHOULD );
193         q.add(
194             indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
195             BooleanClause.Occur.SHOULD );
196
197         IndexingContext ctx = indexer.getIndexingContexts().get( repositoryConfig.getId() );
198
199         IndexSearcher searcher = ctx.acquireIndexSearcher();
200         TopDocs topDocs = searcher.search( q, null, 10 );
201
202         //searcher.close();
203         ctx.releaseIndexSearcher( searcher );
204
205         assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
206         assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
207
208         // should only return 1 hit!
209         assertEquals( 1, topDocs.totalHits );
210     }
211
212     @Test
213     public void testRemoveArtifactFromIndex()
214         throws Exception
215     {
216         File artifactFile = new File( repositoryConfig.getLocation(),
217                                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
218
219         ArtifactIndexingTask task =
220             new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
221                                       getIndexingContext() );
222
223         // add artifact to index
224         indexingExecutor.executeTask( task );
225
226         BooleanQuery q = new BooleanQuery();
227         q.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
228                BooleanClause.Occur.SHOULD );
229         //q.add(
230         //    indexer.constructQuery( MAVEN.ARTIFACT_ID, new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
231         //    Occur.SHOULD );
232
233         FlatSearchRequest flatSearchRequest =
234             new FlatSearchRequest( q, indexer.getIndexingContexts().get( repositoryConfig.getId() ) );
235
236         FlatSearchResponse response = indexer.searchFlat( flatSearchRequest );
237
238         assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
239         assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
240
241         // should return 1 hit
242         assertEquals( 1, response.getTotalHitsCount() );
243
244         // remove added artifact from index
245         task = new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.DELETE,
246                                          getIndexingContext() );
247         indexingExecutor.executeTask( task );
248
249         task = new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.FINISH,
250                                          getIndexingContext() );
251         indexingExecutor.executeTask( task );
252
253         q = new BooleanQuery();
254         q.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
255                BooleanClause.Occur.SHOULD );
256         q.add( indexer.constructQuery( MAVEN.ARTIFACT_ID,
257                                        new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
258                BooleanClause.Occur.SHOULD );
259
260         assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
261         assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
262
263         flatSearchRequest = new FlatSearchRequest( q, 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         File indexerDirectory = new File( repositoryConfig.getLocation(), ".indexer" );
278
279         indexerDirectory.listFiles( new FilenameFilter()
280         {
281             @Override
282             public boolean accept( File file, String s )
283             {
284                 if ( s.startsWith( "nexus-maven-repository-index" ) )
285                 {
286                     new File( file, s ).delete();
287                 }
288                 return false;
289             }
290         } );
291
292         File artifactFile = new File( repositoryConfig.getLocation(),
293                                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
294
295         ArtifactIndexingTask task =
296             new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
297                                       getIndexingContext() );
298         task.setExecuteOnEntireRepo( false );
299
300         indexingExecutor.executeTask( task );
301
302         task = new ArtifactIndexingTask( repositoryConfig, null, ArtifactIndexingTask.Action.FINISH,
303                                          getIndexingContext() );
304
305         task.setExecuteOnEntireRepo( false );
306
307         indexingExecutor.executeTask( task );
308
309         assertTrue( indexerDirectory.exists() );
310
311         // test packed index file creation
312         //no more zip
313         //Assertions.assertThat(new File( indexerDirectory, "nexus-maven-repository-index.zip" )).exists();
314         Assertions.assertThat( new File( indexerDirectory, "nexus-maven-repository-index.properties" ) ).exists();
315         Assertions.assertThat( new File( indexerDirectory, "nexus-maven-repository-index.gz" ) ).exists();
316
317         // unpack .zip index
318         File destDir = new File( repositoryConfig.getLocation(), ".indexer/tmp" );
319         //unzipIndex( indexerDirectory.getPath(), destDir.getPath() );
320
321         DefaultIndexUpdater.FileFetcher fetcher = new DefaultIndexUpdater.FileFetcher( indexerDirectory );
322         IndexUpdateRequest updateRequest = new IndexUpdateRequest( getIndexingContext(), fetcher );
323         //updateRequest.setLocalIndexCacheDir( indexerDirectory );
324         indexUpdater.fetchAndUpdateIndex( updateRequest );
325
326         BooleanQuery q = new BooleanQuery();
327         q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
328                BooleanClause.Occur.SHOULD );
329         q.add(
330             indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
331             BooleanClause.Occur.SHOULD );
332
333         FlatSearchRequest request = new FlatSearchRequest( q, getIndexingContext() );
334         FlatSearchResponse response = indexer.searchFlat( request );
335
336         Set<ArtifactInfo> results = response.getResults();
337
338         ArtifactInfo artifactInfo = results.iterator().next();
339         assertEquals( "org.apache.archiva", artifactInfo.getGroupId() );
340         assertEquals( "archiva-index-methods-jar-test", artifactInfo.getArtifactId() );
341         assertEquals( "test-repo", artifactInfo.getRepository() );
342
343         assertEquals( 1, response.getTotalHits() );
344     }
345
346 }