]> source.dussan.org Git - archiva.git/blob
fbc25fdef2e634754ef2b3ee543f1d12a908a7bb
[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.io.File;
23 import java.nio.file.Path;
24 import java.text.SimpleDateFormat;
25 import java.util.Calendar;
26 import java.util.Collections;
27 import java.util.HashSet;
28 import java.util.List;
29 import java.util.Set;
30
31 import org.apache.archiva.admin.model.beans.ManagedRepository;
32 import org.apache.archiva.metadata.model.ArtifactMetadata;
33 import org.apache.archiva.repository.events.RepositoryListener;
34 import org.apache.commons.lang.time.DateUtils;
35 import org.junit.After;
36 import org.junit.Test;
37 import org.mockito.ArgumentCaptor;
38
39 import static org.junit.Assert.assertTrue;
40 import static org.mockito.Matchers.eq;
41 import static org.mockito.Mockito.*;
42
43 /**
44  */
45 public class DaysOldRepositoryPurgeTest
46     extends AbstractRepositoryPurgeTest
47 {
48     private static final int OLD_TIMESTAMP = 1179382029;
49
50     private void setLastModified( String dirPath, long lastModified )
51     {
52         File dir = new File( dirPath );
53         File[] contents = dir.listFiles();
54         for ( File content : contents )
55         {
56             content.setLastModified( lastModified );
57         }
58     }
59
60     @After
61     @Override
62     public void tearDown()
63         throws Exception
64     {
65         super.tearDown();
66         repoPurge = null;
67     }
68
69     @Test
70     public void testByLastModified()
71         throws Exception
72     {
73         ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
74         repoPurge = new DaysOldRepositoryPurge( getRepository(), repoConfiguration.getDaysOlder(),
75                                                 repoConfiguration.getRetentionCount(), repositorySession,
76                                                 Collections.singletonList( listener ) );
77
78         String repoRoot = prepareTestRepos();
79         String projectNs = "org.apache.maven.plugins";
80         String projectPath = projectNs.replaceAll("\\.","/");
81         String projectName = "maven-install-plugin";
82         String projectVersion = "2.2-SNAPSHOT";
83         String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
84         Path repo = getTestRepoRootPath();
85         Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
86         Set<String> deletedVersions = new HashSet<>();
87         deletedVersions.add("2.2-SNAPSHOT");
88         deletedVersions.add("2.2-20061118.060401-2");
89
90         setLastModified( projectRoot + "/" + projectVersion + "/", OLD_TIMESTAMP );
91
92         // test listeners for the correct artifacts
93         listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
94                                  "maven-install-plugin", "2.2-SNAPSHOT", "maven-install-plugin-2.2-SNAPSHOT.jar" );
95         listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
96                                  "maven-install-plugin", "2.2-SNAPSHOT", "maven-install-plugin-2.2-SNAPSHOT.pom" );
97         listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
98                                  "maven-install-plugin", "2.2-20061118.060401-2",
99                                  "maven-install-plugin-2.2-20061118.060401-2.jar" );
100         listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
101                                  "maven-install-plugin", "2.2-20061118.060401-2",
102                                  "maven-install-plugin-2.2-20061118.060401-2.pom" );
103         listenerControl.replay();
104
105         // Provide the metadata list
106         List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir );
107         when(metadataRepository.getArtifacts(TEST_REPO_ID, projectNs,
108             projectName, projectVersion)).thenReturn(ml);
109
110         repoPurge.process( PATH_TO_BY_DAYS_OLD_ARTIFACT );
111
112         listenerControl.verify();
113
114         // Verify the metadataRepository invocations
115         verify(metadataRepository, never()).removeProjectVersion(eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion));
116         ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class);
117         verify(metadataRepository, times(2)).removeArtifact(metadataArg.capture(), eq(projectVersion));
118         List<ArtifactMetadata> metaL = metadataArg.getAllValues();
119         for (ArtifactMetadata meta : metaL) {
120             assertTrue(meta.getId().startsWith(projectName));
121             assertTrue(deletedVersions.contains(meta.getVersion()));
122         }
123
124
125         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar" );
126         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.md5" );
127         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.sha1" );
128         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom" );
129         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.md5" );
130         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.sha1" );
131
132         // shouldn't be deleted because even if older than 30 days (because retention count = 2)
133         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar" );
134         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar.md5" );
135         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar.sha1" );
136         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom" );
137         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom.md5" );
138         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom.sha1" );
139
140         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar" );
141         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar.md5" );
142         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar.sha1" );
143         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom" );
144         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom.md5" );
145         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom.sha1" );
146
147         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar" );
148         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.md5" );
149         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.sha1" );
150         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom" );
151         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.md5" );
152         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.sha1" );
153     }
154
155     @Test
156     public void testOrderOfDeletion()
157         throws Exception
158     {
159         ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
160         List<RepositoryListener> listeners = Collections.singletonList( listener );
161         repoPurge = new DaysOldRepositoryPurge( getRepository(), repoConfiguration.getDaysOlder(),
162                                                 repoConfiguration.getRetentionCount(), repositorySession, listeners );
163
164         String repoRoot = prepareTestRepos();
165         String projectNs = "org.apache.maven.plugins";
166         String projectPath = projectNs.replaceAll("\\.","/");
167         String projectName = "maven-assembly-plugin";
168         String projectVersion = "1.1.2-SNAPSHOT";
169         String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
170         Path repo = getTestRepoRootPath();
171         Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
172         Set<String> deletedVersions = new HashSet<>();
173         deletedVersions.add("1.1.2-20070427.065136-1");
174
175         setLastModified( projectRoot + "/" + projectVersion + "/", OLD_TIMESTAMP );
176
177         // test listeners for the correct artifacts
178         listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
179                                  "maven-assembly-plugin", "1.1.2-20070427.065136-1",
180                                  "maven-assembly-plugin-1.1.2-20070427.065136-1.jar" );
181         listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.maven.plugins",
182                                  "maven-assembly-plugin", "1.1.2-20070427.065136-1",
183                                  "maven-assembly-plugin-1.1.2-20070427.065136-1.pom" );
184         listenerControl.replay();
185
186         // Provide the metadata list
187         List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir );
188         when(metadataRepository.getArtifacts(TEST_REPO_ID, projectNs,
189             projectName, projectVersion)).thenReturn(ml);
190
191
192         repoPurge.process( PATH_TO_TEST_ORDER_OF_DELETION );
193
194         listenerControl.verify();
195
196         // Verify the metadataRepository invocations
197         verify(metadataRepository, never()).removeProjectVersion(eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion));
198         ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class);
199         verify(metadataRepository, times(deletedVersions.size())).removeArtifact(metadataArg.capture(), eq(projectVersion));
200         List<ArtifactMetadata> metaL = metadataArg.getAllValues();
201         for (ArtifactMetadata meta : metaL) {
202             assertTrue(meta.getId().startsWith(projectName));
203             assertTrue(deletedVersions.contains(meta.getVersion()));
204         }
205
206
207         assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.jar" );
208         assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.jar.sha1" );
209         assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.jar.md5" );
210         assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.pom" );
211         assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.pom.sha1" );
212         assertDeleted( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070427.065136-1.pom.md5" );
213
214         // the following should not have been deleted
215         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.jar" );
216         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.jar.sha1" );
217         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.jar.md5" );
218         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.pom" );
219         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.pom.sha1" );
220         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070506.163513-2.pom.md5" );
221
222         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.jar" );
223         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.jar.sha1" );
224         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.jar.md5" );
225         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.pom" );
226         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.pom.sha1" );
227         assertExists( projectRoot + "/1.1.2-SNAPSHOT/maven-assembly-plugin-1.1.2-20070615.105019-3.pom.md5" );
228     }
229
230     @Test
231     public void testMetadataDrivenSnapshots()
232         throws Exception
233     {
234         ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
235         List<RepositoryListener> listeners = Collections.singletonList( listener );
236         repoPurge = new DaysOldRepositoryPurge( getRepository(), repoConfiguration.getDaysOlder(),
237                                                 repoConfiguration.getRetentionCount(), repositorySession, listeners );
238
239         String repoRoot = prepareTestRepos();
240         String projectNs = "org.codehaus.plexus";
241         String projectPath = projectNs.replaceAll("\\.","/");
242         String projectName = "plexus-utils";
243         String projectVersion = "1.4.3-SNAPSHOT";
244         String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
245         Path repo = getTestRepoRootPath();
246         Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
247         Set<String> deletedVersions = new HashSet<>();
248         deletedVersions.add("1.4.3-20070113.163208-4");
249
250
251         String versionRoot = projectRoot + "/"+ projectVersion;
252
253         Calendar currentDate = Calendar.getInstance( DateUtils.UTC_TIME_ZONE );
254         setLastModified( versionRoot, currentDate.getTimeInMillis() );
255
256         String timestamp = new SimpleDateFormat( "yyyyMMdd.HHmmss" ).format( currentDate.getTime() );
257
258         for ( int i = 5; i <= 7; i++ )
259         {
260             File jarFile = new File( versionRoot, "/plexus-utils-1.4.3-" + timestamp + "-" + i + ".jar" );
261             jarFile.createNewFile();
262             File pomFile = new File( versionRoot, "/plexus-utils-1.4.3-" + timestamp + "-" + i + ".pom" );
263             pomFile.createNewFile();
264
265             // set timestamp to older than 100 days for the first build, but ensure the filename timestamp is honoured instead
266             if ( i == 5 )
267             {
268                 jarFile.setLastModified( OLD_TIMESTAMP );
269                 pomFile.setLastModified( OLD_TIMESTAMP );
270             }
271         }
272
273         // test listeners for the correct artifacts
274         listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.codehaus.plexus", "plexus-utils",
275                                  "1.4.3-20070113.163208-4", "plexus-utils-1.4.3-20070113.163208-4.jar" );
276         listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.codehaus.plexus", "plexus-utils",
277                                  "1.4.3-20070113.163208-4", "plexus-utils-1.4.3-20070113.163208-4.pom" );
278         listenerControl.replay();
279
280         // Provide the metadata list
281         List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir );
282         when(metadataRepository.getArtifacts(TEST_REPO_ID, projectNs,
283             projectName, projectVersion)).thenReturn(ml);
284
285
286         repoPurge.process( PATH_TO_BY_DAYS_OLD_METADATA_DRIVEN_ARTIFACT );
287
288         listenerControl.verify();
289
290         // Verify the metadataRepository invocations
291         verify(metadataRepository, never()).removeProjectVersion(eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion));
292         ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class);
293         verify(metadataRepository, times(deletedVersions.size())).removeArtifact(metadataArg.capture(), eq(projectVersion));
294         List<ArtifactMetadata> metaL = metadataArg.getAllValues();
295         for (ArtifactMetadata meta : metaL) {
296             assertTrue(meta.getId().startsWith(projectName));
297             assertTrue(deletedVersions.contains(meta.getVersion()));
298         }
299
300
301         // this should be deleted since the filename version (timestamp) is older than
302         // 100 days even if the last modified date was <100 days ago
303         assertDeleted( versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.jar" );
304         assertDeleted( versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.jar.sha1" );
305         assertDeleted( versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.pom" );
306         assertDeleted( versionRoot + "/plexus-utils-1.4.3-20070113.163208-4.pom.sha1" );
307
308         // this should not be deleted because last modified date is <100 days ago
309         assertExists( versionRoot + "/plexus-utils-1.4.3-SNAPSHOT.jar" );
310         assertExists( versionRoot + "/plexus-utils-1.4.3-SNAPSHOT.pom" );
311
312         for ( int i = 5; i <= 7; i++ )
313         {
314             assertExists( versionRoot + "/plexus-utils-1.4.3-" + timestamp + "-" + i + ".jar" );
315             assertExists( versionRoot + "/plexus-utils-1.4.3-" + timestamp + "-" + i + ".pom" );
316         }
317     }
318
319
320 }