]> source.dussan.org Git - archiva.git/commitdiff
Adding unit tests for maven repository
authorMartin Stockhammer <martin_s@apache.org>
Sun, 8 Mar 2020 11:44:59 +0000 (12:44 +0100)
committerMartin Stockhammer <martin_s@apache.org>
Sun, 8 Mar 2020 11:44:59 +0000 (12:44 +0100)
archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/mock/ManagedRepositoryContentMock.java
archiva-modules/archiva-base/archiva-storage-fs/src/main/java/org/apache/archiva/repository/storage/fs/FilesystemStorage.java
archiva-modules/archiva-maven/archiva-maven-proxy/src/test/java/org/apache/archiva/repository/mock/ManagedRepositoryContentMock.java
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/main/java/org/apache/archiva/repository/maven/content/MavenContentHelper.java
archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/content/AbstractDefaultRepositoryContentTestCase.java [deleted file]
archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/content/AbstractManagedRepositoryContentTest.java [new file with mode: 0644]
archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/content/AbstractRepositoryContentTest.java [new file with mode: 0644]
archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/content/ManagedDefaultRepositoryContentTest.java
archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/content/MavenContentHelperTest.java [new file with mode: 0644]
archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/content/RemoteDefaultRepositoryContentTest.java

index cc7aac64c40bfd380eeb97a36a5701d49bb29f72..c719d38713f6d7383fcaebf69120d92a84b6a875 100644 (file)
@@ -180,6 +180,18 @@ public class ManagedRepositoryContentMock implements ManagedRepositoryContent
 
     }
 
+    @Override
+    public ContentItem toItem( String path ) throws LayoutException
+    {
+        return null;
+    }
+
+    @Override
+    public ContentItem toItem( StorageAsset assetPath ) throws LayoutException
+    {
+        return null;
+    }
+
 
     @Override
     public void deleteGroupId( String groupId ) throws ContentNotFoundException, ContentAccessException
