]> source.dussan.org Git - archiva.git/blob
6eb6059a8db1c7a06fbef31c1b4f9df230ff81a3
[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
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.FileNotFoundException;
52 import java.io.FileOutputStream;
53 import java.io.IOException;
54 import java.util.Set;
55 import java.util.zip.ZipEntry;
56 import java.util.zip.ZipInputStream;
57 import org.apache.archiva.test.ArchivaSpringJUnit4ClassRunner;
58
59 /**
60  * ArchivaIndexingTaskExecutorTest
61  */
62 @RunWith( ArchivaSpringJUnit4ClassRunner.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(
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     public void tearDown()
106         throws Exception
107     {
108
109         for ( IndexingContext indexingContext : indexer.getIndexingContexts().values() )
110         {
111             indexer.removeIndexingContext( indexingContext, true );
112         }
113
114         // delete created index in the repository
115         File indexDir = new File( repositoryConfig.getLocation(), ".indexer" );
116         FileUtils.deleteDirectory( indexDir );
117         assertFalse( indexDir.exists() );
118
119         indexDir = new File( repositoryConfig.getLocation(), ".index" );
120         FileUtils.deleteDirectory( indexDir );
121         assertFalse( indexDir.exists() );
122
123         super.tearDown();
124     }
125
126     protected IndexingContext getIndexingContext()
127     {
128         return indexer.getIndexingContexts().get( repositoryConfig.getId() );
129     }
130
131     @Test
132     public void testAddArtifactToIndex()
133         throws Exception
134     {
135         File artifactFile = new File( repositoryConfig.getLocation(),
136                                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
137
138         ArtifactIndexingTask task =
139             new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
140                                       getIndexingContext() );
141
142         indexingExecutor.executeTask( task );
143
144         BooleanQuery q = new BooleanQuery();
145         q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
146                Occur.SHOULD );
147         q.add(
148             indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
149             Occur.SHOULD );
150
151         if ( !indexer.getIndexingContexts().containsKey( repositoryConfig.getId() ) )
152         {
153             IndexingContext context = indexer.addIndexingContext( repositoryConfig.getId(), repositoryConfig.getId(),
154                                                                   new File( repositoryConfig.getLocation() ),
155                                                                   new File( repositoryConfig.getLocation(),
156                                                                             ".indexer" ), null, null,
157                                                                   mavenIndexerUtils.getAllIndexCreators() );
158             context.setSearchable( true );
159         }
160
161         FlatSearchRequest request = new FlatSearchRequest( q );
162         FlatSearchResponse response = indexer.searchFlat( request );
163
164         assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
165         assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
166         assertEquals( 1, response.getTotalHits() );
167
168         Set<ArtifactInfo> results = response.getResults();
169
170         ArtifactInfo artifactInfo = results.iterator().next();
171         assertEquals( "org.apache.archiva", artifactInfo.groupId );
172         assertEquals( "archiva-index-methods-jar-test", artifactInfo.artifactId );
173         assertEquals( "test-repo", artifactInfo.repository );
174
175     }
176
177     @Test
178     public void testUpdateArtifactInIndex()
179         throws Exception
180     {
181         File artifactFile = new File( repositoryConfig.getLocation(),
182                                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
183
184         ArtifactIndexingTask task =
185             new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
186                                       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                Occur.SHOULD );
194         q.add(
195             indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
196             Occur.SHOULD );
197
198         IndexSearcher searcher = indexer.getIndexingContexts().get( repositoryConfig.getId() ).getIndexSearcher();
199         TopDocs topDocs = searcher.search( q, null, 10 );
200
201         searcher.close();
202
203         assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
204         assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
205
206         // should only return 1 hit!
207         assertEquals( 1, topDocs.totalHits );
208     }
209
210     @Test
211     public void testRemoveArtifactFromIndex()
212         throws Exception
213     {
214         File artifactFile = new File( repositoryConfig.getLocation(),
215                                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
216
217         ArtifactIndexingTask task =
218             new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
219                                       getIndexingContext() );
220
221         // add artifact to index
222         indexingExecutor.executeTask( task );
223
224         BooleanQuery q = new BooleanQuery();
225         q.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
226                Occur.SHOULD );
227         //q.add(
228         //    indexer.constructQuery( MAVEN.ARTIFACT_ID, new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
229         //    Occur.SHOULD );
230
231         FlatSearchRequest flatSearchRequest =
232             new FlatSearchRequest( q, indexer.getIndexingContexts().get( repositoryConfig.getId() ) );
233
234         FlatSearchResponse response = indexer.searchFlat( flatSearchRequest );
235
236         assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
237         assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
238
239         // should return 1 hit
240         assertEquals( 1, response.getTotalHitsCount() );
241
242         // remove added artifact from index
243         task = new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.DELETE,
244                                          getIndexingContext() );
245         indexingExecutor.executeTask( task );
246
247         task = new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.FINISH,
248                                          getIndexingContext() );
249         indexingExecutor.executeTask( task );
250
251         q = new BooleanQuery();
252         q.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
253                Occur.SHOULD );
254         q.add( indexer.constructQuery( MAVEN.ARTIFACT_ID,
255                                        new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
256                Occur.SHOULD );
257
258         assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
259         assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
260
261         flatSearchRequest = new FlatSearchRequest( q, getIndexingContext() );
262
263         response = indexer.searchFlat( flatSearchRequest );
264         // artifact should have been removed from the index!
265         assertEquals( 0, response.getTotalHitsCount() );//.totalHits );
266
267         // TODO: test it was removed from the packaged index also
268     }
269
270     @Test
271     public void testPackagedIndex()
272         throws Exception
273     {
274         File artifactFile = new File( repositoryConfig.getLocation(),
275                                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
276
277         ArtifactIndexingTask task =
278             new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
279                                       getIndexingContext() );
280         task.setExecuteOnEntireRepo( false );
281
282         indexingExecutor.executeTask( task );
283
284         task = new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.FINISH,
285                                          getIndexingContext() );
286
287         task.setExecuteOnEntireRepo( false );
288
289         indexingExecutor.executeTask( task );
290
291         assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
292
293         // unpack .zip index
294         File destDir = new File( repositoryConfig.getLocation(), ".indexer/tmp" );
295         unzipIndex( new File( repositoryConfig.getLocation(), ".indexer" ).getPath(), destDir.getPath() );
296
297         BooleanQuery q = new BooleanQuery();
298         q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
299                Occur.SHOULD );
300         q.add(
301             indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
302             Occur.SHOULD );
303
304         FlatSearchRequest request = new FlatSearchRequest( q, getIndexingContext() );
305         FlatSearchResponse response = indexer.searchFlat( request );
306
307         Set<ArtifactInfo> results = response.getResults();
308
309         ArtifactInfo artifactInfo = results.iterator().next();
310         assertEquals( "org.apache.archiva", artifactInfo.groupId );
311         assertEquals( "archiva-index-methods-jar-test", artifactInfo.artifactId );
312         assertEquals( "test-repo", artifactInfo.repository );
313
314         assertEquals( 1, response.getTotalHits() );
315     }
316
317     private void unzipIndex( String indexDir, String destDir )
318         throws FileNotFoundException, IOException
319     {
320         final int buff = 2048;
321
322         new File( destDir ).mkdirs();
323
324         BufferedOutputStream out = null;
325         FileInputStream fin = new FileInputStream( new File( indexDir, "nexus-maven-repository-index.zip" ) );
326         ZipInputStream in = new ZipInputStream( new BufferedInputStream( fin ) );
327         ZipEntry entry;
328
329         while ( ( entry = in.getNextEntry() ) != null )
330         {
331             int count;
332             byte data[] = new byte[buff];
333             FileOutputStream fout = new FileOutputStream( new File( destDir, entry.getName() ) );
334             out = new BufferedOutputStream( fout, buff );
335
336             while ( ( count = in.read( data, 0, buff ) ) != -1 )
337             {
338                 out.write( data, 0, count );
339             }
340             out.flush();
341             out.close();
342         }
343
344         in.close();
345     }
346 }