]> source.dussan.org Git - archiva.git/blob
85ee9426d608e8a7947102cd902d5d78624ca241
[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.common.utils.PathUtil;
24 import org.apache.archiva.configuration.ArchivaConfiguration;
25 import org.apache.archiva.configuration.FileTypes;
26 import org.apache.archiva.components.taskqueue.TaskQueueException;
27 import org.apache.archiva.repository.base.ArchivaRepositoryRegistry;
28 import org.apache.archiva.repository.base.BasicManagedRepository;
29 import org.apache.archiva.repository.ReleaseScheme;
30 import org.apache.archiva.scheduler.ArchivaTaskScheduler;
31 import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask;
32 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
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
40 import javax.inject.Inject;
41 import java.io.IOException;
42 import java.net.URI;
43 import java.nio.file.Files;
44 import java.nio.file.Path;
45 import java.nio.file.Paths;
46 import java.util.*;
47
48 /**
49  * NexusIndexerConsumerTest
50  */
51 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
52 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
53 public class NexusIndexerConsumerTest
54     extends TestCase
55 {
56     private final class ArchivaTaskSchedulerStub
57         implements ArchivaTaskScheduler<ArtifactIndexingTask>
58     {
59         Set<Path> indexed = new HashSet<>();
60
61         @Override
62         public void queueTask( ArtifactIndexingTask task )
63             throws TaskQueueException
64         {
65             switch ( task.getAction() )
66             {
67                 case ADD:
68                     indexed.add( task.getResourceFile() );
69                     break;
70                 case DELETE:
71                     indexed.remove( task.getResourceFile() );
72                     break;
73                 case FINISH:
74                     try
75                     {
76                         task.getContext().close( false );
77                     }
78                     catch ( IOException e )
79                     {
80                         throw new TaskQueueException( e.getMessage() );
81                     }
82                     break;
83             }
84         }
85     }
86
87     private NexusIndexerConsumer nexusIndexerConsumer;
88
89     private BasicManagedRepository repositoryConfig;
90
91     private ArchivaTaskSchedulerStub scheduler;
92
93     @Inject
94     private ApplicationContext applicationContext;
95
96     @Inject
97     ArchivaRepositoryRegistry repositoryRegistry;
98
99
100     @Override
101     @Before
102     public void setUp()
103         throws Exception
104     {
105         super.setUp();
106
107         scheduler = new ArchivaTaskSchedulerStub();
108
109         ArchivaConfiguration configuration = applicationContext.getBean( ArchivaConfiguration.class );
110
111         FileTypes filetypes = applicationContext.getBean( FileTypes.class );
112
113         nexusIndexerConsumer =
114             new NexusIndexerConsumer( scheduler, configuration, filetypes);
115
116         // initialize to set the file types to be processed
117         nexusIndexerConsumer.initialize();
118
119         repositoryConfig = BasicManagedRepository.newFilesystemInstance( "test-repo", "Test Repository", Paths.get("target/test-classes").resolve("test-repo") );
120         repositoryConfig.setLocation( new URI("target/test-classes/test-repo") );
121         repositoryConfig.setLayout( "default" );
122         repositoryConfig.setScanned( true );
123         repositoryConfig.addActiveReleaseScheme( ReleaseScheme.RELEASE );
124         repositoryConfig.removeActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
125         repositoryRegistry.putRepository(repositoryConfig);
126     }
127
128
129     @Override
130     @After
131     public void tearDown()
132         throws Exception
133     {
134         // delete created index in the repository
135         Path basePath = PathUtil.getPathFromUri( repositoryConfig.getLocation() );
136         Path indexDir = basePath.resolve( ".indexer" );
137         org.apache.archiva.common.utils.FileUtils.deleteDirectory( indexDir );
138         assertFalse( Files.exists(indexDir) );
139
140         indexDir = basePath.resolve( ".index" );
141         org.apache.archiva.common.utils.FileUtils.deleteDirectory( indexDir );
142         assertFalse( Files.exists(indexDir) );
143
144         repositoryRegistry.destroy();
145
146         super.tearDown();
147     }
148
149     @Test
150     public void testIndexerIndexArtifact()
151         throws Exception
152     {
153         Path basePath = PathUtil.getPathFromUri( repositoryConfig.getLocation() );
154         Path artifactFile = basePath.resolve(
155                                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
156
157         // begin scan
158         Date now = Calendar.getInstance().getTime();
159         nexusIndexerConsumer.beginScan( repositoryConfig, now );
160         nexusIndexerConsumer.processFile(
161             "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
162         nexusIndexerConsumer.completeScan();
163
164         assertTrue( scheduler.indexed.contains( artifactFile ) );
165     }
166
167     @Test
168     public void testIndexerArtifactAlreadyIndexed()
169         throws Exception
170     {
171         Path basePath = PathUtil.getPathFromUri( repositoryConfig.getLocation() );
172         Path artifactFile = basePath.resolve(
173                                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
174
175         // begin scan
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();
181
182         assertTrue( scheduler.indexed.contains( artifactFile ) );
183
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();
190
191         assertTrue( scheduler.indexed.contains( artifactFile ) );
192     }
193
194     @Test
195     public void testIndexerIndexArtifactThenPom()
196         throws Exception
197     {
198         Path basePath = PathUtil.getPathFromUri( repositoryConfig.getLocation( ) );
199         Path artifactFile = basePath.resolve(
200                                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
201
202         // begin scan
203         Date now = Calendar.getInstance().getTime();
204         nexusIndexerConsumer.beginScan( repositoryConfig, now );
205         nexusIndexerConsumer.processFile(
206             "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
207         nexusIndexerConsumer.completeScan();
208
209         assertTrue( scheduler.indexed.contains( artifactFile ) );
210
211         artifactFile =
212             basePath.resolve( "org/apache/archiva/archiva-index-methods-jar-test/1.0/pom.xml" );
213
214         // scan and index again
215         now = Calendar.getInstance().getTime();
216         nexusIndexerConsumer.beginScan( repositoryConfig, now );
217         nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/pom.xml" );
218         nexusIndexerConsumer.completeScan();
219
220         assertTrue( scheduler.indexed.contains( artifactFile ) );
221     }
222
223     // MRM-1275 - Include other file types for the index consumer instead of just the indexable-content
224     @Test
225     public void testIncludedFileTypes()
226         throws Exception
227     {
228         List<String> includes = nexusIndexerConsumer.getIncludes();
229         assertTrue( ".pom artifacts should be processed.", includes.contains( "**/*.pom" ) );
230         assertTrue( ".xml artifacts should be processed.", includes.contains( "**/*.xml" ) );
231         assertTrue( ".txt artifacts should be processed.", includes.contains( "**/*.txt" ) );
232         assertTrue( ".jar artifacts should be processed.", includes.contains( "**/*.jar" ) );
233         assertTrue( ".war artifacts should be processed.", includes.contains( "**/*.war" ) );
234         assertTrue( ".zip artifacts should be processed.", includes.contains( "**/*.zip" ) );
235     }
236
237 }