]> source.dussan.org Git - archiva.git/blob
241eeb09bd00f59227c40a4660d42928aa767cf1
[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.repository.base.RepositoryGroupHandler;
31 import org.apache.archiva.scheduler.ArchivaTaskScheduler;
32 import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask;
33 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
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.IOException;
43 import java.net.URI;
44 import java.nio.file.Files;
45 import java.nio.file.Path;
46 import java.nio.file.Paths;
47 import java.util.*;
48
49 /**
50  * NexusIndexerConsumerTest
51  */
52 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
53 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
54 public class NexusIndexerConsumerTest
55     extends TestCase
56 {
57     private final class ArchivaTaskSchedulerStub
58         implements ArchivaTaskScheduler<ArtifactIndexingTask>
59     {
60         Set<Path> indexed = new HashSet<>();
61
62         @Override
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 BasicManagedRepository repositoryConfig;
91
92     private ArchivaTaskSchedulerStub scheduler;
93
94     @Inject
95     private ApplicationContext applicationContext;
96
97     @Inject
98     ArchivaRepositoryRegistry repositoryRegistry;
99
100     @SuppressWarnings( "unused" )
101     @Inject
102     RepositoryGroupHandler repositoryGroupHandler;
103
104     @Override
105     @Before
106     public void setUp()
107         throws Exception
108     {
109         super.setUp();
110
111         scheduler = new ArchivaTaskSchedulerStub();
112
113         ArchivaConfiguration configuration = applicationContext.getBean( ArchivaConfiguration.class );
114
115         FileTypes filetypes = applicationContext.getBean( FileTypes.class );
116
117         nexusIndexerConsumer =
118             new NexusIndexerConsumer( scheduler, configuration, filetypes);
119
120         // initialize to set the file types to be processed
121         nexusIndexerConsumer.initialize();
122
123         repositoryConfig = BasicManagedRepository.newFilesystemInstance( "test-repo", "Test Repository", Paths.get("target/test-classes").resolve("test-repo") );
124         repositoryConfig.setLocation( new URI("target/test-classes/test-repo") );
125         repositoryConfig.setLayout( "default" );
126         repositoryConfig.setScanned( true );
127         repositoryConfig.addActiveReleaseScheme( ReleaseScheme.RELEASE );
128         repositoryConfig.removeActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
129         repositoryRegistry.putRepository(repositoryConfig);
130     }
131
132
133     @Override
134     @After
135     public void tearDown()
136         throws Exception
137     {
138         // delete created index in the repository
139         Path basePath = PathUtil.getPathFromUri( repositoryConfig.getLocation() );
140         Path indexDir = basePath.resolve( ".indexer" );
141         org.apache.archiva.common.utils.FileUtils.deleteDirectory( indexDir );
142         assertFalse( Files.exists(indexDir) );
143
144         indexDir = basePath.resolve( ".index" );
145         org.apache.archiva.common.utils.FileUtils.deleteDirectory( indexDir );
146         assertFalse( Files.exists(indexDir) );
147
148         repositoryRegistry.destroy();
149
150         super.tearDown();
151     }
152
153     @Test
154     public void testIndexerIndexArtifact()
155         throws Exception
156     {
157         Path basePath = PathUtil.getPathFromUri( repositoryConfig.getLocation() );
158         Path artifactFile = basePath.resolve(
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         Path basePath = PathUtil.getPathFromUri( repositoryConfig.getLocation() );
176         Path artifactFile = basePath.resolve(
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         Path basePath = PathUtil.getPathFromUri( repositoryConfig.getLocation( ) );
203         Path artifactFile = basePath.resolve(
204                                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
205
206         // begin scan
207         Date now = Calendar.getInstance().getTime();
208         nexusIndexerConsumer.beginScan( repositoryConfig, now );
209         nexusIndexerConsumer.processFile(
210             "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
211         nexusIndexerConsumer.completeScan();
212
213         assertTrue( scheduler.indexed.contains( artifactFile ) );
214
215         artifactFile =
216             basePath.resolve( "org/apache/archiva/archiva-index-methods-jar-test/1.0/pom.xml" );
217
218         // scan and index again
219         now = Calendar.getInstance().getTime();
220         nexusIndexerConsumer.beginScan( repositoryConfig, now );
221         nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/pom.xml" );
222         nexusIndexerConsumer.completeScan();
223
224         assertTrue( scheduler.indexed.contains( artifactFile ) );
225     }
226
227     // MRM-1275 - Include other file types for the index consumer instead of just the indexable-content
228     @Test
229     public void testIncludedFileTypes()
230         throws Exception
231     {
232         List<String> includes = nexusIndexerConsumer.getIncludes();
233         assertTrue( ".pom artifacts should be processed.", includes.contains( "**/*.pom" ) );
234         assertTrue( ".xml artifacts should be processed.", includes.contains( "**/*.xml" ) );
235         assertTrue( ".txt artifacts should be processed.", includes.contains( "**/*.txt" ) );
236         assertTrue( ".jar artifacts should be processed.", includes.contains( "**/*.jar" ) );
237         assertTrue( ".war artifacts should be processed.", includes.contains( "**/*.war" ) );
238         assertTrue( ".zip artifacts should be processed.", includes.contains( "**/*.zip" ) );
239     }
240
241 }