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.commons.io.FileUtils;
28 import org.apache.lucene.search.BooleanClause.Occur;
29 import org.apache.lucene.search.BooleanQuery;
30 import org.apache.lucene.search.IndexSearcher;
31 import org.apache.lucene.search.TopDocs;
32 import org.apache.maven.index.ArtifactInfo;
33 import org.apache.maven.index.FlatSearchRequest;
34 import org.apache.maven.index.FlatSearchResponse;
35 import org.apache.maven.index.MAVEN;
36 import org.apache.maven.index.NexusIndexer;
37 import org.apache.maven.index.context.IndexingContext;
38 import org.apache.maven.index.expr.SourcedSearchExpression;
39 import org.apache.maven.index.expr.StringSearchExpression;
40 import org.junit.After;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.springframework.test.context.ContextConfiguration;
45 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
47 import javax.inject.Inject;
48 import java.io.BufferedInputStream;
49 import java.io.BufferedOutputStream;
51 import java.io.FileInputStream;
52 import java.io.FileNotFoundException;
53 import java.io.FileOutputStream;
54 import java.io.IOException;
56 import java.util.zip.ZipEntry;
57 import java.util.zip.ZipInputStream;
60 * ArchivaIndexingTaskExecutorTest
62 @RunWith( SpringJUnit4ClassRunner.class )
63 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
64 public class ArchivaIndexingTaskExecutorTest
68 private ArchivaIndexingTaskExecutor indexingExecutor;
70 private ManagedRepository repositoryConfig;
72 private NexusIndexer indexer;
75 PlexusSisuBridge plexusSisuBridge;
78 MavenIndexerUtils mavenIndexerUtils;
81 ManagedRepositoryAdmin managedRepositoryAdmin;
89 repositoryConfig = new ManagedRepository();
90 repositoryConfig.setId( "test-repo" );
91 repositoryConfig.setLocation(
92 new File( System.getProperty( "basedir" ), "target/test-classes/test-repo" ).getAbsolutePath() );
93 repositoryConfig.setLayout( "default" );
94 repositoryConfig.setName( "Test Repository" );
95 repositoryConfig.setScanned( true );
96 repositoryConfig.setSnapshots( false );
97 repositoryConfig.setReleases( true );
99 indexer = plexusSisuBridge.lookup( NexusIndexer.class );
101 managedRepositoryAdmin.createIndexContext( repositoryConfig );
105 public void tearDown()
109 for ( IndexingContext indexingContext : indexer.getIndexingContexts().values() )
111 indexer.removeIndexingContext( indexingContext, true );
114 // delete created index in the repository
115 File indexDir = new File( repositoryConfig.getLocation(), ".indexer" );
116 FileUtils.deleteDirectory( indexDir );
117 assertFalse( indexDir.exists() );
119 indexDir = new File( repositoryConfig.getLocation(), ".index" );
120 FileUtils.deleteDirectory( indexDir );
121 assertFalse( indexDir.exists() );
126 protected IndexingContext getIndexingContext()
128 return indexer.getIndexingContexts().get( repositoryConfig.getId() );
132 public void testAddArtifactToIndex()
135 File artifactFile = new File( repositoryConfig.getLocation(),
136 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
138 ArtifactIndexingTask task =
139 new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
140 getIndexingContext() );
142 indexingExecutor.executeTask( task );
144 BooleanQuery q = new BooleanQuery();
145 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
148 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
151 if ( !indexer.getIndexingContexts().containsKey( repositoryConfig.getId() ) )
153 IndexingContext context = indexer.addIndexingContext( repositoryConfig.getId(), repositoryConfig.getId(),
154 new File( repositoryConfig.getLocation() ),
155 new File( repositoryConfig.getLocation(),
156 ".indexer" ), null, null,
157 mavenIndexerUtils.getAllIndexCreators() );
158 context.setSearchable( true );
161 FlatSearchRequest request = new FlatSearchRequest( q );
162 FlatSearchResponse response = indexer.searchFlat( request );
164 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
165 assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
166 assertEquals( 1, response.getTotalHits() );
168 Set<ArtifactInfo> results = response.getResults();
170 ArtifactInfo artifactInfo = results.iterator().next();
171 assertEquals( "org.apache.archiva", artifactInfo.groupId );
172 assertEquals( "archiva-index-methods-jar-test", artifactInfo.artifactId );
173 assertEquals( "test-repo", artifactInfo.repository );
178 public void testUpdateArtifactInIndex()
181 File artifactFile = new File( repositoryConfig.getLocation(),
182 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
184 ArtifactIndexingTask task =
185 new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
186 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" ) ),
195 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
198 IndexSearcher searcher = indexer.getIndexingContexts().get( repositoryConfig.getId() ).getIndexSearcher();
199 TopDocs topDocs = searcher.search( q, null, 10 );
203 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
204 assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
206 // should only return 1 hit!
207 assertEquals( 1, topDocs.totalHits );
211 public void testRemoveArtifactFromIndex()
214 File artifactFile = new File( repositoryConfig.getLocation(),
215 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
217 ArtifactIndexingTask task =
218 new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
219 getIndexingContext() );
221 // add artifact to index
222 indexingExecutor.executeTask( task );
224 BooleanQuery q = new BooleanQuery();
225 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
228 // indexer.constructQuery( MAVEN.ARTIFACT_ID, new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
231 FlatSearchRequest flatSearchRequest =
232 new FlatSearchRequest( q, indexer.getIndexingContexts().get( repositoryConfig.getId() ) );
234 FlatSearchResponse response = indexer.searchFlat( flatSearchRequest );
236 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
237 assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
239 // should return 1 hit
240 assertEquals( 1, response.getTotalHitsCount() );
242 // remove added artifact from index
243 task = new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.DELETE,
244 getIndexingContext() );
245 indexingExecutor.executeTask( task );
247 task = new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.FINISH,
248 getIndexingContext() );
249 indexingExecutor.executeTask( task );
251 q = new BooleanQuery();
252 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new SourcedSearchExpression( "org.apache.archiva" ) ),
254 q.add( indexer.constructQuery( MAVEN.ARTIFACT_ID,
255 new SourcedSearchExpression( "archiva-index-methods-jar-test" ) ),
258 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
259 assertFalse( new File( repositoryConfig.getLocation(), ".index" ).exists() );
261 flatSearchRequest = new FlatSearchRequest( q, getIndexingContext() );
263 response = indexer.searchFlat( flatSearchRequest );
264 // artifact should have been removed from the index!
265 assertEquals( 0, response.getTotalHitsCount() );//.totalHits );
267 // TODO: test it was removed from the packaged index also
271 public void testPackagedIndex()
274 File artifactFile = new File( repositoryConfig.getLocation(),
275 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
277 ArtifactIndexingTask task =
278 new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.ADD,
279 getIndexingContext() );
280 task.setExecuteOnEntireRepo( false );
282 indexingExecutor.executeTask( task );
284 task = new ArtifactIndexingTask( repositoryConfig, artifactFile, ArtifactIndexingTask.Action.FINISH,
285 getIndexingContext() );
287 task.setExecuteOnEntireRepo( false );
289 indexingExecutor.executeTask( task );
291 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
294 File destDir = new File( repositoryConfig.getLocation(), ".indexer/tmp" );
295 unzipIndex( new File( repositoryConfig.getLocation(), ".indexer" ).getPath(), destDir.getPath() );
297 BooleanQuery q = new BooleanQuery();
298 q.add( indexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "org.apache.archiva" ) ),
301 indexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( "archiva-index-methods-jar-test" ) ),
304 FlatSearchRequest request = new FlatSearchRequest( q, getIndexingContext() );
305 FlatSearchResponse response = indexer.searchFlat( request );
307 Set<ArtifactInfo> results = response.getResults();
309 ArtifactInfo artifactInfo = results.iterator().next();
310 assertEquals( "org.apache.archiva", artifactInfo.groupId );
311 assertEquals( "archiva-index-methods-jar-test", artifactInfo.artifactId );
312 assertEquals( "test-repo", artifactInfo.repository );
314 assertEquals( 1, response.getTotalHits() );
317 private void unzipIndex( String indexDir, String destDir )
318 throws FileNotFoundException, IOException
320 final int buff = 2048;
322 new File( destDir ).mkdirs();
324 BufferedOutputStream out = null;
325 FileInputStream fin = new FileInputStream( new File( indexDir, "nexus-maven-repository-index.zip" ) );
326 ZipInputStream in = new ZipInputStream( new BufferedInputStream( fin ) );
329 while ( ( entry = in.getNextEntry() ) != null )
332 byte data[] = new byte[buff];
333 FileOutputStream fout = new FileOutputStream( new File( destDir, entry.getName() ) );
334 out = new BufferedOutputStream( fout, buff );
336 while ( ( count = in.read( data, 0, buff ) ) != -1 )
338 out.write( data, 0, count );