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.redback.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.scheduler.ArchivaTaskScheduler;
31 import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask;
32 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.springframework.context.ApplicationContext;
38 import org.springframework.test.context.ContextConfiguration;
40 import javax.inject.Inject;
41 import java.io.IOException;
43 import java.nio.file.Files;
44 import java.nio.file.Path;
45 import java.nio.file.Paths;
49 * NexusIndexerConsumerTest
51 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
52 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
53 public class NexusIndexerConsumerTest
56 private final class ArchivaTaskSchedulerStub
57 implements ArchivaTaskScheduler<ArtifactIndexingTask>
59 Set<Path> indexed = new HashSet<>();
62 public void queueTask( ArtifactIndexingTask task )
63 throws TaskQueueException
65 switch ( task.getAction() )
68 indexed.add( task.getResourceFile() );
71 indexed.remove( task.getResourceFile() );
76 task.getContext().close( false );
78 catch ( IOException e )
80 throw new TaskQueueException( e.getMessage() );
87 private NexusIndexerConsumer nexusIndexerConsumer;
89 private BasicManagedRepository repositoryConfig;
91 private ArchivaTaskSchedulerStub scheduler;
94 private ApplicationContext applicationContext;
97 ArchivaRepositoryRegistry repositoryRegistry;
107 scheduler = new ArchivaTaskSchedulerStub();
109 ArchivaConfiguration configuration = applicationContext.getBean( ArchivaConfiguration.class );
111 FileTypes filetypes = applicationContext.getBean( FileTypes.class );
113 nexusIndexerConsumer =
114 new NexusIndexerConsumer( scheduler, configuration, filetypes);
116 // initialize to set the file types to be processed
117 nexusIndexerConsumer.initialize();
119 repositoryConfig = BasicManagedRepository.newFilesystemInstance( "test-repo", "Test Repository", Paths.get("target/test-classes").resolve("test-repo") );
120 repositoryConfig.setLocation( new URI("target/test-classes/test-repo") );
121 repositoryConfig.setLayout( "default" );
122 repositoryConfig.setScanned( true );
123 repositoryConfig.addActiveReleaseScheme( ReleaseScheme.RELEASE );
124 repositoryConfig.removeActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
125 repositoryRegistry.putRepository(repositoryConfig);
131 public void tearDown()
134 // delete created index in the repository
135 Path basePath = PathUtil.getPathFromUri( repositoryConfig.getLocation() );
136 Path indexDir = basePath.resolve( ".indexer" );
137 org.apache.archiva.common.utils.FileUtils.deleteDirectory( indexDir );
138 assertFalse( Files.exists(indexDir) );
140 indexDir = basePath.resolve( ".index" );
141 org.apache.archiva.common.utils.FileUtils.deleteDirectory( indexDir );
142 assertFalse( Files.exists(indexDir) );
144 repositoryRegistry.destroy();
150 public void testIndexerIndexArtifact()
153 Path basePath = PathUtil.getPathFromUri( repositoryConfig.getLocation() );
154 Path artifactFile = basePath.resolve(
155 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
158 Date now = Calendar.getInstance().getTime();
159 nexusIndexerConsumer.beginScan( repositoryConfig, now );
160 nexusIndexerConsumer.processFile(
161 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
162 nexusIndexerConsumer.completeScan();
164 assertTrue( scheduler.indexed.contains( artifactFile ) );
168 public void testIndexerArtifactAlreadyIndexed()
171 Path basePath = PathUtil.getPathFromUri( repositoryConfig.getLocation() );
172 Path artifactFile = basePath.resolve(
173 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
176 Date now = Calendar.getInstance().getTime();
177 nexusIndexerConsumer.beginScan( repositoryConfig, now );
178 nexusIndexerConsumer.processFile(
179 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
180 nexusIndexerConsumer.completeScan();
182 assertTrue( scheduler.indexed.contains( artifactFile ) );
184 // scan and index again
185 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 ) );
195 public void testIndexerIndexArtifactThenPom()
198 Path basePath = PathUtil.getPathFromUri( repositoryConfig.getLocation( ) );
199 Path artifactFile = basePath.resolve(
200 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
203 Date now = Calendar.getInstance().getTime();
204 nexusIndexerConsumer.beginScan( repositoryConfig, now );
205 nexusIndexerConsumer.processFile(
206 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
207 nexusIndexerConsumer.completeScan();
209 assertTrue( scheduler.indexed.contains( artifactFile ) );
212 basePath.resolve( "org/apache/archiva/archiva-index-methods-jar-test/1.0/pom.xml" );
214 // scan and index again
215 now = Calendar.getInstance().getTime();
216 nexusIndexerConsumer.beginScan( repositoryConfig, now );
217 nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/pom.xml" );
218 nexusIndexerConsumer.completeScan();
220 assertTrue( scheduler.indexed.contains( artifactFile ) );
223 // MRM-1275 - Include other file types for the index consumer instead of just the indexable-content
225 public void testIncludedFileTypes()
228 List<String> includes = nexusIndexerConsumer.getIncludes();
229 assertTrue( ".pom artifacts should be processed.", includes.contains( "**/*.pom" ) );
230 assertTrue( ".xml artifacts should be processed.", includes.contains( "**/*.xml" ) );
231 assertTrue( ".txt artifacts should be processed.", includes.contains( "**/*.txt" ) );
232 assertTrue( ".jar artifacts should be processed.", includes.contains( "**/*.jar" ) );
233 assertTrue( ".war artifacts should be processed.", includes.contains( "**/*.war" ) );
234 assertTrue( ".zip artifacts should be processed.", includes.contains( "**/*.zip" ) );