]> source.dussan.org Git - archiva.git/commitdiff
[MRM-1958] Merging fix from master tree
authorMartin Stockhammer <martin_s@apache.org>
Mon, 28 Aug 2017 19:19:13 +0000 (21:19 +0200)
committerMartin Stockhammer <martin_s@apache.org>
Mon, 28 Aug 2017 19:19:13 +0000 (21:19 +0200)
Replace streams with old style code
Fix mock verifications

archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/archiva/consumers/core/repository/AbstractRepositoryPurge.java
archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/AbstractArtifactConsumerTest.java
archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/AbstractRepositoryPurgeTest.java
archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/RepositoryPurgeConsumerTest.java

index 9b1615f7664cb872620f004b08d12d28ee0912ca..de2d4893d4735290183d0c6563e6ad3975d70b3b 100644 (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 =
index ac689847ad82d95bc3f01aa93afcac61d8e98c63..394093fc78d648deca497d0a65174ba9d34ce9c1 100644 (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;
index 0e599833f95514dc7db40a4d1ef448e56988f087..4df51b6f7a6b6db2a71ae6cde54e68ba9676bd6e 100644 (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;
     }
+
 }
index b4ef96da514378e3da71f8e6357aafafffc24c7e..f444e5abce4442dc934da74c78fad7d72e0c37e0 100644 (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" );