]> source.dussan.org Git - archiva.git/blob
aff511520f2bc553dff24f4e972479e0fd59acbb
[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.commons.io.FileUtils;
28 import org.apache.lucene.search.BooleanClause.Occur;
29 import org.apache.lucene.search.BooleanQuery;
30 import org.apache.lucene.search.IndexSearcher;
31 import org.apache.lucene.search.TopDocs;
32 import org.apache.maven.index.ArtifactInfo;
33 import org.apache.maven.index.FlatSearchRequest;
34 import org.apache.maven.index.FlatSearchResponse;
35 import org.apache.maven.index.MAVEN;
36 import org.apache.maven.index.NexusIndexer;
37 import org.apache.maven.index.context.IndexingContext;
38 import org.apache.maven.index.expr.SourcedSearchExpression;
39 import org.apache.maven.index.expr.StringSearchExpression;
40 import org.junit.After;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.springframework.test.context.ContextConfiguration;
45 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
46
47 import javax.inject.Inject;
48 import java.io.BufferedInputStream;
49 import java.io.BufferedOutputStream;
50 import java.io.File;
51 import java.io.FileInputStream;
52 import java.io.FileNotFoundException;
53 import java.io.FileOutputStream;
54 import java.io.IOException;
55 import java.util.Set;
56 import java.util.zip.ZipEntry;
57 import java.util.zip.ZipInputStream;
58
59 /**
60  * ArchivaIndexingTaskExecutorTest
61  */
62 @RunWith( SpringJUnit4ClassRunner.class )
63 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
64 public class ArchivaIndexingTaskExecutorTest
65     extends TestCase
66 {
67     @Inject
68     private ArchivaIndexingTaskExecutor indexingExecutor;
69
70     private ManagedRepository repositoryConfig;
71
72     private NexusIndexer indexer;
73
74     @Inject
75     PlexusSisuBridge plexusSisuBridge;
76
77     @Inject
78     MavenIndexerUtils mavenIndexerUtils;
79
80     @Inject
81     ManagedRepositoryAdmin managedRepositoryAdmin;
82
83     @Before
84     public void setUp()
85         throws Exception
86     {
87         super.setUp();
88
89         repositoryConfig = new ManagedRepository();
90         repositoryConfig.setId( "test-repo" );
91         repositoryConfig.setLocation( "target/test-classes/test-repo" );
92         repositoryConfig.setLayout( "default" );
93         repositoryConfig.setName( "Test Repository" );
94         repositoryConfig.setScanned( true );
95         repositoryConfig.setSnapshots( false );
96         repositoryConfig.setReleases( true );
97
98         indexer = plexusSisuBridge.lookup( NexusIndexer.class );
99
100         managedRepositoryAdmin.createIndexContext( repositoryConfig );
101     }
102
103     @After
104     public void tearDown()
105         throws Exception
106     {
107
108         for ( IndexingContext indexingContext : indexer.getIndexingContexts().values() )
109         {
110             indexer.removeIndexingContext( indexingContext, true );
111         }
112
113         // delete created index in the repository
114         File indexDir = new File( repositoryConfig.getLocation(), ".indexer" );
115         FileUtils.deleteDirectory( indexDir );
116         assertFalse( indexDir.exists() );
117
118         indexDir = new File( repositoryConfig.getLocation(), ".index" );
119         FileUtils.deleteDirectory( indexDir );
120         assertFalse( indexDir.exists() );
121
122         super.tearDown();
123     }
124
125     protected IndexingContext getIndexingContext()
126     {
127         return indexer.getIndexingContexts().get( repositoryConfig.getId() );
128     }
129
130     @Test
131     public void testAddArtifactToIndex()
132         throws Exception
133     {
134         File artifactFile = new File( repositoryConfig.getLocation(),
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( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
139                                       getIndexingContext() );
140
141         indexingExecutor.executeTask( task );
142
143         BooleanQuery q = new BooleanQuery();
144         q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
145                Occur.SHOULD );
146         q.add(
147             indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
148             Occur.SHOULD );
149
150         if ( !indexer.getIndexingContexts().containsKey( repositoryConfig.getId() ) )
151         {
152             IndexingContext context = indexer.addIndexingContext( repositoryConfig.getId(), repositoryConfig.getId(),
153                                                                   new File( repositoryConfig.getLocation() ),
154                                                                   new File( repositoryConfig.getLocation(),
155                                                                             ".indexer" ), null, null,
156                                                                   mavenIndexerUtils.getAllIndexCreators() );
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.groupId );
171         assertEquals( "archiva-index-methods-jar-test", artifactInfo.artifactId );
172         assertEquals( "test-repo", artifactInfo.repository );
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                Occur.SHOULD );
193         q.add(
194             indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
195             Occur.SHOULD );
196
197         IndexSearcher searcher = indexer.getIndexingContexts().get( repositoryConfig.getId() ).getIndexSearcher();
198         TopDocs topDocs = searcher.search( q, null, 10 );
199
200         searcher.close();
201
202         assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
203         assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
204
205         // should only return 1 hit!
206         assertEquals( 1, topDocs.totalHits );
207     }
208
209     @Test
210     public void testRemoveArtifactFromIndex()
211         throws Exception
212     {
213         File artifactFile = new File( repositoryConfig.getLocation(),
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( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
218                                       getIndexingContext() );
219
220         // add artifact to index
221         indexingExecutor.executeTask( task );
222
223         BooleanQuery q = new BooleanQuery();
224         q.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
225                Occur.SHOULD );
226         //q.add(
227         //    indexer.constructQuery( MAVEN.ARTIFACT_ID, new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
228         //    Occur.SHOULD );
229
230         FlatSearchRequest flatSearchRequest =
231             new FlatSearchRequest( q, indexer.getIndexingContexts().get( repositoryConfig.getId() ) );
232
233         FlatSearchResponse response = indexer.searchFlat( flatSearchRequest );
234
235         assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
236         assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
237
238         // should return 1 hit
239         assertEquals( 1, response.getTotalHitsCount() );
240
241         // remove added artifact from index
242         task = new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.DELETE,
243                                          getIndexingContext() );
244         indexingExecutor.executeTask( task );
245
246         task = new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.FINISH,
247                                          getIndexingContext() );
248         indexingExecutor.executeTask( task );
249
250         q = new BooleanQuery();
251         q.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
252                Occur.SHOULD );
253         q.add( indexer.constructQuery( MAVEN.ARTIFACT_ID,
254                                        new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
255                Occur.SHOULD );
256
257         assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
258         assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
259
260         flatSearchRequest = new FlatSearchRequest( q, getIndexingContext() );
261
262         response = indexer.searchFlat( flatSearchRequest );
263         // artifact should have been removed from the index!
264         assertEquals( 0, response.getTotalHitsCount() );//.totalHits );
265
266         // TODO: test it was removed from the packaged index also
267     }
268
269     @Test
270     public void testPackagedIndex()
271         throws Exception
272     {
273         File artifactFile = new File( repositoryConfig.getLocation(),
274                                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
275
276         ArtifactIndexingTask task =
277             new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
278                                       getIndexingContext() );
279         task.setExecuteOnEntireRepo( false );
280
281         indexingExecutor.executeTask( task );
282
283         task = new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.FINISH,
284                                          getIndexingContext() );
285
286         task.setExecuteOnEntireRepo( false );
287
288         indexingExecutor.executeTask( task );
289
290         assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
291
292         // unpack .zip index
293         File destDir = new File( repositoryConfig.getLocation(), ".indexer/tmp" );
294         unzipIndex( new File( repositoryConfig.getLocation(), ".indexer" ).getPath(), destDir.getPath() );
295
296         BooleanQuery q = new BooleanQuery();
297         q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
298                Occur.SHOULD );
299         q.add(
300             indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
301             Occur.SHOULD );
302
303         FlatSearchRequest request = new FlatSearchRequest( q, getIndexingContext() );
304         FlatSearchResponse response = indexer.searchFlat( request );
305
306         Set<ArtifactInfo> results = response.getResults();
307
308         ArtifactInfo artifactInfo = results.iterator().next();
309         assertEquals( "org.apache.archiva", artifactInfo.groupId );
310         assertEquals( "archiva-index-methods-jar-test", artifactInfo.artifactId );
311         assertEquals( "test-repo", artifactInfo.repository );
312
313         assertEquals( 1, response.getTotalHits() );
314     }
315
316     private void unzipIndex( String indexDir, String destDir )
317         throws FileNotFoundException, IOException
318     {
319         final int buff = 2048;
320
321         new File( destDir ).mkdirs();
322
323         BufferedOutputStream out = null;
324         FileInputStream fin = new FileInputStream( new File( indexDir, "nexus-maven-repository-index.zip" ) );
325         ZipInputStream in = new ZipInputStream( new BufferedInputStream( fin ) );
326         ZipEntry entry;
327
328         while ( ( entry = in.getNextEntry() ) != null )
329         {
330             int count;
331             byte data[] = new byte[buff];
332             FileOutputStream fout = new FileOutputStream( new File( destDir, entry.getName() ) );
333             out = new BufferedOutputStream( fout, buff );
334
335             while ( ( count = in.read( data, 0, buff ) ) != -1 )
336             {
337                 out.write( data, 0, count );
338             }
339             out.flush();
340             out.close();
341         }
342
343         in.close();
344     }
345 }