1 package org.apache.archiva.consumers.core.repository;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.admin.model.beans.ManagedRepository;
23 import org.apache.archiva.metadata.model.ArtifactMetadata;
24 import org.apache.archiva.repository.events.RepositoryListener;
25 import org.apache.commons.lang.time.DateUtils;
26 import org.junit.After;
27 import org.junit.Test;
28 import org.mockito.ArgumentCaptor;
31 import java.nio.file.Path;
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;
39 import static org.junit.Assert.assertTrue;
40 import static org.mockito.Matchers.eq;
41 import static org.mockito.Mockito.*;
45 public class DaysOldRepositoryPurgeTest
46 extends AbstractRepositoryPurgeTest
48 private static final int OLD_TIMESTAMP = 1179382029;
50 private void setLastModified( String dirPath, long lastModified )
52 File dir = new File( dirPath );
53 File[] contents = dir.listFiles();
54 for ( File content : contents )
56 content.setLastModified( lastModified );
62 public void tearDown()
70 public void testByLastModified()
73 ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
74 repoPurge = new DaysOldRepositoryPurge( getRepository(), repoConfiguration.getDaysOlder(),
75 repoConfiguration.getRetentionCount(), repositorySession,
76 Collections.singletonList( listener ) );
78 String repoRoot = prepareTestRepos();
79 String projectNs = "org.apache.maven.plugins";
80 String projectPath = projectNs.replaceAll("\\.","/");
81 String projectName = "maven-install-plugin";
82 String projectVersion = "2.2-SNAPSHOT";
83 String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
84 Path repo = getTestRepoRootPath();
85 Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
86 Set<String> deletedVersions = new HashSet<>();
87 deletedVersions.add("2.2-SNAPSHOT");
88 deletedVersions.add("2.2-20061118.060401-2");
90 setLastModified( projectRoot + "/" + projectVersion + "/", OLD_TIMESTAMP );
92 // test listeners for the correct artifacts
93 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
94 "maven-install-plugin", "2.2-SNAPSHOT", "maven-install-plugin-2.2-SNAPSHOT.jar" );
95 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
96 "maven-install-plugin", "2.2-SNAPSHOT", "maven-install-plugin-2.2-SNAPSHOT.pom" );
97 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
98 "maven-install-plugin", "2.2-20061118.060401-2",
99 "maven-install-plugin-2.2-20061118.060401-2.jar" );
100 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
101 "maven-install-plugin", "2.2-20061118.060401-2",
102 "maven-install-plugin-2.2-20061118.060401-2.pom" );
103 listenerControl.replay();
105 // Provide the metadata list
106 List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir );
107 when(metadataRepository.getArtifacts(TEST_REPO_ID, projectNs,
108 projectName, projectVersion)).thenReturn(ml);
110 repoPurge.process( PATH_TO_BY_DAYS_OLD_ARTIFACT );
112 listenerControl.verify();
114 // Verify the metadataRepository invocations
115 verify(metadataRepository, never()).removeProjectVersion(eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion));
116 ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class);
117 verify(metadataRepository, times(2)).removeArtifact(metadataArg.capture(), eq(projectVersion));
118 List<ArtifactMetadata> metaL = metadataArg.getAllValues();
119 for (ArtifactMetadata meta : metaL) {
120 assertTrue(meta.getId().startsWith(projectName));
121 assertTrue(deletedVersions.contains(meta.getVersion()));
125 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar" );
126 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.md5" );
127 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.sha1" );
128 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom" );
129 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.md5" );
130 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.sha1" );
132 // shouldn't be deleted because even if older than 30 days (because retention count = 2)
133 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar" );
134 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar.md5" );
135 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar.sha1" );
136 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom" );
137 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom.md5" );
138 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom.sha1" );
140 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar" );
141 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar.md5" );
142 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar.sha1" );
143 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom" );
144 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom.md5" );
145 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom.sha1" );
147 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar" );
148 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.md5" );
149 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.sha1" );
150 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom" );
151 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.md5" );
152 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.sha1" );
156 public void testOrderOfDeletion()
159 ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
160 List<RepositoryListener> listeners = Collections.singletonList( listener );
161 repoPurge = new DaysOldRepositoryPurge( getRepository(), repoConfiguration.getDaysOlder(),
162 repoConfiguration.getRetentionCount(), repositorySession, listeners );
164 String repoRoot = prepareTestRepos();
165 String projectNs = "org.apache.maven.plugins";
166 String projectPath = projectNs.replaceAll("\\.","/");
167 String projectName = "maven-assembly-plugin";
168 String projectVersion = "1.1.2-SNAPSHOT";
169 String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
170 Path repo = getTestRepoRootPath();
171 Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
172 Set<String> deletedVersions = new HashSet<>();
173 deletedVersions.add("1.1.2-20070427.065136-1");
175 setLastModified( projectRoot + "/" + projectVersion + "/", OLD_TIMESTAMP );
177 // test listeners for the correct artifacts
178 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
179 "maven-assembly-plugin", "1.1.2-20070427.065136-1",
180 "maven-assembly-plugin-1.1.2-20070427.065136-1.jar" );
181 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
182 "maven-assembly-plugin", "1.1.2-20070427.065136-1",
183 "maven-assembly-plugin-1.1.2-20070427.065136-1.pom" );
184 listenerControl.replay();
186 // Provide the metadata list
187 List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir );
188 when(metadataRepository.getArtifacts(TEST_REPO_ID, projectNs,
189 projectName, projectVersion)).thenReturn(ml);
192 repoPurge.process( PATH_TO_TEST_ORDER_OF_DELETION );
194 listenerControl.verify();
196 // Verify the metadataRepository invocations
197 verify(metadataRepository, never()).removeProjectVersion(eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion));
198 ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class);
199 verify(metadataRepository, times(deletedVersions.size())).removeArtifact(metadataArg.capture(), eq(projectVersion));
200 List<ArtifactMetadata> metaL = metadataArg.getAllValues();
201 for (ArtifactMetadata meta : metaL) {
202 assertTrue(meta.getId().startsWith(projectName));
203 assertTrue(deletedVersions.contains(meta.getVersion()));
207 assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.jar" );
208 assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.jar.sha1" );
209 assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.jar.md5" );
210 assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.pom" );
211 assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.pom.sha1" );
212 assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.pom.md5" );
214 // the following should not have been deleted
215 assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.jar" );
216 assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.jar.sha1" );
217 assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.jar.md5" );
218 assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.pom" );
219 assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.pom.sha1" );
220 assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.pom.md5" );
222 assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.jar" );
223 assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.jar.sha1" );
224 assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.jar.md5" );
225 assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.pom" );
226 assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.pom.sha1" );
227 assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.pom.md5" );
231 public void testMetadataDrivenSnapshots()
234 ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
235 List<RepositoryListener> listeners = Collections.singletonList( listener );
236 repoPurge = new DaysOldRepositoryPurge( getRepository(), repoConfiguration.getDaysOlder(),
237 repoConfiguration.getRetentionCount(), repositorySession, listeners );
239 String repoRoot = prepareTestRepos();
240 String projectNs = "org.codehaus.plexus";
241 String projectPath = projectNs.replaceAll("\\.","/");
242 String projectName = "plexus-utils";
243 String projectVersion = "1.4.3-SNAPSHOT";
244 String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
245 Path repo = getTestRepoRootPath();
246 Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
247 Set<String> deletedVersions = new HashSet<>();
248 deletedVersions.add("1.4.3-20070113.163208-4");
251 String versionRoot = projectRoot + "/"+ projectVersion;
253 Calendar currentDate = Calendar.getInstance( DateUtils.UTC_TIME_ZONE );
254 setLastModified( versionRoot, currentDate.getTimeInMillis() );
256 String timestamp = new SimpleDateFormat( "yyyyMMdd.HHmmss" ).format( currentDate.getTime() );
258 for ( int i = 5; i <= 7; i++ )
260 File jarFile = new File( versionRoot, "/plexus-utils-1.4.3-" + timestamp + "-" + i + ".jar" );
261 jarFile.createNewFile();
262 File pomFile = new File( versionRoot, "/plexus-utils-1.4.3-" + timestamp + "-" + i + ".pom" );
263 pomFile.createNewFile();
265 // set timestamp to older than 100 days for the first build, but ensure the filename timestamp is honoured instead
268 jarFile.setLastModified( OLD_TIMESTAMP );
269 pomFile.setLastModified( OLD_TIMESTAMP );
273 // test listeners for the correct artifacts
274 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.codehaus.plexus", "plexus-utils",
275 "1.4.3-20070113.163208-4", "plexus-utils-1.4.3-20070113.163208-4.jar" );
276 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.codehaus.plexus", "plexus-utils",
277 "1.4.3-20070113.163208-4", "plexus-utils-1.4.3-20070113.163208-4.pom" );
278 listenerControl.replay();
280 // Provide the metadata list
281 List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir );
282 when(metadataRepository.getArtifacts(TEST_REPO_ID, projectNs,
283 projectName, projectVersion)).thenReturn(ml);
286 repoPurge.process( PATH_TO_BY_DAYS_OLD_METADATA_DRIVEN_ARTIFACT );
288 listenerControl.verify();
290 // Verify the metadataRepository invocations
291 verify(metadataRepository, never()).removeProjectVersion(eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion));
292 ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class);
293 verify(metadataRepository, times(deletedVersions.size())).removeArtifact(metadataArg.capture(), eq(projectVersion));
294 List<ArtifactMetadata> metaL = metadataArg.getAllValues();
295 for (ArtifactMetadata meta : metaL) {
296 assertTrue(meta.getId().startsWith(projectName));
297 assertTrue(deletedVersions.contains(meta.getVersion()));
301 // this should be deleted since the filename version (timestamp) is older than
302 // 100 days even if the last modified date was <100 days ago
303 assertDeleted( versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.jar" );
304 assertDeleted( versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.jar.sha1" );
305 assertDeleted( versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.pom" );
306 assertDeleted( versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.pom.sha1" );
308 // this should not be deleted because last modified date is <100 days ago
309 assertExists( versionRoot + "/plexus-utils-1.4.3-SNAPSHOT.jar" );
310 assertExists( versionRoot + "/plexus-utils-1.4.3-SNAPSHOT.pom" );
312 for ( int i = 5; i <= 7; i++ )
314 assertExists( versionRoot + "/plexus-utils-1.4.3-" + timestamp + "-" + i + ".jar" );
315 assertExists( versionRoot + "/plexus-utils-1.4.3-" + timestamp + "-" + i + ".pom" );