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