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
23 import java.util.Calendar;
24 import java.util.Collection;
25 import java.util.Date;
26 import java.util.List;
28 import org.apache.archiva.metadata.repository.stats.RepositoryStatistics;
29 import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
30 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
31 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
32 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
33 import org.apache.maven.archiva.model.ArtifactReference;
34 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
35 import org.codehaus.plexus.taskqueue.execution.TaskExecutor;
36 import org.codehaus.plexus.util.FileUtils;
39 * ArchivaRepositoryScanningTaskExecutorTest
43 public class ArchivaRepositoryScanningTaskExecutorTest
44 extends PlexusInSpringTestCase
46 private TaskExecutor taskExecutor;
50 private static final String TEST_REPO_ID = "testRepo";
52 private RepositoryStatisticsManager repositoryStatisticsManager;
54 private TestConsumer testConsumer;
56 protected void setUp()
61 taskExecutor = (TaskExecutor) lookup( TaskExecutor.class, "test-repository-scanning" );
63 File sourceRepoDir = new File( getBasedir(), "src/test/repositories/default-repository" );
64 repoDir = new File( getBasedir(), "target/default-repository" );
66 FileUtils.deleteDirectory( repoDir );
67 assertFalse( "Default Test Repository should not exist.", repoDir.exists() );
71 FileUtils.copyDirectoryStructure( sourceRepoDir, repoDir );
72 // set the timestamps to a time well in the past
73 Calendar cal = Calendar.getInstance();
74 cal.add( Calendar.YEAR, -1 );
75 for ( File f : (List<File>) FileUtils.getFiles( repoDir, "**", null ) )
77 f.setLastModified( cal.getTimeInMillis() );
79 // TODO: test they are excluded instead
80 for ( String dir : (List<String>) FileUtils.getDirectoryNames( repoDir, "**/.svn", null, false ) )
82 FileUtils.deleteDirectory( new File( repoDir, dir ) );
85 assertTrue( "Default Test Repository should exist.", repoDir.exists() && repoDir.isDirectory() );
87 ArchivaConfiguration archivaConfig = (ArchivaConfiguration) lookup( ArchivaConfiguration.class );
88 assertNotNull( archivaConfig );
91 ManagedRepositoryConfiguration repositoryConfiguration = new ManagedRepositoryConfiguration();
92 repositoryConfiguration.setId( TEST_REPO_ID );
93 repositoryConfiguration.setName( "Test Repository" );
94 repositoryConfiguration.setLocation( repoDir.getAbsolutePath() );
95 archivaConfig.getConfiguration().getManagedRepositories().clear();
96 archivaConfig.getConfiguration().addManagedRepository( repositoryConfiguration );
98 repositoryStatisticsManager = (RepositoryStatisticsManager) lookup( RepositoryStatisticsManager.class );
99 testConsumer = (TestConsumer) lookup( KnownRepositoryContentConsumer.class, "test-consumer" );
102 protected void tearDown()
105 FileUtils.deleteDirectory( repoDir );
107 assertFalse( repoDir.exists() );
112 public void testExecutor()
115 RepositoryTask repoTask = new RepositoryTask();
117 repoTask.setRepositoryId( TEST_REPO_ID );
119 taskExecutor.executeTask( repoTask );
121 Collection<ArtifactReference> unprocessedResultList = testConsumer.getConsumed();
123 assertNotNull( unprocessedResultList );
124 assertEquals( "Incorrect number of unprocessed artifacts detected.", 8, unprocessedResultList.size() );
127 public void testExecutorScanOnlyNewArtifacts()
130 RepositoryTask repoTask = new RepositoryTask();
132 repoTask.setRepositoryId( TEST_REPO_ID );
133 repoTask.setScanAll( false );
135 createAndSaveTestStats();
137 taskExecutor.executeTask( repoTask );
139 // check no artifacts processed
140 Collection<ArtifactReference> unprocessedResultList = testConsumer.getConsumed();
142 assertNotNull( unprocessedResultList );
143 assertEquals( "Incorrect number of unprocessed artifacts detected. No new artifacts should have been found.", 0,
144 unprocessedResultList.size() );
146 // check correctness of new stats
147 RepositoryStatistics newStats = repositoryStatisticsManager.getLastStatistics( TEST_REPO_ID );
148 assertEquals( 0, newStats.getNewFileCount() );
149 assertEquals( 31, newStats.getTotalFileCount() );
150 // TODO: can't test these as they weren't stored in the database, move to tests for RepositoryStatisticsManager implementation
151 // assertEquals( 8, newStats.getTotalArtifactCount() );
152 // assertEquals( 3, newStats.getTotalGroupCount() );
153 // assertEquals( 5, newStats.getTotalProjectCount() );
154 // assertEquals( 14159, newStats.getTotalArtifactFileSize() );
156 File newArtifactGroup = new File( repoDir, "org/apache/archiva" );
158 FileUtils.copyDirectoryStructure( new File( getBasedir(), "target/test-classes/test-repo/org/apache/archiva" ),
161 // update last modified date
162 new File( newArtifactGroup, "archiva-index-methods-jar-test/1.0/pom.xml" ).setLastModified(
163 Calendar.getInstance().getTimeInMillis() + 1000 );
164 new File( newArtifactGroup,
165 "archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" ).setLastModified(
166 Calendar.getInstance().getTimeInMillis() + 1000 );
168 assertTrue( newArtifactGroup.exists() );
170 taskExecutor.executeTask( repoTask );
172 unprocessedResultList = testConsumer.getConsumed();
173 assertNotNull( unprocessedResultList );
174 assertEquals( "Incorrect number of unprocessed artifacts detected. One new artifact should have been found.", 1,
175 unprocessedResultList.size() );
177 // check correctness of new stats
178 RepositoryStatistics updatedStats = repositoryStatisticsManager.getLastStatistics( TEST_REPO_ID );
179 assertEquals( 2, updatedStats.getNewFileCount() );
180 assertEquals( 33, updatedStats.getTotalFileCount() );
181 // TODO: can't test these as they weren't stored in the database, move to tests for RepositoryStatisticsManager implementation
182 // assertEquals( 8, newStats.getTotalArtifactCount() );
183 // assertEquals( 3, newStats.getTotalGroupCount() );
184 // assertEquals( 5, newStats.getTotalProjectCount() );
185 // assertEquals( 19301, updatedStats.getTotalArtifactFileSize() );
188 public void testExecutorScanOnlyNewArtifactsChangeTimes()
191 RepositoryTask repoTask = new RepositoryTask();
193 repoTask.setRepositoryId( TEST_REPO_ID );
194 repoTask.setScanAll( false );
196 createAndSaveTestStats();
198 File newArtifactGroup = new File( repoDir, "org/apache/archiva" );
200 FileUtils.copyDirectoryStructure( new File( getBasedir(), "target/test-classes/test-repo/org/apache/archiva" ),
203 // update last modified date, placing shortly after last scan
204 new File( newArtifactGroup, "archiva-index-methods-jar-test/1.0/pom.xml" ).setLastModified(
205 Calendar.getInstance().getTimeInMillis() + 1000 );
206 new File( newArtifactGroup,
207 "archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" ).setLastModified(
208 Calendar.getInstance().getTimeInMillis() + 1000 );
210 assertTrue( newArtifactGroup.exists() );
212 // scan using the really long previous duration
213 taskExecutor.executeTask( repoTask );
215 // check no artifacts processed
216 Collection<ArtifactReference> unprocessedResultList = testConsumer.getConsumed();
217 assertNotNull( unprocessedResultList );
218 assertEquals( "Incorrect number of unprocessed artifacts detected. One new artifact should have been found.", 1,
219 unprocessedResultList.size() );
221 // check correctness of new stats
222 RepositoryStatistics newStats = repositoryStatisticsManager.getLastStatistics( TEST_REPO_ID );
223 assertEquals( 2, newStats.getNewFileCount() );
224 assertEquals( 33, newStats.getTotalFileCount() );
225 // TODO: can't test these as they weren't stored in the database, move to tests for RepositoryStatisticsManager implementation
226 // assertEquals( 8, newStats.getTotalArtifactCount() );
227 // assertEquals( 3, newStats.getTotalGroupCount() );
228 // assertEquals( 5, newStats.getTotalProjectCount() );
229 // assertEquals( 19301, newStats.getTotalArtifactFileSize() );
232 public void testExecutorScanOnlyNewArtifactsMidScan()
235 RepositoryTask repoTask = new RepositoryTask();
237 repoTask.setRepositoryId( TEST_REPO_ID );
238 repoTask.setScanAll( false );
240 createAndSaveTestStats();
242 File newArtifactGroup = new File( repoDir, "org/apache/archiva" );
244 FileUtils.copyDirectoryStructure( new File( getBasedir(), "target/test-classes/test-repo/org/apache/archiva" ),
247 // update last modified date, placing in middle of last scan
248 new File( newArtifactGroup, "archiva-index-methods-jar-test/1.0/pom.xml" ).setLastModified(
249 Calendar.getInstance().getTimeInMillis() - 50000 );
250 new File( newArtifactGroup,
251 "archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" ).setLastModified(
252 Calendar.getInstance().getTimeInMillis() - 50000 );
254 assertTrue( newArtifactGroup.exists() );
256 // scan using the really long previous duration
257 taskExecutor.executeTask( repoTask );
259 // check no artifacts processed
260 Collection<ArtifactReference> unprocessedResultList = testConsumer.getConsumed();
261 assertNotNull( unprocessedResultList );
262 assertEquals( "Incorrect number of unprocessed artifacts detected. One new artifact should have been found.", 1,
263 unprocessedResultList.size() );
265 // check correctness of new stats
266 RepositoryStatistics newStats = repositoryStatisticsManager.getLastStatistics( TEST_REPO_ID );
267 assertEquals( 2, newStats.getNewFileCount() );
268 assertEquals( 33, newStats.getTotalFileCount() );
269 // TODO: can't test these as they weren't stored in the database, move to tests for RepositoryStatisticsManager implementation
270 // assertEquals( 8, newStats.getTotalArtifactCount() );
271 // assertEquals( 3, newStats.getTotalGroupCount() );
272 // assertEquals( 5, newStats.getTotalProjectCount() );
273 // assertEquals( 19301, newStats.getTotalArtifactFileSize() );
276 public void testExecutorForceScanAll()
279 RepositoryTask repoTask = new RepositoryTask();
281 repoTask.setRepositoryId( TEST_REPO_ID );
282 repoTask.setScanAll( true );
284 Date date = Calendar.getInstance().getTime();
285 RepositoryStatistics stats = new RepositoryStatistics();
286 stats.setScanStartTime( new Date( date.getTime() - 1234567 ) );
287 stats.setScanEndTime( date );
288 stats.setNewFileCount( 8 );
289 stats.setTotalArtifactCount( 8 );
290 stats.setTotalFileCount( 8 );
291 stats.setTotalGroupCount( 3 );
292 stats.setTotalProjectCount( 5 );
293 stats.setTotalArtifactFileSize( 999999 );
295 repositoryStatisticsManager.addStatisticsAfterScan( TEST_REPO_ID, stats );
297 taskExecutor.executeTask( repoTask );
299 Collection<ArtifactReference> unprocessedResultList = testConsumer.getConsumed();
301 assertNotNull( unprocessedResultList );
302 assertEquals( "Incorrect number of unprocessed artifacts detected.", 8, unprocessedResultList.size() );
305 private void createAndSaveTestStats()
307 Date date = Calendar.getInstance().getTime();
308 RepositoryStatistics stats = new RepositoryStatistics();
309 stats.setScanStartTime( new Date( date.getTime() - 1234567 ) );
310 stats.setScanEndTime( date );
311 stats.setNewFileCount( 31 );
312 stats.setTotalArtifactCount( 8 );
313 stats.setTotalFileCount( 31 );
314 stats.setTotalGroupCount( 3 );
315 stats.setTotalProjectCount( 5 );
316 stats.setTotalArtifactFileSize( 38545 );
318 repositoryStatisticsManager.addStatisticsAfterScan( TEST_REPO_ID, stats );