1 package org.apache.archiva.scheduler.repository;
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.archiva.metadata.repository.MetadataRepository;
23 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
24 import org.apache.archiva.metadata.repository.stats.RepositoryStatistics;
25 import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
26 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
27 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
28 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
29 import org.apache.maven.archiva.model.ArtifactReference;
30 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
31 import org.codehaus.plexus.taskqueue.execution.TaskExecutor;
32 import org.codehaus.plexus.util.FileUtils;
35 import java.util.Calendar;
36 import java.util.Collection;
37 import java.util.Date;
38 import java.util.List;
40 import static org.mockito.Mockito.mock;
43 * ArchivaRepositoryScanningTaskExecutorTest
47 public class ArchivaRepositoryScanningTaskExecutorTest
48 extends PlexusInSpringTestCase
50 private TaskExecutor taskExecutor;
54 private static final String TEST_REPO_ID = "testRepo";
56 private RepositoryStatisticsManager repositoryStatisticsManager;
58 private TestConsumer testConsumer;
60 private MetadataRepository metadataRepository;
62 protected void setUp()
69 taskExecutor = (TaskExecutor) lookup( TaskExecutor.class, "test-repository-scanning" );
73 // TODO: handle cleanup in plexus-spring lookup method instead
74 applicationContext.close();
78 File sourceRepoDir = new File( getBasedir(), "src/test/repositories/default-repository" );
79 repoDir = new File( getBasedir(), "target/default-repository" );
81 FileUtils.deleteDirectory( repoDir );
82 assertFalse( "Default Test Repository should not exist.", repoDir.exists() );
86 FileUtils.copyDirectoryStructure( sourceRepoDir, repoDir );
87 // set the timestamps to a time well in the past
88 Calendar cal = Calendar.getInstance();
89 cal.add( Calendar.YEAR, -1 );
90 for ( File f : (List<File>) FileUtils.getFiles( repoDir, "**", null ) )
92 f.setLastModified( cal.getTimeInMillis() );
94 // TODO: test they are excluded instead
95 for ( String dir : (List<String>) FileUtils.getDirectoryNames( repoDir, "**/.svn", null, false ) )
97 FileUtils.deleteDirectory( new File( repoDir, dir ) );
100 assertTrue( "Default Test Repository should exist.", repoDir.exists() && repoDir.isDirectory() );
102 ArchivaConfiguration archivaConfig = (ArchivaConfiguration) lookup( ArchivaConfiguration.class );
103 assertNotNull( archivaConfig );
106 ManagedRepositoryConfiguration repositoryConfiguration = new ManagedRepositoryConfiguration();
107 repositoryConfiguration.setId( TEST_REPO_ID );
108 repositoryConfiguration.setName( "Test Repository" );
109 repositoryConfiguration.setLocation( repoDir.getAbsolutePath() );
110 archivaConfig.getConfiguration().getManagedRepositories().clear();
111 archivaConfig.getConfiguration().addManagedRepository( repositoryConfiguration );
113 repositoryStatisticsManager = (RepositoryStatisticsManager) lookup( RepositoryStatisticsManager.class );
114 testConsumer = (TestConsumer) lookup( KnownRepositoryContentConsumer.class, "test-consumer" );
116 metadataRepository = mock( MetadataRepository.class );
119 protected void tearDown()
122 FileUtils.deleteDirectory( repoDir );
124 assertFalse( repoDir.exists() );
129 public void testExecutor()
132 RepositoryTask repoTask = new RepositoryTask();
134 repoTask.setRepositoryId( TEST_REPO_ID );
136 taskExecutor.executeTask( repoTask );
138 Collection<ArtifactReference> unprocessedResultList = testConsumer.getConsumed();
140 assertNotNull( unprocessedResultList );
141 assertEquals( "Incorrect number of unprocessed artifacts detected.", 8, unprocessedResultList.size() );
144 public void testExecutorScanOnlyNewArtifacts()
147 RepositoryTask repoTask = new RepositoryTask();
149 repoTask.setRepositoryId( TEST_REPO_ID );
150 repoTask.setScanAll( false );
152 createAndSaveTestStats();
154 taskExecutor.executeTask( repoTask );
156 // check no artifacts processed
157 Collection<ArtifactReference> unprocessedResultList = testConsumer.getConsumed();
159 assertNotNull( unprocessedResultList );
160 assertEquals( "Incorrect number of unprocessed artifacts detected. No new artifacts should have been found.", 0,
161 unprocessedResultList.size() );
163 // check correctness of new stats
164 RepositoryStatistics newStats = repositoryStatisticsManager.getLastStatistics( metadataRepository,
166 assertEquals( 0, newStats.getNewFileCount() );
167 assertEquals( 31, newStats.getTotalFileCount() );
168 // FIXME: can't test these as they weren't stored in the database, move to tests for RepositoryStatisticsManager implementation
169 // assertEquals( 8, newStats.getTotalArtifactCount() );
170 // assertEquals( 3, newStats.getTotalGroupCount() );
171 // assertEquals( 5, newStats.getTotalProjectCount() );
172 // assertEquals( 14159, newStats.getTotalArtifactFileSize() );
174 File newArtifactGroup = new File( repoDir, "org/apache/archiva" );
176 FileUtils.copyDirectoryStructure( new File( getBasedir(), "target/test-classes/test-repo/org/apache/archiva" ),
179 // update last modified date
180 new File( newArtifactGroup, "archiva-index-methods-jar-test/1.0/pom.xml" ).setLastModified(
181 Calendar.getInstance().getTimeInMillis() + 1000 );
182 new File( newArtifactGroup,
183 "archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" ).setLastModified(
184 Calendar.getInstance().getTimeInMillis() + 1000 );
186 assertTrue( newArtifactGroup.exists() );
188 taskExecutor.executeTask( repoTask );
190 unprocessedResultList = testConsumer.getConsumed();
191 assertNotNull( unprocessedResultList );
192 assertEquals( "Incorrect number of unprocessed artifacts detected. One new artifact should have been found.", 1,
193 unprocessedResultList.size() );
195 // check correctness of new stats
196 RepositoryStatistics updatedStats = repositoryStatisticsManager.getLastStatistics( metadataRepository,
198 assertEquals( 2, updatedStats.getNewFileCount() );
199 assertEquals( 33, updatedStats.getTotalFileCount() );
200 // FIXME: can't test these as they weren't stored in the database, move to tests for RepositoryStatisticsManager implementation
201 // assertEquals( 8, newStats.getTotalArtifactCount() );
202 // assertEquals( 3, newStats.getTotalGroupCount() );
203 // assertEquals( 5, newStats.getTotalProjectCount() );
204 // assertEquals( 19301, updatedStats.getTotalArtifactFileSize() );
207 public void testExecutorScanOnlyNewArtifactsChangeTimes()
210 RepositoryTask repoTask = new RepositoryTask();
212 repoTask.setRepositoryId( TEST_REPO_ID );
213 repoTask.setScanAll( false );
215 createAndSaveTestStats();
217 File newArtifactGroup = new File( repoDir, "org/apache/archiva" );
219 FileUtils.copyDirectoryStructure( new File( getBasedir(), "target/test-classes/test-repo/org/apache/archiva" ),
222 // update last modified date, placing shortly after last scan
223 new File( newArtifactGroup, "archiva-index-methods-jar-test/1.0/pom.xml" ).setLastModified(
224 Calendar.getInstance().getTimeInMillis() + 1000 );
225 new File( newArtifactGroup,
226 "archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" ).setLastModified(
227 Calendar.getInstance().getTimeInMillis() + 1000 );
229 assertTrue( newArtifactGroup.exists() );
231 // scan using the really long previous duration
232 taskExecutor.executeTask( repoTask );
234 // check no artifacts processed
235 Collection<ArtifactReference> unprocessedResultList = testConsumer.getConsumed();
236 assertNotNull( unprocessedResultList );
237 assertEquals( "Incorrect number of unprocessed artifacts detected. One new artifact should have been found.", 1,
238 unprocessedResultList.size() );
240 // check correctness of new stats
241 RepositoryStatistics newStats = repositoryStatisticsManager.getLastStatistics( metadataRepository,
243 assertEquals( 2, newStats.getNewFileCount() );
244 assertEquals( 33, newStats.getTotalFileCount() );
245 // FIXME: can't test these as they weren't stored in the database, move to tests for RepositoryStatisticsManager implementation
246 // assertEquals( 8, newStats.getTotalArtifactCount() );
247 // assertEquals( 3, newStats.getTotalGroupCount() );
248 // assertEquals( 5, newStats.getTotalProjectCount() );
249 // assertEquals( 19301, newStats.getTotalArtifactFileSize() );
252 public void testExecutorScanOnlyNewArtifactsMidScan()
255 RepositoryTask repoTask = new RepositoryTask();
257 repoTask.setRepositoryId( TEST_REPO_ID );
258 repoTask.setScanAll( false );
260 createAndSaveTestStats();
262 File newArtifactGroup = new File( repoDir, "org/apache/archiva" );
264 FileUtils.copyDirectoryStructure( new File( getBasedir(), "target/test-classes/test-repo/org/apache/archiva" ),
267 // update last modified date, placing in middle of last scan
268 new File( newArtifactGroup, "archiva-index-methods-jar-test/1.0/pom.xml" ).setLastModified(
269 Calendar.getInstance().getTimeInMillis() - 50000 );
270 new File( newArtifactGroup,
271 "archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" ).setLastModified(
272 Calendar.getInstance().getTimeInMillis() - 50000 );
274 assertTrue( newArtifactGroup.exists() );
276 // scan using the really long previous duration
277 taskExecutor.executeTask( repoTask );
279 // check no artifacts processed
280 Collection<ArtifactReference> unprocessedResultList = testConsumer.getConsumed();
281 assertNotNull( unprocessedResultList );
282 assertEquals( "Incorrect number of unprocessed artifacts detected. One new artifact should have been found.", 1,
283 unprocessedResultList.size() );
285 // check correctness of new stats
286 RepositoryStatistics newStats = repositoryStatisticsManager.getLastStatistics( metadataRepository,
288 assertEquals( 2, newStats.getNewFileCount() );
289 assertEquals( 33, newStats.getTotalFileCount() );
290 // FIXME: can't test these as they weren't stored in the database, move to tests for RepositoryStatisticsManager implementation
291 // assertEquals( 8, newStats.getTotalArtifactCount() );
292 // assertEquals( 3, newStats.getTotalGroupCount() );
293 // assertEquals( 5, newStats.getTotalProjectCount() );
294 // assertEquals( 19301, newStats.getTotalArtifactFileSize() );
297 public void testExecutorForceScanAll()
300 RepositoryTask repoTask = new RepositoryTask();
302 repoTask.setRepositoryId( TEST_REPO_ID );
303 repoTask.setScanAll( true );
305 Date date = Calendar.getInstance().getTime();
306 repositoryStatisticsManager.addStatisticsAfterScan( metadataRepository, TEST_REPO_ID, new Date(
307 date.getTime() - 1234567 ), date, 8, 8 );
309 taskExecutor.executeTask( repoTask );
311 Collection<ArtifactReference> unprocessedResultList = testConsumer.getConsumed();
313 assertNotNull( unprocessedResultList );
314 assertEquals( "Incorrect number of unprocessed artifacts detected.", 8, unprocessedResultList.size() );
317 private void createAndSaveTestStats()
318 throws MetadataRepositoryException
320 Date date = Calendar.getInstance().getTime();
321 RepositoryStatistics stats = new RepositoryStatistics();
322 stats.setScanStartTime( new Date( date.getTime() - 1234567 ) );
323 stats.setScanEndTime( date );
324 stats.setNewFileCount( 31 );
325 stats.setTotalArtifactCount( 8 );
326 stats.setTotalFileCount( 31 );
327 stats.setTotalGroupCount( 3 );
328 stats.setTotalProjectCount( 5 );
329 stats.setTotalArtifactFileSize( 38545 );
331 repositoryStatisticsManager.addStatisticsAfterScan( metadataRepository, TEST_REPO_ID, new Date(
332 date.getTime() - 1234567 ), date, 31, 31 );