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.Before;
28 import org.junit.Test;
29 import org.mockito.ArgumentCaptor;
31 import java.nio.file.Path;
32 import java.util.Collections;
33 import java.util.HashSet;
34 import java.util.List;
37 import static org.junit.Assert.assertTrue;
38 import static org.mockito.Matchers.eq;
39 import static org.mockito.Mockito.*;
42 * Test RetentionsCountRepositoryPurgeTest
44 public class RetentionCountRepositoryPurgeTest
45 extends AbstractRepositoryPurgeTest
54 org.apache.archiva.repository.ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
55 List<RepositoryListener> listeners = Collections.singletonList( listener );
56 ArtifactCleanupFeature acf = repoConfiguration.getFeature( ArtifactCleanupFeature.class ).get();
58 sessionControl.reset();
59 sessionFactoryControl.reset();
60 EasyMock.expect( sessionFactory.createSession( ) ).andStubReturn( repositorySession );
61 EasyMock.expect( repositorySession.getRepository()).andStubReturn( metadataRepository );
62 repositorySession.save();
63 EasyMock.expectLastCall().anyTimes();
64 sessionFactoryControl.replay();
65 sessionControl.replay();
67 repoPurge = new RetentionCountRepositoryPurge( getRepository(), acf.getRetentionCount(),
68 repositorySession, listeners );
73 public void tearDown()
80 * Test if the artifact to be processed was a jar.
83 public void testIfAJarWasFound()
86 String repoRoot = prepareTestRepos();
87 String projectNs = "org.jruby.plugins";
88 String projectPath = projectNs.replaceAll("\\.","/");
89 String projectName = "jruby-rake-plugin";
90 String projectVersion = "1.0RC1-SNAPSHOT";
91 String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
92 Path repo = getTestRepoRootPath();
93 Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
94 Set<String> deletedVersions = new HashSet<>();
95 deletedVersions.add("1.0RC1-20070504.153317-1");
96 deletedVersions.add("1.0RC1-20070504.160758-2");
97 String versionRoot = projectRoot + "/" + projectVersion;
99 // test listeners for the correct artifacts
100 String[] exts = { ".md5", ".sha1", ""};
101 for (int i=0 ; i<exts.length; i++) {
102 listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.jruby.plugins", "jruby-rake-plugin",
103 "1.0RC1-20070504.153317-1", "jruby-rake-plugin-1.0RC1-20070504.153317-1.jar"+exts[i]);
104 listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.jruby.plugins", "jruby-rake-plugin",
105 "1.0RC1-20070504.153317-1", "jruby-rake-plugin-1.0RC1-20070504.153317-1.pom"+exts[i]);
107 listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.jruby.plugins", "jruby-rake-plugin",
108 "1.0RC1-20070504.160758-2", "jruby-rake-plugin-1.0RC1-20070504.160758-2.jar"+exts[i]);
111 listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.jruby.plugins", "jruby-rake-plugin",
112 "1.0RC1-20070504.160758-2", "jruby-rake-plugin-1.0RC1-20070504.160758-2.pom"+exts[i]);
114 listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.jruby.plugins", "jruby-rake-plugin",
115 "1.0RC1-20070504.160758-2", "jruby-rake-plugin-1.0RC1-20070504.160758-2-javadoc.jar");
117 listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.jruby.plugins", "jruby-rake-plugin",
118 "1.0RC1-20070504.160758-2", "jruby-rake-plugin-1.0RC1-20070504.160758-2-javadoc.zip");
119 listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.jruby.plugins", "jruby-rake-plugin",
120 "1.0RC1-20070504.153317-1", "jruby-rake-plugin-1.0RC1-20070504.153317-1-javadoc.jar");
122 listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.jruby.plugins", "jruby-rake-plugin",
123 "1.0RC1-20070504.153317-1", "jruby-rake-plugin-1.0RC1-20070504.153317-1-javadoc.zip");
125 listenerControl.replay();
127 // Provide the metadata list
128 List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir );
129 when(metadataRepository.getArtifacts( repositorySession, TEST_REPO_ID,
130 projectNs, projectName, projectVersion )).thenReturn(ml);
133 repoPurge.process( PATH_TO_BY_RETENTION_COUNT_ARTIFACT );
135 listenerControl.verify();
137 // Verify the metadataRepository invocations
138 verify(metadataRepository, never()).removeProjectVersion( eq(repositorySession), eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) );
139 ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class);
140 verify(metadataRepository, times(deletedVersions.size())).removeTimestampedArtifact( eq(repositorySession), metadataArg.capture(), eq(projectVersion) );
141 List<ArtifactMetadata> metaL = metadataArg.getAllValues();
142 for (ArtifactMetadata meta : metaL) {
143 assertTrue(meta.getId().startsWith(projectName));
144 assertTrue(deletedVersions.contains(meta.getVersion()));
148 // assert if removed from repo
149 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar" );
150 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar.md5" );
151 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar.sha1" );
152 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom" );
153 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom.md5" );
154 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom.sha1" );
156 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar" );
157 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar.md5" );
158 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar.sha1" );
159 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom" );
160 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom.md5" );
161 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom.sha1" );
163 // assert if not removed from repo
164 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar" );
165 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar.md5" );
166 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar.sha1" );
167 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom" );
168 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom.md5" );
169 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom.sha1" );
171 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar" );
172 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar.md5" );
173 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar.sha1" );
174 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom" );
175 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom.md5" );
176 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom.sha1" );
180 * Test if the artifact to be processed is a pom
183 public void testIfAPomWasFound()
186 String repoRoot = prepareTestRepos();
187 String projectNs = "org.codehaus.castor";
188 String projectPath = projectNs.replaceAll("\\.","/");
189 String projectName = "castor-anttasks";
190 String projectVersion = "1.1.2-SNAPSHOT";
191 String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
192 Path repo = getTestRepoRootPath();
193 Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
194 Set<String> deletedVersions = new HashSet<>();
195 deletedVersions.add("1.1.2-20070427.065136-1");
196 String versionRoot = projectRoot + "/" + projectVersion;
199 // test listeners for the correct artifacts
200 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.codehaus.castor", "castor-anttasks",
201 "1.1.2-20070427.065136-1", "castor-anttasks-1.1.2-20070427.065136-1.jar.md5" );
202 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.codehaus.castor", "castor-anttasks",
203 "1.1.2-20070427.065136-1", "castor-anttasks-1.1.2-20070427.065136-1.jar.sha1" );
204 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.codehaus.castor", "castor-anttasks",
205 "1.1.2-20070427.065136-1", "castor-anttasks-1.1.2-20070427.065136-1.jar" );
206 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.codehaus.castor", "castor-anttasks",
207 "1.1.2-20070427.065136-1", "castor-anttasks-1.1.2-20070427.065136-1.pom.md5" );
208 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.codehaus.castor", "castor-anttasks",
209 "1.1.2-20070427.065136-1", "castor-anttasks-1.1.2-20070427.065136-1.pom.sha1" );
210 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.codehaus.castor", "castor-anttasks",
211 "1.1.2-20070427.065136-1", "castor-anttasks-1.1.2-20070427.065136-1.pom" );
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);
219 repoPurge.process( PATH_TO_BY_RETENTION_COUNT_POM );
221 listenerControl.verify();
223 // Verify the metadataRepository invocations
224 verify(metadataRepository, never()).removeProjectVersion( eq(repositorySession), eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) );
225 ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class);
226 verify(metadataRepository, times(deletedVersions.size())).removeTimestampedArtifact( eq(repositorySession), metadataArg.capture(), eq(projectVersion) );
227 List<ArtifactMetadata> metaL = metadataArg.getAllValues();
228 for (ArtifactMetadata meta : metaL) {
229 assertTrue(meta.getId().startsWith(projectName));
230 assertTrue(deletedVersions.contains(meta.getVersion()));
234 // assert if removed from repo
235 assertDeleted( versionRoot + "/castor-anttasks-1.1.2-20070427.065136-1.jar" );
236 assertDeleted( versionRoot + "/castor-anttasks-1.1.2-20070427.065136-1.jar.md5" );
237 assertDeleted( versionRoot + "/castor-anttasks-1.1.2-20070427.065136-1.jar.sha1" );
238 assertDeleted( versionRoot + "/castor-anttasks-1.1.2-20070427.065136-1.pom" );
239 assertDeleted( versionRoot + "/castor-anttasks-1.1.2-20070427.065136-1.pom.md5" );
240 assertDeleted( versionRoot + "/castor-anttasks-1.1.2-20070427.065136-1.pom.sha1" );
242 // assert if not removed from repo
243 assertExists( versionRoot + "/castor-anttasks-1.1.2-20070615.105019-3.pom" );
244 assertExists( versionRoot + "/castor-anttasks-1.1.2-20070615.105019-3.pom.md5" );
245 assertExists( versionRoot + "/castor-anttasks-1.1.2-20070615.105019-3.pom.sha1" );
246 assertExists( versionRoot + "/castor-anttasks-1.1.2-20070615.105019-3.jar" );
247 assertExists( versionRoot + "/castor-anttasks-1.1.2-20070615.105019-3.jar.md5" );
248 assertExists( versionRoot + "/castor-anttasks-1.1.2-20070615.105019-3.jar.sha1" );
249 assertExists( versionRoot + "/castor-anttasks-1.1.2-20070615.105019-3-sources.jar" );
250 assertExists( versionRoot + "/castor-anttasks-1.1.2-20070615.105019-3-sources.jar.md5" );
251 assertExists( versionRoot + "/castor-anttasks-1.1.2-20070615.105019-3-sources.jar.sha1" );
253 assertExists( versionRoot + "/castor-anttasks-1.1.2-20070506.163513-2.pom" );
254 assertExists( versionRoot + "/castor-anttasks-1.1.2-20070506.163513-2.pom.md5" );
255 assertExists( versionRoot + "/castor-anttasks-1.1.2-20070506.163513-2.pom.sha1" );
256 assertExists( versionRoot + "/castor-anttasks-1.1.2-20070506.163513-2.jar" );
257 assertExists( versionRoot + "/castor-anttasks-1.1.2-20070506.163513-2.jar.md5" );
258 assertExists( versionRoot + "/castor-anttasks-1.1.2-20070506.163513-2.jar.sha1" );
259 assertExists( versionRoot + "/castor-anttasks-1.1.2-20070506.163513-2-sources.jar" );
260 assertExists( versionRoot + "/castor-anttasks-1.1.2-20070506.163513-2-sources.jar.md5" );
261 assertExists( versionRoot + "/castor-anttasks-1.1.2-20070506.163513-2-sources.jar.sha1" );
265 public void testOrderOfDeletion()
268 String repoRoot = prepareTestRepos();
269 String projectNs = "org.apache.maven.plugins";
270 String projectPath = projectNs.replaceAll("\\.","/");
271 String projectName = "maven-assembly-plugin";
272 String projectVersion = "1.1.2-SNAPSHOT";
273 String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
274 Path repo = getTestRepoRootPath();
275 Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
276 Set<String> deletedVersions = new HashSet<>();
277 deletedVersions.add("1.1.2-20070427.065136-1");
278 String versionRoot = projectRoot + "/" + projectVersion;
281 // test listeners for the correct artifacts
282 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
283 "maven-assembly-plugin", "1.1.2-20070427.065136-1",
284 "maven-assembly-plugin-1.1.2-20070427.065136-1.jar.md5" );
285 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
286 "maven-assembly-plugin", "1.1.2-20070427.065136-1",
287 "maven-assembly-plugin-1.1.2-20070427.065136-1.jar.sha1" );
288 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
289 "maven-assembly-plugin", "1.1.2-20070427.065136-1",
290 "maven-assembly-plugin-1.1.2-20070427.065136-1.jar" );
291 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
292 "maven-assembly-plugin", "1.1.2-20070427.065136-1",
293 "maven-assembly-plugin-1.1.2-20070427.065136-1.pom.md5" );
294 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
295 "maven-assembly-plugin", "1.1.2-20070427.065136-1",
296 "maven-assembly-plugin-1.1.2-20070427.065136-1.pom.sha1" );
297 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
298 "maven-assembly-plugin", "1.1.2-20070427.065136-1",
299 "maven-assembly-plugin-1.1.2-20070427.065136-1.pom" );
300 listenerControl.replay();
302 // Provide the metadata list
303 List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir );
304 when(metadataRepository.getArtifacts( repositorySession, TEST_REPO_ID,
305 projectNs, projectName, projectVersion )).thenReturn(ml);
307 repoPurge.process( PATH_TO_TEST_ORDER_OF_DELETION );
309 listenerControl.verify();
311 // Verify the metadataRepository invocations
312 verify(metadataRepository, never()).removeProjectVersion( eq(repositorySession), eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) );
313 ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class);
314 verify(metadataRepository, times(deletedVersions.size())).removeTimestampedArtifact( eq(repositorySession), metadataArg.capture(), eq(projectVersion) );
315 List<ArtifactMetadata> metaL = metadataArg.getAllValues();
316 for (ArtifactMetadata meta : metaL) {
317 assertTrue(meta.getId().startsWith(projectName));
318 assertTrue(deletedVersions.contains(meta.getVersion()));
322 assertDeleted( versionRoot + "/maven-assembly-plugin-1.1.2-20070427.065136-1.jar" );
323 assertDeleted( versionRoot + "/maven-assembly-plugin-1.1.2-20070427.065136-1.jar.sha1" );
324 assertDeleted( versionRoot + "/maven-assembly-plugin-1.1.2-20070427.065136-1.jar.md5" );
325 assertDeleted( versionRoot + "/maven-assembly-plugin-1.1.2-20070427.065136-1.pom" );
326 assertDeleted( versionRoot + "/maven-assembly-plugin-1.1.2-20070427.065136-1.pom.sha1" );
327 assertDeleted( versionRoot + "/maven-assembly-plugin-1.1.2-20070427.065136-1.pom.md5" );
329 // the following should not have been deleted
330 assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070506.163513-2.jar" );
331 assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070506.163513-2.jar.sha1" );
332 assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070506.163513-2.jar.md5" );
333 assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070506.163513-2.pom" );
334 assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070506.163513-2.pom.sha1" );
335 assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070506.163513-2.pom.md5" );
337 assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070615.105019-3.jar" );
338 assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070615.105019-3.jar.sha1" );
339 assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070615.105019-3.jar.md5" );
340 assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070615.105019-3.pom" );
341 assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070615.105019-3.pom.sha1" );
342 assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070615.105019-3.pom.md5" );