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.plexusbridge.MavenIndexerUtils;
24 import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
25 import org.apache.archiva.scheduler.ArchivaTaskScheduler;
26 import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask;
27 import org.apache.commons.io.FileUtils;
28 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
29 import org.apache.maven.archiva.configuration.FileTypes;
30 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
31 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
32 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
33 import org.codehaus.plexus.taskqueue.TaskQueueException;
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;
40 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
42 import javax.inject.Inject;
44 import java.io.IOException;
45 import java.util.Calendar;
46 import java.util.Date;
47 import java.util.HashSet;
48 import java.util.List;
52 * NexusIndexerConsumerTest
54 @RunWith( SpringJUnit4ClassRunner.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<File> indexed = new HashSet<File>();
64 public void queueTask( ArtifactIndexingTask task )
65 throws TaskQueueException
67 switch ( task.getAction() )
70 indexed.add( task.getResourceFile() );
73 indexed.remove( task.getResourceFile() );
78 task.getContext().close( false );
80 catch ( IOException e )
82 throw new TaskQueueException( e.getMessage() );
89 private KnownRepositoryContentConsumer nexusIndexerConsumer;
91 private ManagedRepositoryConfiguration repositoryConfig;
93 private ArchivaTaskSchedulerStub scheduler;
96 private ApplicationContext applicationContext;
99 private PlexusSisuBridge plexusSisuBridge;
102 private MavenIndexerUtils mavenIndexerUtils;
112 scheduler = new ArchivaTaskSchedulerStub();
114 ArchivaConfiguration configuration = applicationContext.getBean( ArchivaConfiguration.class );
116 FileTypes filetypes = applicationContext.getBean( FileTypes.class );
118 nexusIndexerConsumer =
119 new NexusIndexerConsumer( scheduler, configuration, filetypes, plexusSisuBridge, mavenIndexerUtils );
121 // initialize to set the file types to be processed
122 ( (Initializable) nexusIndexerConsumer ).initialize();
124 repositoryConfig = new ManagedRepositoryConfiguration();
125 repositoryConfig.setId( "test-repo" );
126 repositoryConfig.setLocation( "target/test-classes/test-repo" );
127 repositoryConfig.setLayout( "default" );
128 repositoryConfig.setName( "Test Repository" );
129 repositoryConfig.setScanned( true );
130 repositoryConfig.setSnapshots( false );
131 repositoryConfig.setReleases( true );
136 public void tearDown()
139 // delete created index in the repository
140 File indexDir = new File( repositoryConfig.getLocation(), ".indexer" );
141 FileUtils.deleteDirectory( indexDir );
142 assertFalse( indexDir.exists() );
144 indexDir = new File( repositoryConfig.getLocation(), ".index" );
145 FileUtils.deleteDirectory( indexDir );
146 assertFalse( indexDir.exists() );
152 public void testIndexerIndexArtifact()
155 File artifactFile = new File( repositoryConfig.getLocation(),
156 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
159 Date now = Calendar.getInstance().getTime();
160 nexusIndexerConsumer.beginScan( repositoryConfig, now );
161 nexusIndexerConsumer.processFile(
162 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
163 nexusIndexerConsumer.completeScan();
165 assertTrue( scheduler.indexed.contains( artifactFile ) );
169 public void testIndexerArtifactAlreadyIndexed()
172 File artifactFile = new File( repositoryConfig.getLocation(),
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 File artifactFile = new File( repositoryConfig.getLocation(),
199 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
202 Date now = Calendar.getInstance().getTime();
203 nexusIndexerConsumer.beginScan( repositoryConfig, now );
204 nexusIndexerConsumer.processFile(
205 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
206 nexusIndexerConsumer.completeScan();
208 assertTrue( scheduler.indexed.contains( artifactFile ) );
211 new File( repositoryConfig.getLocation(), "org/apache/archiva/archiva-index-methods-jar-test/1.0/pom.xml" );
213 // scan and index again
214 now = Calendar.getInstance().getTime();
215 nexusIndexerConsumer.beginScan( repositoryConfig, now );
216 nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/pom.xml" );
217 nexusIndexerConsumer.completeScan();
219 assertTrue( scheduler.indexed.contains( artifactFile ) );
222 // MRM-1275 - Include other file types for the index consumer instead of just the indexable-content
224 public void testIncludedFileTypes()
227 List<String> includes = nexusIndexerConsumer.getIncludes();
228 assertTrue( ".pom artifacts should be processed.", includes.contains( "**/*.pom" ) );
229 assertTrue( ".xml artifacts should be processed.", includes.contains( "**/*.xml" ) );
230 assertTrue( ".txt artifacts should be processed.", includes.contains( "**/*.txt" ) );
231 assertTrue( ".jar artifacts should be processed.", includes.contains( "**/*.jar" ) );
232 assertTrue( ".war artifacts should be processed.", includes.contains( "**/*.war" ) );
233 assertTrue( ".zip artifacts should be processed.", includes.contains( "**/*.zip" ) );