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