1 package org.apache.archiva.repository.scanner;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
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;
33 import javax.inject.Inject;
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;
46 * RepositoryScannerTest
50 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
51 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml" } )
52 public class RepositoryScannerTest
57 ApplicationContext applicationContext;
59 protected ManagedRepository createRepository( String id, String name, File location )
61 ManagedRepository repo = new ManagedRepository();
64 repo.setLocation( location.getAbsolutePath() );
68 protected RemoteRepository createRemoteRepository( String id, String name, String url )
70 RemoteRepository repo = new RemoteRepository();
77 private static final String[] ARTIFACT_PATTERNS =
78 new String[]{ "**/*.jar", "**/*.pom", "**/*.rar", "**/*.zip", "**/*.war", "**/*.tar.gz" };
80 private ManagedRepository createDefaultRepository()
82 File repoDir = new File( "src/test/repositories/default-repository" );
84 assertTrue( "Default Test Repository should exist.", repoDir.exists() && repoDir.isDirectory() );
86 return createRepository( "testDefaultRepo", "Test Default Repository", repoDir );
89 private ManagedRepository createSimpleRepository()
90 throws IOException, ParseException
92 File srcDir = new File( "src/test/repositories/simple-repository" );
94 File repoDir = new File( "target/test-repos/simple-repository" );
96 FileUtils.deleteDirectory( repoDir );
98 FileUtils.copyDirectory( srcDir, repoDir );
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" ) );
104 assertTrue( "Simple Test Repository should exist.", repoDir.exists() && repoDir.isDirectory() );
106 return createRepository( "testSimpleRepo", "Test Simple Repository", repoDir );
109 private static long getTimestampAsMillis( String timestamp )
110 throws ParseException
112 SimpleDateFormat fmt = new SimpleDateFormat( "yyyyMMdd.HHmmss", Locale.US );
113 fmt.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
114 return fmt.parse( timestamp ).getTime();
117 private ManagedRepository createLegacyRepository()
119 File repoDir = new File( "src/test/repositories/legacy-repository" );
121 assertTrue( "Legacy Test Repository should exist.", repoDir.exists() && repoDir.isDirectory() );
123 ManagedRepository repo = createRepository( "testLegacyRepo", "Test Legacy Repository", repoDir );
124 repo.setLayout( "legacy" );
129 private void assertMinimumHits( String msg, int minimumHitCount, long actualCount )
131 if ( actualCount < minimumHitCount )
133 fail( "Minimum hit count on " + msg + " not satisfied. Expected more than <" + minimumHitCount
134 + ">, but actually got <" + actualCount + ">." );
138 private RepositoryScanner lookupRepositoryScanner()
141 return applicationContext.getBean( RepositoryScanner.class );
144 private List<String> getIgnoreList()
146 List<String> ignores = new ArrayList<>();
147 ignores.addAll( Arrays.asList( RepositoryScanner.IGNORABLE_CONTENT ) );
152 public void testTimestampRepositoryScanner()
155 ManagedRepository repository = createSimpleRepository();
157 List<KnownRepositoryContentConsumer> knownConsumers = new ArrayList<>();
158 KnownScanConsumer consumer = new KnownScanConsumer();
159 consumer.setIncludes( ARTIFACT_PATTERNS );
160 knownConsumers.add( consumer );
162 List<InvalidRepositoryContentConsumer> invalidConsumers = new ArrayList<>();
163 InvalidScanConsumer badconsumer = new InvalidScanConsumer();
164 invalidConsumers.add( badconsumer );
166 RepositoryScanner scanner = lookupRepositoryScanner();
168 RepositoryScanStatistics stats = scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(),
169 getTimestampAsMillis( "20061101.000000" ) );
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() );
179 public void testTimestampRepositoryScannerFreshScan()
182 ManagedRepository repository = createSimpleRepository();
184 List<KnownRepositoryContentConsumer> knownConsumers = new ArrayList<>();
185 KnownScanConsumer consumer = new KnownScanConsumer();
186 consumer.setIncludes( ARTIFACT_PATTERNS );
187 knownConsumers.add( consumer );
189 List<InvalidRepositoryContentConsumer> invalidConsumers = new ArrayList<>();
190 InvalidScanConsumer badconsumer = new InvalidScanConsumer();
191 invalidConsumers.add( badconsumer );
193 RepositoryScanner scanner = lookupRepositoryScanner();
194 RepositoryScanStatistics stats =
195 scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(), RepositoryScanner.FRESH_SCAN );
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() );
205 public void testTimestampRepositoryScannerProcessUnmodified()
208 ManagedRepository repository = createSimpleRepository();
210 List<KnownRepositoryContentConsumer> knownConsumers = new ArrayList<>();
211 KnownScanConsumer consumer = new KnownScanConsumer();
212 consumer.setProcessUnmodified( true );
213 consumer.setIncludes( ARTIFACT_PATTERNS );
214 knownConsumers.add( consumer );
216 List<InvalidRepositoryContentConsumer> invalidConsumers = new ArrayList<>();
217 InvalidScanConsumer badconsumer = new InvalidScanConsumer();
218 invalidConsumers.add( badconsumer );
220 RepositoryScanner scanner = lookupRepositoryScanner();
221 RepositoryScanStatistics stats = scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(),
222 getTimestampAsMillis( "20061101.000000" ) );
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() );
232 public void testDefaultRepositoryScanner()
235 ManagedRepository repository = createDefaultRepository();
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 );
244 List<InvalidRepositoryContentConsumer> invalidConsumers = new ArrayList<>();
245 InvalidScanConsumer badconsumer = new InvalidScanConsumer();
246 invalidConsumers.add( badconsumer );
248 RepositoryScanner scanner = lookupRepositoryScanner();
249 RepositoryScanStatistics stats =
250 scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(), RepositoryScanner.FRESH_SCAN );
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() );
259 public void testDefaultRepositoryArtifactScanner()
262 List<String> actualArtifactPaths = new ArrayList<>();
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" );
296 ManagedRepository repository = createDefaultRepository();
298 List<KnownRepositoryContentConsumer> knownConsumers = new ArrayList<>();
299 KnownScanConsumer consumer = new KnownScanConsumer();
300 consumer.setIncludes( ARTIFACT_PATTERNS );
301 knownConsumers.add( consumer );
303 List<InvalidRepositoryContentConsumer> invalidConsumers = new ArrayList<>();
304 InvalidScanConsumer badconsumer = new InvalidScanConsumer();
305 invalidConsumers.add( badconsumer );
307 RepositoryScanner scanner = lookupRepositoryScanner();
308 RepositoryScanStatistics stats =
309 scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(), RepositoryScanner.FRESH_SCAN );
311 assertNotNull( "Stats should not be null.", stats );
312 assertMinimumHits( "Stats.totalFileCount", actualArtifactPaths.size(), stats.getTotalFileCount() );
313 assertMinimumHits( "Processed Count", actualArtifactPaths.size(), consumer.getProcessCount() );
317 public void testDefaultRepositoryMetadataScanner()
320 List<String> actualMetadataPaths = new ArrayList<>();
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" );
333 ManagedRepository repository = createDefaultRepository();
335 List<KnownRepositoryContentConsumer> knownConsumers = new ArrayList<>();
336 KnownScanConsumer knownConsumer = new KnownScanConsumer();
337 knownConsumer.setIncludes( new String[]{ "**/maven-metadata*.xml" } );
338 knownConsumers.add( knownConsumer );
340 List<InvalidRepositoryContentConsumer> invalidConsumers = new ArrayList<>();
341 InvalidScanConsumer badconsumer = new InvalidScanConsumer();
342 invalidConsumers.add( badconsumer );
344 RepositoryScanner scanner = lookupRepositoryScanner();
345 RepositoryScanStatistics stats =
346 scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(), RepositoryScanner.FRESH_SCAN );
348 assertNotNull( "Stats should not be null.", stats );
349 assertMinimumHits( "Stats.totalFileCount", actualMetadataPaths.size(), stats.getTotalFileCount() );
350 assertMinimumHits( "Processed Count", actualMetadataPaths.size(), knownConsumer.getProcessCount() );
354 public void testDefaultRepositoryProjectScanner()
357 List<String> actualProjectPaths = new ArrayList<>();
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" );
370 ManagedRepository repository = createDefaultRepository();
372 List<KnownRepositoryContentConsumer> knownConsumers = new ArrayList<>();
373 KnownScanConsumer consumer = new KnownScanConsumer();
374 consumer.setIncludes( new String[]{ "**/*.pom" } );
375 knownConsumers.add( consumer );
377 List<InvalidRepositoryContentConsumer> invalidConsumers = new ArrayList<>();
378 InvalidScanConsumer badconsumer = new InvalidScanConsumer();
379 invalidConsumers.add( badconsumer );
381 RepositoryScanner scanner = lookupRepositoryScanner();
382 RepositoryScanStatistics stats =
383 scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(), RepositoryScanner.FRESH_SCAN );
385 assertNotNull( "Stats should not be null.", stats );
386 assertMinimumHits( "Stats.totalFileCount", actualProjectPaths.size(), stats.getTotalFileCount() );
387 assertMinimumHits( "Processed Count", actualProjectPaths.size(), consumer.getProcessCount() );
391 public void testLegacyRepositoryArtifactScanner()
394 List<String> actualArtifactPaths = new ArrayList<>();
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" );
411 ManagedRepository repository = createLegacyRepository();
413 List<KnownRepositoryContentConsumer> knownConsumers = new ArrayList<>();
414 KnownScanConsumer consumer = new KnownScanConsumer();
415 consumer.setIncludes( ARTIFACT_PATTERNS );
416 knownConsumers.add( consumer );
418 List<InvalidRepositoryContentConsumer> invalidConsumers = new ArrayList<>();
419 InvalidScanConsumer badconsumer = new InvalidScanConsumer();
420 invalidConsumers.add( badconsumer );
422 RepositoryScanner scanner = lookupRepositoryScanner();
423 RepositoryScanStatistics stats =
424 scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(), RepositoryScanner.FRESH_SCAN );
426 assertNotNull( "Stats should not be null.", stats );
427 assertMinimumHits( "Stats.totalFileCount", actualArtifactPaths.size(), stats.getTotalFileCount() );
428 assertMinimumHits( "Processed Count", actualArtifactPaths.size(), consumer.getProcessCount() );