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.model.ArtifactReference;
25 import org.codehaus.plexus.util.FileUtils;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.springframework.test.context.ContextConfiguration;
29 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
32 import java.util.Calendar;
33 import java.util.Collection;
34 import java.util.Date;
37 * ArchivaRepositoryScanningTaskExecutorPhase2Test
39 * @version $Id: ArchivaRepositoryScanningTaskExecutorTest.java 1214303 2011-12-14 15:37:51Z olamy $
41 @RunWith( SpringJUnit4ClassRunner.class )
42 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
43 public class ArchivaRepositoryScanningTaskExecutorPhase2Test
44 extends ArchivaRepositoryScanningTaskExecutorAbstractTest
48 public void testExecutorScanOnlyNewArtifacts()
51 RepositoryTask repoTask = new RepositoryTask();
53 repoTask.setRepositoryId( TEST_REPO_ID );
54 repoTask.setScanAll( false );
56 createAndSaveTestStats();
58 taskExecutor.executeTask( repoTask );
60 // check no artifacts processed
61 Collection<ArtifactReference> unprocessedResultList = testConsumer.getConsumed();
63 assertNotNull( unprocessedResultList );
64 assertEquals( "Incorrect number of unprocessed artifacts detected. No new artifacts should have been found.", 0,
65 unprocessedResultList.size() );
67 // check correctness of new stats
68 RepositoryStatistics newStats =
69 repositoryStatisticsManager.getLastStatistics( metadataRepository, TEST_REPO_ID );
70 assertEquals( 0, newStats.getNewFileCount() );
71 assertEquals( 31, newStats.getTotalFileCount() );
72 // FIXME: can't test these as they weren't stored in the database, move to tests for RepositoryStatisticsManager implementation
73 // assertEquals( 8, newStats.getTotalArtifactCount() );
74 // assertEquals( 3, newStats.getTotalGroupCount() );
75 // assertEquals( 5, newStats.getTotalProjectCount() );
76 // assertEquals( 14159, newStats.getTotalArtifactFileSize() );
78 File newArtifactGroup = new File( repoDir, "org/apache/archiva" );
79 assertFalse( "newArtifactGroup should not exist.", newArtifactGroup.exists() );
81 FileUtils.copyDirectoryStructure( new File( "target/test-classes/test-repo/org/apache/archiva" ),
84 // update last modified date
85 new File( newArtifactGroup, "archiva-index-methods-jar-test/1.0/pom.xml" ).setLastModified(
86 Calendar.getInstance().getTimeInMillis() + 1000 );
87 new File( newArtifactGroup,
88 "archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" ).setLastModified(
89 Calendar.getInstance().getTimeInMillis() + 1000 );
91 assertTrue( newArtifactGroup.exists() );
93 taskExecutor.executeTask( repoTask );
95 unprocessedResultList = testConsumer.getConsumed();
96 assertNotNull( unprocessedResultList );
97 assertEquals( "Incorrect number of unprocessed artifacts detected. One new artifact should have been found.", 1,
98 unprocessedResultList.size() );
100 // check correctness of new stats
101 RepositoryStatistics updatedStats =
102 repositoryStatisticsManager.getLastStatistics( metadataRepository, TEST_REPO_ID );
103 assertEquals( 2, updatedStats.getNewFileCount() );
104 assertEquals( 33, updatedStats.getTotalFileCount() );
105 // FIXME: can't test these as they weren't stored in the database, move to tests for RepositoryStatisticsManager implementation
106 // assertEquals( 8, newStats.getTotalArtifactCount() );
107 // assertEquals( 3, newStats.getTotalGroupCount() );
108 // assertEquals( 5, newStats.getTotalProjectCount() );
109 // assertEquals( 19301, updatedStats.getTotalArtifactFileSize() );
113 public void testExecutorScanOnlyNewArtifactsChangeTimes()
116 RepositoryTask repoTask = new RepositoryTask();
118 repoTask.setRepositoryId( TEST_REPO_ID );
119 repoTask.setScanAll( false );
121 createAndSaveTestStats();
123 File newArtifactGroup = new File( repoDir, "org/apache/archiva" );
124 assertFalse( "newArtifactGroup should not exist.", newArtifactGroup.exists() );
126 FileUtils.copyDirectoryStructure( new File( "target/test-classes/test-repo/org/apache/archiva" ),
129 // update last modified date, placing shortly after last scan
130 new File( newArtifactGroup, "archiva-index-methods-jar-test/1.0/pom.xml" ).setLastModified(
131 Calendar.getInstance().getTimeInMillis() + 1000 );
132 new File( newArtifactGroup,
133 "archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" ).setLastModified(
134 Calendar.getInstance().getTimeInMillis() + 1000 );
136 assertTrue( newArtifactGroup.exists() );
138 // scan using the really long previous duration
139 taskExecutor.executeTask( repoTask );
141 // check no artifacts processed
142 Collection<ArtifactReference> unprocessedResultList = testConsumer.getConsumed();
143 assertNotNull( unprocessedResultList );
144 assertEquals( "Incorrect number of unprocessed artifacts detected. One new artifact should have been found.", 1,
145 unprocessedResultList.size() );
147 // check correctness of new stats
148 RepositoryStatistics newStats =
149 repositoryStatisticsManager.getLastStatistics( metadataRepository, TEST_REPO_ID );
150 assertEquals( 2, newStats.getNewFileCount() );
151 assertEquals( 33, newStats.getTotalFileCount() );
152 // FIXME: can't test these as they weren't stored in the database, move to tests for RepositoryStatisticsManager implementation
153 // assertEquals( 8, newStats.getTotalArtifactCount() );
154 // assertEquals( 3, newStats.getTotalGroupCount() );
155 // assertEquals( 5, newStats.getTotalProjectCount() );
156 // assertEquals( 19301, newStats.getTotalArtifactFileSize() );
160 public void testExecutorScanOnlyNewArtifactsMidScan()
163 RepositoryTask repoTask = new RepositoryTask();
165 repoTask.setRepositoryId( TEST_REPO_ID );
166 repoTask.setScanAll( false );
168 createAndSaveTestStats();
170 File newArtifactGroup = new File( repoDir, "org/apache/archiva" );
171 assertFalse( "newArtifactGroup should not exist.", newArtifactGroup.exists() );
173 FileUtils.copyDirectoryStructure( new File( "target/test-classes/test-repo/org/apache/archiva" ),
176 // update last modified date, placing in middle of last scan
177 new File( newArtifactGroup, "archiva-index-methods-jar-test/1.0/pom.xml" ).setLastModified(
178 Calendar.getInstance().getTimeInMillis() - 50000 );
179 new File( newArtifactGroup,
180 "archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" ).setLastModified(
181 Calendar.getInstance().getTimeInMillis() - 50000 );
183 assertTrue( newArtifactGroup.exists() );
185 // scan using the really long previous duration
186 taskExecutor.executeTask( repoTask );
188 // check no artifacts processed
189 Collection<ArtifactReference> unprocessedResultList = testConsumer.getConsumed();
190 assertNotNull( unprocessedResultList );
191 assertEquals( "Incorrect number of unprocessed artifacts detected. One new artifact should have been found.", 1,
192 unprocessedResultList.size() );
194 // check correctness of new stats
195 RepositoryStatistics newStats =
196 repositoryStatisticsManager.getLastStatistics( metadataRepository, TEST_REPO_ID );
197 assertEquals( 2, newStats.getNewFileCount() );
198 assertEquals( 33, newStats.getTotalFileCount() );
199 // FIXME: can't test these as they weren't stored in the database, move to tests for RepositoryStatisticsManager implementation
200 // assertEquals( 8, newStats.getTotalArtifactCount() );
201 // assertEquals( 3, newStats.getTotalGroupCount() );
202 // assertEquals( 5, newStats.getTotalProjectCount() );
203 // assertEquals( 19301, newStats.getTotalArtifactFileSize() );
207 public void testExecutorForceScanAll()
210 RepositoryTask repoTask = new RepositoryTask();
212 repoTask.setRepositoryId( TEST_REPO_ID );
213 repoTask.setScanAll( true );
215 Date date = Calendar.getInstance().getTime();
216 repositoryStatisticsManager.addStatisticsAfterScan( metadataRepository, TEST_REPO_ID,
217 new Date( date.getTime() - 1234567 ), date, 8, 8 );
219 taskExecutor.executeTask( repoTask );
221 Collection<ArtifactReference> unprocessedResultList = testConsumer.getConsumed();
223 assertNotNull( unprocessedResultList );
224 assertEquals( "Incorrect number of unprocessed artifacts detected.", 8, unprocessedResultList.size() );
227 private void createAndSaveTestStats()
228 throws MetadataRepositoryException
230 Date date = Calendar.getInstance().getTime();
231 RepositoryStatistics stats = new RepositoryStatistics();
232 stats.setScanStartTime( new Date( date.getTime() - 1234567 ) );
233 stats.setScanEndTime( date );
234 stats.setNewFileCount( 31 );
235 stats.setTotalArtifactCount( 8 );
236 stats.setTotalFileCount( 31 );
237 stats.setTotalGroupCount( 3 );
238 stats.setTotalProjectCount( 5 );
239 stats.setTotalArtifactFileSize( 38545 );
241 repositoryStatisticsManager.addStatisticsAfterScan( metadataRepository, TEST_REPO_ID,
242 new Date( date.getTime() - 1234567 ), date, 31, 31 );