[MRM-1293] adjust the EditManagedRepositoryAction to delete statistics when the repository location changes.

git-svn-id: https://svn.apache.org/repos/asf/archiva/branches/MRM-1025@888185 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Porter 2009-12-07 23:27:58 +00:00
parent 7c7fffc71c
commit 1dad23aaf3
11 changed files with 163 additions and 117 deletions

View File

@ -118,4 +118,9 @@ public class TestMetadataRepository
{
//To change body of implemented methods use File | Settings | File Templates.
}
public void removeMetadataFacets( String repositoryId, String facetId )
{
//To change body of implemented methods use File | Settings | File Templates.
}
}

View File

@ -46,6 +46,11 @@ public class TestRepositoryStatisticsManager
stats.add( repositoryStatistics );
}
public void deleteStatistics( String repositoryId )
{
repoStats.remove( repositoryId );
}
private List<RepositoryStatistics> getStatsList( String repositoryId )
{
List<RepositoryStatistics> stats = repoStats.get( repositoryId );

View File

@ -21,19 +21,13 @@ package org.apache.maven.archiva.web.action.admin.repositories;
import java.io.File;
import java.io.IOException;
import java.util.List;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.Validateable;
import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
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;
@ -58,9 +52,9 @@ public class EditManagedRepositoryAction
private final String action = "editRepository";
/**
* @plexus.requirement role-hint="jdo"
* @plexus.requirement
*/
private ArchivaDAO archivaDAO;
private RepositoryStatisticsManager repositoryStatisticsManager;
public void prepare()
{
@ -145,16 +139,6 @@ 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;
}
@ -171,20 +155,8 @@ public class EditManagedRepositoryAction
}
private void resetStatistics()
throws ObjectNotFoundException, ArchivaDatabaseException
{
RepositoryContentStatisticsDAO repoContentStatsDao = archivaDAO.getRepositoryContentStatisticsDAO();
List<RepositoryContentStatistics> contentStats = repoContentStatsDao.queryRepositoryContentStatistics(
new RepositoryContentStatisticsByRepositoryConstraint( repository.getId() ) );
if ( contentStats != null )
{
for ( RepositoryContentStatistics stats : contentStats )
{
repoContentStatsDao.deleteRepositoryContentStatistics( stats );
}
}
repositoryStatisticsManager.deleteStatistics( repository.getId() );
}
public String getRepoid()
@ -212,10 +184,8 @@ public class EditManagedRepositoryAction
return action;
}
// for testing
public void setArchivaDAO( ArchivaDAO archivaDao )
public void setRepositoryStatisticsManager( RepositoryStatisticsManager repositoryStatisticsManager )
{
this.archivaDAO = archivaDao;
this.repositoryStatisticsManager = repositoryStatisticsManager;
}
}

View File

@ -19,28 +19,22 @@ package org.apache.maven.archiva.web.action.admin.repositories;
* under the License.
*/
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import com.opensymphony.xwork2.Action;
import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
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.plexus.spring.PlexusInSpringTestCase;
import org.codehaus.redback.integration.interceptor.SecureActionBundle;
import org.codehaus.redback.integration.interceptor.SecureActionException;
import org.codehaus.plexus.spring.PlexusInSpringTestCase;
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
*
@ -59,14 +53,6 @@ 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;
@ -92,13 +78,6 @@ 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()
@ -204,24 +183,15 @@ public class EditManagedRepositoryActionTest
archivaConfigurationControl.replay();
archivaDaoControl.expectAndReturn( archivaDao.getRepositoryContentStatisticsDAO(), repoContentStatsDao );
MockControl repositoryStatisticsManagerControl = MockControl.createControl( RepositoryStatisticsManager.class );
RepositoryStatisticsManager repositoryStatisticsManager =
(RepositoryStatisticsManager) repositoryStatisticsManagerControl.getMock();
action.setRepositoryStatisticsManager( repositoryStatisticsManager );
archivaDaoControl.replay();
repositoryStatisticsManager.deleteStatistics( REPO_ID );
repoContentStatsDao.queryRepositoryContentStatistics(
new RepositoryContentStatisticsByRepositoryConstraint( REPO_ID ) );
repoContentStatsDaoControl.setMatcher( MockControl.ALWAYS_MATCHER );
repositoryStatisticsManagerControl.replay();
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() );
@ -236,8 +206,7 @@ public class EditManagedRepositoryActionTest
roleManagerControl.verify();
archivaConfigurationControl.verify();
archivaDaoControl.verify();
repoContentStatsDaoControl.verify();
repositoryStatisticsManagerControl.verify();
}
private void assertRepositoryEquals( ManagedRepositoryConfiguration expectedRepository,
@ -288,30 +257,4 @@ public class EditManagedRepositoryActionTest
repository.setScanned( false );
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;
}
}

