diff options
author | Maria Odea B. Ching <oching@apache.org> | 2009-11-20 08:41:50 +0000 |
---|---|---|
committer | Maria Odea B. Ching <oching@apache.org> | 2009-11-20 08:41:50 +0000 |
commit | 607b7fb090d0672e06bb8c3ec6ebf0be697726d7 (patch) | |
tree | d00d80922ac334decc6ef6e81abfb39400d7fc8a | |
parent | 92d9bce38a55a889f4a1ab4732c45c2268f7e79d (diff) | |
download | archiva-607b7fb090d0672e06bb8c3ec6ebf0be697726d7.tar.gz archiva-607b7fb090d0672e06bb8c3ec6ebf0be697726d7.zip |
[MRM-1033] Changing the location of a scanned repository results to the contents of the new location not getting indexed
submitted by Maria Catherine Tan
o delete all stats when repository location is changed
git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@882448 13f79535-47bb-0310-9956-ffa450edef68
2 files changed, 170 insertions, 6 deletions
diff --git a/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/admin/repositories/EditManagedRepositoryAction.java b/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/admin/repositories/EditManagedRepositoryAction.java index de0cdd242..f128ba152 100644 --- a/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/admin/repositories/EditManagedRepositoryAction.java +++ b/archiva-modules/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/admin/repositories/EditManagedRepositoryAction.java @@ -25,12 +25,19 @@ import com.opensymphony.xwork2.Validateable; import org.apache.commons.lang.StringUtils; import org.apache.maven.archiva.configuration.Configuration; import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; +import org.apache.maven.archiva.database.ArchivaDAO; +import org.apache.maven.archiva.database.ArchivaDatabaseException; +import org.apache.maven.archiva.database.ObjectNotFoundException; +import org.apache.maven.archiva.database.RepositoryContentStatisticsDAO; +import org.apache.maven.archiva.database.constraints.RepositoryContentStatisticsByRepositoryConstraint; +import org.apache.maven.archiva.model.RepositoryContentStatistics; import org.apache.maven.archiva.repository.audit.AuditEvent; import org.codehaus.plexus.redback.role.RoleManagerException; import org.codehaus.plexus.scheduler.CronExpressionValidator; import java.io.File; import java.io.IOException; +import java.util.List; /** * AddManagedRepositoryAction @@ -52,6 +59,11 @@ public class EditManagedRepositoryAction private final String action = "editRepository"; + /** + * @plexus.requirement role-hint="jdo" + */ + private ArchivaDAO archivaDAO; + public void prepare() { if ( StringUtils.isNotBlank( repoid ) ) @@ -78,17 +90,22 @@ public class EditManagedRepositoryAction public String confirmUpdate() { - return save(); + // location was changed + return save( true ); } public String commit() - { + { ManagedRepositoryConfiguration existingConfig = archivaConfiguration.getConfiguration().findManagedRepositoryById( repository.getId() ); - + + boolean resetStats = false; + // check if the location was changed if( !StringUtils.equalsIgnoreCase( existingConfig.getLocation().trim(), repository.getLocation().trim() ) ) { + resetStats = true; + File dir = new File( repository.getLocation() ); if( dir.exists() ) { @@ -96,10 +113,10 @@ public class EditManagedRepositoryAction } } - return save(); + return save( resetStats ); } - - private String save() + + private String save( boolean resetStats ) { // Ensure that the fields are valid. Configuration configuration = archivaConfiguration.getConfiguration(); @@ -115,6 +132,7 @@ public class EditManagedRepositoryAction triggerAuditEvent( repository.getId(), null, AuditEvent.MODIFY_MANAGED_REPO ); addRepositoryRoles( repository ); result = saveConfiguration( configuration ); + resetStatistics( resetStats ); } catch ( IOException e ) { @@ -126,6 +144,16 @@ public class EditManagedRepositoryAction addActionError( "Role Manager Exception: " + e.getMessage() ); result = ERROR; } + catch ( ObjectNotFoundException e ) + { + addActionError( e.getMessage() ); + result = ERROR; + } + catch ( ArchivaDatabaseException e ) + { + addActionError( e.getMessage() ); + result = ERROR; + } return result; } @@ -141,6 +169,28 @@ public class EditManagedRepositoryAction } } + private void resetStatistics( boolean reset ) + throws ObjectNotFoundException, ArchivaDatabaseException + { + if ( !reset ) + { + return; + } + + RepositoryContentStatisticsDAO repoContentStatsDao = archivaDAO.getRepositoryContentStatisticsDAO(); + + List<RepositoryContentStatistics> contentStats = repoContentStatsDao.queryRepositoryContentStatistics( + new RepositoryContentStatisticsByRepositoryConstraint( repository.getId() ) ); + + if ( contentStats != null ) + { + for ( RepositoryContentStatistics stats : contentStats ) + { + repoContentStatsDao.deleteRepositoryContentStatistics( stats ); + } + } + } + public String getRepoid() { return repoid; @@ -165,4 +215,11 @@ public class EditManagedRepositoryAction { return action; } + + // for testing + + public void setArchivaDAO( ArchivaDAO archivaDao ) + { + this.archivaDAO = archivaDao; + } } diff --git a/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/admin/repositories/EditManagedRepositoryActionTest.java b/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/admin/repositories/EditManagedRepositoryActionTest.java index c3f3dd5b2..e688f8a29 100644 --- a/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/admin/repositories/EditManagedRepositoryActionTest.java +++ b/archiva-modules/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/admin/repositories/EditManagedRepositoryActionTest.java @@ -23,6 +23,10 @@ import com.opensymphony.xwork2.Action; import org.apache.maven.archiva.configuration.ArchivaConfiguration; import org.apache.maven.archiva.configuration.Configuration; import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; +import org.apache.maven.archiva.database.ArchivaDAO; +import org.apache.maven.archiva.database.RepositoryContentStatisticsDAO; +import org.apache.maven.archiva.database.constraints.RepositoryContentStatisticsByRepositoryConstraint; +import org.apache.maven.archiva.model.RepositoryContentStatistics; import org.apache.maven.archiva.security.ArchivaRoleConstants; import org.codehaus.plexus.redback.role.RoleManager; import org.codehaus.redback.integration.interceptor.SecureActionBundle; @@ -32,7 +36,10 @@ import org.easymock.MockControl; import java.io.File; import java.io.IOException; +import java.util.ArrayList; import java.util.Collections; +import java.util.Date; +import java.util.List; /** * EditManagedRepositoryActionTest @@ -52,6 +59,14 @@ public class EditManagedRepositoryActionTest private ArchivaConfiguration archivaConfiguration; + private MockControl archivaDaoControl; + + private ArchivaDAO archivaDao; + + private MockControl repoContentStatsDaoControl; + + private RepositoryContentStatisticsDAO repoContentStatsDao; + private static final String REPO_ID = "repo-ident"; private File location; @@ -77,6 +92,13 @@ public class EditManagedRepositoryActionTest roleManager = (RoleManager) roleManagerControl.getMock(); action.setRoleManager( roleManager ); location = getTestFile( "target/test/location" ); + + archivaDaoControl = MockControl.createControl( ArchivaDAO.class ); + archivaDao = (ArchivaDAO) archivaDaoControl.getMock(); + action.setArchivaDAO( archivaDao ); + + repoContentStatsDaoControl = MockControl.createControl( RepositoryContentStatisticsDAO.class ); + repoContentStatsDao = (RepositoryContentStatisticsDAO) repoContentStatsDaoControl.getMock(); } public void testSecureActionBundle() @@ -157,6 +179,66 @@ public class EditManagedRepositoryActionTest roleManagerControl.verify(); archivaConfigurationControl.verify(); } + + public void testEditRepositoryLocationChanged() + throws Exception + { + roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID ); + roleManagerControl.setReturnValue( false ); + roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID ); + roleManagerControl.setVoidCallable(); + roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID ); + roleManagerControl.setReturnValue( false ); + roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID ); + roleManagerControl.setVoidCallable(); + + roleManagerControl.replay(); + + Configuration configuration = createConfigurationForEditing( createRepository() ); + archivaConfiguration.getConfiguration(); + archivaConfigurationControl.setReturnValue( configuration ); + archivaConfigurationControl.setReturnValue( configuration ); + archivaConfigurationControl.setReturnValue( configuration ); + + archivaConfiguration.save( configuration ); + + archivaConfigurationControl.replay(); + + archivaDaoControl.expectAndReturn( archivaDao.getRepositoryContentStatisticsDAO(), repoContentStatsDao ); + + archivaDaoControl.replay(); + + repoContentStatsDao.queryRepositoryContentStatistics( + new RepositoryContentStatisticsByRepositoryConstraint( REPO_ID ) ); + repoContentStatsDaoControl.setMatcher( MockControl.ALWAYS_MATCHER ); + + List<RepositoryContentStatistics> repoStats = createRepositoryContentStatisticsList(); + repoContentStatsDaoControl.setReturnValue( repoStats ); + + repoContentStatsDao.deleteRepositoryContentStatistics( repoStats.get( 0 ) ); + repoContentStatsDaoControl.setVoidCallable(); + repoContentStatsDao.deleteRepositoryContentStatistics( repoStats.get( 1 ) ); + repoContentStatsDaoControl.setVoidCallable(); + + repoContentStatsDaoControl.replay(); + + action.setRepoid( REPO_ID ); + action.prepare(); + assertEquals( REPO_ID, action.getRepoid() ); + + ManagedRepositoryConfiguration repository = new ManagedRepositoryConfiguration(); + populateRepository( repository ); + repository.setLocation( new File( "target/test/location/new" ).getCanonicalPath() ); + action.setRepository( repository ); + String status = action.commit(); + assertEquals( Action.SUCCESS, status ); + assertEquals( Collections.singletonList( repository ), configuration.getManagedRepositories() ); + + roleManagerControl.verify(); + archivaConfigurationControl.verify(); + archivaDaoControl.verify(); + repoContentStatsDaoControl.verify(); + } private void assertRepositoryEquals( ManagedRepositoryConfiguration expectedRepository, ManagedRepositoryConfiguration actualRepository ) @@ -207,4 +289,29 @@ public class EditManagedRepositoryActionTest repository.setDeleteReleasedSnapshots( true ); } + private List<RepositoryContentStatistics> createRepositoryContentStatisticsList() + { + List<RepositoryContentStatistics> repoStatsList = new ArrayList<RepositoryContentStatistics>(); + + repoStatsList.add( createRepositoryContentStatistics() ); + repoStatsList.add( createRepositoryContentStatistics() ); + + return repoStatsList; + } + + private RepositoryContentStatistics createRepositoryContentStatistics() + { + RepositoryContentStatistics repoStats = new RepositoryContentStatistics(); + repoStats.setRepositoryId( REPO_ID ); + repoStats.setDuration( 1000 ); + repoStats.setTotalArtifactCount( 100 ); + repoStats.setTotalSize( 10 ); + repoStats.setTotalFileCount( 10 ); + repoStats.setTotalProjectCount( 2 ); + repoStats.setTotalGroupCount( 1 ); + repoStats.setNewFileCount( 3 ); + repoStats.setWhenGathered( new Date( System.currentTimeMillis() ) ); + + return repoStats; + } } |