1 package org.apache.archiva.consumers.core.repository;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
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;
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;
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.*;
61 @ContextConfiguration (
62 locations = { "classpath*:/META-INF/spring-context.xml", "classpath:/spring-context-purge-consumer-test.xml" } )
63 public class RepositoryPurgeConsumerTest
64 extends AbstractRepositoryPurgeTest
66 private static final Logger log = LoggerFactory.getLogger( RepositoryPurgeConsumerTest.class );
79 public void tearDown()
86 public void testConsumption()
89 assertNotConsumed( "org/apache/maven/plugins/maven-plugin-plugin/2.4.1/maven-metadata.xml" );
94 public void testConsumptionOfOtherMetadata()
97 assertNotConsumed( "org/apache/maven/plugins/maven-plugin-plugin/2.4.1/maven-metadata-central.xml" );
101 private void cleanupFileTypes()
103 ArchivaConfiguration archivaConfiguration =
104 applicationContext.getBean( "archivaConfiguration#default", ArchivaConfiguration.class );
106 FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
107 fileType.removePattern( "**/*.xml" );
110 @SuppressWarnings( "deprecation" )
111 private void assertNotConsumed( String path )
114 ArchivaConfiguration archivaConfiguration =
115 applicationContext.getBean( "archivaConfiguration#default", ArchivaConfiguration.class );
117 FileType fileType = archivaConfiguration.getConfiguration().getRepositoryScanning().getFileTypes().get( 0 );
118 assertEquals( FileTypes.ARTIFACTS, fileType.getId() );
119 fileType.addPattern( "**/*.xml" );
122 //FileTypes fileTypes = applicationContext.getBean( FileTypes.class );
123 for ( FileTypes fileTypes : applicationContext.getBeansOfType( FileTypes.class ).values() )
125 fileTypes.afterConfigurationChange( null, "repositoryScanning.fileTypes", null );
127 KnownRepositoryContentConsumer repoPurgeConsumer =
128 applicationContext.getBean( "knownRepositoryContentConsumer#repository-purge",
129 KnownRepositoryContentConsumer.class );
131 Path repoLocation = Paths.get( "target/test-" + getName() + "/test-repo" );
133 Path localFile = repoLocation.resolve( path );
135 ConsumerWantsFilePredicate predicate = new ConsumerWantsFilePredicate();
136 BaseFile baseFile = new BaseFile( repoLocation.toFile(), localFile.toFile() );
137 predicate.setBasefile( baseFile );
139 assertFalse( predicate.evaluate( repoPurgeConsumer ) );
142 private void setLastModified( String path ) throws IOException
144 Path dir = Paths.get( path );
145 Path[] contents = new Path[0];
148 contents = Files.list( dir ).toArray(Path[]::new);
150 catch ( IOException e )
152 log.error("Could not list files {}: {}", dir, e.getMessage(), e);
153 contents = new Path[0];
155 for ( int i = 0; i < contents.length; i++ )
157 Files.setLastModifiedTime( contents[i], FileTime.fromMillis( 1179382029 ) );
162 public void testConsumerByRetentionCount()
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 );
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();
184 repoPurgeConsumer.beginScan( repoConfiguration, null );
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;
194 Path repo = getTestRepoRootPath();
195 Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
197 // Provide the metadata list
198 List<ArtifactMetadata> ml = getArtifactMetadataFromDir( TEST_REPO_ID, projectName, repo, vDir );
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");
208 repoPurgeConsumer.processFile( PATH_TO_BY_RETENTION_COUNT_ARTIFACT );
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()));
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" );
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" );
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" );
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" );
258 removeRepoFromConfiguration( "retention-count", repoConfiguration );
261 private void addRepoToConfiguration( String configHint, org.apache.archiva.repository.ManagedRepository repoConfiguration )
264 RepositoryRegistry repositoryRegistry = applicationContext.getBean( ArchivaRepositoryRegistry.class);
265 repositoryRegistry.putRepository( repoConfiguration );
268 private void removeRepoFromConfiguration( String configHint, org.apache.archiva.repository.ManagedRepository repoConfiguration )
271 RepositoryRegistry repositoryRegistry = applicationContext.getBean( ArchivaRepositoryRegistry.class);
272 repositoryRegistry.removeRepository( repoConfiguration );
276 public void testConsumerByDaysOld()
279 RepositoryPurgeConsumer repoPurgeConsumer =
280 applicationContext.getBean( "knownRepositoryContentConsumer#repo-purge-consumer-by-days-old",
281 RepositoryPurgeConsumer.class );
283 repoPurgeConsumer.setRepositorySessionFactory( sessionFactory );
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 );
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 );
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;
307 setLastModified( projectRoot + "/"+projectVersion);
310 Path repo = getTestRepoRootPath();
311 Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
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");
321 repoPurgeConsumer.processFile( PATH_TO_BY_DAYS_OLD_ARTIFACT );
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()));
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" );
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" );
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" );
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" );
363 removeRepoFromConfiguration( "days-old", repoConfiguration );
367 * Test the snapshot clean consumer on a repository set to NOT clean/delete snapshots based on released versions.
372 public void testReleasedSnapshotsWereNotCleaned()
375 KnownRepositoryContentConsumer repoPurgeConsumer =
376 applicationContext.getBean( "knownRepositoryContentConsumer#repo-purge-consumer-by-retention-count",
377 KnownRepositoryContentConsumer.class );
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 );
384 repoPurgeConsumer.beginScan( repoConfiguration, null );
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;
393 Path repo = getTestRepoRootPath();
394 Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion);
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);
401 repoPurgeConsumer.processFile(
402 CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO );
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) );
409 // check if the snapshot wasn't removed
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" );
419 // check if metadata file wasn't updated
420 Path artifactMetadataFile = Paths.get( projectRoot + "/maven-metadata.xml" );
422 String metadataXml = org.apache.archiva.common.utils.FileUtils.readFileToString( artifactMetadataFile, Charset.defaultCharset() );
424 String expectedVersions = "<expected><versions><version>2.3-SNAPSHOT</version></versions></expected>";
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 );
431 removeRepoFromConfiguration( "retention-count", repoConfiguration );
435 public void testReleasedSnapshotsWereCleaned()
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 );
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 );
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);
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);
471 repoPurgeConsumer.processFile(
472 CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO );
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() );
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" );
487 // check if metadata file was updated
488 Path artifactMetadataFile = Paths.get( projectRoot + "/maven-metadata.xml" );
490 String metadataXml = org.apache.archiva.common.utils.FileUtils.readFileToString( artifactMetadataFile, Charset.defaultCharset() );
492 String expectedVersions =
493 "<expected><versions><version>2.2</version>" + "<version>2.3</version></versions></expected>";
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 );
500 removeRepoFromConfiguration( "days-old", repoConfiguration );