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