]> source.dussan.org Git - archiva.git/blob
1a5955327009b936e5ded2399dd2c94e5c94a3d5
[archiva.git] /
1 package org.apache.archiva.consumers.lucene;
2
3 /*
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
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.Calendar;
25 import java.util.Date;
26 import java.util.HashSet;
27 import java.util.Set;
28
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;
40
41 /**
42  * NexusIndexerConsumerTest
43  */
44 public class NexusIndexerConsumerTest
45     extends PlexusInSpringTestCase
46 {
47     private final class ArchivaTaskSchedulerStub
48         implements ArchivaTaskScheduler
49     {
50         Set<File> indexed = new HashSet<File>();
51         
52         public void startup()
53             throws ArchivaException
54         {
55         }
56
57         public void scheduleDatabaseTasks()
58             throws TaskExecutionException
59         {
60         }
61
62         public void queueRepositoryTask( RepositoryTask task )
63             throws TaskQueueException
64         {
65         }
66
67         public void queueIndexingTask( ArtifactIndexingTask task )
68             throws TaskQueueException
69         {
70             switch ( task.getAction() )
71             {
72                 case ADD:
73                     indexed.add( task.getResourceFile() );
74                     break;
75                 case DELETE:
76                     indexed.remove( task.getResourceFile() );
77                     break;
78                 case FINISH:
79                     try
80                     {
81                         task.getContext().close( false );
82                     }
83                     catch ( IOException e )
84                     {
85                         throw new TaskQueueException( e.getMessage() );
86                     }
87                     break;
88             }
89         }
90
91         public void queueDatabaseTask( DatabaseTask task )
92             throws TaskQueueException
93         {
94         }
95
96         public boolean isProcessingRepositoryTask( String repositoryId )
97         {
98             return false;
99         }
100
101         public boolean isProcessingDatabaseTask()
102         {
103             return false;
104         }
105     }
106
107     private KnownRepositoryContentConsumer nexusIndexerConsumer;
108
109     private ManagedRepositoryConfiguration repositoryConfig;
110
111     private ArchivaTaskSchedulerStub scheduler;
112
113     @Override
114     protected void setUp()
115         throws Exception
116     {
117         super.setUp();
118
119         scheduler = new ArchivaTaskSchedulerStub();
120
121         nexusIndexerConsumer = new NexusIndexerConsumer( scheduler );
122
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 );
131     }
132
133     @Override
134     protected void tearDown()
135         throws Exception
136     {
137         // delete created index in the repository
138         File indexDir = new File( repositoryConfig.getLocation(), ".indexer" );
139         FileUtils.deleteDirectory( indexDir );
140         assertFalse( indexDir.exists() );
141
142         indexDir = new File( repositoryConfig.getLocation(), ".index" );
143         FileUtils.deleteDirectory( indexDir );
144         assertFalse( indexDir.exists() );
145
146         super.tearDown();
147     }
148
149     public void testIndexerIndexArtifact()
150         throws Exception
151     {
152         File artifactFile =
153             new File( repositoryConfig.getLocation(),
154                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
155
156         // begin scan
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();
161
162         assertTrue( scheduler.indexed.contains( artifactFile ) );
163     }
164
165     public void testIndexerArtifactAlreadyIndexed()
166         throws Exception
167     {
168         File artifactFile =
169             new File( repositoryConfig.getLocation(),
170                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
171
172         // begin scan
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();
177
178         assertTrue( scheduler.indexed.contains( artifactFile ) );
179
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();
185
186         assertTrue( scheduler.indexed.contains( artifactFile ) );
187     }
188
189     public void testIndexerIndexArtifactThenPom()
190         throws Exception
191     {
192         File artifactFile =
193             new File( repositoryConfig.getLocation(),
194                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
195
196         // begin scan
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();
201
202         assertTrue( scheduler.indexed.contains( artifactFile ) );
203
204         artifactFile =
205             new File( repositoryConfig.getLocation(), "org/apache/archiva/archiva-index-methods-jar-test/1.0/pom.xml" );
206
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();
212
213         assertTrue( scheduler.indexed.contains( artifactFile ) );
214     }
215
216     @Override
217     protected String getPlexusConfigLocation()
218     {
219         return "/org/apache/archiva/consumers/lucene/LuceneConsumersTest.xml";
220     }
221 }