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.common.utils.PathUtil;
24 import org.apache.archiva.configuration.ArchivaConfiguration;
25 import org.apache.archiva.configuration.FileTypes;
26 import org.apache.archiva.components.taskqueue.TaskQueueException;
27 import org.apache.archiva.repository.base.ArchivaRepositoryRegistry;
28 import org.apache.archiva.repository.base.managed.BasicManagedRepository;
29 import org.apache.archiva.repository.ReleaseScheme;
30 import org.apache.archiva.repository.base.group.RepositoryGroupHandler;
31 import org.apache.archiva.repository.base.managed.ManagedRepositoryHandler;
32 import org.apache.archiva.scheduler.ArchivaTaskScheduler;
33 import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask;
34 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.springframework.context.ApplicationContext;
40 import org.springframework.test.context.ContextConfiguration;
42 import javax.inject.Inject;
43 import java.io.IOException;
45 import java.nio.file.Files;
46 import java.nio.file.Path;
47 import java.nio.file.Paths;
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<Path> 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 BasicManagedRepository repositoryConfig;
93 private ArchivaTaskSchedulerStub scheduler;
96 private ApplicationContext applicationContext;
99 ArchivaRepositoryRegistry repositoryRegistry;
101 @SuppressWarnings( "unused" )
103 RepositoryGroupHandler repositoryGroupHandler;
105 @SuppressWarnings( "unused" )
107 ManagedRepositoryHandler managedRepositoryHandler;
116 scheduler = new ArchivaTaskSchedulerStub();
118 ArchivaConfiguration configuration = applicationContext.getBean( ArchivaConfiguration.class );
120 FileTypes filetypes = applicationContext.getBean( FileTypes.class );
122 nexusIndexerConsumer =
123 new NexusIndexerConsumer( scheduler, configuration, filetypes);
125 // initialize to set the file types to be processed
126 nexusIndexerConsumer.initialize();
128 repositoryConfig = BasicManagedRepository.newFilesystemInstance( "test-repo", "Test Repository", Paths.get("target/test-classes").resolve("test-repo") );
129 repositoryConfig.setLocation( new URI("target/test-classes/test-repo") );
130 repositoryConfig.setLayout( "default" );
131 repositoryConfig.setScanned( true );
132 repositoryConfig.addActiveReleaseScheme( ReleaseScheme.RELEASE );
133 repositoryConfig.removeActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
134 repositoryRegistry.putRepository(repositoryConfig);
140 public void tearDown()
143 // delete created index in the repository
144 Path basePath = PathUtil.getPathFromUri( repositoryConfig.getLocation() );
145 Path indexDir = basePath.resolve( ".indexer" );
146 org.apache.archiva.common.utils.FileUtils.deleteDirectory( indexDir );
147 assertFalse( Files.exists(indexDir) );
149 indexDir = basePath.resolve( ".index" );
150 org.apache.archiva.common.utils.FileUtils.deleteDirectory( indexDir );
151 assertFalse( Files.exists(indexDir) );
153 repositoryRegistry.destroy();
159 public void testIndexerIndexArtifact()
162 Path basePath = PathUtil.getPathFromUri( repositoryConfig.getLocation() );
163 Path artifactFile = basePath.resolve(
164 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
167 Date now = Calendar.getInstance().getTime();
168 nexusIndexerConsumer.beginScan( repositoryConfig, now );
169 nexusIndexerConsumer.processFile(
170 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
171 nexusIndexerConsumer.completeScan();
173 assertTrue( scheduler.indexed.contains( artifactFile ) );
177 public void testIndexerArtifactAlreadyIndexed()
180 Path basePath = PathUtil.getPathFromUri( repositoryConfig.getLocation() );
181 Path artifactFile = basePath.resolve(
182 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
185 Date now = Calendar.getInstance().getTime();
186 nexusIndexerConsumer.beginScan( repositoryConfig, now );
187 nexusIndexerConsumer.processFile(
188 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
189 nexusIndexerConsumer.completeScan();
191 assertTrue( scheduler.indexed.contains( artifactFile ) );
193 // scan and index again
194 now = Calendar.getInstance().getTime();
195 nexusIndexerConsumer.beginScan( repositoryConfig, now );
196 nexusIndexerConsumer.processFile(
197 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
198 nexusIndexerConsumer.completeScan();
200 assertTrue( scheduler.indexed.contains( artifactFile ) );
204 public void testIndexerIndexArtifactThenPom()
207 Path basePath = PathUtil.getPathFromUri( repositoryConfig.getLocation( ) );
208 Path artifactFile = basePath.resolve(
209 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
212 Date now = Calendar.getInstance().getTime();
213 nexusIndexerConsumer.beginScan( repositoryConfig, now );
214 nexusIndexerConsumer.processFile(
215 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
216 nexusIndexerConsumer.completeScan();
218 assertTrue( scheduler.indexed.contains( artifactFile ) );
221 basePath.resolve( "org/apache/archiva/archiva-index-methods-jar-test/1.0/pom.xml" );
223 // scan and index again
224 now = Calendar.getInstance().getTime();
225 nexusIndexerConsumer.beginScan( repositoryConfig, now );
226 nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/pom.xml" );
227 nexusIndexerConsumer.completeScan();
229 assertTrue( scheduler.indexed.contains( artifactFile ) );
232 // MRM-1275 - Include other file types for the index consumer instead of just the indexable-content
234 public void testIncludedFileTypes()
237 List<String> includes = nexusIndexerConsumer.getIncludes();
238 assertTrue( ".pom artifacts should be processed.", includes.contains( "**/*.pom" ) );
239 assertTrue( ".xml artifacts should be processed.", includes.contains( "**/*.xml" ) );
240 assertTrue( ".txt artifacts should be processed.", includes.contains( "**/*.txt" ) );
241 assertTrue( ".jar artifacts should be processed.", includes.contains( "**/*.jar" ) );
242 assertTrue( ".war artifacts should be processed.", includes.contains( "**/*.war" ) );
243 assertTrue( ".zip artifacts should be processed.", includes.contains( "**/*.zip" ) );