From 808aa92aa0962ef981dd9fa9c7c1d82fc2ac32b7 Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Wed, 14 Sep 2011 10:23:09 +0000 Subject: [PATCH] more cleanup of direct use of archiva-configuration git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1170514 13f79535-47bb-0310-9956-ffa450edef68 --- .../action/reports/GenerateReportAction.java | 22 ++++++-- .../reports/GenerateReportActionTest.java | 52 +++++++++++++++++-- 2 files changed, 64 insertions(+), 10 deletions(-) diff --git a/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/reports/GenerateReportAction.java b/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/reports/GenerateReportAction.java index c43749602..547858444 100644 --- a/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/reports/GenerateReportAction.java +++ b/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/reports/GenerateReportAction.java @@ -20,6 +20,8 @@ package org.apache.maven.archiva.web.action.reports; */ import com.opensymphony.xwork2.Preparable; +import org.apache.archiva.admin.model.RepositoryAdminException; +import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin; import org.apache.archiva.metadata.repository.MetadataRepository; import org.apache.archiva.metadata.repository.MetadataRepositoryException; import org.apache.archiva.metadata.repository.RepositorySession; @@ -30,7 +32,6 @@ import org.apache.archiva.security.common.ArchivaRoleConstants; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.time.DateUtils; -import org.apache.maven.archiva.configuration.ArchivaConfiguration; import org.apache.maven.archiva.web.action.AbstractRepositoryBasedAction; import org.codehaus.plexus.redback.rbac.Resource; import org.codehaus.redback.integration.interceptor.SecureAction; @@ -78,7 +79,7 @@ public class GenerateReportAction private Logger log = LoggerFactory.getLogger( GenerateReportAction.class ); @Inject - private ArchivaConfiguration archivaConfiguration; + private ManagedRepositoryAdmin managedRepositoryAdmin; @Inject private RepositoryStatisticsManager repositoryStatisticsManager; @@ -99,7 +100,7 @@ public class GenerateReportAction private int numPages; - private Collection repositoryIds; + private List repositoryIds; private Map> repositoriesMap = new TreeMap>(); @@ -114,6 +115,7 @@ public class GenerateReportAction @SuppressWarnings( "unchecked" ) public void prepare() + throws RepositoryAdminException { repositoryIds = new ArrayList(); repositoryIds.add( ALL_REPOSITORIES ); // comes first to be first in the list @@ -122,7 +124,7 @@ public class GenerateReportAction availableRepositories = new ArrayList(); // remove selected repositories in the option for the statistics report - availableRepositories.addAll( archivaConfiguration.getConfiguration().getManagedRepositoriesAsMap().keySet() ); + availableRepositories.addAll( managedRepositoryAdmin.getManagedRepositoriesAsMap().keySet() ); for ( String repo : selectedRepositories ) { if ( availableRepositories.contains( repo ) ) @@ -630,7 +632,7 @@ public class GenerateReportAction return bundle; } - public Collection getRepositoryIds() + public List getRepositoryIds() { return repositoryIds; } @@ -759,4 +761,14 @@ public class GenerateReportAction { this.repositoryStatisticsManager = repositoryStatisticsManager; } + + public ManagedRepositoryAdmin getManagedRepositoryAdmin() + { + return managedRepositoryAdmin; + } + + public void setManagedRepositoryAdmin( ManagedRepositoryAdmin managedRepositoryAdmin ) + { + this.managedRepositoryAdmin = managedRepositoryAdmin; + } } diff --git a/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/reports/GenerateReportActionTest.java b/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/reports/GenerateReportActionTest.java index 21ccd53b5..d43acc87f 100644 --- a/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/reports/GenerateReportActionTest.java +++ b/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/reports/GenerateReportActionTest.java @@ -21,6 +21,8 @@ package org.apache.maven.archiva.web.action.reports; import com.google.common.collect.Lists; import com.opensymphony.xwork2.Action; +import org.apache.archiva.admin.model.managed.ManagedRepository; +import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin; import org.apache.archiva.metadata.model.MetadataFacet; import org.apache.archiva.metadata.repository.MetadataRepository; import org.apache.archiva.metadata.repository.RepositorySession; @@ -28,8 +30,8 @@ import org.apache.archiva.metadata.repository.memory.TestRepositorySessionFactor import org.apache.archiva.metadata.repository.stats.RepositoryStatistics; import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager; import org.apache.archiva.reports.RepositoryProblemFacet; -import org.apache.commons.io.IOUtils; import org.apache.archiva.security.UserRepositoriesStub; +import org.apache.commons.io.IOUtils; import org.apache.maven.archiva.web.action.AbstractActionTestCase; import org.easymock.MockControl; import org.junit.After; @@ -39,7 +41,9 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Date; +import java.util.HashMap; import java.util.List; +import java.util.Map; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -70,14 +74,13 @@ public class GenerateReportActionTest private static final String PROBLEM = "problem"; + @Override protected void setUp() throws Exception { super.setUp(); - //action = (GenerateReportAction) lookup( Action.class, "generateReport" ); - UserRepositoriesStub stub = applicationContext.getBean( "userRepositories", UserRepositoriesStub.class ); stub.setRepoIds( Lists.newArrayList( "internal", "snapshots" ) ); @@ -104,21 +107,50 @@ public class GenerateReportActionTest { UserRepositoriesStub stub = applicationContext.getBean( "userRepositories", UserRepositoriesStub.class ); stub.setRepoIds( Lists.newArrayList( "test-repo" ) ); + super.tearDown(); } private void prepareAction( List selectedRepositories, List availableRepositories ) + throws Exception { + MockControl managedRepositoryControl = MockControl.createControl( ManagedRepositoryAdmin.class ); + ManagedRepositoryAdmin managedRepositoryAdmin = (ManagedRepositoryAdmin) managedRepositoryControl.getMock(); + + Map map = new HashMap( availableRepositories.size() ); + for ( String repoId : availableRepositories ) + { + map.put( repoId, new ManagedRepository() ); + } + + managedRepositoryControl.expectAndReturn( managedRepositoryAdmin.getManagedRepositoriesAsMap(), map, 1, 10 ); + + managedRepositoryControl.replay(); + action.setManagedRepositoryAdmin( managedRepositoryAdmin ); + + action.setSelectedRepositories( selectedRepositories ); action.prepare(); - assertEquals( Arrays.asList( GenerateReportAction.ALL_REPOSITORIES, INTERNAL, SNAPSHOTS ), - action.getRepositoryIds() ); + List repos = Arrays.asList( GenerateReportAction.ALL_REPOSITORIES, INTERNAL, SNAPSHOTS ); + + Collections.sort( repos ); + + Collections.sort( action.getRepositoryIds() ); + + assertEquals( repos, action.getRepositoryIds() ); + Collections.sort( action.getAvailableRepositories() ); + + availableRepositories = new ArrayList( availableRepositories ); + Collections.sort( availableRepositories ); + + assertEquals( availableRepositories, action.getAvailableRepositories() ); } public void testGenerateStatisticsInvalidRowCount() + throws Exception { repositoryStatisticsManagerControl.replay(); prepareAction( Collections.singletonList( INTERNAL ), Collections.singletonList( SNAPSHOTS ) ); @@ -131,6 +163,7 @@ public class GenerateReportActionTest } public void testGenerateStatisticsInvalidEndDate() + throws Exception { repositoryStatisticsManagerControl.replay(); prepareAction( Collections.singletonList( INTERNAL ), Collections.singletonList( SNAPSHOTS ) ); @@ -144,6 +177,7 @@ public class GenerateReportActionTest } public void testGenerateStatisticsMalformedEndDate() + throws Exception { repositoryStatisticsManagerControl.replay(); prepareAction( Collections.singletonList( INTERNAL ), Collections.singletonList( SNAPSHOTS ) ); @@ -158,6 +192,7 @@ public class GenerateReportActionTest } public void testGenerateStatisticsInvalidEndDateMultiRepo() + throws Exception { repositoryStatisticsManagerControl.replay(); prepareAction( Arrays.asList( SNAPSHOTS, INTERNAL ), Collections.emptyList() ); @@ -171,6 +206,7 @@ public class GenerateReportActionTest } public void testGenerateStatisticsMalformedEndDateMultiRepo() + throws Exception { repositoryStatisticsManagerControl.replay(); prepareAction( Arrays.asList( SNAPSHOTS, INTERNAL ), Collections.emptyList() ); @@ -185,6 +221,7 @@ public class GenerateReportActionTest } public void testGenerateStatisticsNoRepos() + throws Exception { repositoryStatisticsManagerControl.replay(); prepareAction( Collections.emptyList(), Arrays.asList( SNAPSHOTS, INTERNAL ) ); @@ -329,6 +366,7 @@ public class GenerateReportActionTest } public void testDownloadStatisticsMalformedEndDateMultiRepo() + throws Exception { repositoryStatisticsManagerControl.replay(); prepareAction( Arrays.asList( SNAPSHOTS, INTERNAL ), Collections.emptyList() ); @@ -343,6 +381,7 @@ public class GenerateReportActionTest } public void testDownloadStatisticsMalformedEndDateSingleRepo() + throws Exception { repositoryStatisticsManagerControl.replay(); prepareAction( Arrays.asList( SNAPSHOTS ), Arrays.asList( INTERNAL ) ); @@ -357,6 +396,7 @@ public class GenerateReportActionTest } public void testDownloadStatisticsInvalidEndDateMultiRepo() + throws Exception { repositoryStatisticsManagerControl.replay(); prepareAction( Arrays.asList( SNAPSHOTS, INTERNAL ), Collections.emptyList() ); @@ -370,6 +410,7 @@ public class GenerateReportActionTest } public void testDownloadStatisticsInvalidEndDateSingleRepo() + throws Exception { repositoryStatisticsManagerControl.replay(); prepareAction( Arrays.asList( SNAPSHOTS ), Arrays.asList( INTERNAL ) ); @@ -399,6 +440,7 @@ public class GenerateReportActionTest } public void testDownloadStatisticsNoRepos() + throws Exception { repositoryStatisticsManagerControl.replay(); prepareAction( Collections.emptyList(), Arrays.asList( SNAPSHOTS, INTERNAL ) );