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.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;
46 import javax.inject.Inject;
47 import java.io.BufferedInputStream;
48 import java.io.BufferedOutputStream;
50 import java.io.FileInputStream;
51 import java.io.FileNotFoundException;
52 import java.io.FileOutputStream;
53 import java.io.IOException;
55 import java.util.zip.ZipEntry;
56 import java.util.zip.ZipInputStream;
59 * ArchivaIndexingTaskExecutorTest
61 @RunWith( SpringJUnit4ClassRunner.class )
62 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
63 public class ArchivaIndexingTaskExecutorTest
67 private ArchivaIndexingTaskExecutor indexingExecutor;
69 private ManagedRepository repositoryConfig;
71 private NexusIndexer indexer;
74 PlexusSisuBridge plexusSisuBridge;
77 MavenIndexerUtils mavenIndexerUtils;
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 );
94 indexer = plexusSisuBridge.lookup( NexusIndexer.class );
96 ArtifactIndexingTask.createContext( repositoryConfig, indexer, mavenIndexerUtils.getAllIndexCreators() );
100 public void tearDown()
104 for ( IndexingContext indexingContext : indexer.getIndexingContexts().values() )
106 indexer.removeIndexingContext( indexingContext, true );
109 // delete created index in the repository
110 File indexDir = new File( repositoryConfig.getLocation(), ".indexer" );
111 FileUtils.deleteDirectory( indexDir );
112 assertFalse( indexDir.exists() );
114 indexDir = new File( repositoryConfig.getLocation(), ".index" );
115 FileUtils.deleteDirectory( indexDir );
116 assertFalse( indexDir.exists() );
121 protected IndexingContext getIndexingContext()
123 return indexer.getIndexingContexts().get( repositoryConfig.getId() );
127 public void testAddArtifactToIndex()
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" );
133 ArtifactIndexingTask task =
134 new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
135 getIndexingContext() );
137 indexingExecutor.executeTask( task );
139 BooleanQuery q = new BooleanQuery();
140 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
143 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
146 if ( !indexer.getIndexingContexts().containsKey( repositoryConfig.getId() ) )
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 );
156 FlatSearchRequest request = new FlatSearchRequest( q );
157 FlatSearchResponse response = indexer.searchFlat( request );
159 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
160 assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
161 assertEquals( 1, response.getTotalHits() );
163 Set<ArtifactInfo> results = response.getResults();
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 );
173 public void testUpdateArtifactInIndex()
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" );
179 ArtifactIndexingTask task =
180 new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
181 getIndexingContext() );
183 indexingExecutor.executeTask( task );
184 indexingExecutor.executeTask( task );
186 BooleanQuery q = new BooleanQuery();
187 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
190 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
193 IndexSearcher searcher = indexer.getIndexingContexts().get( repositoryConfig.getId() ).getIndexSearcher();
194 TopDocs topDocs = searcher.search( q, null, 10 );
198 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
199 assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
201 // should only return 1 hit!
202 assertEquals( 1, topDocs.totalHits );
206 public void testRemoveArtifactFromIndex()
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" );
212 ArtifactIndexingTask task =
213 new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
214 getIndexingContext() );
216 // add artifact to index
217 indexingExecutor.executeTask( task );
219 BooleanQuery q = new BooleanQuery();
220 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
223 // indexer.constructQuery( MAVEN.ARTIFACT_ID, new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
226 FlatSearchRequest flatSearchRequest =
227 new FlatSearchRequest( q, indexer.getIndexingContexts().get( repositoryConfig.getId() ) );
229 FlatSearchResponse response = indexer.searchFlat( flatSearchRequest );
231 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
232 assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
234 // should return 1 hit
235 assertEquals( 1, response.getTotalHitsCount() );
237 // remove added artifact from index
238 task = new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.DELETE,
239 getIndexingContext() );
240 indexingExecutor.executeTask( task );
242 task = new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.FINISH,
243 getIndexingContext() );
244 indexingExecutor.executeTask( task );
246 q = new BooleanQuery();
247 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
249 q.add( indexer.constructQuery( MAVEN.ARTIFACT_ID,
250 new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
253 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
254 assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
256 flatSearchRequest = new FlatSearchRequest( q, getIndexingContext() );
258 response = indexer.searchFlat( flatSearchRequest );
259 // artifact should have been removed from the index!
260 assertEquals( 0, response.getTotalHitsCount() );//.totalHits );
262 // TODO: test it was removed from the packaged index also
266 public void testPackagedIndex()
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" );
272 ArtifactIndexingTask task =
273 new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
274 getIndexingContext() );
275 task.setExecuteOnEntireRepo( false );
277 indexingExecutor.executeTask( task );
279 task = new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.FINISH,
280 getIndexingContext() );
282 task.setExecuteOnEntireRepo( false );
284 indexingExecutor.executeTask( task );
286 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
289 File destDir = new File( repositoryConfig.getLocation(), ".indexer/tmp" );
290 unzipIndex( new File( repositoryConfig.getLocation(), ".indexer" ).getPath(), destDir.getPath() );
292 BooleanQuery q = new BooleanQuery();
293 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
296 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
299 FlatSearchRequest request = new FlatSearchRequest( q, getIndexingContext() );
300 FlatSearchResponse response = indexer.searchFlat( request );
302 Set<ArtifactInfo> results = response.getResults();
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 );
309 assertEquals( 1, response.getTotalHits() );
312 private void unzipIndex( String indexDir, String destDir )
313 throws FileNotFoundException, IOException
315 final int buff = 2048;
317 new File( destDir ).mkdirs();
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 ) );
324 while ( ( entry = in.getNextEntry() ) != null )
327 byte data[] = new byte[buff];
328 FileOutputStream fout = new FileOutputStream( new File( destDir, entry.getName() ) );
329 out = new BufferedOutputStream( fout, buff );
331 while ( ( count = in.read( data, 0, buff ) ) != -1 )
333 out.write( data, 0, count );