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

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