]> source.dussan.org Git - archiva.git/blob
3a06774f1877f60d4787c4ceccffb4fec33203ff
[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 org.apache.archiva.metadata.model.ArtifactMetadata;
23 import org.apache.archiva.repository.events.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;
30
31 import java.nio.file.Path;
32 import java.util.Collections;
33 import java.util.HashSet;
34 import java.util.List;
35 import java.util.Set;
36
37 import static org.junit.Assert.assertTrue;
38 import static org.mockito.Matchers.eq;
39 import static org.mockito.Mockito.*;
40
41 /**
42  * Test RetentionsCountRepositoryPurgeTest
43  */
44 public class RetentionCountRepositoryPurgeTest
45     extends AbstractRepositoryPurgeTest
46 {
47     @Before
48     @Override
49     public void setUp()
50         throws Exception
51     {
52         super.setUp();
53
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();
57
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();
66
67         repoPurge = new RetentionCountRepositoryPurge( getRepository(), acf.getRetentionCount(),
68                                                        repositorySession, listeners );
69     }
70
71     @After
72     @Override
73     public void tearDown()
74         throws Exception
75     {
76         super.tearDown();
77     }
78
79     /**
80      * Test if the artifact to be processed was a jar.
81      */
82     @Test
83     public void testIfAJarWasFound()
84         throws Exception
85     {
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;
98
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]);
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.jar"+exts[i]);
109
110
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]);
113         }
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");
116
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");
121
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");
124
125         listenerControl.replay();
126
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);
131
132
133         repoPurge.process( PATH_TO_BY_RETENTION_COUNT_ARTIFACT );
134
135         listenerControl.verify();
136
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()));
145         }
146
147
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" );
155
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" );
162
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" );
170
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" );
177     }
178
179     /**
180      * Test if the artifact to be processed is a pom
181      */
182     @Test
183     public void testIfAPomWasFound()
184         throws Exception
185     {
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;
197
198
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();
213
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);
218
219         repoPurge.process( PATH_TO_BY_RETENTION_COUNT_POM );
220
221         listenerControl.verify();
222
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()));
231         }
232
233
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" );
241
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" );
252
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" );
262     }
263
264     @Test
265     public void testOrderOfDeletion()
266         throws Exception
267     {
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;
279
280
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();
301
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);
306
307         repoPurge.process( PATH_TO_TEST_ORDER_OF_DELETION );
308
309         listenerControl.verify();
310
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()));
319         }
320
321
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" );
328
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" );
336
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" );
343     }
344 }