]> source.dussan.org Git - archiva.git/blob
d6198e3d1e8cebb89eb9a5da55222fb01b3f0909
[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.managed.BasicManagedRepository;
29 import org.apache.archiva.repository.ReleaseScheme;
30 import org.apache.archiva.repository.base.group.RepositoryGroupHandler;
31 import org.apache.archiva.repository.base.managed.ManagedRepositoryHandler;
32 import org.apache.archiva.scheduler.ArchivaTaskScheduler;
33 import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask;
34 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.springframework.context.ApplicationContext;
40 import org.springframework.test.context.ContextConfiguration;
41
42 import javax.inject.Inject;
43 import java.io.IOException;
44 import java.net.URI;
45 import java.nio.file.Files;
46 import java.nio.file.Path;
47 import java.nio.file.Paths;
48 import java.util.*;
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<Path> 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 BasicManagedRepository repositoryConfig;
92
93     private ArchivaTaskSchedulerStub scheduler;
94
95     @Inject
96     private ApplicationContext applicationContext;
97
98     @Inject
99     ArchivaRepositoryRegistry repositoryRegistry;
100
101     @SuppressWarnings( "unused" )
102     @Inject
103     RepositoryGroupHandler repositoryGroupHandler;
104
105     @SuppressWarnings( "unused" )
106     @Inject
107     ManagedRepositoryHandler managedRepositoryHandler;
108
109     @Override
110     @Before
111     public void setUp()
112         throws Exception
113     {
114         super.setUp();
115
116         scheduler = new ArchivaTaskSchedulerStub();
117
118         ArchivaConfiguration configuration = applicationContext.getBean( ArchivaConfiguration.class );
119
120         FileTypes filetypes = applicationContext.getBean( FileTypes.class );
121
122         nexusIndexerConsumer =
123             new NexusIndexerConsumer( scheduler, configuration, filetypes);
124
125         // initialize to set the file types to be processed
126         nexusIndexerConsumer.initialize();
127
128         repositoryConfig = BasicManagedRepository.newFilesystemInstance( "test-repo", "Test Repository", Paths.get("target/test-classes").resolve("test-repo") );
129         repositoryConfig.setLocation( new URI("target/test-classes/test-repo") );
130         repositoryConfig.setLayout( "default" );
131         repositoryConfig.setScanned( true );
132         repositoryConfig.addActiveReleaseScheme( ReleaseScheme.RELEASE );
133         repositoryConfig.removeActiveReleaseScheme( ReleaseScheme.SNAPSHOT );
134         repositoryRegistry.putRepository(repositoryConfig);
135     }
136
137
138     @Override
139     @After
140     public void tearDown()
141         throws Exception
142     {
143         // delete created index in the repository
144         Path basePath = PathUtil.getPathFromUri( repositoryConfig.getLocation() );
145         Path indexDir = basePath.resolve( ".indexer" );
146         org.apache.archiva.common.utils.FileUtils.deleteDirectory( indexDir );
147         assertFalse( Files.exists(indexDir) );
148
149         indexDir = basePath.resolve( ".index" );
150         org.apache.archiva.common.utils.FileUtils.deleteDirectory( indexDir );
151         assertFalse( Files.exists(indexDir) );
152
153         repositoryRegistry.destroy();
154
155         super.tearDown();
156     }
157
158     @Test
159     public void testIndexerIndexArtifact()
160         throws Exception
161     {
162         Path basePath = PathUtil.getPathFromUri( repositoryConfig.getLocation() );
163         Path artifactFile = basePath.resolve(
164                                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
165
166         // begin scan
167         Date now = Calendar.getInstance().getTime();
168         nexusIndexerConsumer.beginScan( repositoryConfig, now );
169         nexusIndexerConsumer.processFile(
170             "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
171         nexusIndexerConsumer.completeScan();
172
173         assertTrue( scheduler.indexed.contains( artifactFile ) );
174     }
175
176     @Test
177     public void testIndexerArtifactAlreadyIndexed()
178         throws Exception
179     {
180         Path basePath = PathUtil.getPathFromUri( repositoryConfig.getLocation() );
181         Path artifactFile = basePath.resolve(
182                                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
183
184         // begin scan
185         Date 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         // scan and index again
194         now = Calendar.getInstance().getTime();
195         nexusIndexerConsumer.beginScan( repositoryConfig, now );
196         nexusIndexerConsumer.processFile(
197             "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
198         nexusIndexerConsumer.completeScan();
199
200         assertTrue( scheduler.indexed.contains( artifactFile ) );
201     }
202
203     @Test
204     public void testIndexerIndexArtifactThenPom()
205         throws Exception
206     {
207         Path basePath = PathUtil.getPathFromUri( repositoryConfig.getLocation( ) );
208         Path artifactFile = basePath.resolve(
209                                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
210
211         // begin scan
212         Date now = Calendar.getInstance().getTime();
213         nexusIndexerConsumer.beginScan( repositoryConfig, now );
214         nexusIndexerConsumer.processFile(
215             "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
216         nexusIndexerConsumer.completeScan();
217
218         assertTrue( scheduler.indexed.contains( artifactFile ) );
219
220         artifactFile =
221             basePath.resolve( "org/apache/archiva/archiva-index-methods-jar-test/1.0/pom.xml" );
222
223         // scan and index again
224         now = Calendar.getInstance().getTime();
225         nexusIndexerConsumer.beginScan( repositoryConfig, now );
226         nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/pom.xml" );
227         nexusIndexerConsumer.completeScan();
228
229         assertTrue( scheduler.indexed.contains( artifactFile ) );
230     }
231
232     // MRM-1275 - Include other file types for the index consumer instead of just the indexable-content
233     @Test
234     public void testIncludedFileTypes()
235         throws Exception
236     {
237         List<String> includes = nexusIndexerConsumer.getIncludes();
238         assertTrue( ".pom artifacts should be processed.", includes.contains( "**/*.pom" ) );
239         assertTrue( ".xml artifacts should be processed.", includes.contains( "**/*.xml" ) );
240         assertTrue( ".txt artifacts should be processed.", includes.contains( "**/*.txt" ) );
241         assertTrue( ".jar artifacts should be processed.", includes.contains( "**/*.jar" ) );
242         assertTrue( ".war artifacts should be processed.", includes.contains( "**/*.war" ) );
243         assertTrue( ".zip artifacts should be processed.", includes.contains( "**/*.zip" ) );
244     }
245
246 }