]> source.dussan.org Git - archiva.git/blob
ef4804b2f1b2d2f25a3cfd74e0173e4d3a85cfef
[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 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;
39 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
40
41 import javax.inject.Inject;
42 import java.io.File;
43 import java.io.IOException;
44 import java.util.Calendar;
45 import java.util.Date;
46 import java.util.HashSet;
47 import java.util.List;
48 import java.util.Set;
49
50 /**
51  * NexusIndexerConsumerTest
52  */
53 @RunWith( SpringJUnit4ClassRunner.class )
54 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
55 public class NexusIndexerConsumerTest
56     extends TestCase
57 {
58     private final class ArchivaTaskSchedulerStub
59         implements ArchivaTaskScheduler<ArtifactIndexingTask>
60     {
61         Set<File> indexed = new HashSet<File>();
62
63         public void queueTask( ArtifactIndexingTask task )
64             throws TaskQueueException
65         {
66             switch ( task.getAction() )
67             {
68                 case ADD:
69                     indexed.add( task.getResourceFile() );
70                     break;
71                 case DELETE:
72                     indexed.remove( task.getResourceFile() );
73                     break;
74                 case FINISH:
75                     try
76                     {
77                         task.getContext().close( false );
78                     }
79                     catch ( IOException e )
80                     {
81                         throw new TaskQueueException( e.getMessage() );
82                     }
83                     break;
84             }
85         }
86     }
87
88     private NexusIndexerConsumer nexusIndexerConsumer;
89
90     private ManagedRepository repositoryConfig;
91
92     private ArchivaTaskSchedulerStub scheduler;
93
94     @Inject
95     private ApplicationContext applicationContext;
96
97     @Inject
98     private PlexusSisuBridge plexusSisuBridge;
99
100     @Inject
101     private MavenIndexerUtils mavenIndexerUtils;
102
103     @Inject
104     private ManagedRepositoryAdmin managedRepositoryAdmin;
105
106
107     @Override
108     @Before
109     public void setUp()
110         throws Exception
111     {
112         super.setUp();
113
114         scheduler = new ArchivaTaskSchedulerStub();
115
116         ArchivaConfiguration configuration = applicationContext.getBean( ArchivaConfiguration.class );
117
118         FileTypes filetypes = applicationContext.getBean( FileTypes.class );
119
120         nexusIndexerConsumer =
121             new NexusIndexerConsumer( scheduler, configuration, filetypes, plexusSisuBridge, mavenIndexerUtils,
122                                       managedRepositoryAdmin );
123
124         // initialize to set the file types to be processed
125         ( (NexusIndexerConsumer) nexusIndexerConsumer ).initialize();
126
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 );
135     }
136
137     @Override
138     @After
139     public void tearDown()
140         throws Exception
141     {
142         // delete created index in the repository
143         File indexDir = new File( repositoryConfig.getLocation(), ".indexer" );
144         FileUtils.deleteDirectory( indexDir );
145         assertFalse( indexDir.exists() );
146
147         indexDir = new File( repositoryConfig.getLocation(), ".index" );
148         FileUtils.deleteDirectory( indexDir );
149         assertFalse( indexDir.exists() );
150
151         super.tearDown();
152     }
153
154     @Test
155     public void testIndexerIndexArtifact()
156         throws Exception
157     {
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" );
160
161         // begin scan
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();
167
168         assertTrue( scheduler.indexed.contains( artifactFile ) );
169     }
170
171     @Test
172     public void testIndexerArtifactAlreadyIndexed()
173         throws Exception
174     {
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" );
177
178         // begin scan
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();
184
185         assertTrue( scheduler.indexed.contains( artifactFile ) );
186
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();
193
194         assertTrue( scheduler.indexed.contains( artifactFile ) );
195     }
196
197     @Test
198     public void testIndexerIndexArtifactThenPom()
199         throws Exception
200     {
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" );
203
204         // begin scan
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();
210
211         assertTrue( scheduler.indexed.contains( artifactFile ) );
212
213         artifactFile =
214             new File( repositoryConfig.getLocation(), "org/apache/archiva/archiva-index-methods-jar-test/1.0/pom.xml" );
215
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();
221
222         assertTrue( scheduler.indexed.contains( artifactFile ) );
223     }
224
225     // MRM-1275 - Include other file types for the index consumer instead of just the indexable-content
226     @Test
227     public void testIncludedFileTypes()
228         throws Exception
229     {
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" ) );
237     }
238
239 }