1 package org.apache.archiva.scheduler.indexing;
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 junit.framework.TestCase;
23 import org.apache.archiva.admin.model.beans.ManagedRepository;
24 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
25 import org.apache.archiva.common.plexusbridge.MavenIndexerUtils;
26 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
27 import org.apache.commons.io.FileUtils;
28 import org.apache.lucene.search.BooleanClause.Occur;
29 import org.apache.lucene.search.BooleanQuery;
30 import org.apache.lucene.search.IndexSearcher;
31 import org.apache.lucene.search.TopDocs;
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;
47 import javax.inject.Inject;
48 import java.io.BufferedInputStream;
49 import java.io.BufferedOutputStream;
51 import java.io.FileInputStream;
52 import java.io.FileNotFoundException;
53 import java.io.FileOutputStream;
54 import java.io.IOException;
56 import java.util.zip.ZipEntry;
57 import java.util.zip.ZipInputStream;
60 * ArchivaIndexingTaskExecutorTest
62 @RunWith( SpringJUnit4ClassRunner.class )
63 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
64 public class ArchivaIndexingTaskExecutorTest
68 private ArchivaIndexingTaskExecutor indexingExecutor;
70 private ManagedRepository repositoryConfig;
72 private NexusIndexer indexer;
75 PlexusSisuBridge plexusSisuBridge;
78 MavenIndexerUtils mavenIndexerUtils;
81 ManagedRepositoryAdmin managedRepositoryAdmin;
89 repositoryConfig = new ManagedRepository();
90 repositoryConfig.setId( "test-repo" );
91 repositoryConfig.setLocation( "target/test-classes/test-repo" );
92 repositoryConfig.setLayout( "default" );
93 repositoryConfig.setName( "Test Repository" );
94 repositoryConfig.setScanned( true );
95 repositoryConfig.setSnapshots( false );
96 repositoryConfig.setReleases( true );
98 indexer = plexusSisuBridge.lookup( NexusIndexer.class );
100 managedRepositoryAdmin.createIndexContext( repositoryConfig );
104 public void tearDown()
108 for ( IndexingContext indexingContext : indexer.getIndexingContexts().values() )
110 indexer.removeIndexingContext( indexingContext, true );
113 // delete created index in the repository
114 File indexDir = new File( repositoryConfig.getLocation(), ".indexer" );
115 FileUtils.deleteDirectory( indexDir );
116 assertFalse( indexDir.exists() );
118 indexDir = new File( repositoryConfig.getLocation(), ".index" );
119 FileUtils.deleteDirectory( indexDir );
120 assertFalse( indexDir.exists() );
125 protected IndexingContext getIndexingContext()
127 return indexer.getIndexingContexts().get( repositoryConfig.getId() );
131 public void testAddArtifactToIndex()
134 File artifactFile = new File( repositoryConfig.getLocation(),
135 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
137 ArtifactIndexingTask task =
138 new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
139 getIndexingContext() );
141 indexingExecutor.executeTask( task );
143 BooleanQuery q = new BooleanQuery();
144 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
147 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
150 if ( !indexer.getIndexingContexts().containsKey( repositoryConfig.getId() ) )
152 IndexingContext context = indexer.addIndexingContext( repositoryConfig.getId(), repositoryConfig.getId(),
153 new File( repositoryConfig.getLocation() ),
154 new File( repositoryConfig.getLocation(),
155 ".indexer" ), null, null,
156 mavenIndexerUtils.getAllIndexCreators() );
157 context.setSearchable( true );
160 FlatSearchRequest request = new FlatSearchRequest( q );
161 FlatSearchResponse response = indexer.searchFlat( request );
163 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
164 assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
165 assertEquals( 1, response.getTotalHits() );
167 Set<ArtifactInfo> results = response.getResults();
169 ArtifactInfo artifactInfo = results.iterator().next();
170 assertEquals( "org.apache.archiva", artifactInfo.groupId );
171 assertEquals( "archiva-index-methods-jar-test", artifactInfo.artifactId );
172 assertEquals( "test-repo", artifactInfo.repository );
177 public void testUpdateArtifactInIndex()
180 File artifactFile = new File( repositoryConfig.getLocation(),
181 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
183 ArtifactIndexingTask task =
184 new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
185 getIndexingContext() );
187 indexingExecutor.executeTask( task );
188 indexingExecutor.executeTask( task );
190 BooleanQuery q = new BooleanQuery();
191 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
194 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
197 IndexSearcher searcher = indexer.getIndexingContexts().get( repositoryConfig.getId() ).getIndexSearcher();
198 TopDocs topDocs = searcher.search( q, null, 10 );
202 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
203 assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
205 // should only return 1 hit!
206 assertEquals( 1, topDocs.totalHits );
210 public void testRemoveArtifactFromIndex()
213 File artifactFile = new File( repositoryConfig.getLocation(),
214 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
216 ArtifactIndexingTask task =
217 new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
218 getIndexingContext() );
220 // add artifact to index
221 indexingExecutor.executeTask( task );
223 BooleanQuery q = new BooleanQuery();
224 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
227 // indexer.constructQuery( MAVEN.ARTIFACT_ID, new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
230 FlatSearchRequest flatSearchRequest =
231 new FlatSearchRequest( q, indexer.getIndexingContexts().get( repositoryConfig.getId() ) );
233 FlatSearchResponse response = indexer.searchFlat( flatSearchRequest );
235 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
236 assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
238 // should return 1 hit
239 assertEquals( 1, response.getTotalHitsCount() );
241 // remove added artifact from index
242 task = new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.DELETE,
243 getIndexingContext() );
244 indexingExecutor.executeTask( task );
246 task = new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.FINISH,
247 getIndexingContext() );
248 indexingExecutor.executeTask( task );
250 q = new BooleanQuery();
251 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
253 q.add( indexer.constructQuery( MAVEN.ARTIFACT_ID,
254 new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
257 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
258 assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
260 flatSearchRequest = new FlatSearchRequest( q, getIndexingContext() );
262 response = indexer.searchFlat( flatSearchRequest );
263 // artifact should have been removed from the index!
264 assertEquals( 0, response.getTotalHitsCount() );//.totalHits );
266 // TODO: test it was removed from the packaged index also
270 public void testPackagedIndex()
273 File artifactFile = new File( repositoryConfig.getLocation(),
274 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
276 ArtifactIndexingTask task =
277 new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
278 getIndexingContext() );
279 task.setExecuteOnEntireRepo( false );
281 indexingExecutor.executeTask( task );
283 task = new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.FINISH,
284 getIndexingContext() );
286 task.setExecuteOnEntireRepo( false );
288 indexingExecutor.executeTask( task );
290 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
293 File destDir = new File( repositoryConfig.getLocation(), ".indexer/tmp" );
294 unzipIndex( new File( repositoryConfig.getLocation(), ".indexer" ).getPath(), destDir.getPath() );
296 BooleanQuery q = new BooleanQuery();
297 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
300 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
303 FlatSearchRequest request = new FlatSearchRequest( q, getIndexingContext() );
304 FlatSearchResponse response = indexer.searchFlat( request );
306 Set<ArtifactInfo> results = response.getResults();
308 ArtifactInfo artifactInfo = results.iterator().next();
309 assertEquals( "org.apache.archiva", artifactInfo.groupId );
310 assertEquals( "archiva-index-methods-jar-test", artifactInfo.artifactId );
311 assertEquals( "test-repo", artifactInfo.repository );
313 assertEquals( 1, response.getTotalHits() );
316 private void unzipIndex( String indexDir, String destDir )
317 throws FileNotFoundException, IOException
319 final int buff = 2048;
321 new File( destDir ).mkdirs();
323 BufferedOutputStream out = null;
324 FileInputStream fin = new FileInputStream( new File( indexDir, "nexus-maven-repository-index.zip" ) );
325 ZipInputStream in = new ZipInputStream( new BufferedInputStream( fin ) );
328 while ( ( entry = in.getNextEntry() ) != null )
331 byte data[] = new byte[buff];
332 FileOutputStream fout = new FileOutputStream( new File( destDir, entry.getName() ) );
333 out = new BufferedOutputStream( fout, buff );
335 while ( ( count = in.read( data, 0, buff ) ) != -1 )
337 out.write( data, 0, count );