You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ArchivaRepositoryScanningTaskExecutorPhase2Test.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package org.apache.archiva.scheduler.repository;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import org.apache.archiva.metadata.repository.MetadataRepositoryException;
  21. import org.apache.archiva.metadata.repository.stats.RepositoryStatistics;
  22. import org.apache.archiva.model.ArtifactReference;
  23. import org.apache.archiva.scheduler.repository.model.RepositoryTask;
  24. import org.codehaus.plexus.util.FileUtils;
  25. import org.junit.Test;
  26. import org.springframework.test.context.ContextConfiguration;
  27. import java.io.File;
  28. import java.util.Calendar;
  29. import java.util.Collection;
  30. import java.util.Date;
  31. /**
  32. * ArchivaRepositoryScanningTaskExecutorPhase2Test
  33. */
  34. @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context.xml" } )
  35. public class ArchivaRepositoryScanningTaskExecutorPhase2Test
  36. extends AbstractArchivaRepositoryScanningTaskExecutorTest
  37. {
  38. @Test
  39. public void testExecutorScanOnlyNewArtifacts()
  40. throws Exception
  41. {
  42. RepositoryTask repoTask = new RepositoryTask();
  43. repoTask.setRepositoryId( TEST_REPO_ID );
  44. repoTask.setScanAll( false );
  45. createAndSaveTestStats();
  46. taskExecutor.executeTask( repoTask );
  47. // check no artifacts processed
  48. Collection<ArtifactReference> unprocessedResultList = testConsumer.getConsumed();
  49. assertNotNull( unprocessedResultList );
  50. assertEquals( "Incorrect number of unprocessed artifacts detected. No new artifacts should have been found.", 0,
  51. unprocessedResultList.size() );
  52. // check correctness of new stats
  53. RepositoryStatistics newStats =
  54. repositoryStatisticsManager.getLastStatistics( metadataRepository, TEST_REPO_ID );
  55. assertEquals( 0, newStats.getNewFileCount() );
  56. assertEquals( 31, newStats.getTotalFileCount() );
  57. // FIXME: can't test these as they weren't stored in the database, move to tests for RepositoryStatisticsManager implementation
  58. // assertEquals( 8, newStats.getTotalArtifactCount() );
  59. // assertEquals( 3, newStats.getTotalGroupCount() );
  60. // assertEquals( 5, newStats.getTotalProjectCount() );
  61. // assertEquals( 14159, newStats.getTotalArtifactFileSize() );
  62. File newArtifactGroup = new File( repoDir, "org/apache/archiva" );
  63. assertFalse( "newArtifactGroup should not exist.", newArtifactGroup.exists() );
  64. FileUtils.copyDirectoryStructure( new File( "target/test-classes/test-repo/org/apache/archiva" ),
  65. newArtifactGroup );
  66. // update last modified date
  67. new File( newArtifactGroup, "archiva-index-methods-jar-test/1.0/pom.xml" ).setLastModified(
  68. Calendar.getInstance().getTimeInMillis() + 1000 );
  69. new File( newArtifactGroup,
  70. "archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" ).setLastModified(
  71. Calendar.getInstance().getTimeInMillis() + 1000 );
  72. assertTrue( newArtifactGroup.exists() );
  73. taskExecutor.executeTask( repoTask );
  74. unprocessedResultList = testConsumer.getConsumed();
  75. assertNotNull( unprocessedResultList );
  76. assertEquals( "Incorrect number of unprocessed artifacts detected. One new artifact should have been found.", 1,
  77. unprocessedResultList.size() );
  78. // check correctness of new stats
  79. RepositoryStatistics updatedStats =
  80. repositoryStatisticsManager.getLastStatistics( metadataRepository, TEST_REPO_ID );
  81. assertEquals( 2, updatedStats.getNewFileCount() );
  82. assertEquals( 33, updatedStats.getTotalFileCount() );
  83. // FIXME: can't test these as they weren't stored in the database, move to tests for RepositoryStatisticsManager implementation
  84. // assertEquals( 8, newStats.getTotalArtifactCount() );
  85. // assertEquals( 3, newStats.getTotalGroupCount() );
  86. // assertEquals( 5, newStats.getTotalProjectCount() );
  87. // assertEquals( 19301, updatedStats.getTotalArtifactFileSize() );
  88. }
  89. @Test
  90. public void testExecutorScanOnlyNewArtifactsChangeTimes()
  91. throws Exception
  92. {
  93. RepositoryTask repoTask = new RepositoryTask();
  94. repoTask.setRepositoryId( TEST_REPO_ID );
  95. repoTask.setScanAll( false );
  96. createAndSaveTestStats();
  97. File newArtifactGroup = new File( repoDir, "org/apache/archiva" );
  98. assertFalse( "newArtifactGroup should not exist.", newArtifactGroup.exists() );
  99. FileUtils.copyDirectoryStructure( new File( "target/test-classes/test-repo/org/apache/archiva" ),
  100. newArtifactGroup );
  101. // update last modified date, placing shortly after last scan
  102. new File( newArtifactGroup, "archiva-index-methods-jar-test/1.0/pom.xml" ).setLastModified(
  103. Calendar.getInstance().getTimeInMillis() + 1000 );
  104. new File( newArtifactGroup,
  105. "archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" ).setLastModified(
  106. Calendar.getInstance().getTimeInMillis() + 1000 );
  107. assertTrue( newArtifactGroup.exists() );
  108. // scan using the really long previous duration
  109. taskExecutor.executeTask( repoTask );
  110. // check no artifacts processed
  111. Collection<ArtifactReference> unprocessedResultList = testConsumer.getConsumed();
  112. assertNotNull( unprocessedResultList );
  113. assertEquals( "Incorrect number of unprocessed artifacts detected. One new artifact should have been found.", 1,
  114. unprocessedResultList.size() );
  115. // check correctness of new stats
  116. RepositoryStatistics newStats =
  117. repositoryStatisticsManager.getLastStatistics( metadataRepository, TEST_REPO_ID );
  118. assertEquals( 2, newStats.getNewFileCount() );
  119. assertEquals( 33, newStats.getTotalFileCount() );
  120. // FIXME: can't test these as they weren't stored in the database, move to tests for RepositoryStatisticsManager implementation
  121. // assertEquals( 8, newStats.getTotalArtifactCount() );
  122. // assertEquals( 3, newStats.getTotalGroupCount() );
  123. // assertEquals( 5, newStats.getTotalProjectCount() );
  124. // assertEquals( 19301, newStats.getTotalArtifactFileSize() );
  125. }
  126. @Test
  127. public void testExecutorScanOnlyNewArtifactsMidScan()
  128. throws Exception
  129. {
  130. RepositoryTask repoTask = new RepositoryTask();
  131. repoTask.setRepositoryId( TEST_REPO_ID );
  132. repoTask.setScanAll( false );
  133. createAndSaveTestStats();
  134. File newArtifactGroup = new File( repoDir, "org/apache/archiva" );
  135. assertFalse( "newArtifactGroup should not exist.", newArtifactGroup.exists() );
  136. FileUtils.copyDirectoryStructure( new File( "target/test-classes/test-repo/org/apache/archiva" ),
  137. newArtifactGroup );
  138. // update last modified date, placing in middle of last scan
  139. new File( newArtifactGroup, "archiva-index-methods-jar-test/1.0/pom.xml" ).setLastModified(
  140. Calendar.getInstance().getTimeInMillis() - 50000 );
  141. new File( newArtifactGroup,
  142. "archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" ).setLastModified(
  143. Calendar.getInstance().getTimeInMillis() - 50000 );
  144. assertTrue( newArtifactGroup.exists() );
  145. // scan using the really long previous duration
  146. taskExecutor.executeTask( repoTask );
  147. // check no artifacts processed
  148. Collection<ArtifactReference> unprocessedResultList = testConsumer.getConsumed();
  149. assertNotNull( unprocessedResultList );
  150. assertEquals( "Incorrect number of unprocessed artifacts detected. One new artifact should have been found.", 1,
  151. unprocessedResultList.size() );
  152. // check correctness of new stats
  153. RepositoryStatistics newStats =
  154. repositoryStatisticsManager.getLastStatistics( metadataRepository, TEST_REPO_ID );
  155. assertEquals( 2, newStats.getNewFileCount() );
  156. assertEquals( 33, newStats.getTotalFileCount() );
  157. // FIXME: can't test these as they weren't stored in the database, move to tests for RepositoryStatisticsManager implementation
  158. // assertEquals( 8, newStats.getTotalArtifactCount() );
  159. // assertEquals( 3, newStats.getTotalGroupCount() );
  160. // assertEquals( 5, newStats.getTotalProjectCount() );
  161. // assertEquals( 19301, newStats.getTotalArtifactFileSize() );
  162. }
  163. @Test
  164. public void testExecutorForceScanAll()
  165. throws Exception
  166. {
  167. RepositoryTask repoTask = new RepositoryTask();
  168. repoTask.setRepositoryId( TEST_REPO_ID );
  169. repoTask.setScanAll( true );
  170. Date date = Calendar.getInstance().getTime();
  171. repositoryStatisticsManager.addStatisticsAfterScan( metadataRepository, TEST_REPO_ID, //
  172. new Date( date.getTime() - 1234567 ), //
  173. date, 8, 8 ); //
  174. taskExecutor.executeTask( repoTask );
  175. Collection<ArtifactReference> unprocessedResultList = testConsumer.getConsumed();
  176. assertNotNull( unprocessedResultList );
  177. assertEquals( "Incorrect number of unprocessed artifacts detected.", 8, unprocessedResultList.size() );
  178. }
  179. private void createAndSaveTestStats()
  180. throws MetadataRepositoryException
  181. {
  182. Date date = Calendar.getInstance().getTime();
  183. RepositoryStatistics stats = new RepositoryStatistics();
  184. stats.setScanStartTime( new Date( date.getTime() - 1234567 ) );
  185. stats.setScanEndTime( date );
  186. stats.setNewFileCount( 31 );
  187. stats.setTotalArtifactCount( 8 );
  188. stats.setTotalFileCount( 31 );
  189. stats.setTotalGroupCount( 3 );
  190. stats.setTotalProjectCount( 5 );
  191. stats.setTotalArtifactFileSize( 38545 );
  192. repositoryStatisticsManager.addStatisticsAfterScan( metadataRepository, TEST_REPO_ID, //
  193. new Date( date.getTime() - 1234567 ), //
  194. date, 31, 31 );
  195. }
  196. }