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