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.BasicManagedRepository;
29 import org.apache.archiva.repository.ReleaseScheme;
30 import org.apache.archiva.repository.base.RepositoryGroupHandler;
31 import org.apache.archiva.scheduler.ArchivaTaskScheduler;
32 import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask;
33 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
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;
42 import java.io.IOException;
44 import java.nio.file.Files;
45 import java.nio.file.Path;
46 import java.nio.file.Paths;
50 * NexusIndexerConsumerTest
52 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
53 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
54 public class NexusIndexerConsumerTest
57 private final class ArchivaTaskSchedulerStub
58 implements ArchivaTaskScheduler<ArtifactIndexingTask>
60 Set<Path> indexed = new HashSet<>();
63 public void queueTask( ArtifactIndexingTask task )
64 throws TaskQueueException
66 switch ( task.getAction() )
69 indexed.add( task.getResourceFile() );
72 indexed.remove( task.getResourceFile() );
77 task.getContext().close( false );
79 catch ( IOException e )
81 throw new TaskQueueException( e.getMessage() );
88 private NexusIndexerConsumer nexusIndexerConsumer;
90 private BasicManagedRepository repositoryConfig;
92 private ArchivaTaskSchedulerStub scheduler;
95 private ApplicationContext applicationContext;
98 ArchivaRepositoryRegistry repositoryRegistry;
100 @SuppressWarnings( "unused" )
102 RepositoryGroupHandler repositoryGroupHandler;
111 scheduler = new ArchivaTaskSchedulerStub();
113 ArchivaConfiguration configuration = applicationContext.getBean( ArchivaConfiguration.class );
115 FileTypes filetypes = applicationContext.getBean( FileTypes.class );
117 nexusIndexerConsumer =
118 new NexusIndexerConsumer( scheduler, configuration, filetypes);
120 // initialize to set the file types to be processed
121 nexusIndexerConsumer.initialize();
123 repositoryConfig = BasicManagedRepository.newFilesystemInstance( "test-repo", "Test Repository", Paths.get("target/test-classes").resolve("test-repo") );
124 repositoryConfig.setLocation( new URI("target/test-classes/test-repo") );
125 repositoryConfig.setLayout( "default" );
126 repositoryConfig.setScanned( true );
127 repositoryConfig.addActiveReleaseScheme( ReleaseScheme.RELEASE );
128 repositoryConfig.removeActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
129 repositoryRegistry.putRepository(repositoryConfig);
135 public void tearDown()
138 // delete created index in the repository
139 Path basePath = PathUtil.getPathFromUri( repositoryConfig.getLocation() );
140 Path indexDir = basePath.resolve( ".indexer" );
141 org.apache.archiva.common.utils.FileUtils.deleteDirectory( indexDir );
142 assertFalse( Files.exists(indexDir) );
144 indexDir = basePath.resolve( ".index" );
145 org.apache.archiva.common.utils.FileUtils.deleteDirectory( indexDir );
146 assertFalse( Files.exists(indexDir) );
148 repositoryRegistry.destroy();
154 public void testIndexerIndexArtifact()
157 Path basePath = PathUtil.getPathFromUri( repositoryConfig.getLocation() );
158 Path artifactFile = basePath.resolve(
159 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
162 Date now = Calendar.getInstance().getTime();
163 nexusIndexerConsumer.beginScan( repositoryConfig, now );
164 nexusIndexerConsumer.processFile(
165 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
166 nexusIndexerConsumer.completeScan();
168 assertTrue( scheduler.indexed.contains( artifactFile ) );
172 public void testIndexerArtifactAlreadyIndexed()
175 Path basePath = PathUtil.getPathFromUri( repositoryConfig.getLocation() );
176 Path artifactFile = basePath.resolve(
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 Path basePath = PathUtil.getPathFromUri( repositoryConfig.getLocation( ) );
203 Path artifactFile = basePath.resolve(
204 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
207 Date now = Calendar.getInstance().getTime();
208 nexusIndexerConsumer.beginScan( repositoryConfig, now );
209 nexusIndexerConsumer.processFile(
210 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
211 nexusIndexerConsumer.completeScan();
213 assertTrue( scheduler.indexed.contains( artifactFile ) );
216 basePath.resolve( "org/apache/archiva/archiva-index-methods-jar-test/1.0/pom.xml" );
218 // scan and index again
219 now = Calendar.getInstance().getTime();
220 nexusIndexerConsumer.beginScan( repositoryConfig, now );
221 nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/pom.xml" );
222 nexusIndexerConsumer.completeScan();
224 assertTrue( scheduler.indexed.contains( artifactFile ) );
227 // MRM-1275 - Include other file types for the index consumer instead of just the indexable-content
229 public void testIncludedFileTypes()
232 List<String> includes = nexusIndexerConsumer.getIncludes();
233 assertTrue( ".pom artifacts should be processed.", includes.contains( "**/*.pom" ) );
234 assertTrue( ".xml artifacts should be processed.", includes.contains( "**/*.xml" ) );
235 assertTrue( ".txt artifacts should be processed.", includes.contains( "**/*.txt" ) );
236 assertTrue( ".jar artifacts should be processed.", includes.contains( "**/*.jar" ) );
237 assertTrue( ".war artifacts should be processed.", includes.contains( "**/*.war" ) );
238 assertTrue( ".zip artifacts should be processed.", includes.contains( "**/*.zip" ) );