1 package org.apache.maven.archiva.scheduled.executors;
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
25 import org.apache.commons.io.FileUtils;
26 import org.apache.lucene.search.BooleanQuery;
27 import org.apache.lucene.search.IndexSearcher;
28 import org.apache.lucene.search.TopDocs;
29 import org.apache.lucene.search.BooleanClause.Occur;
30 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
31 import org.apache.maven.archiva.configuration.Configuration;
32 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
33 import org.apache.maven.archiva.scheduled.tasks.ArtifactIndexingTask;
34 import org.apache.maven.archiva.scheduled.tasks.TaskCreator;
35 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
36 import org.easymock.MockControl;
37 import org.sonatype.nexus.index.ArtifactInfo;
38 import org.sonatype.nexus.index.FlatSearchRequest;
39 import org.sonatype.nexus.index.FlatSearchResponse;
40 import org.sonatype.nexus.index.IndexerEngine;
41 import org.sonatype.nexus.index.NexusIndexer;
42 import org.sonatype.nexus.index.context.IndexingContext;
43 import org.sonatype.nexus.index.packer.IndexPacker;
46 * ArchivaIndexingTaskExecutorTest
48 public class ArchivaIndexingTaskExecutorTest
49 extends PlexusInSpringTestCase
51 private ArchivaIndexingTaskExecutor indexingExecutor;
53 private IndexerEngine indexerEngine;
55 private IndexPacker indexPacker;
57 private MockControl archivaConfigControl;
59 private ArchivaConfiguration archivaConfiguration;
61 private ManagedRepositoryConfiguration repositoryConfig;
63 private Configuration configuration;
65 private NexusIndexer indexer;
67 protected void setUp() throws Exception
71 indexingExecutor = new ArchivaIndexingTaskExecutor();
72 indexingExecutor.initialize();
74 repositoryConfig = new ManagedRepositoryConfiguration();
75 repositoryConfig.setId( "test-repo" );
76 repositoryConfig.setLocation( getBasedir() + "/target/test-classes/test-repo" );
77 repositoryConfig.setLayout( "default" );
78 repositoryConfig.setName( "Test Repository" );
79 repositoryConfig.setScanned( true );
80 repositoryConfig.setSnapshots( false );
81 repositoryConfig.setReleases( true );
83 configuration = new Configuration();
84 configuration.addManagedRepository( repositoryConfig );
86 archivaConfigControl = MockControl.createControl( ArchivaConfiguration.class );
87 archivaConfiguration = ( ArchivaConfiguration ) archivaConfigControl.getMock();
89 indexer = ( NexusIndexer ) lookup( NexusIndexer.class );
90 indexerEngine = ( IndexerEngine ) lookup ( IndexerEngine.class );
91 indexPacker = ( IndexPacker ) lookup( IndexPacker.class );
93 indexingExecutor.setIndexerEngine( indexerEngine );
94 indexingExecutor.setIndexPacker( indexPacker );
95 indexingExecutor.setArchivaConfiguration( archivaConfiguration );
98 protected void tearDown() throws Exception
100 // delete created index in the repository
101 File indexDir = new File( repositoryConfig.getLocation(), ".indexer" );
102 FileUtils.deleteDirectory( indexDir );
103 assertFalse( indexDir.exists() );
105 indexDir = new File( repositoryConfig.getLocation(), ".index" );
106 FileUtils.deleteDirectory( indexDir );
107 assertFalse( indexDir.exists() );
112 public void testAddArtifactToIndex()
116 new File( repositoryConfig.getLocation(),
117 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
119 ArtifactIndexingTask task =
120 TaskCreator.createIndexingTask( repositoryConfig.getId(), artifactFile, ArtifactIndexingTask.ADD );
122 archivaConfigControl.expectAndReturn( archivaConfiguration.getConfiguration(), configuration );
124 archivaConfigControl.replay();
126 indexingExecutor.executeTask( task );
128 archivaConfigControl.verify();
130 BooleanQuery q = new BooleanQuery();
131 q.add( indexer.constructQuery( ArtifactInfo.GROUP_ID, "org.apache.archiva" ), Occur.SHOULD );
132 q.add( indexer.constructQuery( ArtifactInfo.ARTIFACT_ID, "archiva-index-methods-jar-test" ), Occur.SHOULD );
134 IndexingContext context = indexer.addIndexingContext( repositoryConfig.getId(), repositoryConfig.getId(), new File( repositoryConfig.getLocation() ),
135 new File( repositoryConfig.getLocation(), ".indexer" ), null, null, NexusIndexer.FULL_INDEX );
136 context.setSearchable( true );
138 FlatSearchRequest request = new FlatSearchRequest( q );
139 FlatSearchResponse response = indexer.searchFlat( request );
141 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
142 assertTrue( new File( repositoryConfig.getLocation(), ".index" ).exists() );
143 assertEquals( 1, response.getTotalHits() );
145 Set<ArtifactInfo> results = response.getResults();
147 ArtifactInfo artifactInfo = (ArtifactInfo) results.iterator().next();
148 assertEquals( "org.apache.archiva", artifactInfo.groupId );
149 assertEquals( "archiva-index-methods-jar-test", artifactInfo.artifactId );
150 assertEquals( "test-repo", artifactInfo.repository );
153 public void testUpdateArtifactInIndex()
157 new File( repositoryConfig.getLocation(),
158 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
160 ArtifactIndexingTask task =
161 TaskCreator.createIndexingTask( repositoryConfig.getId(), artifactFile, ArtifactIndexingTask.ADD );
163 archivaConfigControl.expectAndReturn( archivaConfiguration.getConfiguration(), configuration, 2 );
165 archivaConfigControl.replay();
167 indexingExecutor.executeTask( task );
168 indexingExecutor.executeTask( task );
170 archivaConfigControl.verify();
172 BooleanQuery q = new BooleanQuery();
173 q.add( indexer.constructQuery( ArtifactInfo.GROUP_ID, "org.apache.archiva" ), Occur.SHOULD );
174 q.add( indexer.constructQuery( ArtifactInfo.ARTIFACT_ID, "archiva-index-methods-jar-test" ), Occur.SHOULD );
176 IndexSearcher searcher = new IndexSearcher( repositoryConfig.getLocation() + "/.indexer" );
177 TopDocs topDocs = searcher.search( q, null, 10 );
179 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
180 assertTrue( new File( repositoryConfig.getLocation(), ".index" ).exists() );
182 // should only return 1 hit!
183 assertEquals( 1, topDocs.totalHits );
186 public void testRemoveArtifactFromIndex()
190 new File( repositoryConfig.getLocation(),
191 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
193 ArtifactIndexingTask task =
194 TaskCreator.createIndexingTask( repositoryConfig.getId(), artifactFile, ArtifactIndexingTask.ADD );
196 archivaConfigControl.expectAndReturn( archivaConfiguration.getConfiguration(), configuration, 2 );
198 archivaConfigControl.replay();
200 // add artifact to index
201 indexingExecutor.executeTask( task );
203 BooleanQuery q = new BooleanQuery();
204 q.add( indexer.constructQuery( ArtifactInfo.GROUP_ID, "org.apache.archiva" ), Occur.SHOULD );
205 q.add( indexer.constructQuery( ArtifactInfo.ARTIFACT_ID, "archiva-index-methods-jar-test" ), Occur.SHOULD );
207 IndexSearcher searcher = new IndexSearcher( repositoryConfig.getLocation() + "/.indexer" );
208 TopDocs topDocs = searcher.search( q, null, 10 );
210 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
211 assertTrue( new File( repositoryConfig.getLocation(), ".index" ).exists() );
213 // should return 1 hit
214 assertEquals( 1, topDocs.totalHits );
216 // remove added artifact from index
218 TaskCreator.createIndexingTask( repositoryConfig.getId(), artifactFile, ArtifactIndexingTask.DELETE );
219 indexingExecutor.executeTask( task );
221 archivaConfigControl.verify();
223 q = new BooleanQuery();
224 q.add( indexer.constructQuery( ArtifactInfo.GROUP_ID, "org.apache.archiva" ), Occur.SHOULD );
225 q.add( indexer.constructQuery( ArtifactInfo.ARTIFACT_ID, "archiva-index-methods-jar-test" ), Occur.SHOULD );
227 searcher = new IndexSearcher( repositoryConfig.getLocation() + "/.indexer" );
228 topDocs = searcher.search( q, null, 10 );
230 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
231 assertTrue( new File( repositoryConfig.getLocation(), ".index" ).exists() );
233 // artifact should have been removed from the index!
234 assertEquals( 0, topDocs.totalHits );