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