]> source.dussan.org Git - archiva.git/blob
a19b16db773f6231588a4cf0e0368afc7f73ca63
[archiva.git] /
1 package org.apache.archiva.repository.scanner;
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.beans.RemoteRepository;
25 import org.apache.commons.io.FileUtils;
26 import org.apache.archiva.consumers.InvalidRepositoryContentConsumer;
27 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.springframework.context.ApplicationContext;
31 import org.springframework.test.context.ContextConfiguration;
32
33 import javax.inject.Inject;
34 import java.io.File;
35 import java.io.IOException;
36 import java.text.ParseException;
37 import java.text.SimpleDateFormat;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.List;
41 import java.util.Locale;
42 import java.util.TimeZone;
43 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
44
45 /**
46  * RepositoryScannerTest
47  *
48  *
49  */
50 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
51 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml" } )
52 public class RepositoryScannerTest
53     extends TestCase
54 {
55
56     @Inject
57     ApplicationContext applicationContext;
58
59     protected ManagedRepository createRepository( String id, String name, File location )
60     {
61         ManagedRepository repo = new ManagedRepository();
62         repo.setId( id );
63         repo.setName( name );
64         repo.setLocation( location.getAbsolutePath() );
65         return repo;
66     }
67
68     protected RemoteRepository createRemoteRepository( String id, String name, String url )
69     {
70         RemoteRepository repo = new RemoteRepository();
71         repo.setId( id );
72         repo.setName( name );
73         repo.setUrl( url );
74         return repo;
75     }
76
77     private static final String[] ARTIFACT_PATTERNS =
78         new String[]{ "**/*.jar", "**/*.pom", "**/*.rar", "**/*.zip", "**/*.war", "**/*.tar.gz" };
79
80     private ManagedRepository createDefaultRepository()
81     {
82         File repoDir = new File( "src/test/repositories/default-repository" );
83
84         assertTrue( "Default Test Repository should exist.", repoDir.exists() && repoDir.isDirectory() );
85
86         return createRepository( "testDefaultRepo", "Test Default Repository", repoDir );
87     }
88
89     private ManagedRepository createSimpleRepository()
90         throws IOException, ParseException
91     {
92         File srcDir = new File( "src/test/repositories/simple-repository" );
93
94         File repoDir = new File( "target/test-repos/simple-repository" );
95
96         FileUtils.deleteDirectory( repoDir );
97
98         FileUtils.copyDirectory( srcDir, repoDir );
99
100         File repoFile = new File( repoDir,
101                                   "groupId/snapshot-artifact/1.0-alpha-1-SNAPSHOT/snapshot-artifact-1.0-alpha-1-20050611.202024-1.pom" );
102         repoFile.setLastModified( getTimestampAsMillis( "20050611.202024" ) );
103
104         assertTrue( "Simple Test Repository should exist.", repoDir.exists() && repoDir.isDirectory() );
105
106         return createRepository( "testSimpleRepo", "Test Simple Repository", repoDir );
107     }
108
109     private static long getTimestampAsMillis( String timestamp )
110         throws ParseException
111     {
112         SimpleDateFormat fmt = new SimpleDateFormat( "yyyyMMdd.HHmmss", Locale.US );
113         fmt.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
114         return fmt.parse( timestamp ).getTime();
115     }
116
117     private ManagedRepository createLegacyRepository()
118     {
119         File repoDir = new File( "src/test/repositories/legacy-repository" );
120
121         assertTrue( "Legacy Test Repository should exist.", repoDir.exists() && repoDir.isDirectory() );
122
123         ManagedRepository repo = createRepository( "testLegacyRepo", "Test Legacy Repository", repoDir );
124         repo.setLayout( "legacy" );
125
126         return repo;
127     }
128
129     private void assertMinimumHits( String msg, int minimumHitCount, long actualCount )
130     {
131         if ( actualCount < minimumHitCount )
132         {
133             fail( "Minimum hit count on " + msg + " not satisfied.  Expected more than <" + minimumHitCount
134                       + ">, but actually got <" + actualCount + ">." );
135         }
136     }
137
138     private RepositoryScanner lookupRepositoryScanner()
139         throws Exception
140     {
141         return applicationContext.getBean( RepositoryScanner.class );
142     }
143
144     private List<String> getIgnoreList()
145     {
146         List<String> ignores = new ArrayList<>();
147         ignores.addAll( Arrays.asList( RepositoryScanner.IGNORABLE_CONTENT ) );
148         return ignores;
149     }
150
151     @Test
152     public void testTimestampRepositoryScanner()
153         throws Exception
154     {
155         ManagedRepository repository = createSimpleRepository();
156
157         List<KnownRepositoryContentConsumer> knownConsumers = new ArrayList<>();
158         KnownScanConsumer consumer = new KnownScanConsumer();
159         consumer.setIncludes( ARTIFACT_PATTERNS );
160         knownConsumers.add( consumer );
161
162         List<InvalidRepositoryContentConsumer> invalidConsumers = new ArrayList<>();
163         InvalidScanConsumer badconsumer = new InvalidScanConsumer();
164         invalidConsumers.add( badconsumer );
165
166         RepositoryScanner scanner = lookupRepositoryScanner();
167
168         RepositoryScanStatistics stats = scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(),
169                                                        getTimestampAsMillis( "20061101.000000" ) );
170
171         assertNotNull( "Stats should not be null.", stats );
172         assertEquals( "Stats.totalFileCount", 4, stats.getTotalFileCount() );
173         assertEquals( "Stats.newFileCount", 3, stats.getNewFileCount() );
174         assertEquals( "Processed Count", 2, consumer.getProcessCount() );
175         assertEquals( "Processed Count (of invalid items)", 1, badconsumer.getProcessCount() );
176     }
177
178     @Test
179     public void testTimestampRepositoryScannerFreshScan()
180         throws Exception
181     {
182         ManagedRepository repository = createSimpleRepository();
183
184         List<KnownRepositoryContentConsumer> knownConsumers = new ArrayList<>();
185         KnownScanConsumer consumer = new KnownScanConsumer();
186         consumer.setIncludes( ARTIFACT_PATTERNS );
187         knownConsumers.add( consumer );
188
189         List<InvalidRepositoryContentConsumer> invalidConsumers = new ArrayList<>();
190         InvalidScanConsumer badconsumer = new InvalidScanConsumer();
191         invalidConsumers.add( badconsumer );
192
193         RepositoryScanner scanner = lookupRepositoryScanner();
194         RepositoryScanStatistics stats =
195             scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(), RepositoryScanner.FRESH_SCAN );
196
197         assertNotNull( "Stats should not be null.", stats );
198         assertEquals( "Stats.totalFileCount", 4, stats.getTotalFileCount() );
199         assertEquals( "Stats.newFileCount", 4, stats.getNewFileCount() );
200         assertEquals( "Processed Count", 3, consumer.getProcessCount() );
201         assertEquals( "Processed Count (of invalid items)", 1, badconsumer.getProcessCount() );
202     }
203
204     @Test
205     public void testTimestampRepositoryScannerProcessUnmodified()
206         throws Exception
207     {
208         ManagedRepository repository = createSimpleRepository();
209
210         List<KnownRepositoryContentConsumer> knownConsumers = new ArrayList<>();
211         KnownScanConsumer consumer = new KnownScanConsumer();
212         consumer.setProcessUnmodified( true );
213         consumer.setIncludes( ARTIFACT_PATTERNS );
214         knownConsumers.add( consumer );
215
216         List<InvalidRepositoryContentConsumer> invalidConsumers = new ArrayList<>();
217         InvalidScanConsumer badconsumer = new InvalidScanConsumer();
218         invalidConsumers.add( badconsumer );
219
220         RepositoryScanner scanner = lookupRepositoryScanner();
221         RepositoryScanStatistics stats = scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(),
222                                                        getTimestampAsMillis( "20061101.000000" ) );
223
224         assertNotNull( "Stats should not be null.", stats );
225         assertEquals( "Stats.totalFileCount", 4, stats.getTotalFileCount() );
226         assertEquals( "Stats.newFileCount", 3, stats.getNewFileCount() );
227         assertEquals( "Processed Count", 3, consumer.getProcessCount() );
228         assertEquals( "Processed Count (of invalid items)", 1, badconsumer.getProcessCount() );
229     }
230
231     @Test
232     public void testDefaultRepositoryScanner()
233         throws Exception
234     {
235         ManagedRepository repository = createDefaultRepository();
236
237         List<KnownRepositoryContentConsumer> knownConsumers = new ArrayList<>();
238         KnownScanConsumer consumer = new KnownScanConsumer();
239         consumer.setIncludes(
240             new String[]{ "**/*.jar", "**/*.war", "**/*.pom", "**/maven-metadata.xml", "**/*-site.xml", "**/*.zip",
241                 "**/*.tar.gz", "**/*.sha1", "**/*.md5" } );
242         knownConsumers.add( consumer );
243
244         List<InvalidRepositoryContentConsumer> invalidConsumers = new ArrayList<>();
245         InvalidScanConsumer badconsumer = new InvalidScanConsumer();
246         invalidConsumers.add( badconsumer );
247
248         RepositoryScanner scanner = lookupRepositoryScanner();
249         RepositoryScanStatistics stats =
250             scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(), RepositoryScanner.FRESH_SCAN );
251
252         assertNotNull( "Stats should not be null.", stats );
253         assertMinimumHits( "Stats.totalFileCount", 17, stats.getTotalFileCount() );
254         assertMinimumHits( "Processed Count", 17, consumer.getProcessCount() );
255         assertEquals( "Processed Count (of invalid items)", 6, badconsumer.getProcessCount() );
256     }
257
258     @Test
259     public void testDefaultRepositoryArtifactScanner()
260         throws Exception
261     {
262         List<String> actualArtifactPaths = new ArrayList<>();
263
264         actualArtifactPaths.add( "invalid/invalid/1.0-20050611.123456-1/invalid-1.0-20050611.123456-1.jar" );
265         actualArtifactPaths.add( "invalid/invalid/1.0-SNAPSHOT/invalid-1.0.jar" );
266         actualArtifactPaths.add( "invalid/invalid/1.0/invalid-1.0b.jar" );
267         actualArtifactPaths.add( "invalid/invalid/1.0/invalid-2.0.jar" );
268         actualArtifactPaths.add( "invalid/invalid-1.0.jar" );
269         actualArtifactPaths.add( "org/apache/maven/test/1.0-SNAPSHOT/wrong-artifactId-1.0-20050611.112233-1.jar" );
270         actualArtifactPaths.add( "org/apache/maven/test/1.0-SNAPSHOT/test-1.0-20050611.112233-1-javadoc.jar" );
271         actualArtifactPaths.add( "org/apache/maven/test/1.0-SNAPSHOT/test-1.0-20050611.112233-1.jar" );
272         actualArtifactPaths.add( "org/apache/maven/A/1.0/A-1.0.war" );
273         actualArtifactPaths.add( "org/apache/maven/A/1.0/A-1.0.pom" );
274         actualArtifactPaths.add( "org/apache/maven/B/2.0/B-2.0.pom" );
275         actualArtifactPaths.add( "org/apache/maven/B/1.0/B-1.0.pom" );
276         actualArtifactPaths.add( "org/apache/maven/some-ejb/1.0/some-ejb-1.0-client.jar" );
277         actualArtifactPaths.add( "org/apache/maven/C/1.0/C-1.0.war" );
278         actualArtifactPaths.add( "org/apache/maven/C/1.0/C-1.0.pom" );
279         actualArtifactPaths.add( "org/apache/maven/update/test-not-updated/1.0/test-not-updated-1.0.pom" );
280         actualArtifactPaths.add( "org/apache/maven/update/test-not-updated/1.0/test-not-updated-1.0.jar" );
281         actualArtifactPaths.add( "org/apache/maven/update/test-updated/1.0/test-updated-1.0.pom" );
282         actualArtifactPaths.add( "org/apache/maven/update/test-updated/1.0/test-updated-1.0.jar" );
283         actualArtifactPaths.add( "org/apache/maven/discovery/1.0/discovery-1.0.pom" );
284         actualArtifactPaths.add( "org/apache/maven/testing/1.0/testing-1.0-test-sources.jar" );
285         actualArtifactPaths.add( "org/apache/maven/testing/1.0/testing-1.0.jar" );
286         actualArtifactPaths.add( "org/apache/maven/testing/1.0/testing-1.0-sources.jar" );
287         actualArtifactPaths.add( "org/apache/maven/testing/1.0/testing-1.0.zip" );
288         actualArtifactPaths.add( "org/apache/maven/testing/1.0/testing-1.0.tar.gz" );
289         actualArtifactPaths.add( "org/apache/maven/samplejar/2.0/samplejar-2.0.pom" );
290         actualArtifactPaths.add( "org/apache/maven/samplejar/2.0/samplejar-2.0.jar" );
291         actualArtifactPaths.add( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" );
292         actualArtifactPaths.add( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" );
293         actualArtifactPaths.add( "org/apache/testgroup/discovery/1.0/discovery-1.0.pom" );
294         actualArtifactPaths.add( "javax/sql/jdbc/2.0/jdbc-2.0.jar" );
295
296         ManagedRepository repository = createDefaultRepository();
297
298         List<KnownRepositoryContentConsumer> knownConsumers = new ArrayList<>();
299         KnownScanConsumer consumer = new KnownScanConsumer();
300         consumer.setIncludes( ARTIFACT_PATTERNS );
301         knownConsumers.add( consumer );
302
303         List<InvalidRepositoryContentConsumer> invalidConsumers = new ArrayList<>();
304         InvalidScanConsumer badconsumer = new InvalidScanConsumer();
305         invalidConsumers.add( badconsumer );
306
307         RepositoryScanner scanner = lookupRepositoryScanner();
308         RepositoryScanStatistics stats =
309             scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(), RepositoryScanner.FRESH_SCAN );
310
311         assertNotNull( "Stats should not be null.", stats );
312         assertMinimumHits( "Stats.totalFileCount", actualArtifactPaths.size(), stats.getTotalFileCount() );
313         assertMinimumHits( "Processed Count", actualArtifactPaths.size(), consumer.getProcessCount() );
314     }
315
316     @Test
317     public void testDefaultRepositoryMetadataScanner()
318         throws Exception
319     {
320         List<String> actualMetadataPaths = new ArrayList<>();
321
322         actualMetadataPaths.add( "org/apache/maven/some-ejb/1.0/maven-metadata.xml" );
323         actualMetadataPaths.add( "org/apache/maven/update/test-not-updated/maven-metadata.xml" );
324         actualMetadataPaths.add( "org/apache/maven/update/test-updated/maven-metadata.xml" );
325         actualMetadataPaths.add( "org/apache/maven/maven-metadata.xml" );
326         actualMetadataPaths.add( "org/apache/testgroup/discovery/1.0/maven-metadata.xml" );
327         actualMetadataPaths.add( "org/apache/testgroup/discovery/maven-metadata.xml" );
328         actualMetadataPaths.add( "javax/sql/jdbc/2.0/maven-metadata-repository.xml" );
329         actualMetadataPaths.add( "javax/sql/jdbc/maven-metadata-repository.xml" );
330         actualMetadataPaths.add( "javax/sql/maven-metadata-repository.xml" );
331         actualMetadataPaths.add( "javax/maven-metadata.xml" );
332
333         ManagedRepository repository = createDefaultRepository();
334
335         List<KnownRepositoryContentConsumer> knownConsumers = new ArrayList<>();
336         KnownScanConsumer knownConsumer = new KnownScanConsumer();
337         knownConsumer.setIncludes( new String[]{ "**/maven-metadata*.xml" } );
338         knownConsumers.add( knownConsumer );
339
340         List<InvalidRepositoryContentConsumer> invalidConsumers = new ArrayList<>();
341         InvalidScanConsumer badconsumer = new InvalidScanConsumer();
342         invalidConsumers.add( badconsumer );
343
344         RepositoryScanner scanner = lookupRepositoryScanner();
345         RepositoryScanStatistics stats =
346             scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(), RepositoryScanner.FRESH_SCAN );
347
348         assertNotNull( "Stats should not be null.", stats );
349         assertMinimumHits( "Stats.totalFileCount", actualMetadataPaths.size(), stats.getTotalFileCount() );
350         assertMinimumHits( "Processed Count", actualMetadataPaths.size(), knownConsumer.getProcessCount() );
351     }
352
353     @Test
354     public void testDefaultRepositoryProjectScanner()
355         throws Exception
356     {
357         List<String> actualProjectPaths = new ArrayList<>();
358
359         actualProjectPaths.add( "org/apache/maven/A/1.0/A-1.0.pom" );
360         actualProjectPaths.add( "org/apache/maven/B/2.0/B-2.0.pom" );
361         actualProjectPaths.add( "org/apache/maven/B/1.0/B-1.0.pom" );
362         actualProjectPaths.add( "org/apache/maven/C/1.0/C-1.0.pom" );
363         actualProjectPaths.add( "org/apache/maven/update/test-not-updated/1.0/test-not-updated-1.0.pom" );
364         actualProjectPaths.add( "org/apache/maven/update/test-updated/1.0/test-updated-1.0.pom" );
365         actualProjectPaths.add( "org/apache/maven/discovery/1.0/discovery-1.0.pom" );
366         actualProjectPaths.add( "org/apache/maven/samplejar/2.0/samplejar-2.0.pom" );
367         actualProjectPaths.add( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" );
368         actualProjectPaths.add( "org/apache/testgroup/discovery/1.0/discovery-1.0.pom" );
369
370         ManagedRepository repository = createDefaultRepository();
371
372         List<KnownRepositoryContentConsumer> knownConsumers = new ArrayList<>();
373         KnownScanConsumer consumer = new KnownScanConsumer();
374         consumer.setIncludes( new String[]{ "**/*.pom" } );
375         knownConsumers.add( consumer );
376
377         List<InvalidRepositoryContentConsumer> invalidConsumers = new ArrayList<>();
378         InvalidScanConsumer badconsumer = new InvalidScanConsumer();
379         invalidConsumers.add( badconsumer );
380
381         RepositoryScanner scanner = lookupRepositoryScanner();
382         RepositoryScanStatistics stats =
383             scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(), RepositoryScanner.FRESH_SCAN );
384
385         assertNotNull( "Stats should not be null.", stats );
386         assertMinimumHits( "Stats.totalFileCount", actualProjectPaths.size(), stats.getTotalFileCount() );
387         assertMinimumHits( "Processed Count", actualProjectPaths.size(), consumer.getProcessCount() );
388     }
389
390     @Test
391     public void testLegacyRepositoryArtifactScanner()
392         throws Exception
393     {
394         List<String> actualArtifactPaths = new ArrayList<>();
395
396         actualArtifactPaths.add( "invalid/jars/1.0/invalid-1.0.jar" );
397         actualArtifactPaths.add( "invalid/jars/invalid-1.0.rar" );
398         actualArtifactPaths.add( "invalid/jars/invalid.jar" );
399         actualArtifactPaths.add( "invalid/invalid-1.0.jar" );
400         actualArtifactPaths.add( "javax.sql/jars/jdbc-2.0.jar" );
401         actualArtifactPaths.add( "org.apache.maven/jars/some-ejb-1.0-client.jar" );
402         actualArtifactPaths.add( "org.apache.maven/jars/testing-1.0.jar" );
403         actualArtifactPaths.add( "org.apache.maven/jars/testing-1.0-sources.jar" );
404         actualArtifactPaths.add( "org.apache.maven/jars/testing-UNKNOWN.jar" );
405         actualArtifactPaths.add( "org.apache.maven/jars/testing-1.0.zip" );
406         actualArtifactPaths.add( "org.apache.maven/jars/testing-1.0-20050611.112233-1.jar" );
407         actualArtifactPaths.add( "org.apache.maven/jars/testing-1.0.tar.gz" );
408         actualArtifactPaths.add( "org.apache.maven.update/jars/test-not-updated-1.0.jar" );
409         actualArtifactPaths.add( "org.apache.maven.update/jars/test-updated-1.0.jar" );
410
411         ManagedRepository repository = createLegacyRepository();
412
413         List<KnownRepositoryContentConsumer> knownConsumers = new ArrayList<>();
414         KnownScanConsumer consumer = new KnownScanConsumer();
415         consumer.setIncludes( ARTIFACT_PATTERNS );
416         knownConsumers.add( consumer );
417
418         List<InvalidRepositoryContentConsumer> invalidConsumers = new ArrayList<>();
419         InvalidScanConsumer badconsumer = new InvalidScanConsumer();
420         invalidConsumers.add( badconsumer );
421
422         RepositoryScanner scanner = lookupRepositoryScanner();
423         RepositoryScanStatistics stats =
424             scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(), RepositoryScanner.FRESH_SCAN );
425
426         assertNotNull( "Stats should not be null.", stats );
427         assertMinimumHits( "Stats.totalFileCount", actualArtifactPaths.size(), stats.getTotalFileCount() );
428         assertMinimumHits( "Processed Count", actualArtifactPaths.size(), consumer.getProcessCount() );
429     }
430 }