1 package org.apache.maven.archiva.scheduled.executors;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import java.io.BufferedInputStream;
23 import java.io.BufferedOutputStream;
25 import java.io.FileInputStream;
26 import java.io.FileNotFoundException;
27 import java.io.FileOutputStream;
28 import java.io.IOException;
30 import java.util.zip.ZipEntry;
31 import java.util.zip.ZipInputStream;
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;
52 * ArchivaIndexingTaskExecutorTest
54 public class ArchivaIndexingTaskExecutorTest
55 extends PlexusInSpringTestCase
57 private ArchivaIndexingTaskExecutor indexingExecutor;
59 private IndexerEngine indexerEngine;
61 private IndexPacker indexPacker;
63 private ManagedRepositoryConfiguration repositoryConfig;
65 private Configuration configuration;
67 private NexusIndexer indexer;
69 private IndexingContext context;
71 protected void setUp()
76 indexingExecutor = new ArchivaIndexingTaskExecutor();
77 indexingExecutor.initialize();
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 );
88 configuration = new Configuration();
89 configuration.addManagedRepository( repositoryConfig );
91 indexer = (NexusIndexer) lookup( NexusIndexer.class );
92 indexerEngine = (IndexerEngine) lookup( IndexerEngine.class );
93 indexPacker = (IndexPacker) lookup( IndexPacker.class );
95 indexingExecutor.setIndexerEngine( indexerEngine );
96 indexingExecutor.setIndexPacker( indexPacker );
98 context = TaskCreator.createContext( repositoryConfig );
101 protected void tearDown()
104 context.close( true );
105 indexer.removeIndexingContext( context, true );
107 // delete created index in the repository
108 File indexDir = new File( repositoryConfig.getLocation(), ".indexer" );
109 FileUtils.deleteDirectory( indexDir );
110 assertFalse( indexDir.exists() );
112 indexDir = new File( repositoryConfig.getLocation(), ".index" );
113 FileUtils.deleteDirectory( indexDir );
114 assertFalse( indexDir.exists() );
119 public void testAddArtifactToIndex()
123 new File( repositoryConfig.getLocation(),
124 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
126 ArtifactIndexingTask task =
127 TaskCreator.createIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD, context );
129 indexingExecutor.executeTask( task );
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 );
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 );
142 FlatSearchRequest request = new FlatSearchRequest( q );
143 FlatSearchResponse response = indexer.searchFlat( request );
145 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
146 assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
147 assertEquals( 1, response.getTotalHits() );
149 Set<ArtifactInfo> results = response.getResults();
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 );
156 context.close( true );
159 public void testUpdateArtifactInIndex()
163 new File( repositoryConfig.getLocation(),
164 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
166 ArtifactIndexingTask task =
167 TaskCreator.createIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD, context );
169 indexingExecutor.executeTask( task );
170 indexingExecutor.executeTask( task );
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 );
176 IndexSearcher searcher = new IndexSearcher( repositoryConfig.getLocation() + "/.indexer" );
177 TopDocs topDocs = searcher.search( q, null, 10 );
181 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
182 assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
184 // should only return 1 hit!
185 assertEquals( 1, topDocs.totalHits );
188 public void testRemoveArtifactFromIndex()
192 new File( repositoryConfig.getLocation(),
193 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
195 ArtifactIndexingTask task =
196 TaskCreator.createIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD, context );
198 // remove artifact from index
199 indexingExecutor.executeTask( task );
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 );
205 IndexSearcher searcher = new IndexSearcher( repositoryConfig.getLocation() + "/.indexer" );
206 TopDocs topDocs = searcher.search( q, null, 10 );
208 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
209 assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
211 // should return 1 hit
212 assertEquals( 1, topDocs.totalHits );
216 context = TaskCreator.createContext( repositoryConfig );
218 // remove added artifact from index
220 TaskCreator.createIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.DELETE, context );
221 indexingExecutor.executeTask( task );
224 TaskCreator.createIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.FINISH, context );
225 indexingExecutor.executeTask( task );
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 );
231 searcher = new IndexSearcher( repositoryConfig.getLocation() + "/.indexer" );
232 topDocs = searcher.search( q, null, 10 );
234 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
235 assertTrue( new File( repositoryConfig.getLocation(), ".index" ).exists() );
237 // artifact should have been removed from the index!
238 assertEquals( 0, topDocs.totalHits );
240 context.close( true );
242 // TODO: test it was removed from the packaged index also
245 public void testPackagedIndex()
249 new File( repositoryConfig.getLocation(),
250 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
252 ArtifactIndexingTask task =
253 TaskCreator.createIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD, context );
255 indexingExecutor.executeTask( task );
258 TaskCreator.createIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.FINISH, context );
260 indexingExecutor.executeTask( task );
262 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
263 assertTrue( new File( repositoryConfig.getLocation(), ".index" ).exists() );
266 File destDir = new File( repositoryConfig.getLocation(), ".index/tmp" );
267 unzipIndex( new File( repositoryConfig.getLocation(), ".index" ).getPath(), destDir.getPath() );
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 );
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 );
279 FlatSearchRequest request = new FlatSearchRequest( q );
280 FlatSearchResponse response = indexer.searchFlat( request );
282 assertEquals( 1, response.getTotalHits() );
284 Set<ArtifactInfo> results = response.getResults();
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 );
291 context.close( true );
294 private void unzipIndex( String indexDir, String destDir )
295 throws FileNotFoundException, IOException
297 final int buff = 2048;
299 new File( destDir ).mkdirs();
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 ) );
306 while ( ( entry = in.getNextEntry() ) != null )
309 byte data[] = new byte[buff];
310 FileOutputStream fout = new FileOutputStream( new File( destDir, entry.getName() ) );
311 out = new BufferedOutputStream( fout, buff );
313 while ( ( count = in.read( data, 0, buff ) ) != -1 )
315 out.write( data, 0, count );