1 package org.apache.archiva.scheduler.indexing.maven;
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.indexer.ArchivaIndexingContext;
24 import org.apache.archiva.indexer.UnsupportedBaseContextException;
25 import org.apache.archiva.repository.ManagedRepository;
26 import org.apache.archiva.repository.ReleaseScheme;
27 import org.apache.archiva.repository.base.ArchivaRepositoryRegistry;
28 import org.apache.archiva.repository.base.RepositoryHandlerDependencies;
29 import org.apache.archiva.repository.base.managed.BasicManagedRepository;
30 import org.apache.archiva.repository.features.IndexCreationFeature;
31 import org.apache.archiva.repository.storage.StorageAsset;
32 import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask;
33 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
34 import org.apache.maven.index.ArtifactInfo;
35 import org.apache.maven.index.FlatSearchRequest;
36 import org.apache.maven.index.FlatSearchResponse;
37 import org.apache.maven.index.Indexer;
38 import org.apache.maven.index.MAVEN;
39 import org.apache.maven.index.context.IndexingContext;
40 import org.apache.maven.index.expr.SourcedSearchExpression;
41 import org.apache.maven.index.expr.StringSearchExpression;
42 import org.apache.maven.index.updater.DefaultIndexUpdater;
43 import org.apache.maven.index.updater.IndexUpdateRequest;
44 import org.apache.maven.index.updater.IndexUpdater;
45 import org.apache.maven.index_shaded.lucene.search.BooleanClause;
46 import org.apache.maven.index_shaded.lucene.search.BooleanQuery;
47 import org.apache.maven.index_shaded.lucene.search.IndexSearcher;
48 import org.apache.maven.index_shaded.lucene.search.TopDocs;
49 import org.assertj.core.api.Assertions;
50 import org.junit.After;
51 import org.junit.Before;
52 import org.junit.Test;
53 import org.junit.runner.RunWith;
54 import org.springframework.test.context.ContextConfiguration;
56 import javax.inject.Inject;
57 import java.io.IOException;
58 import java.nio.file.Files;
59 import java.nio.file.Path;
60 import java.nio.file.Paths;
64 * ArchivaIndexingTaskExecutorTest
66 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
67 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
68 public class ArchivaIndexingTaskExecutorTest
72 private ArchivaIndexingTaskExecutor indexingExecutor;
75 ArchivaRepositoryRegistry repositoryRegistry;
77 @SuppressWarnings( "unused" )
79 RepositoryHandlerDependencies repositoryHandlerDependencies;
82 private IndexUpdater indexUpdater;
84 private ManagedRepository repo;
87 private Indexer indexer;
96 Path baseDir = Paths.get(System.getProperty("basedir"), "target/test-classes").toAbsolutePath();
97 BasicManagedRepository repositoryConfig = BasicManagedRepository.newFilesystemInstance("test-repo", "Test Repository", baseDir.resolve("test-repo"));
98 Path repoLocation = baseDir.resolve("test-repo" );
99 repositoryConfig.setLocation(repoLocation.toUri() );
100 repositoryConfig.setLayout( "default" );
101 repositoryConfig.setScanned( true );
102 repositoryConfig.addActiveReleaseScheme( ReleaseScheme.RELEASE );
103 repositoryConfig.removeActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
104 repositoryRegistry.putRepository(repositoryConfig);
105 repo = repositoryRegistry.getManagedRepository( repositoryConfig.getId() );
110 public void tearDown()
114 repositoryRegistry.destroy();
116 removeIndexingContext with true cleanup files.
117 // delete created index in the repository
118 File indexDir = new File( repositoryConfig.getLocation(), ".indexer" );
119 FileUtils.deleteDirectory( indexDir );
120 assertFalse( indexDir.exists() );
122 indexDir = new File( repositoryConfig.getLocation(), ".index" );
123 FileUtils.deleteDirectory( indexDir );
124 assertFalse( indexDir.exists() );
129 protected IndexingContext getIndexingContext() throws UnsupportedBaseContextException {
131 ArchivaIndexingContext ctx = repo.getIndexingContext();
133 return ctx.getBaseContext(IndexingContext.class);
137 public void testAddArtifactToIndex()
140 Path basePath = repo.getRoot().getFilePath();
141 Path artifactFile = basePath.resolve(
142 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
144 ArtifactIndexingTask task =
145 new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.ADD,
146 repo.getIndexingContext());
148 indexingExecutor.executeTask( task );
150 task = new ArtifactIndexingTask( repo, null, ArtifactIndexingTask.Action.FINISH,
151 repo.getIndexingContext() );
152 indexingExecutor.executeTask( task );
154 BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder( );
155 queryBuilder.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
156 BooleanClause.Occur.SHOULD );
158 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
159 BooleanClause.Occur.SHOULD );
160 BooleanQuery q = queryBuilder.build();
162 FlatSearchRequest request = new FlatSearchRequest( q , getIndexingContext());
163 FlatSearchResponse response = indexer.searchFlat( request );
165 assertTrue( Files.exists(basePath.resolve( ".indexer" )) );
166 assertTrue( Files.exists(basePath.resolve(".index" )) );
167 assertEquals( 1, response.getTotalHitsCount());
169 Set<ArtifactInfo> results = response.getResults();
171 ArtifactInfo artifactInfo = results.iterator().next();
172 assertEquals( "org.apache.archiva", artifactInfo.getGroupId() );
173 assertEquals( "archiva-index-methods-jar-test", artifactInfo.getArtifactId() );
174 assertEquals( "test-repo", artifactInfo.getRepository() );
179 public void testUpdateArtifactInIndex()
182 Path basePath = repo.getRoot().getFilePath();
183 Path artifactFile = basePath.resolve(
184 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
186 ArtifactIndexingTask task =
187 new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.ADD,
188 repo.getIndexingContext() );
190 indexingExecutor.executeTask( task );
191 indexingExecutor.executeTask( task );
193 BooleanQuery.Builder qb = new BooleanQuery.Builder();
194 qb.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
195 BooleanClause.Occur.SHOULD );
197 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
198 BooleanClause.Occur.SHOULD );
200 IndexingContext ctx = getIndexingContext();
202 IndexSearcher searcher = ctx.acquireIndexSearcher();
203 TopDocs topDocs = searcher.search( qb.build(), 10 );
206 ctx.releaseIndexSearcher( searcher );
208 assertTrue( Files.exists(basePath.resolve(".indexer" )) );
209 assertTrue( Files.exists(basePath.resolve(".index" )) );
211 // should only return 1 hit!
212 assertEquals( 1, topDocs.totalHits );
216 public void testRemoveArtifactFromIndex()
219 Path basePath = repo.getRoot().getFilePath();
220 Path artifactFile = basePath.resolve(
221 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
223 ArtifactIndexingTask task =
224 new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.ADD,
225 repo.getIndexingContext() );
227 // add artifact to index
228 indexingExecutor.executeTask( task );
230 BooleanQuery.Builder qb = new BooleanQuery.Builder();
231 qb.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
232 BooleanClause.Occur.SHOULD );
234 // indexer.constructQuery( MAVEN.ARTIFACT_ID, new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
237 IndexingContext ctx = repo.getIndexingContext( ).getBaseContext( IndexingContext.class );
238 FlatSearchRequest flatSearchRequest =
239 new FlatSearchRequest( qb.build(), ctx );
241 FlatSearchResponse response = indexer.searchFlat( flatSearchRequest );
243 assertTrue( Files.exists(basePath.resolve(".indexer" )) );
244 assertTrue( Files.exists(basePath.resolve( ".index" )) );
246 // should return 1 hit
247 assertEquals( 1, response.getTotalHitsCount() );
249 // remove added artifact from index
250 task = new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.DELETE,
251 repo.getIndexingContext());
252 indexingExecutor.executeTask( task );
254 task = new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.FINISH,
255 repo.getIndexingContext() );
256 indexingExecutor.executeTask( task );
258 qb = new BooleanQuery.Builder();
259 qb.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
260 BooleanClause.Occur.SHOULD );
261 qb.add( indexer.constructQuery( MAVEN.ARTIFACT_ID,
262 new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
263 BooleanClause.Occur.SHOULD );
265 assertTrue( Files.exists(basePath.resolve( ".indexer" )) );
266 assertTrue( Files.exists(basePath.resolve(".index" )) );
268 flatSearchRequest = new FlatSearchRequest( qb.build(), getIndexingContext() );
270 response = indexer.searchFlat( flatSearchRequest );
271 // artifact should have been removed from the index!
272 assertEquals( 0, response.getTotalHitsCount() );//.totalHits );
274 // TODO: test it was removed from the packaged index also
278 public void testPackagedIndex()
282 Path basePath = repo.getRoot().getFilePath();
283 IndexCreationFeature icf = repo.getFeature( IndexCreationFeature.class ).get();
284 StorageAsset packedIndexDirectory = icf.getLocalPackedIndexPath();
285 StorageAsset indexerDirectory = icf.getLocalIndexPath();
287 for (StorageAsset dir : new StorageAsset[] { packedIndexDirectory, indexerDirectory }) {
288 if (dir.getFilePath()!=null)
290 Path localDirPath = dir.getFilePath();
291 Files.list( localDirPath ).filter( path -> path.getFileName( ).toString( ).startsWith( "nexus-maven-repository-index" ) )
296 System.err.println( "Deleting " + path );
297 Files.delete( path );
299 catch ( IOException e )
301 e.printStackTrace( );
310 Path artifactFile = basePath.resolve(
311 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
312 ArtifactIndexingTask task =
313 new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.ADD,
314 repo.getIndexingContext() );
315 task.setExecuteOnEntireRepo( false );
317 indexingExecutor.executeTask( task );
319 task = new ArtifactIndexingTask( repo, null, ArtifactIndexingTask.Action.FINISH,
320 repo.getIndexingContext() );
322 task.setExecuteOnEntireRepo( false );
324 indexingExecutor.executeTask( task );
326 assertTrue( Files.exists(packedIndexDirectory.getFilePath()) );
327 assertTrue( Files.exists(indexerDirectory.getFilePath()) );
329 // test packed index file creation
331 //Assertions.assertThat(new File( indexerDirectory, "nexus-maven-repository-index.zip" )).exists();
332 Assertions.assertThat( Files.exists(packedIndexDirectory.getFilePath().resolve("nexus-maven-repository-index.properties" ) ));
333 Assertions.assertThat( Files.exists(packedIndexDirectory.getFilePath().resolve("nexus-maven-repository-index.gz" ) ));
334 assertFalse( Files.exists(packedIndexDirectory.getFilePath().resolve("nexus-maven-repository-index.1.gz" ) ));
337 //unzipIndex( indexerDirectory.getPath(), destDir.getPath() );
339 DefaultIndexUpdater.FileFetcher fetcher = new DefaultIndexUpdater.FileFetcher( packedIndexDirectory.getFilePath().toFile() );
340 IndexUpdateRequest updateRequest = new IndexUpdateRequest( getIndexingContext(), fetcher );
341 //updateRequest.setLocalIndexCacheDir( indexerDirectory );
342 indexUpdater.fetchAndUpdateIndex( updateRequest );
344 BooleanQuery.Builder qb = new BooleanQuery.Builder();
345 qb.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
346 BooleanClause.Occur.SHOULD );
348 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
349 BooleanClause.Occur.SHOULD );
351 FlatSearchRequest request = new FlatSearchRequest( qb.build(), getIndexingContext() );
352 FlatSearchResponse response = indexer.searchFlat( request );
354 assertEquals( 1, response.getTotalHitsCount() );
355 Set<ArtifactInfo> results = response.getResults();
357 ArtifactInfo artifactInfo = results.iterator().next();
358 assertEquals( "org.apache.archiva", artifactInfo.getGroupId() );
359 assertEquals( "archiva-index-methods-jar-test", artifactInfo.getArtifactId() );
360 assertEquals( "test-repo", artifactInfo.getRepository() );