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.metadata.model.ArtifactMetadata;
23 import org.apache.archiva.metadata.audit.RepositoryListener;
24 import org.apache.archiva.repository.features.ArtifactCleanupFeature;
25 import org.easymock.EasyMock;
26 import org.junit.After;
27 import org.junit.Test;
28 import org.mockito.ArgumentCaptor;
30 import java.io.IOException;
31 import java.nio.file.Files;
32 import java.nio.file.Path;
33 import java.nio.file.Paths;
34 import java.nio.file.attribute.FileTime;
35 import java.text.SimpleDateFormat;
38 import static org.junit.Assert.assertTrue;
39 import static org.mockito.Matchers.eq;
40 import static org.mockito.Mockito.*;
44 public class DaysOldRepositoryPurgeTest
45 extends AbstractRepositoryPurgeTest
47 private static final int OLD_TIMESTAMP = 1179382029;
49 private void setLastModified( String dirPath, long lastModified ) throws IOException
51 Path dir = Paths.get( dirPath );
52 Path[] contents = Files.list( dir ).toArray(Path[]::new );
53 for ( Path content : contents )
55 Files.setLastModifiedTime( content, FileTime.fromMillis( lastModified ));
61 public void tearDown()
69 public void testByLastModified()
72 org.apache.archiva.repository.ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
73 ArtifactCleanupFeature atf = repoConfiguration.getFeature( ArtifactCleanupFeature.class ).get();
76 sessionControl.reset();
77 sessionFactoryControl.reset();
78 EasyMock.expect( sessionFactory.createSession( ) ).andStubReturn( repositorySession );
79 EasyMock.expect( repositorySession.getRepository()).andStubReturn( metadataRepository );
80 repositorySession.save();
81 EasyMock.expectLastCall().anyTimes();
82 sessionFactoryControl.replay();
83 sessionControl.replay();
85 repoPurge = new DaysOldRepositoryPurge( getRepository(), atf.getRetentionPeriod().getDays(),
86 atf.getRetentionCount(), repositorySession,
87 Collections.singletonList( listener ) );
89 String repoRoot = prepareTestRepos();
90 String projectNs = "org.apache.maven.plugins";
91 String projectPath = projectNs.replaceAll("\\.","/");
92 String projectName = "maven-install-plugin";
93 String projectVersion = "2.2-SNAPSHOT";
94 String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
95 Path repo = getTestRepoRootPath();
96 Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
97 Set<String> deletedVersions = new HashSet<>();
98 deletedVersions.add("2.2-SNAPSHOT");
99 deletedVersions.add("2.2-20061118.060401-2");
101 setLastModified( projectRoot + "/" + projectVersion + "/", OLD_TIMESTAMP );
103 // test listeners for the correct artifacts
104 String[] exts = {".md5",".sha1",""};
105 for (int i=0; i<exts.length; i++) {
107 listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
108 "maven-install-plugin", "2.2-SNAPSHOT", "maven-install-plugin-2.2-SNAPSHOT.jar"+exts[i]);
109 listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
110 "maven-install-plugin", "2.2-SNAPSHOT", "maven-install-plugin-2.2-SNAPSHOT.pom"+exts[i]);
111 listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
112 "maven-install-plugin", "2.2-20061118.060401-2",
113 "maven-install-plugin-2.2-20061118.060401-2.jar"+exts[i]);
114 listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
115 "maven-install-plugin", "2.2-20061118.060401-2",
116 "maven-install-plugin-2.2-20061118.060401-2.pom"+exts[i]);
118 listenerControl.replay();
120 // Provide the metadata list
121 List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir );
122 when(metadataRepository.getArtifacts( repositorySession, TEST_REPO_ID,
123 projectNs, projectName, projectVersion )).thenReturn(ml);
125 repoPurge.process( PATH_TO_BY_DAYS_OLD_ARTIFACT );
127 listenerControl.verify();
129 // Verify the metadataRepository invocations
130 verify(metadataRepository, never()).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) );
131 ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class);
132 verify(metadataRepository, times(2)).removeTimestampedArtifact(eq(repositorySession) , metadataArg.capture(), eq(projectVersion) );
133 List<ArtifactMetadata> metaL = metadataArg.getAllValues();
134 for (ArtifactMetadata meta : metaL) {
135 assertTrue(meta.getId().startsWith(projectName));
136 assertTrue(deletedVersions.contains(meta.getVersion()));
140 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar" );
141 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.md5" );
142 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.sha1" );
143 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom" );
144 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.md5" );
145 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.sha1" );
147 // shouldn't be deleted because even if older than 30 days (because retention count = 2)
148 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar" );
149 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar.md5" );
150 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar.sha1" );
151 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom" );
152 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom.md5" );
153 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom.sha1" );
155 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar" );
156 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar.md5" );
157 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar.sha1" );
158 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom" );
159 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom.md5" );
160 assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom.sha1" );
162 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar" );
163 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.md5" );
164 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.sha1" );
165 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom" );
166 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.md5" );
167 assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.sha1" );
171 public void testOrderOfDeletion()
174 org.apache.archiva.repository.ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
175 ArtifactCleanupFeature atf = repoConfiguration.getFeature( ArtifactCleanupFeature.class ).get();
176 List<RepositoryListener> listeners = Collections.singletonList( listener );
178 sessionControl.reset();
179 sessionFactoryControl.reset();
180 EasyMock.expect( sessionFactory.createSession( ) ).andStubReturn( repositorySession );
181 EasyMock.expect( repositorySession.getRepository()).andStubReturn( metadataRepository );
182 repositorySession.save();
183 EasyMock.expectLastCall().anyTimes();
184 sessionFactoryControl.replay();
185 sessionControl.replay();
186 repoPurge = new DaysOldRepositoryPurge( getRepository(), atf.getRetentionPeriod().getDays(),
187 atf.getRetentionCount(), repositorySession, listeners );
189 String repoRoot = prepareTestRepos();
190 String projectNs = "org.apache.maven.plugins";
191 String projectPath = projectNs.replaceAll("\\.","/");
192 String projectName = "maven-assembly-plugin";
193 String projectVersion = "1.1.2-SNAPSHOT";
194 String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
195 Path repo = getTestRepoRootPath();
196 Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
197 Set<String> deletedVersions = new HashSet<>();
198 deletedVersions.add("1.1.2-20070427.065136-1");
200 setLastModified( projectRoot + "/" + projectVersion + "/", OLD_TIMESTAMP );
202 // test listeners for the correct artifacts
203 String[] exts = {".md5",".sha1",""};
204 for (int i=0; i<exts.length; i++) {
205 listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
206 "maven-assembly-plugin", "1.1.2-20070427.065136-1",
207 "maven-assembly-plugin-1.1.2-20070427.065136-1.jar"+exts[i]);
208 listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
209 "maven-assembly-plugin", "1.1.2-20070427.065136-1",
210 "maven-assembly-plugin-1.1.2-20070427.065136-1.pom"+exts[i]);
212 listenerControl.replay();
214 // Provide the metadata list
215 List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir );
216 when(metadataRepository.getArtifacts(repositorySession , TEST_REPO_ID,
217 projectNs, projectName, projectVersion )).thenReturn(ml);
220 repoPurge.process( PATH_TO_TEST_ORDER_OF_DELETION );
222 listenerControl.verify();
224 // Verify the metadataRepository invocations
225 verify(metadataRepository, never()).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) );
226 ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class);
227 verify(metadataRepository, times(deletedVersions.size())).removeTimestampedArtifact(eq(repositorySession) , metadataArg.capture(), eq(projectVersion) );
228 List<ArtifactMetadata> metaL = metadataArg.getAllValues();
229 for (ArtifactMetadata meta : metaL) {
230 assertTrue(meta.getId().startsWith(projectName));
231 assertTrue(deletedVersions.contains(meta.getVersion()));
235 assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.jar" );
236 assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.jar.sha1" );
237 assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.jar.md5" );
238 assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.pom" );
239 assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.pom.sha1" );
240 assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.pom.md5" );
242 // the following should not have been deleted
243 assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.jar" );
244 assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.jar.sha1" );
245 assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.jar.md5" );
246 assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.pom" );
247 assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.pom.sha1" );
248 assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.pom.md5" );
250 assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.jar" );
251 assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.jar.sha1" );
252 assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.jar.md5" );
253 assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.pom" );
254 assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.pom.sha1" );
255 assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.pom.md5" );
259 public void testMetadataDrivenSnapshots()
262 org.apache.archiva.repository.ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
263 ArtifactCleanupFeature atf = repoConfiguration.getFeature( ArtifactCleanupFeature.class ).get();
264 List<RepositoryListener> listeners = Collections.singletonList( listener );
266 sessionControl.reset();
267 sessionFactoryControl.reset();
268 EasyMock.expect( sessionFactory.createSession( ) ).andStubReturn( repositorySession );
269 EasyMock.expect( repositorySession.getRepository()).andStubReturn( metadataRepository );
270 repositorySession.save();
271 EasyMock.expectLastCall().anyTimes();
272 sessionFactoryControl.replay();
273 sessionControl.replay();
274 repoPurge = new DaysOldRepositoryPurge( getRepository(), atf.getRetentionPeriod().getDays(),
275 atf.getRetentionCount(), repositorySession, listeners );
277 String repoRoot = prepareTestRepos();
278 String projectNs = "org.codehaus.plexus";
279 String projectPath = projectNs.replaceAll("\\.","/");
280 String projectName = "plexus-utils";
281 String projectVersion = "1.4.3-SNAPSHOT";
282 String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
283 Path repo = getTestRepoRootPath();
284 Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
285 Set<String> deletedVersions = new HashSet<>();
286 deletedVersions.add("1.4.3-20070113.163208-4");
289 String versionRoot = projectRoot + "/"+ projectVersion;
291 Calendar currentDate = Calendar.getInstance( TimeZone.getTimeZone("UTC") );
292 setLastModified( versionRoot, currentDate.getTimeInMillis() );
294 String timestamp = new SimpleDateFormat( "yyyyMMdd.HHmmss" ).format( currentDate.getTime() );
296 for ( int i = 5; i <= 7; i++ )
298 Path jarFile = Paths.get( versionRoot, "/plexus-utils-1.4.3-" + timestamp + "-" + i + ".jar" );
299 Files.createFile( jarFile );
300 Path pomFile = Paths.get( versionRoot, "/plexus-utils-1.4.3-" + timestamp + "-" + i + ".pom" );
301 Files.createFile(pomFile);
303 // set timestamp to older than 100 days for the first build, but ensure the filename timestamp is honoured instead
306 Files.setLastModifiedTime( jarFile, FileTime.fromMillis( OLD_TIMESTAMP ));
307 Files.setLastModifiedTime( pomFile, FileTime.fromMillis( OLD_TIMESTAMP ));
311 // test listeners for the correct artifacts
312 String[] exts = {".sha1",""};
313 for (int i=0; i<exts.length; i++) {
315 listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.codehaus.plexus", "plexus-utils",
316 "1.4.3-20070113.163208-4", "plexus-utils-1.4.3-20070113.163208-4.jar"+exts[i]);
317 listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.codehaus.plexus", "plexus-utils",
318 "1.4.3-20070113.163208-4", "plexus-utils-1.4.3-20070113.163208-4.pom"+exts[i]);
320 listenerControl.replay();
322 // Provide the metadata list
323 List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir );
324 when(metadataRepository.getArtifacts(repositorySession , TEST_REPO_ID,
325 projectNs, projectName, projectVersion )).thenReturn(ml);
328 repoPurge.process( PATH_TO_BY_DAYS_OLD_METADATA_DRIVEN_ARTIFACT );
330 listenerControl.verify();
332 // Verify the metadataRepository invocations
333 verify(metadataRepository, never()).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) );
334 ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class);
335 verify(metadataRepository, times(deletedVersions.size())).removeTimestampedArtifact( eq(repositorySession), metadataArg.capture(), eq(projectVersion) );
336 List<ArtifactMetadata> metaL = metadataArg.getAllValues();
337 for (ArtifactMetadata meta : metaL) {
338 assertTrue(meta.getId().startsWith(projectName));
339 assertTrue(deletedVersions.contains(meta.getVersion()));
343 // this should be deleted since the filename version (timestamp) is older than
344 // 100 days even if the last modified date was <100 days ago
345 assertDeleted( versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.jar" );
346 assertDeleted( versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.jar.sha1" );
347 assertDeleted( versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.pom" );
348 assertDeleted( versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.pom.sha1" );
350 // this should not be deleted because last modified date is <100 days ago
351 assertExists( versionRoot + "/plexus-utils-1.4.3-SNAPSHOT.jar" );
352 assertExists( versionRoot + "/plexus-utils-1.4.3-SNAPSHOT.pom" );
354 for ( int i = 5; i <= 7; i++ )
356 assertExists( versionRoot + "/plexus-utils-1.4.3-" + timestamp + "-" + i + ".jar" );
357 assertExists( versionRoot + "/plexus-utils-1.4.3-" + timestamp + "-" + i + ".pom" );