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
23 import java.io.IOException;
24 import java.util.Calendar;
25 import java.util.Date;
26 import java.util.HashSet;
29 import org.apache.commons.io.FileUtils;
30 import org.apache.maven.archiva.common.ArchivaException;
31 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
32 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
33 import org.apache.maven.archiva.scheduled.ArchivaTaskScheduler;
34 import org.apache.maven.archiva.scheduled.tasks.ArtifactIndexingTask;
35 import org.apache.maven.archiva.scheduled.tasks.DatabaseTask;
36 import org.apache.maven.archiva.scheduled.tasks.RepositoryTask;
37 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
38 import org.codehaus.plexus.taskqueue.TaskQueueException;
39 import org.codehaus.plexus.taskqueue.execution.TaskExecutionException;
42 * NexusIndexerConsumerTest
44 public class NexusIndexerConsumerTest
45 extends PlexusInSpringTestCase
47 private final class ArchivaTaskSchedulerStub
48 implements ArchivaTaskScheduler
50 Set<File> indexed = new HashSet<File>();
53 throws ArchivaException
57 public void scheduleDatabaseTasks()
58 throws TaskExecutionException
62 public void queueRepositoryTask( RepositoryTask task )
63 throws TaskQueueException
67 public void queueIndexingTask( 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() );
91 public void queueDatabaseTask( DatabaseTask task )
92 throws TaskQueueException
96 public boolean isProcessingRepositoryTask( String repositoryId )
101 public boolean isProcessingDatabaseTask()
107 private KnownRepositoryContentConsumer nexusIndexerConsumer;
109 private ManagedRepositoryConfiguration repositoryConfig;
111 private ArchivaTaskSchedulerStub scheduler;
114 protected void setUp()
119 scheduler = new ArchivaTaskSchedulerStub();
121 nexusIndexerConsumer = new NexusIndexerConsumer( scheduler );
123 repositoryConfig = new ManagedRepositoryConfiguration();
124 repositoryConfig.setId( "test-repo" );
125 repositoryConfig.setLocation( getBasedir() + "/target/test-classes/test-repo" );
126 repositoryConfig.setLayout( "default" );
127 repositoryConfig.setName( "Test Repository" );
128 repositoryConfig.setScanned( true );
129 repositoryConfig.setSnapshots( false );
130 repositoryConfig.setReleases( true );
134 protected void tearDown()
137 // delete created index in the repository
138 File indexDir = new File( repositoryConfig.getLocation(), ".indexer" );
139 FileUtils.deleteDirectory( indexDir );
140 assertFalse( indexDir.exists() );
142 indexDir = new File( repositoryConfig.getLocation(), ".index" );
143 FileUtils.deleteDirectory( indexDir );
144 assertFalse( indexDir.exists() );
149 public void testIndexerIndexArtifact()
153 new File( repositoryConfig.getLocation(),
154 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
157 Date now = Calendar.getInstance().getTime();
158 nexusIndexerConsumer.beginScan( repositoryConfig, now );
159 nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
160 nexusIndexerConsumer.completeScan();
162 assertTrue( scheduler.indexed.contains( artifactFile ) );
165 public void testIndexerArtifactAlreadyIndexed()
169 new File( repositoryConfig.getLocation(),
170 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
173 Date now = Calendar.getInstance().getTime();
174 nexusIndexerConsumer.beginScan( repositoryConfig, now );
175 nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
176 nexusIndexerConsumer.completeScan();
178 assertTrue( scheduler.indexed.contains( artifactFile ) );
180 // scan and index again
181 now = Calendar.getInstance().getTime();
182 nexusIndexerConsumer.beginScan( repositoryConfig, now );
183 nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
184 nexusIndexerConsumer.completeScan();
186 assertTrue( scheduler.indexed.contains( artifactFile ) );
189 public void testIndexerIndexArtifactThenPom()
193 new File( repositoryConfig.getLocation(),
194 "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
197 Date now = Calendar.getInstance().getTime();
198 nexusIndexerConsumer.beginScan( repositoryConfig, now );
199 nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
200 nexusIndexerConsumer.completeScan();
202 assertTrue( scheduler.indexed.contains( artifactFile ) );
205 new File( repositoryConfig.getLocation(), "org/apache/archiva/archiva-index-methods-jar-test/1.0/pom.xml" );
207 // scan and index again
208 now = Calendar.getInstance().getTime();
209 nexusIndexerConsumer.beginScan( repositoryConfig, now );
210 nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/pom.xml" );
211 nexusIndexerConsumer.completeScan();
213 assertTrue( scheduler.indexed.contains( artifactFile ) );
217 protected String getPlexusConfigLocation()
219 return "/org/apache/archiva/consumers/lucene/LuceneConsumersTest.xml";