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