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.model.DefaultRepositoryStatistics;
24 import org.apache.archiva.metadata.repository.stats.model.RepositoryStatistics;
25 import org.apache.archiva.model.ArtifactReference;
26 import org.apache.archiva.scheduler.repository.model.RepositoryTask;
27 import org.codehaus.plexus.util.FileUtils;
28 import org.junit.Test;
29 import org.springframework.test.context.ContextConfiguration;
32 import java.util.Calendar;
33 import java.util.Collection;
34 import java.util.Date;
37 * ArchivaRepositoryScanningTaskExecutorPhase2Test
40 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
41 public class ArchivaRepositoryScanningTaskExecutorPhase2Test
42 extends AbstractArchivaRepositoryScanningTaskExecutorTest
46 public void testExecutorScanOnlyNewArtifacts()
50 RepositoryTask repoTask = new RepositoryTask();
52 repoTask.setRepositoryId( TEST_REPO_ID );
53 repoTask.setScanAll( false );
54 createAndSaveTestStats();
55 taskExecutor.executeTask( repoTask );
57 // check no artifacts processed
58 Collection<ArtifactReference> unprocessedResultList = testConsumer.getConsumed();
60 assertNotNull( unprocessedResultList );
61 assertEquals( "Incorrect number of unprocessed artifacts detected. No new artifacts should have been found.", 0,
62 unprocessedResultList.size() );
64 // check correctness of new stats
65 RepositoryStatistics newStats =
66 repositoryStatisticsManager.getLastStatistics( metadataRepository, TEST_REPO_ID );
67 assertEquals( 0, newStats.getNewFileCount() );
68 assertEquals( 31, newStats.getTotalFileCount() );
69 // FIXME: can't test these as they weren't stored in the database, move to tests for RepositoryStatisticsManager implementation
70 // assertEquals( 8, newStats.getTotalArtifactCount() );
71 // assertEquals( 3, newStats.getTotalGroupCount() );
72 // assertEquals( 5, newStats.getTotalProjectCount() );
73 // assertEquals( 14159, newStats.getTotalArtifactFileSize() );
75 File newArtifactGroup = new File( repoDir, "org/apache/archiva" );
76 assertFalse( "newArtifactGroup should not exist.", newArtifactGroup.exists() );
78 FileUtils.copyDirectoryStructure( new File( "target/test-classes/test-repo/org/apache/archiva" ),
81 // update last modified date
82 new File( newArtifactGroup, "archiva-index-methods-jar-test/1.0/pom.xml" ).setLastModified(
83 Calendar.getInstance().getTimeInMillis() + 1000 );
84 new File( newArtifactGroup,
85 "archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" ).setLastModified(
86 Calendar.getInstance().getTimeInMillis() + 1000 );
88 assertTrue( newArtifactGroup.exists() );
90 taskExecutor.executeTask( repoTask );
92 unprocessedResultList = testConsumer.getConsumed();
93 assertNotNull( unprocessedResultList );
94 assertEquals( "Incorrect number of unprocessed artifacts detected. One new artifact should have been found.", 1,
95 unprocessedResultList.size() );
97 // check correctness of new stats
98 RepositoryStatistics updatedStats =
99 repositoryStatisticsManager.getLastStatistics( metadataRepository, TEST_REPO_ID );
100 assertEquals( 2, updatedStats.getNewFileCount() );
101 assertEquals( 33, updatedStats.getTotalFileCount() );
102 // FIXME: can't test these as they weren't stored in the database, move to tests for RepositoryStatisticsManager implementation
103 // assertEquals( 8, newStats.getTotalArtifactCount() );
104 // assertEquals( 3, newStats.getTotalGroupCount() );
105 // assertEquals( 5, newStats.getTotalProjectCount() );
106 // assertEquals( 19301, updatedStats.getTotalArtifactFileSize() );
110 public void testExecutorScanOnlyNewArtifactsChangeTimes()
113 RepositoryTask repoTask = new RepositoryTask();
115 repoTask.setRepositoryId( TEST_REPO_ID );
116 repoTask.setScanAll( false );
118 createAndSaveTestStats();
120 File newArtifactGroup = new File( repoDir, "org/apache/archiva" );
121 assertFalse( "newArtifactGroup should not exist.", newArtifactGroup.exists() );
123 FileUtils.copyDirectoryStructure( new File( "target/test-classes/test-repo/org/apache/archiva" ),
126 // update last modified date, placing shortly after last scan
127 new File( newArtifactGroup, "archiva-index-methods-jar-test/1.0/pom.xml" ).setLastModified(
128 Calendar.getInstance().getTimeInMillis() + 1000 );
129 new File( newArtifactGroup,
130 "archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" ).setLastModified(
131 Calendar.getInstance().getTimeInMillis() + 1000 );
133 assertTrue( newArtifactGroup.exists() );
135 // scan using the really long previous duration
136 taskExecutor.executeTask( repoTask );
138 // check no artifacts processed
139 Collection<ArtifactReference> unprocessedResultList = testConsumer.getConsumed();
140 assertNotNull( unprocessedResultList );
141 assertEquals( "Incorrect number of unprocessed artifacts detected. One new artifact should have been found.", 1,
142 unprocessedResultList.size() );
144 // check correctness of new stats
145 RepositoryStatistics newStats =
146 repositoryStatisticsManager.getLastStatistics( metadataRepository, TEST_REPO_ID );
147 assertEquals( 2, newStats.getNewFileCount() );
148 assertEquals( 33, newStats.getTotalFileCount() );
149 // FIXME: can't test these as they weren't stored in the database, move to tests for RepositoryStatisticsManager implementation
150 // assertEquals( 8, newStats.getTotalArtifactCount() );
151 // assertEquals( 3, newStats.getTotalGroupCount() );
152 // assertEquals( 5, newStats.getTotalProjectCount() );
153 // assertEquals( 19301, newStats.getTotalArtifactFileSize() );
157 public void testExecutorScanOnlyNewArtifactsMidScan()
160 RepositoryTask repoTask = new RepositoryTask();
162 repoTask.setRepositoryId( TEST_REPO_ID );
163 repoTask.setScanAll( false );
165 createAndSaveTestStats();
167 File newArtifactGroup = new File( repoDir, "org/apache/archiva" );
168 assertFalse( "newArtifactGroup should not exist.", newArtifactGroup.exists() );
170 FileUtils.copyDirectoryStructure( new File( "target/test-classes/test-repo/org/apache/archiva" ),
173 // update last modified date, placing in middle of last scan
174 new File( newArtifactGroup, "archiva-index-methods-jar-test/1.0/pom.xml" ).setLastModified(
175 Calendar.getInstance().getTimeInMillis() - 50000 );
176 new File( newArtifactGroup,
177 "archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" ).setLastModified(
178 Calendar.getInstance().getTimeInMillis() - 50000 );
180 assertTrue( newArtifactGroup.exists() );
182 // scan using the really long previous duration
183 taskExecutor.executeTask( repoTask );
185 // check no artifacts processed
186 Collection<ArtifactReference> unprocessedResultList = testConsumer.getConsumed();
187 assertNotNull( unprocessedResultList );
188 assertEquals( "Incorrect number of unprocessed artifacts detected. One new artifact should have been found.", 1,
189 unprocessedResultList.size() );
191 // check correctness of new stats
192 RepositoryStatistics newStats =
193 repositoryStatisticsManager.getLastStatistics( metadataRepository, TEST_REPO_ID );
194 assertEquals( 2, newStats.getNewFileCount() );
195 assertEquals( 33, newStats.getTotalFileCount() );
196 // FIXME: can't test these as they weren't stored in the database, move to tests for RepositoryStatisticsManager implementation
197 // assertEquals( 8, newStats.getTotalArtifactCount() );
198 // assertEquals( 3, newStats.getTotalGroupCount() );
199 // assertEquals( 5, newStats.getTotalProjectCount() );
200 // assertEquals( 19301, newStats.getTotalArtifactFileSize() );
204 public void testExecutorForceScanAll()
207 RepositoryTask repoTask = new RepositoryTask();
209 repoTask.setRepositoryId( TEST_REPO_ID );
210 repoTask.setScanAll( true );
212 Date date = Calendar.getInstance().getTime();
213 repositoryStatisticsManager.addStatisticsAfterScan( metadataRepository, TEST_REPO_ID, //
214 new Date( date.getTime() - 1234567 ), //
217 taskExecutor.executeTask( repoTask );
219 Collection<ArtifactReference> unprocessedResultList = testConsumer.getConsumed();
221 assertNotNull( unprocessedResultList );
222 assertEquals( "Incorrect number of unprocessed artifacts detected.", 8, unprocessedResultList.size() );
225 private void createAndSaveTestStats()
226 throws MetadataRepositoryException
228 Date date = Calendar.getInstance().getTime();
229 DefaultRepositoryStatistics stats = new DefaultRepositoryStatistics();
230 stats.setScanStartTime( new Date( date.getTime() - 1234567 ) );
231 stats.setScanEndTime( date );
232 stats.setNewFileCount( 31 );
233 stats.setTotalArtifactCount( 8 );
234 stats.setTotalFileCount( 31 );
235 stats.setTotalGroupCount( 3 );
236 stats.setTotalProjectCount( 5 );
237 stats.setTotalArtifactFileSize( 38545 );
239 repositoryStatisticsManager.addStatisticsAfterScan( metadataRepository, TEST_REPO_ID, //
240 new Date( date.getTime() - 1234567 ), //