From d61f68e6848d0bf5a214e22bf4be4a3ece1b4a74 Mon Sep 17 00:00:00 2001 From: Martin Stockhammer Date: Fri, 28 Apr 2017 22:41:47 +0200 Subject: [PATCH] Fixing browse service filter handling Improved handling of slashes at the beginning of filters Added patterns for surefire tests to ensure the test runs --- .../archiva-rest-services/pom.xml | 4 ++++ .../rest/services/DefaultBrowseService.java | 19 ++++++++++--------- .../services/ArtifactContentEntriesTests.java | 3 ++- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/pom.xml b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/pom.xml index 1617e827d..cc4442230 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/pom.xml +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/pom.xml @@ -455,6 +455,10 @@ org.apache.maven.plugins maven-surefire-plugin + + **/*Tests.java + **/*Test.java + -Xmx512m -Xms512m -server -XX:MaxPermSize=256m ${jacocoagent} ${project.build.directory}/appserver-base diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultBrowseService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultBrowseService.java index 682c367bb..e2efea419 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultBrowseService.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultBrowseService.java @@ -1093,15 +1093,16 @@ public class DefaultBrowseService } } - protected List readFileEntries( File file, String filterPath, String repoId ) + protected List readFileEntries(final File file, final String filterPath, final String repoId ) throws IOException { + String cleanedfilterPath = filterPath==null ? "" : (StringUtils.startsWith(filterPath, "/") ? + StringUtils.substringAfter(filterPath, "/") : filterPath); Map artifactContentEntryMap = new HashMap<>(); - int filterDepth = StringUtils.countMatches( filterPath, "/" ); - /*if ( filterDepth == 0 ) - { - filterDepth = 1; - }*/ + int filterDepth = StringUtils.countMatches( cleanedfilterPath, "/" ); + if (!StringUtils.endsWith(cleanedfilterPath,"/") && !StringUtils.isEmpty(cleanedfilterPath)) { + filterDepth++; + } JarFile jarFile = new JarFile( file ); try { @@ -1113,7 +1114,7 @@ public class DefaultBrowseService StringUtils.substringBeforeLast( currentEntry.getName(), "/" ) : currentEntry.getName(); String entryRootPath = getRootPath( cleanedEntryName ); int depth = StringUtils.countMatches( cleanedEntryName, "/" ); - if ( StringUtils.isEmpty( filterPath ) // + if ( StringUtils.isEmpty( cleanedfilterPath ) // && !artifactContentEntryMap.containsKey( entryRootPath ) // && depth == filterDepth ) { @@ -1124,7 +1125,7 @@ public class DefaultBrowseService } else { - if ( StringUtils.startsWith( cleanedEntryName, filterPath ) // + if ( StringUtils.startsWith( cleanedEntryName, cleanedfilterPath ) // && ( depth == filterDepth || ( !currentEntry.isDirectory() && depth == filterDepth ) ) ) { artifactContentEntryMap.put( cleanedEntryName, new ArtifactContentEntry( cleanedEntryName, @@ -1134,7 +1135,7 @@ public class DefaultBrowseService } } - if ( StringUtils.isNotEmpty( filterPath ) ) + if ( StringUtils.isNotEmpty( cleanedfilterPath ) ) { Map filteredArtifactContentEntryMap = new HashMap<>(); diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/ArtifactContentEntriesTests.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/ArtifactContentEntriesTests.java index 721cfb277..461cb8018 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/ArtifactContentEntriesTests.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/ArtifactContentEntriesTests.java @@ -20,6 +20,7 @@ package org.apache.archiva.rest.services; import junit.framework.TestCase; import org.apache.archiva.rest.api.model.ArtifactContentEntry; +import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -34,7 +35,7 @@ import static org.assertj.core.api.Assertions.assertThat; /** * @author Olivier Lamy */ -@RunWith( JUnit4.class ) +@RunWith( ArchivaBlockJUnit4ClassRunner.class ) public class ArtifactContentEntriesTests extends TestCase { -- 2.39.5