1 package org.apache.archiva.consumers.lucene;
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.configuration.ArchivaConfiguration;
27 import org.apache.archiva.configuration.FileTypes;
28 import org.apache.archiva.redback.components.taskqueue.TaskQueueException;
29 import org.apache.archiva.scheduler.ArchivaTaskScheduler;
30 import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask;
31 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
32 import org.apache.commons.io.FileUtils;
33 import org.apache.maven.index.NexusIndexer;
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.springframework.context.ApplicationContext;
39 import org.springframework.test.context.ContextConfiguration;
41 import javax.inject.Inject;
43 import java.io.IOException;
44 import java.util.Calendar;
45 import java.util.Date;
46 import java.util.HashSet;
47 import java.util.List;
51 * NexusIndexerConsumerTest
53 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
54 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
55 public class NexusIndexerConsumerTest
58 private final class ArchivaTaskSchedulerStub
59 implements ArchivaTaskScheduler<ArtifactIndexingTask>
61 Set<File> indexed = new HashSet<>();
64 public void queueTask( ArtifactIndexingTask task )
65 throws TaskQueueException
67 switch ( task.getAction() )
70 indexed.add( task.getResourceFile() );
73 indexed.remove( task.getResourceFile() );
78 task.getContext().close( false );
80 catch ( IOException e )
82 throw new TaskQueueException( e.getMessage() );
89 private NexusIndexerConsumer nexusIndexerConsumer;
91 private ManagedRepository repositoryConfig;
93 private ArchivaTaskSchedulerStub scheduler;
96 private ApplicationContext applicationContext;
99 private NexusIndexer nexusIndexer;
102 private MavenIndexerUtils mavenIndexerUtils;
105 private ManagedRepositoryAdmin managedRepositoryAdmin;
115 scheduler = new ArchivaTaskSchedulerStub();
117 ArchivaConfiguration configuration = applicationContext.getBean( ArchivaConfiguration.class );
119 FileTypes filetypes = applicationContext.getBean( FileTypes.class );
121 nexusIndexerConsumer =
122 new NexusIndexerConsumer( scheduler, configuration, filetypes, mavenIndexerUtils,
123 managedRepositoryAdmin, nexusIndexer );
125 // initialize to set the file types to be processed
126 ( (NexusIndexerConsumer) nexusIndexerConsumer ).initialize();
128 repositoryConfig = new ManagedRepository();
129 repositoryConfig.setId( "test-repo" );
130 repositoryConfig.setLocation( "target/test-classes/test-repo" );
131 repositoryConfig.setLayout( "default" );
132 repositoryConfig.setName( "Test Repository" );
133 repositoryConfig.setScanned( true );
134 repositoryConfig.setSnapshots( false );
135 repositoryConfig.setReleases( true );
140 public void tearDown()
143 // delete created index in the repository
144 File indexDir = new File( repositoryConfig.getLocation(), ".indexer" );
145 FileUtils.deleteDirectory( indexDir );
146 assertFalse( indexDir.exists() );
148 indexDir = new File( repositoryConfig.getLocation(), ".index" );
149 FileUtils.deleteDirectory( indexDir );
150 assertFalse( indexDir.exists() );
156 public void testIndexerIndexArtifact()
159 File artifactFile = new File( repositoryConfig.getLocation(),
160 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
163 Date now = Calendar.getInstance().getTime();
164 nexusIndexerConsumer.beginScan( repositoryConfig, now );
165 nexusIndexerConsumer.processFile(
166 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
167 nexusIndexerConsumer.completeScan();
169 assertTrue( scheduler.indexed.contains( artifactFile ) );
173 public void testIndexerArtifactAlreadyIndexed()
176 File artifactFile = new File( repositoryConfig.getLocation(),
177 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
180 Date now = Calendar.getInstance().getTime();
181 nexusIndexerConsumer.beginScan( repositoryConfig, now );
182 nexusIndexerConsumer.processFile(
183 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
184 nexusIndexerConsumer.completeScan();
186 assertTrue( scheduler.indexed.contains( artifactFile ) );
188 // scan and index again
189 now = Calendar.getInstance().getTime();
190 nexusIndexerConsumer.beginScan( repositoryConfig, now );
191 nexusIndexerConsumer.processFile(
192 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
193 nexusIndexerConsumer.completeScan();
195 assertTrue( scheduler.indexed.contains( artifactFile ) );
199 public void testIndexerIndexArtifactThenPom()
202 File artifactFile = new File( repositoryConfig.getLocation(),
203 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
206 Date now = Calendar.getInstance().getTime();
207 nexusIndexerConsumer.beginScan( repositoryConfig, now );
208 nexusIndexerConsumer.processFile(
209 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
210 nexusIndexerConsumer.completeScan();
212 assertTrue( scheduler.indexed.contains( artifactFile ) );
215 new File( repositoryConfig.getLocation(), "org/apache/archiva/archiva-index-methods-jar-test/1.0/pom.xml" );
217 // scan and index again
218 now = Calendar.getInstance().getTime();
219 nexusIndexerConsumer.beginScan( repositoryConfig, now );
220 nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/pom.xml" );
221 nexusIndexerConsumer.completeScan();
223 assertTrue( scheduler.indexed.contains( artifactFile ) );
226 // MRM-1275 - Include other file types for the index consumer instead of just the indexable-content
228 public void testIncludedFileTypes()
231 List<String> includes = nexusIndexerConsumer.getIncludes();
232 assertTrue( ".pom artifacts should be processed.", includes.contains( "**/*.pom" ) );
233 assertTrue( ".xml artifacts should be processed.", includes.contains( "**/*.xml" ) );
234 assertTrue( ".txt artifacts should be processed.", includes.contains( "**/*.txt" ) );
235 assertTrue( ".jar artifacts should be processed.", includes.contains( "**/*.jar" ) );
236 assertTrue( ".war artifacts should be processed.", includes.contains( "**/*.war" ) );
237 assertTrue( ".zip artifacts should be processed.", includes.contains( "**/*.zip" ) );