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