1 package org.apache.maven.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 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;
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;
40 * RepositoryScannerTest
44 public class RepositoryScannerTest
45 extends AbstractRepositoryLayerTestCase
47 private static final String[] ARTIFACT_PATTERNS =
48 new String[]{"**/*.jar", "**/*.pom", "**/*.rar", "**/*.zip", "**/*.war", "**/*.tar.gz"};
50 private ManagedRepositoryConfiguration createDefaultRepository()
52 File repoDir = new File( getBasedir(), "src/test/repositories/default-repository" );
54 assertTrue( "Default Test Repository should exist.", repoDir.exists() && repoDir.isDirectory() );
56 return createRepository( "testDefaultRepo", "Test Default Repository", repoDir );
59 private ManagedRepositoryConfiguration createSimpleRepository()
60 throws IOException, ParseException
62 File srcDir = new File( getBasedir(), "src/test/repositories/simple-repository" );
64 File repoDir = getTestFile( "target/test-repos/simple-repository" );
66 FileUtils.deleteDirectory( repoDir );
68 FileUtils.copyDirectory( srcDir, repoDir );
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" ) );
74 assertTrue( "Simple Test Repository should exist.", repoDir.exists() && repoDir.isDirectory() );
76 return createRepository( "testSimpleRepo", "Test Simple Repository", repoDir );
79 private static long getTimestampAsMillis( String timestamp )
82 SimpleDateFormat fmt = new SimpleDateFormat( "yyyyMMdd.HHmmss", Locale.US );
83 fmt.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
84 return fmt.parse( timestamp ).getTime();
87 private ManagedRepositoryConfiguration createLegacyRepository()
89 File repoDir = new File( getBasedir(), "src/test/repositories/legacy-repository" );
91 assertTrue( "Legacy Test Repository should exist.", repoDir.exists() && repoDir.isDirectory() );
93 ManagedRepositoryConfiguration repo = createRepository( "testLegacyRepo", "Test Legacy Repository", repoDir );
94 repo.setLayout( "legacy" );
99 private void assertMinimumHits( String msg, int minimumHitCount, long actualCount )
101 if ( actualCount < minimumHitCount )
103 fail( "Minimum hit count on " + msg + " not satisfied. Expected more than <" + minimumHitCount +
104 ">, but actually got <" + actualCount + ">." );
108 private RepositoryScanner lookupRepositoryScanner()
111 return (RepositoryScanner) lookup( RepositoryScanner.class );
114 private List<String> getIgnoreList()
116 List<String> ignores = new ArrayList<String>();
117 ignores.addAll( Arrays.asList( RepositoryScanner.IGNORABLE_CONTENT ) );
121 public void testTimestampRepositoryScanner()
124 ManagedRepositoryConfiguration repository = createSimpleRepository();
126 List<KnownRepositoryContentConsumer> knownConsumers = new ArrayList<KnownRepositoryContentConsumer>();
127 KnownScanConsumer consumer = new KnownScanConsumer();
128 consumer.setIncludes( ARTIFACT_PATTERNS );
129 knownConsumers.add( consumer );
131 List<InvalidRepositoryContentConsumer> invalidConsumers = new ArrayList<InvalidRepositoryContentConsumer>();
132 InvalidScanConsumer badconsumer = new InvalidScanConsumer();
133 invalidConsumers.add( badconsumer );
135 RepositoryScanner scanner = lookupRepositoryScanner();
137 RepositoryContentStatistics stats = scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(),
138 getTimestampAsMillis( "20061101.000000" ) );
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() );
147 public void testTimestampRepositoryScannerFreshScan()
150 ManagedRepositoryConfiguration repository = createSimpleRepository();
152 List<KnownRepositoryContentConsumer> knownConsumers = new ArrayList<KnownRepositoryContentConsumer>();
153 KnownScanConsumer consumer = new KnownScanConsumer();
154 consumer.setIncludes( ARTIFACT_PATTERNS );
155 knownConsumers.add( consumer );
157 List<InvalidRepositoryContentConsumer> invalidConsumers = new ArrayList<InvalidRepositoryContentConsumer>();
158 InvalidScanConsumer badconsumer = new InvalidScanConsumer();
159 invalidConsumers.add( badconsumer );
161 RepositoryScanner scanner = lookupRepositoryScanner();
162 RepositoryContentStatistics stats =
163 scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(), RepositoryScanner.FRESH_SCAN );
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() );
172 public void testTimestampRepositoryScannerProcessUnmodified()
175 ManagedRepositoryConfiguration repository = createSimpleRepository();
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 );
183 List<InvalidRepositoryContentConsumer> invalidConsumers = new ArrayList<InvalidRepositoryContentConsumer>();
184 InvalidScanConsumer badconsumer = new InvalidScanConsumer();
185 invalidConsumers.add( badconsumer );
187 RepositoryScanner scanner = lookupRepositoryScanner();
188 RepositoryContentStatistics stats = scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(),
189 getTimestampAsMillis( "20061101.000000" ) );
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() );
198 public void testDefaultRepositoryScanner()
201 ManagedRepositoryConfiguration repository = createDefaultRepository();
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 );
209 List<InvalidRepositoryContentConsumer> invalidConsumers = new ArrayList<InvalidRepositoryContentConsumer>();
210 InvalidScanConsumer badconsumer = new InvalidScanConsumer();
211 invalidConsumers.add( badconsumer );
213 RepositoryScanner scanner = lookupRepositoryScanner();
214 RepositoryContentStatistics stats =
215 scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(), RepositoryScanner.FRESH_SCAN );
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() );
223 public void testDefaultRepositoryArtifactScanner()
226 List<String> actualArtifactPaths = new ArrayList<String>();
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" );
260 ManagedRepositoryConfiguration repository = createDefaultRepository();
262 List<KnownRepositoryContentConsumer> knownConsumers = new ArrayList<KnownRepositoryContentConsumer>();
263 KnownScanConsumer consumer = new KnownScanConsumer();
264 consumer.setIncludes( ARTIFACT_PATTERNS );
265 knownConsumers.add( consumer );
267 List<InvalidRepositoryContentConsumer> invalidConsumers = new ArrayList<InvalidRepositoryContentConsumer>();
268 InvalidScanConsumer badconsumer = new InvalidScanConsumer();
269 invalidConsumers.add( badconsumer );
271 RepositoryScanner scanner = lookupRepositoryScanner();
272 RepositoryContentStatistics stats =
273 scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(), RepositoryScanner.FRESH_SCAN );
275 assertNotNull( "Stats should not be null.", stats );
276 assertMinimumHits( "Stats.totalFileCount", actualArtifactPaths.size(), stats.getTotalFileCount() );
277 assertMinimumHits( "Processed Count", actualArtifactPaths.size(), consumer.getProcessCount() );
280 public void testDefaultRepositoryMetadataScanner()
283 List<String> actualMetadataPaths = new ArrayList<String>();
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" );
296 ManagedRepositoryConfiguration repository = createDefaultRepository();
298 List<KnownRepositoryContentConsumer> knownConsumers = new ArrayList<KnownRepositoryContentConsumer>();
299 KnownScanConsumer knownConsumer = new KnownScanConsumer();
300 knownConsumer.setIncludes( new String[]{"**/maven-metadata*.xml"} );
301 knownConsumers.add( knownConsumer );
303 List<InvalidRepositoryContentConsumer> invalidConsumers = new ArrayList<InvalidRepositoryContentConsumer>();
304 InvalidScanConsumer badconsumer = new InvalidScanConsumer();
305 invalidConsumers.add( badconsumer );
307 RepositoryScanner scanner = lookupRepositoryScanner();
308 RepositoryContentStatistics stats =
309 scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(), RepositoryScanner.FRESH_SCAN );
311 assertNotNull( "Stats should not be null.", stats );
312 assertMinimumHits( "Stats.totalFileCount", actualMetadataPaths.size(), stats.getTotalFileCount() );
313 assertMinimumHits( "Processed Count", actualMetadataPaths.size(), knownConsumer.getProcessCount() );
316 public void testDefaultRepositoryProjectScanner()
319 List<String> actualProjectPaths = new ArrayList<String>();
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" );
332 ManagedRepositoryConfiguration repository = createDefaultRepository();
334 List<KnownRepositoryContentConsumer> knownConsumers = new ArrayList<KnownRepositoryContentConsumer>();
335 KnownScanConsumer consumer = new KnownScanConsumer();
336 consumer.setIncludes( new String[]{"**/*.pom"} );
337 knownConsumers.add( consumer );
339 List<InvalidRepositoryContentConsumer> invalidConsumers = new ArrayList<InvalidRepositoryContentConsumer>();
340 InvalidScanConsumer badconsumer = new InvalidScanConsumer();
341 invalidConsumers.add( badconsumer );
343 RepositoryScanner scanner = lookupRepositoryScanner();
344 RepositoryContentStatistics stats =
345 scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(), RepositoryScanner.FRESH_SCAN );
347 assertNotNull( "Stats should not be null.", stats );
348 assertMinimumHits( "Stats.totalFileCount", actualProjectPaths.size(), stats.getTotalFileCount() );
349 assertMinimumHits( "Processed Count", actualProjectPaths.size(), consumer.getProcessCount() );
352 public void testLegacyRepositoryArtifactScanner()
355 List<String> actualArtifactPaths = new ArrayList<String>();
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" );
372 ManagedRepositoryConfiguration repository = createLegacyRepository();
374 List<KnownRepositoryContentConsumer> knownConsumers = new ArrayList<KnownRepositoryContentConsumer>();
375 KnownScanConsumer consumer = new KnownScanConsumer();
376 consumer.setIncludes( ARTIFACT_PATTERNS );
377 knownConsumers.add( consumer );
379 List<InvalidRepositoryContentConsumer> invalidConsumers = new ArrayList<InvalidRepositoryContentConsumer>();
380 InvalidScanConsumer badconsumer = new InvalidScanConsumer();
381 invalidConsumers.add( badconsumer );
383 RepositoryScanner scanner = lookupRepositoryScanner();
384 RepositoryContentStatistics stats =
385 scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(), RepositoryScanner.FRESH_SCAN );
387 assertNotNull( "Stats should not be null.", stats );
388 assertMinimumHits( "Stats.totalFileCount", actualArtifactPaths.size(), stats.getTotalFileCount() );
389 assertMinimumHits( "Processed Count", actualArtifactPaths.size(), consumer.getProcessCount() );