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.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
28 import org.apache.maven.index.ArtifactInfo;
29 import org.apache.maven.index.FlatSearchRequest;
30 import org.apache.maven.index.FlatSearchResponse;
31 import org.apache.maven.index.MAVEN;
32 import org.apache.maven.index.NexusIndexer;
33 import org.apache.maven.index.context.IndexingContext;
34 import org.apache.maven.index.expr.SourcedSearchExpression;
35 import org.apache.maven.index.expr.StringSearchExpression;
36 import org.apache.maven.index.shaded.lucene.search.BooleanClause;
37 import org.apache.maven.index.shaded.lucene.search.BooleanQuery;
38 import org.apache.maven.index.shaded.lucene.search.IndexSearcher;
39 import org.apache.maven.index.shaded.lucene.search.TopDocs;
40 import org.apache.maven.index.updater.IndexUpdateRequest;
41 import org.assertj.core.api.Assertions;
42 import org.junit.After;
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.springframework.test.context.ContextConfiguration;
48 import javax.inject.Inject;
49 import java.io.BufferedInputStream;
50 import java.io.BufferedOutputStream;
52 import java.io.FilenameFilter;
53 import java.io.IOException;
54 import java.io.InputStream;
55 import java.io.OutputStream;
56 import java.nio.file.Files;
57 import java.nio.file.Paths;
59 import java.util.zip.GZIPInputStream;
60 import java.util.zip.ZipEntry;
61 import java.util.zip.ZipInputStream;
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 ManagedRepository repositoryConfig;
77 private NexusIndexer indexer;
80 MavenIndexerUtils mavenIndexerUtils;
83 ManagedRepositoryAdmin managedRepositoryAdmin;
92 repositoryConfig = new ManagedRepository();
93 repositoryConfig.setId( "test-repo" );
94 repositoryConfig.setLocation(
95 new File( System.getProperty( "basedir" ), "target/test-classes/test-repo" ).getAbsolutePath() );
96 repositoryConfig.setLayout( "default" );
97 repositoryConfig.setName( "Test Repository" );
98 repositoryConfig.setScanned( true );
99 repositoryConfig.setSnapshots( false );
100 repositoryConfig.setReleases( true );
102 managedRepositoryAdmin.createIndexContext( repositoryConfig );
107 public void tearDown()
111 for ( IndexingContext indexingContext : indexer.getIndexingContexts().values() )
113 indexer.removeIndexingContext( indexingContext, true );
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()
131 return indexer.getIndexingContexts().get( repositoryConfig.getId() );
135 public void testAddArtifactToIndex()
138 File artifactFile = new File( repositoryConfig.getLocation(),
139 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
141 ArtifactIndexingTask task =
142 new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
143 getIndexingContext() );
145 indexingExecutor.executeTask( task );
147 BooleanQuery q = new BooleanQuery();
148 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
149 BooleanClause.Occur.SHOULD );
151 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
152 BooleanClause.Occur.SHOULD );
154 if ( !indexer.getIndexingContexts().containsKey( repositoryConfig.getId() ) )
156 IndexingContext context = indexer.addIndexingContext( repositoryConfig.getId(), repositoryConfig.getId(),
157 new File( repositoryConfig.getLocation() ),
158 new File( repositoryConfig.getLocation(),
159 ".indexer" ), null, null,
160 mavenIndexerUtils.getAllIndexCreators()
162 context.setSearchable( true );
165 FlatSearchRequest request = new FlatSearchRequest( q );
166 FlatSearchResponse response = indexer.searchFlat( request );
168 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
169 assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
170 assertEquals( 1, response.getTotalHits() );
172 Set<ArtifactInfo> results = response.getResults();
174 ArtifactInfo artifactInfo = results.iterator().next();
175 assertEquals( "org.apache.archiva", artifactInfo.getGroupId() );
176 assertEquals( "archiva-index-methods-jar-test", artifactInfo.getArtifactId() );
177 assertEquals( "test-repo", artifactInfo.getRepository() );
182 public void testUpdateArtifactInIndex()
185 File artifactFile = new File( repositoryConfig.getLocation(),
186 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
188 ArtifactIndexingTask task =
189 new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
190 getIndexingContext() );
192 indexingExecutor.executeTask( task );
193 indexingExecutor.executeTask( task );
195 BooleanQuery q = new BooleanQuery();
196 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
197 BooleanClause.Occur.SHOULD );
199 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
200 BooleanClause.Occur.SHOULD );
202 IndexingContext ctx = indexer.getIndexingContexts().get( repositoryConfig.getId() );
204 IndexSearcher searcher = ctx.acquireIndexSearcher();
205 TopDocs topDocs = searcher.search( q, null, 10 );
208 ctx.releaseIndexSearcher( searcher );
210 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
211 assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
213 // should only return 1 hit!
214 assertEquals( 1, topDocs.totalHits );
218 public void testRemoveArtifactFromIndex()
221 File artifactFile = new File( repositoryConfig.getLocation(),
222 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
224 ArtifactIndexingTask task =
225 new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
226 getIndexingContext() );
228 // add artifact to index
229 indexingExecutor.executeTask( task );
231 BooleanQuery q = new BooleanQuery();
232 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
233 BooleanClause.Occur.SHOULD );
235 // indexer.constructQuery( MAVEN.ARTIFACT_ID, new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
238 FlatSearchRequest flatSearchRequest =
239 new FlatSearchRequest( q, indexer.getIndexingContexts().get( repositoryConfig.getId() ) );
241 FlatSearchResponse response = indexer.searchFlat( flatSearchRequest );
243 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
244 assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
246 // should return 1 hit
247 assertEquals( 1, response.getTotalHitsCount() );
249 // remove added artifact from index
250 task = new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.DELETE,
251 getIndexingContext() );
252 indexingExecutor.executeTask( task );
254 task = new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.FINISH,
255 getIndexingContext() );
256 indexingExecutor.executeTask( task );
258 q = new BooleanQuery();
259 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
260 BooleanClause.Occur.SHOULD );
261 q.add( indexer.constructQuery( MAVEN.ARTIFACT_ID,
262 new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
263 BooleanClause.Occur.SHOULD
266 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
267 assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
269 flatSearchRequest = new FlatSearchRequest( q, getIndexingContext() );
271 response = indexer.searchFlat( flatSearchRequest );
272 // artifact should have been removed from the index!
273 assertEquals( 0, response.getTotalHitsCount() );//.totalHits );
275 // TODO: test it was removed from the packaged index also
279 public void testPackagedIndex()
283 File indexerDirectory = new File( repositoryConfig.getLocation(), ".indexer" );
285 indexerDirectory.listFiles( new FilenameFilter()
288 public boolean accept( File file, String s )
290 if ( s.startsWith( "nexus-maven-repository-index" ) )
292 new File( file, s ).delete();
298 File artifactFile = new File( repositoryConfig.getLocation(),
299 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
301 ArtifactIndexingTask task =
302 new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
303 getIndexingContext() );
304 task.setExecuteOnEntireRepo( false );
306 indexingExecutor.executeTask( task );
308 task = new ArtifactIndexingTask( repositoryConfig, null, ArtifactIndexingTask.Action.FINISH,
309 getIndexingContext() );
311 task.setExecuteOnEntireRepo( false );
313 indexingExecutor.executeTask( task );
315 assertTrue( indexerDirectory.exists() );
317 // test packed index file creation
319 //Assertions.assertThat(new File( indexerDirectory, "nexus-maven-repository-index.zip" )).exists();
320 Assertions.assertThat(new File( indexerDirectory, "nexus-maven-repository-index.properties" )).exists();
321 Assertions.assertThat(new File( indexerDirectory, "nexus-maven-repository-index.gz" )).exists();
324 File destDir = new File( repositoryConfig.getLocation(), ".indexer/tmp" );
325 unzipIndex( indexerDirectory.getPath(), destDir.getPath() );
327 TrackingFetcher fetcher = new TrackingFetcher( remoteRepo );
328 updateRequest = new IndexUpdateRequest( testContext, fetcher );
329 updateRequest.setLocalIndexCacheDir( localCacheDir );
330 updater.fetchAndUpdateIndex( updateRequest );
332 BooleanQuery q = new BooleanQuery();
333 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
334 BooleanClause.Occur.SHOULD );
336 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
337 BooleanClause.Occur.SHOULD );
339 FlatSearchRequest request = new FlatSearchRequest( q, getIndexingContext() );
340 FlatSearchResponse response = indexer.searchFlat( request );
342 Set<ArtifactInfo> results = response.getResults();
344 ArtifactInfo artifactInfo = results.iterator().next();
345 assertEquals( "org.apache.archiva", artifactInfo.getGroupId() );
346 assertEquals( "archiva-index-methods-jar-test", artifactInfo.getArtifactId() );
347 assertEquals( "test-repo", artifactInfo.getRepository() );
349 assertEquals( 1, response.getTotalHits() );