]> source.dussan.org Git - archiva.git/commitdiff
Fixing unit tests
authorMartin Stockhammer <martin_s@apache.org>
Fri, 16 Aug 2019 20:36:09 +0000 (22:36 +0200)
committerMartin Stockhammer <martin_s@apache.org>
Fri, 16 Aug 2019 20:36:09 +0000 (22:36 +0200)
41 files changed:
archiva-modules/archiva-base/archiva-configuration/src/main/java/org/apache/archiva/configuration/FileTypes.java
archiva-modules/archiva-base/archiva-consumers/archiva-consumer-archetype/src/main/resources/archetype-resources/src/main/java/SimpleArtifactConsumer.java
archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/archiva/consumers/core/repository/RepositoryPurgeConsumer.java
archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/AbstractRepositoryPurgeTest.java
archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/CleanupReleasedSnapshotsRepositoryPurgeTest.java
archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/DaysOldRepositoryPurgeTest.java
archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/RepositoryPurgeConsumerTest.java
archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/RetentionCountRepositoryPurgeTest.java
archiva-modules/archiva-base/archiva-repository-admin/archiva-repository-admin-default/src/main/java/org/apache/archiva/admin/repository/managed/DefaultManagedRepositoryAdmin.java
archiva-modules/archiva-scheduler/archiva-scheduler-repository/src/main/java/org/apache/archiva/scheduler/repository/ArchivaRepositoryScanningTaskExecutor.java
archiva-modules/archiva-scheduler/archiva-scheduler-repository/src/main/java/org/apache/archiva/scheduler/repository/DefaultRepositoryArchivaTaskScheduler.java
archiva-modules/archiva-scheduler/archiva-scheduler-repository/src/test/java/org/apache/archiva/scheduler/repository/ArchivaRepositoryScanningTaskExecutorPhase2Test.java
archiva-modules/archiva-scheduler/archiva-scheduler-repository/src/test/java/org/apache/archiva/scheduler/repository/TestRepositoryStatisticsManager.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultBrowseService.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultManagedRepositoriesService.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultReportRepositoriesService.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/AbstractArchivaRestTest.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/BrowseServiceTest.java
archiva-modules/archiva-web/archiva-rss/src/main/java/org/apache/archiva/rss/processor/NewArtifactsRssFeedProcessor.java
archiva-modules/archiva-web/archiva-rss/src/main/java/org/apache/archiva/rss/processor/NewVersionsOfArtifactRssFeedProcessor.java
archiva-modules/archiva-web/archiva-rss/src/main/java/org/apache/archiva/rss/processor/RssFeedProcessor.java
archiva-modules/archiva-web/archiva-rss/src/test/java/org/apache/archiva/rss/processor/NewArtifactsRssFeedProcessorTest.java
archiva-modules/archiva-web/archiva-rss/src/test/java/org/apache/archiva/rss/processor/NewVersionsOfArtifactRssFeedProcessorTest.java
archiva-modules/archiva-web/archiva-web-common/src/main/java/org/apache/archiva/web/rss/RssFeedServlet.java
archiva-modules/archiva-web/archiva-web-common/src/test/java/org/apache/archiva/web/rss/RssFeedServletTest.java
archiva-modules/metadata/metadata-repository-api/src/test/java/org/apache/archiva/metadata/repository/AbstractMetadataRepositoryTest.java
archiva-modules/metadata/metadata-statistics-api/src/main/java/org/apache/archiva/metadata/repository/stats/model/RepositoryStatisticsManager.java
archiva-modules/plugins/audit/src/main/java/org/apache/archiva/audit/DefaultAuditManager.java
archiva-modules/plugins/audit/src/test/java/org/apache/archiva/audit/AuditManagerTest.java
archiva-modules/plugins/metadata-store-cassandra/src/test/java/org/apache/archiva/metadata/repository/cassandra/CassandraMetadataRepositoryTest.java
archiva-modules/plugins/metadata-store-file/src/test/java/org/apache/archiva/metadata/repository/file/FileMetadataRepositoryTest.java
archiva-modules/plugins/metadata-store-jcr/src/main/java/org/apache/archiva/metadata/repository/jcr/JcrMetadataRepository.java
archiva-modules/plugins/problem-reports/src/main/java/org/apache/archiva/reports/consumers/DuplicateArtifactsConsumer.java
archiva-modules/plugins/problem-reports/src/test/java/org/apache/archiva/reports/consumers/DuplicateArtifactsConsumerTest.java
archiva-modules/plugins/repository-statistics/src/main/java/org/apache/archiva/metadata/repository/stats/DefaultRepositoryStatisticsManager.java
archiva-modules/plugins/repository-statistics/src/test/java/org/apache/archiva/metadata/repository/stats/RepositoryStatisticsManagerTest.java
archiva-modules/plugins/stage-repository-merge/pom.xml
archiva-modules/plugins/stage-repository-merge/src/main/java/org/apache/archiva/stagerepository/merge/Maven2RepositoryMerger.java
archiva-modules/plugins/stage-repository-merge/src/test/java/org/apache/archiva/configuration/StubConfiguration.java
archiva-modules/plugins/stage-repository-merge/src/test/java/org/apache/archiva/stagerepository/merge/Maven2RepositoryMergerTest.java
archiva-modules/plugins/stage-repository-merge/src/test/resources/spring-context.xml

index c5d8931eead1354d86a68bc6d1244b3d96cdbe31..1b263edb9816bfa973eef1921cdb04172c3cd792 100644 (file)
@@ -62,6 +62,11 @@ public class FileTypes
     @Named(value = "archivaConfiguration#default")
     private ArchivaConfiguration archivaConfiguration;
 
+
+    public FileTypes() {
+
+    }
+
     /**
      * Map of default values for the file types.
      */
index 5c5f45f212ee30fbb4b3d7c02ca055fa97df283a..c8bd82371055d7fcde5c1d39b34637ed2e089cf4 100644 (file)
@@ -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 );
         }
index 6c153f2156ecf2277963acf8a5a0147fac9eeabe..536e1fd46f1a3c6dd00ac34125b7522bcee2ff32 100644 (file)
@@ -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;
-    }
 }
index 613ec3054cda866867a07c6281539796da4fed5e..8f6172bf51e78218ec2c352be8425fbca6b33dc5 100644 (file)
@@ -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 );
 
     }
 
index fdd86cb94438412a98e77b25a38f75b9978bfdeb..9d44b28b7a87d23071043c3ba2692551a40613fc 100644 (file)
@@ -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) );
 
 
 
index 651e90a3c21f8ed70f43356771404329a6a325a5..848d4eda6f42e1b4ff9a96fd02e67c2771f65716 100644 (file)
@@ -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));
index d6f634fd9c0a0f1ad314d476700e764c0080926f..31814456389936a91f38659ea9af9a5a363e5866 100644 (file)
@@ -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" );
index ba16bc3d8428c868a10deea9a7205aa7360f3d66..e8ffc78b3bde4d6f30827a91927dd96baf82ca74 100644 (file)
@@ -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));
index 6d7d8d7c8601b5d308d151f9d1b825cc72ea720c..94266eb9ee2a19272696fd225055d71c556709e6 100644 (file)
@@ -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();
             }
 
