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.MetadataRepositoryException;
23 import org.apache.archiva.metadata.repository.stats.RepositoryStatistics;
24 import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
25 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
26 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
27 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
28 import org.apache.maven.archiva.model.ArtifactReference;
29 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
30 import org.codehaus.plexus.taskqueue.execution.TaskExecutor;
31 import org.codehaus.plexus.util.FileUtils;
34 import java.util.Calendar;
35 import java.util.Collection;
36 import java.util.Date;
37 import java.util.List;
40 * ArchivaRepositoryScanningTaskExecutorTest
44 public class ArchivaRepositoryScanningTaskExecutorTest
45 extends PlexusInSpringTestCase
47 private TaskExecutor taskExecutor;
51 private static final String TEST_REPO_ID = "testRepo";
53 private RepositoryStatisticsManager repositoryStatisticsManager;
55 private TestConsumer testConsumer;
57 protected void setUp()
62 taskExecutor = (TaskExecutor) lookup( TaskExecutor.class, "test-repository-scanning" );
64 File sourceRepoDir = new File( getBasedir(), "src/test/repositories/default-repository" );
65 repoDir = new File( getBasedir(), "target/default-repository" );
67 FileUtils.deleteDirectory( repoDir );
68 assertFalse( "Default Test Repository should not exist.", repoDir.exists() );
72 FileUtils.copyDirectoryStructure( sourceRepoDir, repoDir );
73 // set the timestamps to a time well in the past
74 Calendar cal = Calendar.getInstance();
75 cal.add( Calendar.YEAR, -1 );
76 for ( File f : (List<File>) FileUtils.getFiles( repoDir, "**", null ) )
78 f.setLastModified( cal.getTimeInMillis() );
80 // TODO: test they are excluded instead
81 for ( String dir : (List<String>) FileUtils.getDirectoryNames( repoDir, "**/.svn", null, false ) )
83 FileUtils.deleteDirectory( new File( repoDir, dir ) );
86 assertTrue( "Default Test Repository should exist.", repoDir.exists() && repoDir.isDirectory() );
88 ArchivaConfiguration archivaConfig = (ArchivaConfiguration) lookup( ArchivaConfiguration.class );
89 assertNotNull( archivaConfig );
92 ManagedRepositoryConfiguration repositoryConfiguration = new ManagedRepositoryConfiguration();
93 repositoryConfiguration.setId( TEST_REPO_ID );
94 repositoryConfiguration.setName( "Test Repository" );
95 repositoryConfiguration.setLocation( repoDir.getAbsolutePath() );
96 archivaConfig.getConfiguration().getManagedRepositories().clear();
97 archivaConfig.getConfiguration().addManagedRepository( repositoryConfiguration );
99 repositoryStatisticsManager = (RepositoryStatisticsManager) lookup( RepositoryStatisticsManager.class );
100 testConsumer = (TestConsumer) lookup( KnownRepositoryContentConsumer.class, "test-consumer" );
103 protected void tearDown()
106 FileUtils.deleteDirectory( repoDir );
108 assertFalse( repoDir.exists() );
113 public void testExecutor()
116 RepositoryTask repoTask = new RepositoryTask();
118 repoTask.setRepositoryId( TEST_REPO_ID );
120 taskExecutor.executeTask( repoTask );
122 Collection<ArtifactReference> unprocessedResultList = testConsumer.getConsumed();
124 assertNotNull( unprocessedResultList );
125 assertEquals( "Incorrect number of unprocessed artifacts detected.", 8, unprocessedResultList.size() );
128 public void testExecutorScanOnlyNewArtifacts()
131 RepositoryTask repoTask = new RepositoryTask();
133 repoTask.setRepositoryId( TEST_REPO_ID );
134 repoTask.setScanAll( false );
136 createAndSaveTestStats();
138 taskExecutor.executeTask( repoTask );
140 // check no artifacts processed
141 Collection<ArtifactReference> unprocessedResultList = testConsumer.getConsumed();
143 assertNotNull( unprocessedResultList );
144 assertEquals( "Incorrect number of unprocessed artifacts detected. No new artifacts should have been found.", 0,
145 unprocessedResultList.size() );
147 // check correctness of new stats
148 RepositoryStatistics newStats = repositoryStatisticsManager.getLastStatistics( TEST_REPO_ID );
149 assertEquals( 0, newStats.getNewFileCount() );
150 assertEquals( 31, newStats.getTotalFileCount() );
151 // FIXME: can't test these as they weren't stored in the database, move to tests for RepositoryStatisticsManager implementation
152 // assertEquals( 8, newStats.getTotalArtifactCount() );
153 // assertEquals( 3, newStats.getTotalGroupCount() );
154 // assertEquals( 5, newStats.getTotalProjectCount() );
155 // assertEquals( 14159, newStats.getTotalArtifactFileSize() );
157 File newArtifactGroup = new File( repoDir, "org/apache/archiva" );
159 FileUtils.copyDirectoryStructure( new File( getBasedir(), "target/test-classes/test-repo/org/apache/archiva" ),
162 // update last modified date
163 new File( newArtifactGroup, "archiva-index-methods-jar-test/1.0/pom.xml" ).setLastModified(
164 Calendar.getInstance().getTimeInMillis() + 1000 );
165 new File( newArtifactGroup,
166 "archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" ).setLastModified(
167 Calendar.getInstance().getTimeInMillis() + 1000 );
169 assertTrue( newArtifactGroup.exists() );
171 taskExecutor.executeTask( repoTask );
173 unprocessedResultList = testConsumer.getConsumed();
174 assertNotNull( unprocessedResultList );
175 assertEquals( "Incorrect number of unprocessed artifacts detected. One new artifact should have been found.", 1,
176 unprocessedResultList.size() );
178 // check correctness of new stats
179 RepositoryStatistics updatedStats = repositoryStatisticsManager.getLastStatistics( TEST_REPO_ID );
180 assertEquals( 2, updatedStats.getNewFileCount() );
181 assertEquals( 33, updatedStats.getTotalFileCount() );
182 // FIXME: can't test these as they weren't stored in the database, move to tests for RepositoryStatisticsManager implementation
183 // assertEquals( 8, newStats.getTotalArtifactCount() );
184 // assertEquals( 3, newStats.getTotalGroupCount() );
185 // assertEquals( 5, newStats.getTotalProjectCount() );
186 // assertEquals( 19301, updatedStats.getTotalArtifactFileSize() );
189 public void testExecutorScanOnlyNewArtifactsChangeTimes()
192 RepositoryTask repoTask = new RepositoryTask();
194 repoTask.setRepositoryId( TEST_REPO_ID );
195 repoTask.setScanAll( false );
197 createAndSaveTestStats();
199 File newArtifactGroup = new File( repoDir, "org/apache/archiva" );
201 FileUtils.copyDirectoryStructure( new File( getBasedir(), "target/test-classes/test-repo/org/apache/archiva" ),
204 // update last modified date, placing shortly after last scan
205 new File( newArtifactGroup, "archiva-index-methods-jar-test/1.0/pom.xml" ).setLastModified(
206 Calendar.getInstance().getTimeInMillis() + 1000 );
207 new File( newArtifactGroup,
208 "archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" ).setLastModified(
209 Calendar.getInstance().getTimeInMillis() + 1000 );
211 assertTrue( newArtifactGroup.exists() );
213 // scan using the really long previous duration
214 taskExecutor.executeTask( repoTask );
216 // check no artifacts processed
217 Collection<ArtifactReference> unprocessedResultList = testConsumer.getConsumed();
218 assertNotNull( unprocessedResultList );
219 assertEquals( "Incorrect number of unprocessed artifacts detected. One new artifact should have been found.", 1,
220 unprocessedResultList.size() );
222 // check correctness of new stats
223 RepositoryStatistics newStats = repositoryStatisticsManager.getLastStatistics( TEST_REPO_ID );
224 assertEquals( 2, newStats.getNewFileCount() );
225 assertEquals( 33, newStats.getTotalFileCount() );
226 // FIXME: can't test these as they weren't stored in the database, move to tests for RepositoryStatisticsManager implementation
227 // assertEquals( 8, newStats.getTotalArtifactCount() );
228 // assertEquals( 3, newStats.getTotalGroupCount() );
229 // assertEquals( 5, newStats.getTotalProjectCount() );
230 // assertEquals( 19301, newStats.getTotalArtifactFileSize() );
233 public void testExecutorScanOnlyNewArtifactsMidScan()
236 RepositoryTask repoTask = new RepositoryTask();
238 repoTask.setRepositoryId( TEST_REPO_ID );
239 repoTask.setScanAll( false );
241 createAndSaveTestStats();
243 File newArtifactGroup = new File( repoDir, "org/apache/archiva" );
245 FileUtils.copyDirectoryStructure( new File( getBasedir(), "target/test-classes/test-repo/org/apache/archiva" ),
248 // update last modified date, placing in middle of last scan
249 new File( newArtifactGroup, "archiva-index-methods-jar-test/1.0/pom.xml" ).setLastModified(
250 Calendar.getInstance().getTimeInMillis() - 50000 );
251 new File( newArtifactGroup,
252 "archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" ).setLastModified(
253 Calendar.getInstance().getTimeInMillis() - 50000 );
255 assertTrue( newArtifactGroup.exists() );
257 // scan using the really long previous duration
258 taskExecutor.executeTask( repoTask );
260 // check no artifacts processed
261 Collection<ArtifactReference> unprocessedResultList = testConsumer.getConsumed();
262 assertNotNull( unprocessedResultList );
263 assertEquals( "Incorrect number of unprocessed artifacts detected. One new artifact should have been found.", 1,
264 unprocessedResultList.size() );
266 // check correctness of new stats
267 RepositoryStatistics newStats = repositoryStatisticsManager.getLastStatistics( TEST_REPO_ID );
268 assertEquals( 2, newStats.getNewFileCount() );
269 assertEquals( 33, newStats.getTotalFileCount() );
270 // FIXME: can't test these as they weren't stored in the database, move to tests for RepositoryStatisticsManager implementation
271 // assertEquals( 8, newStats.getTotalArtifactCount() );
272 // assertEquals( 3, newStats.getTotalGroupCount() );
273 // assertEquals( 5, newStats.getTotalProjectCount() );
274 // assertEquals( 19301, newStats.getTotalArtifactFileSize() );
277 public void testExecutorForceScanAll()
280 RepositoryTask repoTask = new RepositoryTask();
282 repoTask.setRepositoryId( TEST_REPO_ID );
283 repoTask.setScanAll( true );
285 Date date = Calendar.getInstance().getTime();
286 repositoryStatisticsManager.addStatisticsAfterScan( TEST_REPO_ID, new Date( date.getTime() - 1234567 ), date, 8,
289 taskExecutor.executeTask( repoTask );
291 Collection<ArtifactReference> unprocessedResultList = testConsumer.getConsumed();
293 assertNotNull( unprocessedResultList );
294 assertEquals( "Incorrect number of unprocessed artifacts detected.", 8, unprocessedResultList.size() );
297 private void createAndSaveTestStats()
298 throws MetadataRepositoryException
300 Date date = Calendar.getInstance().getTime();
301 RepositoryStatistics stats = new RepositoryStatistics();
302 stats.setScanStartTime( new Date( date.getTime() - 1234567 ) );
303 stats.setScanEndTime( date );
304 stats.setNewFileCount( 31 );
305 stats.setTotalArtifactCount( 8 );
306 stats.setTotalFileCount( 31 );
307 stats.setTotalGroupCount( 3 );
308 stats.setTotalProjectCount( 5 );
309 stats.setTotalArtifactFileSize( 38545 );
311 repositoryStatisticsManager.addStatisticsAfterScan( TEST_REPO_ID, new Date( date.getTime() - 1234567 ), date,