]> source.dussan.org Git - archiva.git/blob
7d6ea28eacbe7cdb668f578c8c628e966405030f
[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.configuration.ArchivaConfiguration;
26 import org.apache.archiva.configuration.FileTypes;
27 import org.apache.archiva.redback.components.taskqueue.TaskQueueException;
28 import org.apache.archiva.scheduler.ArchivaTaskScheduler;
29 import org.apache.archiva.scheduler.indexing.ArtifactIndexingTask;
30 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
31 import org.apache.maven.index.NexusIndexer;
32 import org.apache.maven.index.context.IndexCreator;
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.nio.file.Files;
43 import java.nio.file.Path;
44 import java.nio.file.Paths;
45 import java.util.Calendar;
46 import java.util.Date;
47 import java.util.HashSet;
48 import java.util.List;
49 import java.util.Set;
50
51 /**
52  * NexusIndexerConsumerTest
53  */
54 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
55 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
56 public class NexusIndexerConsumerTest
57     extends TestCase
58 {
59     private final class ArchivaTaskSchedulerStub
60         implements ArchivaTaskScheduler<ArtifactIndexingTask>
61     {
62         Set<Path> indexed = new HashSet<>();
63
64         @Override
65         public void queueTask( ArtifactIndexingTask task )
66             throws TaskQueueException
67         {
68             switch ( task.getAction() )
69             {
70                 case ADD:
71                     indexed.add( task.getResourceFile().toPath() );
72                     break;
73                 case DELETE:
74                     indexed.remove( task.getResourceFile() );
75                     break;
76                 case FINISH:
77                     try
78                     {
79                         task.getContext().close( false );
80                     }
81                     catch ( IOException e )
82                     {
83                         throw new TaskQueueException( e.getMessage() );
84                     }
85                     break;
86             }
87         }
88     }
89
90     private NexusIndexerConsumer nexusIndexerConsumer;
91
92     private ManagedRepository repositoryConfig;
93
94     private ArchivaTaskSchedulerStub scheduler;
95
96     @Inject
97     private ApplicationContext applicationContext;
98
99     @Inject
100     private NexusIndexer nexusIndexer;
101
102     @Inject
103     private List<IndexCreator> indexCreators;
104
105     @Inject
106     private ManagedRepositoryAdmin managedRepositoryAdmin;
107
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, indexCreators,
124                                       managedRepositoryAdmin, nexusIndexer );
125
126         // initialize to set the file types to be processed
127         nexusIndexerConsumer.initialize();
128
129         repositoryConfig = new ManagedRepository();
130         repositoryConfig.setId( "test-repo" );
131         repositoryConfig.setLocation( "target/test-classes/test-repo" );
132         repositoryConfig.setLayout( "default" );
133         repositoryConfig.setName( "Test Repository" );
134         repositoryConfig.setScanned( true );
135         repositoryConfig.setSnapshots( false );
136         repositoryConfig.setReleases( true );
137     }
138
139     @Override
140     @After
141     public void tearDown()
142         throws Exception
143     {
144         // delete created index in the repository
145         Path indexDir = Paths.get( repositoryConfig.getLocation(), ".indexer" );
146         org.apache.archiva.common.utils.FileUtils.deleteDirectory( indexDir );
147         assertFalse( Files.exists(indexDir) );
148
149         indexDir = Paths.get( repositoryConfig.getLocation(), ".index" );
150         org.apache.archiva.common.utils.FileUtils.deleteDirectory( indexDir );
151         assertFalse( Files.exists(indexDir) );
152
153         super.tearDown();
154     }
155
156     @Test
157     public void testIndexerIndexArtifact()
158         throws Exception
159     {
160         Path artifactFile = Paths.get( repositoryConfig.getLocation(),
161                                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
162
163         // begin scan
164         Date now = Calendar.getInstance().getTime();
165         nexusIndexerConsumer.beginScan( repositoryConfig, now );
166         nexusIndexerConsumer.processFile(
167             "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
168         nexusIndexerConsumer.completeScan();
169
170         assertTrue( scheduler.indexed.contains( artifactFile ) );
171     }
172
173     @Test
174     public void testIndexerArtifactAlreadyIndexed()
175         throws Exception
176     {
177         Path artifactFile = Paths.get( repositoryConfig.getLocation(),
178                                       "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
179
180         // begin scan
181         Date now = Calendar.getInstance().getTime();
182         nexusIndexerConsumer.beginScan( repositoryConfig, now );
183         nexusIndexerConsumer.processFile(
184             "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
185         nexusIndexerConsumer.completeScan();
186
187         assertTrue( scheduler.indexed.contains( artifactFile ) );
188
189         // scan and index again
190         now = Calendar.getInstance().getTime();
191         nexusIndexerConsumer.beginScan( repositoryConfig, now );
192         nexusIndexerConsumer.processFile(
193             "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
194         nexusIndexerConsumer.completeScan();
195
196         assertTrue( scheduler.indexed.contains( artifactFile ) );
197     }
198
199     @Test
200     public void testIndexerIndexArtifactThenPom()
201         throws Exception
202     {
203         Path artifactFile = Paths.get( repositoryConfig.getLocation(),
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             Paths.get( repositoryConfig.getLocation(), "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 }