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 24KB

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