index 2c97314ef1e4fb8d2b93d3364cc5e6f8d35d21d9..bb7690e5e0c8d1a0f2209e97432f09f4200606cd 100644 (file)
@@ -141,7 +141,7 @@ public class ArchivaRepositoryScanningTaskExecutor
                     if ( !task.isScanAll() )
                     {
                         RepositoryStatistics previousStats =
-                            repositoryStatisticsManager.getLastStatistics( metadataRepository, repoId );
+                            repositoryStatisticsManager.getLastStatistics( repoId );
                         if ( previousStats != null )
                         {
                             sinceWhen = previousStats.getScanStartTime().getTime();
@@ -166,7 +166,7 @@ public class ArchivaRepositoryScanningTaskExecutor
 
                     log.info( "Gathering repository statistics" );
 
-                    repositoryStatisticsManager.addStatisticsAfterScan( metadataRepository, repoId,
+                    repositoryStatisticsManager.addStatisticsAfterScan( repoId,
                                                                         stats.getWhenGathered(), endTime,
                                                                         stats.getTotalFileCount(),
                                                                         stats.getTotalFileCount() - previousFileCount );
index ef9067df5499d8fe1d82abb238731c4549995474..d87e10a7238c2d4acb7d8a5a845fc8dff382d10a 100644 (file)
@@ -315,7 +315,7 @@ public class DefaultRepositoryArchivaTaskScheduler
     {
         long start = System.currentTimeMillis();
 
-        boolean res = repositoryStatisticsManager.hasStatistics( metadataRepository, repoConfig.getId() );
+        boolean res = repositoryStatisticsManager.hasStatistics( repoConfig.getId() );
 
         long end = System.currentTimeMillis();
 
index 835454370e73002520296d8df93285efb5111f3a..291d6678effe89219f289444557db816fa1b5240 100644 (file)
@@ -66,7 +66,7 @@ public class ArchivaRepositoryScanningTaskExecutorPhase2Test
 
         // check correctness of new stats
         RepositoryStatistics newStats =
-            repositoryStatisticsManager.getLastStatistics( metadataRepository, TEST_REPO_ID );
+            repositoryStatisticsManager.getLastStatistics( TEST_REPO_ID );
         assertEquals( 0, newStats.getNewFileCount() );
         assertEquals( 31, newStats.getTotalFileCount() );
         // FIXME: can't test these as they weren't stored in the database, move to tests for RepositoryStatisticsManager implementation
@@ -99,7 +99,7 @@ public class ArchivaRepositoryScanningTaskExecutorPhase2Test
 
         // check correctness of new stats
         RepositoryStatistics updatedStats =
-            repositoryStatisticsManager.getLastStatistics( metadataRepository, TEST_REPO_ID );
+            repositoryStatisticsManager.getLastStatistics( TEST_REPO_ID );
         assertEquals( 2, updatedStats.getNewFileCount() );
         assertEquals( 33, updatedStats.getTotalFileCount() );
         // FIXME: can't test these as they weren't stored in the database, move to tests for RepositoryStatisticsManager implementation
@@ -146,7 +146,7 @@ public class ArchivaRepositoryScanningTaskExecutorPhase2Test
 
         // check correctness of new stats
         RepositoryStatistics newStats =
-            repositoryStatisticsManager.getLastStatistics( metadataRepository, TEST_REPO_ID );
+            repositoryStatisticsManager.getLastStatistics( TEST_REPO_ID );
         assertEquals( 2, newStats.getNewFileCount() );
         assertEquals( 33, newStats.getTotalFileCount() );
         // FIXME: can't test these as they weren't stored in the database, move to tests for RepositoryStatisticsManager implementation
@@ -193,7 +193,7 @@ public class ArchivaRepositoryScanningTaskExecutorPhase2Test
 
         // check correctness of new stats
         RepositoryStatistics newStats =
-            repositoryStatisticsManager.getLastStatistics( metadataRepository, TEST_REPO_ID );
+            repositoryStatisticsManager.getLastStatistics( TEST_REPO_ID );
         assertEquals( 2, newStats.getNewFileCount() );
         assertEquals( 33, newStats.getTotalFileCount() );
         // FIXME: can't test these as they weren't stored in the database, move to tests for RepositoryStatisticsManager implementation
@@ -213,7 +213,7 @@ public class ArchivaRepositoryScanningTaskExecutorPhase2Test
         repoTask.setScanAll( true );
 
         Date date = Calendar.getInstance().getTime();
-        repositoryStatisticsManager.addStatisticsAfterScan( metadataRepository, TEST_REPO_ID, //
+        repositoryStatisticsManager.addStatisticsAfterScan( TEST_REPO_ID, //
                                                             new Date( date.getTime() - 1234567 ), //
                                                             date, 8, 8 ); //
 
@@ -239,7 +239,7 @@ public class ArchivaRepositoryScanningTaskExecutorPhase2Test
         stats.setTotalProjectCount( 5 );
         stats.setTotalArtifactFileSize( 38545 );
 
-        repositoryStatisticsManager.addStatisticsAfterScan( metadataRepository, TEST_REPO_ID, //
+        repositoryStatisticsManager.addStatisticsAfterScan( TEST_REPO_ID, //
                                                             new Date( date.getTime() - 1234567 ), //
                                                             date, 31, 31 );
     }
index 7f0512c84db088a2c70e257e7f5033fa2a115c71..8f1e0c5055254a12f21ef85120c94546fac3d74a 100644 (file)
@@ -19,7 +19,6 @@ package org.apache.archiva.scheduler.repository;
  * under the License.
  */
 
-import org.apache.archiva.metadata.repository.MetadataRepository;
 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
 import org.apache.archiva.metadata.repository.stats.model.DefaultRepositoryStatistics;
 import org.apache.archiva.metadata.repository.stats.model.RepositoryStatistics;
@@ -39,14 +38,14 @@ public class TestRepositoryStatisticsManager
     private Map<String, List<RepositoryStatistics>> repoStats = new HashMap<>();
 
     @Override
-    public boolean hasStatistics( MetadataRepository metadataRepository, String repositoryId )
+    public boolean hasStatistics( String repositoryId )
         throws MetadataRepositoryException
     {
         return !repoStats.isEmpty();
     }
 
     @Override
-    public RepositoryStatistics getLastStatistics( MetadataRepository metadataRepository, String repositoryId )
+    public RepositoryStatistics getLastStatistics( String repositoryId )
     {
         List<RepositoryStatistics> repositoryStatisticsList = getStatsList( repositoryId );
         return !repositoryStatisticsList.isEmpty()
@@ -55,7 +54,7 @@ public class TestRepositoryStatisticsManager
     }
 
     @Override
-    public void addStatisticsAfterScan( MetadataRepository metadataRepository, String repositoryId, Date startTime,
+    public void addStatisticsAfterScan( String repositoryId, Date startTime,
                                         Date endTime, long totalFiles, long newFiles )
     {
         List<RepositoryStatistics> stats = getStatsList( repositoryId );
@@ -71,13 +70,13 @@ public class TestRepositoryStatisticsManager
     }
 
     @Override
-    public void deleteStatistics( MetadataRepository metadataRepository, String repositoryId )
+    public void deleteStatistics( String repositoryId )
     {
         repoStats.remove( repositoryId );
     }
 
     @Override
-    public List<RepositoryStatistics> getStatisticsInRange( MetadataRepository metadataRepository, String repositoryId,
+    public List<RepositoryStatistics> getStatisticsInRange( String repositoryId,
                                                             Date startDate, Date endDate )
     {
         throw new UnsupportedOperationException();
index ee606ae3e6cac63058fc27ac6d220fa507c83701..adc2c0af0e06e37cb8b8a1b8e85b9d07cec3a374 100644 (file)
@@ -1110,16 +1110,7 @@ public class DefaultBrowseService
     public List<Artifact> searchArtifacts( String text, String repositoryId, Boolean exact )
         throws ArchivaRestServiceException
     {
-        RepositorySession repositorySession = null;
-        try
-        {
-            repositorySession = repositorySessionFactory.createSession();
-        }
-        catch ( MetadataRepositoryException e )
-        {
-            e.printStackTrace( );
-        }
-        try
+        try(RepositorySession repositorySession = repositorySessionFactory.createSession())
         {
             List<ArtifactMetadata> artifactMetadatas =
                 repositorySession.getRepository().searchArtifacts(repositorySession , repositoryId, text, exact == null ? false : exact );
@@ -1129,10 +1120,6 @@ public class DefaultBrowseService
         {
             throw new ArchivaRestServiceException( e.getMessage(), e );
         }
-        finally
-        {
-            repositorySession.close();
-        }
     }
 
     @Override
index b1e72fe5a5b75fb0b779296ba2f60281c89fe0be..6452409bac4e844176180e0f0c8698dbfa06bacb 100644 (file)
@@ -180,7 +180,7 @@ public class DefaultManagedRepositoriesService
             RepositoryStatistics stats = null;
             try
             {
-                stats = repositoryStatisticsManager.getLastStatistics( metadataRepository, repositoryId );
+                stats = repositoryStatisticsManager.getLastStatistics( repositoryId );
             }
             catch ( MetadataRepositoryException e )
             {
index 7e1ebc04b967a2132152f1e6cd51c6796817a024..429c5e2031a7892d844ab0d66f263299fc5b02ad 100644 (file)
@@ -87,7 +87,7 @@ public class DefaultReportRepositoriesService
             {
                 try
                 {
-                    stats.add( repositoryStatisticsManager.getLastStatistics( metadataRepository, repo ) );
+                    stats.add( repositoryStatisticsManager.getLastStatistics( repo ) );
                 }
                 catch ( MetadataRepositoryException e )
                 {
@@ -121,7 +121,7 @@ public class DefaultReportRepositoriesService
             List<RepositoryStatistics> stats = null;
             try
             {
-                stats = repositoryStatisticsManager.getStatisticsInRange( metadataRepository, repositoryId, startDate,
+                stats = repositoryStatisticsManager.getStatisticsInRange( repositoryId, startDate,
                                                                           endDate );
             }
             catch ( MetadataRepositoryException e )
index f133365822555b302212fec9ff3efeda2f0a012a..82a8fe720332a427e884d04af342884d1cce8d27 100644 (file)
@@ -62,6 +62,7 @@ import java.util.Collections;
 import java.util.Date;
 import java.util.Locale;
 import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.Function;
 
 /**
  * @author Olivier Lamy
@@ -85,7 +86,60 @@ public abstract class AbstractArchivaRestTest
         return this.reuseServer;
     }
 
+    /*
+     * Used by tryAssert to allow to throw exceptions in the lambda expression.
+     */
+    @FunctionalInterface
+    protected interface AssertFunction
+    {
+        void accept( ) throws Exception;
+    }
+
+    protected void tryAssert( AssertFunction func ) throws Exception
+    {
+        tryAssert( func, 10, 500 );
+    }
 
+    /*
+     * Runs the assert method until the assert is successful or the number of retries
+     * is reached. This is needed because the JCR Oak index update is asynchronous, so updates
+     * may not be visible immediately after the modification.
+     */
+    private void tryAssert( AssertFunction func, int retries, int sleepMillis ) throws Exception
+    {
+        Throwable t = null;
+        int retry = retries;
+        while ( retry-- > 0 )
+        {
+            try
+            {
+                func.accept( );
+                return;
+            }
+            catch ( Exception | AssertionError e )
+            {
+                t = e;
+                Thread.currentThread( ).sleep( sleepMillis );
+                log.warn( "Retrying assert {}: {}", retry, e.getMessage( ) );
+            }
+        }
+        log.warn( "Retries: {}, Exception: {}", retry, t.getMessage( ) );
+        if ( retry <= 0 && t != null )
+        {
+            if ( t instanceof RuntimeException )
+            {
+                throw (RuntimeException) t;
+            }
+            else if ( t instanceof Exception )
+            {
+                throw (Exception) t;
+            }
+            else if ( t instanceof Error )
+            {
+                throw (Error) t;
+            }
+        }
+    }
 
     // START SNIPPET: authz-header
     // guest with an empty password
index 448405e7a2f5ea600270b3efd3c7a512bf7e9136..f85287214c418d6494c44b09e5bcde9929b70cd4 100644 (file)
@@ -334,11 +334,13 @@ public class BrowseServiceTest
 
         browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", TEST_REPO_ID );
 
-        List<Artifact> artifactDownloadInfos =
-            browseService.getArtifactsByProjectVersionMetadata( "wine", "bordeaux", TEST_REPO_ID );
+        tryAssert( ( ) -> {
+            List<Artifact> artifactDownloadInfos =
+                browseService.getArtifactsByProjectVersionMetadata( "wine", "bordeaux", TEST_REPO_ID );
 
-        assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 3 );
-        // END SNIPPET: get-artifacts-by-project-version-metadata
+            assertThat( artifactDownloadInfos ).isNotNull( ).isNotEmpty( ).hasSize( 3 );
+            // END SNIPPET: get-artifacts-by-project-version-metadata
+        } );
     }
 
 
@@ -346,14 +348,24 @@ public class BrowseServiceTest
     public void getArtifactsByProjectVersionMetadataWithNoRepository()
         throws Exception
     {
-        BrowseService browseService = getBrowseService( authorizationHeader, true );
+        final BrowseService browseService = getBrowseService( authorizationHeader, true );
 
         browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", TEST_REPO_ID );
 
-        List<Artifact> artifactDownloadInfos =
-            browseService.getArtifactsByProjectVersionMetadata( "wine", "bordeaux", null );
 
-        assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 3 );
+        tryAssert( ( ) -> {
+            List<Artifact> artifactDownloadInfos =
+                null;
+            try
+            {
+                artifactDownloadInfos = browseService.getArtifactsByProjectVersionMetadata( "wine", "bordeaux", null );
+            }
+            catch ( ArchivaRestServiceException e )
+            {
+                throw new AssertionError( "ArchivaRestServiceException", e );
+            }
+            assertThat( artifactDownloadInfos ).isNotNull( ).isNotEmpty( ).hasSize( 3 );
+        });
     }
 
 
@@ -379,10 +391,12 @@ public class BrowseServiceTest
         // START SNIPPET: search-artifacts
         BrowseService browseService = getBrowseService( authorizationHeader, true );
 
-        List<Artifact> artifactDownloadInfos =
-            browseService.searchArtifacts( "The Apache Software Foundation", TEST_REPO_ID, true );
+        tryAssert( ( ) -> {
+            List<Artifact> artifactDownloadInfos =
+                browseService.searchArtifacts( "The Apache Software Foundation", TEST_REPO_ID, true );
 
-        assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 7 );
+            assertThat( artifactDownloadInfos ).isNotNull( ).isNotEmpty( ).hasSize( 7 );
+        } );
         // END SNIPPET: search-artifacts
     }
 
index 6bac27ec35f0e1593e10bc2713acf4fe5c7341bd..ac68bd6f7ab964fe94a8fd6615323bc2beaac949 100644 (file)
@@ -22,7 +22,6 @@ package org.apache.archiva.rss.processor;
 import com.sun.syndication.feed.synd.SyndFeed;
 import com.sun.syndication.io.FeedException;
 import org.apache.archiva.metadata.model.ArtifactMetadata;
-import org.apache.archiva.metadata.repository.MetadataRepository;
 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
 import org.apache.archiva.metadata.repository.RepositorySession;
 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
@@ -75,7 +74,7 @@ public class NewArtifactsRssFeedProcessor
      * new versions of artifact.
      */
     @Override
-    public SyndFeed process( Map<String, String> reqParams, MetadataRepository metadataRepository )
+    public SyndFeed process( Map<String, String> reqParams )
         throws FeedException
     {
         log.debug( "Process new artifacts into rss feeds." );
@@ -83,13 +82,13 @@ public class NewArtifactsRssFeedProcessor
         String repoId = reqParams.get( RssFeedProcessor.KEY_REPO_ID );
         if ( repoId != null )
         {
-            return processNewArtifactsInRepo( repoId, metadataRepository );
+            return processNewArtifactsInRepo( repoId );
         }
 
         return null;
     }
 
-    private SyndFeed processNewArtifactsInRepo( String repoId, MetadataRepository metadataRepository )
+    private SyndFeed processNewArtifactsInRepo( String repoId )
         throws FeedException
     {
         Calendar greaterThanThisDate = Calendar.getInstance( GMT_TIME_ZONE );
@@ -99,7 +98,7 @@ public class NewArtifactsRssFeedProcessor
         List<ArtifactMetadata> artifacts;
         try(RepositorySession session = repositorySessionFactory.createSession())
         {
-            artifacts = metadataRepository.getArtifactsByDateRange(session , repoId, greaterThanThisDate.getTime(), null );
+            artifacts = session.getRepository().getArtifactsByDateRange(session , repoId, greaterThanThisDate.getTime(), null );
         }
         catch ( MetadataRepositoryException e )
         {
@@ -183,4 +182,14 @@ public class NewArtifactsRssFeedProcessor
     {
         this.numberOfDaysBeforeNow = numberOfDaysBeforeNow;
     }
+
+    public RepositorySessionFactory getRepositorySessionFactory( )
+    {
+        return repositorySessionFactory;
+    }
+
+    public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
+    {
+        this.repositorySessionFactory = repositorySessionFactory;
+    }
 }
index daf1864381fe3a154aff42242772c63c816f42c0..aae625c64f61a77f9b2d6e71c0ad2d9a0dbe15f9 100644 (file)
@@ -67,7 +67,7 @@ public class NewVersionsOfArtifactRssFeedProcessor
      * Process all versions of the artifact which had a rss feed request.
      */
     @Override
-    public SyndFeed process( Map<String, String> reqParams, MetadataRepository metadataRepository )
+    public SyndFeed process( Map<String, String> reqParams )
         throws FeedException
     {
         String groupId = reqParams.get( RssFeedProcessor.KEY_GROUP_ID );
@@ -75,19 +75,19 @@ public class NewVersionsOfArtifactRssFeedProcessor
 
         if ( groupId != null && artifactId != null )
         {
-            return processNewVersionsOfArtifact( groupId, artifactId, metadataRepository );
+            return processNewVersionsOfArtifact( groupId, artifactId );
         }
 
         return null;
     }
 
-    private SyndFeed processNewVersionsOfArtifact( String groupId, String artifactId,
-                                                   MetadataRepository metadataRepository )
+    private SyndFeed processNewVersionsOfArtifact( String groupId, String artifactId )
         throws FeedException
     {
         List<ArtifactMetadata> artifacts = new ArrayList<>();
         try(RepositorySession session = repositorySessionFactory.createSession())
         {
+            final MetadataRepository metadataRepository = session.getRepository( );
             for ( Repository repo : repositoryRegistry.getRepositories() )
             {
                 final String repoId = repo.getId();
@@ -177,4 +177,24 @@ public class NewVersionsOfArtifactRssFeedProcessor
     {
         this.generator = generator;
     }
+
+    public RepositorySessionFactory getRepositorySessionFactory( )
+    {
+        return repositorySessionFactory;
+    }
+
+    public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
+    {
+        this.repositorySessionFactory = repositorySessionFactory;
+    }
+
+    public RepositoryRegistry getRepositoryRegistry( )
+    {
+        return repositoryRegistry;
+    }
+
+    public void setRepositoryRegistry( RepositoryRegistry repositoryRegistry )
+    {
+        this.repositoryRegistry = repositoryRegistry;
+    }
 }
index 2fe62d7f19804bb9a2714e99ae60ee67cd0055cd..7bf903af0fa7f3e2453bfd0ad141b61957b32e92 100644 (file)
@@ -21,7 +21,6 @@ package org.apache.archiva.rss.processor;
 
 import com.sun.syndication.feed.synd.SyndFeed;
 import com.sun.syndication.io.FeedException;
-import org.apache.archiva.metadata.repository.MetadataRepository;
 
 import java.util.Map;
 
@@ -36,6 +35,6 @@ public interface RssFeedProcessor
 
     String KEY_ARTIFACT_ID = "artifactId";
 
-    SyndFeed process( Map<String, String> reqParams, MetadataRepository metadataRepository )
+    SyndFeed process( Map<String, String> reqParams )
         throws FeedException;
 }
index 0706d6491b185fff4e38a90c927a2276cffb0554..473d7aaf2b43350ca3d7f5920a676b0c1bce4e27 100644 (file)
@@ -25,8 +25,11 @@ import junit.framework.TestCase;
 import org.apache.archiva.metadata.model.ArtifactMetadata;
 import org.apache.archiva.metadata.repository.AbstractMetadataRepository;
 import org.apache.archiva.metadata.repository.RepositorySession;
+import org.apache.archiva.metadata.repository.RepositorySessionFactory;
 import org.apache.archiva.rss.RssFeedGenerator;
 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
+import org.easymock.EasyMock;
+import org.easymock.IMocksControl;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -49,6 +52,12 @@ public class NewArtifactsRssFeedProcessorTest
 
     private MetadataRepositoryMock metadataRepository;
 
+    private IMocksControl sessionFactoryControl;
+    private RepositorySessionFactory sessionFactory;
+
+    private IMocksControl sessionControl;
+    private RepositorySession session;
+
     @Before
     @Override
     public void setUp()
@@ -60,6 +69,22 @@ public class NewArtifactsRssFeedProcessorTest
         newArtifactsProcessor.setGenerator( new RssFeedGenerator() );
 
         metadataRepository = new MetadataRepositoryMock();
+
+        sessionFactoryControl = EasyMock.createControl();
+        sessionControl = EasyMock.createControl();
+        sessionControl.resetToNice();
+
+        sessionFactory = sessionFactoryControl.createMock( RepositorySessionFactory.class );
+        session = sessionControl.createMock( RepositorySession.class );
+
+        EasyMock.expect( sessionFactory.createSession() ).andStubReturn( session );
+        EasyMock.expect( session.getRepository( ) ).andStubReturn( metadataRepository );
+
+        sessionFactoryControl.replay();
+        sessionControl.replay();
+
+        newArtifactsProcessor.setRepositorySessionFactory( sessionFactory );
+
     }
 
     @SuppressWarnings ("unchecked")
@@ -81,10 +106,11 @@ public class NewArtifactsRssFeedProcessorTest
 
         metadataRepository.setArtifactsByDateRange( newArtifacts );
 
+
         Map<String, String> reqParams = new HashMap<>();
         reqParams.put( RssFeedProcessor.KEY_REPO_ID, TEST_REPO );
 
-        SyndFeed feed = newArtifactsProcessor.process( reqParams, metadataRepository );
+        SyndFeed feed = newArtifactsProcessor.process( reqParams );
 
         // check that the date used in the call is close to the one passed (5 seconds difference at most)
         Calendar cal = Calendar.getInstance( TimeZone.getTimeZone( "GMT" ) );
index da0a0dc311c278e415856ca8782d6fd7d9d824e8..eef6a96ada7392f697d3c624c1624fd05de5089e 100644 (file)
@@ -22,17 +22,25 @@ package org.apache.archiva.rss.processor;
 import com.sun.syndication.feed.synd.SyndEntry;
 import com.sun.syndication.feed.synd.SyndFeed;
 import junit.framework.TestCase;
+import org.apache.archiva.common.filelock.DefaultFileLockManager;
 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.repository.BasicManagedRepository;
+import org.apache.archiva.repository.Repository;
+import org.apache.archiva.repository.RepositoryRegistry;
+import org.apache.archiva.repository.storage.FilesystemStorage;
 import org.apache.archiva.rss.RssFeedGenerator;
 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
+import org.easymock.EasyMock;
 import org.easymock.IMocksControl;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
+import java.nio.file.Paths;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.Date;
@@ -59,8 +67,14 @@ public class NewVersionsOfArtifactRssFeedProcessorTest
 
     private MetadataRepository metadataRepository;
 
-    private IMocksControl factoryControl;
-    private RepositorySessionFactory repositorySessionFactory;
+    private IMocksControl sessionFactoryControl;
+    private RepositorySessionFactory sessionFactory;
+
+    private IMocksControl sessionControl;
+    private RepositorySession session;
+
+    private IMocksControl repositoryRegistryControl;
+    private RepositoryRegistry repositoryRegistry;
 
 
     @Before
@@ -76,8 +90,28 @@ public class NewVersionsOfArtifactRssFeedProcessorTest
         metadataRepositoryControl = createControl();
         metadataRepository = metadataRepositoryControl.createMock( MetadataRepository.class );
 
-        factoryControl = createControl();
-        repositorySessionFactory = factoryControl.createMock(RepositorySessionFactory.class);
+        sessionFactoryControl = EasyMock.createControl();
+        sessionControl = EasyMock.createControl();
+        sessionControl.resetToNice();
+
+        sessionFactory = sessionFactoryControl.createMock( RepositorySessionFactory.class );
+        session = sessionControl.createMock( RepositorySession.class );
+
+        EasyMock.expect( sessionFactory.createSession() ).andStubReturn( session );
+        EasyMock.expect( session.getRepository( ) ).andStubReturn( metadataRepository );
+        sessionFactoryControl.replay();
+        sessionControl.replay();
+
+        repositoryRegistryControl = EasyMock.createControl();
+        repositoryRegistry = repositoryRegistryControl.createMock( RepositoryRegistry.class );
+
+        List<Repository> reg = new ArrayList<>( );
+        reg.add( new BasicManagedRepository( TEST_REPO, TEST_REPO, new FilesystemStorage( Paths.get("target/test-storage"), new DefaultFileLockManager() ) ) );
+        EasyMock.expect( repositoryRegistry.getRepositories() ).andStubReturn( reg );
+        repositoryRegistryControl.replay();
+
+        newVersionsProcessor.setRepositorySessionFactory( sessionFactory );
+        newVersionsProcessor.setRepositoryRegistry( repositoryRegistry );
     }
 
     @SuppressWarnings("unchecked")
@@ -98,7 +132,6 @@ public class NewVersionsOfArtifactRssFeedProcessorTest
         reqParams.put( RssFeedProcessor.KEY_GROUP_ID, GROUP_ID );
         reqParams.put( RssFeedProcessor.KEY_ARTIFACT_ID, ARTIFACT_ID );
 
-        try(RepositorySession session = repositorySessionFactory.createSession()) {
             expect(metadataRepository.getProjectVersions(session, TEST_REPO, GROUP_ID, ARTIFACT_ID)).andReturn(
                     Arrays.asList("1.0.1", "1.0.2", "1.0.3-SNAPSHOT"));
             expect(metadataRepository.getArtifacts(session, TEST_REPO, GROUP_ID, ARTIFACT_ID, "1.0.1")).andReturn(
@@ -107,10 +140,9 @@ public class NewVersionsOfArtifactRssFeedProcessorTest
                     Collections.singletonList(artifact2));
             expect(metadataRepository.getArtifacts(session, TEST_REPO, GROUP_ID, ARTIFACT_ID, "1.0.3-SNAPSHOT")).andReturn(
                     Collections.singletonList(artifact3));
-        }
         metadataRepositoryControl.replay();
 
-        SyndFeed feed = newVersionsProcessor.process( reqParams, metadataRepository );
+        SyndFeed feed = newVersionsProcessor.process( reqParams );
 
         assertEquals( "New Versions of Artifact 'org.apache.archiva:artifact-two'", feed.getTitle() );
         assertEquals( "New versions of artifact 'org.apache.archiva:artifact-two' found during repository scan.",
index 0da0c61e5f002b457ee357042791db0b66e2331c..0288d15a4ed003e3687f22ca2d8743e68c7b0bb4 100644 (file)
@@ -175,15 +175,8 @@ public class RssFeedServlet
                 return;
             }
 
-            RepositorySession repositorySession = repositorySessionFactory.createSession();
-            try
-            {
-                feed = processor.process( map, repositorySession.getRepository() );
-            }
-            finally
-            {
-                repositorySession.close();
-            }
+            feed = processor.process( map );
+
             if ( feed == null )
             {
                 res.sendError( HttpServletResponse.SC_NO_CONTENT, "No information available." );
@@ -242,10 +235,6 @@ public class RssFeedServlet
 
             res.sendError( HttpServletResponse.SC_UNAUTHORIZED, USER_NOT_AUTHORIZED );
         }
-        catch ( org.apache.archiva.metadata.repository.MetadataRepositoryException e )
-        {
-            e.printStackTrace( );
-        }
     }
 
     /**
index e20632bb6412719d62f5f6fcf7bd1b56fc336f47..bac608e0378e415f02a8e4b7be5685d6f0bc3f13 100644 (file)
@@ -21,7 +21,11 @@ package org.apache.archiva.web.rss;
 
 
 import junit.framework.TestCase;
+import org.apache.archiva.common.filelock.DefaultFileLockManager;
 import org.apache.archiva.configuration.ArchivaConfiguration;
+import org.apache.archiva.repository.BasicManagedRepository;
+import org.apache.archiva.repository.RepositoryRegistry;
+import org.apache.archiva.repository.storage.FilesystemStorage;
 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
 import org.apache.commons.codec.Encoder;
 import org.apache.commons.codec.binary.Base64;
@@ -54,6 +58,7 @@ import javax.servlet.ServletContext;
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
 import java.lang.annotation.Annotation;
+import java.nio.file.Paths;
 import java.util.Locale;
 import java.util.Map;
 
@@ -71,6 +76,9 @@ public class RssFeedServletTest
     @Inject
     protected ApplicationContext applicationContext;
 
+    @Inject
+    protected RepositoryRegistry repositoryRegistry;
+
     @BeforeClass
     public static void initConfigurationPath()
         throws Exception
@@ -110,6 +118,9 @@ public class RssFeedServletTest
             }
         };
 
+        repositoryRegistry.reload();
+        repositoryRegistry.putRepository( new BasicManagedRepository( "internal", "internal",
+            new FilesystemStorage( Paths.get( "target/appserver-base/repositories/internal" ), new DefaultFileLockManager( ) ) ) );
         rssFeedServlet.init( mockServletConfig );
     }
 
index 2d7587902c3e1691249927a9a8d404c042a58d0b..a5574d63574553fbaf31d9655462c7a1dc400c63 100644 (file)
@@ -494,8 +494,8 @@ public abstract class AbstractMetadataRepositoryTest
                 assertNull( metadata1.getScm( ) );
                 assertNull( metadata1.getIssueManagement( ) );
                 assertNull( metadata1.getOrganization( ) );
-                assertEquals( "", metadata1.getDescription( ) );
-                assertEquals( "", metadata1.getName( ) );
+                assertTrue( metadata1.getDescription( )==null || "".equals(metadata1.getDescription()) );
+                assertTrue( metadata1.getName( )==null || "".equals(metadata1.getName()) );
                 assertEquals( TEST_PROJECT_VERSION, metadata1.getId( ) );
                 assertEquals( TEST_PROJECT_VERSION, metadata1.getVersion( ) );
                 assertTrue( metadata1.getMailingLists( ).isEmpty( ) );
index f47e8cc16bc3c7cd12f00f30e7282a43761001e7..4f149ab7cab901fdaf5e08c6fa6611d69bac5dd5 100644 (file)
@@ -19,7 +19,6 @@ package org.apache.archiva.metadata.repository.stats.model;
  * under the License.
  */
 
-import org.apache.archiva.metadata.repository.MetadataRepository;
 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
 
 import java.util.Date;
@@ -33,20 +32,20 @@ import java.util.List;
  */
 public interface RepositoryStatisticsManager
 {
-    RepositoryStatistics getLastStatistics( MetadataRepository metadataRepository, String repositoryId )
+    RepositoryStatistics getLastStatistics( String repositoryId )
         throws MetadataRepositoryException;
 
-    boolean hasStatistics( MetadataRepository metadataRepository, String repositoryId )
+    boolean hasStatistics( String repositoryId )
         throws MetadataRepositoryException;
 
-    void addStatisticsAfterScan( MetadataRepository metadataRepository, String repositoryId, Date startTime,
+    void addStatisticsAfterScan( String repositoryId, Date startTime,
                                  Date endTime, long totalFiles, long newFiles )
         throws MetadataRepositoryException;
 
-    void deleteStatistics( MetadataRepository metadataRepository, String repositoryId )
+    void deleteStatistics( String repositoryId )
         throws MetadataRepositoryException;
 
-    List<RepositoryStatistics> getStatisticsInRange( MetadataRepository metadataRepository, String repositoryId,
+    List<RepositoryStatistics> getStatisticsInRange( String repositoryId,
                                                      Date startTime, Date endTime )
         throws MetadataRepositoryException;
 }
index 6cf335879240c4d9d5d5792fb1eeab59017519af..63b7c366adceea487796fd2c20d9a1d38224f179 100644 (file)
@@ -178,4 +178,14 @@ public class DefaultAuditManager
             return other.name.compareTo( name );
         }
     }
+
+    public RepositorySessionFactory getRepositorySessionFactory( )
+    {
+        return repositorySessionFactory;
+    }
+
+    public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
+    {
+        this.repositorySessionFactory = repositorySessionFactory;
+    }
 }
index eaf2d8a07966d4b7cb98ae33698d81e715b6c8f8..4622b1b3fd9f302f5469b05ef29e5f7c968af2a0 100644 (file)
@@ -57,6 +57,8 @@ public class AuditManagerTest
 
     private RepositorySessionFactory repositorySessionFactory;
 
+    private RepositorySession session;
+
     private static final String AUDIT_EVENT_BASE = "2010/01/18/123456.";
 
     private static final String TEST_REPO_ID = "test-repo";
@@ -73,6 +75,7 @@ public class AuditManagerTest
 
     private static final DecimalFormat MILLIS_FORMAT = new DecimalFormat( "000" );
     private IMocksControl factoryControl;
+    private IMocksControl sessionControl;
 
     private static SimpleDateFormat createTimestampFormat()
     {
@@ -96,6 +99,11 @@ public class AuditManagerTest
         factoryControl = EasyMock.createControl();
         repositorySessionFactory = factoryControl.createMock(RepositorySessionFactory.class);
 
+        sessionControl = EasyMock.createControl( );
+        session = sessionControl.createMock( RepositorySession.class );
+
+        auditManager.setRepositorySessionFactory( repositorySessionFactory );
+
         ManagedRepositoryConfiguration repository = new ManagedRepositoryConfiguration();
         repository.setId( TEST_REPO_ID );
         repository.setLocation( "" );
@@ -113,7 +121,10 @@ public class AuditManagerTest
             expectedEvents.add( event );
         }
 
-        try(RepositorySession session = repositorySessionFactory.createSession()) {
+        sessionControl.reset();
+        factoryControl.reset();
+        EasyMock.expect( repositorySessionFactory.createSession() ).andStubReturn( session );
+        factoryControl.replay();
             EasyMock.expect(metadataRepository.getMetadataFacets(session, TEST_REPO_ID, AuditEvent.FACET_ID)).andReturn(
                     getEventNames(expectedEvents));
 
@@ -122,7 +133,6 @@ public class AuditManagerTest
                         metadataRepository.getMetadataFacet(session, TEST_REPO_ID, AuditEvent.FACET_ID, event.getName())).andReturn(
                         event);
             }
-        }
         metadataRepositoryControl.replay();
 
         List<AuditEvent> events =
@@ -151,7 +161,10 @@ public class AuditManagerTest
             expectedEvents.add( createEvent( AUDIT_EVENT_BASE + MILLIS_FORMAT.format( i ) ) );
         }
 
-        try(RepositorySession session = repositorySessionFactory.createSession()) {
+        sessionControl.reset();
+        factoryControl.reset();
+        EasyMock.expect( repositorySessionFactory.createSession() ).andStubReturn( session );
+        factoryControl.replay();
             EasyMock.expect(metadataRepository.getMetadataFacets(session, TEST_REPO_ID, AuditEvent.FACET_ID)).andReturn(
                     getEventNames(expectedEvents));
             for (AuditEvent event : expectedEvents) {
@@ -159,7 +172,6 @@ public class AuditManagerTest
                         metadataRepository.getMetadataFacet(session, TEST_REPO_ID, AuditEvent.FACET_ID, event.getName())).andReturn(
                         event);
             }
-        }
         metadataRepositoryControl.replay();
 
         List<AuditEvent> events =
@@ -194,8 +206,10 @@ public class AuditManagerTest
             events.add( event );
             eventNames.get( repositoryId ).add( event.getName() );
         }
-
-        try(RepositorySession session = repositorySessionFactory.createSession()) {
+        sessionControl.reset();
+        factoryControl.reset();
+        EasyMock.expect( repositorySessionFactory.createSession() ).andStubReturn( session );
+        factoryControl.replay();
             EasyMock.expect(metadataRepository.getMetadataFacets(session, TEST_REPO_ID, AuditEvent.FACET_ID)).andReturn(
                     eventNames.get(TEST_REPO_ID));
             EasyMock.expect(metadataRepository.getMetadataFacets(session, TEST_REPO_ID_2, AuditEvent.FACET_ID)).andReturn(
@@ -205,7 +219,6 @@ public class AuditManagerTest
                 EasyMock.expect(metadataRepository.getMetadataFacet(session, event.getRepositoryId(),
                         AuditEvent.FACET_ID, event.getName())).andReturn(event);
             }
-        }
         metadataRepositoryControl.replay();
 
         events =
@@ -229,11 +242,12 @@ public class AuditManagerTest
         throws Exception
 
     {
-
-        try(RepositorySession session = repositorySessionFactory.createSession()) {
+        sessionControl.reset();
+        factoryControl.reset();
+        EasyMock.expect( repositorySessionFactory.createSession() ).andStubReturn( session );
+        factoryControl.replay();
             EasyMock.expect(metadataRepository.getMetadataFacets(session, TEST_REPO_ID, AuditEvent.FACET_ID)).andReturn(
                     Collections.<String>emptyList());
-        }
         metadataRepositoryControl.replay();
 
         assertTrue( auditManager.getMostRecentAuditEvents( metadataRepository,
@@ -249,9 +263,11 @@ public class AuditManagerTest
     {
         AuditEvent event = createEvent( new Date() );
 
-        try(RepositorySession session = repositorySessionFactory.createSession()) {
+        sessionControl.reset();
+        factoryControl.reset();
+        EasyMock.expect( repositorySessionFactory.createSession() ).andStubReturn( session );
+        factoryControl.replay();
             metadataRepository.addMetadataFacet(session, TEST_REPO_ID, event);
-        }
 
         metadataRepositoryControl.replay();
 
@@ -281,9 +297,13 @@ public class AuditManagerTest
         throws Exception
 
     {
-        try(RepositorySession session = repositorySessionFactory.createSession()) {
-            metadataRepository.removeMetadataFacets(session, TEST_REPO_ID, AuditEvent.FACET_ID);
-        }
+
+        sessionControl.reset();
+        factoryControl.reset();
+        EasyMock.expect( repositorySessionFactory.createSession() ).andStubReturn( session );
+        factoryControl.replay();
+
+        metadataRepository.removeMetadataFacets(session, TEST_REPO_ID, AuditEvent.FACET_ID);
 
         metadataRepositoryControl.replay();
 
@@ -303,15 +323,17 @@ public class AuditManagerTest
         Date expectedTimestamp = new Date( current.getTime() - 3000 );
         AuditEvent expectedEvent = createEvent( expectedTimestamp );
         AuditEvent event3 = createEvent( new Date( current.getTime() - 1000 ) );
+        sessionControl.reset();
+        factoryControl.reset();
+        EasyMock.expect( repositorySessionFactory.createSession() ).andStubReturn( session );
+        factoryControl.replay();
 
-        try(RepositorySession session = repositorySessionFactory.createSession()) {
             EasyMock.expect(metadataRepository.getMetadataFacets(session, TEST_REPO_ID, AuditEvent.FACET_ID)).andReturn(
                     Arrays.asList(event1.getName(), expectedEvent.getName(), event3.getName()));
 
             // only match the middle one
             EasyMock.expect(metadataRepository.getMetadataFacet(session, TEST_REPO_ID,
                     AuditEvent.FACET_ID, expectedEvent.getName())).andReturn(expectedEvent);
-        }
 
         metadataRepositoryControl.replay();
 
@@ -338,7 +360,11 @@ public class AuditManagerTest
         Date ts3 = new Date( current.getTime() - 1000 );
         AuditEvent expectedEvent3 = createEvent( ts3 );
 
-        try(RepositorySession session = repositorySessionFactory.createSession()) {
+        sessionControl.reset();
+        factoryControl.reset();
+        EasyMock.expect( repositorySessionFactory.createSession() ).andStubReturn( session );
+        factoryControl.replay();
+
             EasyMock.expect(metadataRepository.getMetadataFacets(session, TEST_REPO_ID, AuditEvent.FACET_ID)).andReturn(
                     Arrays.asList(event1.getName(), expectedEvent2.getName(), expectedEvent3.getName()));
 
@@ -346,7 +372,6 @@ public class AuditManagerTest
                     AuditEvent.FACET_ID, expectedEvent2.getName())).andReturn(expectedEvent2);
             EasyMock.expect(metadataRepository.getMetadataFacet(session, TEST_REPO_ID,
                     AuditEvent.FACET_ID, expectedEvent3.getName())).andReturn(expectedEvent3);
-        }
         metadataRepositoryControl.replay();
 
         List<AuditEvent> events =
@@ -372,7 +397,11 @@ public class AuditManagerTest
         AuditEvent expectedEvent2 = createEvent( expectedTimestamp );
         AuditEvent event3 = createEvent( new Date( current.getTime() - 1000 ) );
 
-        try(RepositorySession session = repositorySessionFactory.createSession()) {
+        sessionControl.reset();
+        factoryControl.reset();
+        EasyMock.expect( repositorySessionFactory.createSession() ).andStubReturn( session );
+        factoryControl.replay();
+
             EasyMock.expect(metadataRepository.getMetadataFacets(session, TEST_REPO_ID, AuditEvent.FACET_ID)).andReturn(
                     Arrays.asList(expectedEvent1.getName(), expectedEvent2.getName(), event3.getName()));
 
@@ -381,7 +410,6 @@ public class AuditManagerTest
 
             EasyMock.expect(metadataRepository.getMetadataFacet(session, TEST_REPO_ID,
                     AuditEvent.FACET_ID, expectedEvent2.getName())).andReturn(expectedEvent2);
-        }
         metadataRepositoryControl.replay();
 
         List<AuditEvent> events =
@@ -409,7 +437,10 @@ public class AuditManagerTest
         Date ts3 = new Date( current.getTime() - 1000 );
         AuditEvent expectedEvent3 = createEvent( ts3 );
 
-        try(RepositorySession session = repositorySessionFactory.createSession()) {
+        sessionControl.reset();
+        factoryControl.reset();
+        EasyMock.expect( repositorySessionFactory.createSession() ).andStubReturn( session );
+        factoryControl.replay();
             EasyMock.expect(metadataRepository.getMetadataFacets(session, TEST_REPO_ID, AuditEvent.FACET_ID)).andReturn(
                     Arrays.asList(expectedEvent1.getName(), expectedEvent2.getName(), expectedEvent3.getName()));
 
@@ -421,7 +452,6 @@ public class AuditManagerTest
 
             EasyMock.expect(metadataRepository.getMetadataFacet(session, TEST_REPO_ID,
                     AuditEvent.FACET_ID, expectedEvent3.getName())).andReturn(expectedEvent3);
-        }
         metadataRepositoryControl.replay();
 
         List<AuditEvent> events =
@@ -450,8 +480,10 @@ public class AuditManagerTest
         Date ts3 = new Date( current.getTime() - 1000 );
         AuditEvent expectedEvent3 = createEvent( ts3 );
 
-
-        try(RepositorySession session = repositorySessionFactory.createSession()) {
+        sessionControl.reset();
+        factoryControl.reset();
+        EasyMock.expect( repositorySessionFactory.createSession() ).andStubReturn( session );
+        factoryControl.replay();
             EasyMock.expect(metadataRepository.getMetadataFacets(session, TEST_REPO_ID, AuditEvent.FACET_ID)).andReturn(
                     Arrays.asList(expectedEvent1.getName(), expectedEvent2.getName(), expectedEvent3.getName()));
 
@@ -464,7 +496,6 @@ public class AuditManagerTest
 
             EasyMock.expect(metadataRepository.getMetadataFacet(session, TEST_REPO_ID,
                     AuditEvent.FACET_ID, expectedEvent3.getName())).andReturn(expectedEvent3);
-        }
         metadataRepositoryControl.replay();
 
         List<AuditEvent> events =
@@ -490,8 +521,10 @@ public class AuditManagerTest
         expectedEvent2.setResource( "different-resource" );
         AuditEvent expectedEvent3 = createEvent( new Date( current.getTime() - 1000 ) );
 
-
-        try(RepositorySession session = repositorySessionFactory.createSession()) {
+        sessionControl.reset();
+        factoryControl.reset();
+        EasyMock.expect( repositorySessionFactory.createSession() ).andStubReturn( session );
+        factoryControl.replay();
             EasyMock.expect(metadataRepository.getMetadataFacets(session, TEST_REPO_ID, AuditEvent.FACET_ID)).andReturn(
                     Arrays.asList(expectedEvent1.getName(), expectedEvent2.getName(), expectedEvent3.getName()));
 
@@ -502,7 +535,6 @@ public class AuditManagerTest
 
             EasyMock.expect(metadataRepository.getMetadataFacet(session, TEST_REPO_ID, AuditEvent.FACET_ID, expectedEvent3.getName())).andReturn(expectedEvent3);
 
-        }
         metadataRepositoryControl.replay();
 
         List<AuditEvent> events =
@@ -528,7 +560,10 @@ public class AuditManagerTest
         Date ts3 = new Date( current.getTime() - 1000 );
         AuditEvent expectedEvent3 = createEvent( ts3 );
 
-        try(RepositorySession session = repositorySessionFactory.createSession()) {
+        sessionControl.reset();
+        factoryControl.reset();
+        EasyMock.expect( repositorySessionFactory.createSession() ).andStubReturn( session );
+        factoryControl.replay();
             EasyMock.expect(metadataRepository.getMetadataFacets(session, TEST_REPO_ID, AuditEvent.FACET_ID))
                     .andReturn(Arrays.asList(expectedEvent1.getName(), expectedEvent3.getName()));
 
@@ -544,7 +579,6 @@ public class AuditManagerTest
 
             EasyMock.expect(metadataRepository.getMetadataFacet(session, TEST_REPO_ID, AuditEvent.FACET_ID, expectedEvent3.getName()))
                     .andReturn(expectedEvent3);
-        }
         metadataRepositoryControl.replay();
 
         List<AuditEvent> events =
@@ -571,10 +605,12 @@ public class AuditManagerTest
         String name2 = createEvent( expectedTimestamp ).getName();
         String name3 = createEvent( new Date( current.getTime() - 1000 ) ).getName();
 
-        try(RepositorySession session = repositorySessionFactory.createSession()) {
+        sessionControl.reset();
+        factoryControl.reset();
+        EasyMock.expect( repositorySessionFactory.createSession() ).andStubReturn( session );
+        factoryControl.replay();
             EasyMock.expect(metadataRepository.getMetadataFacets(session, TEST_REPO_ID, AuditEvent.FACET_ID))
                     .andReturn(Arrays.asList(name1, name2, name3));
-        }
         metadataRepositoryControl.replay();
 
         List<AuditEvent> events =
index 6bd8c28c9d2c934ff0fe3326358ffc76079c8f78..e7ba4eda0228cd0e9983eafe3b1f817253a6f797 100644 (file)
@@ -22,8 +22,11 @@ package org.apache.archiva.metadata.repository.cassandra;
 import org.apache.archiva.metadata.model.MetadataFacetFactory;
 import org.apache.archiva.metadata.repository.AbstractMetadataRepositoryTest;
 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.cassandra.model.ProjectVersionMetadataModel;
+import org.easymock.EasyMock;
+import org.easymock.IMocksControl;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -49,10 +52,18 @@ public class CassandraMetadataRepositoryTest
 
     CassandraMetadataRepository cmr;
 
+    IMocksControl sessionFactoryControl;
+    RepositorySessionFactory sessionFactory;
+
+    IMocksControl sessionControl;
+    RepositorySession session;
+
+
+
     @Override
     protected RepositorySessionFactory getSessionFactory( )
     {
-        return null;
+        return sessionFactory;
     }
 
     @Override
@@ -78,6 +89,15 @@ public class CassandraMetadataRepositoryTest
 
         this.cmr = new CassandraMetadataRepository( factories, null, cassandraArchivaManager );
 
+        sessionFactoryControl = EasyMock.createControl( );
+        sessionFactory = sessionFactoryControl.createMock( RepositorySessionFactory.class );
+        sessionControl = EasyMock.createControl( );
+        session = sessionControl.createMock( RepositorySession.class );
+
+        EasyMock.expect( sessionFactory.createSession( ) ).andStubReturn( session );
+
+        sessionFactoryControl.replay();
+
         clearReposAndNamespace( cassandraArchivaManager );
     }
 
index 93ef8ce80486c9068f4258ffe478a155c969cc08..1795518d40ea584d30c6c715ce13b0ffcdb83b77 100644 (file)
@@ -42,7 +42,7 @@ public class FileMetadataRepositoryTest
 {
 
     private FileMetadataRepository repository;
-    private RepositorySessionFactory sessionFactory = null;
+    private RepositorySessionFactory sessionFactory = new FileRepositorySessionFactory();
 
     @Override
     protected MetadataRepository getRepository( )
index f9df40d7ce55afe7da0b84a410bb22573a3e2c65..00cef08285cc67b47efdf7de8e77e16d32f3fce3 100644 (file)
@@ -1522,7 +1522,7 @@ public class JcrMetadataRepository
             " AND [projectVersion].[jcr:path] LIKE '/repositories/%/content/%'" :
             " AND ISDESCENDANTNODE(projectVersion,'/" + getRepositoryContentPath( repositoryId ) + "')";
         List<ArtifactMetadata> result = new ArrayList<>( );
-        if (key!=null && Arrays.binarySearch( PROJECT_VERSION_VERSION_PROPERTIES, key )>=0)
+        if (key==null || (key!=null && Arrays.binarySearch( PROJECT_VERSION_VERSION_PROPERTIES, key )>=0))
         {
             // We search only for project version properties if the key is a valid property name
             String q1 =
index 09d0e3309eaf0240c5c086b7aa1cb7c55b8aaa21..965e42139e4db9bc63c67d1b17af66efd10fc72f 100644 (file)
@@ -282,4 +282,14 @@ public class DuplicateArtifactsConsumer
         initIncludes();
         configuration.addChangeListener( this );
     }
+
+    public RepositorySessionFactory getRepositorySessionFactory( )
+    {
+        return repositorySessionFactory;
+    }
+
+    public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
+    {
+        this.repositorySessionFactory = repositorySessionFactory;
+    }
 }
index 2e4acf9c1461218dfb067f116f555f40e952e74e..7a08ab116d2c3075c9daa66ab4f6dfe8c17ed2ef 100644 (file)
@@ -87,6 +87,8 @@ public class DuplicateArtifactsConsumerTest
 
     RepositorySessionFactory repositorySessionFactory;
 
+    RepositorySession session;
+
     @Before
     @Override
     public void setUp()
@@ -102,29 +104,29 @@ public class DuplicateArtifactsConsumerTest
         metadataRepository = mock( MetadataRepository.class );
         repositorySessionFactory = mock(RepositorySessionFactory.class);
 
-        RepositorySession session = mock( RepositorySession.class );
-        when( session.getRepository() ).thenReturn( metadataRepository );
+        session = mock( RepositorySession.class );
 
-        RepositorySessionFactory factory = applicationContext.getBean( RepositorySessionFactory.class );
-        //(RepositorySessionFactory) lookup( RepositorySessionFactory.class );
-        when( factory.createSession() ).thenReturn( session );
+        when( repositorySessionFactory.createSession( ) ).thenReturn( session );
+        when( session.getRepository() ).thenReturn( metadataRepository );
 
         when( pathTranslator.getArtifactForPath( TEST_REPO, TEST_FILE ) ).thenReturn( TEST_METADATA );
+
+        consumer.setRepositorySessionFactory( repositorySessionFactory );
+
     }
 
     @Test
     public void testConsumerArtifactNotDuplicated()
         throws Exception
     {
-        RepositorySession session = repositorySessionFactory.createSession();
-        when( metadataRepository.getArtifactsByChecksum(session , TEST_REPO, TEST_CHECKSUM ) ).thenReturn(
+        when( metadataRepository.getArtifactsByChecksum(eq(session) , eq(TEST_REPO), eq(TEST_CHECKSUM) ) ).thenReturn(
             Arrays.asList( TEST_METADATA ) );
 
         consumer.beginScan( config, new Date() );
         consumer.processFile( TEST_FILE );
         consumer.completeScan();
 
-        verify( metadataRepository, never() ).addMetadataFacet(session , eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
+        verify( metadataRepository, never() ).addMetadataFacet(eq(session) , eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
     }
 
     // TODO: Doesn't currently work
@@ -145,7 +147,6 @@ public class DuplicateArtifactsConsumerTest
     public void testConsumerArtifactDuplicated()
         throws Exception
     {
-        RepositorySession session = repositorySessionFactory.createSession();
         when( metadataRepository.getArtifactsByChecksum(session , TEST_REPO, TEST_CHECKSUM ) ).thenReturn(
             Arrays.asList( TEST_METADATA, createMetadata( "1.0" ) ) );
 
@@ -154,7 +155,7 @@ public class DuplicateArtifactsConsumerTest
         consumer.completeScan();
 
         ArgumentCaptor<RepositoryProblemFacet> argument = ArgumentCaptor.forClass( RepositoryProblemFacet.class );
-        verify( metadataRepository ).addMetadataFacet(session , eq( TEST_REPO ), argument.capture() );
+        verify( metadataRepository ).addMetadataFacet(eq(session) , eq( TEST_REPO ), argument.capture() );
         RepositoryProblemFacet problem = argument.getValue();
         assertProblem( problem );
     }
@@ -163,7 +164,6 @@ public class DuplicateArtifactsConsumerTest
     public void testConsumerArtifactDuplicatedButSelfNotInMetadataRepository()
         throws Exception
     {
-        RepositorySession session = repositorySessionFactory.createSession();
         when( metadataRepository.getArtifactsByChecksum(session , TEST_REPO, TEST_CHECKSUM ) ).thenReturn(
             Arrays.asList( createMetadata( "1.0" ) ) );
 
@@ -172,7 +172,7 @@ public class DuplicateArtifactsConsumerTest
         consumer.completeScan();
 
         ArgumentCaptor<RepositoryProblemFacet> argument = ArgumentCaptor.forClass( RepositoryProblemFacet.class );
-        verify( metadataRepository ).addMetadataFacet(session , eq( TEST_REPO ), argument.capture() );
+        verify( metadataRepository ).addMetadataFacet(eq(session) , eq( TEST_REPO ), argument.capture() );
         RepositoryProblemFacet problem = argument.getValue();
         assertProblem( problem );
     }
@@ -195,8 +195,7 @@ public class DuplicateArtifactsConsumerTest
         {
             consumer.completeScan();
         }
-        RepositorySession session = repositorySessionFactory.createSession();
-        verify( metadataRepository, never() ).addMetadataFacet(session , eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
+        verify( metadataRepository, never() ).addMetadataFacet(eq(session) , eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
     }
 
     @Test
@@ -207,16 +206,14 @@ public class DuplicateArtifactsConsumerTest
         // No exception unnecessarily for something we can't report on
         consumer.processFile( "com/example/invalid-artifact.txt" );
         consumer.completeScan();
-        RepositorySession session = repositorySessionFactory.createSession();
-        verify( metadataRepository, never() ).addMetadataFacet(session , eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
+        verify( metadataRepository, never() ).addMetadataFacet(eq(session) , eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
     }
 
     @Test
     public void testConsumerArtifactNotAnArtifactPathResults()
         throws Exception
     {
-        RepositorySession session = repositorySessionFactory.createSession();
-        when( metadataRepository.getArtifactsByChecksum(session , eq( TEST_REPO ), anyString() ) ).thenReturn(
+        when( metadataRepository.getArtifactsByChecksum(eq(session) , eq( TEST_REPO ), anyString() ) ).thenReturn(
             Arrays.asList( TEST_METADATA, createMetadata( "1.0" ) ) );
 
         // override, this feels a little overspecified though
@@ -228,7 +225,7 @@ public class DuplicateArtifactsConsumerTest
         consumer.processFile( "com/example/invalid-artifact.txt" );
         consumer.completeScan();
 
-        verify( metadataRepository, never() ).addMetadataFacet(session , eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
+        verify( metadataRepository, never() ).addMetadataFacet(eq(session) , eq( TEST_REPO ), Matchers.<MetadataFacet>anyObject() );
     }
 
     private static void assertProblem( RepositoryProblemFacet problem )
index 0bc82e0aa3a230837cc5891e5227e944aa9a1434..3c5ae8eefde3cd6b1ed8b0dd6785cd4ccc2e0c8f 100644 (file)
@@ -55,25 +55,30 @@ public class DefaultRepositoryStatisticsManager
 
     private RepositoryWalkingStatisticsProvider walkingProvider = new RepositoryWalkingStatisticsProvider();
 
+
+
     @Inject
     RepositorySessionFactory repositorySessionFactory;
 
     @Override
-    public boolean hasStatistics( MetadataRepository metadataRepository, String repositoryId )
+    public boolean hasStatistics( String repositoryId )
         throws MetadataRepositoryException
     {
         try(RepositorySession session = repositorySessionFactory.createSession()) {
+            final MetadataRepository metadataRepository = session.getRepository( );
             return metadataRepository.hasMetadataFacet(session, repositoryId, DefaultRepositoryStatistics.FACET_ID);
         }
     }
 
     @Override
-    public RepositoryStatistics getLastStatistics( MetadataRepository metadataRepository, String repositoryId )
+    public RepositoryStatistics getLastStatistics( String repositoryId )
         throws MetadataRepositoryException
     {
         StopWatch stopWatch = new StopWatch();
         stopWatch.start();
         try(RepositorySession session = repositorySessionFactory.createSession()) {
+            final MetadataRepository metadataRepository = session.getRepository( );
+
             // TODO: consider a more efficient implementation that directly gets the last one from the content repository
             List<String> scans = metadataRepository.getMetadataFacets(session, repositoryId, DefaultRepositoryStatistics.FACET_ID);
             if (scans == null) {
@@ -95,11 +100,13 @@ public class DefaultRepositoryStatisticsManager
     }
 
     @Override
-    public void addStatisticsAfterScan( MetadataRepository metadataRepository, String repositoryId, Date startTime,
+    public void addStatisticsAfterScan( String repositoryId, Date startTime,
                                         Date endTime, long totalFiles, long newFiles )
         throws MetadataRepositoryException
     {
         try(RepositorySession session = repositorySessionFactory.createSession()) {
+            final MetadataRepository metadataRepository = session.getRepository( );
+
             DefaultRepositoryStatistics repositoryStatistics = new DefaultRepositoryStatistics();
             repositoryStatistics.setRepositoryId(repositoryId);
             repositoryStatistics.setScanStartTime(startTime);
@@ -130,20 +137,22 @@ public class DefaultRepositoryStatisticsManager
     }
 
     @Override
-    public void deleteStatistics( MetadataRepository metadataRepository, String repositoryId )
+    public void deleteStatistics( String repositoryId )
         throws MetadataRepositoryException
     {
         try(RepositorySession session = repositorySessionFactory.createSession()) {
+            final MetadataRepository metadataRepository = session.getRepository( );
             metadataRepository.removeMetadataFacets(session, repositoryId, DefaultRepositoryStatistics.FACET_ID);
         }
     }
 
     @Override
-    public List<RepositoryStatistics> getStatisticsInRange( MetadataRepository metadataRepository, String repositoryId,
+    public List<RepositoryStatistics> getStatisticsInRange( String repositoryId,
                                                             Date startTime, Date endTime )
         throws MetadataRepositoryException
     {
         try(RepositorySession session = repositorySessionFactory.createSession()) {
+            final MetadataRepository metadataRepository = session.getRepository( );
             List<RepositoryStatistics> results = new ArrayList<>();
             List<String> list = metadataRepository.getMetadataFacets(session, repositoryId, DefaultRepositoryStatistics.FACET_ID);
             Collections.sort(list, Collections.reverseOrder());
@@ -173,4 +182,14 @@ public class DefaultRepositoryStatisticsManager
         fmt.setTimeZone( UTC_TIME_ZONE );
         return fmt;
     }
+
+    public RepositorySessionFactory getRepositorySessionFactory( )
+    {
+        return repositorySessionFactory;
+    }
+
+    public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
+    {
+        this.repositorySessionFactory = repositorySessionFactory;
+    }
 }
index 0c31249e41cbdd6d02851bdfd43b13f301b6e3b0..f888f1f1afaaa6f308a90b5627abdbaf7309f11e 100644 (file)
@@ -43,8 +43,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.TimeZone;
 
-import static org.easymock.EasyMock.createControl;
-import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.*;
 
 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
 public class RepositoryStatisticsManagerTest
@@ -68,6 +67,8 @@ public class RepositoryStatisticsManagerTest
 
     private RepositorySessionFactory repositorySessionFactory;
     private IMocksControl factoryControl;
+    private IMocksControl sessionControl;
+    private RepositorySession session;
 
     private static SimpleDateFormat createTimestampFormat()
     {
@@ -90,6 +91,12 @@ public class RepositoryStatisticsManagerTest
 
         factoryControl = createControl();
         repositorySessionFactory = factoryControl.createMock(RepositorySessionFactory.class);
+
+        repositoryStatisticsManager.setRepositorySessionFactory( repositorySessionFactory );
+
+        sessionControl = createControl( );
+        session = sessionControl.createMock( RepositorySession.class );
+
     }
 
     @Test
@@ -111,16 +118,23 @@ public class RepositoryStatisticsManagerTest
         stats.setTotalFileCount( 56229 );
 
 
-        try(RepositorySession session = repositorySessionFactory.createSession()) {
-            expect(metadataRepository.getMetadataFacets(session, TEST_REPO_ID, DefaultRepositoryStatistics.FACET_ID)).andReturn(
+        sessionControl.reset();
+        factoryControl.reset();
+        expect( repositorySessionFactory.createSession( ) ).andStubReturn( session );
+        expect( session.getRepository() ).andStubReturn( metadataRepository );
+        session.close();
+        expectLastCall( ).anyTimes( );
+        factoryControl.replay();
+        sessionControl.replay();
+
+        expect(metadataRepository.getMetadataFacets(session, TEST_REPO_ID, DefaultRepositoryStatistics.FACET_ID)).andReturn(
                     Arrays.asList(FIRST_TEST_SCAN, SECOND_TEST_SCAN));
 
-            expect(metadataRepository.getMetadataFacet(session, TEST_REPO_ID,
+        expect(metadataRepository.getMetadataFacet(session, TEST_REPO_ID,
                     DefaultRepositoryStatistics.FACET_ID, SECOND_TEST_SCAN)).andReturn(stats);
-        }
         metadataRepositoryControl.replay();
 
-        stats = repositoryStatisticsManager.getLastStatistics( metadataRepository, TEST_REPO_ID );
+        stats = repositoryStatisticsManager.getLastStatistics( TEST_REPO_ID );
         assertNotNull( stats );
         assertEquals( 1314527915L, stats.getTotalArtifactFileSize() );
         assertEquals( 123, stats.getNewFileCount() );
@@ -140,12 +154,20 @@ public class RepositoryStatisticsManagerTest
         throws Exception
     {
 
-        RepositorySession session = repositorySessionFactory.createSession();
+        sessionControl.reset();
+        factoryControl.reset();
+        expect( repositorySessionFactory.createSession( ) ).andStubReturn( session );
+        expect( session.getRepository() ).andStubReturn( metadataRepository );
+        session.close();
+        expectLastCall( ).anyTimes( );
+        factoryControl.replay();
+        sessionControl.replay();
+
         expect( metadataRepository.getMetadataFacets(session , TEST_REPO_ID, DefaultRepositoryStatistics.FACET_ID ) ).andReturn(
             Collections.<String>emptyList() );
         metadataRepositoryControl.replay();
 
-        RepositoryStatistics stats = repositoryStatisticsManager.getLastStatistics( metadataRepository, TEST_REPO_ID );
+        RepositoryStatistics stats = repositoryStatisticsManager.getLastStatistics( TEST_REPO_ID );
         assertNull( stats );
 
         metadataRepositoryControl.verify();
@@ -161,8 +183,15 @@ public class RepositoryStatisticsManagerTest
         RepositoryStatistics stats = createTestStats( startTime, current );
 
         walkRepository( 1 );
-        RepositorySession session = repositorySessionFactory.createSession();
 
+        sessionControl.reset();
+        factoryControl.reset();
+        expect( repositorySessionFactory.createSession( ) ).andStubReturn( session );
+        expect( session.getRepository() ).andStubReturn( metadataRepository );
+        session.close();
+        expectLastCall( ).anyTimes( );
+        factoryControl.replay();
+        sessionControl.replay();
 
         metadataRepository.addMetadataFacet(session , TEST_REPO_ID, stats );
 
@@ -174,10 +203,10 @@ public class RepositoryStatisticsManagerTest
 
         metadataRepositoryControl.replay();
 
-        repositoryStatisticsManager.addStatisticsAfterScan( metadataRepository, TEST_REPO_ID, startTime, current, 56345,
+        repositoryStatisticsManager.addStatisticsAfterScan( TEST_REPO_ID, startTime, current, 56345,
                                                             45 );
 
-        stats = repositoryStatisticsManager.getLastStatistics( metadataRepository, TEST_REPO_ID );
+        stats = repositoryStatisticsManager.getLastStatistics( TEST_REPO_ID );
         assertNotNull( stats );
         assertEquals( 246900, stats.getTotalArtifactFileSize() );
         assertEquals( 45, stats.getNewFileCount() );
@@ -201,7 +230,15 @@ public class RepositoryStatisticsManagerTest
 
         Date startTime1 = new Date( current.getTime() - 12345 );
         DefaultRepositoryStatistics stats1 = createTestStats( startTime1, new Date( current.getTime() - 6000 ) );
-        RepositorySession session = repositorySessionFactory.createSession();
+
+        sessionControl.reset();
+        factoryControl.reset();
+        expect( repositorySessionFactory.createSession( ) ).andStubReturn( session );
+        expect( session.getRepository() ).andStubReturn( metadataRepository );
+        session.close();
+        expectLastCall( ).anyTimes( );
+        factoryControl.replay();
+        sessionControl.replay();
 
         metadataRepository.addMetadataFacet(session , TEST_REPO_ID, stats1 );
 
@@ -222,16 +259,16 @@ public class RepositoryStatisticsManagerTest
 
         metadataRepositoryControl.replay();
 
-        repositoryStatisticsManager.addStatisticsAfterScan( metadataRepository, TEST_REPO_ID, startTime1,
+        repositoryStatisticsManager.addStatisticsAfterScan( TEST_REPO_ID, startTime1,
                                                             stats1.getScanEndTime(), 56345, 45 );
-        repositoryStatisticsManager.addStatisticsAfterScan( metadataRepository, TEST_REPO_ID, startTime2,
+        repositoryStatisticsManager.addStatisticsAfterScan( TEST_REPO_ID, startTime2,
                                                             stats2.getScanEndTime(), 56345, 45 );
 
-        assertNotNull( repositoryStatisticsManager.getLastStatistics( metadataRepository, TEST_REPO_ID ) );
+        assertNotNull( repositoryStatisticsManager.getLastStatistics( TEST_REPO_ID ) );
 
-        repositoryStatisticsManager.deleteStatistics( metadataRepository, TEST_REPO_ID );
+        repositoryStatisticsManager.deleteStatistics( TEST_REPO_ID );
 
-        assertNull( repositoryStatisticsManager.getLastStatistics( metadataRepository, TEST_REPO_ID ) );
+        assertNull( repositoryStatisticsManager.getLastStatistics( TEST_REPO_ID ) );
 
         metadataRepositoryControl.verify();
     }
@@ -240,7 +277,14 @@ public class RepositoryStatisticsManagerTest
     public void testDeleteStatsWhenEmpty()
         throws Exception
     {
-        RepositorySession session = repositorySessionFactory.createSession();
+        sessionControl.reset();
+        factoryControl.reset();
+        expect( repositorySessionFactory.createSession( ) ).andStubReturn( session );
+        expect( session.getRepository() ).andStubReturn( metadataRepository );
+        session.close();
+        expectLastCall( ).anyTimes( );
+        factoryControl.replay();
+        sessionControl.replay();
 
         expect( metadataRepository.getMetadataFacets(session , TEST_REPO_ID, DefaultRepositoryStatistics.FACET_ID ) ).andReturn(
             Collections.<String>emptyList() ).times( 2 );
@@ -248,11 +292,11 @@ public class RepositoryStatisticsManagerTest
 
         metadataRepositoryControl.replay();
 
-        assertNull( repositoryStatisticsManager.getLastStatistics( metadataRepository, TEST_REPO_ID ) );
+        assertNull( repositoryStatisticsManager.getLastStatistics( TEST_REPO_ID ) );
 
-        repositoryStatisticsManager.deleteStatistics( metadataRepository, TEST_REPO_ID );
+        repositoryStatisticsManager.deleteStatistics( TEST_REPO_ID );
 
-        assertNull( repositoryStatisticsManager.getLastStatistics( metadataRepository, TEST_REPO_ID ) );
+        assertNull( repositoryStatisticsManager.getLastStatistics( TEST_REPO_ID ) );
 
         metadataRepositoryControl.verify();
     }
@@ -265,14 +309,22 @@ public class RepositoryStatisticsManagerTest
 
         Date current = new Date();
 
+        sessionControl.reset();
+        factoryControl.reset();
+        expect( repositorySessionFactory.createSession( ) ).andStubReturn( session );
+        expect( session.getRepository() ).andStubReturn( metadataRepository );
+        session.close();
+        expectLastCall( ).anyTimes( );
+        factoryControl.replay();
+        sessionControl.replay();
+
+
         addStats( new Date( current.getTime() - 12345 ), new Date( current.getTime() - 6000 ) );
         addStats( new Date( current.getTime() - 3000 ), new Date( current.getTime() - 2000 ) );
         addStats( new Date( current.getTime() - 1000 ), current );
 
         ArrayList<String> keys = new ArrayList<>( statsCreated.keySet() );
 
-        RepositorySession session = repositorySessionFactory.createSession();
-
         expect( metadataRepository.getMetadataFacets(session , TEST_REPO_ID, DefaultRepositoryStatistics.FACET_ID ) ).andReturn( keys );
 
         // only match the middle one
@@ -286,13 +338,13 @@ public class RepositoryStatisticsManagerTest
 
         for ( RepositoryStatistics stats : statsCreated.values() )
         {
-            repositoryStatisticsManager.addStatisticsAfterScan( metadataRepository, TEST_REPO_ID,
+            repositoryStatisticsManager.addStatisticsAfterScan( TEST_REPO_ID,
                                                                 stats.getScanStartTime(), stats.getScanEndTime(), 56345,
                                                                 45 );
         }
 
         List<RepositoryStatistics> list =
-            repositoryStatisticsManager.getStatisticsInRange( metadataRepository, TEST_REPO_ID,
+            repositoryStatisticsManager.getStatisticsInRange( TEST_REPO_ID,
                                                               new Date( current.getTime() - 4000 ),
                                                               new Date( current.getTime() - 2000 ) );
 
@@ -310,14 +362,21 @@ public class RepositoryStatisticsManagerTest
 
         Date current = new Date();
 
+        sessionControl.reset();
+        factoryControl.reset();
+        expect( repositorySessionFactory.createSession( ) ).andStubReturn( session );
+        expect( session.getRepository() ).andStubReturn( metadataRepository );
+        session.close();
+        expectLastCall( ).anyTimes( );
+        factoryControl.replay();
+        sessionControl.replay();
+
         addStats( new Date( current.getTime() - 12345 ), new Date( current.getTime() - 6000 ) );
         addStats( new Date( current.getTime() - 3000 ), new Date( current.getTime() - 2000 ) );
         addStats( new Date( current.getTime() - 1000 ), current );
 
         List<String> keys = new ArrayList<>( statsCreated.keySet() );
 
-        RepositorySession session = repositorySessionFactory.createSession();
-
         expect( metadataRepository.getMetadataFacets(session , TEST_REPO_ID, DefaultRepositoryStatistics.FACET_ID ) ).andReturn( keys );
 
         String key = keys.get( 1 );
@@ -336,13 +395,13 @@ public class RepositoryStatisticsManagerTest
 
         for ( RepositoryStatistics stats : statsCreated.values() )
         {
-            repositoryStatisticsManager.addStatisticsAfterScan( metadataRepository, TEST_REPO_ID,
+            repositoryStatisticsManager.addStatisticsAfterScan( TEST_REPO_ID,
                                                                 stats.getScanStartTime(), stats.getScanEndTime(), 56345,
                                                                 45 );
         }
 
         List<RepositoryStatistics> list =
-            repositoryStatisticsManager.getStatisticsInRange( metadataRepository, TEST_REPO_ID,
+            repositoryStatisticsManager.getStatisticsInRange( TEST_REPO_ID,
                                                               new Date( current.getTime() - 4000 ), current );
 
         assertEquals( 2, list.size() );
@@ -360,14 +419,21 @@ public class RepositoryStatisticsManagerTest
 
         Date current = new Date();
 
+        sessionControl.reset();
+        factoryControl.reset();
+        expect( repositorySessionFactory.createSession( ) ).andStubReturn( session );
+        expect( session.getRepository() ).andStubReturn( metadataRepository );
+        session.close();
+        expectLastCall( ).anyTimes( );
+        factoryControl.replay();
+        sessionControl.replay();
+
         addStats( new Date( current.getTime() - 12345 ), new Date( current.getTime() - 6000 ) );
         addStats( new Date( current.getTime() - 3000 ), new Date( current.getTime() - 2000 ) );
         addStats( new Date( current.getTime() - 1000 ), current );
 
         List<String> keys = new ArrayList<>( statsCreated.keySet() );
 
-        RepositorySession session = repositorySessionFactory.createSession();
-
         expect( metadataRepository.getMetadataFacets(session , TEST_REPO_ID, DefaultRepositoryStatistics.FACET_ID ) ).andReturn( keys );
 
         String key = keys.get( 0 );
@@ -383,13 +449,13 @@ public class RepositoryStatisticsManagerTest
 
         for ( RepositoryStatistics stats : statsCreated.values() )
         {
-            repositoryStatisticsManager.addStatisticsAfterScan( metadataRepository, TEST_REPO_ID,
+            repositoryStatisticsManager.addStatisticsAfterScan( TEST_REPO_ID,
                                                                 stats.getScanStartTime(), stats.getScanEndTime(), 56345,
                                                                 45 );
         }
 
         List<RepositoryStatistics> list =
-            repositoryStatisticsManager.getStatisticsInRange( metadataRepository, TEST_REPO_ID,
+            repositoryStatisticsManager.getStatisticsInRange( TEST_REPO_ID,
                                                               new Date( current.getTime() - 20000 ),
                                                               new Date( current.getTime() - 2000 ) );
 
@@ -408,14 +474,21 @@ public class RepositoryStatisticsManagerTest
 
         Date current = new Date();
 
+        sessionControl.reset();
+        factoryControl.reset();
+        expect( repositorySessionFactory.createSession( ) ).andStubReturn( session );
+        expect( session.getRepository() ).andStubReturn( metadataRepository );
+        session.close();
+        expectLastCall( ).anyTimes( );
+        factoryControl.replay();
+        sessionControl.replay();
+
         addStats( new Date( current.getTime() - 12345 ), new Date( current.getTime() - 6000 ) );
         addStats( new Date( current.getTime() - 3000 ), new Date( current.getTime() - 2000 ) );
         addStats( new Date( current.getTime() - 1000 ), current );
 
         ArrayList<String> keys = new ArrayList<>( statsCreated.keySet() );
 
-        RepositorySession session = repositorySessionFactory.createSession();
-
         expect( metadataRepository.getMetadataFacets(session , TEST_REPO_ID, DefaultRepositoryStatistics.FACET_ID ) ).andReturn( keys );
 
         String key = keys.get( 0 );
@@ -435,13 +508,13 @@ public class RepositoryStatisticsManagerTest
 
         for ( RepositoryStatistics stats : statsCreated.values() )
         {
-            repositoryStatisticsManager.addStatisticsAfterScan( metadataRepository, TEST_REPO_ID,
+            repositoryStatisticsManager.addStatisticsAfterScan( TEST_REPO_ID,
                                                                 stats.getScanStartTime(), stats.getScanEndTime(), 56345,
                                                                 45 );
         }
 
         List<RepositoryStatistics> list =
-            repositoryStatisticsManager.getStatisticsInRange( metadataRepository, TEST_REPO_ID,
+            repositoryStatisticsManager.getStatisticsInRange( TEST_REPO_ID,
                                                               new Date( current.getTime() - 20000 ), current );
 
         assertEquals( 3, list.size() );
@@ -459,6 +532,14 @@ public class RepositoryStatisticsManagerTest
         walkRepository( 3 );
 
         Date current = new Date();
+        sessionControl.reset();
+        factoryControl.reset();
+        expect( repositorySessionFactory.createSession( ) ).andStubReturn( session );
+        expect( session.getRepository() ).andStubReturn( metadataRepository );
+        session.close();
+        expectLastCall( ).anyTimes( );
+        factoryControl.replay();
+        sessionControl.replay();
 
         addStats( new Date( current.getTime() - 12345 ), new Date( current.getTime() - 6000 ) );
         addStats( new Date( current.getTime() - 3000 ), new Date( current.getTime() - 2000 ) );
@@ -466,21 +547,19 @@ public class RepositoryStatisticsManagerTest
 
         ArrayList<String> keys = new ArrayList<>( statsCreated.keySet() );
 
-        RepositorySession session = repositorySessionFactory.createSession();
-
         expect( metadataRepository.getMetadataFacets(session , TEST_REPO_ID, DefaultRepositoryStatistics.FACET_ID ) ).andReturn( keys );
 
         metadataRepositoryControl.replay();
 
         for ( RepositoryStatistics stats : statsCreated.values() )
         {
-            repositoryStatisticsManager.addStatisticsAfterScan( metadataRepository, TEST_REPO_ID,
+            repositoryStatisticsManager.addStatisticsAfterScan( TEST_REPO_ID,
                                                                 stats.getScanStartTime(), stats.getScanEndTime(), 56345,
                                                                 45 );
         }
 
         List<RepositoryStatistics> list =
-            repositoryStatisticsManager.getStatisticsInRange( metadataRepository, TEST_REPO_ID,
+            repositoryStatisticsManager.getStatisticsInRange( TEST_REPO_ID,
                                                               new Date( current.getTime() - 20000 ),
                                                               new Date( current.getTime() - 16000 ) );
 
@@ -537,7 +616,9 @@ public class RepositoryStatisticsManagerTest
     private void walkRepository( int count )
         throws Exception
     {
-        RepositorySession session = repositorySessionFactory.createSession();
+        sessionControl.reset();
+        expect( repositorySessionFactory.createSession( ) ).andStubReturn( session );
+        factoryControl.replay();
 
         for ( int i = 0; i < count; i++ )
         {
index c04f28e4593aee5a7510b23a4ceb5c4e3b2c4fb8..90ed7728607528bd41683ddefb62de979e029f04 100644 (file)
       <groupId>org.apache.archiva</groupId>
       <artifactId>archiva-model</artifactId>
     </dependency>
-    <dependency>
-      <groupId>org.apache.archiva</groupId>
-      <artifactId>metadata-store-jcr</artifactId>
-    </dependency>
     <dependency>
       <groupId>org.mockito</groupId>
       <artifactId>mockito-all</artifactId>
index eab7ea9acd4c5e33817c4b15dfc920a7cf5d5a4d..a36d7c11fb9567a4150448136bcd94ae2c164f0f 100644 (file)
@@ -417,5 +417,13 @@ public class Maven2RepositoryMerger
         }
     }
 
+    public RepositorySessionFactory getRepositorySessionFactory( )
+    {
+        return repositorySessionFactory;
+    }
 
+    public void setRepositorySessionFactory( RepositorySessionFactory repositorySessionFactory )
+    {
+        this.repositorySessionFactory = repositorySessionFactory;
+    }
 }
index 50bf74570cb1f76cc4a5344e70bad714dfbb54dd..9690360bc5a9926f41dc233bb657f219223d0413 100644 (file)
@@ -33,7 +33,7 @@ import java.util.Locale;
 public class StubConfiguration
     implements ArchivaConfiguration
 {
-    private Configuration configuration;
+    private Configuration configuration = new Configuration();
 
     @Override
     public Configuration getConfiguration()
@@ -57,7 +57,7 @@ public class StubConfiguration
     @Override
     public void addListener( ConfigurationListener listener )
     {
-        throw new UnsupportedOperationException();
+        // throw new UnsupportedOperationException();
     }
 
     @Override
@@ -69,7 +69,7 @@ public class StubConfiguration
     @Override
     public void addChangeListener( RegistryListener listener )
     {
-        throw new UnsupportedOperationException();
+        // throw new UnsupportedOperationException();
     }
 
     @Override
@@ -123,7 +123,7 @@ public class StubConfiguration
 
     @Override
     public Path getDataDirectory() {
-        if (configuration!=null && StringUtils.isNotEmpty(configuration.getArchivaRuntimeConfiguration().getDataDirectory())) {
+        if (configuration!=null && configuration.getArchivaRuntimeConfiguration()!=null && StringUtils.isNotEmpty(configuration.getArchivaRuntimeConfiguration().getDataDirectory())) {
             Path dataDir = Paths.get(configuration.getArchivaRuntimeConfiguration().getDataDirectory());
             if (dataDir.isAbsolute()) {
                 return dataDir;
index 7405b6480b2c700d138cbfc8dd1900ce6479d093..2ac7e8b75740be6e4c087d9c46d2bb6c038beff2 100644 (file)
@@ -26,10 +26,13 @@ import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
 import org.apache.archiva.configuration.RepositoryScanningConfiguration;
 import org.apache.archiva.metadata.model.ArtifactMetadata;
 import org.apache.archiva.metadata.repository.MetadataRepository;
+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.repository.Repository;
 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
 import org.junit.Before;
+import org.junit.BeforeClass;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.MockitoAnnotations;
@@ -60,7 +63,31 @@ public class Maven2RepositoryMergerTest
 
     private MetadataRepository metadataRepository;
 
-    private RepositorySessionFactory repositorySessionFactory;
+    private static RepositorySessionFactory repositorySessionFactory;
+
+    private static RepositorySession session;
+
+    static
+    {
+        repositorySessionFactory = mock(RepositorySessionFactory.class);
+        session = mock( RepositorySession.class );
+
+        try
+        {
+            when( repositorySessionFactory.createSession( ) ).thenReturn( session );
+        }
+        catch ( MetadataRepositoryException e )
+        {
+            throw new RuntimeException( e );
+        }
+
+    }
+
+    public static RepositorySessionFactory getRepositorySessionFactory() {
+        return repositorySessionFactory;
+    }
+
+
 
     @Before
     @Override
@@ -70,7 +97,8 @@ public class Maven2RepositoryMergerTest
         super.setUp();
         MockitoAnnotations.initMocks( this );
         metadataRepository = mock( MetadataRepository.class );
-        repositorySessionFactory = mock(RepositorySessionFactory.class);
+        repositoryMerger.setRepositorySessionFactory( repositorySessionFactory );
+
     }
 
     private List<ArtifactMetadata> getArtifacts()
@@ -125,11 +153,10 @@ public class Maven2RepositoryMergerTest
         c.addManagedRepository( targetRepo );
         configuration.save( c );
 
-        try(RepositorySession session = repositorySessionFactory.createSession()) {
+
             when(metadataRepository.getArtifacts(session, TEST_REPO_ID)).thenReturn(getArtifacts());
             repositoryMerger.merge(metadataRepository, TEST_REPO_ID, "target-rep");
             verify(metadataRepository).getArtifacts(session, TEST_REPO_ID);
-        }
         assertTrue( Files.exists(mergedArtifact) );
         assertTrue( Files.exists(mavenMetadata) );
         assertTrue( Files.exists(pom) );
@@ -176,14 +203,12 @@ public class Maven2RepositoryMergerTest
             "/target/test-repository/com/example/test/test-artifact/1.0-SNAPSHOT/test-artifact-1.0-20100308.230825-1.jar" );
         targetRepoFile.toFile().setReadOnly();
 
-        try(RepositorySession session = repositorySessionFactory.createSession()) {
             when(metadataRepository.getArtifacts(session, sourceRepoId)).thenReturn(sourceRepoArtifactsList);
             when(metadataRepository.getArtifacts(session, TEST_REPO_ID)).thenReturn(targetRepoArtifactsList);
 
             assertEquals(1, repositoryMerger.getConflictingArtifacts(metadataRepository, sourceRepoId,
                     TEST_REPO_ID).size());
             verify(metadataRepository).getArtifacts(session, TEST_REPO_ID);
-        }
     }
 
 }
\ No newline at end of file
index db41eff4d14a1ab2dabef303d037757017040139..87b681be22380385a002d9df4d9065b221bbfdd4 100644 (file)
@@ -32,4 +32,8 @@
   <alias name="archivaConfiguration#mocked" alias="archivaConfiguration"/>
   <alias name="archivaConfiguration#mocked" alias="archivaConfiguration#default" />
 
+  <bean name="repositorySessionFactory#mock"
+        class="org.apache.archiva.stagerepository.merge.Maven2RepositoryMergerTest" factory-method="getRepositorySessionFactory" />
+ <alias alias="repositorySessionFactory#jcr" name="repositorySessionFactory#mock" />
+
 </beans>
\ No newline at end of file