View Javadoc

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