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.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;
37 import javax.inject.Inject;
38 import java.io.IOException;
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;
54 * RepositoryScannerTest
56 @RunWith(ArchivaSpringJUnit4ClassRunner.class)
57 @ContextConfiguration(locations = { "classpath*:/META-INF/spring-context.xml" })
58 public class RepositoryScannerTest
63 ApplicationContext applicationContext;
65 protected EditableManagedRepository createRepository( String id, String name, Path location )
67 BasicManagedRepository repo = new BasicManagedRepository(id, name, location.getParent());
68 repo.setLocation( location.toAbsolutePath().toUri());
72 protected EditableRemoteRepository createRemoteRepository( String id, String name, String url ) throws URISyntaxException
74 BasicRemoteRepository repo = new BasicRemoteRepository(id, name, Paths.get("remotes"));
75 repo.setLocation( new URI( url ) );
79 private static final String[] ARTIFACT_PATTERNS =
80 new String[]{ "**/*.jar", "**/*.pom", "**/*.rar", "**/*.zip", "**/*.war", "**/*.tar.gz" };
82 private ManagedRepository createDefaultRepository()
85 Paths.get( System.getProperty( "basedir" ), "src/test/repositories/default-repository" );
87 assertTrue( "Default Test Repository should exist.", Files.exists(repoDir) && Files.isDirectory(repoDir) );
89 return createRepository( "testDefaultRepo", "Test Default Repository", repoDir );
92 private ManagedRepository createSimpleRepository()
93 throws IOException, ParseException
95 Path srcDir = Paths.get( System.getProperty( "basedir" ), "src/test/repositories/simple-repository" );
97 Path repoDir = Paths.get( System.getProperty( "basedir" ), "target/test-repos/simple-repository" );
99 org.apache.archiva.common.utils.FileUtils.deleteDirectory( repoDir );
101 FileUtils.copyDirectory( srcDir.toFile(), repoDir.toFile() );
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" ) ));
107 assertTrue( "Simple Test Repository should exist.", Files.exists(repoDir) && Files.isDirectory(repoDir) );
109 return createRepository( "testSimpleRepo", "Test Simple Repository", repoDir );
112 private static long getTimestampAsMillis( String timestamp )
113 throws ParseException
115 SimpleDateFormat fmt = new SimpleDateFormat( "yyyyMMdd.HHmmss", Locale.US );
116 fmt.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
117 return fmt.parse( timestamp ).getTime();
120 private ManagedRepository createLegacyRepository()
122 Path repoDir = Paths.get( System.getProperty( "basedir" ), "src/test/repositories/legacy-repository" );
124 assertTrue( "Legacy Test Repository should exist.", Files.exists(repoDir) && Files.isDirectory(repoDir) );
126 EditableManagedRepository repo = createRepository( "testLegacyRepo", "Test Legacy Repository", repoDir );
127 repo.setLayout( "legacy" );
132 private void assertMinimumHits( String msg, int minimumHitCount, long actualCount )
134 if ( actualCount < minimumHitCount )
136 fail( "Minimum hit count on " + msg + " not satisfied. Expected more than <" + minimumHitCount
137 + ">, but actually got <" + actualCount + ">." );
141 private RepositoryScanner lookupRepositoryScanner()
144 return applicationContext.getBean( RepositoryScanner.class );
147 private List<String> getIgnoreList()
149 List<String> ignores = new ArrayList<>();
150 ignores.addAll( Arrays.asList( RepositoryScanner.IGNORABLE_CONTENT ) );
155 public void testTimestampRepositoryScanner()
158 ManagedRepository repository = createSimpleRepository();
160 List<KnownRepositoryContentConsumer> knownConsumers = new ArrayList<>();
161 KnownScanConsumer consumer = new KnownScanConsumer();
162 consumer.setIncludes( ARTIFACT_PATTERNS );
163 knownConsumers.add( consumer );
165 List<InvalidRepositoryContentConsumer> invalidConsumers = new ArrayList<>();
166 InvalidScanConsumer badconsumer = new InvalidScanConsumer();
167 invalidConsumers.add( badconsumer );
169 RepositoryScanner scanner = lookupRepositoryScanner();
171 RepositoryScanStatistics stats = scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(),
172 getTimestampAsMillis( "20061101.000000" ) );
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() );
182 public void testTimestampRepositoryScannerFreshScan()
185 ManagedRepository repository = createSimpleRepository();
187 List<KnownRepositoryContentConsumer> knownConsumers = new ArrayList<>();
188 KnownScanConsumer consumer = new KnownScanConsumer();
189 consumer.setIncludes( ARTIFACT_PATTERNS );
190 knownConsumers.add( consumer );
192 List<InvalidRepositoryContentConsumer> invalidConsumers = new ArrayList<>();
193 InvalidScanConsumer badconsumer = new InvalidScanConsumer();
194 invalidConsumers.add( badconsumer );
196 RepositoryScanner scanner = lookupRepositoryScanner();
197 RepositoryScanStatistics stats =
198 scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(), RepositoryScanner.FRESH_SCAN );
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() );
208 public void testTimestampRepositoryScannerProcessUnmodified()
211 ManagedRepository repository = createSimpleRepository();
213 List<KnownRepositoryContentConsumer> knownConsumers = new ArrayList<>();
214 KnownScanConsumer consumer = new KnownScanConsumer();
215 consumer.setProcessUnmodified( true );
216 consumer.setIncludes( ARTIFACT_PATTERNS );
217 knownConsumers.add( consumer );
219 List<InvalidRepositoryContentConsumer> invalidConsumers = new ArrayList<>();
220 InvalidScanConsumer badconsumer = new InvalidScanConsumer();
221 invalidConsumers.add( badconsumer );
223 RepositoryScanner scanner = lookupRepositoryScanner();
224 RepositoryScanStatistics stats = scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(),
225 getTimestampAsMillis( "20061101.000000" ) );
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() );
235 public void testDefaultRepositoryScanner()
238 ManagedRepository repository = createDefaultRepository();
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" }
246 knownConsumers.add( consumer );
248 List<InvalidRepositoryContentConsumer> invalidConsumers = new ArrayList<>();
249 InvalidScanConsumer badconsumer = new InvalidScanConsumer();
250 invalidConsumers.add( badconsumer );
252 RepositoryScanner scanner = lookupRepositoryScanner();
253 RepositoryScanStatistics stats =
254 scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(), RepositoryScanner.FRESH_SCAN );
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() );
263 public void testDefaultRepositoryArtifactScanner()
266 List<String> actualArtifactPaths = new ArrayList<>();
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" );
300 ManagedRepository repository = createDefaultRepository();
302 List<KnownRepositoryContentConsumer> knownConsumers = new ArrayList<>();
303 KnownScanConsumer consumer = new KnownScanConsumer();
304 consumer.setIncludes( ARTIFACT_PATTERNS );
305 knownConsumers.add( consumer );
307 List<InvalidRepositoryContentConsumer> invalidConsumers = new ArrayList<>();
308 InvalidScanConsumer badconsumer = new InvalidScanConsumer();
309 invalidConsumers.add( badconsumer );
311 RepositoryScanner scanner = lookupRepositoryScanner();
312 RepositoryScanStatistics stats =
313 scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(), RepositoryScanner.FRESH_SCAN );
315 assertNotNull( "Stats should not be null.", stats );
316 assertMinimumHits( "Stats.totalFileCount", actualArtifactPaths.size(), stats.getTotalFileCount() );
317 assertMinimumHits( "Processed Count", actualArtifactPaths.size(), consumer.getProcessCount() );
321 public void testDefaultRepositoryMetadataScanner()
324 List<String> actualMetadataPaths = new ArrayList<>();
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" );
337 ManagedRepository repository = createDefaultRepository();
339 List<KnownRepositoryContentConsumer> knownConsumers = new ArrayList<>();
340 KnownScanConsumer knownConsumer = new KnownScanConsumer();
341 knownConsumer.setIncludes( new String[]{ "**/maven-metadata*.xml" } );
342 knownConsumers.add( knownConsumer );
344 List<InvalidRepositoryContentConsumer> invalidConsumers = new ArrayList<>();
345 InvalidScanConsumer badconsumer = new InvalidScanConsumer();
346 invalidConsumers.add( badconsumer );
348 RepositoryScanner scanner = lookupRepositoryScanner();
349 RepositoryScanStatistics stats =
350 scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(), RepositoryScanner.FRESH_SCAN );
352 assertNotNull( "Stats should not be null.", stats );
353 assertMinimumHits( "Stats.totalFileCount", actualMetadataPaths.size(), stats.getTotalFileCount() );
354 assertMinimumHits( "Processed Count", actualMetadataPaths.size(), knownConsumer.getProcessCount() );
358 public void testDefaultRepositoryProjectScanner()
361 List<String> actualProjectPaths = new ArrayList<>();
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" );
374 ManagedRepository repository = createDefaultRepository();
376 List<KnownRepositoryContentConsumer> knownConsumers = new ArrayList<>();
377 KnownScanConsumer consumer = new KnownScanConsumer();
378 consumer.setIncludes( new String[]{ "**/*.pom" } );
379 knownConsumers.add( consumer );
381 List<InvalidRepositoryContentConsumer> invalidConsumers = new ArrayList<>();
382 InvalidScanConsumer badconsumer = new InvalidScanConsumer();
383 invalidConsumers.add( badconsumer );
385 RepositoryScanner scanner = lookupRepositoryScanner();
386 RepositoryScanStatistics stats =
387 scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(), RepositoryScanner.FRESH_SCAN );
389 assertNotNull( "Stats should not be null.", stats );
390 assertMinimumHits( "Stats.totalFileCount", actualProjectPaths.size(), stats.getTotalFileCount() );
391 assertMinimumHits( "Processed Count", actualProjectPaths.size(), consumer.getProcessCount() );
395 public void testLegacyRepositoryArtifactScanner()
398 List<String> actualArtifactPaths = new ArrayList<>();
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" );
415 ManagedRepository repository = createLegacyRepository();
417 List<KnownRepositoryContentConsumer> knownConsumers = new ArrayList<>();
418 KnownScanConsumer consumer = new KnownScanConsumer();
419 consumer.setIncludes( ARTIFACT_PATTERNS );
420 knownConsumers.add( consumer );
422 List<InvalidRepositoryContentConsumer> invalidConsumers = new ArrayList<>();
423 InvalidScanConsumer badconsumer = new InvalidScanConsumer();
424 invalidConsumers.add( badconsumer );
426 RepositoryScanner scanner = lookupRepositoryScanner();
427 RepositoryScanStatistics stats =
428 scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(), RepositoryScanner.FRESH_SCAN );
430 assertNotNull( "Stats should not be null.", stats );
431 assertMinimumHits( "Stats.totalFileCount", actualArtifactPaths.size(), stats.getTotalFileCount() );
432 assertMinimumHits( "Processed Count", actualArtifactPaths.size(), consumer.getProcessCount() );