]> source.dussan.org Git - archiva.git/blob
3b8da8e5d091bcc5d64095f04d929c77155aeba0
[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.common.plexusbridge.MavenIndexerUtils;
24 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
25 import org.apache.commons.io.FileUtils;
26 import org.apache.lucene.search.BooleanClause.Occur;
27 import org.apache.lucene.search.BooleanQuery;
28 import org.apache.lucene.search.IndexSearcher;
29 import org.apache.lucene.search.TopDocs;
30 import org.apache.maven.archiva.configuration.Configuration;
31 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
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 ManagedRepositoryConfiguration repositoryConfig;
71
72     private Configuration configuration;
73
74     private NexusIndexer indexer;
75
76     @Inject
77     PlexusSisuBridge plexusSisuBridge;
78
79     @Inject
80     MavenIndexerUtils mavenIndexerUtils;
81
82     @Before
83     public void setUp()
84         throws Exception
85     {
86         super.setUp();
87
88         repositoryConfig = new ManagedRepositoryConfiguration();
89         repositoryConfig.setId( "test-repo" );
90         repositoryConfig.setLocation( "target/test-classes/test-repo" );
91         repositoryConfig.setLayout( "default" );
92         repositoryConfig.setName( "Test Repository" );
93         repositoryConfig.setScanned( true );
94         repositoryConfig.setSnapshots( false );
95         repositoryConfig.setReleases( true );
96
97         configuration = new Configuration();
98         configuration.addManagedRepository( repositoryConfig );
99
100         indexer = plexusSisuBridge.lookup( NexusIndexer.class );
101
102         ArtifactIndexingTask.createContext( repositoryConfig, indexer, mavenIndexerUtils.getAllIndexCreators() );
103     }
104
105     @After
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         // delete created index in the repository
116         File indexDir = new File( repositoryConfig.getLocation(), ".indexer" );
117         FileUtils.deleteDirectory( indexDir );
118         assertFalse( indexDir.exists() );
119
120         indexDir = new File( repositoryConfig.getLocation(), ".index" );
121         FileUtils.deleteDirectory( indexDir );
122         assertFalse( indexDir.exists() );
123
124         super.tearDown();
125     }
126
127     protected IndexingContext getIndexingContext()
128     {
129         return indexer.getIndexingContexts().get( repositoryConfig.getId() );
130     }
131
132     @Test
133     public void testAddArtifactToIndex()
134         throws Exception
135     {
136         File artifactFile = new File( repositoryConfig.getLocation(),
137                                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
138
139         ArtifactIndexingTask task =
140             new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
141                                       getIndexingContext() );
142
143         indexingExecutor.executeTask( task );
144
145         BooleanQuery q = new BooleanQuery();
146         q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
147                Occur.SHOULD );
148         q.add(
149             indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
150             Occur.SHOULD );
151
152         if ( !indexer.getIndexingContexts().containsKey( repositoryConfig.getId() ) )
153         {
154             IndexingContext context = indexer.addIndexingContext( repositoryConfig.getId(), repositoryConfig.getId(),
155                                                                   new File( repositoryConfig.getLocation() ),
156                                                                   new File( repositoryConfig.getLocation(),
157                                                                             ".indexer" ), null, null,
158                                                                   mavenIndexerUtils.getAllIndexCreators() );
159             context.setSearchable( true );
160         }
161
162         FlatSearchRequest request = new FlatSearchRequest( q );
163         FlatSearchResponse response = indexer.searchFlat( request );
164
165         assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
166         assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
167         assertEquals( 1, response.getTotalHits() );
168
169         Set<ArtifactInfo> results = response.getResults();
170
171         ArtifactInfo artifactInfo = results.iterator().next();
172         assertEquals( "org.apache.archiva", artifactInfo.groupId );
173         assertEquals( "archiva-index-methods-jar-test", artifactInfo.artifactId );
174         assertEquals( "test-repo", artifactInfo.repository );
175
176     }
177
178     @Test
179     public void testUpdateArtifactInIndex()
180         throws Exception
181     {
182         File artifactFile = new File( repositoryConfig.getLocation(),
183                                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
184
185         ArtifactIndexingTask task =
186             new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
187                                       getIndexingContext() );
188
189         indexingExecutor.executeTask( task );
190         indexingExecutor.executeTask( task );
191
192         BooleanQuery q = new BooleanQuery();
193         q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
194                Occur.SHOULD );
195         q.add(
196             indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
197             Occur.SHOULD );
198
199         IndexSearcher searcher = indexer.getIndexingContexts().get( repositoryConfig.getId() ).getIndexSearcher();
200         TopDocs topDocs = searcher.search( q, null, 10 );
201
202         searcher.close();
203
204         assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
205         assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
206
207         // should only return 1 hit!
208         assertEquals( 1, topDocs.totalHits );
209     }
210
211     @Test
212     public void testRemoveArtifactFromIndex()
213         throws Exception
214     {
215         File artifactFile = new File( repositoryConfig.getLocation(),
216                                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
217
218         ArtifactIndexingTask task =
219             new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
220                                       getIndexingContext() );
221
222         // add artifact to index
223         indexingExecutor.executeTask( task );
224
225         BooleanQuery q = new BooleanQuery();
226         q.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
227                Occur.SHOULD );
228         //q.add(
229         //    indexer.constructQuery( MAVEN.ARTIFACT_ID, new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
230         //    Occur.SHOULD );
231
232         FlatSearchRequest flatSearchRequest =
233             new FlatSearchRequest( q, indexer.getIndexingContexts().get( repositoryConfig.getId() ) );
234
235         FlatSearchResponse response = indexer.searchFlat( flatSearchRequest );
236
237         assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
238         assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
239
240         // should return 1 hit
241         assertEquals( 1, response.getTotalHitsCount() );
242
243         // remove added artifact from index
244         task = new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.DELETE,
245                                          getIndexingContext() );
246         indexingExecutor.executeTask( task );
247
248         task = new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.FINISH,
249                                          getIndexingContext() );
250         indexingExecutor.executeTask( task );
251
252         q = new BooleanQuery();
253         q.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
254                Occur.SHOULD );
255         q.add( indexer.constructQuery( MAVEN.ARTIFACT_ID,
256                                        new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
257                Occur.SHOULD );
258
259         assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
260         assertTrue( new File( repositoryConfig.getLocation(), ".index" ).exists() );
261
262         flatSearchRequest = new FlatSearchRequest( q, getIndexingContext() );
263
264         response = indexer.searchFlat( flatSearchRequest );
265         // artifact should have been removed from the index!
266         assertEquals( 0, response.getTotalHitsCount() );//.totalHits );
267
268         // TODO: test it was removed from the packaged index also
269     }
270
271     @Test
272     public void testPackagedIndex()
273         throws Exception
274     {
275         File artifactFile = new File( repositoryConfig.getLocation(),
276                                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
277
278         ArtifactIndexingTask task =
279             new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
280                                       getIndexingContext() );
281         task.setExecuteOnEntireRepo( false );
282
283         indexingExecutor.executeTask( task );
284
285         task = new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.FINISH,
286                                          getIndexingContext() );
287
288         task.setExecuteOnEntireRepo( false );
289
290         indexingExecutor.executeTask( task );
291
292         assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
293         assertTrue( new File( repositoryConfig.getLocation(), ".index" ).exists() );
294
295         // unpack .zip index
296         File destDir = new File( repositoryConfig.getLocation(), ".index/tmp" );
297         unzipIndex( new File( repositoryConfig.getLocation(), ".index" ).getPath(), destDir.getPath() );
298
299         BooleanQuery q = new BooleanQuery();
300         q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
301                Occur.SHOULD );
302         q.add(
303             indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
304             Occur.SHOULD );
305
306         FlatSearchRequest request = new FlatSearchRequest( q, getIndexingContext() );
307         FlatSearchResponse response = indexer.searchFlat( request );
308
309         Set<ArtifactInfo> results = response.getResults();
310
311         ArtifactInfo artifactInfo = results.iterator().next();
312         assertEquals( "org.apache.archiva", artifactInfo.groupId );
313         assertEquals( "archiva-index-methods-jar-test", artifactInfo.artifactId );
314         assertEquals( "test-repo", artifactInfo.repository );
315
316         assertEquals( 1, response.getTotalHits() );
317     }
318
319     private void unzipIndex( String indexDir, String destDir )
320         throws FileNotFoundException, IOException
321     {
322         final int buff = 2048;
323
324         new File( destDir ).mkdirs();
325
326         BufferedOutputStream out = null;
327         FileInputStream fin = new FileInputStream( new File( indexDir, "nexus-maven-repository-index.zip" ) );
328         ZipInputStream in = new ZipInputStream( new BufferedInputStream( fin ) );
329         ZipEntry entry;
330
331         while ( ( entry = in.getNextEntry() ) != null )
332         {
333             int count;
334             byte data[] = new byte[buff];
335             FileOutputStream fout = new FileOutputStream( new File( destDir, entry.getName() ) );
336             out = new BufferedOutputStream( fout, buff );
337
338             while ( ( count = in.read( data, 0, buff ) ) != -1 )
339             {
340                 out.write( data, 0, count );
341             }
342             out.flush();
343             out.close();
344         }
345
346         in.close();
347     }
348 }