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