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.model.RepositoryContentStatistics;
25 import org.apache.maven.archiva.repository.AbstractRepositoryLayerTestCase;
28 import java.io.IOException;
29 import java.text.ParseException;
30 import java.text.SimpleDateFormat;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.List;
34 import java.util.Locale;
35 import java.util.TimeZone;
38 * RepositoryScannerTest
42 public class RepositoryScannerTest
43 extends AbstractRepositoryLayerTestCase
45 private static final String[] ARTIFACT_PATTERNS =
46 new String[]{"**/*.jar", "**/*.pom", "**/*.rar", "**/*.zip", "**/*.war", "**/*.tar.gz"};
48 private ManagedRepositoryConfiguration createDefaultRepository()
50 File repoDir = new File( getBasedir(), "src/test/repositories/default-repository" );
52 assertTrue( "Default Test Repository should exist.", repoDir.exists() && repoDir.isDirectory() );
54 return createRepository( "testDefaultRepo", "Test Default Repository", repoDir );
57 private ManagedRepositoryConfiguration createSimpleRepository()
58 throws IOException, ParseException
60 File srcDir = new File( getBasedir(), "src/test/repositories/simple-repository" );
62 File repoDir = getTestFile( "target/test-repos/simple-repository" );
64 FileUtils.deleteDirectory( repoDir );
66 FileUtils.copyDirectory( srcDir, repoDir );
68 File repoFile = new File( repoDir,
69 "groupId/snapshot-artifact/1.0-alpha-1-SNAPSHOT/snapshot-artifact-1.0-alpha-1-20050611.202024-1.pom" );
70 repoFile.setLastModified( getTimestampAsMillis( "20050611.202024" ) );
72 assertTrue( "Simple Test Repository should exist.", repoDir.exists() && repoDir.isDirectory() );
74 return createRepository( "testSimpleRepo", "Test Simple Repository", repoDir );
77 private static long getTimestampAsMillis( String timestamp )
80 SimpleDateFormat fmt = new SimpleDateFormat( "yyyyMMdd.HHmmss", Locale.US );
81 fmt.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
82 return fmt.parse( timestamp ).getTime();
85 private ManagedRepositoryConfiguration createLegacyRepository()
87 File repoDir = new File( getBasedir(), "src/test/repositories/legacy-repository" );
89 assertTrue( "Legacy Test Repository should exist.", repoDir.exists() && repoDir.isDirectory() );
91 ManagedRepositoryConfiguration repo = createRepository( "testLegacyRepo", "Test Legacy Repository", repoDir );
92 repo.setLayout( "legacy" );
97 private void assertMinimumHits( String msg, int minimumHitCount, long actualCount )
99 if ( actualCount < minimumHitCount )
101 fail( "Minimum hit count on " + msg + " not satisfied. Expected more than <" + minimumHitCount +
102 ">, but actually got <" + actualCount + ">." );
106 private RepositoryScanner lookupRepositoryScanner()
109 return (RepositoryScanner) lookup( RepositoryScanner.class );
112 private List getIgnoreList()
114 List ignores = new ArrayList();
115 ignores.addAll( Arrays.asList( RepositoryScanner.IGNORABLE_CONTENT ) );
119 public void testTimestampRepositoryScanner()
122 ManagedRepositoryConfiguration repository = createSimpleRepository();
124 List knownConsumers = new ArrayList();
125 KnownScanConsumer consumer = new KnownScanConsumer();
126 consumer.setIncludes( ARTIFACT_PATTERNS );
127 knownConsumers.add( consumer );
129 List invalidConsumers = new ArrayList();
130 InvalidScanConsumer badconsumer = new InvalidScanConsumer();
131 invalidConsumers.add( badconsumer );
133 RepositoryScanner scanner = lookupRepositoryScanner();
135 RepositoryContentStatistics stats = scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(),
136 getTimestampAsMillis( "20061101.000000" ) );
138 assertNotNull( "Stats should not be null.", stats );
139 assertEquals( "Stats.totalFileCount", 4, stats.getTotalFileCount() );
140 assertEquals( "Stats.newFileCount", 3, stats.getNewFileCount() );
141 assertEquals( "Processed Count", 2, consumer.getProcessCount() );
142 assertEquals( "Processed Count (of invalid items)", 1, badconsumer.getProcessCount() );
145 public void testTimestampRepositoryScannerFreshScan()
148 ManagedRepositoryConfiguration repository = createSimpleRepository();
150 List knownConsumers = new ArrayList();
151 KnownScanConsumer consumer = new KnownScanConsumer();
152 consumer.setIncludes( ARTIFACT_PATTERNS );
153 knownConsumers.add( consumer );
155 List invalidConsumers = new ArrayList();
156 InvalidScanConsumer badconsumer = new InvalidScanConsumer();
157 invalidConsumers.add( badconsumer );
159 RepositoryScanner scanner = lookupRepositoryScanner();
160 RepositoryContentStatistics stats =
161 scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(), RepositoryScanner.FRESH_SCAN );
163 assertNotNull( "Stats should not be null.", stats );
164 assertEquals( "Stats.totalFileCount", 4, stats.getTotalFileCount() );
165 assertEquals( "Stats.newFileCount", 4, stats.getNewFileCount() );
166 assertEquals( "Processed Count", 3, consumer.getProcessCount() );
167 assertEquals( "Processed Count (of invalid items)", 1, badconsumer.getProcessCount() );
170 public void testTimestampRepositoryScannerProcessUnmodified()
173 ManagedRepositoryConfiguration repository = createSimpleRepository();
175 List knownConsumers = new ArrayList();
176 KnownScanConsumer consumer = new KnownScanConsumer();
177 consumer.setProcessUnmodified( true );
178 consumer.setIncludes( ARTIFACT_PATTERNS );
179 knownConsumers.add( consumer );
181 List invalidConsumers = new ArrayList();
182 InvalidScanConsumer badconsumer = new InvalidScanConsumer();
183 invalidConsumers.add( badconsumer );
185 RepositoryScanner scanner = lookupRepositoryScanner();
186 RepositoryContentStatistics stats = scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(),
187 getTimestampAsMillis( "20061101.000000" ) );
189 assertNotNull( "Stats should not be null.", stats );
190 assertEquals( "Stats.totalFileCount", 4, stats.getTotalFileCount() );
191 assertEquals( "Stats.newFileCount", 3, stats.getNewFileCount() );
192 assertEquals( "Processed Count", 3, consumer.getProcessCount() );
193 assertEquals( "Processed Count (of invalid items)", 1, badconsumer.getProcessCount() );
196 public void testDefaultRepositoryScanner()
199 ManagedRepositoryConfiguration repository = createDefaultRepository();
201 List knownConsumers = new ArrayList();
202 KnownScanConsumer consumer = new KnownScanConsumer();
203 consumer.setIncludes( new String[]{"**/*.jar", "**/*.war", "**/*.pom", "**/maven-metadata.xml", "**/*-site.xml",
204 "**/*.zip", "**/*.tar.gz", "**/*.sha1", "**/*.md5"} );
205 knownConsumers.add( consumer );
207 List invalidConsumers = new ArrayList();
208 InvalidScanConsumer badconsumer = new InvalidScanConsumer();
209 invalidConsumers.add( badconsumer );
211 RepositoryScanner scanner = lookupRepositoryScanner();
212 RepositoryContentStatistics stats =
213 scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(), RepositoryScanner.FRESH_SCAN );
215 assertNotNull( "Stats should not be null.", stats );
216 assertMinimumHits( "Stats.totalFileCount", 17, stats.getTotalFileCount() );
217 assertMinimumHits( "Processed Count", 17, consumer.getProcessCount() );
218 assertEquals( "Processed Count (of invalid items)", 6, badconsumer.getProcessCount() );
221 public void testDefaultRepositoryArtifactScanner()
224 List actualArtifactPaths = new ArrayList();
226 actualArtifactPaths.add( "invalid/invalid/1.0-20050611.123456-1/invalid-1.0-20050611.123456-1.jar" );
227 actualArtifactPaths.add( "invalid/invalid/1.0-SNAPSHOT/invalid-1.0.jar" );
228 actualArtifactPaths.add( "invalid/invalid/1.0/invalid-1.0b.jar" );
229 actualArtifactPaths.add( "invalid/invalid/1.0/invalid-2.0.jar" );
230 actualArtifactPaths.add( "invalid/invalid-1.0.jar" );
231 actualArtifactPaths.add( "org/apache/maven/test/1.0-SNAPSHOT/wrong-artifactId-1.0-20050611.112233-1.jar" );
232 actualArtifactPaths.add( "org/apache/maven/test/1.0-SNAPSHOT/test-1.0-20050611.112233-1-javadoc.jar" );
233 actualArtifactPaths.add( "org/apache/maven/test/1.0-SNAPSHOT/test-1.0-20050611.112233-1.jar" );
234 actualArtifactPaths.add( "org/apache/maven/A/1.0/A-1.0.war" );
235 actualArtifactPaths.add( "org/apache/maven/A/1.0/A-1.0.pom" );
236 actualArtifactPaths.add( "org/apache/maven/B/2.0/B-2.0.pom" );
237 actualArtifactPaths.add( "org/apache/maven/B/1.0/B-1.0.pom" );
238 actualArtifactPaths.add( "org/apache/maven/some-ejb/1.0/some-ejb-1.0-client.jar" );
239 actualArtifactPaths.add( "org/apache/maven/C/1.0/C-1.0.war" );
240 actualArtifactPaths.add( "org/apache/maven/C/1.0/C-1.0.pom" );
241 actualArtifactPaths.add( "org/apache/maven/update/test-not-updated/1.0/test-not-updated-1.0.pom" );
242 actualArtifactPaths.add( "org/apache/maven/update/test-not-updated/1.0/test-not-updated-1.0.jar" );
243 actualArtifactPaths.add( "org/apache/maven/update/test-updated/1.0/test-updated-1.0.pom" );
244 actualArtifactPaths.add( "org/apache/maven/update/test-updated/1.0/test-updated-1.0.jar" );
245 actualArtifactPaths.add( "org/apache/maven/discovery/1.0/discovery-1.0.pom" );
246 actualArtifactPaths.add( "org/apache/maven/testing/1.0/testing-1.0-test-sources.jar" );
247 actualArtifactPaths.add( "org/apache/maven/testing/1.0/testing-1.0.jar" );
248 actualArtifactPaths.add( "org/apache/maven/testing/1.0/testing-1.0-sources.jar" );
249 actualArtifactPaths.add( "org/apache/maven/testing/1.0/testing-1.0.zip" );
250 actualArtifactPaths.add( "org/apache/maven/testing/1.0/testing-1.0.tar.gz" );
251 actualArtifactPaths.add( "org/apache/maven/samplejar/2.0/samplejar-2.0.pom" );
252 actualArtifactPaths.add( "org/apache/maven/samplejar/2.0/samplejar-2.0.jar" );
253 actualArtifactPaths.add( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" );
254 actualArtifactPaths.add( "org/apache/maven/samplejar/1.0/samplejar-1.0.jar" );
255 actualArtifactPaths.add( "org/apache/testgroup/discovery/1.0/discovery-1.0.pom" );
256 actualArtifactPaths.add( "javax/sql/jdbc/2.0/jdbc-2.0.jar" );
258 ManagedRepositoryConfiguration repository = createDefaultRepository();
260 List knownConsumers = new ArrayList();
261 KnownScanConsumer consumer = new KnownScanConsumer();
262 consumer.setIncludes( ARTIFACT_PATTERNS );
263 knownConsumers.add( consumer );
265 List invalidConsumers = new ArrayList();
266 InvalidScanConsumer badconsumer = new InvalidScanConsumer();
267 invalidConsumers.add( badconsumer );
269 RepositoryScanner scanner = lookupRepositoryScanner();
270 RepositoryContentStatistics stats =
271 scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(), RepositoryScanner.FRESH_SCAN );
273 assertNotNull( "Stats should not be null.", stats );
274 assertMinimumHits( "Stats.totalFileCount", actualArtifactPaths.size(), stats.getTotalFileCount() );
275 assertMinimumHits( "Processed Count", actualArtifactPaths.size(), consumer.getProcessCount() );
278 public void testDefaultRepositoryMetadataScanner()
281 List actualMetadataPaths = new ArrayList();
283 actualMetadataPaths.add( "org/apache/maven/some-ejb/1.0/maven-metadata.xml" );
284 actualMetadataPaths.add( "org/apache/maven/update/test-not-updated/maven-metadata.xml" );
285 actualMetadataPaths.add( "org/apache/maven/update/test-updated/maven-metadata.xml" );
286 actualMetadataPaths.add( "org/apache/maven/maven-metadata.xml" );
287 actualMetadataPaths.add( "org/apache/testgroup/discovery/1.0/maven-metadata.xml" );
288 actualMetadataPaths.add( "org/apache/testgroup/discovery/maven-metadata.xml" );
289 actualMetadataPaths.add( "javax/sql/jdbc/2.0/maven-metadata-repository.xml" );
290 actualMetadataPaths.add( "javax/sql/jdbc/maven-metadata-repository.xml" );
291 actualMetadataPaths.add( "javax/sql/maven-metadata-repository.xml" );
292 actualMetadataPaths.add( "javax/maven-metadata.xml" );
294 ManagedRepositoryConfiguration repository = createDefaultRepository();
296 List knownConsumers = new ArrayList();
297 KnownScanConsumer knownConsumer = new KnownScanConsumer();
298 knownConsumer.setIncludes( new String[]{"**/maven-metadata*.xml"} );
299 knownConsumers.add( knownConsumer );
301 List invalidConsumers = new ArrayList();
302 InvalidScanConsumer badconsumer = new InvalidScanConsumer();
303 invalidConsumers.add( badconsumer );
305 RepositoryScanner scanner = lookupRepositoryScanner();
306 RepositoryContentStatistics stats =
307 scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(), RepositoryScanner.FRESH_SCAN );
309 assertNotNull( "Stats should not be null.", stats );
310 assertMinimumHits( "Stats.totalFileCount", actualMetadataPaths.size(), stats.getTotalFileCount() );
311 assertMinimumHits( "Processed Count", actualMetadataPaths.size(), knownConsumer.getProcessCount() );
314 public void testDefaultRepositoryProjectScanner()
317 List actualProjectPaths = new ArrayList();
319 actualProjectPaths.add( "org/apache/maven/A/1.0/A-1.0.pom" );
320 actualProjectPaths.add( "org/apache/maven/B/2.0/B-2.0.pom" );
321 actualProjectPaths.add( "org/apache/maven/B/1.0/B-1.0.pom" );
322 actualProjectPaths.add( "org/apache/maven/C/1.0/C-1.0.pom" );
323 actualProjectPaths.add( "org/apache/maven/update/test-not-updated/1.0/test-not-updated-1.0.pom" );
324 actualProjectPaths.add( "org/apache/maven/update/test-updated/1.0/test-updated-1.0.pom" );
325 actualProjectPaths.add( "org/apache/maven/discovery/1.0/discovery-1.0.pom" );
326 actualProjectPaths.add( "org/apache/maven/samplejar/2.0/samplejar-2.0.pom" );
327 actualProjectPaths.add( "org/apache/maven/samplejar/1.0/samplejar-1.0.pom" );
328 actualProjectPaths.add( "org/apache/testgroup/discovery/1.0/discovery-1.0.pom" );
330 ManagedRepositoryConfiguration repository = createDefaultRepository();
332 List knownConsumers = new ArrayList();
333 KnownScanConsumer consumer = new KnownScanConsumer();
334 consumer.setIncludes( new String[]{"**/*.pom"} );
335 knownConsumers.add( consumer );
337 List invalidConsumers = new ArrayList();
338 InvalidScanConsumer badconsumer = new InvalidScanConsumer();
339 invalidConsumers.add( badconsumer );
341 RepositoryScanner scanner = lookupRepositoryScanner();
342 RepositoryContentStatistics stats =
343 scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(), RepositoryScanner.FRESH_SCAN );
345 assertNotNull( "Stats should not be null.", stats );
346 assertMinimumHits( "Stats.totalFileCount", actualProjectPaths.size(), stats.getTotalFileCount() );
347 assertMinimumHits( "Processed Count", actualProjectPaths.size(), consumer.getProcessCount() );
350 public void testLegacyRepositoryArtifactScanner()
353 List actualArtifactPaths = new ArrayList();
355 actualArtifactPaths.add( "invalid/jars/1.0/invalid-1.0.jar" );
356 actualArtifactPaths.add( "invalid/jars/invalid-1.0.rar" );
357 actualArtifactPaths.add( "invalid/jars/invalid.jar" );
358 actualArtifactPaths.add( "invalid/invalid-1.0.jar" );
359 actualArtifactPaths.add( "javax.sql/jars/jdbc-2.0.jar" );
360 actualArtifactPaths.add( "org.apache.maven/jars/some-ejb-1.0-client.jar" );
361 actualArtifactPaths.add( "org.apache.maven/jars/testing-1.0.jar" );
362 actualArtifactPaths.add( "org.apache.maven/jars/testing-1.0-sources.jar" );
363 actualArtifactPaths.add( "org.apache.maven/jars/testing-UNKNOWN.jar" );
364 actualArtifactPaths.add( "org.apache.maven/jars/testing-1.0.zip" );
365 actualArtifactPaths.add( "org.apache.maven/jars/testing-1.0-20050611.112233-1.jar" );
366 actualArtifactPaths.add( "org.apache.maven/jars/testing-1.0.tar.gz" );
367 actualArtifactPaths.add( "org.apache.maven.update/jars/test-not-updated-1.0.jar" );
368 actualArtifactPaths.add( "org.apache.maven.update/jars/test-updated-1.0.jar" );
370 ManagedRepositoryConfiguration repository = createLegacyRepository();
372 List knownConsumers = new ArrayList();
373 KnownScanConsumer consumer = new KnownScanConsumer();
374 consumer.setIncludes( ARTIFACT_PATTERNS );
375 knownConsumers.add( consumer );
377 List invalidConsumers = new ArrayList();
378 InvalidScanConsumer badconsumer = new InvalidScanConsumer();
379 invalidConsumers.add( badconsumer );
381 RepositoryScanner scanner = lookupRepositoryScanner();
382 RepositoryContentStatistics stats =
383 scanner.scan( repository, knownConsumers, invalidConsumers, getIgnoreList(), RepositoryScanner.FRESH_SCAN );
385 assertNotNull( "Stats should not be null.", stats );
386 assertMinimumHits( "Stats.totalFileCount", actualArtifactPaths.size(), stats.getTotalFileCount() );
387 assertMinimumHits( "Processed Count", actualArtifactPaths.size(), consumer.getProcessCount() );