View File

@ -118,4 +118,9 @@ public class TestMetadataRepository
{
//To change body of implemented methods use File | Settings | File Templates.
}
public void removeMetadataFacets( String repositoryId, String facetId )
{
//To change body of implemented methods use File | Settings | File Templates.
}
}

View File

@ -54,4 +54,6 @@ public interface MetadataRepository
MetadataFacet getMetadataFacet( String repositoryId, String facetId, String name );
void addMetadataFacet( String repositoryId, String facetId, String name, MetadataFacet metadataFacet );
void removeMetadataFacets( String repositoryId, String facetId );
}

View File

@ -49,6 +49,7 @@ import org.apache.archiva.metadata.model.ProjectVersionMetadata;
import org.apache.archiva.metadata.model.ProjectVersionReference;
import org.apache.archiva.metadata.model.Scm;
import org.apache.archiva.metadata.repository.MetadataRepository;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -296,6 +297,19 @@ public class FileMetadataRepository
}
}
public void removeMetadataFacets( String repositoryId, String facetId )
{
try
{
FileUtils.deleteDirectory( getMetadataDirectory( repositoryId, facetId ) );
}
catch ( IOException e )
{
// TODO!
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
private File getMetadataDirectory( String repositoryId, String facetId )
{
return new File( this.directory, repositoryId + "/.meta/" + facetId );

View File

@ -160,6 +160,35 @@ public class FileMetadataRepositoryTest
assertTrue( facets.isEmpty() );
}
public void testRemoveFacets()
{
List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
assertTrue( facets.isEmpty() );
repository.removeMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
assertTrue( facets.isEmpty() );
}
public void testRemoveFacetsWhenEmpty()
{
repository.addMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME, new TestMetadataFacet( TEST_VALUE ) );
List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
assertFalse( facets.isEmpty() );
repository.removeMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
assertTrue( facets.isEmpty() );
}
public void testRemoveFacetsWhenUnknown()
{
repository.removeMetadataFacets( TEST_REPO_ID, UNKNOWN );
}
private static class TestMetadataFacet
implements MetadataFacet
{

View File

@ -73,6 +73,11 @@ public class DefaultRepositoryStatisticsManager
repositoryStatistics );
}
public void deleteStatistics( String repositoryId )
{
metadataRepository.removeMetadataFacets( repositoryId, RepositoryStatistics.FACET_ID );
}
public void setMetadataRepository( MetadataRepository metadataRepository )
{
this.metadataRepository = metadataRepository;

View File

@ -24,4 +24,6 @@ public interface RepositoryStatisticsManager
RepositoryStatistics getLastStatistics( String repositoryId );
void addStatisticsAfterScan( String repositoryId, RepositoryStatistics repositoryStatistics );
void deleteStatistics( String repositoryId );
}

View File

