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.

RepositoryPurgeConsumerTest.java 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. package org.apache.archiva.consumers.core.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.common.utils.BaseFile;
  21. import org.apache.archiva.configuration.ArchivaConfiguration;
  22. import org.apache.archiva.configuration.FileType;
  23. import org.apache.archiva.configuration.FileTypes;
  24. import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
  25. import org.apache.archiva.consumers.functors.ConsumerWantsFilePredicate;
  26. import org.apache.archiva.metadata.model.ArtifactMetadata;
  27. import org.apache.archiva.metadata.model.MetadataFacet;
  28. import org.apache.archiva.repository.base.ArchivaRepositoryRegistry;
  29. import org.apache.archiva.repository.RepositoryRegistry;
  30. import org.apache.archiva.repository.features.ArtifactCleanupFeature;
  31. import org.custommonkey.xmlunit.XMLAssert;
  32. import org.easymock.EasyMock;
  33. import org.junit.After;
  34. import org.junit.Before;
  35. import org.junit.Test;
  36. import org.mockito.ArgumentCaptor;
  37. import org.slf4j.Logger;
  38. import org.slf4j.LoggerFactory;
  39. import org.springframework.test.context.ContextConfiguration;
  40. import java.io.IOException;
  41. import java.nio.charset.Charset;
  42. import java.nio.file.Files;
  43. import java.nio.file.Path;
  44. import java.nio.file.Paths;
  45. import java.nio.file.attribute.FileTime;
  46. import java.time.Period;
  47. import java.util.HashSet;
  48. import java.util.List;
  49. import java.util.Set;
  50. import static org.junit.Assert.*;
  51. import static org.mockito.Matchers.any;
  52. import static org.mockito.Matchers.eq;
  53. import static org.mockito.Mockito.*;
  54. /**
  55. */
  56. @ContextConfiguration (
  57. locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context-purge-consumer-test.xml" } )
  58. public class RepositoryPurgeConsumerTest
  59. extends AbstractRepositoryPurgeTest
  60. {
  61. private static final Logger log = LoggerFactory.getLogger( RepositoryPurgeConsumerTest.class );
  62. @Before
  63. @Override
  64. public void setUp()
  65. throws Exception
  66. {
  67. super.setUp();
  68. }
  69. @After
  70. @Override
  71. public void tearDown()
  72. throws Exception
  73. {
  74. super.tearDown();
  75. }
  76. @Test
  77. public void testConsumption()
  78. throws Exception
  79. {
  80. assertNotConsumed( "org/apache/maven/plugins/maven-plugin-plugin/2.4.1/maven-metadata.xml" );
  81. cleanupFileTypes();
  82. }
  83. @Test
  84. public void testConsumptionOfOtherMetadata()
  85. throws Exception
  86. {
  87. assertNotConsumed( "org/apache/maven/plugins/maven-plugin-plugin/2.4.1/maven-metadata-central.xml" );
  88. cleanupFileTypes();
  89. }
  90. private void cleanupFileTypes()
  91. {
  92. ArchivaConfiguration archivaConfiguration =
  93. applicationContext.getBean( "archivaConfiguration#default", ArchivaConfiguration.class );
  94. FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
  95. fileType.removePattern( "**/*.xml" );
  96. }
  97. @SuppressWarnings( "deprecation" )
  98. private void assertNotConsumed( String path )
  99. throws Exception
  100. {
  101. ArchivaConfiguration archivaConfiguration =
  102. applicationContext.getBean( "archivaConfiguration#default", ArchivaConfiguration.class );
  103. FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
  104. assertEquals( FileTypes.ARTIFACTS, fileType.getId() );
  105. fileType.addPattern( "**/*.xml" );
  106. // trigger reload
  107. //FileTypes fileTypes = applicationContext.getBean( FileTypes.class );
  108. for ( FileTypes fileTypes : applicationContext.getBeansOfType( FileTypes.class ).values() )
  109. {
  110. fileTypes.afterConfigurationChange( null, "repositoryScanning.fileTypes", null );
  111. }
  112. KnownRepositoryContentConsumer repoPurgeConsumer =
  113. applicationContext.getBean( "knownRepositoryContentConsumer#repository-purge",
  114. KnownRepositoryContentConsumer.class );
  115. Path repoLocation = Paths.get( "target/test-" + getName() + "/test-repo" );
  116. Path localFile = repoLocation.resolve( path );
  117. ConsumerWantsFilePredicate predicate = new ConsumerWantsFilePredicate();
  118. BaseFile baseFile = new BaseFile( repoLocation.toFile(), localFile.toFile() );
  119. predicate.setBasefile( baseFile );
  120. assertFalse( predicate.evaluate( repoPurgeConsumer ) );
  121. }
  122. private void setLastModified( String path ) throws IOException
  123. {
  124. Path dir = Paths.get( path );
  125. Path[] contents = new Path[0];
  126. try
  127. {
  128. contents = Files.list( dir ).toArray(Path[]::new);
  129. }
  130. catch ( IOException e )
  131. {
  132. log.error("Could not list files {}: {}", dir, e.getMessage(), e);
  133. contents = new Path[0];
  134. }
  135. for ( int i = 0; i < contents.length; i++ )
  136. {
  137. Files.setLastModifiedTime( contents[i], FileTime.fromMillis( 1179382029 ) );
  138. }
  139. }
  140. @Test
  141. public void testConsumerByRetentionCount()
  142. throws Exception
  143. {
  144. RepositoryPurgeConsumer repoPurgeConsumer =
  145. applicationContext.getBean( "knownRepositoryContentConsumer#repo-purge-consumer-by-retention-count",
  146. RepositoryPurgeConsumer.class );
  147. repoPurgeConsumer.setRepositorySessionFactory( sessionFactory );
  148. org.apache.archiva.repository.ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
  149. ArtifactCleanupFeature atf = repoConfiguration.getFeature( ArtifactCleanupFeature.class ).get();
  150. atf.setRetentionPeriod( Period.ofDays( 0 ) ); // force days older off to allow retention count purge to execute.
  151. atf.setRetentionCount( TEST_RETENTION_COUNT );
  152. addRepoToConfiguration( "retention-count", repoConfiguration );
  153. sessionControl.reset();
  154. sessionFactoryControl.reset();
  155. EasyMock.expect( sessionFactory.createSession( ) ).andStubReturn( repositorySession );
  156. EasyMock.expect( repositorySession.getRepository()).andStubReturn( metadataRepository );
  157. repositorySession.save();
  158. EasyMock.expectLastCall().anyTimes();
  159. sessionFactoryControl.replay();
  160. sessionControl.replay();
  161. repoPurgeConsumer.beginScan( repoConfiguration, null );
  162. String repoRoot = prepareTestRepos();
  163. String projectNs = "org.jruby.plugins";
  164. String projectPath = projectNs.replaceAll("\\.","/");
  165. String projectName = "jruby-rake-plugin";
  166. String projectVersion = "1.0RC1-SNAPSHOT";
  167. String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
  168. String versionRoot = projectRoot + "/" + projectVersion;
  169. Path repo = getTestRepoRootPath();
  170. Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
  171. // Provide the metadata list
  172. List<ArtifactMetadata> ml = getArtifactMetadataFromDir( TEST_REPO_ID, projectName, repo, vDir );
  173. when(metadataRepository.getArtifacts( repositorySession, TEST_REPO_ID,
  174. projectNs, projectName, projectVersion )).thenReturn(ml);
  175. Set<String> deletedVersions = new HashSet<>();
  176. deletedVersions.add("1.0RC1-20070504.153317-1");
  177. deletedVersions.add("1.0RC1-20070504.160758-2");
  178. repoPurgeConsumer.processFile( PATH_TO_BY_RETENTION_COUNT_ARTIFACT );
  179. // Verify the metadataRepository invocations
  180. verify(metadataRepository, never()).removeProjectVersion( eq(repositorySession), eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) );
  181. ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class);
  182. verify(metadataRepository, times(2)).removeTimestampedArtifact( eq(repositorySession), metadataArg.capture(), eq(projectVersion) );
  183. List<ArtifactMetadata> metaL = metadataArg.getAllValues();
  184. for (ArtifactMetadata meta : metaL) {
  185. assertTrue(meta.getId().startsWith(projectName));
  186. assertTrue(deletedVersions.contains(meta.getVersion()));
  187. }
  188. // assert if removed from repo
  189. assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar" );
  190. assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1-javadoc.jar" );
  191. assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1-javadoc.zip" );
  192. assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar.md5" );
  193. assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar.sha1" );
  194. assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom" );
  195. assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom.md5" );
  196. assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom.sha1" );
  197. assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar" );
  198. assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2-javadoc.jar" );
  199. assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2-javadoc.zip" );
  200. assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar.md5" );
  201. assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar.sha1" );
  202. assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom" );
  203. assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom.md5" );
  204. assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom.sha1" );
  205. // assert if not removed from repo
  206. assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar" );
  207. assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3-javadoc.jar" );
  208. assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3-javadoc.zip" );
  209. assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar.md5" );
  210. assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar.sha1" );
  211. assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom" );
  212. assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom.md5" );
  213. assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom.sha1" );
  214. assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar" );
  215. assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar.md5" );
  216. assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar.sha1" );
  217. assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom" );
  218. assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom.md5" );
  219. assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom.sha1" );
  220. removeRepoFromConfiguration( "retention-count", repoConfiguration );
  221. }
  222. private void addRepoToConfiguration( String configHint, org.apache.archiva.repository.ManagedRepository repoConfiguration )
  223. throws Exception
  224. {
  225. RepositoryRegistry repositoryRegistry = applicationContext.getBean( ArchivaRepositoryRegistry.class);
  226. repositoryRegistry.putRepository( repoConfiguration );
  227. }
  228. private void removeRepoFromConfiguration( String configHint, org.apache.archiva.repository.ManagedRepository repoConfiguration )
  229. throws Exception
  230. {
  231. RepositoryRegistry repositoryRegistry = applicationContext.getBean( ArchivaRepositoryRegistry.class);
  232. repositoryRegistry.removeRepository( repoConfiguration );
  233. }
  234. @Test
  235. public void testConsumerByDaysOld()
  236. throws Exception
  237. {
  238. RepositoryPurgeConsumer repoPurgeConsumer =
  239. applicationContext.getBean( "knownRepositoryContentConsumer#repo-purge-consumer-by-days-old",
  240. RepositoryPurgeConsumer.class );
  241. repoPurgeConsumer.setRepositorySessionFactory( sessionFactory );
  242. org.apache.archiva.repository.ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
  243. ArtifactCleanupFeature atf = repoConfiguration.getFeature( ArtifactCleanupFeature.class ).get();
  244. atf.setRetentionPeriod( Period.ofDays( TEST_DAYS_OLDER ) );
  245. addRepoToConfiguration( "days-old", repoConfiguration );
  246. sessionControl.reset();
  247. sessionFactoryControl.reset();
  248. EasyMock.expect( sessionFactory.createSession( ) ).andStubReturn( repositorySession );
  249. EasyMock.expect( repositorySession.getRepository()).andStubReturn( metadataRepository );
  250. repositorySession.save();
  251. EasyMock.expectLastCall().anyTimes();
  252. sessionFactoryControl.replay();
  253. sessionControl.replay();
  254. repoPurgeConsumer.beginScan( repoConfiguration, null );
  255. String repoRoot = prepareTestRepos();
  256. String projectNs = "org.apache.maven.plugins";
  257. String projectPath = projectNs.replaceAll("\\.","/");
  258. String projectName = "maven-install-plugin";
  259. String projectVersion = "2.2-SNAPSHOT";
  260. String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
  261. setLastModified( projectRoot + "/"+projectVersion);
  262. Path repo = getTestRepoRootPath();
  263. Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
  264. // Provide the metadata list
  265. List<ArtifactMetadata> ml = getArtifactMetadataFromDir( TEST_REPO_ID, projectName, repo, vDir );
  266. when(metadataRepository.getArtifacts( repositorySession, TEST_REPO_ID,
  267. projectNs, projectName, projectVersion )).thenReturn(ml);
  268. Set<String> deletedVersions = new HashSet<>();
  269. deletedVersions.add("2.2-SNAPSHOT");
  270. deletedVersions.add("2.2-20061118.060401-2");
  271. repoPurgeConsumer.processFile( PATH_TO_BY_DAYS_OLD_ARTIFACT );
  272. // Verify the metadataRepository invocations
  273. verify(metadataRepository, never()).removeProjectVersion( eq(repositorySession), eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) );
  274. ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class);
  275. verify(metadataRepository, times(2)).removeTimestampedArtifact( eq(repositorySession), metadataArg.capture(), eq(projectVersion) );
  276. List<ArtifactMetadata> metaL = metadataArg.getAllValues();
  277. assertTrue( metaL.size( ) > 0 );
  278. for (ArtifactMetadata meta : metaL) {
  279. assertTrue(meta.getId().startsWith(projectName));
  280. assertTrue(deletedVersions.contains(meta.getVersion()));
  281. }
  282. assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar" );
  283. assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.md5" );
  284. assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.sha1" );
  285. assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom" );
  286. assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.md5" );
  287. assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.sha1" );
  288. // shouldn't be deleted because even if older than 30 days (because retention count = 2)
  289. assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar" );
  290. assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar.md5" );
  291. assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar.sha1" );
  292. assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom" );
  293. assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom.md5" );
  294. assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom.sha1" );
  295. assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar" );
  296. assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar.md5" );
  297. assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar.sha1" );
  298. assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom" );
  299. assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom.md5" );
  300. assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom.sha1" );
  301. assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar" );
  302. assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.md5" );
  303. assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.sha1" );
  304. assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom" );
  305. assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.md5" );
  306. assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.sha1" );
  307. removeRepoFromConfiguration( "days-old", repoConfiguration );
  308. }
  309. /**
  310. * Test the snapshot clean consumer on a repository set to NOT clean/delete snapshots based on released versions.
  311. *
  312. * @throws Exception
  313. */
  314. @Test
  315. public void testReleasedSnapshotsWereNotCleaned()
  316. throws Exception
  317. {
  318. KnownRepositoryContentConsumer repoPurgeConsumer =
  319. applicationContext.getBean( "knownRepositoryContentConsumer#repo-purge-consumer-by-retention-count",
  320. KnownRepositoryContentConsumer.class );
  321. org.apache.archiva.repository.ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
  322. ArtifactCleanupFeature acf = repoConfiguration.getFeature( ArtifactCleanupFeature.class ).get();
  323. acf.setDeleteReleasedSnapshots( false ); // Set to NOT delete released snapshots.
  324. addRepoToConfiguration( "retention-count", repoConfiguration );
  325. repoPurgeConsumer.beginScan( repoConfiguration, null );
  326. String repoRoot = prepareTestRepos();
  327. String projectNs = "org.apache.maven.plugins";
  328. String projectPath = projectNs.replaceAll("\\.","/");
  329. String projectName = "maven-plugin-plugin";
  330. String projectVersion = "2.3-SNAPSHOT";
  331. String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
  332. Path repo = getTestRepoRootPath();
  333. Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
  334. // Provide the metadata list
  335. List<ArtifactMetadata> ml = getArtifactMetadataFromDir( TEST_REPO_ID, projectName, repo, vDir );
  336. when(metadataRepository.getArtifacts( repositorySession, TEST_REPO_ID,
  337. projectNs, projectName, projectVersion )).thenReturn(ml);
  338. repoPurgeConsumer.processFile(
  339. CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO );
  340. verify(metadataRepository, never()).removeProjectVersion( eq(repositorySession), eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) );
  341. ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class);
  342. verify(metadataRepository, never()).removeTimestampedArtifact( eq(repositorySession), any(), any() );
  343. verify(metadataRepository, never()).removeFacetFromArtifact( eq(repositorySession), any(), any(), any(), any(), any(MetadataFacet.class) );
  344. // check if the snapshot wasn't removed
  345. assertExists( projectRoot + "/2.3-SNAPSHOT" );
  346. assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
  347. assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
  348. assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
  349. assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
  350. assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
  351. assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
  352. // check if metadata file wasn't updated
  353. Path artifactMetadataFile = Paths.get( projectRoot + "/maven-metadata.xml" );
  354. String metadataXml = org.apache.archiva.common.utils.FileUtils.readFileToString( artifactMetadataFile, Charset.defaultCharset() );
  355. String expectedVersions = "<expected><versions><version>2.3-SNAPSHOT</version></versions></expected>";
  356. XMLAssert.assertXpathEvaluatesTo( "2.3-SNAPSHOT", "//metadata/versioning/latest", metadataXml );
  357. XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
  358. "//metadata/versioning/versions/version", metadataXml );
  359. XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
  360. removeRepoFromConfiguration( "retention-count", repoConfiguration );
  361. }
  362. @Test
  363. public void testReleasedSnapshotsWereCleaned()
  364. throws Exception
  365. {
  366. RepositoryPurgeConsumer repoPurgeConsumer =
  367. applicationContext.getBean( "knownRepositoryContentConsumer#repo-purge-consumer-by-days-old",
  368. RepositoryPurgeConsumer.class );
  369. repoPurgeConsumer.setRepositorySessionFactory( sessionFactory );
  370. org.apache.archiva.repository.ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
  371. ArtifactCleanupFeature acf = repoConfiguration.getFeature( ArtifactCleanupFeature.class ).get();
  372. acf.setDeleteReleasedSnapshots( true );
  373. addRepoToConfiguration( "days-old", repoConfiguration );
  374. sessionControl.reset();
  375. sessionFactoryControl.reset();
  376. EasyMock.expect( sessionFactory.createSession( ) ).andStubReturn( repositorySession );
  377. EasyMock.expect( repositorySession.getRepository()).andStubReturn( metadataRepository );
  378. repositorySession.save();
  379. EasyMock.expectLastCall().anyTimes();
  380. sessionFactoryControl.replay();
  381. sessionControl.replay();
  382. repoPurgeConsumer.beginScan( repoConfiguration, null );
  383. String repoRoot = prepareTestRepos();
  384. String projectNs = "org.apache.maven.plugins";
  385. String projectPath = projectNs.replaceAll("\\.","/");
  386. String projectName = "maven-plugin-plugin";
  387. String projectVersion = "2.3-SNAPSHOT";
  388. String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
  389. Path repo = getTestRepoRootPath();
  390. Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
  391. // Provide the metadata list
  392. List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir );
  393. when(metadataRepository.getArtifacts( repositorySession, TEST_REPO_ID,
  394. projectNs, projectName, projectVersion )).thenReturn(ml);
  395. repoPurgeConsumer.processFile(
  396. CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO );
  397. verify(metadataRepository, times(1)).removeProjectVersion( eq(repositorySession), eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) );
  398. ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class);
  399. verify(metadataRepository, never()).removeTimestampedArtifact( eq(repositorySession), any(), any() );
  400. // check if the snapshot was removed
  401. assertDeleted( projectRoot + "/2.3-SNAPSHOT" );
  402. assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
  403. assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
  404. assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
  405. assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
  406. assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
  407. assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
  408. // check if metadata file was updated
  409. Path artifactMetadataFile = Paths.get( projectRoot + "/maven-metadata.xml" );
  410. String metadataXml = org.apache.archiva.common.utils.FileUtils.readFileToString( artifactMetadataFile, Charset.defaultCharset() );
  411. String expectedVersions =
  412. "<expected><versions><version>2.2</version>" + "<version>2.3</version></versions></expected>";
  413. XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/latest", metadataXml );
  414. XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
  415. "//metadata/versioning/versions/version", metadataXml );
  416. XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
  417. removeRepoFromConfiguration( "days-old", repoConfiguration );
  418. }
  419. }