[MRM-1958] Merging fix from master tree

Replace streams with old style code
Fix mock verifications
This commit is contained in:
Martin Stockhammer 2017-08-28 21:19:13 +02:00
parent 940412ebfb
commit 85687a34e0
4 changed files with 61 additions and 14 deletions

View File

@ -36,8 +36,13 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
@ -400,9 +405,7 @@ public abstract class AbstractRepositoryPurge
try
{
Files.find( parentDir, 3,
( path, basicFileAttributes ) -> path.getFileName( ).toString( ).startsWith( artifactName )
&& Files.isRegularFile( path ) ).forEach( this::deleteSilently );
deleteArtifactFiles( parentDir, 3, artifactName );
}
catch ( IOException e )
{
@ -411,6 +414,29 @@ public abstract class AbstractRepositoryPurge
}
public static void deleteArtifactFiles(final Path directory, final int maxDepth, final String artifactName) throws IOException
{
Files.walkFileTree(directory, new HashSet<FileVisitOption>( ), maxDepth, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile( Path file, BasicFileAttributes attrs) throws IOException {
if (file.getFileName().toString().startsWith(artifactName)) {
try {
Files.delete( file );
} catch (IOException e) {
// We do not stop, if an error occurs.
}
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
});
}
private void triggerAuditEvent( String repoId, String resource, String action )
{
String msg =

View File

@ -19,6 +19,7 @@ package org.apache.archiva.consumers.core;
* under the License.
*/
import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
import org.apache.archiva.common.utils.BaseFile;
import org.apache.archiva.configuration.ArchivaConfiguration;
import org.apache.archiva.configuration.FileType;

View File

@ -20,9 +20,11 @@ package org.apache.archiva.consumers.core.repository;
*/
import org.apache.archiva.admin.model.beans.ManagedRepository;
import org.apache.archiva.common.plexusbridge.PlexusSisuBridge;
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.storage.maven2.ArtifactMappingProvider;
import org.apache.archiva.metadata.repository.storage.maven2.Maven2RepositoryPathTranslator;
import org.apache.archiva.repository.ManagedRepositoryContent;
import org.apache.archiva.repository.events.RepositoryListener;
@ -43,12 +45,16 @@ import org.springframework.test.context.ContextConfiguration;
import javax.inject.Inject;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@ -231,13 +237,27 @@ public abstract class AbstractRepositoryPurgeTest
return StringUtils.substringAfterLast( getClass().getName(), "." );
}
protected List<ArtifactMetadata> getArtifactMetadataFromDir( String repoId, String projectName, Path repoDir, Path vDir ) throws IOException
protected List<ArtifactMetadata> getArtifactMetadataFromDir( final String repoId, final String projectName, final Path repoDir, final Path vDir ) throws IOException
{
Maven2RepositoryPathTranslator translator = new Maven2RepositoryPathTranslator( new ArrayList<>( ) );
return Files.find(vDir, 1,
(path, basicFileAttributes) -> basicFileAttributes.isRegularFile() && path.getFileName().toString().startsWith(projectName))
.map( path ->
translator.getArtifactForPath( repoId, repoDir.relativize( path ).toString() )
).collect( Collectors.toList());
final Maven2RepositoryPathTranslator translator = new Maven2RepositoryPathTranslator( new ArrayList<ArtifactMappingProvider>( ) );
final List<ArtifactMetadata> result = new ArrayList<>( );
Files.walkFileTree(vDir, new HashSet<FileVisitOption>( ), 1, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile( Path file, BasicFileAttributes attrs) throws IOException {
if (file.getFileName().toString().startsWith(projectName)) {
ArtifactMetadata m = translator.getArtifactForPath( repoId, repoDir.relativize( file ).toString() );
result.add(m);
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
});
return result;
}
}

View File

@ -386,8 +386,8 @@ public class RepositoryPurgeConsumerTest
verify(metadataRepository, never()).removeProjectVersion(eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion));
ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class);
verify(metadataRepository, never()).removeArtifact(any(), any());
verify(metadataRepository, never()).removeArtifact( any(), any(), any(), any(), any(MetadataFacet.class) );
verify(metadataRepository, never()).removeArtifact(any(ArtifactMetadata.class), any(String.class));
verify(metadataRepository, never()).removeArtifact( any(String.class), any(String.class), any(String.class), any(String.class), any(MetadataFacet.class) );
// check if the snapshot wasn't removed
@ -447,7 +447,7 @@ public class RepositoryPurgeConsumerTest
verify(metadataRepository, times(1)).removeProjectVersion(eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion));
ArgumentCaptor<ArtifactMetadata> metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class);
verify(metadataRepository, never()).removeArtifact(any(), any());
verify(metadataRepository, never()).removeArtifact(any(ArtifactMetadata.class), any(String.class));
// check if the snapshot was removed
assertDeleted( projectRoot + "/2.3-SNAPSHOT" );