]> source.dussan.org Git - archiva.git/blob
b5e688b8861b7fab41d3bb2dab27a10ebfe291a7
[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.configuration.ArchivaConfiguration;
27 import org.apache.archiva.configuration.FileTypes;
28 import org.apache.archiva.redback.components.taskqueue.TaskQueueException;
29 import org.apache.archiva.scheduler.ArchivaTaskScheduler;
30 import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask;
31 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
32 import org.apache.commons.io.FileUtils;
33 import org.apache.maven.index.NexusIndexer;
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
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( ArchivaSpringJUnit4ClassRunner.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<>();
62
63         @Override
64         public void queueTask( ArtifactIndexingTask task )
65             throws TaskQueueException
66         {
67             switch ( task.getAction() )
68             {
69                 case ADD:
70                     indexed.add( task.getResourceFile() );
71                     break;
72                 case DELETE:
73                     indexed.remove( task.getResourceFile() );
74                     break;
75                 case FINISH:
76                     try
77                     {
78                         task.getContext().close( false );
79                     }
80                     catch ( IOException e )
81                     {
82                         throw new TaskQueueException( e.getMessage() );
83                     }
84                     break;
85             }
86         }
87     }
88
89     private NexusIndexerConsumer nexusIndexerConsumer;
90
91     private ManagedRepository repositoryConfig;
92
93     private ArchivaTaskSchedulerStub scheduler;
94
95     @Inject
96     private ApplicationContext applicationContext;
97
98     @Inject
99     private NexusIndexer nexusIndexer;
100
101     @Inject
102     private MavenIndexerUtils mavenIndexerUtils;
103
104     @Inject
105     private ManagedRepositoryAdmin managedRepositoryAdmin;
106
107
108     @Override
109     @Before
110     public void setUp()
111         throws Exception
112     {
113         super.setUp();
114
115         scheduler = new ArchivaTaskSchedulerStub();
116
117         ArchivaConfiguration configuration = applicationContext.getBean( ArchivaConfiguration.class );
118
119         FileTypes filetypes = applicationContext.getBean( FileTypes.class );
120
121         nexusIndexerConsumer =
122             new NexusIndexerConsumer( scheduler, configuration, filetypes, mavenIndexerUtils,
123                                       managedRepositoryAdmin, nexusIndexer );
124
125         // initialize to set the file types to be processed
126         ( (NexusIndexerConsumer) nexusIndexerConsumer ).initialize();
127
128         repositoryConfig = new ManagedRepository();
129         repositoryConfig.setId( "test-repo" );
130         repositoryConfig.setLocation( "target/test-classes/test-repo" );
131         repositoryConfig.setLayout( "default" );
132         repositoryConfig.setName( "Test Repository" );
133         repositoryConfig.setScanned( true );
134         repositoryConfig.setSnapshots( false );
135         repositoryConfig.setReleases( true );
136     }
137
138     @Override
139     @After
140     public void tearDown()
141         throws Exception
142     {
143         // delete created index in the repository
144         File indexDir = new File( repositoryConfig.getLocation(), ".indexer" );
145         FileUtils.deleteDirectory( indexDir );
146         assertFalse( indexDir.exists() );
147
148         indexDir = new File( repositoryConfig.getLocation(), ".index" );
149         FileUtils.deleteDirectory( indexDir );
150         assertFalse( indexDir.exists() );
151
152         super.tearDown();
153     }
154
155     @Test
156     public void testIndexerIndexArtifact()
157         throws Exception
158     {
159         File artifactFile = new File( repositoryConfig.getLocation(),
160                                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
161
162         // begin scan
163         Date now = Calendar.getInstance().getTime();
164         nexusIndexerConsumer.beginScan( repositoryConfig, now );
165         nexusIndexerConsumer.processFile(
166             "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
167         nexusIndexerConsumer.completeScan();
168
169         assertTrue( scheduler.indexed.contains( artifactFile ) );
170     }
171
172     @Test
173     public void testIndexerArtifactAlreadyIndexed()
174         throws Exception
175     {
176         File artifactFile = new File( repositoryConfig.getLocation(),
177                                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
178
179         // begin scan
180         Date now = Calendar.getInstance().getTime();
181         nexusIndexerConsumer.beginScan( repositoryConfig, now );
182         nexusIndexerConsumer.processFile(
183             "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         // scan and index again
189         now = Calendar.getInstance().getTime();
190         nexusIndexerConsumer.beginScan( repositoryConfig, now );
191         nexusIndexerConsumer.processFile(
192             "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
193         nexusIndexerConsumer.completeScan();
194
195         assertTrue( scheduler.indexed.contains( artifactFile ) );
196     }
197
198     @Test
199     public void testIndexerIndexArtifactThenPom()
200         throws Exception
201     {
202         File artifactFile = new File( repositoryConfig.getLocation(),
203                                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
204
205         // begin scan
206         Date now = Calendar.getInstance().getTime();
207         nexusIndexerConsumer.beginScan( repositoryConfig, now );
208         nexusIndexerConsumer.processFile(
209             "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
210         nexusIndexerConsumer.completeScan();
211
212         assertTrue( scheduler.indexed.contains( artifactFile ) );
213
214         artifactFile =
215             new File( repositoryConfig.getLocation(), "org/apache/archiva/archiva-index-methods-jar-test/1.0/pom.xml" );
216
217         // scan and index again
218         now = Calendar.getInstance().getTime();
219         nexusIndexerConsumer.beginScan( repositoryConfig, now );
220         nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/pom.xml" );
221         nexusIndexerConsumer.completeScan();
222
223         assertTrue( scheduler.indexed.contains( artifactFile ) );
224     }
225
226     // MRM-1275 - Include other file types for the index consumer instead of just the indexable-content
227     @Test
228     public void testIncludedFileTypes()
229         throws Exception
230     {
231         List<String> includes = nexusIndexerConsumer.getIncludes();
232         assertTrue( ".pom artifacts should be processed.", includes.contains( "**/*.pom" ) );
233         assertTrue( ".xml artifacts should be processed.", includes.contains( "**/*.xml" ) );
234         assertTrue( ".txt artifacts should be processed.", includes.contains( "**/*.txt" ) );
235         assertTrue( ".jar artifacts should be processed.", includes.contains( "**/*.jar" ) );
236         assertTrue( ".war artifacts should be processed.", includes.contains( "**/*.war" ) );
237         assertTrue( ".zip artifacts should be processed.", includes.contains( "**/*.zip" ) );
238     }
239
240 }