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.base.ArchivaRepositoryRegistry;
26 import org.apache.archiva.repository.base.managed.BasicManagedRepository;
27 import org.apache.archiva.repository.ManagedRepository;
28 import org.apache.archiva.repository.ReleaseScheme;
29 import org.apache.archiva.repository.base.group.RepositoryGroupHandler;
30 import org.apache.archiva.repository.base.managed.ManagedRepositoryHandler;
31 import org.apache.archiva.repository.storage.StorageAsset;
32 import org.apache.archiva.repository.features.IndexCreationFeature;
33 import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask;
34 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
35 import org.apache.maven.index.ArtifactInfo;
36 import org.apache.maven.index.FlatSearchRequest;
37 import org.apache.maven.index.FlatSearchResponse;
38 import org.apache.maven.index.Indexer;
39 import org.apache.maven.index.MAVEN;
40 import org.apache.maven.index.context.IndexingContext;
41 import org.apache.maven.index.expr.SourcedSearchExpression;
42 import org.apache.maven.index.expr.StringSearchExpression;
43 import org.apache.maven.index.updater.DefaultIndexUpdater;
44 import org.apache.maven.index.updater.IndexUpdateRequest;
45 import org.apache.maven.index.updater.IndexUpdater;
46 import org.apache.maven.index_shaded.lucene.search.BooleanClause;
47 import org.apache.maven.index_shaded.lucene.search.BooleanQuery;
48 import org.apache.maven.index_shaded.lucene.search.IndexSearcher;
49 import org.apache.maven.index_shaded.lucene.search.TopDocs;
50 import org.assertj.core.api.Assertions;
51 import org.junit.After;
52 import org.junit.Before;
53 import org.junit.Test;
54 import org.junit.runner.RunWith;
55 import org.springframework.test.context.ContextConfiguration;
57 import javax.inject.Inject;
58 import java.io.IOException;
59 import java.nio.file.Files;
60 import java.nio.file.Path;
61 import java.nio.file.Paths;
65 * ArchivaIndexingTaskExecutorTest
67 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
68 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
69 public class ArchivaIndexingTaskExecutorTest
73 private ArchivaIndexingTaskExecutor indexingExecutor;
76 ArchivaRepositoryRegistry repositoryRegistry;
78 @SuppressWarnings( "unused" )
80 ManagedRepositoryHandler managedRepositoryHandler;
82 @SuppressWarnings( "unused" )
84 RepositoryGroupHandler repositoryGroupHandler;
87 private IndexUpdater indexUpdater;
89 private ManagedRepository repo;
92 private Indexer indexer;
101 Path baseDir = Paths.get(System.getProperty("basedir"), "target/test-classes").toAbsolutePath();
102 BasicManagedRepository repositoryConfig = BasicManagedRepository.newFilesystemInstance("test-repo", "Test Repository", baseDir.resolve("test-repo"));
103 Path repoLocation = baseDir.resolve("test-repo" );
104 repositoryConfig.setLocation(repoLocation.toUri() );
105 repositoryConfig.setLayout( "default" );
106 repositoryConfig.setScanned( true );
107 repositoryConfig.addActiveReleaseScheme( ReleaseScheme.RELEASE );
108 repositoryConfig.removeActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
109 repositoryRegistry.putRepository(repositoryConfig);
110 repo = repositoryRegistry.getManagedRepository( repositoryConfig.getId() );
115 public void tearDown()
119 repositoryRegistry.destroy();
121 removeIndexingContext with true cleanup files.
122 // delete created index in the repository
123 File indexDir = new File( repositoryConfig.getLocation(), ".indexer" );
124 FileUtils.deleteDirectory( indexDir );
125 assertFalse( indexDir.exists() );
127 indexDir = new File( repositoryConfig.getLocation(), ".index" );
128 FileUtils.deleteDirectory( indexDir );
129 assertFalse( indexDir.exists() );
134 protected IndexingContext getIndexingContext() throws UnsupportedBaseContextException {
136 ArchivaIndexingContext ctx = repo.getIndexingContext();
138 return ctx.getBaseContext(IndexingContext.class);
142 public void testAddArtifactToIndex()
145 Path basePath = repo.getRoot().getFilePath();
146 Path artifactFile = basePath.resolve(
147 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
149 ArtifactIndexingTask task =
150 new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.ADD,
151 repo.getIndexingContext());
153 indexingExecutor.executeTask( task );
155 task = new ArtifactIndexingTask( repo, null, ArtifactIndexingTask.Action.FINISH,
156 repo.getIndexingContext() );
157 indexingExecutor.executeTask( task );
159 BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder( );
160 queryBuilder.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
161 BooleanClause.Occur.SHOULD );
163 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
164 BooleanClause.Occur.SHOULD );
165 BooleanQuery q = queryBuilder.build();
167 FlatSearchRequest request = new FlatSearchRequest( q , getIndexingContext());
168 FlatSearchResponse response = indexer.searchFlat( request );
170 assertTrue( Files.exists(basePath.resolve( ".indexer" )) );
171 assertTrue( Files.exists(basePath.resolve(".index" )) );
172 assertEquals( 1, response.getTotalHitsCount());
174 Set<ArtifactInfo> results = response.getResults();
176 ArtifactInfo artifactInfo = results.iterator().next();
177 assertEquals( "org.apache.archiva", artifactInfo.getGroupId() );
178 assertEquals( "archiva-index-methods-jar-test", artifactInfo.getArtifactId() );
179 assertEquals( "test-repo", artifactInfo.getRepository() );
184 public void testUpdateArtifactInIndex()
187 Path basePath = repo.getRoot().getFilePath();
188 Path artifactFile = basePath.resolve(
189 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
191 ArtifactIndexingTask task =
192 new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.ADD,
193 repo.getIndexingContext() );
195 indexingExecutor.executeTask( task );
196 indexingExecutor.executeTask( task );
198 BooleanQuery.Builder qb = new BooleanQuery.Builder();
199 qb.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
200 BooleanClause.Occur.SHOULD );
202 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
203 BooleanClause.Occur.SHOULD );
205 IndexingContext ctx = getIndexingContext();
207 IndexSearcher searcher = ctx.acquireIndexSearcher();
208 TopDocs topDocs = searcher.search( qb.build(), 10 );
211 ctx.releaseIndexSearcher( searcher );
213 assertTrue( Files.exists(basePath.resolve(".indexer" )) );
214 assertTrue( Files.exists(basePath.resolve(".index" )) );
216 // should only return 1 hit!
217 assertEquals( 1, topDocs.totalHits );
221 public void testRemoveArtifactFromIndex()
224 Path basePath = repo.getRoot().getFilePath();
225 Path artifactFile = basePath.resolve(
226 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
228 ArtifactIndexingTask task =
229 new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.ADD,
230 repo.getIndexingContext() );
232 // add artifact to index
233 indexingExecutor.executeTask( task );
235 BooleanQuery.Builder qb = new BooleanQuery.Builder();
236 qb.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
237 BooleanClause.Occur.SHOULD );
239 // indexer.constructQuery( MAVEN.ARTIFACT_ID, new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
242 IndexingContext ctx = repo.getIndexingContext( ).getBaseContext( IndexingContext.class );
243 FlatSearchRequest flatSearchRequest =
244 new FlatSearchRequest( qb.build(), ctx );
246 FlatSearchResponse response = indexer.searchFlat( flatSearchRequest );
248 assertTrue( Files.exists(basePath.resolve(".indexer" )) );
249 assertTrue( Files.exists(basePath.resolve( ".index" )) );
251 // should return 1 hit
252 assertEquals( 1, response.getTotalHitsCount() );
254 // remove added artifact from index
255 task = new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.DELETE,
256 repo.getIndexingContext());
257 indexingExecutor.executeTask( task );
259 task = new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.FINISH,
260 repo.getIndexingContext() );
261 indexingExecutor.executeTask( task );
263 qb = new BooleanQuery.Builder();
264 qb.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
265 BooleanClause.Occur.SHOULD );
266 qb.add( indexer.constructQuery( MAVEN.ARTIFACT_ID,
267 new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
268 BooleanClause.Occur.SHOULD );
270 assertTrue( Files.exists(basePath.resolve( ".indexer" )) );
271 assertTrue( Files.exists(basePath.resolve(".index" )) );
273 flatSearchRequest = new FlatSearchRequest( qb.build(), getIndexingContext() );
275 response = indexer.searchFlat( flatSearchRequest );
276 // artifact should have been removed from the index!
277 assertEquals( 0, response.getTotalHitsCount() );//.totalHits );
279 // TODO: test it was removed from the packaged index also
283 public void testPackagedIndex()
287 Path basePath = repo.getRoot().getFilePath();
288 IndexCreationFeature icf = repo.getFeature( IndexCreationFeature.class ).get();
289 StorageAsset packedIndexDirectory = icf.getLocalPackedIndexPath();
290 StorageAsset indexerDirectory = icf.getLocalIndexPath();
292 for (StorageAsset dir : new StorageAsset[] { packedIndexDirectory, indexerDirectory }) {
293 if (dir.getFilePath()!=null)
295 Path localDirPath = dir.getFilePath();
296 Files.list( localDirPath ).filter( path -> path.getFileName( ).toString( ).startsWith( "nexus-maven-repository-index" ) )
301 System.err.println( "Deleting " + path );
302 Files.delete( path );
304 catch ( IOException e )
306 e.printStackTrace( );
315 Path artifactFile = basePath.resolve(
316 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
317 ArtifactIndexingTask task =
318 new ArtifactIndexingTask( repo, artifactFile, ArtifactIndexingTask.Action.ADD,
319 repo.getIndexingContext() );
320 task.setExecuteOnEntireRepo( false );
322 indexingExecutor.executeTask( task );
324 task = new ArtifactIndexingTask( repo, null, ArtifactIndexingTask.Action.FINISH,
325 repo.getIndexingContext() );
327 task.setExecuteOnEntireRepo( false );
329 indexingExecutor.executeTask( task );
331 assertTrue( Files.exists(packedIndexDirectory.getFilePath()) );
332 assertTrue( Files.exists(indexerDirectory.getFilePath()) );
334 // test packed index file creation
336 //Assertions.assertThat(new File( indexerDirectory, "nexus-maven-repository-index.zip" )).exists();
337 Assertions.assertThat( Files.exists(packedIndexDirectory.getFilePath().resolve("nexus-maven-repository-index.properties" ) ));
338 Assertions.assertThat( Files.exists(packedIndexDirectory.getFilePath().resolve("nexus-maven-repository-index.gz" ) ));
339 assertFalse( Files.exists(packedIndexDirectory.getFilePath().resolve("nexus-maven-repository-index.1.gz" ) ));
342 //unzipIndex( indexerDirectory.getPath(), destDir.getPath() );
344 DefaultIndexUpdater.FileFetcher fetcher = new DefaultIndexUpdater.FileFetcher( packedIndexDirectory.getFilePath().toFile() );
345 IndexUpdateRequest updateRequest = new IndexUpdateRequest( getIndexingContext(), fetcher );
346 //updateRequest.setLocalIndexCacheDir( indexerDirectory );
347 indexUpdater.fetchAndUpdateIndex( updateRequest );
349 BooleanQuery.Builder qb = new BooleanQuery.Builder();
350 qb.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
351 BooleanClause.Occur.SHOULD );
353 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
354 BooleanClause.Occur.SHOULD );
356 FlatSearchRequest request = new FlatSearchRequest( qb.build(), getIndexingContext() );
357 FlatSearchResponse response = indexer.searchFlat( request );
359 assertEquals( 1, response.getTotalHitsCount() );
360 Set<ArtifactInfo> results = response.getResults();
362 ArtifactInfo artifactInfo = results.iterator().next();
363 assertEquals( "org.apache.archiva", artifactInfo.getGroupId() );
364 assertEquals( "archiva-index-methods-jar-test", artifactInfo.getArtifactId() );
365 assertEquals( "test-repo", artifactInfo.getRepository() );