]> source.dussan.org Git - archiva.git/blob
a0e90b64781085fc90c6a45677bc8e98603020e2
[archiva.git] /
1 package org.apache.maven.archiva.scheduled.executors;
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 java.io.BufferedInputStream;
23 import java.io.BufferedOutputStream;
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.FileNotFoundException;
27 import java.io.FileOutputStream;
28 import java.io.IOException;
29 import java.util.Set;
30 import java.util.zip.ZipEntry;
31 import java.util.zip.ZipInputStream;
32
33 import org.apache.commons.io.FileUtils;
34 import org.apache.lucene.search.BooleanQuery;
35 import org.apache.lucene.search.IndexSearcher;
36 import org.apache.lucene.search.TopDocs;
37 import org.apache.lucene.search.BooleanClause.Occur;
38 import org.apache.maven.archiva.configuration.Configuration;
39 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
40 import org.apache.maven.archiva.scheduled.tasks.ArtifactIndexingTask;
41 import org.apache.maven.archiva.scheduled.tasks.TaskCreator;
42 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
43 import org.sonatype.nexus.index.ArtifactInfo;
44 import org.sonatype.nexus.index.FlatSearchRequest;
45 import org.sonatype.nexus.index.FlatSearchResponse;
46 import org.sonatype.nexus.index.IndexerEngine;
47 import org.sonatype.nexus.index.NexusIndexer;
48 import org.sonatype.nexus.index.context.IndexingContext;
49 import org.sonatype.nexus.index.packer.IndexPacker;
50
51 /**
52  * ArchivaIndexingTaskExecutorTest
53  */
54 public class ArchivaIndexingTaskExecutorTest
55     extends PlexusInSpringTestCase
56 {
57     private ArchivaIndexingTaskExecutor indexingExecutor;
58
59     private IndexerEngine indexerEngine;
60
61     private IndexPacker indexPacker;
62
63     private ManagedRepositoryConfiguration repositoryConfig;
64
65     private Configuration configuration;
66
67     private NexusIndexer indexer;
68
69     private IndexingContext context;
70
71     protected void setUp()
72         throws Exception
73     {
74         super.setUp();
75
76         indexingExecutor = new ArchivaIndexingTaskExecutor();
77         indexingExecutor.initialize();
78
79         repositoryConfig = new ManagedRepositoryConfiguration();
80         repositoryConfig.setId( "test-repo" );
81         repositoryConfig.setLocation( getBasedir() + "/target/test-classes/test-repo" );
82         repositoryConfig.setLayout( "default" );
83         repositoryConfig.setName( "Test Repository" );
84         repositoryConfig.setScanned( true );
85         repositoryConfig.setSnapshots( false );
86         repositoryConfig.setReleases( true );
87
88         configuration = new Configuration();
89         configuration.addManagedRepository( repositoryConfig );
90
91         indexer = (NexusIndexer) lookup( NexusIndexer.class );
92         indexerEngine = (IndexerEngine) lookup( IndexerEngine.class );
93         indexPacker = (IndexPacker) lookup( IndexPacker.class );
94
95         indexingExecutor.setIndexerEngine( indexerEngine );
96         indexingExecutor.setIndexPacker( indexPacker );
97
98         context = TaskCreator.createContext( repositoryConfig );
99     }
100
101     protected void tearDown()
102         throws Exception
103     {
104         context.close( true );
105         indexer.removeIndexingContext( context, true );
106
107         // delete created index in the repository
108         File indexDir = new File( repositoryConfig.getLocation(), ".indexer" );
109         FileUtils.deleteDirectory( indexDir );
110         assertFalse( indexDir.exists() );
111
112         indexDir = new File( repositoryConfig.getLocation(), ".index" );
113         FileUtils.deleteDirectory( indexDir );
114         assertFalse( indexDir.exists() );
115
116         super.tearDown();
117     }
118
119     public void testAddArtifactToIndex()
120         throws Exception
121     {
122         File artifactFile =
123             new File( repositoryConfig.getLocation(),
124                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
125
126         ArtifactIndexingTask task =
127             TaskCreator.createIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD, context );
128
129         indexingExecutor.executeTask( task );
130
131         BooleanQuery q = new BooleanQuery();
132         q.add( indexer.constructQuery( ArtifactInfo.GROUP_ID, "org.apache.archiva" ), Occur.SHOULD );
133         q.add( indexer.constructQuery( ArtifactInfo.ARTIFACT_ID, "archiva-index-methods-jar-test" ), Occur.SHOULD );
134
135         IndexingContext context =
136             indexer.addIndexingContext( repositoryConfig.getId(), repositoryConfig.getId(),
137                                         new File( repositoryConfig.getLocation() ),
138                                         new File( repositoryConfig.getLocation(), ".indexer" ), null, null,
139                                         NexusIndexer.FULL_INDEX );
140         context.setSearchable( true );
141
142         FlatSearchRequest request = new FlatSearchRequest( q );
143         FlatSearchResponse response = indexer.searchFlat( request );
144
145         assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
146         assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
147         assertEquals( 1, response.getTotalHits() );
148
149         Set<ArtifactInfo> results = response.getResults();
150
151         ArtifactInfo artifactInfo = (ArtifactInfo) results.iterator().next();
152         assertEquals( "org.apache.archiva", artifactInfo.groupId );
153         assertEquals( "archiva-index-methods-jar-test", artifactInfo.artifactId );
154         assertEquals( "test-repo", artifactInfo.repository );
155
156         context.close( true );
157     }
158
159     public void testUpdateArtifactInIndex()
160         throws Exception
161     {
162         File artifactFile =
163             new File( repositoryConfig.getLocation(),
164                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
165
166         ArtifactIndexingTask task =
167             TaskCreator.createIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD, context );
168
169         indexingExecutor.executeTask( task );
170         indexingExecutor.executeTask( task );
171
172         BooleanQuery q = new BooleanQuery();
173         q.add( indexer.constructQuery( ArtifactInfo.GROUP_ID, "org.apache.archiva" ), Occur.SHOULD );
174         q.add( indexer.constructQuery( ArtifactInfo.ARTIFACT_ID, "archiva-index-methods-jar-test" ), Occur.SHOULD );
175
176         IndexSearcher searcher = new IndexSearcher( repositoryConfig.getLocation() + "/.indexer" );
177         TopDocs topDocs = searcher.search( q, null, 10 );
178
179         searcher.close();
180
181         assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
182         assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
183
184         // should only return 1 hit!
185         assertEquals( 1, topDocs.totalHits );
186     }
187
188     public void testRemoveArtifactFromIndex()
189         throws Exception
190     {
191         File artifactFile =
192             new File( repositoryConfig.getLocation(),
193                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
194
195         ArtifactIndexingTask task =
196             TaskCreator.createIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD, context );
197
198         // remove artifact from index
199         indexingExecutor.executeTask( task );
200
201         BooleanQuery q = new BooleanQuery();
202         q.add( indexer.constructQuery( ArtifactInfo.GROUP_ID, "org.apache.archiva" ), Occur.SHOULD );
203         q.add( indexer.constructQuery( ArtifactInfo.ARTIFACT_ID, "archiva-index-methods-jar-test" ), Occur.SHOULD );
204
205         IndexSearcher searcher = new IndexSearcher( repositoryConfig.getLocation() + "/.indexer" );
206         TopDocs topDocs = searcher.search( q, null, 10 );
207
208         assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
209         assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
210
211         // should return 1 hit
212         assertEquals( 1, topDocs.totalHits );
213
214         searcher.close();
215
216         context = TaskCreator.createContext( repositoryConfig );
217
218         // remove added artifact from index
219         task =
220             TaskCreator.createIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.DELETE, context );
221         indexingExecutor.executeTask( task );
222
223         task =
224             TaskCreator.createIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.FINISH, context );
225         indexingExecutor.executeTask( task );
226
227         q = new BooleanQuery();
228         q.add( indexer.constructQuery( ArtifactInfo.GROUP_ID, "org.apache.archiva" ), Occur.SHOULD );
229         q.add( indexer.constructQuery( ArtifactInfo.ARTIFACT_ID, "archiva-index-methods-jar-test" ), Occur.SHOULD );
230
231         searcher = new IndexSearcher( repositoryConfig.getLocation() + "/.indexer" );
232         topDocs = searcher.search( q, null, 10 );
233
234         assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
235         assertTrue( new File( repositoryConfig.getLocation(), ".index" ).exists() );
236
237         // artifact should have been removed from the index!
238         assertEquals( 0, topDocs.totalHits );
239
240         context.close( true );
241         searcher.close();
242         // TODO: test it was removed from the packaged index also
243     }
244
245     public void testPackagedIndex()
246         throws Exception
247     {
248         File artifactFile =
249             new File( repositoryConfig.getLocation(),
250                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
251
252         ArtifactIndexingTask task =
253             TaskCreator.createIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD, context );
254
255         indexingExecutor.executeTask( task );
256
257         task =
258             TaskCreator.createIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.FINISH, context );
259
260         indexingExecutor.executeTask( task );
261
262         assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
263         assertTrue( new File( repositoryConfig.getLocation(), ".index" ).exists() );
264
265         // unpack .zip index
266         File destDir = new File( repositoryConfig.getLocation(), ".index/tmp" );
267         unzipIndex( new File( repositoryConfig.getLocation(), ".index" ).getPath(), destDir.getPath() );
268
269         BooleanQuery q = new BooleanQuery();
270         q.add( indexer.constructQuery( ArtifactInfo.GROUP_ID, "org.apache.archiva" ), Occur.SHOULD );
271         q.add( indexer.constructQuery( ArtifactInfo.ARTIFACT_ID, "archiva-index-methods-jar-test" ), Occur.SHOULD );
272
273         IndexingContext context =
274             indexer.addIndexingContext( repositoryConfig.getId(), repositoryConfig.getId(),
275                                         new File( repositoryConfig.getLocation() ), destDir, null, null,
276                                         NexusIndexer.FULL_INDEX );
277         context.setSearchable( true );
278
279         FlatSearchRequest request = new FlatSearchRequest( q );
280         FlatSearchResponse response = indexer.searchFlat( request );
281
282         assertEquals( 1, response.getTotalHits() );
283
284         Set<ArtifactInfo> results = response.getResults();
285
286         ArtifactInfo artifactInfo = (ArtifactInfo) results.iterator().next();
287         assertEquals( "org.apache.archiva", artifactInfo.groupId );
288         assertEquals( "archiva-index-methods-jar-test", artifactInfo.artifactId );
289         assertEquals( "test-repo", artifactInfo.repository );
290
291         context.close( true );
292     }
293
294     private void unzipIndex( String indexDir, String destDir )
295         throws FileNotFoundException, IOException
296     {
297         final int buff = 2048;
298
299         new File( destDir ).mkdirs();
300
301         BufferedOutputStream out = null;
302         FileInputStream fin = new FileInputStream( new File( indexDir, "nexus-maven-repository-index.zip" ) );
303         ZipInputStream in = new ZipInputStream( new BufferedInputStream( fin ) );
304         ZipEntry entry;
305
306         while ( ( entry = in.getNextEntry() ) != null )
307         {
308             int count;
309             byte data[] = new byte[buff];
310             FileOutputStream fout = new FileOutputStream( new File( destDir, entry.getName() ) );
311             out = new BufferedOutputStream( fout, buff );
312
313             while ( ( count = in.read( data, 0, buff ) ) != -1 )
314             {
315                 out.write( data, 0, count );
316             }
317             out.flush();
318             out.close();
319         }
320
321         in.close();
322     }
323 }