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