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.configuration.ArchivaConfiguration;
26 import org.apache.archiva.configuration.FileTypes;
27 import org.apache.archiva.redback.components.taskqueue.TaskQueueException;
28 import org.apache.archiva.scheduler.ArchivaTaskScheduler;
29 import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask;
30 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
31 import org.apache.maven.index.NexusIndexer;
32 import org.apache.maven.index.context.IndexCreator;
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;
42 import java.nio.file.Files;
43 import java.nio.file.Path;
44 import java.nio.file.Paths;
45 import java.util.Calendar;
46 import java.util.Date;
47 import java.util.HashSet;
48 import java.util.List;
52 * NexusIndexerConsumerTest
54 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
55 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
56 public class NexusIndexerConsumerTest
59 private final class ArchivaTaskSchedulerStub
60 implements ArchivaTaskScheduler<ArtifactIndexingTask>
62 Set<Path> indexed = new HashSet<>();
65 public void queueTask( ArtifactIndexingTask task )
66 throws TaskQueueException
68 switch ( task.getAction() )
71 indexed.add( task.getResourceFile().toPath() );
74 indexed.remove( task.getResourceFile() );
79 task.getContext().close( false );
81 catch ( IOException e )
83 throw new TaskQueueException( e.getMessage() );
90 private NexusIndexerConsumer nexusIndexerConsumer;
92 private ManagedRepository repositoryConfig;
94 private ArchivaTaskSchedulerStub scheduler;
97 private ApplicationContext applicationContext;
100 private NexusIndexer nexusIndexer;
103 private List<IndexCreator> indexCreators;
106 private ManagedRepositoryAdmin managedRepositoryAdmin;
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, indexCreators,
124 managedRepositoryAdmin, nexusIndexer );
126 // initialize to set the file types to be processed
127 nexusIndexerConsumer.initialize();
129 repositoryConfig = new ManagedRepository();
130 repositoryConfig.setId( "test-repo" );
131 repositoryConfig.setLocation( "target/test-classes/test-repo" );
132 repositoryConfig.setLayout( "default" );
133 repositoryConfig.setName( "Test Repository" );
134 repositoryConfig.setScanned( true );
135 repositoryConfig.setSnapshots( false );
136 repositoryConfig.setReleases( true );
141 public void tearDown()
144 // delete created index in the repository
145 Path indexDir = Paths.get( repositoryConfig.getLocation(), ".indexer" );
146 org.apache.archiva.common.utils.FileUtils.deleteDirectory( indexDir );
147 assertFalse( Files.exists(indexDir) );
149 indexDir = Paths.get( repositoryConfig.getLocation(), ".index" );
150 org.apache.archiva.common.utils.FileUtils.deleteDirectory( indexDir );
151 assertFalse( Files.exists(indexDir) );
157 public void testIndexerIndexArtifact()
160 Path artifactFile = Paths.get( repositoryConfig.getLocation(),
161 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
164 Date now = Calendar.getInstance().getTime();
165 nexusIndexerConsumer.beginScan( repositoryConfig, now );
166 nexusIndexerConsumer.processFile(
167 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
168 nexusIndexerConsumer.completeScan();
170 assertTrue( scheduler.indexed.contains( artifactFile ) );
174 public void testIndexerArtifactAlreadyIndexed()
177 Path artifactFile = Paths.get( repositoryConfig.getLocation(),
178 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
181 Date now = Calendar.getInstance().getTime();
182 nexusIndexerConsumer.beginScan( repositoryConfig, now );
183 nexusIndexerConsumer.processFile(
184 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
185 nexusIndexerConsumer.completeScan();
187 assertTrue( scheduler.indexed.contains( artifactFile ) );
189 // scan and index again
190 now = Calendar.getInstance().getTime();
191 nexusIndexerConsumer.beginScan( repositoryConfig, now );
192 nexusIndexerConsumer.processFile(
193 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
194 nexusIndexerConsumer.completeScan();
196 assertTrue( scheduler.indexed.contains( artifactFile ) );
200 public void testIndexerIndexArtifactThenPom()
203 Path artifactFile = Paths.get( repositoryConfig.getLocation(),
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 Paths.get( repositoryConfig.getLocation(), "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" ) );