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

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