Adding cleanup for unit test methods

This commit is contained in:
Martin Stockhammer 2021-08-01 17:14:27 +02:00
parent cefb99661d
commit 32cf204092
2 changed files with 203 additions and 98 deletions

View File

@ -437,8 +437,7 @@ public class RepositoryGroupHandler
{ {
final String id = repositoryConfiguration.getId( ); final String id = repositoryConfiguration.getId( );
RepositoryGroup currentGroup = repositoryGroups.get( id ); RepositoryGroup currentGroup = repositoryGroups.get( id );
Configuration configuration = configurationHandler.getBaseConfiguration( ); RepositoryGroup repositoryGroup = newInstance( repositoryConfiguration );
RepositoryGroup repositoryGroup = put( repositoryConfiguration, configuration );
CheckedResult<RepositoryGroup, D> result; CheckedResult<RepositoryGroup, D> result;
if ( currentGroup == null ) if ( currentGroup == null )
{ {
@ -593,7 +592,7 @@ public class RepositoryGroupHandler
@Override @Override
public Collection<RepositoryGroup> getAll( ) public Collection<RepositoryGroup> getAll( )
{ {
return repositoryGroups.values( ); return Collections.unmodifiableCollection( repositoryGroups.values( ) );
} }
@Override @Override

View File

@ -20,8 +20,12 @@ package org.apache.archiva.repository.base.group;
import org.apache.archiva.common.filelock.DefaultFileLockManager; import org.apache.archiva.common.filelock.DefaultFileLockManager;
import org.apache.archiva.common.filelock.FileLockManager; import org.apache.archiva.common.filelock.FileLockManager;
import org.apache.archiva.common.utils.FileUtils;
import org.apache.archiva.common.utils.PathUtil;
import org.apache.archiva.components.registry.RegistryException;
import org.apache.archiva.configuration.ArchivaConfiguration; import org.apache.archiva.configuration.ArchivaConfiguration;
import org.apache.archiva.configuration.Configuration; import org.apache.archiva.configuration.Configuration;
import org.apache.archiva.configuration.IndeterminateConfigurationException;
import org.apache.archiva.configuration.RepositoryGroupConfiguration; import org.apache.archiva.configuration.RepositoryGroupConfiguration;
import org.apache.archiva.indexer.merger.MergedRemoteIndexesScheduler; import org.apache.archiva.indexer.merger.MergedRemoteIndexesScheduler;
import org.apache.archiva.repository.EditableRepositoryGroup; import org.apache.archiva.repository.EditableRepositoryGroup;
@ -36,6 +40,7 @@ import org.apache.archiva.repository.storage.fs.FilesystemStorage;
import org.apache.archiva.repository.validation.CheckedResult; import org.apache.archiva.repository.validation.CheckedResult;
import org.apache.archiva.repository.validation.RepositoryValidator; import org.apache.archiva.repository.validation.RepositoryValidator;
import org.apache.archiva.repository.validation.ValidationError; import org.apache.archiva.repository.validation.ValidationError;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock; import org.mockito.Mock;
@ -48,7 +53,9 @@ import javax.inject.Named;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -65,6 +72,9 @@ import static org.mockito.Mockito.verify;
@ContextConfiguration( locations = {"classpath*:/META-INF/spring-context.xml", "classpath:/spring-context-group.xml"} ) @ContextConfiguration( locations = {"classpath*:/META-INF/spring-context.xml", "classpath:/spring-context-group.xml"} )
class RepositoryGroupHandlerTest class RepositoryGroupHandlerTest
{ {
static {
initialize();
}
@Inject @Inject
@Named( "repositoryRegistry" ) @Named( "repositoryRegistry" )
@ -85,6 +95,51 @@ class RepositoryGroupHandlerTest
Path repoBaseDir; Path repoBaseDir;
private static void initialize() {
Path baseDir = Paths.get( FileUtils.getBasedir( ) );
Path config = baseDir.resolve( "src/test/resources/archiva-group.xml" );
if ( Files.exists( config ) )
{
Path destConfig = baseDir.resolve( "target/test-classes/archiva-group.xml" );
try
{
Files.copy( config, destConfig );
}
catch ( IOException e )
{
System.err.println( "Could not copy file: " + e.getMessage( ) );
}
}
}
// Helper method that removes a group from the configuration
private void removeGroupFromConfig(String groupId) {
Configuration configuration = configurationHandler.getBaseConfiguration( );
Iterator<RepositoryGroupConfiguration> groupIter = configuration.getRepositoryGroups().iterator();
while(groupIter.hasNext()) {
RepositoryGroupConfiguration group = groupIter.next( );
if (groupId.equals(group.getId())) {
groupIter.remove();
break;
}
}
try
{
configurationHandler.save( configuration );
}
catch ( Throwable e )
{
System.err.println( "Could not remove repo group from config "+groupId );
}
}
private boolean hasGroupInConfig(String groupId) {
assertNotNull( configurationHandler.getBaseConfiguration( ).getRepositoryGroups( ) );
return configurationHandler.getBaseConfiguration( ).getRepositoryGroups( ).stream( ).anyMatch( g -> g != null && groupId.equals( g.getId( ) ) ) ;
}
private RepositoryGroupHandler createHandler( ) private RepositoryGroupHandler createHandler( )
{ {
RepositoryGroupHandler groupHandler = new RepositoryGroupHandler( repositoryRegistry, configurationHandler, mergedRemoteIndexesScheduler, repositoryValidatorList ); RepositoryGroupHandler groupHandler = new RepositoryGroupHandler( repositoryRegistry, configurationHandler, mergedRemoteIndexesScheduler, repositoryValidatorList );
@ -133,11 +188,13 @@ class RepositoryGroupHandlerTest
@Test @Test
void activateRepository( ) throws RepositoryException void activateRepository( ) throws RepositoryException
{ {
String id = "test-group-02";
RepositoryGroupHandler groupHandler = createHandler( ); RepositoryGroupHandler groupHandler = createHandler( );
RepositoryGroup repo = groupHandler.newInstance( RepositoryType.MAVEN, "test-group-02" ); RepositoryGroup repo = groupHandler.newInstance( RepositoryType.MAVEN, id );
groupHandler.activateRepository( repo ); groupHandler.activateRepository( repo );
verify( mergedRemoteIndexesScheduler ).schedule( eq( repo ), any( ) ); verify( mergedRemoteIndexesScheduler ).schedule( eq( repo ), any( ) );
assertEquals( RepositoryState.INITIALIZED, repo.getLastState( ) ); assertEquals( RepositoryState.INITIALIZED, repo.getLastState( ) );
assertFalse(hasGroupInConfig( id ));
} }
@Test @Test
@ -153,59 +210,74 @@ class RepositoryGroupHandlerTest
@Test @Test
void newInstance( ) throws RepositoryException void newInstance( ) throws RepositoryException
{ {
String id = "test-group-03";
RepositoryGroupHandler groupHandler = createHandler( ); RepositoryGroupHandler groupHandler = createHandler( );
RepositoryGroup instance = groupHandler.newInstance( RepositoryType.MAVEN, "test-group-03" ); RepositoryGroup instance = groupHandler.newInstance( RepositoryType.MAVEN, id );
assertNotNull( instance ); assertNotNull( instance );
assertEquals( "test-group-03", instance.getId( ) ); assertEquals( id, instance.getId( ) );
assertFalse( groupHandler.hasRepository( "test-group-03" ) ); assertFalse( groupHandler.hasRepository( id ) );
assertEquals( RepositoryState.REFERENCES_SET, instance.getLastState( ) ); assertEquals( RepositoryState.REFERENCES_SET, instance.getLastState( ) );
assertFalse( hasGroupInConfig( id ) );
} }
@Test @Test
void put( ) throws IOException, RepositoryException void put( ) throws IOException, RepositoryException
{
final String id = "test-group-04";
try
{ {
RepositoryGroupHandler groupHandler = createHandler( ); RepositoryGroupHandler groupHandler = createHandler( );
EditableRepositoryGroup repositoryGroup = createRepository( "test-group-04", "n-test-group-04" ); EditableRepositoryGroup repositoryGroup = createRepository( id, "n-"+id );
groupHandler.put( repositoryGroup ); groupHandler.put( repositoryGroup );
RepositoryGroup storedGroup = groupHandler.get( "test-group-04" ); RepositoryGroup storedGroup = groupHandler.get( id );
assertNotNull( storedGroup ); assertNotNull( storedGroup );
assertEquals( "test-group-04", storedGroup.getId( ) ); assertEquals( id, storedGroup.getId( ) );
assertEquals( "n-test-group-04", storedGroup.getName( ) ); assertEquals( "n-"+id, storedGroup.getName( ) );
EditableRepositoryGroup repositoryGroup2 = createRepository( "test-group-04", "n2-test-group-04" ); EditableRepositoryGroup repositoryGroup2 = createRepository( id, "n2-"+id );
groupHandler.put( repositoryGroup2 ); groupHandler.put( repositoryGroup2 );
storedGroup = groupHandler.get( "test-group-04" ); storedGroup = groupHandler.get( id );
assertNotNull( storedGroup ); assertNotNull( storedGroup );
assertEquals( "test-group-04", storedGroup.getId( ) ); assertEquals( id, storedGroup.getId( ) );
assertEquals( "n2-test-group-04", storedGroup.getName( ) ); assertEquals( "n2-"+id, storedGroup.getName( ) );
assertNotNull( configurationHandler.getBaseConfiguration().getRepositoryGroups( ) ); assertTrue( hasGroupInConfig( id ));
assertTrue( configurationHandler.getBaseConfiguration().getRepositoryGroups( ).stream( ).anyMatch( g -> g!=null && "test-group-04".equals( g.getId( ) ) ) ); } finally {
removeGroupFromConfig( id );
}
} }
@Test @Test
void testPut( ) throws RepositoryException void putWithConfiguration( ) throws RepositoryException
{
String id = "test-group-05";
try
{ {
RepositoryGroupHandler groupHandler = createHandler( ); RepositoryGroupHandler groupHandler = createHandler( );
RepositoryGroupConfiguration configuration = new RepositoryGroupConfiguration( ); RepositoryGroupConfiguration configuration = new RepositoryGroupConfiguration( );
configuration.setId( "test-group-05" ); configuration.setId( id );
configuration.setName( "n-test-group-05" ); configuration.setName( "n-" + id );
ArrayList<String> repos = new ArrayList<>( ); ArrayList<String> repos = new ArrayList<>( );
repos.add( "internal" ); repos.add( "internal" );
configuration.setRepositories( repos ); configuration.setRepositories( repos );
groupHandler.put( configuration ); groupHandler.put( configuration );
RepositoryGroup repo = groupHandler.get( "test-group-05" ); RepositoryGroup repo = groupHandler.get( id );
assertNotNull( repo ); assertNotNull( repo );
assertEquals( "test-group-05", repo.getId( ) ); assertEquals( id, repo.getId( ) );
assertEquals( "n-test-group-05", repo.getName( ) ); assertEquals( "n-" + id, repo.getName( ) );
assertNotNull( repo.getRepositories( ) ); assertNotNull( repo.getRepositories( ) );
assertEquals( 1, repo.getRepositories( ).size( ) ); assertEquals( 1, repo.getRepositories( ).size( ) );
assertEquals( "internal", repo.getRepositories( ).get( 0 ).getId( ) ); assertEquals( "internal", repo.getRepositories( ).get( 0 ).getId( ) );
assertNotNull( configurationHandler.getBaseConfiguration().getRepositoryGroups( ) ); assertTrue( hasGroupInConfig( id ) );
assertTrue( configurationHandler.getBaseConfiguration().getRepositoryGroups( ).stream( ).anyMatch( g -> g!=null && "test-group-05".equals( g.getId( ) ) ) ); }
finally
{
removeGroupFromConfig( id );
}
} }
@Test @Test
@ -214,38 +286,48 @@ class RepositoryGroupHandlerTest
RepositoryGroupHandler groupHandler = createHandler( ); RepositoryGroupHandler groupHandler = createHandler( );
Configuration aCfg = new Configuration( ); Configuration aCfg = new Configuration( );
RepositoryGroupConfiguration configuration = new RepositoryGroupConfiguration( ); RepositoryGroupConfiguration configuration = new RepositoryGroupConfiguration( );
configuration.setId( "test-group-06" ); final String id = "test-group-06";
configuration.setName( "n-test-group-06" ); configuration.setId( id );
configuration.setName( "n-"+id );
ArrayList<String> repos = new ArrayList<>( ); ArrayList<String> repos = new ArrayList<>( );
repos.add( "internal" ); repos.add( "internal" );
configuration.setRepositories( repos ); configuration.setRepositories( repos );
groupHandler.put( configuration, aCfg ); groupHandler.put( configuration, aCfg );
RepositoryGroup repo = groupHandler.get( "test-group-06" ); RepositoryGroup repo = groupHandler.get( id );
assertNull( repo ); assertNull( repo );
assertNotNull( configurationHandler.getBaseConfiguration().getRepositoryGroups( ) ); assertFalse( hasGroupInConfig( id ) );
assertTrue( configurationHandler.getBaseConfiguration().getRepositoryGroups( ).stream( ).noneMatch( g -> g!=null && "test-group-06".equals( g.getId( ) ) ) ); assertTrue( aCfg.getRepositoryGroups( ).stream( ).anyMatch( g -> g!=null && id.equals( g.getId( ) ) ) );
assertTrue( aCfg.getRepositoryGroups( ).stream( ).anyMatch( g -> g!=null && "test-group-06".equals( g.getId( ) ) ) );
} }
@Test @Test
void putWithCheck( ) throws RepositoryException void putWithCheck_invalid( ) throws RepositoryException
{
final String id = "test-group-07";
final String name = "n-"+id;
try
{ {
RepositoryGroupHandler groupHandler = createHandler( ); RepositoryGroupHandler groupHandler = createHandler( );
BasicRepositoryGroupValidator checker = new BasicRepositoryGroupValidator( configurationHandler ); BasicRepositoryGroupValidator checker = new BasicRepositoryGroupValidator( configurationHandler );
RepositoryGroupConfiguration configuration = new RepositoryGroupConfiguration( ); RepositoryGroupConfiguration configuration = new RepositoryGroupConfiguration( );
configuration.setId( "" ); configuration.setId( "" );
configuration.setName( "n-test-group-07" ); configuration.setName( name );
ArrayList<String> repos = new ArrayList<>( ); ArrayList<String> repos = new ArrayList<>( );
repos.add( "internal" ); repos.add( "internal" );
configuration.setRepositories( repos ); configuration.setRepositories( repos );
CheckedResult<RepositoryGroup, Map<String, List<ValidationError>>> result = groupHandler.putWithCheck( configuration, checker ); CheckedResult<RepositoryGroup, Map<String, List<ValidationError>>> result = groupHandler.putWithCheck( configuration, checker );
assertNull( groupHandler.get( "test-group-07" ) ); assertNull( groupHandler.get( id ) );
assertNotNull( result.getResult( ) ); assertNotNull( result.getResult( ) );
assertNotNull( result.getResult( ).get( "id" ) ); assertNotNull( result.getResult( ).get( "id" ) );
assertEquals( 1, result.getResult( ).get( "id" ).size( ) ); assertEquals( 1, result.getResult( ).get( "id" ).size( ) );
assertEquals( ISEMPTY, result.getResult( ).get( "id" ).get( 0 ).getType( ) ); assertEquals( ISEMPTY, result.getResult( ).get( "id" ).get( 0 ).getType( ) );
assertFalse( hasGroupInConfig( id ) );
assertFalse( hasGroupInConfig( "" ) );
} finally
{
removeGroupFromConfig( id );
}
} }
@Test @Test
@ -253,31 +335,35 @@ class RepositoryGroupHandlerTest
{ {
RepositoryGroupHandler groupHandler = createHandler( ); RepositoryGroupHandler groupHandler = createHandler( );
RepositoryGroupConfiguration configuration = new RepositoryGroupConfiguration( ); RepositoryGroupConfiguration configuration = new RepositoryGroupConfiguration( );
configuration.setId( "test-group-08" ); final String id = "test-group-08";
configuration.setName( "n-test-group-08" ); configuration.setId( id );
configuration.setName( "n-"+id );
groupHandler.put( configuration ); groupHandler.put( configuration );
assertNotNull( groupHandler.get( "test-group-08" ) ); assertTrue( hasGroupInConfig( id ) );
groupHandler.remove( "test-group-08" ); assertNotNull( groupHandler.get( id ) );
assertNull( groupHandler.get( "test-group-08" ) ); groupHandler.remove( id );
assertNull( groupHandler.get( id ) );
assertFalse( hasGroupInConfig( id ) );
} }
@Test @Test
void testRemove( ) throws RepositoryException void removeWithoutSave( ) throws RepositoryException
{ {
RepositoryGroupHandler groupHandler = createHandler( ); RepositoryGroupHandler groupHandler = createHandler( );
Configuration aCfg = new Configuration( ); Configuration aCfg = new Configuration( );
RepositoryGroupConfiguration configuration = new RepositoryGroupConfiguration( ); RepositoryGroupConfiguration configuration = new RepositoryGroupConfiguration( );
configuration.setId( "test-group-09" ); final String id = "test-group-09";
configuration.setName( "n-test-group-09" ); configuration.setId( id );
configuration.setName( "n-"+id );
ArrayList<String> repos = new ArrayList<>( ); ArrayList<String> repos = new ArrayList<>( );
repos.add( "internal" ); repos.add( "internal" );
configuration.setRepositories( repos ); configuration.setRepositories( repos );
groupHandler.put( configuration, aCfg ); groupHandler.put( configuration, aCfg );
assertTrue( aCfg.getRepositoryGroups( ).stream( ).anyMatch( g -> g != null && "test-group-09".equals( g.getId( ) ) ) ); assertTrue( aCfg.getRepositoryGroups( ).stream( ).anyMatch( g -> g != null && id.equals( g.getId( ) ) ) );
groupHandler.remove( "test-group-09", aCfg ); groupHandler.remove( id, aCfg );
assertNull( groupHandler.get( "test-group-09" ) ); assertNull( groupHandler.get( id ) );
assertTrue( aCfg.getRepositoryGroups( ).stream( ).noneMatch( g -> g != null && "test-group-09".equals( g.getId( ) ) ) ); assertTrue( aCfg.getRepositoryGroups( ).stream( ).noneMatch( g -> g != null && id.equals( g.getId( ) ) ) );
assertNull( groupHandler.get( "test-group-09" ) ); assertNull( groupHandler.get( id ) );
} }
@ -286,13 +372,14 @@ class RepositoryGroupHandlerTest
void validateRepository( ) throws IOException void validateRepository( ) throws IOException
{ {
RepositoryGroupHandler groupHandler = createHandler( ); RepositoryGroupHandler groupHandler = createHandler( );
EditableRepositoryGroup repositoryGroup = createRepository( "test-group-10", "n-test-group-10" ); final String id = "test-group-10";
EditableRepositoryGroup repositoryGroup = createRepository( id, "n-"+id );
repositoryGroup.setMergedIndexTTL( 5 ); repositoryGroup.setMergedIndexTTL( 5 );
CheckedResult<RepositoryGroup, Map<String, List<ValidationError>>> result = groupHandler.validateRepository( repositoryGroup ); CheckedResult<RepositoryGroup, Map<String, List<ValidationError>>> result = groupHandler.validateRepository( repositoryGroup );
assertNotNull( result ); assertNotNull( result );
assertEquals( 0, result.getResult( ).size( ) ); assertEquals( 0, result.getResult( ).size( ) );
repositoryGroup = createRepository( "test-group-10", "n-test-group-10###" ); repositoryGroup = createRepository( id, "n-test-group-10###" );
result = groupHandler.validateRepository( repositoryGroup ); result = groupHandler.validateRepository( repositoryGroup );
assertNotNull( result ); assertNotNull( result );
assertEquals( 2, result.getResult( ).size( ) ); assertEquals( 2, result.getResult( ).size( ) );
@ -303,40 +390,59 @@ class RepositoryGroupHandlerTest
@Test @Test
void validateRepositoryIfExisting( ) throws IOException, RepositoryException void validateRepositoryIfExisting( ) throws IOException, RepositoryException
{
final String id = "test-group-11";
try
{ {
RepositoryGroupHandler groupHandler = createHandler( ); RepositoryGroupHandler groupHandler = createHandler( );
EditableRepositoryGroup repositoryGroup = createRepository( "test-group-11", "n-test-group-11" ); EditableRepositoryGroup repositoryGroup = createRepository( id, "n-" + id );
repositoryGroup.setMergedIndexTTL( 5 ); repositoryGroup.setMergedIndexTTL( 5 );
groupHandler.put( repositoryGroup ); groupHandler.put( repositoryGroup );
CheckedResult<RepositoryGroup, Map<String, List<ValidationError>>> result = groupHandler.validateRepository( repositoryGroup ); CheckedResult<RepositoryGroup, Map<String, List<ValidationError>>> result = groupHandler.validateRepository( repositoryGroup );
assertNotNull( result ); assertNotNull( result );
assertEquals( 1, result.getResult( ).size( ) ); assertEquals( 1, result.getResult( ).size( ) );
} finally
{
removeGroupFromConfig( id );
}
} }
@Test @Test
void validateRepositoryForUpdate( ) throws IOException, RepositoryException void validateRepositoryForUpdate( ) throws IOException, RepositoryException
{
final String id = "test-group-12";
try
{ {
RepositoryGroupHandler groupHandler = createHandler( ); RepositoryGroupHandler groupHandler = createHandler( );
EditableRepositoryGroup repositoryGroup = createRepository( "test-group-12", "n-test-group-12" ); EditableRepositoryGroup repositoryGroup = createRepository( id, "n-" + id );
repositoryGroup.setMergedIndexTTL( 5 ); repositoryGroup.setMergedIndexTTL( 5 );
groupHandler.put( repositoryGroup ); groupHandler.put( repositoryGroup );
CheckedResult<RepositoryGroup, Map<String, List<ValidationError>>> result = groupHandler.validateRepositoryForUpdate( repositoryGroup ); CheckedResult<RepositoryGroup, Map<String, List<ValidationError>>> result = groupHandler.validateRepositoryForUpdate( repositoryGroup );
assertNotNull( result ); assertNotNull( result );
assertEquals( 0, result.getResult( ).size( ) ); assertEquals( 0, result.getResult( ).size( ) );
} finally
{
removeGroupFromConfig( id );
}
} }
@Test @Test
void has( ) throws IOException, RepositoryException void has( ) throws IOException, RepositoryException
{
final String id = "test-group-13";
try
{ {
RepositoryGroupHandler groupHandler = createHandler( ); RepositoryGroupHandler groupHandler = createHandler( );
EditableRepositoryGroup repositoryGroup = createRepository( "test-group-13", "n-test-group-13" ); EditableRepositoryGroup repositoryGroup = createRepository( id, "n-" + id );
repositoryGroup.setMergedIndexTTL( 5 ); repositoryGroup.setMergedIndexTTL( 5 );
assertFalse( groupHandler.hasRepository( "test-group-13" ) ); assertFalse( groupHandler.hasRepository( id ) );
groupHandler.put( repositoryGroup ); groupHandler.put( repositoryGroup );
assertTrue( groupHandler.hasRepository( "test-group-13" ) ); assertTrue( groupHandler.hasRepository( id ) );
} finally
{
removeGroupFromConfig( id );
}
} }
} }