index 0ae56e794039d1eb0d995f5022dfef48b34a5cce..339e65d384e11030c11fb55280c155643c4ddeb3 100644 (file)
@@ -321,7 +321,7 @@ public class FilesystemStorage implements RepositoryStorage {
     public StorageAsset getAsset( String path )
     {
         try {
-            return new FilesystemAsset(this, path, getAssetPath(path));
+            return new FilesystemAsset(this, path, getAssetPath(path), basePath);
         } catch (IOException e) {
             throw new IllegalArgumentException("Path navigates outside of base directory "+path);
         }
index fd655b8e6b1ca9cf57250cd20ed264d8c75ec4f3..bc1a16f1e71be64d781baf237764389c57462254 100644 (file)
@@ -185,6 +185,18 @@ public class ManagedRepositoryContentMock implements ManagedRepositoryContent
 
     }
 
+    @Override
+    public ContentItem toItem( String path ) throws LayoutException
+    {
+        return null;
+    }
+
+    @Override
+    public ContentItem toItem( StorageAsset assetPath ) throws LayoutException
+    {
+        return null;
+    }
+
     @Override
     public void deleteVersion( VersionedReference reference ) throws ContentNotFoundException, ContentAccessException
     {
index e82ce3410f3566c7aad5b66350264f3713467b9c..c5d371e5d984e93e13bd2e4d711b082534ac9397 100644 (file)
@@ -141,38 +141,32 @@ public class ManagedDefaultRepositoryContent
 
     }
 
-    /**
-     * Returns a version reference from the coordinates
-     * @param groupId the group id
-     * @param artifactId the artifact id
-     * @param version the version
-     * @return the versioned reference object
-     */
-    @Override
-    public VersionedReference toVersion( String groupId, String artifactId, String version ) {
-        return new VersionedReference().groupId( groupId ).artifactId( artifactId ).version( version );
+    private StorageAsset getAssetByPath(String assetPath) {
+        return getStorage( ).getAsset( assetPath );
     }
 
-    @Override
-    public VersionedReference toGenericVersion( ArtifactReference artifactReference )
-    {
-        return toVersion( artifactReference.getGroupId( ), artifactReference.getArtifactId( ), VersionUtil.getBaseVersion( artifactReference.getVersion( ) ));
+    private StorageAsset getAsset(String namespace) {
+        String namespacePath = formatAsDirectory( namespace.trim() );
+        if (StringUtils.isEmpty( namespacePath )) {
+            namespacePath = "";
+        }
+        return getAssetByPath(namespacePath);
     }
 
-    /**
-     * Return the version the artifact is part of
-     * @param artifactReference
-     * @return
-     */
-    public VersionedReference toVersion( ArtifactReference artifactReference) {
-        return toVersion( artifactReference.getGroupId( ), artifactReference.getArtifactId( ), artifactReference.getVersion( ) );
+    private StorageAsset getAsset(String namespace, String project) {
+        return getAsset( namespace ).resolve( project );
     }
 
-    @Override
-    public ArtifactReference toArtifact( String groupId, String artifactId, String version, String type, String classifier) {
-        return new ArtifactReference( ).groupId( groupId ).artifactId( artifactId ).version( version ).type( type ).classifier( classifier );
+    private StorageAsset getAsset(String namespace, String project, String version) {
+        return getAsset( namespace, project ).resolve( version );
     }
 
+    private StorageAsset getAsset(String namespace, String project, String version, String fileName) {
+        return getAsset( namespace, project, version ).resolve( fileName );
+    }
+
+
+    /// ************* End of new generation interface ******************
     @Override
     public void deleteItem( ContentItem item ) throws ItemNotFoundException, ContentAccessException
     {
@@ -205,31 +199,6 @@ public class ManagedDefaultRepositoryContent
         }
     }
 
-    private StorageAsset getAssetByPath(String assetPath) {
-        return getStorage( ).getAsset( assetPath );
-    }
-
-    private StorageAsset getAsset(String namespace) {
-        String namespacePath = formatAsDirectory( namespace.trim() );
-        if (StringUtils.isEmpty( namespacePath )) {
-            namespacePath = "";
-        }
-        return getAssetByPath(namespacePath);
-    }
-
-    private StorageAsset getAsset(String namespace, String project) {
-        return getAsset( namespace ).resolve( project );
-    }
-
-    private StorageAsset getAsset(String namespace, String project, String version) {
-        return getAsset( namespace, project ).resolve( version );
-    }
-
-    private StorageAsset getAsset(String namespace, String project, String version, String fileName) {
-        return getAsset( namespace, project, version ).resolve( fileName );
-    }
-
-
     @Override
     public Namespace getNamespace( final ItemSelector namespaceSelector ) throws ContentAccessException, IllegalArgumentException
     {
@@ -501,7 +470,8 @@ public class ManagedDefaultRepositoryContent
         final String artifactVersion = mavenContentHelper.getArtifactVersion( artifactDir, selector );
         final String classifier = MavenContentHelper.getClassifier( selector );
         final String extension = MavenContentHelper.getArtifactExtension( selector );
-        final String fileName = MavenContentHelper.getArtifactFileName( selector, artifactVersion, classifier, extension );
+        final String artifactId = StringUtils.isEmpty( selector.getArtifactId( ) ) ? selector.getProjectId( ) : selector.getArtifactId( );
+        final String fileName = MavenContentHelper.getArtifactFileName( artifactId, artifactVersion, classifier, extension );
         final StorageAsset path = getAsset( selector.getNamespace( ), selector.getProjectId( ),
             selector.getVersion( ), fileName );
         return artifactMap.computeIfAbsent( path, artifactPath -> createArtifact( path, selector, classifier, extension ) );
@@ -659,6 +629,41 @@ public class ManagedDefaultRepositoryContent
         return getItemFromPath( assetPath );
     }
 
+    /// ************* End of new generation interface ******************
+
+    /**
+     * Returns a version reference from the coordinates
+     * @param groupId the group id
+     * @param artifactId the artifact id
+     * @param version the version
+     * @return the versioned reference object
+     */
+    @Override
+    public VersionedReference toVersion( String groupId, String artifactId, String version ) {
+        return new VersionedReference().groupId( groupId ).artifactId( artifactId ).version( version );
+    }
+
+    @Override
+    public VersionedReference toGenericVersion( ArtifactReference artifactReference )
+    {
+        return toVersion( artifactReference.getGroupId( ), artifactReference.getArtifactId( ), VersionUtil.getBaseVersion( artifactReference.getVersion( ) ));
+    }
+
+    /**
+     * Return the version the artifact is part of
+     * @param artifactReference
+     * @return
+     */
+    public VersionedReference toVersion( ArtifactReference artifactReference) {
+        return toVersion( artifactReference.getGroupId( ), artifactReference.getArtifactId( ), artifactReference.getVersion( ) );
+    }
+
+    @Override
+    public ArtifactReference toArtifact( String groupId, String artifactId, String version, String type, String classifier) {
+        return new ArtifactReference( ).groupId( groupId ).artifactId( artifactId ).version( version ).type( type ).classifier( classifier );
+    }
+
+
     @Override
     public void deleteVersion( VersionedReference ref ) throws ContentNotFoundException, ContentAccessException
     {
index 73a9120d6521c7919fe0d2b392c23d9830480173..1a807a0e4ae9387fe71b016c5840e8a7999ca20a 100644 (file)
@@ -32,7 +32,9 @@ import org.springframework.stereotype.Service;
 
 import javax.inject.Inject;
 import javax.inject.Named;
+import java.util.Comparator;
 import java.util.LinkedList;
+import java.util.regex.Pattern;
 
 /**
  * Helper class that contains certain maven specific methods
@@ -42,6 +44,8 @@ public class MavenContentHelper
 {
 
     private static final Logger log = LoggerFactory.getLogger( MavenContentHelper.class );
+    public static final Pattern UNIQUE_SNAPSHOT_NUMBER_PATTERN = Pattern.compile( "^([0-9]{8}\\.[0-9]{6}-[0-9]+)(.*)" );
+
 
     @Inject
     @Named( "metadataReader#maven" )
@@ -53,6 +57,11 @@ public class MavenContentHelper
 
     }
 
+    public void setMetadataReader( MavenMetadataReader metadataReader )
+    {
+        this.metadataReader = metadataReader;
+    }
+
     /**
      * Returns the namespace string for a given path in the repository
      *
@@ -64,6 +73,7 @@ public class MavenContentHelper
         StorageAsset current = namespacePath;
         while (current.hasParent()) {
             names.addFirst( current.getName() );
+            current = current.getParent( );
         }
         return String.join( ".", names );
     }
@@ -103,33 +113,40 @@ public class MavenContentHelper
 
             // re-adjust to timestamp if present, otherwise retain the original -SNAPSHOT filename
             SnapshotVersion metadataVersion = metadata.getSnapshotVersion( );
-            if ( metadataVersion != null )
+            if ( metadataVersion != null && StringUtils.isNotEmpty( metadataVersion.getTimestamp( ) ) )
             {
                 version.append( snapshotVersion, 0, snapshotVersion.length( ) - 8 ); // remove SNAPSHOT from end
-                version.append( metadataVersion.getTimestamp( )).append("-").append( metadataVersion.getBuildNumber( ) );
+                version.append( metadataVersion.getTimestamp( ) ).append( "-" ).append( metadataVersion.getBuildNumber( ) );
+                return version.toString( );
             }
-            return version.toString( );
         }
         catch ( RepositoryMetadataException e )
         {
             // unable to parse metadata - LOGGER it, and continue with the version as the original SNAPSHOT version
             log.warn( "Invalid metadata: {} - {}", metadataFile, e.getMessage( ) );
-            return snapshotVersion;
         }
+        final String baseVersion = StringUtils.removeEnd( snapshotVersion, "-SNAPSHOT" );
+        final String prefix = metadataFile.getParent( ).getParent( ).getName( ) + "-"+baseVersion+"-";
+        return artifactDir.list( ).stream( ).filter( a -> a.getName( ).startsWith( prefix ) )
+            .map( a -> StringUtils.removeStart( a.getName( ), prefix ) )
+            .map( n -> UNIQUE_SNAPSHOT_NUMBER_PATTERN.matcher( n ) )
+            .filter( m -> m.matches( ) )
+            .map( m -> baseVersion+"-"+m.group( 1 ) )
+            .sorted( Comparator.reverseOrder() ).findFirst().orElse( snapshotVersion );
     }
 
 
     /**
      * Returns a artifact filename that corresponds to the given data.
-     * @param selector the selector data
+     * @param artifactId the selector data
      * @param artifactVersion the artifactVersion
      * @param classifier the artifact classifier
      * @param extension the file extension
      */
-    static String getArtifactFileName( ItemSelector selector, String artifactVersion,
+    static String getArtifactFileName( String artifactId, String artifactVersion,
                                        String classifier, String extension )
     {
-        StringBuilder fileName = new StringBuilder( selector.getArtifactId( ) ).append( "-" );
+        StringBuilder fileName = new StringBuilder( artifactId ).append( "-" );
         fileName.append( artifactVersion );
         if ( !StringUtils.isEmpty( classifier ) )
         {
@@ -171,33 +188,30 @@ public class MavenContentHelper
      */
     static String getClassifierFromType( final String type )
     {
-        if ( "pom".equalsIgnoreCase( type ) || "jar".equalsIgnoreCase( type )
-            || "maven-plugin".equalsIgnoreCase( type )
-            || "ejb".equalsIgnoreCase( type )
-            || "ear".equalsIgnoreCase( type )
-            || "war".equalsIgnoreCase( type )
-            || "rar".equalsIgnoreCase( type )
-        ) return "";
-        if ( "test-jar".equalsIgnoreCase( type ) )
-        {
-            return "tests";
-        }
-        else if ( "ejb-client".equalsIgnoreCase( type ) )
-        {
-            return "client";
-        }
-        else if ( "java-source".equalsIgnoreCase( type ) )
-        {
-            return "sources";
-        }
-        else if ( "javadoc".equalsIgnoreCase( type ) )
-        {
-            return "javadoc";
-        }
-        else
+        String testType = type.trim( ).toLowerCase( );
+        switch (testType.length( ))
         {
-            return "";
+            case 7:
+                if ("javadoc".equals(testType)) {
+                    return "javadoc";
+                }
+            case 8:
+                if ("test-jar".equals(testType))
+                {
+                    return "tests";
+                }
+            case 10:
+                if ("ejb-client".equals(testType)) {
+                    return "client";
+                }
+            case 11:
+                if ("java-source".equals(testType)) {
+                    return "sources";
+                }
+            default:
+                return "";
         }
+
     }
 
     /**
@@ -227,7 +241,7 @@ public class MavenContentHelper
         {
             return "ejb-client";
         }
-        else if ( classifier.equals( "source" ) && extension.equals( "jar" ) )
+        else if ( classifier.equals( "sources" ) && extension.equals( "jar" ) )
         {
             return "java-source";
         }
@@ -256,26 +270,15 @@ public class MavenContentHelper
         }
         else if ( selector.hasType( ) )
         {
-            String type = selector.getType( ).toLowerCase( );
-            if ( "test-jar".equals( type ) )
-            {
-                return "jar";
-            }
-            else if ( "ejb-client".equals( type ) )
-            {
-                return "jar";
-            }
-            else if ( "java-source".equals( type ) )
-            {
-                return "jar";
-            }
-            else if ( "javadoc".equals( type ) )
-            {
-                return "jar";
-            }
-            else
-            {
-                return "jar";
+            final String type = selector.getType( ).trim().toLowerCase( );
+            switch (type.length()) {
+                case 3:
+                    if ("pom".equals(type) || "war".equals(type) || "ear".equals(type) || "rar".equals(type)) {
+                        return type;
+                    }
+                default:
+                    return "jar";
+
             }
         }
         else
diff --git a/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/content/AbstractDefaultRepositoryContentTestCase.java b/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/content/AbstractDefaultRepositoryContentTestCase.java
deleted file mode 100644 (file)
index 34210c8..0000000
+++ /dev/null
@@ -1,619 +0,0 @@
-package org.apache.archiva.repository.maven.content;
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import org.apache.archiva.model.ArtifactReference;
-import org.apache.archiva.repository.maven.AbstractRepositoryLayerTestCase;
-import org.apache.archiva.repository.LayoutException;
-import org.apache.archiva.repository.ManagedRepositoryContent;
-import org.apache.archiva.repository.content.Artifact;
-import org.apache.archiva.repository.content.ItemSelector;
-import org.apache.archiva.repository.content.Namespace;
-import org.apache.archiva.repository.content.Project;
-import org.apache.archiva.repository.content.Version;
-import org.apache.archiva.repository.content.base.ArchivaItemSelector;
-import org.apache.commons.lang3.StringUtils;
-import org.junit.Test;
-
-import static org.junit.Assert.*;
-
-/**
- * AbstractDefaultRepositoryContentTestCase
- */
-public abstract class AbstractDefaultRepositoryContentTestCase
-    extends AbstractRepositoryLayerTestCase
-{
-    @Test
-    public void testBadPathMissingType()
-    {
-        assertBadPath( "invalid/invalid/1/invalid-1", "missing type" );
-    }
-
-    @Test
-    public void testBadPathReleaseInSnapshotDir()
-    {
-        assertBadPath( "invalid/invalid/1.0-SNAPSHOT/invalid-1.0.jar",
-                       "non snapshot artifact inside of a snapshot dir" );
-    }
-
-    @Test
-    public void testBadPathTimestampedSnapshotNotInSnapshotDir()
-    {
-        assertBadPath( "invalid/invalid/1.0-20050611.123456-1/invalid-1.0-20050611.123456-1.jar",
-                       "Timestamped Snapshot artifact not inside of an Snapshot dir" );
-    }
-
-    @Test
-    public void testBadPathTooShort()
-    {
-        assertBadPath( "invalid/invalid-1.0.jar", "path is too short" );
-    }
-
-    @Test
-    public void testBadPathVersionMismatchA()
-    {
-        assertBadPath( "invalid/invalid/1.0/invalid-2.0.jar", "version mismatch between path and artifact" );
-    }
-
-    @Test
-    public void testBadPathVersionMismatchB()
-    {
-        assertBadPath( "invalid/invalid/1.0/invalid-1.0b.jar", "version mismatch between path and artifact" );
-    }
-
-    @Test
-    public void testBadPathWrongArtifactId()
-    {
-        assertBadPath( "org/apache/maven/test/1.0-SNAPSHOT/wrong-artifactId-1.0-20050611.112233-1.jar",
-                       "wrong artifact id" );
-    }
-
-    /**
-     * [MRM-432] Oddball version spec.
-     * Example of an oddball / unusual version spec.
-     *
-     * @throws LayoutException
-     *
-     */
-    @Test
-    public void testGoodButOddVersionSpecGanymedSsh2()
-        throws LayoutException
-    {
-        String groupId = "ch.ethz.ganymed";
-        String artifactId = "ganymed-ssh2";
-        String version = "build210";
-        String classifier = null;
-        String type = "jar";
-        String path = "ch/ethz/ganymed/ganymed-ssh2/build210/ganymed-ssh2-build210.jar";
-
-        assertLayout( path, groupId, artifactId, version, classifier, type );
-        assertLayoutCi( path, groupId, artifactId, version, classifier, type);
-    }
-
-    /**
-     * [MRM-432] Oddball version spec.
-     * Example of an oddball / unusual version spec.
-     *
-     * @throws LayoutException
-     *
-     */
-    @Test
-    public void testGoodButOddVersionSpecJavaxComm()
-        throws LayoutException
-    {
-        String groupId = "javax";
-        String artifactId = "comm";
-        String version = "3.0-u1";
-        String classifier = null;
-        String type = "jar";
-        String path = "javax/comm/3.0-u1/comm-3.0-u1.jar";
-
-        assertLayout( path, groupId, artifactId, version, classifier, type );
-        assertLayoutCi( path, groupId, artifactId, version, classifier, type );
-    }
-
-    /**
-     * Test the ejb-client type spec.
-     * Type specs are not a 1 to 1 map to the extension.
-     * This tests that effect.
-     * @throws LayoutException
-     */
-    /* TODO: Re-enabled in the future.
-    public void testGoodFooEjbClient()
-        throws LayoutException
-    {
-        String groupId = "com.foo";
-        String artifactId = "foo-client";
-        String version = "1.0";
-        String classifier = null;
-        String type = "ejb-client"; // oddball type-spec (should result in jar extension)
-        String path = "com/foo/foo-client/1.0/foo-client-1.0.jar";
-
-        assertLayout( path, groupId, artifactId, version, classifier, type );
-    }
-    */
-
-    /**
-     * [MRM-432] Oddball version spec.
-     * Example of an oddball / unusual version spec.
-     *
-     * @throws LayoutException
-     *
-     */
-    @Test
-    public void testGoodButOddVersionSpecJavaxPersistence()
-        throws LayoutException
-    {
-        String groupId = "javax.persistence";
-        String artifactId = "ejb";
-        String version = "3.0-public_review";
-        String classifier = null;
-        String type = "jar";
-        String path = "javax/persistence/ejb/3.0-public_review/ejb-3.0-public_review.jar";
-
-        /*
-         * The version id of "public_review" can cause problems. is it part of
-         * the version spec? or the classifier?
-         * Since the path spec below shows it in the path, then it is really
-         * part of the version spec.
-         */
-
-        assertLayout( path, groupId, artifactId, version, classifier, type );
-        assertLayoutCi( path, groupId, artifactId, version, classifier, type );
-    }
-
-    @Test
-    public void testGoodComFooTool()
-        throws LayoutException
-    {
-        String groupId = "com.foo";
-        String artifactId = "foo-tool";
-        String version = "1.0";
-        String classifier = null;
-        String type = "jar";
-        String path = "com/foo/foo-tool/1.0/foo-tool-1.0.jar";
-
-        assertLayout( path, groupId, artifactId, version, classifier, type );
-        assertLayoutCi( path, groupId, artifactId, version, classifier, type );
-    }
-
-    @Test
-    public void testGoodCommonsLang()
-        throws LayoutException
-    {
-        String groupId = "commons-lang";
-        String artifactId = "commons-lang";
-        String version = "2.1";
-        String classifier = null;
-        String type = "jar";
-        String path = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
-
-        assertLayout( path, groupId, artifactId, version, classifier, type );
-        assertLayoutCi( path, groupId, artifactId, version, classifier, type );
-    }
-
-    /**
-     * [MRM-486] Can not deploy artifact test.maven-arch:test-arch due to "No ArtifactID Detected"
-     */
-    @Test
-    public void testGoodDashedArtifactId()
-        throws LayoutException
-    {
-        String groupId = "test.maven-arch";
-        String artifactId = "test-arch";
-        String version = "2.0.3-SNAPSHOT";
-        String classifier = null;
-        String type = "pom";
-        String path = "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.pom";
-
-        assertLayout( path, groupId, artifactId, version, classifier, type );
-        assertLayoutCi( path, groupId, artifactId, version, classifier, type );
-
-    }
-
-    /**
-     * It may seem odd, but this is a valid artifact.
-     */
-    @Test
-    public void testGoodDotNotationArtifactId()
-        throws LayoutException
-    {
-        String groupId = "com.company.department";
-        String artifactId = "com.company.department";
-        String version = "0.2";
-        String classifier = null;
-        String type = "pom";
-        String path = "com/company/department/com.company.department/0.2/com.company.department-0.2.pom";
-
-        assertLayout( path, groupId, artifactId, version, classifier, type );
-        assertLayoutCi( path, groupId, artifactId, version, classifier, type );
-    }
-
-    /**
-     * It may seem odd, but this is a valid artifact.
-     */
-    @Test
-    public void testGoodDotNotationSameGroupIdAndArtifactId()
-        throws LayoutException
-    {
-        String groupId = "com.company.department";
-        String artifactId = "com.company.department.project";
-        String version = "0.3";
-        String classifier = null;
-        String type = "pom";
-        String path =
-            "com/company/department/com.company.department.project/0.3/com.company.department.project-0.3.pom";
-
-        assertLayout( path, groupId, artifactId, version, classifier, type );
-        assertLayoutCi( path, groupId, artifactId, version, classifier, type );
-
-    }
-
-    /**
-     * Test the classifier, and java-source type spec.
-     *
-     * @throws LayoutException
-     *
-     */
-    @Test
-    public void testGoodFooLibSources()
-        throws LayoutException
-    {
-        String groupId = "com.foo.lib";
-        String artifactId = "foo-lib";
-        String version = "2.1-alpha-1";
-        String classifier = "sources";
-        String type = "java-source"; // oddball type-spec (should result in jar extension)
-        String path = "com/foo/lib/foo-lib/2.1-alpha-1/foo-lib-2.1-alpha-1-sources.jar";
-
-        assertLayout( path, groupId, artifactId, version, classifier, type );
-        assertLayoutCi( path, groupId, artifactId, version, classifier, type );
-
-    }
-
-    /**
-     * A timestamped versioned artifact, should reside in a SNAPSHOT baseversion directory.
-     *
-     * @throws LayoutException
-     *
-     */
-    @Test
-    public void testGoodSnapshotMavenTest()
-        throws LayoutException
-    {
-        String groupId = "org.apache.archiva.test";
-        String artifactId = "redonkulous";
-        String version = "3.1-beta-1-20050831.101112-42";
-        String classifier = null;
-        String type = "jar";
-        String path =
-            "org/apache/archiva/test/redonkulous/3.1-beta-1-SNAPSHOT/redonkulous-3.1-beta-1-20050831.101112-42.jar";
-
-        assertLayout( path, groupId, artifactId, version, classifier, type );
-        assertLayoutCi( path, groupId, artifactId, version, classifier, type );
-    }
-
-    /**
-     * [MRM-519] version identifiers within filename cause misidentification of version.
-     * Example uses "test" in artifact Id, which is also part of the versionKeyword list.
-     */
-    @Test
-    public void testGoodVersionKeywordInArtifactId()
-        throws LayoutException
-    {
-        String groupId = "maven";
-        String artifactId = "maven-test-plugin";
-        String version = "1.8.2";
-        String classifier = null;
-        String type = "pom";
-        String path = "maven/maven-test-plugin/1.8.2/maven-test-plugin-1.8.2.pom";
-
-        assertLayout( path, groupId, artifactId, version, classifier, type );
-        assertLayoutCi( path, groupId, artifactId, version, classifier, type );
-    }
-
-    /**
-     * [MRM-562] Artifact type "maven-plugin" is not detected correctly in .toArtifactReference() methods.
-     * Example uses "test" in artifact Id, which is also part of the versionKeyword list.
-     */
-    @Test
-    public void testGoodDetectMavenTestPlugin()
-        throws LayoutException
-    {
-        String groupId = "maven";
-        String artifactId = "maven-test-plugin";
-        String version = "1.8.2";
-        String classifier = null;
-        String type = "maven-plugin";
-        String path = "maven/maven-test-plugin/1.8.2/maven-test-plugin-1.8.2.jar";
-
-        assertLayout( path, groupId, artifactId, version, classifier, type );
-        assertLayoutCi( path, groupId, artifactId, version, classifier, type );
-
-    }
-
-    /**
-     * [MRM-562] Artifact type "maven-plugin" is not detected correctly in .toArtifactReference() methods.
-     */
-    @Test
-    public void testGoodDetectCoberturaMavenPlugin()
-        throws LayoutException
-    {
-        String groupId = "org.codehaus.mojo";
-        String artifactId = "cobertura-maven-plugin";
-        String version = "2.1";
-        String classifier = null;
-        String type = "maven-plugin";
-        String path = "org/codehaus/mojo/cobertura-maven-plugin/2.1/cobertura-maven-plugin-2.1.jar";
-
-        assertLayout( path, groupId, artifactId, version, classifier, type );
-        assertLayoutCi( path, groupId, artifactId, version, classifier, type );
-    }
-
-    @Test
-    public void testToArtifactOnEmptyPath()
-    {
-        try
-        {
-            toArtifactReference( "" );
-            fail( "Should have failed due to empty path." );
-        }
-        catch ( LayoutException e )
-        {
-            /* expected path */
-        }
-    }
-
-    @Test
-    public void testToArtifactOnNullPath()
-    {
-        try
-        {
-            toArtifactReference( null );
-            fail( "Should have failed due to null path." );
-        }
-        catch ( LayoutException e )
-        {
-            /* expected path */
-        }
-    }
-
-    @Test
-    public void testToArtifactReferenceOnEmptyPath()
-    {
-        try
-        {
-            toArtifactReference( "" );
-            fail( "Should have failed due to empty path." );
-        }
-        catch ( LayoutException e )
-        {
-            /* expected path */
-        }
-    }
-
-    @Test
-    public void testToArtifactReferenceOnNullPath()
-    {
-        try
-        {
-            toArtifactReference( null );
-            fail( "Should have failed due to null path." );
-        }
-        catch ( LayoutException e )
-        {
-            /* expected path */
-        }
-    }
-
-    @Test
-    public void testToPathOnNullArtifactReference()
-
-    {
-        try
-        {
-            ArtifactReference reference = null;
-            toPath( reference );
-            fail( "Should have failed due to null artifact reference." );
-        }
-        catch ( IllegalArgumentException e )
-        {
-            /* expected path */
-        }
-    }
-
-    public void testToPathOnNullItemSelector()
-
-    {
-        try
-        {
-            ItemSelector selector = null;
-            toPath( selector );
-            fail( "Should have failed due to null artifact reference." );
-        }
-        catch ( IllegalArgumentException e )
-        {
-            /* expected path */
-        }
-    }
-
-    private void assertArtifactReference( ArtifactReference actualReference, String groupId, String artifactId,
-                                          String version, String classifier, String type )
-    {
-        String expectedId =
-            "ArtifactReference - " + groupId + ":" + artifactId + ":" + version + ":" + classifier + ":" + type;
-
-        assertNotNull( expectedId + " - Should not be null.", actualReference );
-
-        assertEquals( expectedId + " - Group ID", groupId, actualReference.getGroupId() );
-        assertEquals( expectedId + " - Artifact ID", artifactId, actualReference.getArtifactId() );
-        if ( StringUtils.isNotBlank( classifier ) )
-        {
-            assertEquals( expectedId + " - Classifier", classifier, actualReference.getClassifier() );
-        }
-        assertEquals( expectedId + " - Version ID", version, actualReference.getVersion() );
-        assertEquals( expectedId + " - Type", type, actualReference.getType() );
-    }
-
-    private void assertItemSelector( ItemSelector actualReference, String groupId, String artifactId,
-                                          String version, String classifier, String type )
-    {
-        String expectedId =
-            "ArtifactReference - " + groupId + ":" + artifactId + ":" + version + ":" + classifier + ":" + type;
-
-        assertNotNull( expectedId + " - Should not be null.", actualReference );
-
-        assertEquals( expectedId + " - Group ID", groupId, actualReference.getNamespace() );
-        assertEquals( expectedId + " - Artifact ID", artifactId, actualReference.getArtifactId() );
-        if ( StringUtils.isNotBlank( classifier ) )
-        {
-            assertEquals( expectedId + " - Classifier", classifier, actualReference.getClassifier() );
-        }
-        assertEquals( expectedId + " - Version ID", version, actualReference.getArtifactVersion() );
-        assertEquals( expectedId + " - Type", type, actualReference.getType() );
-    }
-
-    private void assertBadPath( String path, String reason )
-    {
-        try
-        {
-            toArtifactReference( path );
-            fail(
-                "Should have thrown a LayoutException on the invalid path [" + path + "] because of [" + reason + "]" );
-        }
-        catch ( LayoutException e )
-        {
-            /* expected path */
-        }
-    }
-
-    /**
-     * Perform a roundtrip through the layout routines to determine success.
-     */
-    private void assertLayout( String path, String groupId, String artifactId, String version, String classifier,
-                               String type )
-        throws LayoutException
-    {
-        ArtifactReference expectedArtifact = createArtifact( groupId, artifactId, version, classifier, type );
-
-        // --- Artifact Tests.
-
-        // Artifact to Path 
-        assertEquals( "Artifact <" + expectedArtifact + "> to path:", path, toPath( expectedArtifact ) );
-
-        // --- Artifact Reference Tests
-
-        // Path to Artifact Reference.
-        ArtifactReference testReference = toArtifactReference( path );
-        assertArtifactReference( testReference, groupId, artifactId, version, classifier, type );
-
-        // And back again, using test Reference from previous step.
-        assertEquals( "Artifact <" + expectedArtifact + "> to path:", path, toPath( testReference ) );
-    }
-
-    private void assertLayoutCi( String path, String groupId, String artifactId, String version, String classifier,
-                               String type )
-        throws LayoutException
-    {
-        ItemSelector expectedArtifact = createItemSelector( groupId, artifactId, version, classifier, type );
-
-        // --- Artifact Tests.
-
-        // Artifact to Path
-        assertEquals( "Artifact <" + expectedArtifact + "> to path:", path, toPath( expectedArtifact ) );
-
-        // --- Artifact Reference Tests
-
-        // Path to Artifact Reference.
-        ItemSelector testReference = toItemSelector( path );
-        assertItemSelector( testReference, groupId, artifactId, version, classifier, type );
-
-        // And back again, using test Reference from previous step.
-        assertEquals( "Artifact <" + expectedArtifact + "> to path:", path, toPath( testReference ) );
-
-        if (getManaged()!=null)
-        {
-            Namespace ns = null;
-            Project pr = null;
-            Version ver = null;
-            if ( StringUtils.isNotEmpty( groupId ) )
-            {
-                ns = getManaged( ).getNamespace( expectedArtifact );
-                assertNotNull( ns );
-                assertEquals( groupId, ns.getNamespace( ) );
-            }
-            if ( StringUtils.isNotEmpty( artifactId ) )
-            {
-                pr = getManaged( ).getProject( expectedArtifact );
-                assertNotNull( pr );
-                assertEquals( artifactId, pr.getId( ) );
-                assertEquals( ns, pr.getNamespace( ) );
-            }
-            if ( StringUtils.isNotEmpty( version ) )
-            {
-                ver = getManaged( ).getVersion( expectedArtifact );
-                assertNotNull( ver );
-                assertEquals( version, ver.getVersion( ) );
-                assertEquals( pr, ver.getProject( ) );
-            }
-            Artifact artifact = getManaged( ).getArtifact( expectedArtifact );
-            assertNotNull( artifact );
-            assertEquals( artifactId, artifact.getId( ) );
-            assertEquals( ver, artifact.getVersion( ) );
-        }
-
-    }
-
-    protected ArtifactReference createArtifact( String groupId, String artifactId, String version, String classifier,
-                                              String type )
-    {
-        ArtifactReference artifact = new ArtifactReference();
-        artifact.setGroupId( groupId );
-        artifact.setArtifactId( artifactId );
-        artifact.setVersion( version );
-        artifact.setClassifier( classifier );
-        artifact.setType( type );
-        assertNotNull( artifact );
-        return artifact;
-    }
-
-    protected ItemSelector createItemSelector(String groupId, String artifactId, String version, String classifier,
-                                              String type) {
-        return ArchivaItemSelector.builder( ).withNamespace( groupId )
-            .withProjectId( artifactId )
-            .withArtifactId( artifactId )
-            .withVersion( version )
-            .withClassifier( classifier )
-            .withType( type )
-            .build( );
-
-    }
-
-    protected abstract ArtifactReference toArtifactReference( String path )
-        throws LayoutException;
-
-    protected abstract String toPath( ArtifactReference reference );
-
-
-    protected abstract String toPath( ItemSelector selector );
-
-    protected abstract ItemSelector toItemSelector(String path) throws LayoutException;
-
-    protected abstract ManagedRepositoryContent getManaged();
-}
diff --git a/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/content/AbstractManagedRepositoryContentTest.java b/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/content/AbstractManagedRepositoryContentTest.java
new file mode 100644 (file)
index 0000000..be3f4ba
--- /dev/null
@@ -0,0 +1,46 @@
+package org.apache.archiva.repository.maven.content;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.archiva.repository.LayoutException;
+
+import static org.junit.Assert.fail;
+
+/**
+ * @author Martin Stockhammer <martin_s@apache.org>
+ */
+public abstract class AbstractManagedRepositoryContentTest extends AbstractRepositoryContentTest
+{
+
+    @Override
+    protected void assertBadPathCi( String path, String reason )
+    {
+        super.assertBadPathCi( path, reason );
+        try
+        {
+            getManaged().toItem( path );
+            fail(
+                "Should have thrown a LayoutException on the invalid path [" + path + "] because of [" + reason + "]" );
+        }
+        catch ( LayoutException e )
+        {
+            /* expected path */
+        }
+    }
+}
diff --git a/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/content/AbstractRepositoryContentTest.java b/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/content/AbstractRepositoryContentTest.java
new file mode 100644 (file)
index 0000000..34a8be4
--- /dev/null
@@ -0,0 +1,639 @@
+package org.apache.archiva.repository.maven.content;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.archiva.model.ArtifactReference;
+import org.apache.archiva.repository.maven.AbstractRepositoryLayerTestCase;
+import org.apache.archiva.repository.LayoutException;
+import org.apache.archiva.repository.ManagedRepositoryContent;
+import org.apache.archiva.repository.content.Artifact;
+import org.apache.archiva.repository.content.ItemSelector;
+import org.apache.archiva.repository.content.Namespace;
+import org.apache.archiva.repository.content.Project;
+import org.apache.archiva.repository.content.Version;
+import org.apache.archiva.repository.content.base.ArchivaItemSelector;
+import org.apache.commons.lang3.StringUtils;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ * AbstractDefaultRepositoryContentTestCase
+ */
+public abstract class AbstractRepositoryContentTest
+    extends AbstractRepositoryLayerTestCase
+{
+    @Test
+    public void testBadPathMissingType()
+    {
+        assertBadPath( "invalid/invalid/1/invalid-1", "missing type" );
+        assertBadPathCi( "invalid/invalid/1/invalid-1", "missing type" );
+    }
+
+    @Test
+    public void testBadPathReleaseInSnapshotDir()
+    {
+        assertBadPath( "invalid/invalid/1.0-SNAPSHOT/invalid-1.0.jar",
+                       "non snapshot artifact inside of a snapshot dir" );
+        assertBadPathCi( "invalid/invalid/1.0-SNAPSHOT/invalid-1.0.jar",
+            "non snapshot artifact inside of a snapshot dir" );
+
+    }
+
+    @Test
+    public void testBadPathTimestampedSnapshotNotInSnapshotDir()
+    {
+        assertBadPath( "invalid/invalid/1.0-20050611.123456-1/invalid-1.0-20050611.123456-1.jar",
+                       "Timestamped Snapshot artifact not inside of an Snapshot dir" );
+        assertBadPathCi( "invalid/invalid/1.0-20050611.123456-1/invalid-1.0-20050611.123456-1.jar",
+            "Timestamped Snapshot artifact not inside of an Snapshot dir" );
+    }
+
+    @Test
+    public void testBadPathTooShort()
+    {
+        assertBadPath( "invalid/invalid-1.0.jar", "path is too short" );
+    }
+
+    @Test
+    public void testBadPathVersionMismatchA()
+    {
+        assertBadPath( "invalid/invalid/1.0/invalid-2.0.jar", "version mismatch between path and artifact" );
+    }
+
+    @Test
+    public void testBadPathVersionMismatchB()
+    {
+        assertBadPath( "invalid/invalid/1.0/invalid-1.0b.jar", "version mismatch between path and artifact" );
+    }
+
+    @Test
+    public void testBadPathWrongArtifactId()
+    {
+        assertBadPath( "org/apache/maven/test/1.0-SNAPSHOT/wrong-artifactId-1.0-20050611.112233-1.jar",
+                       "wrong artifact id" );
+    }
+
+    /**
+     * [MRM-432] Oddball version spec.
+     * Example of an oddball / unusual version spec.
+     *
+     * @throws LayoutException
+     *
+     */
+    @Test
+    public void testGoodButOddVersionSpecGanymedSsh2()
+        throws LayoutException
+    {
+        String groupId = "ch.ethz.ganymed";
+        String artifactId = "ganymed-ssh2";
+        String version = "build210";
+        String classifier = null;
+        String type = "jar";
+        String path = "ch/ethz/ganymed/ganymed-ssh2/build210/ganymed-ssh2-build210.jar";
+
+        assertLayout( path, groupId, artifactId, version, classifier, type );
+        assertLayoutCi( path, groupId, artifactId, version, classifier, type);
+    }
+
+    /**
+     * [MRM-432] Oddball version spec.
+     * Example of an oddball / unusual version spec.
+     *
+     * @throws LayoutException
+     *
+     */
+    @Test
+    public void testGoodButOddVersionSpecJavaxComm()
+        throws LayoutException
+    {
+        String groupId = "javax";
+        String artifactId = "comm";
+        String version = "3.0-u1";
+        String classifier = null;
+        String type = "jar";
+        String path = "javax/comm/3.0-u1/comm-3.0-u1.jar";
+
+        assertLayout( path, groupId, artifactId, version, classifier, type );
+        assertLayoutCi( path, groupId, artifactId, version, classifier, type );
+    }
+
+    /**
+     * Test the ejb-client type spec.
+     * Type specs are not a 1 to 1 map to the extension.
+     * This tests that effect.
+     * @throws LayoutException
+     */
+    /* TODO: Re-enabled in the future.
+    public void testGoodFooEjbClient()
+        throws LayoutException
+    {
+        String groupId = "com.foo";
+        String artifactId = "foo-client";
+        String version = "1.0";
+        String classifier = null;
+        String type = "ejb-client"; // oddball type-spec (should result in jar extension)
+        String path = "com/foo/foo-client/1.0/foo-client-1.0.jar";
+
+        assertLayout( path, groupId, artifactId, version, classifier, type );
+    }
+    */
+
+    /**
+     * [MRM-432] Oddball version spec.
+     * Example of an oddball / unusual version spec.
+     *
+     * @throws LayoutException
+     *
+     */
+    @Test
+    public void testGoodButOddVersionSpecJavaxPersistence()
+        throws LayoutException
+    {
+        String groupId = "javax.persistence";
+        String artifactId = "ejb";
+        String version = "3.0-public_review";
+        String classifier = null;
+        String type = "jar";
+        String path = "javax/persistence/ejb/3.0-public_review/ejb-3.0-public_review.jar";
+
+        /*
+         * The version id of "public_review" can cause problems. is it part of
+         * the version spec? or the classifier?
+         * Since the path spec below shows it in the path, then it is really
+         * part of the version spec.
+         */
+
+        assertLayout( path, groupId, artifactId, version, classifier, type );
+        assertLayoutCi( path, groupId, artifactId, version, classifier, type );
+    }
+
+    @Test
+    public void testGoodComFooTool()
+        throws LayoutException
+    {
+        String groupId = "com.foo";
+        String artifactId = "foo-tool";
+        String version = "1.0";
+        String classifier = null;
+        String type = "jar";
+        String path = "com/foo/foo-tool/1.0/foo-tool-1.0.jar";
+
+        assertLayout( path, groupId, artifactId, version, classifier, type );
+        assertLayoutCi( path, groupId, artifactId, version, classifier, type );
+    }
+
+    @Test
+    public void testGoodCommonsLang()
+        throws LayoutException
+    {
+        String groupId = "commons-lang";
+        String artifactId = "commons-lang";
+        String version = "2.1";
+        String classifier = null;
+        String type = "jar";
+        String path = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
+
+        assertLayout( path, groupId, artifactId, version, classifier, type );
+        assertLayoutCi( path, groupId, artifactId, version, classifier, type );
+    }
+
+    /**
+     * [MRM-486] Can not deploy artifact test.maven-arch:test-arch due to "No ArtifactID Detected"
+     */
+    @Test
+    public void testGoodDashedArtifactId()
+        throws LayoutException
+    {
+        String groupId = "test.maven-arch";
+        String artifactId = "test-arch";
+        String version = "2.0.3-SNAPSHOT";
+        String classifier = null;
+        String type = "pom";
+        String path = "test/maven-arch/test-arch/2.0.3-SNAPSHOT/test-arch-2.0.3-SNAPSHOT.pom";
+
+        assertLayout( path, groupId, artifactId, version, classifier, type );
+        assertLayoutCi( path, groupId, artifactId, version, classifier, type );
+
+    }
+
+    /**
+     * It may seem odd, but this is a valid artifact.
+     */
+    @Test
+    public void testGoodDotNotationArtifactId()
+        throws LayoutException
+    {
+        String groupId = "com.company.department";
+        String artifactId = "com.company.department";
+        String version = "0.2";
+        String classifier = null;
+        String type = "pom";
+        String path = "com/company/department/com.company.department/0.2/com.company.department-0.2.pom";
+
+        assertLayout( path, groupId, artifactId, version, classifier, type );
+        assertLayoutCi( path, groupId, artifactId, version, classifier, type );
+    }
+
+    /**
+     * It may seem odd, but this is a valid artifact.
+     */
+    @Test
+    public void testGoodDotNotationSameGroupIdAndArtifactId()
+        throws LayoutException
+    {
+        String groupId = "com.company.department";
+        String artifactId = "com.company.department.project";
+        String version = "0.3";
+        String classifier = null;
+        String type = "pom";
+        String path =
+            "com/company/department/com.company.department.project/0.3/com.company.department.project-0.3.pom";
+
+        assertLayout( path, groupId, artifactId, version, classifier, type );
+        assertLayoutCi( path, groupId, artifactId, version, classifier, type );
+
+    }
+
+    /**
+     * Test the classifier, and java-source type spec.
+     *
+     * @throws LayoutException
+     *
+     */
+    @Test
+    public void testGoodFooLibSources()
+        throws LayoutException
+    {
+        String groupId = "com.foo.lib";
+        String artifactId = "foo-lib";
+        String version = "2.1-alpha-1";
+        String classifier = "sources";
+        String type = "java-source"; // oddball type-spec (should result in jar extension)
+        String path = "com/foo/lib/foo-lib/2.1-alpha-1/foo-lib-2.1-alpha-1-sources.jar";
+
+        assertLayout( path, groupId, artifactId, version, classifier, type );
+        assertLayoutCi( path, groupId, artifactId, version, classifier, type );
+
+    }
+
+    /**
+     * A timestamped versioned artifact, should reside in a SNAPSHOT baseversion directory.
+     *
+     * @throws LayoutException
+     *
+     */
+    @Test
+    public void testGoodSnapshotMavenTest()
+        throws LayoutException
+    {
+        String groupId = "org.apache.archiva.test";
+        String artifactId = "redonkulous";
+        String version = "3.1-beta-1-20050831.101112-42";
+        String classifier = null;
+        String type = "jar";
+        String path =
+            "org/apache/archiva/test/redonkulous/3.1-beta-1-SNAPSHOT/redonkulous-3.1-beta-1-20050831.101112-42.jar";
+
+        assertLayout( path, groupId, artifactId, version, classifier, type );
+        assertLayoutCi( path, groupId, artifactId, version, classifier, type );
+    }
+
+    /**
+     * [MRM-519] version identifiers within filename cause misidentification of version.
+     * Example uses "test" in artifact Id, which is also part of the versionKeyword list.
+     */
+    @Test
+    public void testGoodVersionKeywordInArtifactId()
+        throws LayoutException
+    {
+        String groupId = "maven";
+        String artifactId = "maven-test-plugin";
+        String version = "1.8.2";
+        String classifier = null;
+        String type = "pom";
+        String path = "maven/maven-test-plugin/1.8.2/maven-test-plugin-1.8.2.pom";
+
+        assertLayout( path, groupId, artifactId, version, classifier, type );
+        assertLayoutCi( path, groupId, artifactId, version, classifier, type );
+    }
+
+    /**
+     * [MRM-562] Artifact type "maven-plugin" is not detected correctly in .toArtifactReference() methods.
+     * Example uses "test" in artifact Id, which is also part of the versionKeyword list.
+     */
+    @Test
+    public void testGoodDetectMavenTestPlugin()
+        throws LayoutException
+    {
+        String groupId = "maven";
+        String artifactId = "maven-test-plugin";
+        String version = "1.8.2";
+        String classifier = null;
+        String type = "maven-plugin";
+        String path = "maven/maven-test-plugin/1.8.2/maven-test-plugin-1.8.2.jar";
+
+        assertLayout( path, groupId, artifactId, version, classifier, type );
+        assertLayoutCi( path, groupId, artifactId, version, classifier, type );
+
+    }
+
+    /**
+     * [MRM-562] Artifact type "maven-plugin" is not detected correctly in .toArtifactReference() methods.
+     */
+    @Test
+    public void testGoodDetectCoberturaMavenPlugin()
+        throws LayoutException
+    {
+        String groupId = "org.codehaus.mojo";
+        String artifactId = "cobertura-maven-plugin";
+        String version = "2.1";
+        String classifier = null;
+        String type = "maven-plugin";
+        String path = "org/codehaus/mojo/cobertura-maven-plugin/2.1/cobertura-maven-plugin-2.1.jar";
+
+        assertLayout( path, groupId, artifactId, version, classifier, type );
+        assertLayoutCi( path, groupId, artifactId, version, classifier, type );
+    }
+
+    @Test
+    public void testToArtifactOnEmptyPath()
+    {
+        try
+        {
+            toArtifactReference( "" );
+            fail( "Should have failed due to empty path." );
+        }
+        catch ( LayoutException e )
+        {
+            /* expected path */
+        }
+    }
+
+    @Test
+    public void testToArtifactOnNullPath()
+    {
+        try
+        {
+            toArtifactReference( null );
+            fail( "Should have failed due to null path." );
+        }
+        catch ( LayoutException e )
+        {
+            /* expected path */
+        }
+    }
+
+    @Test
+    public void testToArtifactReferenceOnEmptyPath()
+    {
+        try
+        {
+            toArtifactReference( "" );
+            fail( "Should have failed due to empty path." );
+        }
+        catch ( LayoutException e )
+        {
+            /* expected path */
+        }
+    }
+
+    @Test
+    public void testToArtifactReferenceOnNullPath()
+    {
+        try
+        {
+            toArtifactReference( null );
+            fail( "Should have failed due to null path." );
+        }
+        catch ( LayoutException e )
+        {
+            /* expected path */
+        }
+    }
+
+    @Test
+    public void testToPathOnNullArtifactReference()
+
+    {
+        try
+        {
+            ArtifactReference reference = null;
+            toPath( reference );
+            fail( "Should have failed due to null artifact reference." );
+        }
+        catch ( IllegalArgumentException e )
+        {
+            /* expected path */
+        }
+    }
+
+    public void testToPathOnNullItemSelector()
+
+    {
+        try
+        {
+            ItemSelector selector = null;
+            toPath( selector );
+            fail( "Should have failed due to null artifact reference." );
+        }
+        catch ( IllegalArgumentException e )
+        {
+            /* expected path */
+        }
+    }
+
+    private void assertArtifactReference( ArtifactReference actualReference, String groupId, String artifactId,
+                                          String version, String classifier, String type )
+    {
+        String expectedId =
+            "ArtifactReference - " + groupId + ":" + artifactId + ":" + version + ":" + classifier + ":" + type;
+
+        assertNotNull( expectedId + " - Should not be null.", actualReference );
+
+        assertEquals( expectedId + " - Group ID", groupId, actualReference.getGroupId() );
+        assertEquals( expectedId + " - Artifact ID", artifactId, actualReference.getArtifactId() );
+        if ( StringUtils.isNotBlank( classifier ) )
+        {
+            assertEquals( expectedId + " - Classifier", classifier, actualReference.getClassifier() );
+        }
+        assertEquals( expectedId + " - Version ID", version, actualReference.getVersion() );
+        assertEquals( expectedId + " - Type", type, actualReference.getType() );
+    }
+
+    private void assertItemSelector( ItemSelector actualReference, String groupId, String artifactId,
+                                          String version, String classifier, String type )
+    {
+        String expectedId =
+            "ArtifactReference - " + groupId + ":" + artifactId + ":" + version + ":" + classifier + ":" + type;
+
+        assertNotNull( expectedId + " - Should not be null.", actualReference );
+
+        assertEquals( expectedId + " - Group ID", groupId, actualReference.getNamespace() );
+        assertEquals( expectedId + " - Artifact ID", artifactId, actualReference.getArtifactId() );
+        if ( StringUtils.isNotBlank( classifier ) )
+        {
+            assertEquals( expectedId + " - Classifier", classifier, actualReference.getClassifier() );
+        }
+        assertEquals( expectedId + " - Version ID", version, actualReference.getArtifactVersion() );
+        assertEquals( expectedId + " - Type", type, actualReference.getType() );
+    }
+
+    private void assertBadPath( String path, String reason )
+    {
+        try
+        {
+            toArtifactReference( path );
+            fail(
+                "Should have thrown a LayoutException on the invalid path [" + path + "] because of [" + reason + "]" );
+        }
+        catch ( LayoutException e )
+        {
+            /* expected path */
+        }
+    }
+
+    protected void assertBadPathCi( String path, String reason )
+    {
+        try
+        {
+            toItemSelector( path );
+            fail(
+                "Should have thrown a LayoutException on the invalid path [" + path + "] because of [" + reason + "]" );
+        }
+        catch ( LayoutException e )
+        {
+            /* expected path */
+        }
+    }
+
+    /**
+     * Perform a roundtrip through the layout routines to determine success.
+     */
+    private void assertLayout( String path, String groupId, String artifactId, String version, String classifier,
+                               String type )
+        throws LayoutException
+    {
+        ArtifactReference expectedArtifact = createArtifact( groupId, artifactId, version, classifier, type );
+
+        // --- Artifact Tests.
+
+        // Artifact to Path 
+        assertEquals( "Artifact <" + expectedArtifact + "> to path:", path, toPath( expectedArtifact ) );
+
+        // --- Artifact Reference Tests
+
+        // Path to Artifact Reference.
+        ArtifactReference testReference = toArtifactReference( path );
+        assertArtifactReference( testReference, groupId, artifactId, version, classifier, type );
+
+        // And back again, using test Reference from previous step.
+        assertEquals( "Artifact <" + expectedArtifact + "> to path:", path, toPath( testReference ) );
+    }
+
+    private void assertLayoutCi( String path, String groupId, String artifactId, String version, String classifier,
+                               String type )
+        throws LayoutException
+    {
+        ItemSelector expectedArtifact = createItemSelector( groupId, artifactId, version, classifier, type );
+
+        // --- Artifact Tests.
+
+        // Artifact to Path
+        assertEquals( "Artifact <" + expectedArtifact + "> to path:", path, toPath( expectedArtifact ) );
+
+        // --- Artifact Reference Tests
+
+        // Path to Artifact Reference.
+        ItemSelector testReference = toItemSelector( path );
+        assertItemSelector( testReference, groupId, artifactId, version, classifier, type );
+
+        // And back again, using test Reference from previous step.
+        assertEquals( "Artifact <" + expectedArtifact + "> to path:", path, toPath( testReference ) );
+
+        if (getManaged()!=null)
+        {
+            Namespace ns = null;
+            Project pr = null;
+            Version ver = null;
+            if ( StringUtils.isNotEmpty( groupId ) )
+            {
+                ns = getManaged( ).getNamespace( expectedArtifact );
+                assertNotNull( ns );
+                assertEquals( groupId, ns.getNamespace( ) );
+            }
+            if ( StringUtils.isNotEmpty( artifactId ) )
+            {
+                pr = getManaged( ).getProject( expectedArtifact );
+                assertNotNull( pr );
+                assertEquals( artifactId, pr.getId( ) );
+                assertEquals( ns, pr.getNamespace( ) );
+            }
+            if ( StringUtils.isNotEmpty( version ) )
+            {
+                ver = getManaged( ).getVersion( expectedArtifact );
+                assertNotNull( ver );
+                assertEquals( version, ver.getVersion( ) );
+                assertEquals( pr, ver.getProject( ) );
+            }
+            Artifact artifact = getManaged( ).getArtifact( expectedArtifact );
+            assertNotNull( artifact );
+            assertEquals( artifactId, artifact.getId( ) );
+            assertEquals( ver, artifact.getVersion( ) );
+        }
+
+    }
+
+    protected ArtifactReference createArtifact( String groupId, String artifactId, String version, String classifier,
+                                              String type )
+    {
+        ArtifactReference artifact = new ArtifactReference();
+        artifact.setGroupId( groupId );
+        artifact.setArtifactId( artifactId );
+        artifact.setVersion( version );
+        artifact.setClassifier( classifier );
+        artifact.setType( type );
+        assertNotNull( artifact );
+        return artifact;
+    }
+
+    protected ItemSelector createItemSelector(String groupId, String artifactId, String version, String classifier,
+                                              String type) {
+        return ArchivaItemSelector.builder( ).withNamespace( groupId )
+            .withProjectId( artifactId )
+            .withArtifactId( artifactId )
+            .withVersion( version )
+            .withClassifier( classifier )
+            .withType( type )
+            .build( );
+
+    }
+
+    protected abstract ArtifactReference toArtifactReference( String path )
+        throws LayoutException;
+
+    protected abstract String toPath( ArtifactReference reference );
+
+
+    protected abstract String toPath( ItemSelector selector );
+
+    protected abstract ItemSelector toItemSelector(String path) throws LayoutException;
+
+    protected abstract ManagedRepositoryContent getManaged();
+}
index 2b484003bf6fda27bfeb4ec39291fc64b46371bb..c58b159cc4b0ead311653631d2809a83fa70f109 100644 (file)
@@ -55,7 +55,7 @@ import static org.junit.Assert.*;
  * ManagedDefaultRepositoryContentTest
  */
 public class ManagedDefaultRepositoryContentTest
-    extends AbstractDefaultRepositoryContentTestCase
+    extends AbstractManagedRepositoryContentTest
 {
     private ManagedDefaultRepositoryContent repoContent;
 
diff --git a/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/content/MavenContentHelperTest.java b/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/content/MavenContentHelperTest.java
new file mode 100644 (file)
index 0000000..6db54b8
--- /dev/null
@@ -0,0 +1,209 @@
+package org.apache.archiva.repository.maven.content;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.archiva.common.filelock.DefaultFileLockManager;
+import org.apache.archiva.maven2.metadata.MavenMetadataReader;
+import org.apache.archiva.repository.content.ItemSelector;
+import org.apache.archiva.repository.content.base.ArchivaItemSelector;
+import org.apache.archiva.repository.storage.StorageAsset;
+import org.apache.archiva.repository.storage.fs.FilesystemStorage;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+/**
+ * @author Martin Stockhammer <martin_s@apache.org>
+ */
+class MavenContentHelperTest
+{
+
+    private static FilesystemStorage storage;
+    private static Path tempDir;
+
+    @BeforeAll
+    static void setUp() throws IOException
+    {
+        tempDir = Files.createTempDirectory( "archivamaventest" );
+        storage = new FilesystemStorage( tempDir, new DefaultFileLockManager() );
+    }
+
+    @AfterAll
+    static void tearDown() {
+        try
+        {
+            Files.deleteIfExists( tempDir );
+        }
+        catch ( IOException e )
+        {
+            System.err.println( "Could not delete " + tempDir );
+        }
+    }
+
+    @Test
+    void getNamespaceFromNamespacePath( )
+    {
+        StorageAsset asset = storage.getAsset( "org/apache/archiva" );
+        String ns = MavenContentHelper.getNamespaceFromNamespacePath( asset );
+        assertNotNull( ns );
+        assertEquals( "org.apache.archiva", ns );
+
+        asset = storage.getAsset( "" );
+        ns = MavenContentHelper.getNamespaceFromNamespacePath( asset );
+        assertNotNull( ns );
+        assertEquals( "", ns );
+    }
+
+    @Test
+    void getArtifactVersion( ) throws IOException, URISyntaxException
+    {
+        MavenContentHelper mavenContentHelper = new MavenContentHelper( );
+        MavenMetadataReader reader = new MavenMetadataReader( );
+        mavenContentHelper.setMetadataReader( reader );
+        Path testRepoPath = Paths.get( Thread.currentThread( ).getContextClassLoader( ).getResource( "repositories/metadata-repository" ).toURI() );
+        FilesystemStorage storage = new FilesystemStorage( testRepoPath, new DefaultFileLockManager( ) );
+        assertArtifactVersion( mavenContentHelper, "1.0-alpha-11-SNAPSHOT", storage.getAsset( "org/apache/archiva/metadata/tests/snap_shots_1/1.0-alpha-11-SNAPSHOT" )
+        , "1.0-alpha-11-SNAPSHOT", "1.0-alpha-11-SNAPSHOT");
+
+        assertArtifactVersion( mavenContentHelper, "1.0-alpha-11-20070316.175232-11", storage.getAsset( "org/apache/archiva/metadata/tests/snap_shots_a/1.0-alpha-11-SNAPSHOT" )
+            , "", "1.0-alpha-11-SNAPSHOT");
+
+        assertArtifactVersion( mavenContentHelper, "2.2-20070316.153953-10", storage.getAsset( "org/apache/archiva/metadata/tests/snap_shots_b/2.2-SNAPSHOT" )
+            , "", "2.2-SNAPSHOT");
+
+    }
+
+    private void assertArtifactVersion(MavenContentHelper mavenContentHelper, String expectedVersion, StorageAsset dir, String selectorArtifactVersion, String selectorVersion) {
+        ItemSelector selector = ArchivaItemSelector.builder( )
+            .withVersion( selectorVersion )
+            .withArtifactVersion( selectorArtifactVersion )
+            .build( );
+        assertEquals( expectedVersion, mavenContentHelper.getArtifactVersion( dir, selector ) );
+    }
+
+    @Test
+    void getLatestArtifactSnapshotVersion( ) throws URISyntaxException, IOException
+    {
+        MavenContentHelper mavenContentHelper = new MavenContentHelper( );
+        MavenMetadataReader reader = new MavenMetadataReader( );
+        mavenContentHelper.setMetadataReader( reader );
+        Path testRepoPath = Paths.get( Thread.currentThread( ).getContextClassLoader( ).getResource( "repositories/default-repository" ).toURI() );
+        FilesystemStorage storage = new FilesystemStorage( testRepoPath, new DefaultFileLockManager( ) );
+        // Directory without metadata file
+        assertEquals( "2.1-20090808.085535-2", mavenContentHelper.getLatestArtifactSnapshotVersion( storage.getAsset( "org/apache/archiva/sample-parent/2.1-SNAPSHOT" ), "2.1-SNAPSHOT" ) );
+        // Directory with metadata file
+        assertEquals( "1.3-20070802.113139-29", mavenContentHelper.getLatestArtifactSnapshotVersion( storage.getAsset( "org/apache/axis2/axis2/1.3-SNAPSHOT" ), "1.3-SNAPSHOT" ) );
+    }
+
+    @Test
+    void getArtifactFileName( )
+    {
+        assertFileName( "test-1.0.jar", "test", "", "1.0", "jar" );
+        assertFileName( "test-1.1-client.jar", "test", "client", "1.1", "jar" );
+        assertFileName( "te445st-2.1-sources.jar", "te445st", "sources", "2.1", "jar" );
+        assertFileName( "abcde-8888.994894.48484-10.jar", "abcde", "", "8888.994894.48484-10", "jar" );
+        assertFileName( "testarchive-5.0.war", "testarchive", "", "5.0", "war" );
+    }
+
+    private void assertFileName(String expectedFilename, String artifactId, String classifier, String version, String extension) {
+        assertEquals( expectedFilename, MavenContentHelper.getArtifactFileName( artifactId, version, classifier, extension ) );
+    }
+
+    @Test
+    void getClassifier( )
+    {
+        assertClassifier( "sources", "","java-source" );
+        assertClassifier( "tests", "", "test-jar" );
+        assertClassifier( "client", "","ejb-client" );
+        assertClassifier( "javadoc", "","javadoc" );
+        assertClassifier( "", "","test" );
+        assertClassifier( "test1", "test1","java-source" );
+        assertClassifier( "test2", "test2", "test-jar" );
+        assertClassifier( "test3", "test3","ejb-client" );
+        assertClassifier( "test4", "test4","javadoc" );
+        assertClassifier( "test5", "test5","test" );
+    }
+    private void assertClassifier(String expectedClassifier, String classifier, String type) {
+        ItemSelector selector = ArchivaItemSelector.builder( )
+            .withClassifier( classifier )
+            .withType( type ).build();
+        assertEquals( expectedClassifier, MavenContentHelper.getClassifier( selector ) );
+    }
+
+    @Test
+    void getClassifierFromType( )
+    {
+        assertClassifier( "sources", "java-source" );
+        assertClassifier( "tests", "test-jar" );
+        assertClassifier( "client", "ejb-client" );
+        assertClassifier( "javadoc", "javadoc" );
+        assertClassifier( "", "test" );
+    }
+
+    private void assertClassifier(String expectedClassifier, String type) {
+        assertEquals( expectedClassifier, MavenContentHelper.getClassifierFromType( type ) );
+    }
+
+    @Test
+    void getTypeFromClassifierAndExtension( )
+    {
+        assertType( "javadoc", "javadoc", "jar" );
+        assertType( "war", "", "war" );
+        assertType( "ear", "", "ear" );
+        assertType( "rar", "", "rar" );
+        assertType( "java-source", "sources", "jar" );
+        assertType( "ejb-client", "client", "jar" );
+        assertType( "pom", "", "pom" );
+        assertType( "test-jar", "tests", "jar" );
+
+    }
+
+    private void assertType(String expectedType, String classifier, String extension) {
+        assertEquals( expectedType, MavenContentHelper.getTypeFromClassifierAndExtension( classifier, extension ) );
+    }
+
+
+
+    @Test
+    void getArtifactExtension( )
+    {
+        assertExtension( "test", "", "test" );
+        assertExtension( "jar", "javadoc", "" );
+        assertExtension( "war", "war", "" );
+        assertExtension( "ear", "ear", "" );
+        assertExtension( "rar", "rar", "" );
+        assertExtension( "jar", "", "" );
+    }
+
+    private void assertExtension( String expectedExtension, String type, String extension )
+    {
+        ItemSelector selector = ArchivaItemSelector.builder( )
+            .withType( type ).withExtension( extension ).build();
+        assertEquals( expectedExtension, MavenContentHelper.getArtifactExtension( selector ) );
+    }
+}
\ No newline at end of file
index ca774f4736a05999a925c8a270c6d24cfeebc979..d8d08600f068351f31e788c1232afdac99b733c7 100644 (file)
@@ -24,8 +24,6 @@ import org.apache.archiva.repository.ManagedRepositoryContent;
 import org.apache.archiva.repository.RemoteRepository;
 import org.apache.archiva.repository.RemoteRepositoryContent;
 import org.apache.archiva.repository.content.ItemSelector;
-import org.apache.archiva.repository.maven.content.AbstractDefaultRepositoryContentTestCase;
-import org.apache.archiva.repository.maven.content.RemoteDefaultRepositoryContent;
 import org.apache.archiva.repository.maven.metadata.storage.ArtifactMappingProvider;
 import org.junit.Before;
 
@@ -36,7 +34,7 @@ import java.util.List;
  * RemoteDefaultRepositoryContentTest
  */
 public class RemoteDefaultRepositoryContentTest
-    extends AbstractDefaultRepositoryContentTestCase
+    extends AbstractRepositoryContentTest
 {
 
     @Inject