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