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.common.plexusbridge.MavenIndexerUtils;
26 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
27 import org.apache.archiva.configuration.ArchivaConfiguration;
28 import org.apache.archiva.configuration.FileTypes;
29 import org.apache.archiva.scheduler.ArchivaTaskScheduler;
30 import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask;
31 import org.apache.commons.io.FileUtils;
32 import org.apache.archiva.redback.components.taskqueue.TaskQueueException;
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;
42 import java.io.IOException;
43 import java.util.Calendar;
44 import java.util.Date;
45 import java.util.HashSet;
46 import java.util.List;
48 import org.apache.archiva.test.ArchivaSpringJUnit4ClassRunner;
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<File> indexed = new HashSet<File>();
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 ManagedRepository repositoryConfig;
92 private ArchivaTaskSchedulerStub scheduler;
95 private ApplicationContext applicationContext;
98 private PlexusSisuBridge plexusSisuBridge;
101 private MavenIndexerUtils mavenIndexerUtils;
104 private ManagedRepositoryAdmin managedRepositoryAdmin;
114 scheduler = new ArchivaTaskSchedulerStub();
116 ArchivaConfiguration configuration = applicationContext.getBean( ArchivaConfiguration.class );
118 FileTypes filetypes = applicationContext.getBean( FileTypes.class );
120 nexusIndexerConsumer =
121 new NexusIndexerConsumer( scheduler, configuration, filetypes, plexusSisuBridge, mavenIndexerUtils,
122 managedRepositoryAdmin );
124 // initialize to set the file types to be processed
125 ( (NexusIndexerConsumer) nexusIndexerConsumer ).initialize();
127 repositoryConfig = new ManagedRepository();
128 repositoryConfig.setId( "test-repo" );
129 repositoryConfig.setLocation( "target/test-classes/test-repo" );
130 repositoryConfig.setLayout( "default" );
131 repositoryConfig.setName( "Test Repository" );
132 repositoryConfig.setScanned( true );
133 repositoryConfig.setSnapshots( false );
134 repositoryConfig.setReleases( true );
139 public void tearDown()
142 // delete created index in the repository
143 File indexDir = new File( repositoryConfig.getLocation(), ".indexer" );
144 FileUtils.deleteDirectory( indexDir );
145 assertFalse( indexDir.exists() );
147 indexDir = new File( repositoryConfig.getLocation(), ".index" );
148 FileUtils.deleteDirectory( indexDir );
149 assertFalse( indexDir.exists() );
155 public void testIndexerIndexArtifact()
158 File artifactFile = new File( repositoryConfig.getLocation(),
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 File artifactFile = new File( repositoryConfig.getLocation(),
176 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
179 Date now = Calendar.getInstance().getTime();
180 nexusIndexerConsumer.beginScan( repositoryConfig, now );
181 nexusIndexerConsumer.processFile(
182 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
183 nexusIndexerConsumer.completeScan();
185 assertTrue( scheduler.indexed.contains( artifactFile ) );
187 // scan and index again
188 now = Calendar.getInstance().getTime();
189 nexusIndexerConsumer.beginScan( repositoryConfig, now );
190 nexusIndexerConsumer.processFile(
191 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
192 nexusIndexerConsumer.completeScan();
194 assertTrue( scheduler.indexed.contains( artifactFile ) );
198 public void testIndexerIndexArtifactThenPom()
201 File artifactFile = new File( repositoryConfig.getLocation(),
202 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
205 Date now = Calendar.getInstance().getTime();
206 nexusIndexerConsumer.beginScan( repositoryConfig, now );
207 nexusIndexerConsumer.processFile(
208 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
209 nexusIndexerConsumer.completeScan();
211 assertTrue( scheduler.indexed.contains( artifactFile ) );
214 new File( repositoryConfig.getLocation(), "org/apache/archiva/archiva-index-methods-jar-test/1.0/pom.xml" );
216 // scan and index again
217 now = Calendar.getInstance().getTime();
218 nexusIndexerConsumer.beginScan( repositoryConfig, now );
219 nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/pom.xml" );
220 nexusIndexerConsumer.completeScan();
222 assertTrue( scheduler.indexed.contains( artifactFile ) );
225 // MRM-1275 - Include other file types for the index consumer instead of just the indexable-content
227 public void testIncludedFileTypes()
230 List<String> includes = nexusIndexerConsumer.getIncludes();
231 assertTrue( ".pom artifacts should be processed.", includes.contains( "**/*.pom" ) );
232 assertTrue( ".xml artifacts should be processed.", includes.contains( "**/*.xml" ) );
233 assertTrue( ".txt artifacts should be processed.", includes.contains( "**/*.txt" ) );
234 assertTrue( ".jar artifacts should be processed.", includes.contains( "**/*.jar" ) );
235 assertTrue( ".war artifacts should be processed.", includes.contains( "**/*.war" ) );
236 assertTrue( ".zip artifacts should be processed.", includes.contains( "**/*.zip" ) );