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.components.taskqueue.TaskQueueException;
25 import org.apache.archiva.configuration.ArchivaConfiguration;
26 import org.apache.archiva.configuration.FileTypes;
27 import org.apache.archiva.repository.ReleaseScheme;
28 import org.apache.archiva.repository.base.ArchivaRepositoryRegistry;
29 import org.apache.archiva.repository.base.RepositoryHandlerDependencies;
30 import org.apache.archiva.repository.base.managed.BasicManagedRepository;
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;
47 import java.util.Calendar;
48 import java.util.Date;
49 import java.util.HashSet;
50 import java.util.List;
54 * NexusIndexerConsumerTest
56 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
57 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
58 public class NexusIndexerConsumerTest
61 private final class ArchivaTaskSchedulerStub
62 implements ArchivaTaskScheduler<ArtifactIndexingTask>
64 Set<Path> indexed = new HashSet<>();
67 public void queueTask( ArtifactIndexingTask task )
68 throws TaskQueueException
70 switch ( task.getAction() )
73 indexed.add( task.getResourceFile() );
76 indexed.remove( task.getResourceFile() );
81 task.getContext().close( false );
83 catch ( IOException e )
85 throw new TaskQueueException( e.getMessage() );
92 private NexusIndexerConsumer nexusIndexerConsumer;
94 private BasicManagedRepository repositoryConfig;
96 private ArchivaTaskSchedulerStub scheduler;
99 private ApplicationContext applicationContext;
102 ArchivaRepositoryRegistry repositoryRegistry;
104 @SuppressWarnings( "unused" )
106 RepositoryHandlerDependencies repositoryHandlerDependencies;
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);
124 // initialize to set the file types to be processed
125 nexusIndexerConsumer.initialize();
127 repositoryConfig = BasicManagedRepository.newFilesystemInstance( "test-repo", "Test Repository", Paths.get("target/test-classes").resolve("test-repo") );
128 repositoryConfig.setLocation( new URI("target/test-classes/test-repo") );
129 repositoryConfig.setLayout( "default" );
130 repositoryConfig.setScanned( true );
131 repositoryConfig.addActiveReleaseScheme( ReleaseScheme.RELEASE );
132 repositoryConfig.removeActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
133 repositoryRegistry.putRepository(repositoryConfig);
139 public void tearDown()
142 // delete created index in the repository
143 Path basePath = PathUtil.getPathFromUri( repositoryConfig.getLocation() );
144 Path indexDir = basePath.resolve( ".indexer" );
145 org.apache.archiva.common.utils.FileUtils.deleteDirectory( indexDir );
146 assertFalse( Files.exists(indexDir) );
148 indexDir = basePath.resolve( ".index" );
149 org.apache.archiva.common.utils.FileUtils.deleteDirectory( indexDir );
150 assertFalse( Files.exists(indexDir) );
152 repositoryRegistry.destroy();
158 public void testIndexerIndexArtifact()
161 Path basePath = PathUtil.getPathFromUri( repositoryConfig.getLocation() );
162 Path artifactFile = basePath.resolve(
163 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
166 Date now = Calendar.getInstance().getTime();
167 nexusIndexerConsumer.beginScan( repositoryConfig, now );
168 nexusIndexerConsumer.processFile(
169 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
170 nexusIndexerConsumer.completeScan();
172 assertTrue( scheduler.indexed.contains( artifactFile ) );
176 public void testIndexerArtifactAlreadyIndexed()
179 Path basePath = PathUtil.getPathFromUri( repositoryConfig.getLocation() );
180 Path artifactFile = basePath.resolve(
181 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
184 Date now = Calendar.getInstance().getTime();
185 nexusIndexerConsumer.beginScan( repositoryConfig, now );
186 nexusIndexerConsumer.processFile(
187 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
188 nexusIndexerConsumer.completeScan();
190 assertTrue( scheduler.indexed.contains( artifactFile ) );
192 // scan and index again
193 now = Calendar.getInstance().getTime();
194 nexusIndexerConsumer.beginScan( repositoryConfig, now );
195 nexusIndexerConsumer.processFile(
196 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
197 nexusIndexerConsumer.completeScan();
199 assertTrue( scheduler.indexed.contains( artifactFile ) );
203 public void testIndexerIndexArtifactThenPom()
206 Path basePath = PathUtil.getPathFromUri( repositoryConfig.getLocation( ) );
207 Path artifactFile = basePath.resolve(
208 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
211 Date now = Calendar.getInstance().getTime();
212 nexusIndexerConsumer.beginScan( repositoryConfig, now );
213 nexusIndexerConsumer.processFile(
214 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
215 nexusIndexerConsumer.completeScan();
217 assertTrue( scheduler.indexed.contains( artifactFile ) );
220 basePath.resolve( "org/apache/archiva/archiva-index-methods-jar-test/1.0/pom.xml" );
222 // scan and index again
223 now = Calendar.getInstance().getTime();
224 nexusIndexerConsumer.beginScan( repositoryConfig, now );
225 nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/pom.xml" );
226 nexusIndexerConsumer.completeScan();
228 assertTrue( scheduler.indexed.contains( artifactFile ) );
231 // MRM-1275 - Include other file types for the index consumer instead of just the indexable-content
233 public void testIncludedFileTypes()
236 List<String> includes = nexusIndexerConsumer.getIncludes();
237 assertTrue( ".pom artifacts should be processed.", includes.contains( "**/*.pom" ) );
238 assertTrue( ".xml artifacts should be processed.", includes.contains( "**/*.xml" ) );
239 assertTrue( ".txt artifacts should be processed.", includes.contains( "**/*.txt" ) );
240 assertTrue( ".jar artifacts should be processed.", includes.contains( "**/*.jar" ) );
241 assertTrue( ".war artifacts should be processed.", includes.contains( "**/*.war" ) );
242 assertTrue( ".zip artifacts should be processed.", includes.contains( "**/*.zip" ) );