]> source.dussan.org Git - archiva.git/blob
56f2dc696358c5a74f936788c67c5b6f20209ca3
[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.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
46 import javax.inject.Inject;
47 import java.io.BufferedInputStream;
48 import java.io.BufferedOutputStream;
49 import java.io.File;
50 import java.io.FileInputStream;
51 import java.io.FileOutputStream;
52 import java.io.FilenameFilter;
53 import java.io.IOException;
54 import java.util.Set;
55 import java.util.zip.ZipEntry;
56 import java.util.zip.ZipInputStream;
57
58 /**
59  * ArchivaIndexingTaskExecutorTest
60  */
61 @RunWith (ArchivaSpringJUnit4ClassRunner.class)
62 @ContextConfiguration (locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" })
63 public class ArchivaIndexingTaskExecutorTest
64     extends TestCase
65 {
66     @Inject
67     private ArchivaIndexingTaskExecutor indexingExecutor;
68
69     private ManagedRepository repositoryConfig;
70
71     private NexusIndexer indexer;
72
73     @Inject
74     PlexusSisuBridge plexusSisuBridge;
75
76     @Inject
77     MavenIndexerUtils mavenIndexerUtils;
78
79     @Inject
80     ManagedRepositoryAdmin managedRepositoryAdmin;
81
82     @Before
83     @Override
84     public void setUp()
85         throws Exception
86     {
87         super.setUp();
88
89         repositoryConfig = new ManagedRepository();
90         repositoryConfig.setId( "test-repo" );
91         repositoryConfig.setLocation(
92             new File( System.getProperty( "basedir" ), "target/test-classes/test-repo" ).getAbsolutePath() );
93         repositoryConfig.setLayout( "default" );
94         repositoryConfig.setName( "Test Repository" );
95         repositoryConfig.setScanned( true );
96         repositoryConfig.setSnapshots( false );
97         repositoryConfig.setReleases( true );
98
99         indexer = plexusSisuBridge.lookup( NexusIndexer.class );
100
101         managedRepositoryAdmin.createIndexContext( repositoryConfig );
102     }
103
104     @After
105     @Override
106     public void tearDown()
107         throws Exception
108     {
109
110         for ( IndexingContext indexingContext : indexer.getIndexingContexts().values() )
111         {
112             indexer.removeIndexingContext( indexingContext, true );
113         }
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()
129     {
130         return indexer.getIndexingContexts().get( repositoryConfig.getId() );
131     }
132
133     @Test
134     public void testAddArtifactToIndex()
135         throws Exception
136     {
137         File artifactFile = new File( repositoryConfig.getLocation(),
138                                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
139
140         ArtifactIndexingTask task =
141             new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
142                                       getIndexingContext() );
143
144         indexingExecutor.executeTask( task );
145
146         BooleanQuery q = new BooleanQuery();
147         q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
148                Occur.SHOULD );
149         q.add(
150             indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
151             Occur.SHOULD );
152
153         if ( !indexer.getIndexingContexts().containsKey( repositoryConfig.getId() ) )
154         {
155             IndexingContext context = indexer.addIndexingContext( repositoryConfig.getId(), repositoryConfig.getId(),
156                                                                   new File( repositoryConfig.getLocation() ),
157                                                                   new File( repositoryConfig.getLocation(),
158                                                                             ".indexer" ), null, null,
159                                                                   mavenIndexerUtils.getAllIndexCreators() );
160             context.setSearchable( true );
161         }
162
163         FlatSearchRequest request = new FlatSearchRequest( q );
164         FlatSearchResponse response = indexer.searchFlat( request );
165
166         assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
167         assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
168         assertEquals( 1, response.getTotalHits() );
169
170         Set<ArtifactInfo> results = response.getResults();
171
172         ArtifactInfo artifactInfo = results.iterator().next();
173         assertEquals( "org.apache.archiva", artifactInfo.groupId );
174         assertEquals( "archiva-index-methods-jar-test", artifactInfo.artifactId );
175         assertEquals( "test-repo", artifactInfo.repository );
176
177     }
178
179     @Test
180     public void testUpdateArtifactInIndex()
181         throws Exception
182     {
183         File artifactFile = new File( repositoryConfig.getLocation(),
184                                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
185
186         ArtifactIndexingTask task =
187             new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
188                                       getIndexingContext() );
189
190         indexingExecutor.executeTask( task );
191         indexingExecutor.executeTask( task );
192
193         BooleanQuery q = new BooleanQuery();
194         q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
195                Occur.SHOULD );
196         q.add(
197             indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
198             Occur.SHOULD );
199
200         IndexingContext ctx = indexer.getIndexingContexts().get( repositoryConfig.getId() );
201
202         IndexSearcher searcher = ctx.acquireIndexSearcher();
203         TopDocs topDocs = searcher.search( q, null, 10 );
204
205         //searcher.close();
206         ctx.releaseIndexSearcher( searcher );
207
208         assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
209         assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
210
211         // should only return 1 hit!
212         assertEquals( 1, topDocs.totalHits );
213     }
214
215     @Test
216     public void testRemoveArtifactFromIndex()
217         throws Exception
218     {
219         File artifactFile = new File( repositoryConfig.getLocation(),
220                                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
221
222         ArtifactIndexingTask task =
223             new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
224                                       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                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( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
242         assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
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                                          getIndexingContext() );
250         indexingExecutor.executeTask( task );
251
252         task = new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.FINISH,
253                                          getIndexingContext() );
254         indexingExecutor.executeTask( task );
255
256         q = new BooleanQuery();
257         q.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
258                Occur.SHOULD );
259         q.add( indexer.constructQuery( MAVEN.ARTIFACT_ID,
260                                        new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
261                Occur.SHOULD );
262
263         assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
264         assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
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         File indexerDirectory = new File( repositoryConfig.getLocation(), ".indexer" );
281
282         indexerDirectory.listFiles( new FilenameFilter()
283         {
284             @Override
285             public boolean accept( File file, String s )
286             {
287                 if ( s.startsWith( "nexus-maven-repository-index" ) )
288                 {
289                     new File( file, s ).delete();
290                 }
291                 return false;
292             }
293         } );
294
295         File artifactFile = new File( repositoryConfig.getLocation(),
296                                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
297
298         ArtifactIndexingTask task =
299             new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
300                                       getIndexingContext() );
301         task.setExecuteOnEntireRepo( false );
302
303         indexingExecutor.executeTask( task );
304
305         task = new ArtifactIndexingTask( repositoryConfig, null, ArtifactIndexingTask.Action.FINISH,
306                                          getIndexingContext() );
307
308         task.setExecuteOnEntireRepo( false );
309
310         indexingExecutor.executeTask( task );
311
312         assertTrue( indexerDirectory.exists() );
313
314         // test packed index file creation
315         assertTrue( new File( indexerDirectory, "nexus-maven-repository-index.zip" ).exists() );
316         assertTrue( new File( indexerDirectory, "nexus-maven-repository-index.properties" ).exists() );
317         assertTrue( new File( indexerDirectory, "nexus-maven-repository-index.gz" ).exists() );
318
319         // unpack .zip index
320         File destDir = new File( repositoryConfig.getLocation(), ".indexer/tmp" );
321         unzipIndex( indexerDirectory.getPath(), destDir.getPath() );
322
323         BooleanQuery q = new BooleanQuery();
324         q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
325                Occur.SHOULD );
326         q.add(
327             indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
328             Occur.SHOULD );
329
330         FlatSearchRequest request = new FlatSearchRequest( q, getIndexingContext() );
331         FlatSearchResponse response = indexer.searchFlat( request );
332
333         Set<ArtifactInfo> results = response.getResults();
334
335         ArtifactInfo artifactInfo = results.iterator().next();
336         assertEquals( "org.apache.archiva", artifactInfo.groupId );
337         assertEquals( "archiva-index-methods-jar-test", artifactInfo.artifactId );
338         assertEquals( "test-repo", artifactInfo.repository );
339
340         assertEquals( 1, response.getTotalHits() );
341     }
342
343     private void unzipIndex( String indexDir, String destDir )
344         throws IOException
345     {
346         final int buff = 2048;
347
348         new File( destDir ).mkdirs();
349
350         BufferedOutputStream out = null;
351         FileInputStream fin = new FileInputStream( new File( indexDir, "nexus-maven-repository-index.zip" ) );
352         ZipInputStream in = new ZipInputStream( new BufferedInputStream( fin ) );
353         ZipEntry entry;
354
355         while ( ( entry = in.getNextEntry() ) != null )
356         {
357             int count;
358             byte data[] = new byte[buff];
359             FileOutputStream fout = new FileOutputStream( new File( destDir, entry.getName() ) );
360             out = new BufferedOutputStream( fout, buff );
361
362             while ( ( count = in.read( data, 0, buff ) ) != -1 )
363             {
364                 out.write( data, 0, count );
365             }
366             out.flush();
367             out.close();
368         }
369
370         in.close();
371     }
372 }