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.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;
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;
49 * RepositoryScannerTest
51 @RunWith(ArchivaSpringJUnit4ClassRunner.class)
52 @ContextConfiguration(locations = { "classpath*:/META-INF/spring-context.xml" })
53 public class RepositoryScannerTest
58 ApplicationContext applicationContext;
60 protected ManagedRepository createRepository( String id, String name, Path location )
62 ManagedRepository repo = new ManagedRepository();
65 repo.setLocation( location.toAbsolutePath().toString());
69 protected RemoteRepository createRemoteRepository( String id, String name, String url )
71 RemoteRepository repo = new RemoteRepository();
78 private static final String[] ARTIFACT_PATTERNS =
79 new String[]{ "**/*.jar", "**/*.pom", "**/*.rar", "**/*.zip", "**/*.war", "**/*.tar.gz" };
81 private ManagedRepository createDefaultRepository()
84 Paths.get( System.getProperty( "basedir" ), "src/test/repositories/default-repository" );
86 assertTrue( "Default Test Repository should exist.", Files.exists(repoDir) && Files.isDirectory(repoDir) );
88 return createRepository( "testDefaultRepo", "Test Default Repository", repoDir );
91 private ManagedRepository createSimpleRepository()
92 throws IOException, ParseException
94 Path srcDir = Paths.get( System.getProperty( "basedir" ), "src/test/repositories/simple-repository" );
96 Path repoDir = Paths.get( System.getProperty( "basedir" ), "target/test-repos/simple-repository" );
98 org.apache.archiva.common.utils.FileUtils.deleteDirectory( repoDir );
100 FileUtils.copyDirectory( srcDir.toFile(), repoDir.toFile() );
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" ) ));
106 assertTrue( "Simple Test Repository should exist.", Files.exists(repoDir) && Files.isDirectory(repoDir) );
108 return createRepository( "testSimpleRepo", "Test Simple Repository", repoDir );
111 private static long getTimestampAsMillis( String timestamp )
112 throws ParseException
114 SimpleDateFormat fmt = new SimpleDateFormat( "yyyyMMdd.HHmmss", Locale.US );
115 fmt.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
116 return fmt.parse( timestamp ).getTime();
119 private ManagedRepository createLegacyRepository()
121 Path repoDir = Paths.get( System.getProperty( "basedir" ), "src/test/repositories/legacy-repository" );
123 assertTrue( "Legacy Test Repository should exist.", Files.exists(repoDir) && Files.isDirectory(repoDir) );
125 ManagedRepository repo = createRepository( "testLegacyRepo", "Test Legacy Repository", repoDir );
126 repo.setLayout( "legacy" );
131 private void assertMinimumHits( String msg, int minimumHitCount, long actualCount )
133 if ( actualCount < minimumHitCount )
135 fail( "Minimum hit count on " + msg + " not satisfied. Expected more than <" + minimumHitCount
136 + ">, but actually got <" + actualCount + ">." );
140 private RepositoryScanner lookupRepositoryScanner()
143 return applicationContext.getBean( RepositoryScanner.class );
146 private List<String> getIgnoreList()
148 List<String> ignores = new ArrayList<>();
149 ignores.addAll( Arrays.asList( RepositoryScanner.IGNORABLE_CONTENT ) );
154 public void testTimestampRepositoryScanner()
157 ManagedRepository repository = createSimpleRepository();
159 List<KnownRepositoryContentConsumer> knownConsumers = new ArrayList<>();
160 KnownScanConsumer consumer = new KnownScanConsumer();
161 consumer.setIncludes( ARTIFACT_PATTERNS );
162 knownConsumers.add( consumer );
164 List<InvalidRepositoryContentConsumer> invalidConsumers = new ArrayList<>();
165 InvalidScanConsumer badconsumer = new InvalidScanConsumer();
166 invalidConsumers.add( badconsumer );
168 RepositoryScanner scanner = lookupRepositoryScanner();
170 RepositoryScanStatistics stats = scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(),
171 getTimestampAsMillis( "20061101.000000" ) );
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() );
181 public void testTimestampRepositoryScannerFreshScan()
184 ManagedRepository repository = createSimpleRepository();
186 List<KnownRepositoryContentConsumer> knownConsumers = new ArrayList<>();
187 KnownScanConsumer consumer = new KnownScanConsumer();
188 consumer.setIncludes( ARTIFACT_PATTERNS );
189 knownConsumers.add( consumer );
191 List<InvalidRepositoryContentConsumer> invalidConsumers = new ArrayList<>();
192 InvalidScanConsumer badconsumer = new InvalidScanConsumer();
193 invalidConsumers.add( badconsumer );
195 RepositoryScanner scanner = lookupRepositoryScanner();
196 RepositoryScanStatistics stats =
197 scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(), RepositoryScanner.FRESH_SCAN );
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() );
207 public void testTimestampRepositoryScannerProcessUnmodified()
210 ManagedRepository repository = createSimpleRepository();
212 List<KnownRepositoryContentConsumer> knownConsumers = new ArrayList<>();
213 KnownScanConsumer consumer = new KnownScanConsumer();
214 consumer.setProcessUnmodified( true );
215 consumer.setIncludes( ARTIFACT_PATTERNS );
216 knownConsumers.add( consumer );
218 List<InvalidRepositoryContentConsumer> invalidConsumers = new ArrayList<>();
219 InvalidScanConsumer badconsumer = new InvalidScanConsumer();
220 invalidConsumers.add( badconsumer );
222 RepositoryScanner scanner = lookupRepositoryScanner();
223 RepositoryScanStatistics stats = scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(),
224 getTimestampAsMillis( "20061101.000000" ) );
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() );
234 public void testDefaultRepositoryScanner()
237 ManagedRepository repository = createDefaultRepository();
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" }
245 knownConsumers.add( consumer );
247 List<InvalidRepositoryContentConsumer> invalidConsumers = new ArrayList<>();
248 InvalidScanConsumer badconsumer = new InvalidScanConsumer();
249 invalidConsumers.add( badconsumer );
251 RepositoryScanner scanner = lookupRepositoryScanner();
252 RepositoryScanStatistics stats =
253 scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(), RepositoryScanner.FRESH_SCAN );
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() );
262 public void testDefaultRepositoryArtifactScanner()
265 List<String> actualArtifactPaths = new ArrayList<>();
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" );
299 ManagedRepository repository = createDefaultRepository();
301 List<KnownRepositoryContentConsumer> knownConsumers = new ArrayList<>();
302 KnownScanConsumer consumer = new KnownScanConsumer();
303 consumer.setIncludes( ARTIFACT_PATTERNS );
304 knownConsumers.add( consumer );
306 List<InvalidRepositoryContentConsumer> invalidConsumers = new ArrayList<>();
307 InvalidScanConsumer badconsumer = new InvalidScanConsumer();
308 invalidConsumers.add( badconsumer );
310 RepositoryScanner scanner = lookupRepositoryScanner();
311 RepositoryScanStatistics stats =
312 scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(), RepositoryScanner.FRESH_SCAN );
314 assertNotNull( "Stats should not be null.", stats );
315 assertMinimumHits( "Stats.totalFileCount", actualArtifactPaths.size(), stats.getTotalFileCount() );
316 assertMinimumHits( "Processed Count", actualArtifactPaths.size(), consumer.getProcessCount() );
320 public void testDefaultRepositoryMetadataScanner()
323 List<String> actualMetadataPaths = new ArrayList<>();
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" );
336 ManagedRepository repository = createDefaultRepository();
338 List<KnownRepositoryContentConsumer> knownConsumers = new ArrayList<>();
339 KnownScanConsumer knownConsumer = new KnownScanConsumer();
340 knownConsumer.setIncludes( new String[]{ "**/maven-metadata*.xml" } );
341 knownConsumers.add( knownConsumer );
343 List<InvalidRepositoryContentConsumer> invalidConsumers = new ArrayList<>();
344 InvalidScanConsumer badconsumer = new InvalidScanConsumer();
345 invalidConsumers.add( badconsumer );
347 RepositoryScanner scanner = lookupRepositoryScanner();
348 RepositoryScanStatistics stats =
349 scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(), RepositoryScanner.FRESH_SCAN );
351 assertNotNull( "Stats should not be null.", stats );
352 assertMinimumHits( "Stats.totalFileCount", actualMetadataPaths.size(), stats.getTotalFileCount() );
353 assertMinimumHits( "Processed Count", actualMetadataPaths.size(), knownConsumer.getProcessCount() );
357 public void testDefaultRepositoryProjectScanner()
360 List<String> actualProjectPaths = new ArrayList<>();
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" );
373 ManagedRepository repository = createDefaultRepository();
375 List<KnownRepositoryContentConsumer> knownConsumers = new ArrayList<>();
376 KnownScanConsumer consumer = new KnownScanConsumer();
377 consumer.setIncludes( new String[]{ "**/*.pom" } );
378 knownConsumers.add( consumer );
380 List<InvalidRepositoryContentConsumer> invalidConsumers = new ArrayList<>();
381 InvalidScanConsumer badconsumer = new InvalidScanConsumer();
382 invalidConsumers.add( badconsumer );
384 RepositoryScanner scanner = lookupRepositoryScanner();
385 RepositoryScanStatistics stats =
386 scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(), RepositoryScanner.FRESH_SCAN );
388 assertNotNull( "Stats should not be null.", stats );
389 assertMinimumHits( "Stats.totalFileCount", actualProjectPaths.size(), stats.getTotalFileCount() );
390 assertMinimumHits( "Processed Count", actualProjectPaths.size(), consumer.getProcessCount() );
394 public void testLegacyRepositoryArtifactScanner()
397 List<String> actualArtifactPaths = new ArrayList<>();
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" );
414 ManagedRepository repository = createLegacyRepository();
416 List<KnownRepositoryContentConsumer> knownConsumers = new ArrayList<>();
417 KnownScanConsumer consumer = new KnownScanConsumer();
418 consumer.setIncludes( ARTIFACT_PATTERNS );
419 knownConsumers.add( consumer );
421 List<InvalidRepositoryContentConsumer> invalidConsumers = new ArrayList<>();
422 InvalidScanConsumer badconsumer = new InvalidScanConsumer();
423 invalidConsumers.add( badconsumer );
425 RepositoryScanner scanner = lookupRepositoryScanner();
426 RepositoryScanStatistics stats =
427 scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(), RepositoryScanner.FRESH_SCAN );
429 assertNotNull( "Stats should not be null.", stats );
430 assertMinimumHits( "Stats.totalFileCount", actualArtifactPaths.size(), stats.getTotalFileCount() );
431 assertMinimumHits( "Processed Count", actualArtifactPaths.size(), consumer.getProcessCount() );