@ -113,24 +113,17 @@ public class RepositoryStatisticsManagerTest
Date current = new Date();
Date startTime = new Date( current.getTime() - 12345 );
RepositoryStatistics stats = new RepositoryStatistics();
stats.setScanStartTime( startTime );
stats.setScanEndTime( current );
stats.setTotalArtifactFileSize( 1400032000L );
stats.setNewFileCount( 45 );
stats.setTotalArtifactCount( 10412 );
stats.setTotalProjectCount( 2036 );
stats.setTotalGroupCount( 531 );
stats.setTotalFileCount( 56345 );
RepositoryStatistics stats1 = createTestStats( startTime, current );
String startTimeAsString = DefaultRepositoryStatisticsManager.SCAN_TIMESTAMP.format( startTime );
metadataRepository.addMetadataFacet( TEST_REPO_ID, RepositoryStatistics.FACET_ID, startTimeAsString, stats );
metadataRepository.addMetadataFacet( TEST_REPO_ID, RepositoryStatistics.FACET_ID, startTimeAsString, stats1 );
metadataRepositoryControl.expectAndReturn(
metadataRepository.getMetadataFacets( TEST_REPO_ID, RepositoryStatistics.FACET_ID ),
Arrays.asList( startTimeAsString ) );
metadataRepositoryControl.expectAndReturn(
metadataRepository.getMetadataFacet( TEST_REPO_ID, RepositoryStatistics.FACET_ID, startTimeAsString ),
stats );
stats1 );
RepositoryStatistics stats = stats1;
metadataRepositoryControl.replay();
@ -149,4 +142,77 @@ public class RepositoryStatisticsManagerTest
metadataRepositoryControl.verify();
}
public void testDeleteStats()
{
Date current = new Date();
Date startTime1 = new Date( current.getTime() - 12345 );
RepositoryStatistics stats1 = createTestStats( startTime1, new Date( current.getTime() - 6000 ) );
String startTimeAsString1 = DefaultRepositoryStatisticsManager.SCAN_TIMESTAMP.format( startTime1 );
metadataRepository.addMetadataFacet( TEST_REPO_ID, RepositoryStatistics.FACET_ID, startTimeAsString1, stats1 );
Date startTime2 = new Date( current.getTime() - 3000 );
RepositoryStatistics stats2 = createTestStats( startTime2, current );
String startTimeAsString2 = DefaultRepositoryStatisticsManager.SCAN_TIMESTAMP.format( startTime2 );
metadataRepository.addMetadataFacet( TEST_REPO_ID, RepositoryStatistics.FACET_ID, startTimeAsString2, stats2 );
metadataRepositoryControl.expectAndReturn(
metadataRepository.getMetadataFacets( TEST_REPO_ID, RepositoryStatistics.FACET_ID ),
Arrays.asList( startTimeAsString1, startTimeAsString2 ) );
metadataRepositoryControl.expectAndReturn(
metadataRepository.getMetadataFacet( TEST_REPO_ID, RepositoryStatistics.FACET_ID, startTimeAsString2 ),
stats2 );
metadataRepository.removeMetadataFacets( TEST_REPO_ID, RepositoryStatistics.FACET_ID );
metadataRepositoryControl.expectAndReturn(
metadataRepository.getMetadataFacets( TEST_REPO_ID, RepositoryStatistics.FACET_ID ),
Collections.emptyList() );
metadataRepositoryControl.replay();
repositoryStatisticsManager.addStatisticsAfterScan( TEST_REPO_ID, stats1 );
repositoryStatisticsManager.addStatisticsAfterScan( TEST_REPO_ID, stats2 );
assertNotNull( repositoryStatisticsManager.getLastStatistics( TEST_REPO_ID ) );
repositoryStatisticsManager.deleteStatistics( TEST_REPO_ID );
assertNull( repositoryStatisticsManager.getLastStatistics( TEST_REPO_ID ) );
metadataRepositoryControl.verify();
}
public void testDeleteStatsWhenEmpty()
{
metadataRepositoryControl.expectAndReturn(
metadataRepository.getMetadataFacets( TEST_REPO_ID, RepositoryStatistics.FACET_ID ),
Collections.emptyList(), 2 );
metadataRepository.removeMetadataFacets( TEST_REPO_ID, RepositoryStatistics.FACET_ID );
metadataRepositoryControl.replay();
assertNull( repositoryStatisticsManager.getLastStatistics( TEST_REPO_ID ) );
repositoryStatisticsManager.deleteStatistics( TEST_REPO_ID );
assertNull( repositoryStatisticsManager.getLastStatistics( TEST_REPO_ID ) );
metadataRepositoryControl.verify();
}
private RepositoryStatistics createTestStats( Date startTime, Date endTime )
{
RepositoryStatistics stats = new RepositoryStatistics();
stats.setScanStartTime( startTime );
stats.setScanEndTime( endTime );
stats.setTotalArtifactFileSize( 1400032000L );
stats.setNewFileCount( 45 );
stats.setTotalArtifactCount( 10412 );
stats.setTotalProjectCount( 2036 );
stats.setTotalGroupCount( 531 );
stats.setTotalFileCount( 56345 );
return stats;
}
}