]> source.dussan.org Git - archiva.git/blob
ca7685b02292fc6c4acf9230390b6f2f2ce9591d
[archiva.git] /
1 package org.apache.archiva.consumers.core.repository;
2
3 /*
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
22 import java.nio.file.Path;
23 import java.util.Collections;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Set;
27
28 import org.apache.archiva.admin.model.beans.ManagedRepository;
29 import org.apache.archiva.metadata.model.ArtifactMetadata;
30 import org.apache.archiva.repository.events.RepositoryListener;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.ArgumentCaptor;
35
36 import static org.junit.Assert.assertTrue;
37 import static org.mockito.Matchers.eq;
38 import static org.mockito.Mockito.*;
39
40 /**
41  * Test RetentionsCountRepositoryPurgeTest
42  */
43 public class RetentionCountRepositoryPurgeTest
44     extends AbstractRepositoryPurgeTest
45 {
46     @Before
47     @Override
48     public void setUp()
49         throws Exception
50     {
51         super.setUp();
52
53         ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
54         List<RepositoryListener> listeners = Collections.singletonList( listener );
55         repoPurge = new RetentionCountRepositoryPurge( getRepository(), repoConfiguration.getRetentionCount(),
56                                                        repositorySession, listeners );
57     }
58
59     @After
60     @Override
61     public void tearDown()
62         throws Exception
63     {
64         super.tearDown();
65     }
66
67     /**
68      * Test if the artifact to be processed was a jar.
69      */
70     @Test
71     public void testIfAJarWasFound()
72         throws Exception
73     {
74         String repoRoot = prepareTestRepos();
75         String projectNs = "org.jruby.plugins";
76         String projectPath = projectNs.replaceAll("\\.","/");
77         String projectName = "jruby-rake-plugin";
78         String projectVersion = "1.0RC1-SNAPSHOT";
79         String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
80         Path repo = getTestRepoRootPath();
81         Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
82         Set<String> deletedVersions = new HashSet<>();
83         deletedVersions.add("1.0RC1-20070504.153317-1");
84         deletedVersions.add("1.0RC1-20070504.160758-2");
85         String versionRoot = projectRoot + "/" + projectVersion;
86
87         // test listeners for the correct artifacts
88         listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.jruby.plugins", "jruby-rake-plugin",
89                                  "1.0RC1-20070504.153317-1", "jruby-rake-plugin-1.0RC1-20070504.153317-1.jar" );
90         listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.jruby.plugins", "jruby-rake-plugin",
91                                  "1.0RC1-20070504.153317-1", "jruby-rake-plugin-1.0RC1-20070504.153317-1.pom" );
92         listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.jruby.plugins", "jruby-rake-plugin",
93                                  "1.0RC1-20070504.153317-1", "jruby-rake-plugin-1.0RC1-20070504.153317-1-javadoc.jar" );
94
95         listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.jruby.plugins", "jruby-rake-plugin",
96                                  "1.0RC1-20070504.153317-1", "jruby-rake-plugin-1.0RC1-20070504.153317-1-javadoc.zip" );
97
98         listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.jruby.plugins", "jruby-rake-plugin",
99                                  "1.0RC1-20070504.160758-2", "jruby-rake-plugin-1.0RC1-20070504.160758-2.jar" );
100
101         listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.jruby.plugins", "jruby-rake-plugin",
102                                  "1.0RC1-20070504.160758-2", "jruby-rake-plugin-1.0RC1-20070504.160758-2-javadoc.jar" );
103
104         listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.jruby.plugins", "jruby-rake-plugin",
105                                  "1.0RC1-20070504.160758-2", "jruby-rake-plugin-1.0RC1-20070504.160758-2-javadoc.zip" );
106
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.pom" );
109         listenerControl.replay();
110
111         // Provide the metadata list
112         List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir );
113         when(metadataRepository.getArtifacts(TEST_REPO_ID, projectNs,
114             projectName, projectVersion)).thenReturn(ml);
115
116
117         repoPurge.process( PATH_TO_BY_RETENTION_COUNT_ARTIFACT );
118
119         listenerControl.verify();
120
121         // Verify the metadataRepository invocations
122         verify(metadataRepository, never()).removeProjectVersion(eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion));
123         ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class);
124         verify(metadataRepository, times(deletedVersions.size())).removeArtifact(metadataArg.capture(), eq(projectVersion));
125         List<ArtifactMetadata> metaL = metadataArg.getAllValues();
126         for (ArtifactMetadata meta : metaL) {
127             assertTrue(meta.getId().startsWith(projectName));
128             assertTrue(deletedVersions.contains(meta.getVersion()));
129         }
130
131
132         // assert if removed from repo
133         assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar" );
134         assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar.md5" );
135         assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar.sha1" );
136         assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom" );
137         assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom.md5" );
138         assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom.sha1" );
139
140         assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar" );
141         assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar.md5" );
142         assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar.sha1" );
143         assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom" );
144         assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom.md5" );
145         assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom.sha1" );
146
147         // assert if not removed from repo
148         assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar" );
149         assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar.md5" );
150         assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar.sha1" );
151         assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom" );
152         assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom.md5" );
153         assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom.sha1" );
154
155         assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar" );
156         assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar.md5" );
157         assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar.sha1" );
158         assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom" );
159         assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom.md5" );
160         assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom.sha1" );
161     }
162
163     /**
164      * Test if the artifact to be processed is a pom
165      */
166     @Test
167     public void testIfAPomWasFound()
168         throws Exception
169     {
170         String repoRoot = prepareTestRepos();
171         String projectNs = "org.codehaus.castor";
172         String projectPath = projectNs.replaceAll("\\.","/");
173         String projectName = "castor-anttasks";
174         String projectVersion = "1.1.2-SNAPSHOT";
175         String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
176         Path repo = getTestRepoRootPath();
177         Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
178         Set<String> deletedVersions = new HashSet<>();
179         deletedVersions.add("1.1.2-20070427.065136-1");
180         String versionRoot = projectRoot + "/" + projectVersion;
181
182
183         // test listeners for the correct artifacts
184         listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.codehaus.castor", "castor-anttasks",
185                                  "1.1.2-20070427.065136-1", "castor-anttasks-1.1.2-20070427.065136-1.jar" );
186         listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.codehaus.castor", "castor-anttasks",
187                                  "1.1.2-20070427.065136-1", "castor-anttasks-1.1.2-20070427.065136-1.pom" );
188         listenerControl.replay();
189
190         // Provide the metadata list
191         List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir );
192         when(metadataRepository.getArtifacts(TEST_REPO_ID, projectNs,
193             projectName, projectVersion)).thenReturn(ml);
194
195         repoPurge.process( PATH_TO_BY_RETENTION_COUNT_POM );
196
197         listenerControl.verify();
198
199         // Verify the metadataRepository invocations
200         verify(metadataRepository, never()).removeProjectVersion(eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion));
201         ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class);
202         verify(metadataRepository, times(deletedVersions.size())).removeArtifact(metadataArg.capture(), eq(projectVersion));
203         List<ArtifactMetadata> metaL = metadataArg.getAllValues();
204         for (ArtifactMetadata meta : metaL) {
205             assertTrue(meta.getId().startsWith(projectName));
206             assertTrue(deletedVersions.contains(meta.getVersion()));
207         }
208
209
210         // assert if removed from repo
211         assertDeleted( versionRoot + "/castor-anttasks-1.1.2-20070427.065136-1.jar" );
212         assertDeleted( versionRoot + "/castor-anttasks-1.1.2-20070427.065136-1.jar.md5" );
213         assertDeleted( versionRoot + "/castor-anttasks-1.1.2-20070427.065136-1.jar.sha1" );
214         assertDeleted( versionRoot + "/castor-anttasks-1.1.2-20070427.065136-1.pom" );
215         assertDeleted( versionRoot + "/castor-anttasks-1.1.2-20070427.065136-1.pom.md5" );
216         assertDeleted( versionRoot + "/castor-anttasks-1.1.2-20070427.065136-1.pom.sha1" );
217
218         // assert if not removed from repo
219         assertExists( versionRoot + "/castor-anttasks-1.1.2-20070615.105019-3.pom" );
220         assertExists( versionRoot + "/castor-anttasks-1.1.2-20070615.105019-3.pom.md5" );
221         assertExists( versionRoot + "/castor-anttasks-1.1.2-20070615.105019-3.pom.sha1" );
222         assertExists( versionRoot + "/castor-anttasks-1.1.2-20070615.105019-3.jar" );
223         assertExists( versionRoot + "/castor-anttasks-1.1.2-20070615.105019-3.jar.md5" );
224         assertExists( versionRoot + "/castor-anttasks-1.1.2-20070615.105019-3.jar.sha1" );
225         assertExists( versionRoot + "/castor-anttasks-1.1.2-20070615.105019-3-sources.jar" );
226         assertExists( versionRoot + "/castor-anttasks-1.1.2-20070615.105019-3-sources.jar.md5" );
227         assertExists( versionRoot + "/castor-anttasks-1.1.2-20070615.105019-3-sources.jar.sha1" );
228
229         assertExists( versionRoot + "/castor-anttasks-1.1.2-20070506.163513-2.pom" );
230         assertExists( versionRoot + "/castor-anttasks-1.1.2-20070506.163513-2.pom.md5" );
231         assertExists( versionRoot + "/castor-anttasks-1.1.2-20070506.163513-2.pom.sha1" );
232         assertExists( versionRoot + "/castor-anttasks-1.1.2-20070506.163513-2.jar" );
233         assertExists( versionRoot + "/castor-anttasks-1.1.2-20070506.163513-2.jar.md5" );
234         assertExists( versionRoot + "/castor-anttasks-1.1.2-20070506.163513-2.jar.sha1" );
235         assertExists( versionRoot + "/castor-anttasks-1.1.2-20070506.163513-2-sources.jar" );
236         assertExists( versionRoot + "/castor-anttasks-1.1.2-20070506.163513-2-sources.jar.md5" );
237         assertExists( versionRoot + "/castor-anttasks-1.1.2-20070506.163513-2-sources.jar.sha1" );
238     }
239
240     @Test
241     public void testOrderOfDeletion()
242         throws Exception
243     {
244         String repoRoot = prepareTestRepos();
245         String projectNs = "org.apache.maven.plugins";
246         String projectPath = projectNs.replaceAll("\\.","/");
247         String projectName = "maven-assembly-plugin";
248         String projectVersion = "1.1.2-SNAPSHOT";
249         String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
250         Path repo = getTestRepoRootPath();
251         Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
252         Set<String> deletedVersions = new HashSet<>();
253         deletedVersions.add("1.1.2-20070427.065136-1");
254         String versionRoot = projectRoot + "/" + projectVersion;
255
256
257         // test listeners for the correct artifacts
258         listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
259                                  "maven-assembly-plugin", "1.1.2-20070427.065136-1",
260                                  "maven-assembly-plugin-1.1.2-20070427.065136-1.jar" );
261         listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
262                                  "maven-assembly-plugin", "1.1.2-20070427.065136-1",
263                                  "maven-assembly-plugin-1.1.2-20070427.065136-1.pom" );
264         listenerControl.replay();
265
266         // Provide the metadata list
267         List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir );
268         when(metadataRepository.getArtifacts(TEST_REPO_ID, projectNs,
269             projectName, projectVersion)).thenReturn(ml);
270
271         repoPurge.process( PATH_TO_TEST_ORDER_OF_DELETION );
272
273         listenerControl.verify();
274
275         // Verify the metadataRepository invocations
276         verify(metadataRepository, never()).removeProjectVersion(eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion));
277         ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class);
278         verify(metadataRepository, times(deletedVersions.size())).removeArtifact(metadataArg.capture(), eq(projectVersion));
279         List<ArtifactMetadata> metaL = metadataArg.getAllValues();
280         for (ArtifactMetadata meta : metaL) {
281             assertTrue(meta.getId().startsWith(projectName));
282             assertTrue(deletedVersions.contains(meta.getVersion()));
283         }
284
285
286         assertDeleted( versionRoot + "/maven-assembly-plugin-1.1.2-20070427.065136-1.jar" );
287         assertDeleted( versionRoot + "/maven-assembly-plugin-1.1.2-20070427.065136-1.jar.sha1" );
288         assertDeleted( versionRoot + "/maven-assembly-plugin-1.1.2-20070427.065136-1.jar.md5" );
289         assertDeleted( versionRoot + "/maven-assembly-plugin-1.1.2-20070427.065136-1.pom" );
290         assertDeleted( versionRoot + "/maven-assembly-plugin-1.1.2-20070427.065136-1.pom.sha1" );
291         assertDeleted( versionRoot + "/maven-assembly-plugin-1.1.2-20070427.065136-1.pom.md5" );
292
293         // the following should not have been deleted
294         assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070506.163513-2.jar" );
295         assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070506.163513-2.jar.sha1" );
296         assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070506.163513-2.jar.md5" );
297         assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070506.163513-2.pom" );
298         assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070506.163513-2.pom.sha1" );
299         assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070506.163513-2.pom.md5" );
300
301         assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070615.105019-3.jar" );
302         assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070615.105019-3.jar.sha1" );
303         assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070615.105019-3.jar.md5" );
304         assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070615.105019-3.pom" );
305         assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070615.105019-3.pom.sha1" );
306         assertExists( versionRoot + "/maven-assembly-plugin-1.1.2-20070615.105019-3.pom.md5" );
307     }
308 }