diff options
author | Martin Stockhammer <martin_s@apache.org> | 2019-08-16 22:36:09 +0200 |
---|---|---|
committer | Martin Stockhammer <martin_s@apache.org> | 2019-08-16 22:36:09 +0200 |
commit | 118604a706f1fd66bcda450c1ac8370eff60e4cf (patch) | |
tree | b181604dbf884a82697e987500ceb2ed81e24f28 /archiva-modules/archiva-base | |
parent | e4f38c2fbe1f6ce82da98ca9d67d6cc41757f833 (diff) | |
download | archiva-118604a706f1fd66bcda450c1ac8370eff60e4cf.tar.gz archiva-118604a706f1fd66bcda450c1ac8370eff60e4cf.zip |
Fixing unit tests
Diffstat (limited to 'archiva-modules/archiva-base')
9 files changed, 167 insertions, 73 deletions
diff --git a/archiva-modules/archiva-base/archiva-configuration/src/main/java/org/apache/archiva/configuration/FileTypes.java b/archiva-modules/archiva-base/archiva-configuration/src/main/java/org/apache/archiva/configuration/FileTypes.java index c5d8931ee..1b263edb9 100644 --- a/archiva-modules/archiva-base/archiva-configuration/src/main/java/org/apache/archiva/configuration/FileTypes.java +++ b/archiva-modules/archiva-base/archiva-configuration/src/main/java/org/apache/archiva/configuration/FileTypes.java @@ -62,6 +62,11 @@ public class FileTypes @Named(value = "archivaConfiguration#default") private ArchivaConfiguration archivaConfiguration; + + public FileTypes() { + + } + /** * Map of default values for the file types. */ diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/main/resources/archetype-resources/src/main/java/SimpleArtifactConsumer.java b/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/main/resources/archetype-resources/src/main/java/SimpleArtifactConsumer.java index 5c5f45f21..c8bd82371 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/main/resources/archetype-resources/src/main/java/SimpleArtifactConsumer.java +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/main/resources/archetype-resources/src/main/java/SimpleArtifactConsumer.java @@ -25,6 +25,7 @@ import org.apache.archiva.consumers.AbstractMonitoredConsumer; import org.apache.archiva.consumers.ConsumerException; import org.apache.archiva.consumers.KnownRepositoryContentConsumer; import org.apache.archiva.metadata.repository.MetadataResolutionException; +import org.apache.archiva.metadata.repository.MetadataRepositoryException; import org.apache.archiva.metadata.repository.RepositorySession; import org.apache.archiva.metadata.repository.RepositorySessionFactory; import org.apache.archiva.model.ArtifactReference; @@ -100,7 +101,13 @@ public class SimpleArtifactConsumer this.repository = repository; log.info( "Beginning scan of repository [{}]", this.repository.getId() ); - repositorySession = repositorySessionFactory.createSession(); + try + { + repositorySession = repositorySessionFactory.createSession( ); + } catch (MetadataRepositoryException e) { + log.error("Could not create repository session {}", e.getMessage()); + throw new ConsumerException( "Could not create repository session: " + e.getMessage( ), e ); + } } public void processFile( String path ) @@ -119,10 +126,10 @@ public class SimpleArtifactConsumer ManagedRepositoryContent repositoryContent = repository.getContent(); ArtifactReference artifact = repositoryContent.toArtifactReference( path ); - repositorySession.getRepository().getArtifacts( repository.getId(), artifact.getGroupId(), + repositorySession.getRepository().getArtifacts( repositorySession, repository.getId(), artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion() ); } - catch ( LayoutException | MetadataResolutionException e ) + catch ( LayoutException | MetadataResolutionException e ) { throw new ConsumerException( e.getLocalizedMessage(), e ); } diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/archiva/consumers/core/repository/RepositoryPurgeConsumer.java b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/archiva/consumers/core/repository/RepositoryPurgeConsumer.java index 6c153f215..536e1fd46 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/archiva/consumers/core/repository/RepositoryPurgeConsumer.java +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/archiva/consumers/core/repository/RepositoryPurgeConsumer.java @@ -143,7 +143,7 @@ public class RepositoryPurgeConsumer } catch ( org.apache.archiva.metadata.repository.MetadataRepositoryException e ) { - e.printStackTrace( ); + throw new ConsumerException( "Could not create session: " + e.getMessage( ), e ); } if (repository.supportsFeature( ArtifactCleanupFeature.class )) @@ -332,13 +332,4 @@ public class RepositoryPurgeConsumer this.repositorySessionFactory = repositorySessionFactory; } - public RepositorySession getRepositorySession( ) - { - return repositorySession; - } - - public void setRepositorySession( RepositorySession repositorySession ) - { - this.repositorySession = repositorySession; - } } diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/AbstractRepositoryPurgeTest.java b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/AbstractRepositoryPurgeTest.java index 613ec3054..8f6172bf5 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/AbstractRepositoryPurgeTest.java +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/AbstractRepositoryPurgeTest.java @@ -22,6 +22,7 @@ package org.apache.archiva.consumers.core.repository; import org.apache.archiva.metadata.model.ArtifactMetadata; import org.apache.archiva.metadata.repository.MetadataRepository; import org.apache.archiva.metadata.repository.RepositorySession; +import org.apache.archiva.metadata.repository.RepositorySessionFactory; import org.apache.archiva.metadata.repository.storage.maven2.Maven2RepositoryPathTranslator; import org.apache.archiva.repository.BasicManagedRepository; import org.apache.archiva.repository.ManagedRepositoryContent; @@ -101,8 +102,12 @@ public abstract class AbstractRepositoryPurgeTest protected RepositoryListener listener; + protected IMocksControl sessionControl; protected RepositorySession repositorySession; + protected IMocksControl sessionFactoryControl; + protected RepositorySessionFactory sessionFactory; + protected MetadataRepository metadataRepository; @Inject @@ -118,10 +123,14 @@ public abstract class AbstractRepositoryPurgeTest listener = listenerControl.createMock( RepositoryListener.class ); - repositorySession = mock( RepositorySession.class ); - metadataRepository = mock( MetadataRepository.class ); - when( repositorySession.getRepository() ).thenReturn( metadataRepository ); + sessionControl = EasyMock.createControl(); + sessionFactoryControl = EasyMock.createControl( ); + repositorySession = sessionControl.createMock( RepositorySession.class ); + metadataRepository = mock( MetadataRepository.class ); + sessionFactory = sessionFactoryControl.createMock( RepositorySessionFactory.class ); + EasyMock.expect( repositorySession.getRepository() ).andStubReturn( metadataRepository ); + EasyMock.expect( sessionFactory.createSession( ) ).andStubReturn( repositorySession ); } diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/CleanupReleasedSnapshotsRepositoryPurgeTest.java b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/CleanupReleasedSnapshotsRepositoryPurgeTest.java index fdd86cb94..9d44b28b7 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/CleanupReleasedSnapshotsRepositoryPurgeTest.java +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/CleanupReleasedSnapshotsRepositoryPurgeTest.java @@ -91,6 +91,15 @@ public class CleanupReleasedSnapshotsRepositoryPurgeTest listener = listenerControl.createMock( RepositoryListener.class ); List<RepositoryListener> listeners = Collections.singletonList( listener ); + + sessionControl.reset(); + sessionFactoryControl.reset(); + EasyMock.expect( sessionFactory.createSession( ) ).andStubReturn( repositorySession ); + EasyMock.expect( repositorySession.getRepository()).andStubReturn( metadataRepository ); + repositorySession.save(); + EasyMock.expectLastCall().anyTimes(); + sessionFactoryControl.replay(); + sessionControl.replay(); repoPurge = new CleanupReleasedSnapshotsRepositoryPurge( getRepository(), metadataTools, applicationContext.getBean( RepositoryRegistry.class ), @@ -137,8 +146,8 @@ public class CleanupReleasedSnapshotsRepositoryPurgeTest // Verify the metadataRepository invocations // complete snapshot version removal for released - verify(metadataRepository, times(1)).removeProjectVersion(repositorySession , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) ); - verify(metadataRepository, never()).removeProjectVersion(repositorySession , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq("2.3") ); + verify(metadataRepository, times(1)).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) ); + verify(metadataRepository, never()).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq("2.3") ); // check if the snapshot was removed assertDeleted( projectRoot + "/2.3-SNAPSHOT" ); @@ -260,8 +269,8 @@ public class CleanupReleasedSnapshotsRepositoryPurgeTest // Verify the metadataRepository invocations // Complete version removal for cleanup - verify(metadataRepository, times(1)).removeProjectVersion(repositorySession , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) ); - verify(metadataRepository, never()).removeProjectVersion(repositorySession , eq(RELEASES_TEST_REPO_ID), eq(projectNs), eq(projectName), eq(releaseVersion) ); + verify(metadataRepository, times(1)).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) ); + verify(metadataRepository, never()).removeProjectVersion(eq(repositorySession) , eq(RELEASES_TEST_REPO_ID), eq(projectNs), eq(projectName), eq(releaseVersion) ); // check if the snapshot was removed @@ -333,11 +342,11 @@ public class CleanupReleasedSnapshotsRepositoryPurgeTest // Verify the metadataRepository invocations // No removal - verify(metadataRepository, never()).removeProjectVersion(repositorySession , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) ); - verify(metadataRepository, never()).removeProjectVersion(repositorySession , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq("2.0.3-SNAPSHOT") ); - verify(metadataRepository, never()).removeProjectVersion(repositorySession , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq("2.0.4-SNAPSHOT") ); - verify(metadataRepository, never()).removeArtifact(repositorySession , any(ArtifactMetadata.class), any(String.class) ); - verify(metadataRepository, never()).removeArtifact(repositorySession , any(String.class), any(String.class), any(String.class), any(String.class), any( MetadataFacet.class) ); + verify(metadataRepository, never()).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) ); + verify(metadataRepository, never()).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq("2.0.3-SNAPSHOT") ); + verify(metadataRepository, never()).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq("2.0.4-SNAPSHOT") ); + verify(metadataRepository, never()).removeArtifact(eq(repositorySession) , any(ArtifactMetadata.class), any(String.class) ); + verify(metadataRepository, never()).removeArtifact(eq(repositorySession) , any(String.class), any(String.class), any(String.class), any(String.class), any( MetadataFacet.class) ); diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/DaysOldRepositoryPurgeTest.java b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/DaysOldRepositoryPurgeTest.java index 651e90a3c..848d4eda6 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/DaysOldRepositoryPurgeTest.java +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/DaysOldRepositoryPurgeTest.java @@ -23,6 +23,7 @@ import org.apache.archiva.metadata.model.ArtifactMetadata; import org.apache.archiva.repository.events.RepositoryListener; import org.apache.archiva.repository.features.ArtifactCleanupFeature; import org.apache.commons.lang.time.DateUtils; +import org.easymock.EasyMock; import org.junit.After; import org.junit.Test; import org.mockito.ArgumentCaptor; @@ -75,6 +76,17 @@ public class DaysOldRepositoryPurgeTest { org.apache.archiva.repository.ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ); ArtifactCleanupFeature atf = repoConfiguration.getFeature( ArtifactCleanupFeature.class ).get(); + + + sessionControl.reset(); + sessionFactoryControl.reset(); + EasyMock.expect( sessionFactory.createSession( ) ).andStubReturn( repositorySession ); + EasyMock.expect( repositorySession.getRepository()).andStubReturn( metadataRepository ); + repositorySession.save(); + EasyMock.expectLastCall().anyTimes(); + sessionFactoryControl.replay(); + sessionControl.replay(); + repoPurge = new DaysOldRepositoryPurge( getRepository(), atf.getRetentionPeriod().getDays(), atf.getRetentionCount(), repositorySession, Collections.singletonList( listener ) ); @@ -120,9 +132,9 @@ public class DaysOldRepositoryPurgeTest listenerControl.verify(); // Verify the metadataRepository invocations - verify(metadataRepository, never()).removeProjectVersion(repositorySession , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) ); + verify(metadataRepository, never()).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) ); ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class); - verify(metadataRepository, times(2)).removeArtifact(repositorySession , metadataArg.capture(), eq(projectVersion) ); + verify(metadataRepository, times(2)).removeArtifact(eq(repositorySession) , metadataArg.capture(), eq(projectVersion) ); List<ArtifactMetadata> metaL = metadataArg.getAllValues(); for (ArtifactMetadata meta : metaL) { assertTrue(meta.getId().startsWith(projectName)); @@ -167,6 +179,15 @@ public class DaysOldRepositoryPurgeTest org.apache.archiva.repository.ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ); ArtifactCleanupFeature atf = repoConfiguration.getFeature( ArtifactCleanupFeature.class ).get(); List<RepositoryListener> listeners = Collections.singletonList( listener ); + + sessionControl.reset(); + sessionFactoryControl.reset(); + EasyMock.expect( sessionFactory.createSession( ) ).andStubReturn( repositorySession ); + EasyMock.expect( repositorySession.getRepository()).andStubReturn( metadataRepository ); + repositorySession.save(); + EasyMock.expectLastCall().anyTimes(); + sessionFactoryControl.replay(); + sessionControl.replay(); repoPurge = new DaysOldRepositoryPurge( getRepository(), atf.getRetentionPeriod().getDays(), atf.getRetentionCount(), repositorySession, listeners ); @@ -206,9 +227,9 @@ public class DaysOldRepositoryPurgeTest listenerControl.verify(); // Verify the metadataRepository invocations - verify(metadataRepository, never()).removeProjectVersion(repositorySession , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) ); + verify(metadataRepository, never()).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) ); ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class); - verify(metadataRepository, times(deletedVersions.size())).removeArtifact(repositorySession , metadataArg.capture(), eq(projectVersion) ); + verify(metadataRepository, times(deletedVersions.size())).removeArtifact(eq(repositorySession) , metadataArg.capture(), eq(projectVersion) ); List<ArtifactMetadata> metaL = metadataArg.getAllValues(); for (ArtifactMetadata meta : metaL) { assertTrue(meta.getId().startsWith(projectName)); @@ -246,6 +267,15 @@ public class DaysOldRepositoryPurgeTest org.apache.archiva.repository.ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ); ArtifactCleanupFeature atf = repoConfiguration.getFeature( ArtifactCleanupFeature.class ).get(); List<RepositoryListener> listeners = Collections.singletonList( listener ); + + sessionControl.reset(); + sessionFactoryControl.reset(); + EasyMock.expect( sessionFactory.createSession( ) ).andStubReturn( repositorySession ); + EasyMock.expect( repositorySession.getRepository()).andStubReturn( metadataRepository ); + repositorySession.save(); + EasyMock.expectLastCall().anyTimes(); + sessionFactoryControl.replay(); + sessionControl.replay(); repoPurge = new DaysOldRepositoryPurge( getRepository(), atf.getRetentionPeriod().getDays(), atf.getRetentionCount(), repositorySession, listeners ); @@ -305,9 +335,9 @@ public class DaysOldRepositoryPurgeTest listenerControl.verify(); // Verify the metadataRepository invocations - verify(metadataRepository, never()).removeProjectVersion(repositorySession , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) ); + verify(metadataRepository, never()).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) ); ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class); - verify(metadataRepository, times(deletedVersions.size())).removeArtifact( repositorySession, metadataArg.capture(), eq(projectVersion) ); + verify(metadataRepository, times(deletedVersions.size())).removeArtifact( eq(repositorySession), metadataArg.capture(), eq(projectVersion) ); List<ArtifactMetadata> metaL = metadataArg.getAllValues(); for (ArtifactMetadata meta : metaL) { assertTrue(meta.getId().startsWith(projectName)); diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/RepositoryPurgeConsumerTest.java b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/RepositoryPurgeConsumerTest.java index d6f634fd9..318144563 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/RepositoryPurgeConsumerTest.java +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/RepositoryPurgeConsumerTest.java @@ -31,6 +31,7 @@ import org.apache.archiva.mock.MockRepositorySessionFactory; import org.apache.archiva.repository.RepositoryRegistry; import org.apache.archiva.repository.features.ArtifactCleanupFeature; import org.custommonkey.xmlunit.XMLAssert; +import org.easymock.EasyMock; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -71,8 +72,6 @@ public class RepositoryPurgeConsumerTest { super.setUp(); - MockRepositorySessionFactory factory = applicationContext.getBean( MockRepositorySessionFactory.class ); - factory.setRepository( metadataRepository ); } @After @@ -163,16 +162,25 @@ public class RepositoryPurgeConsumerTest public void testConsumerByRetentionCount() throws Exception { - KnownRepositoryContentConsumer repoPurgeConsumer = + RepositoryPurgeConsumer repoPurgeConsumer = applicationContext.getBean( "knownRepositoryContentConsumer#repo-purge-consumer-by-retention-count", - KnownRepositoryContentConsumer.class ); - + RepositoryPurgeConsumer.class ); + repoPurgeConsumer.setRepositorySessionFactory( sessionFactory ); org.apache.archiva.repository.ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ); ArtifactCleanupFeature atf = repoConfiguration.getFeature( ArtifactCleanupFeature.class ).get(); atf.setRetentionPeriod( Period.ofDays( 0 ) ); // force days older off to allow retention count purge to execute. atf.setRetentionCount( TEST_RETENTION_COUNT ); addRepoToConfiguration( "retention-count", repoConfiguration ); + sessionControl.reset(); + sessionFactoryControl.reset(); + EasyMock.expect( sessionFactory.createSession( ) ).andStubReturn( repositorySession ); + EasyMock.expect( repositorySession.getRepository()).andStubReturn( metadataRepository ); + repositorySession.save(); + EasyMock.expectLastCall().anyTimes(); + sessionFactoryControl.replay(); + sessionControl.replay(); + repoPurgeConsumer.beginScan( repoConfiguration, null ); String repoRoot = prepareTestRepos(); @@ -188,6 +196,9 @@ public class RepositoryPurgeConsumerTest // Provide the metadata list List<ArtifactMetadata> ml = getArtifactMetadataFromDir( TEST_REPO_ID, projectName, repo, vDir ); + + + when(metadataRepository.getArtifacts( repositorySession, TEST_REPO_ID, projectNs, projectName, projectVersion )).thenReturn(ml); Set<String> deletedVersions = new HashSet<>(); @@ -197,9 +208,9 @@ public class RepositoryPurgeConsumerTest repoPurgeConsumer.processFile( PATH_TO_BY_RETENTION_COUNT_ARTIFACT ); // Verify the metadataRepository invocations - verify(metadataRepository, never()).removeProjectVersion( repositorySession, eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) ); + verify(metadataRepository, never()).removeProjectVersion( eq(repositorySession), eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) ); ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class); - verify(metadataRepository, times(2)).removeArtifact( repositorySession, metadataArg.capture(), eq(projectVersion) ); + verify(metadataRepository, times(2)).removeArtifact( eq(repositorySession), metadataArg.capture(), eq(projectVersion) ); List<ArtifactMetadata> metaL = metadataArg.getAllValues(); for (ArtifactMetadata meta : metaL) { assertTrue(meta.getId().startsWith(projectName)); @@ -265,15 +276,25 @@ public class RepositoryPurgeConsumerTest public void testConsumerByDaysOld() throws Exception { - KnownRepositoryContentConsumer repoPurgeConsumer = + RepositoryPurgeConsumer repoPurgeConsumer = applicationContext.getBean( "knownRepositoryContentConsumer#repo-purge-consumer-by-days-old", - KnownRepositoryContentConsumer.class ); + RepositoryPurgeConsumer.class ); + + repoPurgeConsumer.setRepositorySessionFactory( sessionFactory ); org.apache.archiva.repository.ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ); ArtifactCleanupFeature atf = repoConfiguration.getFeature( ArtifactCleanupFeature.class ).get(); atf.setRetentionPeriod( Period.ofDays( TEST_DAYS_OLDER ) ); addRepoToConfiguration( "days-old", repoConfiguration ); + sessionControl.reset(); + sessionFactoryControl.reset(); + EasyMock.expect( sessionFactory.createSession( ) ).andStubReturn( repositorySession ); + EasyMock.expect( repositorySession.getRepository()).andStubReturn( metadataRepository ); + repositorySession.save(); + EasyMock.expectLastCall().anyTimes(); + sessionFactoryControl.replay(); + sessionControl.replay(); repoPurgeConsumer.beginScan( repoConfiguration, null ); String repoRoot = prepareTestRepos(); @@ -300,10 +321,11 @@ public class RepositoryPurgeConsumerTest repoPurgeConsumer.processFile( PATH_TO_BY_DAYS_OLD_ARTIFACT ); // Verify the metadataRepository invocations - verify(metadataRepository, never()).removeProjectVersion( repositorySession, eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) ); + verify(metadataRepository, never()).removeProjectVersion( eq(repositorySession), eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) ); ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class); - verify(metadataRepository, times(2)).removeArtifact( repositorySession, metadataArg.capture(), eq(projectVersion) ); + verify(metadataRepository, times(2)).removeArtifact( eq(repositorySession), metadataArg.capture(), eq(projectVersion) ); List<ArtifactMetadata> metaL = metadataArg.getAllValues(); + assertTrue( metaL.size( ) > 0 ); for (ArtifactMetadata meta : metaL) { assertTrue(meta.getId().startsWith(projectName)); assertTrue(deletedVersions.contains(meta.getVersion())); @@ -379,10 +401,10 @@ public class RepositoryPurgeConsumerTest repoPurgeConsumer.processFile( CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO ); - verify(metadataRepository, never()).removeProjectVersion( repositorySession, eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) ); + verify(metadataRepository, never()).removeProjectVersion( eq(repositorySession), eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) ); ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class); - verify(metadataRepository, never()).removeArtifact( repositorySession, any(), any() ); - verify(metadataRepository, never()).removeArtifact( repositorySession, any(), any(), any(), any(), any(MetadataFacet.class) ); + verify(metadataRepository, never()).removeArtifact( eq(repositorySession), any(), any() ); + verify(metadataRepository, never()).removeArtifact( eq(repositorySession), any(), any(), any(), any(), any(MetadataFacet.class) ); // check if the snapshot wasn't removed @@ -413,16 +435,23 @@ public class RepositoryPurgeConsumerTest public void testReleasedSnapshotsWereCleaned() throws Exception { - KnownRepositoryContentConsumer repoPurgeConsumer = + RepositoryPurgeConsumer repoPurgeConsumer = applicationContext.getBean( "knownRepositoryContentConsumer#repo-purge-consumer-by-days-old", - KnownRepositoryContentConsumer.class ); - + RepositoryPurgeConsumer.class ); + repoPurgeConsumer.setRepositorySessionFactory( sessionFactory ); org.apache.archiva.repository.ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ); ArtifactCleanupFeature acf = repoConfiguration.getFeature( ArtifactCleanupFeature.class ).get(); acf.setDeleteReleasedSnapshots( true ); addRepoToConfiguration( "days-old", repoConfiguration ); - + sessionControl.reset(); + sessionFactoryControl.reset(); + EasyMock.expect( sessionFactory.createSession( ) ).andStubReturn( repositorySession ); + EasyMock.expect( repositorySession.getRepository()).andStubReturn( metadataRepository ); + repositorySession.save(); + EasyMock.expectLastCall().anyTimes(); + sessionFactoryControl.replay(); + sessionControl.replay(); repoPurgeConsumer.beginScan( repoConfiguration, null ); String repoRoot = prepareTestRepos(); @@ -442,9 +471,9 @@ public class RepositoryPurgeConsumerTest repoPurgeConsumer.processFile( CleanupReleasedSnapshotsRepositoryPurgeTest.PATH_TO_RELEASED_SNAPSHOT_IN_SAME_REPO ); - verify(metadataRepository, times(1)).removeProjectVersion( repositorySession, eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) ); + verify(metadataRepository, times(1)).removeProjectVersion( eq(repositorySession), eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) ); ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class); - verify(metadataRepository, never()).removeArtifact( repositorySession, any(), any() ); + verify(metadataRepository, never()).removeArtifact( eq(repositorySession), any(), any() ); // check if the snapshot was removed assertDeleted( projectRoot + "/2.3-SNAPSHOT" ); diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/RetentionCountRepositoryPurgeTest.java b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/RetentionCountRepositoryPurgeTest.java index ba16bc3d8..e8ffc78b3 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/RetentionCountRepositoryPurgeTest.java +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/RetentionCountRepositoryPurgeTest.java @@ -22,6 +22,7 @@ package org.apache.archiva.consumers.core.repository; import org.apache.archiva.metadata.model.ArtifactMetadata; import org.apache.archiva.repository.events.RepositoryListener; import org.apache.archiva.repository.features.ArtifactCleanupFeature; +import org.easymock.EasyMock; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -53,6 +54,16 @@ public class RetentionCountRepositoryPurgeTest org.apache.archiva.repository.ManagedRepository repoConfiguration = getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME ); List<RepositoryListener> listeners = Collections.singletonList( listener ); ArtifactCleanupFeature acf = repoConfiguration.getFeature( ArtifactCleanupFeature.class ).get(); + + sessionControl.reset(); + sessionFactoryControl.reset(); + EasyMock.expect( sessionFactory.createSession( ) ).andStubReturn( repositorySession ); + EasyMock.expect( repositorySession.getRepository()).andStubReturn( metadataRepository ); + repositorySession.save(); + EasyMock.expectLastCall().anyTimes(); + sessionFactoryControl.replay(); + sessionControl.replay(); + repoPurge = new RetentionCountRepositoryPurge( getRepository(), acf.getRetentionCount(), repositorySession, listeners ); } @@ -124,9 +135,9 @@ public class RetentionCountRepositoryPurgeTest listenerControl.verify(); // Verify the metadataRepository invocations - verify(metadataRepository, never()).removeProjectVersion( repositorySession, eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) ); + verify(metadataRepository, never()).removeProjectVersion( eq(repositorySession), eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) ); ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class); - verify(metadataRepository, times(deletedVersions.size())).removeArtifact( repositorySession, metadataArg.capture(), eq(projectVersion) ); + verify(metadataRepository, times(deletedVersions.size())).removeArtifact( eq(repositorySession), metadataArg.capture(), eq(projectVersion) ); List<ArtifactMetadata> metaL = metadataArg.getAllValues(); for (ArtifactMetadata meta : metaL) { assertTrue(meta.getId().startsWith(projectName)); @@ -210,9 +221,9 @@ public class RetentionCountRepositoryPurgeTest listenerControl.verify(); // Verify the metadataRepository invocations - verify(metadataRepository, never()).removeProjectVersion( repositorySession, eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) ); + verify(metadataRepository, never()).removeProjectVersion( eq(repositorySession), eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) ); ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class); - verify(metadataRepository, times(deletedVersions.size())).removeArtifact( repositorySession, metadataArg.capture(), eq(projectVersion) ); + verify(metadataRepository, times(deletedVersions.size())).removeArtifact( eq(repositorySession), metadataArg.capture(), eq(projectVersion) ); List<ArtifactMetadata> metaL = metadataArg.getAllValues(); for (ArtifactMetadata meta : metaL) { assertTrue(meta.getId().startsWith(projectName)); @@ -298,9 +309,9 @@ public class RetentionCountRepositoryPurgeTest listenerControl.verify(); // Verify the metadataRepository invocations - verify(metadataRepository, never()).removeProjectVersion( repositorySession, eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) ); + verify(metadataRepository, never()).removeProjectVersion( eq(repositorySession), eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) ); ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class); - verify(metadataRepository, times(deletedVersions.size())).removeArtifact( repositorySession, metadataArg.capture(), eq(projectVersion) ); + verify(metadataRepository, times(deletedVersions.size())).removeArtifact( eq(repositorySession), metadataArg.capture(), eq(projectVersion) ); List<ArtifactMetadata> metaL = metadataArg.getAllValues(); for (ArtifactMetadata meta : metaL) { assertTrue(meta.getId().startsWith(projectName)); diff --git a/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/main/java/org/apache/archiva/admin/repository/managed/DefaultManagedRepositoryAdmin.java b/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/main/java/org/apache/archiva/admin/repository/managed/DefaultManagedRepositoryAdmin.java index 6d7d8d7c8..94266eb9e 100644 --- a/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/main/java/org/apache/archiva/admin/repository/managed/DefaultManagedRepositoryAdmin.java +++ b/archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/main/java/org/apache/archiva/admin/repository/managed/DefaultManagedRepositoryAdmin.java @@ -337,35 +337,38 @@ public class DefaultManagedRepositoryAdmin if ( !stagedOne ) { - RepositorySession repositorySession = null; - try - { - repositorySession = getRepositorySessionFactory().createSession(); - } - catch ( MetadataRepositoryException e ) - { - e.printStackTrace( ); - } - try + boolean success=false; + try(RepositorySession repositorySession = getRepositorySessionFactory().createSession()) { MetadataRepository metadataRepository = repositorySession.getRepository(); metadataRepository.removeRepository(repositorySession , repository.getId() ); //invalidate cache namespacesCache.remove( repository.getId() ); - log.debug( "call repositoryStatisticsManager.deleteStatistics" ); - getRepositoryStatisticsManager().deleteStatistics( metadataRepository, repository.getId() ); repositorySession.save(); + success=true; } catch ( MetadataRepositoryException e ) { //throw new RepositoryAdminException( e.getMessage(), e ); log.warn( "skip error during removing repository from MetadataRepository:{}", e.getMessage(), e ); + success = false; } catch (MetadataSessionException e) { log.warn( "skip error during removing repository from MetadataRepository:{}", e.getMessage(), e ); - } finally + success = false; + } + if (success) { - repositorySession.close(); + log.debug( "call repositoryStatisticsManager.deleteStatistics" ); + try + { + getRepositoryStatisticsManager( ).deleteStatistics( repository.getId( ) ); + } + catch ( MetadataRepositoryException e ) + { + e.printStackTrace( ); + } } + } if ( deleteContent ) @@ -514,8 +517,8 @@ public class DefaultManagedRepositoryAdmin if ( resetStats ) { log.debug( "call repositoryStatisticsManager.deleteStatistics" ); - getRepositoryStatisticsManager().deleteStatistics( repositorySession.getRepository(), - managedRepository.getId() ); + getRepositoryStatisticsManager().deleteStatistics( + managedRepository.getId() ); repositorySession.save(); } |