]> source.dussan.org Git - archiva.git/commitdiff
Adding additional tests
authorMartin Stockhammer <martin_s@apache.org>
Sat, 14 Mar 2020 11:51:04 +0000 (12:51 +0100)
committerMartin Stockhammer <martin_s@apache.org>
Sat, 14 Mar 2020 11:51:04 +0000 (12:51 +0100)
archiva-modules/archiva-maven/archiva-maven-repository/src/main/java/org/apache/archiva/repository/maven/content/ManagedDefaultRepositoryContent.java
archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/content/ManagedDefaultRepositoryContentTest.java

index ef4ec97d809b3918a0c922f0d0d3071bddffaf17..a69ab4a2050bd15544317afe7dc337d6f9c7c09d 100644 (file)
@@ -444,6 +444,12 @@ public class ManagedDefaultRepositoryContent
                 }
             } else {
                 log.debug( "Artifact does not match the maven name pattern: {}", path );
+                if ( fileName.contains( "-"+baseVersion ) )
+                {
+                    info.id = StringUtils.substringBefore( fileName, "-"+baseVersion );
+                } else {
+                    info.id = fileName;
+                }
                 info.artifactType = BaseArtifactTypes.UNKNOWN;
                 info.version = "";
                 info.classifier = "";
@@ -464,6 +470,11 @@ public class ManagedDefaultRepositoryContent
                     info.remainder = classPostfix;
                 }
             } else {
+                if (fileName.contains( "-"+genericVersion )) {
+                    info.id = StringUtils.substringBefore( fileName, "-"+genericVersion );
+                } else {
+                    info.id = fileName;
+                }
                 log.debug( "Artifact does not match the version pattern {}", path );
                 info.artifactType = BaseArtifactTypes.UNKNOWN;
                 info.version = "";
@@ -627,6 +638,15 @@ public class ManagedDefaultRepositoryContent
             fileNamePattern.append("[A-Za-z0-9_\\-.]+-");
         }
         if (selector.hasArtifactVersion()) {
+            if ( selector.getArtifactVersion( ).contains("*")) {
+                String[] tokens = StringUtils.splitPreserveAllTokens( selector.getArtifactVersion( ), "*" );
+                for (String currentToken : tokens) {
+                    if (!currentToken.equals("")) {
+                        fileNamePattern.append( Pattern.quote( currentToken ) );
+                    }
+                    fileNamePattern.append( "[A-Za-z0-9_\\-.]*" );
+                }
+            }
             fileNamePattern.append( Pattern.quote(selector.getArtifactVersion( )) );
         } else  {
             fileNamePattern.append( "[A-Za-z0-9_\\-.]+" );
@@ -637,7 +657,7 @@ public class ManagedDefaultRepositoryContent
         {
             if ( "*".equals( classifier ) )
             {
-                fileNamePattern.append( "-[A-Za-z0-9]+\\." );
+                fileNamePattern.append( "(-[A-Za-z0-9]+)?\\." );
             }
             else
             {
@@ -677,8 +697,13 @@ public class ManagedDefaultRepositoryContent
      *     namespace and project are returned.</li>
      *     <li>If a namespace and a project id or artifact id and a version is given, the artifacts of the given
      *     version are returned</li>
+     *     <li>If no artifact version or artifact id is given, it will return all "artifacts" found in the directory.
+     *     To select only artifacts that match the layout you should add the artifact id and artifact version
+     *     (can contain a '*' pattern).</li>
      * </ul>
      *
+     * The '*' pattern can be used in classifiers and artifact versions and match zero or more characters.
+     *
      * There is no determinate order of the elements in the stream.
      *
      * Returned streams are auto closable and should be used in a try-with-resources statement.
index 4f1d936b25be51fba035f8073096c6e7e434a8f3..d32d0f7321e3a64958480f241f2a6ec8f136e438 100644 (file)
@@ -714,6 +714,21 @@ public class ManagedDefaultRepositoryContentTest
             .recurse()
             .build( );
         List<? extends Artifact> results = repoContent.getArtifacts( selector );
+        checkArtifactListWithNamespaceSelectorRecursive( results );
+    }
+
+    @Test
+    public void testArtifactStreamWithNamespaceSelectorRecursive() {
+        ItemSelector selector = ArchivaItemSelector.builder( )
+            .withNamespace( "org.multilevel" )
+            .recurse()
+            .build( );
+        Stream<? extends Artifact> results = repoContent.newArtifactStream( selector );
+        checkArtifactListWithNamespaceSelectorRecursive( results.collect( Collectors.toList()) );
+    }
+
+    private void checkArtifactListWithNamespaceSelectorRecursive( List<? extends Artifact> results )
+    {
         assertNotNull( results );
         assertEquals( 6, results.size( ) );
 
@@ -726,6 +741,114 @@ public class ManagedDefaultRepositoryContentTest
             .findFirst( ).get( );
         assertNotNull( artifact );
         assertEquals( 5, artifact.getAsset( ).getParent( ).getPath( ).split( "/" ).length );
+    }
+
+
+    @Test
+    public void testArtifactListWithArtifactSelector1() {
+        ItemSelector selector = ArchivaItemSelector.builder( )
+            .withNamespace( "org.apache.maven" )
+            .withProjectId( "test" )
+            .withVersion( "1.0-SNAPSHOT" )
+            .withArtifactId( "test" )
+            .withArtifactVersion( "1.0-20050611.112233-1" )
+            .build( );
+
+        List<? extends Artifact> results = repoContent.getArtifacts( selector );
+
+        assertNotNull( results );
+        assertEquals( 1, results.size( ) );
+
+        Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.112233-1.jar" ) )
+            .findFirst().get();
+        assertNotNull( artifact );
+        assertEquals( "", artifact.getClassifier( ) );
+    }
+
+    @Test
+    public void testArtifactListWithArtifactSelector2() {
+        ItemSelector selector = ArchivaItemSelector.builder( )
+            .withNamespace( "org.apache.maven" )
+            .withProjectId( "test" )
+            .withVersion( "1.0-SNAPSHOT" )
+            .withClassifier( "*" )
+            .withArtifactId( "test" )
+            .withArtifactVersion( "1.0-20050611.112233-1" )
+            .build( );
+
+        List<? extends Artifact> results = repoContent.getArtifacts( selector );
+
+        assertNotNull( results );
+        assertEquals( 2, results.size( ) );
+
+        Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.112233-1-javadoc.jar" ) )
+            .findFirst().get();
+        assertNotNull( artifact );
+        assertEquals( "javadoc", artifact.getClassifier( ) );
+        assertEquals( "javadoc", artifact.getType( ) );
+    }
+
+    @Test
+    public void testArtifactListWithArtifactSelector3() {
+        ItemSelector selector = ArchivaItemSelector.builder( )
+            .withNamespace( "org.apache.maven" )
+            .withProjectId( "test" )
+            .withVersion( "1.0-SNAPSHOT" )
+            .withClassifier( "*" )
+            .withArtifactVersion( "1.0-20050611.112233-1" )
+            .build( );
+
+        List<? extends Artifact> results = repoContent.getArtifacts( selector );
+
+        assertNotNull( results );
+        assertEquals( 3, results.size( ) );
+
+        Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "test-1.0-20050611.112233-1-javadoc.jar" ) )
+            .findFirst().get();
+        assertNotNull( artifact );
+        assertEquals( "javadoc", artifact.getClassifier( ) );
+        assertEquals( "javadoc", artifact.getType( ) );
+
+        artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "wrong-artifactId-1.0-20050611.112233-1.jar" ) )
+            .findFirst().get();
+        assertNotNull( artifact );
+        assertEquals( "", artifact.getClassifier( ) );
+        assertEquals( "wrong-artifactId", artifact.getId( ) );
+    }
+
+    @Test
+    public void testArtifactListWithArtifactSelectorAndRelated() {
+        ItemSelector selector = ArchivaItemSelector.builder( )
+            .withNamespace( "org.apache.maven" )
+            .withProjectId( "samplejar" )
+            .withVersion( "1.0" )
+            .withArtifactVersion( "1.0" )
+            .withArtifactId( "samplejar" )
+            .withExtension( "jar" )
+            .includeRelatedArtifacts()
+            .build( );
+
+        List<? extends Artifact> results = repoContent.getArtifacts( selector );
+
+        assertNotNull( results );
+        assertEquals( 3, results.size( ) );
+
+        Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "samplejar-1.0.jar" ) )
+            .findFirst().get();
+        assertNotNull( artifact );
+        assertEquals( BaseArtifactTypes.MAIN, artifact.getArtifactType( ) );
+
+        artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "samplejar-1.0.jar.md5" ) )
+            .findFirst().get();
+        assertNotNull( artifact );
+        assertEquals( BaseArtifactTypes.RELATED, artifact.getArtifactType( ) );
+        assertEquals( "md5", artifact.getExtension( ) );
+
+        artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "samplejar-1.0.jar.sha1" ) )
+            .findFirst().get();
+        assertNotNull( artifact );
+        assertEquals( BaseArtifactTypes.RELATED, artifact.getArtifactType( ) );
+        assertEquals( "sha1", artifact.getExtension( ) );
 
     }