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.managed.ManagedRepositoryAdmin;
24 import org.apache.archiva.common.utils.PathUtil;
25 import org.apache.archiva.indexer.ArchivaIndexingContext;
26 import org.apache.archiva.indexer.UnsupportedBaseContextException;
27 import org.apache.archiva.repository.*;
28 import org.apache.archiva.repository.features.ArtifactCleanupFeature;
29 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
30 import org.apache.maven.index.ArtifactInfo;
31 import org.apache.maven.index.FlatSearchRequest;
32 import org.apache.maven.index.FlatSearchResponse;
33 import org.apache.maven.index.MAVEN;
34 import org.apache.maven.index.NexusIndexer;
35 import org.apache.maven.index.context.IndexCreator;
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.apache.maven.index.updater.DefaultIndexUpdater;
40 import org.apache.maven.index.updater.IndexUpdateRequest;
41 import org.apache.maven.index.updater.IndexUpdater;
42 import org.apache.maven.index_shaded.lucene.search.BooleanClause;
43 import org.apache.maven.index_shaded.lucene.search.BooleanQuery;
44 import org.apache.maven.index_shaded.lucene.search.IndexSearcher;
45 import org.apache.maven.index_shaded.lucene.search.TopDocs;
46 import org.assertj.core.api.Assertions;
47 import org.junit.After;
48 import org.junit.Before;
49 import org.junit.Test;
50 import org.junit.runner.RunWith;
51 import org.springframework.test.context.ContextConfiguration;
53 import javax.inject.Inject;
54 import java.io.IOException;
55 import java.nio.file.Files;
56 import java.nio.file.Path;
57 import java.nio.file.Paths;
58 import java.util.List;
59 import java.util.Locale;
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;
74 private BasicManagedRepository repositoryConfig;
77 private NexusIndexer indexer;
80 RepositoryRegistry repositoryRegistry;
83 private IndexUpdater indexUpdater;
92 Path baseDir = Paths.get(System.getProperty("basedir"), "target/test-classes").toAbsolutePath();
93 repositoryConfig = new BasicManagedRepository( "test-repo", "Test Repository", baseDir);
94 Path repoLocation = baseDir.resolve("test-repo" );
95 repositoryConfig.setLocation(repoLocation.toUri() );
96 repositoryConfig.setLayout( "default" );
97 repositoryConfig.setScanned( true );
98 repositoryConfig.addActiveReleaseScheme( ReleaseScheme.RELEASE );
99 repositoryConfig.removeActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
100 repositoryRegistry.putRepository(repositoryConfig);
105 public void tearDown()
109 for ( IndexingContext indexingContext : indexer.getIndexingContexts().values() )
111 indexer.removeIndexingContext( indexingContext, true );
113 repositoryRegistry.destroy();
115 removeIndexingContext with true cleanup files.
116 // delete created index in the repository
117 File indexDir = new File( repositoryConfig.getLocation(), ".indexer" );
118 FileUtils.deleteDirectory( indexDir );
119 assertFalse( indexDir.exists() );
121 indexDir = new File( repositoryConfig.getLocation(), ".index" );
122 FileUtils.deleteDirectory( indexDir );
123 assertFalse( indexDir.exists() );
128 protected IndexingContext getIndexingContext() throws UnsupportedBaseContextException {
129 Repository repo = repositoryRegistry.getRepository(repositoryConfig.getId());
131 ArchivaIndexingContext ctx = repo.getIndexingContext();
133 return ctx.getBaseContext(IndexingContext.class);
137 public void testAddArtifactToIndex()
140 Path basePath = PathUtil.getPathFromUri( repositoryConfig.getLocation() );
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 ManagedRepository repo = repositoryRegistry.getManagedRepository(repositoryConfig.getId());
145 ArtifactIndexingTask task =
146 new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
147 repo.getIndexingContext());
149 indexingExecutor.executeTask( task );
151 Map<String, IndexingContext> ctxs = indexer.getIndexingContexts();
152 BooleanQuery q = new BooleanQuery();
153 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
154 BooleanClause.Occur.SHOULD );
156 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
157 BooleanClause.Occur.SHOULD );
159 FlatSearchRequest request = new FlatSearchRequest( q );
160 FlatSearchResponse response = indexer.searchFlat( request );
162 assertTrue( Files.exists(basePath.resolve( ".indexer" )) );
163 assertFalse( Files.exists(basePath.resolve(".index" )) );
164 assertEquals( 1, response.getTotalHits() );
166 Set<ArtifactInfo> results = response.getResults();
168 ArtifactInfo artifactInfo = results.iterator().next();
169 assertEquals( "org.apache.archiva", artifactInfo.getGroupId() );
170 assertEquals( "archiva-index-methods-jar-test", artifactInfo.getArtifactId() );
171 assertEquals( "test-repo", artifactInfo.getRepository() );
176 public void testUpdateArtifactInIndex()
179 Path basePath = PathUtil.getPathFromUri( repositoryConfig.getLocation( ) );
180 Path artifactFile = basePath.resolve(
181 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
183 ManagedRepository repo = repositoryRegistry.getManagedRepository(repositoryConfig.getId());
184 ArtifactIndexingTask task =
185 new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
186 repo.getIndexingContext() );
188 indexingExecutor.executeTask( task );
189 indexingExecutor.executeTask( task );
191 BooleanQuery q = new BooleanQuery();
192 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
193 BooleanClause.Occur.SHOULD );
195 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
196 BooleanClause.Occur.SHOULD );
198 IndexingContext ctx = getIndexingContext();
200 IndexSearcher searcher = ctx.acquireIndexSearcher();
201 TopDocs topDocs = searcher.search( q, null, 10 );
204 ctx.releaseIndexSearcher( searcher );
206 assertTrue( Files.exists(basePath.resolve(".indexer" )) );
207 assertFalse( Files.exists(basePath.resolve(".index" )) );
209 // should only return 1 hit!
210 assertEquals( 1, topDocs.totalHits );
214 public void testRemoveArtifactFromIndex()
217 Path basePath = PathUtil.getPathFromUri( repositoryConfig.getLocation( ) );
218 Path artifactFile = basePath.resolve(
219 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
221 ManagedRepository repo = repositoryRegistry.getManagedRepository(repositoryConfig.getId());
222 ArtifactIndexingTask task =
223 new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
224 repo.getIndexingContext() );
226 // add artifact to index
227 indexingExecutor.executeTask( task );
229 BooleanQuery q = new BooleanQuery();
230 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
231 BooleanClause.Occur.SHOULD );
233 // indexer.constructQuery( MAVEN.ARTIFACT_ID, new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
236 FlatSearchRequest flatSearchRequest =
237 new FlatSearchRequest( q, indexer.getIndexingContexts().get( repositoryConfig.getId() ) );
239 FlatSearchResponse response = indexer.searchFlat( flatSearchRequest );
241 assertTrue( Files.exists(basePath.resolve(".indexer" )) );
242 assertFalse( Files.exists(basePath.resolve( ".index" )) );
244 // should return 1 hit
245 assertEquals( 1, response.getTotalHitsCount() );
247 // remove added artifact from index
248 task = new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.DELETE,
249 repo.getIndexingContext());
250 indexingExecutor.executeTask( task );
252 task = new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.FINISH,
253 repo.getIndexingContext() );
254 indexingExecutor.executeTask( task );
256 q = new BooleanQuery();
257 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
258 BooleanClause.Occur.SHOULD );
259 q.add( indexer.constructQuery( MAVEN.ARTIFACT_ID,
260 new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
261 BooleanClause.Occur.SHOULD );
263 assertTrue( Files.exists(basePath.resolve( ".indexer" )) );
264 assertFalse( Files.exists(basePath.resolve(".index" )) );
266 flatSearchRequest = new FlatSearchRequest( q, getIndexingContext() );
268 response = indexer.searchFlat( flatSearchRequest );
269 // artifact should have been removed from the index!
270 assertEquals( 0, response.getTotalHitsCount() );//.totalHits );
272 // TODO: test it was removed from the packaged index also
276 public void testPackagedIndex()
280 Path basePath = PathUtil.getPathFromUri( repositoryConfig.getLocation());
281 Path indexerDirectory =basePath.resolve( ".indexer" );
283 Files.list(indexerDirectory).filter( path -> path.getFileName().toString().startsWith("nexus-maven-repository-index") )
288 Files.delete( path );
290 catch ( IOException e )
292 e.printStackTrace( );
297 Path artifactFile = basePath.resolve(
298 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
299 ManagedRepository repo = repositoryRegistry.getManagedRepository(repositoryConfig.getId());
300 ArtifactIndexingTask task =
301 new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
302 repo.getIndexingContext() );
303 task.setExecuteOnEntireRepo( false );
305 indexingExecutor.executeTask( task );
307 task = new ArtifactIndexingTask( repositoryConfig, null, ArtifactIndexingTask.Action.FINISH,
308 repo.getIndexingContext() );
310 task.setExecuteOnEntireRepo( false );
312 indexingExecutor.executeTask( task );
314 assertTrue( Files.exists(indexerDirectory) );
316 // test packed index file creation
318 //Assertions.assertThat(new File( indexerDirectory, "nexus-maven-repository-index.zip" )).exists();
319 Assertions.assertThat( Files.exists(indexerDirectory.resolve("nexus-maven-repository-index.properties" ) ));
320 Assertions.assertThat( Files.exists(indexerDirectory.resolve("nexus-maven-repository-index.gz" ) ));
323 Path destDir = basePath.resolve( ".indexer/tmp" );
324 //unzipIndex( indexerDirectory.getPath(), destDir.getPath() );
326 DefaultIndexUpdater.FileFetcher fetcher = new DefaultIndexUpdater.FileFetcher( indexerDirectory.toFile() );
327 IndexUpdateRequest updateRequest = new IndexUpdateRequest( getIndexingContext(), fetcher );
328 //updateRequest.setLocalIndexCacheDir( indexerDirectory );
329 indexUpdater.fetchAndUpdateIndex( updateRequest );
331 BooleanQuery q = new BooleanQuery();
332 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
333 BooleanClause.Occur.SHOULD );
335 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
336 BooleanClause.Occur.SHOULD );
338 FlatSearchRequest request = new FlatSearchRequest( q, getIndexingContext() );
339 FlatSearchResponse response = indexer.searchFlat( request );
341 Set<ArtifactInfo> results = response.getResults();
343 ArtifactInfo artifactInfo = results.iterator().next();
344 assertEquals( "org.apache.archiva", artifactInfo.getGroupId() );
345 assertEquals( "archiva-index-methods-jar-test", artifactInfo.getArtifactId() );
346 assertEquals( "test-repo", artifactInfo.getRepository() );
348 assertEquals( 1, response.getTotalHits() );