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