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.junit.After;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.mockito.ArgumentCaptor;
29 import org.mockito.Mockito;
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.ArgumentMatchers.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 Mockito.when( sessionFactory.createSession( ) ).thenReturn( repositorySession );
59 Mockito.when( repositorySession.getRepository()).thenReturn( metadataRepository );
60 repositorySession.save();
62 repoPurge = new RetentionCountRepositoryPurge( getRepository(), acf.getRetentionCount(),
63 repositorySession, listeners );
68 public void tearDown()
75 * Test if the artifact to be processed was a jar.
78 public void testIfAJarWasFound()
81 String repoRoot = prepareTestRepos();
82 String projectNs = "org.jruby.plugins";
83 String projectPath = projectNs.replaceAll("\\.","/");
84 String projectName = "jruby-rake-plugin";
85 String projectVersion = "1.0RC1-SNAPSHOT";
86 String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
87 Path repo = getTestRepoRootPath();
88 Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
89 Set<String> deletedVersions = new HashSet<>();
90 deletedVersions.add("1.0RC1-20070504.153317-1");
91 deletedVersions.add("1.0RC1-20070504.160758-2");
92 String versionRoot = projectRoot + "/" + projectVersion;
94 // test listeners for the correct artifacts
95 String[] exts = { ".md5", ".sha1", ""};
96 for (int i=0 ; i<exts.length; i++) {
97 listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.jruby.plugins", "jruby-rake-plugin",
98 "1.0RC1-SNAPSHOT", "jruby-rake-plugin-1.0RC1-20070504.153317-1.jar"+exts[i]);
99 listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.jruby.plugins", "jruby-rake-plugin",
100 "1.0RC1-SNAPSHOT", "jruby-rake-plugin-1.0RC1-20070504.153317-1.pom"+exts[i]);
102 listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.jruby.plugins", "jruby-rake-plugin",
103 "1.0RC1-SNAPSHOT", "jruby-rake-plugin-1.0RC1-20070504.160758-2.jar"+exts[i]);
106 listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.jruby.plugins", "jruby-rake-plugin",
107 "1.0RC1-SNAPSHOT", "jruby-rake-plugin-1.0RC1-20070504.160758-2.pom"+exts[i]);
109 listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.jruby.plugins", "jruby-rake-plugin",
110 "1.0RC1-SNAPSHOT", "jruby-rake-plugin-1.0RC1-20070504.160758-2-javadoc.jar");
112 listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.jruby.plugins", "jruby-rake-plugin",
113 "1.0RC1-SNAPSHOT", "jruby-rake-plugin-1.0RC1-20070504.160758-2-javadoc.zip");
114 listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.jruby.plugins", "jruby-rake-plugin",
115 "1.0RC1-SNAPSHOT", "jruby-rake-plugin-1.0RC1-20070504.153317-1-javadoc.jar");
117 listener.deleteArtifact(metadataRepository, getRepository().getId(), "org.jruby.plugins", "jruby-rake-plugin",
118 "1.0RC1-SNAPSHOT", "jruby-rake-plugin-1.0RC1-20070504.153317-1-javadoc.zip");
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);
126 repoPurge.process( PATH_TO_BY_RETENTION_COUNT_ARTIFACT );
128 // Verify the metadataRepository invocations
129 verify(metadataRepository, never()).removeProjectVersion( eq(repositorySession), eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) );
130 ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class);
131 verify(metadataRepository, times(deletedVersions.size())).removeTimestampedArtifact( eq(repositorySession), metadataArg.capture(), eq(projectVersion) );
132 List<ArtifactMetadata> metaL = metadataArg.getAllValues();
133 for (ArtifactMetadata meta : metaL) {
134 assertTrue(meta.getId().startsWith(projectName));
135 assertTrue(deletedVersions.contains(meta.getVersion()));
139 // assert if removed from repo
140 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar" );
141 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar.md5" );
142 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar.sha1" );
143 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom" );
144 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom.md5" );
145 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom.sha1" );
147 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar" );
148 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar.md5" );
149 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar.sha1" );
150 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom" );
151 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom.md5" );
152 assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom.sha1" );
154 // assert if not removed from repo
155 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar" );
156 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar.md5" );
157 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar.sha1" );
158 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom" );
159 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom.md5" );
160 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom.sha1" );
162 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar" );
163 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar.md5" );
164 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar.sha1" );
165 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom" );
166 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom.md5" );
167 assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom.sha1" );
171 * Test if the artifact to be processed is a pom
174 public void testIfAPomWasFound()
177 String repoRoot = prepareTestRepos();
178 String projectNs = "org.codehaus.castor";
179 String projectPath = projectNs.replaceAll("\\.","/");
180 String projectName = "castor-anttasks";
181 String projectVersion = "1.1.2-SNAPSHOT";
182 String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
183 Path repo = getTestRepoRootPath();
184 Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
185 Set<String> deletedVersions = new HashSet<>();
186 deletedVersions.add("1.1.2-20070427.065136-1");
187 String versionRoot = projectRoot + "/" + projectVersion;
190 // test listeners for the correct artifacts
191 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.codehaus.castor", "castor-anttasks",
192 "1.1.2-SNAPSHOT", "castor-anttasks-1.1.2-20070427.065136-1.jar.md5" );
193 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.codehaus.castor", "castor-anttasks",
194 "1.1.2-SNAPSHOT", "castor-anttasks-1.1.2-20070427.065136-1.jar.sha1" );
195 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.codehaus.castor", "castor-anttasks",
196 "1.1.2-SNAPSHOT", "castor-anttasks-1.1.2-20070427.065136-1.jar" );
197 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.codehaus.castor", "castor-anttasks",
198 "1.1.2-SNAPSHOT", "castor-anttasks-1.1.2-20070427.065136-1.pom.md5" );
199 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.codehaus.castor", "castor-anttasks",
200 "1.1.2-SNAPSHOT", "castor-anttasks-1.1.2-20070427.065136-1.pom.sha1" );
201 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.codehaus.castor", "castor-anttasks",
202 "1.1.2-SNAPSHOT", "castor-anttasks-1.1.2-20070427.065136-1.pom" );
203 // Provide the metadata list
204 List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir );
205 when(metadataRepository.getArtifacts( repositorySession, TEST_REPO_ID,
206 projectNs, projectName, projectVersion )).thenReturn(ml);
208 repoPurge.process( PATH_TO_BY_RETENTION_COUNT_POM );
210 // Verify the metadataRepository invocations
211 verify(metadataRepository, never()).removeProjectVersion( eq(repositorySession), eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) );
212 ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class);
213 verify(metadataRepository, times(deletedVersions.size())).removeTimestampedArtifact( eq(repositorySession), metadataArg.capture(), eq(projectVersion) );
214 List<ArtifactMetadata> metaL = metadataArg.getAllValues();
215 for (ArtifactMetadata meta : metaL) {
216 assertTrue(meta.getId().startsWith(projectName));
217 assertTrue(deletedVersions.contains(meta.getVersion()));
221 // assert if removed from repo
222 assertDeleted( versionRoot + "/castor-anttasks-1.1.2-20070427.065136-1.jar" );
223 assertDeleted( versionRoot + "/castor-anttasks-1.1.2-20070427.065136-1.jar.md5" );
224 assertDeleted( versionRoot + "/castor-anttasks-1.1.2-20070427.065136-1.jar.sha1" );
225 assertDeleted( versionRoot + "/castor-anttasks-1.1.2-20070427.065136-1.pom" );
226 assertDeleted( versionRoot + "/castor-anttasks-1.1.2-20070427.065136-1.pom.md5" );
227 assertDeleted( versionRoot + "/castor-anttasks-1.1.2-20070427.065136-1.pom.sha1" );
229 // assert if not removed from repo
230 assertExists( versionRoot + "/castor-anttasks-1.1.2-20070615.105019-3.pom" );
231 assertExists( versionRoot + "/castor-anttasks-1.1.2-20070615.105019-3.pom.md5" );
232 assertExists( versionRoot + "/castor-anttasks-1.1.2-20070615.105019-3.pom.sha1" );
233 assertExists( versionRoot + "/castor-anttasks-1.1.2-20070615.105019-3.jar" );
234 assertExists( versionRoot + "/castor-anttasks-1.1.2-20070615.105019-3.jar.md5" );
235 assertExists( versionRoot + "/castor-anttasks-1.1.2-20070615.105019-3.jar.sha1" );
236 assertExists( versionRoot + "/castor-anttasks-1.1.2-20070615.105019-3-sources.jar" );
237 assertExists( versionRoot + "/castor-anttasks-1.1.2-20070615.105019-3-sources.jar.md5" );
238 assertExists( versionRoot + "/castor-anttasks-1.1.2-20070615.105019-3-sources.jar.sha1" );
240 assertExists( versionRoot + "/castor-anttasks-1.1.2-20070506.163513-2.pom" );
241 assertExists( versionRoot + "/castor-anttasks-1.1.2-20070506.163513-2.pom.md5" );
242 assertExists( versionRoot + "/castor-anttasks-1.1.2-20070506.163513-2.pom.sha1" );
243 assertExists( versionRoot + "/castor-anttasks-1.1.2-20070506.163513-2.jar" );
244 assertExists( versionRoot + "/castor-anttasks-1.1.2-20070506.163513-2.jar.md5" );
245 assertExists( versionRoot + "/castor-anttasks-1.1.2-20070506.163513-2.jar.sha1" );
246 assertExists( versionRoot + "/castor-anttasks-1.1.2-20070506.163513-2-sources.jar" );
247 assertExists( versionRoot + "/castor-anttasks-1.1.2-20070506.163513-2-sources.jar.md5" );
248 assertExists( versionRoot + "/castor-anttasks-1.1.2-20070506.163513-2-sources.jar.sha1" );
252 public void testOrderOfDeletion()
255 String repoRoot = prepareTestRepos();
256 String projectNs = "org.apache.maven.plugins";
257 String projectPath = projectNs.replaceAll("\\.","/");
258 String projectName = "maven-assembly-plugin";
259 String projectVersion = "1.1.2-SNAPSHOT";
260 String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
261 Path repo = getTestRepoRootPath();
262 Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
263 Set<String> deletedVersions = new HashSet<>();
264 deletedVersions.add("1.1.2-20070427.065136-1");
265 String versionRoot = projectRoot + "/" + projectVersion;
268 // test listeners for the correct artifacts
269 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
270 "maven-assembly-plugin", "1.1.2-SNAPSHOT",
271 "maven-assembly-plugin-1.1.2-20070427.065136-1.jar.md5" );
272 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
273 "maven-assembly-plugin", "1.1.2-SNAPSHOT",
274 "maven-assembly-plugin-1.1.2-20070427.065136-1.jar.sha1" );
275 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
276 "maven-assembly-plugin", "1.1.2-SNAPSHOT",
277 "maven-assembly-plugin-1.1.2-20070427.065136-1.jar" );
278 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
279 "maven-assembly-plugin", "1.1.2-SNAPSHOT",
280 "maven-assembly-plugin-1.1.2-20070427.065136-1.pom.md5" );
281 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
282 "maven-assembly-plugin", "1.1.2-SNAPSHOT",
283 "maven-assembly-plugin-1.1.2-20070427.065136-1.pom.sha1" );
284 listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
285 "maven-assembly-plugin", "1.1.2-SNAPSHOT",
286 "maven-assembly-plugin-1.1.2-20070427.065136-1.pom" );
287 // Provide the metadata list
288 List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir );
289 when(metadataRepository.getArtifacts( repositorySession, TEST_REPO_ID,
290 projectNs, projectName, projectVersion )).thenReturn(ml);
292 repoPurge.process( PATH_TO_TEST_ORDER_OF_DELETION );
294 // Verify the metadataRepository invocations
295 verify(metadataRepository, never()).removeProjectVersion( eq(repositorySession), eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) );
296 ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class);
297 verify(metadataRepository, times(deletedVersions.size())).removeTimestampedArtifact( eq(repositorySession), metadataArg.capture(), eq(projectVersion) );
298 List<ArtifactMetadata> metaL = metadataArg.getAllValues();
299 for (ArtifactMetadata meta : metaL) {
300 assertTrue(meta.getId().startsWith(projectName));
301 assertTrue(deletedVersions.contains(meta.getVersion()));
305 assertDeleted( versionRoot + "/maven-assembly-plugin-1.1.2-20070427.065136-1.jar" );
306 assertDeleted( versionRoot + "/maven-assembly-plugin-1.1.2-20070427.065136-1.jar.sha1" );
307 assertDeleted( versionRoot + "/maven-assembly-plugin-1.1.2-20070427.065136-1.jar.md5" );
308 assertDeleted( versionRoot + "/maven-assembly-plugin-1.1.2-20070427.065136-1.pom" );
309 assertDeleted( versionRoot + "/maven-assembly-plugin-1.1.2-20070427.065136-1.pom.sha1" );
310 assertDeleted( versionRoot + "/maven-assembly-plugin-1.1.2-20070427.065136-1.pom.md5" );
312 // the following should not have been deleted
313 assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070506.163513-2.jar" );
314 assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070506.163513-2.jar.sha1" );
315 assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070506.163513-2.jar.md5" );
316 assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070506.163513-2.pom" );
317 assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070506.163513-2.pom.sha1" );
318 assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070506.163513-2.pom.md5" );
320 assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070615.105019-3.jar" );
321 assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070615.105019-3.jar.sha1" );
322 assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070615.105019-3.jar.md5" );
323 assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070615.105019-3.pom" );
324 assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070615.105019-3.pom.sha1" );
325 assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070615.105019-3.pom.md5" );