]> source.dussan.org Git - archiva.git/blob
453b90becf56c3a593e28430d6f081068287be09
[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.common.utils.BaseFile;
23 import org.apache.archiva.configuration.ArchivaConfiguration;
24 import org.apache.archiva.configuration.FileType;
25 import org.apache.archiva.configuration.FileTypes;
26 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
27 import org.apache.archiva.consumers.functors.ConsumerWantsFilePredicate;
28 import org.apache.archiva.metadata.model.ArtifactMetadata;
29 import org.apache.archiva.metadata.model.MetadataFacet;
30 import org.apache.archiva.repository.base.ArchivaRepositoryRegistry;
31 import org.apache.archiva.repository.RepositoryRegistry;
32 import org.apache.archiva.repository.features.ArtifactCleanupFeature;
33 import org.custommonkey.xmlunit.XMLAssert;
34 import org.easymock.EasyMock;
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.ArgumentCaptor;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.test.context.ContextConfiguration;
42
43 import java.io.IOException;
44 import java.nio.charset.Charset;
45 import java.nio.file.Files;
46 import java.nio.file.Path;
47 import java.nio.file.Paths;
48 import java.nio.file.attribute.FileTime;
49 import java.time.Period;
50 import java.util.HashSet;
51 import java.util.List;
52 import java.util.Set;
53
54 import static org.junit.Assert.*;
55 import static org.mockito.Matchers.any;
56 import static org.mockito.Matchers.eq;
57 import static org.mockito.Mockito.*;
58
59 /**
60  */
61 @ContextConfiguration (
62     locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context-purge-consumer-test.xml" } )
63 public class RepositoryPurgeConsumerTest
64     extends AbstractRepositoryPurgeTest
65 {
66     private static final Logger log = LoggerFactory.getLogger( RepositoryPurgeConsumerTest.class );
67
68     @Before
69     @Override
70     public void setUp()
71         throws Exception
72     {
73         super.setUp();
74
75     }
76
77     @After
78     @Override
79     public void tearDown()
80         throws Exception
81     {
82         super.tearDown();
83     }
84
85     @Test
86     public void testConsumption()
87         throws Exception
88     {
89         assertNotConsumed( "org/apache/maven/plugins/maven-plugin-plugin/2.4.1/maven-metadata.xml" );
90         cleanupFileTypes();
91     }
92
93     @Test
94     public void testConsumptionOfOtherMetadata()
95         throws Exception
96     {
97         assertNotConsumed( "org/apache/maven/plugins/maven-plugin-plugin/2.4.1/maven-metadata-central.xml" );
98         cleanupFileTypes();
99     }
100
101     private void cleanupFileTypes()
102     {
103         ArchivaConfiguration archivaConfiguration =
104             applicationContext.getBean( "archivaConfiguration#default", ArchivaConfiguration.class );
105
106         FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
107         fileType.removePattern( "**/*.xml" );
108     }
109
110     @SuppressWarnings( "deprecation" )
111     private void assertNotConsumed( String path )
112         throws Exception
113     {
114         ArchivaConfiguration archivaConfiguration =
115             applicationContext.getBean( "archivaConfiguration#default", ArchivaConfiguration.class );
116
117         FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
118         assertEquals( FileTypes.ARTIFACTS, fileType.getId() );
119         fileType.addPattern( "**/*.xml" );
120
121         // trigger reload
122         //FileTypes fileTypes = applicationContext.getBean( FileTypes.class );
123         for ( FileTypes fileTypes : applicationContext.getBeansOfType( FileTypes.class ).values() )
124         {
125             fileTypes.afterConfigurationChange( null, "repositoryScanning.fileTypes", null );
126         }
127         KnownRepositoryContentConsumer repoPurgeConsumer =
128             applicationContext.getBean( "knownRepositoryContentConsumer#repository-purge",
129                                         KnownRepositoryContentConsumer.class );
130
131         Path repoLocation = Paths.get( "target/test-" + getName() + "/test-repo" );
132
133         Path localFile = repoLocation.resolve( path );
134
135         ConsumerWantsFilePredicate predicate = new ConsumerWantsFilePredicate();
136         BaseFile baseFile = new BaseFile( repoLocation.toFile(), localFile.toFile() );
137         predicate.setBasefile( baseFile );
138
139         assertFalse( predicate.evaluate( repoPurgeConsumer ) );
140     }
141
142     private void setLastModified( String path ) throws IOException
143     {
144         Path dir = Paths.get( path );
145         Path[] contents = new Path[0];
146         try
147         {
148             contents = Files.list( dir ).toArray(Path[]::new);
149         }
150         catch ( IOException e )
151         {
152             log.error("Could not list files {}: {}", dir, e.getMessage(), e);
153             contents = new Path[0];
154         }
155         for ( int i = 0; i < contents.length; i++ )
156         {
157             Files.setLastModifiedTime( contents[i], FileTime.fromMillis( 1179382029 ) );
158         }
159     }
160
161     @Test
162     public void testConsumerByRetentionCount()
163         throws Exception
164     {
165         RepositoryPurgeConsumer repoPurgeConsumer =
166             applicationContext.getBean( "knownRepositoryContentConsumer#repo-purge-consumer-by-retention-count",
167                                         RepositoryPurgeConsumer.class );
168         repoPurgeConsumer.setRepositorySessionFactory( sessionFactory );
169         org.apache.archiva.repository.ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
170         ArtifactCleanupFeature atf = repoConfiguration.getFeature( ArtifactCleanupFeature.class ).get();
171         atf.setRetentionPeriod( Period.ofDays( 0 ) ); // force days older off to allow retention count purge to execute.
172         atf.setRetentionCount( TEST_RETENTION_COUNT );
173         addRepoToConfiguration( "retention-count", repoConfiguration );
174
175         sessionControl.reset();
176         sessionFactoryControl.reset();
177         EasyMock.expect( sessionFactory.createSession( ) ).andStubReturn( repositorySession );
178         EasyMock.expect( repositorySession.getRepository()).andStubReturn( metadataRepository );
179         repositorySession.save();
180         EasyMock.expectLastCall().anyTimes();
181         sessionFactoryControl.replay();
182         sessionControl.replay();
183
184         repoPurgeConsumer.beginScan( repoConfiguration, null );
185
186         String repoRoot = prepareTestRepos();
187         String projectNs = "org.jruby.plugins";
188         String projectPath = projectNs.replaceAll("\\.","/");
189         String projectName = "jruby-rake-plugin";
190         String projectVersion = "1.0RC1-SNAPSHOT";
191         String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
192         String versionRoot = projectRoot + "/" + projectVersion;
193
194         Path repo = getTestRepoRootPath();
195         Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
196
197         // Provide the metadata list
198         List<ArtifactMetadata> ml = getArtifactMetadataFromDir( TEST_REPO_ID, projectName, repo, vDir );
199
200
201
202         when(metadataRepository.getArtifacts( repositorySession, TEST_REPO_ID,
203             projectNs, projectName, projectVersion )).thenReturn(ml);
204         Set<String> deletedVersions = new HashSet<>();
205         deletedVersions.add("1.0RC1-20070504.153317-1");
206         deletedVersions.add("1.0RC1-20070504.160758-2");
207
208         repoPurgeConsumer.processFile( PATH_TO_BY_RETENTION_COUNT_ARTIFACT );
209
210         // Verify the metadataRepository invocations
211         verify(metadataRepository, never()).removeProjectVersion( eq(repositorySession), eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) );
212         ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class);
213         verify(metadataRepository, times(2)).removeTimestampedArtifact( eq(repositorySession), metadataArg.capture(), eq(projectVersion) );
214         List<ArtifactMetadata> metaL = metadataArg.getAllValues();
215         for (ArtifactMetadata meta : metaL) {
216             assertTrue(meta.getId().startsWith(projectName));
217             assertTrue(deletedVersions.contains(meta.getVersion()));
218         }
219
220
221
222         // assert if removed from repo
223         assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar" );
224         assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1-javadoc.jar" );
225         assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1-javadoc.zip" );
226         assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar.md5" );
227         assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.jar.sha1" );
228         assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom" );
229         assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom.md5" );
230         assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.153317-1.pom.sha1" );
231
232         assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar" );
233         assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2-javadoc.jar" );
234         assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2-javadoc.zip" );
235         assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar.md5" );
236         assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.jar.sha1" );
237         assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom" );
238         assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom.md5" );
239         assertDeleted( versionRoot + "/jruby-rake-plugin-1.0RC1-20070504.160758-2.pom.sha1" );
240
241         // assert if not removed from repo
242         assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar" );
243         assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3-javadoc.jar" );
244         assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3-javadoc.zip" );
245         assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar.md5" );
246         assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.jar.sha1" );
247         assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom" );
248         assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom.md5" );
249         assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070505.090015-3.pom.sha1" );
250
251         assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar" );
252         assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar.md5" );
253         assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.jar.sha1" );
254         assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom" );
255         assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom.md5" );
256         assertExists( versionRoot + "/jruby-rake-plugin-1.0RC1-20070506.090132-4.pom.sha1" );
257
258         removeRepoFromConfiguration( "retention-count", repoConfiguration );
259     }
260
261     private void addRepoToConfiguration( String configHint, org.apache.archiva.repository.ManagedRepository repoConfiguration )
262         throws Exception
263     {
264         RepositoryRegistry repositoryRegistry = applicationContext.getBean( ArchivaRepositoryRegistry.class);
265         repositoryRegistry.putRepository( repoConfiguration );
266     }
267
268     private void removeRepoFromConfiguration( String configHint, org.apache.archiva.repository.ManagedRepository repoConfiguration )
269         throws Exception
270     {
271         RepositoryRegistry repositoryRegistry = applicationContext.getBean( ArchivaRepositoryRegistry.class);
272         repositoryRegistry.removeRepository( repoConfiguration );
273     }
274
275     @Test
276     public void testConsumerByDaysOld()
277         throws Exception
278     {
279         RepositoryPurgeConsumer repoPurgeConsumer =
280             applicationContext.getBean( "knownRepositoryContentConsumer#repo-purge-consumer-by-days-old",
281                 RepositoryPurgeConsumer.class );
282
283         repoPurgeConsumer.setRepositorySessionFactory( sessionFactory );
284
285         org.apache.archiva.repository.ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
286         ArtifactCleanupFeature atf = repoConfiguration.getFeature( ArtifactCleanupFeature.class ).get();
287         atf.setRetentionPeriod( Period.ofDays( TEST_DAYS_OLDER ) );
288         addRepoToConfiguration( "days-old", repoConfiguration );
289
290         sessionControl.reset();
291         sessionFactoryControl.reset();
292         EasyMock.expect( sessionFactory.createSession( ) ).andStubReturn( repositorySession );
293         EasyMock.expect( repositorySession.getRepository()).andStubReturn( metadataRepository );
294         repositorySession.save();
295         EasyMock.expectLastCall().anyTimes();
296         sessionFactoryControl.replay();
297         sessionControl.replay();
298         repoPurgeConsumer.beginScan( repoConfiguration, null );
299
300         String repoRoot = prepareTestRepos();
301         String projectNs = "org.apache.maven.plugins";
302         String projectPath = projectNs.replaceAll("\\.","/");
303         String projectName = "maven-install-plugin";
304         String projectVersion = "2.2-SNAPSHOT";
305         String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
306
307         setLastModified( projectRoot + "/"+projectVersion);
308
309
310         Path repo = getTestRepoRootPath();
311         Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
312
313         // Provide the metadata list
314         List<ArtifactMetadata> ml = getArtifactMetadataFromDir( TEST_REPO_ID, projectName, repo, vDir );
315         when(metadataRepository.getArtifacts( repositorySession, TEST_REPO_ID,
316             projectNs, projectName, projectVersion )).thenReturn(ml);
317         Set<String> deletedVersions = new HashSet<>();
318         deletedVersions.add("2.2-SNAPSHOT");
319         deletedVersions.add("2.2-20061118.060401-2");
320
321         repoPurgeConsumer.processFile( PATH_TO_BY_DAYS_OLD_ARTIFACT );
322
323         // Verify the metadataRepository invocations
324         verify(metadataRepository, never()).removeProjectVersion( eq(repositorySession), eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) );
325         ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class);
326         verify(metadataRepository, times(2)).removeTimestampedArtifact( eq(repositorySession), metadataArg.capture(), eq(projectVersion) );
327         List<ArtifactMetadata> metaL = metadataArg.getAllValues();
328         assertTrue( metaL.size( ) > 0 );
329         for (ArtifactMetadata meta : metaL) {
330             assertTrue(meta.getId().startsWith(projectName));
331             assertTrue(deletedVersions.contains(meta.getVersion()));
332         }
333
334         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar" );
335         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.md5" );
336         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.jar.sha1" );
337         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom" );
338         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.md5" );
339         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-SNAPSHOT.pom.sha1" );
340
341         // shouldn't be deleted because even if older than 30 days (because retention count = 2)
342         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar" );
343         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar.md5" );
344         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.jar.sha1" );
345         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom" );
346         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom.md5" );
347         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070513.034619-5.pom.sha1" );
348
349         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar" );
350         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar.md5" );
351         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.jar.sha1" );
352         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom" );
353         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom.md5" );
354         assertExists( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20070510.010101-4.pom.sha1" );
355
356         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar" );
357         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.md5" );
358         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.jar.sha1" );
359         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom" );
360         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.md5" );
361         assertDeleted( projectRoot + "/2.2-SNAPSHOT/maven-install-plugin-2.2-20061118.060401-2.pom.sha1" );
362
363         removeRepoFromConfiguration( "days-old", repoConfiguration );
364     }
365
366     /**
367      * Test the snapshot clean consumer on a repository set to NOT clean/delete snapshots based on released versions.
368      *
369      * @throws Exception
370      */
371     @Test
372     public void testReleasedSnapshotsWereNotCleaned()
373         throws Exception
374     {
375         KnownRepositoryContentConsumer repoPurgeConsumer =
376             applicationContext.getBean( "knownRepositoryContentConsumer#repo-purge-consumer-by-retention-count",
377                                         KnownRepositoryContentConsumer.class );
378
379         org.apache.archiva.repository.ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
380         ArtifactCleanupFeature acf = repoConfiguration.getFeature( ArtifactCleanupFeature.class ).get();
381         acf.setDeleteReleasedSnapshots( false ); // Set to NOT delete released snapshots.
382         addRepoToConfiguration( "retention-count", repoConfiguration );
383
384         repoPurgeConsumer.beginScan( repoConfiguration, null );
385
386         String repoRoot = prepareTestRepos();
387         String projectNs = "org.apache.maven.plugins";
388         String projectPath = projectNs.replaceAll("\\.","/");
389         String projectName = "maven-plugin-plugin";
390         String projectVersion = "2.3-SNAPSHOT";
391         String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
392
393         Path repo = getTestRepoRootPath();
394         Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
395
396         // Provide the metadata list
397         List<ArtifactMetadata> ml = getArtifactMetadataFromDir( TEST_REPO_ID, projectName, repo, vDir );
398         when(metadataRepository.getArtifacts( repositorySession, TEST_REPO_ID,
399             projectNs, projectName, projectVersion )).thenReturn(ml);
400
401         repoPurgeConsumer.processFile(
402             CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO );
403
404         verify(metadataRepository, never()).removeProjectVersion( eq(repositorySession), eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) );
405         ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class);
406         verify(metadataRepository, never()).removeTimestampedArtifact( eq(repositorySession), any(), any() );
407         verify(metadataRepository, never()).removeFacetFromArtifact( eq(repositorySession), any(), any(), any(), any(), any(MetadataFacet.class) );
408
409         // check if the snapshot wasn't removed
410
411         assertExists( projectRoot + "/2.3-SNAPSHOT" );
412         assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
413         assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
414         assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
415         assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
416         assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
417         assertExists( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
418
419         // check if metadata file wasn't updated
420         Path artifactMetadataFile = Paths.get( projectRoot + "/maven-metadata.xml" );
421
422         String metadataXml = org.apache.archiva.common.utils.FileUtils.readFileToString( artifactMetadataFile, Charset.defaultCharset() );
423
424         String expectedVersions = "<expected><versions><version>2.3-SNAPSHOT</version></versions></expected>";
425
426         XMLAssert.assertXpathEvaluatesTo( "2.3-SNAPSHOT", "//metadata/versioning/latest", metadataXml );
427         XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
428                                      "//metadata/versioning/versions/version", metadataXml );
429         XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
430
431         removeRepoFromConfiguration( "retention-count", repoConfiguration );
432     }
433
434     @Test
435     public void testReleasedSnapshotsWereCleaned()
436         throws Exception
437     {
438         RepositoryPurgeConsumer repoPurgeConsumer =
439             applicationContext.getBean( "knownRepositoryContentConsumer#repo-purge-consumer-by-days-old",
440                                         RepositoryPurgeConsumer.class );
441         repoPurgeConsumer.setRepositorySessionFactory( sessionFactory );
442         org.apache.archiva.repository.ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME );
443         ArtifactCleanupFeature acf = repoConfiguration.getFeature( ArtifactCleanupFeature.class ).get();
444         acf.setDeleteReleasedSnapshots( true );
445         addRepoToConfiguration( "days-old", repoConfiguration );
446
447         sessionControl.reset();
448         sessionFactoryControl.reset();
449         EasyMock.expect( sessionFactory.createSession( ) ).andStubReturn( repositorySession );
450         EasyMock.expect( repositorySession.getRepository()).andStubReturn( metadataRepository );
451         repositorySession.save();
452         EasyMock.expectLastCall().anyTimes();
453         sessionFactoryControl.replay();
454         sessionControl.replay();
455         repoPurgeConsumer.beginScan( repoConfiguration, null );
456
457         String repoRoot = prepareTestRepos();
458         String projectNs = "org.apache.maven.plugins";
459         String projectPath = projectNs.replaceAll("\\.","/");
460         String projectName = "maven-plugin-plugin";
461         String projectVersion = "2.3-SNAPSHOT";
462         String projectRoot = repoRoot + "/" + projectPath+"/"+projectName;
463         Path repo = getTestRepoRootPath();
464         Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
465
466         // Provide the metadata list
467         List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir );
468         when(metadataRepository.getArtifacts( repositorySession, TEST_REPO_ID,
469             projectNs, projectName, projectVersion )).thenReturn(ml);
470
471         repoPurgeConsumer.processFile(
472             CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO );
473
474         verify(metadataRepository, times(1)).removeProjectVersion( eq(repositorySession), eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) );
475         ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class);
476         verify(metadataRepository, never()).removeTimestampedArtifact( eq(repositorySession), any(), any() );
477
478         // check if the snapshot was removed
479         assertDeleted( projectRoot + "/2.3-SNAPSHOT" );
480         assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar" );
481         assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.md5" );
482         assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.jar.sha1" );
483         assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom" );
484         assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.md5" );
485         assertDeleted( projectRoot + "/2.3-SNAPSHOT/maven-plugin-plugin-2.3-SNAPSHOT.pom.sha1" );
486
487         // check if metadata file was updated
488         Path artifactMetadataFile = Paths.get( projectRoot + "/maven-metadata.xml" );
489
490         String metadataXml = org.apache.archiva.common.utils.FileUtils.readFileToString( artifactMetadataFile, Charset.defaultCharset() );
491
492         String expectedVersions =
493             "<expected><versions><version>2.2</version>" + "<version>2.3</version></versions></expected>";
494
495         XMLAssert.assertXpathEvaluatesTo( "2.3", "//metadata/versioning/latest", metadataXml );
496         XMLAssert.assertXpathsEqual( "//expected/versions/version", expectedVersions,
497                                      "//metadata/versioning/versions/version", metadataXml );
498         XMLAssert.assertXpathEvaluatesTo( "20070315032817", "//metadata/versioning/lastUpdated", metadataXml );
499
500         removeRepoFromConfiguration( "days-old", repoConfiguration );
501     }
502 }