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.

DaysOldRepositoryPurgeTest.java 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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.metadata.model.ArtifactMetadata;
  21. import org.apache.archiva.repository.events.RepositoryListener;
  22. import org.apache.archiva.repository.features.ArtifactCleanupFeature;
  23. import org.apache.commons.lang.time.DateUtils;
  24. import org.junit.After;
  25. import org.junit.Test;
  26. import org.mockito.ArgumentCaptor;
  27. import java.io.IOException;
  28. import java.nio.file.Files;
  29. import java.nio.file.Path;
  30. import java.nio.file.Paths;
  31. import java.nio.file.attribute.FileTime;
  32. import java.text.SimpleDateFormat;
  33. import java.util.Calendar;
  34. import java.util.Collections;
  35. import java.util.HashSet;
  36. import java.util.List;
  37. import java.util.Set;
  38. import static org.junit.Assert.assertTrue;
  39. import static org.mockito.Matchers.eq;
  40. import static org.mockito.Mockito.*;
  41. /**
  42. */
  43. public class DaysOldRepositoryPurgeTest
  44. extends AbstractRepositoryPurgeTest
  45. {
  46. private static final int OLD_TIMESTAMP = 1179382029;
  47. private void setLastModified( String dirPath, long lastModified ) throws IOException
  48. {
  49. Path dir = Paths.get( dirPath );
  50. Path[] contents = Files.list( dir ).toArray(Path[]::new );
  51. for ( Path content : contents )
  52. {
  53. Files.setLastModifiedTime( content, FileTime.fromMillis( lastModified ));
  54. }
  55. }
  56. @After
  57. @Override
  58. public void tearDown()
  59. throws Exception
  60. {
  61. super.tearDown();
  62. repoPurge = null;
  63. }
  64. @Test
  65. public void testByLastModified()
  66. throws Exception
  67. {
  68. org.apache.archiva.repository.ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
  69. ArtifactCleanupFeature atf = repoConfiguration.getFeature( ArtifactCleanupFeature.class ).get();
  70. repoPurge = new DaysOldRepositoryPurge( getRepository(), atf.getRetentionPeriod().getDays(),
  71. atf.getRetentionCount(), repositorySession,
  72. Collections.singletonList( listener ) );
  73. String repoRoot = prepareTestRepos();
  74. String projectNs = "org.apache.maven.plugins";
  75. String projectPath = projectNs.replaceAll("\\.","/");
  76. String projectName = "maven-install-plugin";
  77. String projectVersion = "2.2-SNAPSHOT";
  78. String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
  79. Path repo = getTestRepoRootPath();
  80. Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
  81. Set<String> deletedVersions = new HashSet<>();
  82. deletedVersions.add("2.2-SNAPSHOT");
  83. deletedVersions.add("2.2-20061118.060401-2");
  84. setLastModified( projectRoot + "/" + projectVersion + "/", OLD_TIMESTAMP );
  85. // test listeners for the correct artifacts
  86. String[] exts = {".md5",".sha1",""};
  87. for (int i=0; i<exts.length; i++) {
  88. listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
  89. "maven-install-plugin", "2.2-SNAPSHOT", "maven-install-plugin-2.2-SNAPSHOT.jar"+exts[i]);
  90. listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
  91. "maven-install-plugin", "2.2-SNAPSHOT", "maven-install-plugin-2.2-SNAPSHOT.pom"+exts[i]);
  92. listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
  93. "maven-install-plugin", "2.2-20061118.060401-2",
  94. "maven-install-plugin-2.2-20061118.060401-2.jar"+exts[i]);
  95. listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
  96. "maven-install-plugin", "2.2-20061118.060401-2",
  97. "maven-install-plugin-2.2-20061118.060401-2.pom"+exts[i]);
  98. }
  99. listenerControl.replay();
  100. // Provide the metadata list
  101. List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir );
  102. when(metadataRepository.getArtifacts(TEST_REPO_ID, projectNs,
  103. projectName, projectVersion)).thenReturn(ml);
  104. repoPurge.process( PATH_TO_BY_DAYS_OLD_ARTIFACT );
  105. listenerControl.verify();
  106. // Verify the metadataRepository invocations
  107. verify(metadataRepository, never()).removeProjectVersion(eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion));
  108. ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class);
  109. verify(metadataRepository, times(2)).removeArtifact(metadataArg.capture(), eq(projectVersion));
  110. List<ArtifactMetadata> metaL = metadataArg.getAllValues();
  111. for (ArtifactMetadata meta : metaL) {
  112. assertTrue(meta.getId().startsWith(projectName));
  113. assertTrue(deletedVersions.contains(meta.getVersion()));
  114. }
  115. assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar" );
  116. assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.md5" );
  117. assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.sha1" );
  118. assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom" );
  119. assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.md5" );
  120. assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.sha1" );
  121. // shouldn't be deleted because even if older than 30 days (because retention count = 2)
  122. assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar" );
  123. assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar.md5" );
  124. assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar.sha1" );
  125. assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom" );
  126. assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom.md5" );
  127. assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom.sha1" );
  128. assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar" );
  129. assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar.md5" );
  130. assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar.sha1" );
  131. assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom" );
  132. assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom.md5" );
  133. assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom.sha1" );
  134. assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar" );
  135. assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.md5" );
  136. assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.sha1" );
  137. assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom" );
  138. assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.md5" );
  139. assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.sha1" );
  140. }
  141. @Test
  142. public void testOrderOfDeletion()
  143. throws Exception
  144. {
  145. org.apache.archiva.repository.ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
  146. ArtifactCleanupFeature atf = repoConfiguration.getFeature( ArtifactCleanupFeature.class ).get();
  147. List<RepositoryListener> listeners = Collections.singletonList( listener );
  148. repoPurge = new DaysOldRepositoryPurge( getRepository(), atf.getRetentionPeriod().getDays(),
  149. atf.getRetentionCount(), repositorySession, listeners );
  150. String repoRoot = prepareTestRepos();
  151. String projectNs = "org.apache.maven.plugins";
  152. String projectPath = projectNs.replaceAll("\\.","/");
  153. String projectName = "maven-assembly-plugin";
  154. String projectVersion = "1.1.2-SNAPSHOT";
  155. String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
  156. Path repo = getTestRepoRootPath();
  157. Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
  158. Set<String> deletedVersions = new HashSet<>();
  159. deletedVersions.add("1.1.2-20070427.065136-1");
  160. setLastModified( projectRoot + "/" + projectVersion + "/", OLD_TIMESTAMP );
  161. // test listeners for the correct artifacts
  162. String[] exts = {".md5",".sha1",""};
  163. for (int i=0; i<exts.length; i++) {
  164. listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
  165. "maven-assembly-plugin", "1.1.2-20070427.065136-1",
  166. "maven-assembly-plugin-1.1.2-20070427.065136-1.jar"+exts[i]);
  167. listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
  168. "maven-assembly-plugin", "1.1.2-20070427.065136-1",
  169. "maven-assembly-plugin-1.1.2-20070427.065136-1.pom"+exts[i]);
  170. }
  171. listenerControl.replay();
  172. // Provide the metadata list
  173. List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir );
  174. when(metadataRepository.getArtifacts(TEST_REPO_ID, projectNs,
  175. projectName, projectVersion)).thenReturn(ml);
  176. repoPurge.process( PATH_TO_TEST_ORDER_OF_DELETION );
  177. listenerControl.verify();
  178. // Verify the metadataRepository invocations
  179. verify(metadataRepository, never()).removeProjectVersion(eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion));
  180. ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class);
  181. verify(metadataRepository, times(deletedVersions.size())).removeArtifact(metadataArg.capture(), eq(projectVersion));
  182. List<ArtifactMetadata> metaL = metadataArg.getAllValues();
  183. for (ArtifactMetadata meta : metaL) {
  184. assertTrue(meta.getId().startsWith(projectName));
  185. assertTrue(deletedVersions.contains(meta.getVersion()));
  186. }
  187. assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.jar" );
  188. assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.jar.sha1" );
  189. assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.jar.md5" );
  190. assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.pom" );
  191. assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.pom.sha1" );
  192. assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.pom.md5" );
  193. // the following should not have been deleted
  194. assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.jar" );
  195. assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.jar.sha1" );
  196. assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.jar.md5" );
  197. assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.pom" );
  198. assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.pom.sha1" );
  199. assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.pom.md5" );
  200. assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.jar" );
  201. assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.jar.sha1" );
  202. assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.jar.md5" );
  203. assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.pom" );
  204. assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.pom.sha1" );
  205. assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.pom.md5" );
  206. }
  207. @Test
  208. public void testMetadataDrivenSnapshots()
  209. throws Exception
  210. {
  211. org.apache.archiva.repository.ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
  212. ArtifactCleanupFeature atf = repoConfiguration.getFeature( ArtifactCleanupFeature.class ).get();
  213. List<RepositoryListener> listeners = Collections.singletonList( listener );
  214. repoPurge = new DaysOldRepositoryPurge( getRepository(), atf.getRetentionPeriod().getDays(),
  215. atf.getRetentionCount(), repositorySession, listeners );
  216. String repoRoot = prepareTestRepos();
  217. String projectNs = "org.codehaus.plexus";
  218. String projectPath = projectNs.replaceAll("\\.","/");
  219. String projectName = "plexus-utils";
  220. String projectVersion = "1.4.3-SNAPSHOT";
  221. String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
  222. Path repo = getTestRepoRootPath();
  223. Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
  224. Set<String> deletedVersions = new HashSet<>();
  225. deletedVersions.add("1.4.3-20070113.163208-4");
  226. String versionRoot = projectRoot + "/"+ projectVersion;
  227. Calendar currentDate = Calendar.getInstance( DateUtils.UTC_TIME_ZONE );
  228. setLastModified( versionRoot, currentDate.getTimeInMillis() );
  229. String timestamp = new SimpleDateFormat( "yyyyMMdd.HHmmss" ).format( currentDate.getTime() );
  230. for ( int i = 5; i <= 7; i++ )
  231. {
  232. Path jarFile = Paths.get( versionRoot, "/plexus-utils-1.4.3-" + timestamp + "-" + i + ".jar" );
  233. Files.createFile( jarFile );
  234. Path pomFile = Paths.get( versionRoot, "/plexus-utils-1.4.3-" + timestamp + "-" + i + ".pom" );
  235. Files.createFile(pomFile);
  236. // set timestamp to older than 100 days for the first build, but ensure the filename timestamp is honoured instead
  237. if ( i == 5 )
  238. {
  239. Files.setLastModifiedTime( jarFile, FileTime.fromMillis( OLD_TIMESTAMP ));
  240. Files.setLastModifiedTime( pomFile, FileTime.fromMillis( OLD_TIMESTAMP ));
  241. }
  242. }
  243. // test listeners for the correct artifacts
  244. String[] exts = {".sha1",""};
  245. for (int i=0; i<exts.length; i++) {
  246. listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.codehaus.plexus", "plexus-utils",
  247. "1.4.3-20070113.163208-4", "plexus-utils-1.4.3-20070113.163208-4.jar"+exts[i]);
  248. listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.codehaus.plexus", "plexus-utils",
  249. "1.4.3-20070113.163208-4", "plexus-utils-1.4.3-20070113.163208-4.pom"+exts[i]);
  250. }
  251. listenerControl.replay();
  252. // Provide the metadata list
  253. List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir );
  254. when(metadataRepository.getArtifacts(TEST_REPO_ID, projectNs,
  255. projectName, projectVersion)).thenReturn(ml);
  256. repoPurge.process( PATH_TO_BY_DAYS_OLD_METADATA_DRIVEN_ARTIFACT );
  257. listenerControl.verify();
  258. // Verify the metadataRepository invocations
  259. verify(metadataRepository, never()).removeProjectVersion(eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion));
  260. ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class);
  261. verify(metadataRepository, times(deletedVersions.size())).removeArtifact(metadataArg.capture(), eq(projectVersion));
  262. List<ArtifactMetadata> metaL = metadataArg.getAllValues();
  263. for (ArtifactMetadata meta : metaL) {
  264. assertTrue(meta.getId().startsWith(projectName));
  265. assertTrue(deletedVersions.contains(meta.getVersion()));
  266. }
  267. // this should be deleted since the filename version (timestamp) is older than
  268. // 100 days even if the last modified date was <100 days ago
  269. assertDeleted( versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.jar" );
  270. assertDeleted( versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.jar.sha1" );
  271. assertDeleted( versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.pom" );
  272. assertDeleted( versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.pom.sha1" );
  273. // this should not be deleted because last modified date is <100 days ago
  274. assertExists( versionRoot + "/plexus-utils-1.4.3-SNAPSHOT.jar" );
  275. assertExists( versionRoot + "/plexus-utils-1.4.3-SNAPSHOT.pom" );
  276. for ( int i = 5; i <= 7; i++ )
  277. {
  278. assertExists( versionRoot + "/plexus-utils-1.4.3-" + timestamp + "-" + i + ".jar" );
  279. assertExists( versionRoot + "/plexus-utils-1.4.3-" + timestamp + "-" + i + ".pom" );
  280. }
  281. }
  282. }