]> source.dussan.org Git - archiva.git/commitdiff
fix an issue with not removing project version when removing an artifact from Metadat...
authorOlivier Lamy <olamy@apache.org>
Fri, 1 Jun 2012 23:48:01 +0000 (23:48 +0000)
committerOlivier Lamy <olamy@apache.org>
Fri, 1 Jun 2012 23:48:01 +0000 (23:48 +0000)
git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1345397 13f79535-47bb-0310-9956-ffa450edef68

13 files changed:
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/AbstractRestService.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultBrowseService.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultRepositoriesService.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/AbstractArchivaRestTest.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/RepositoriesServiceTest.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/resources/META-INF/spring-context-test.xml
archiva-modules/metadata/metadata-repository-api/pom.xml
archiva-modules/metadata/metadata-repository-api/src/main/java/org/apache/archiva/metadata/repository/DefaultMetadataResolver.java
archiva-modules/metadata/metadata-repository-api/src/test/java/org/apache/archiva/metadata/repository/AbstractMetadataRepositoryTest.java
archiva-modules/plugins/metadata-store-file/pom.xml
archiva-modules/plugins/metadata-store-jcr/pom.xml
archiva-modules/plugins/metadata-store-jcr/src/main/java/org/apache/archiva/metadata/repository/jcr/JcrMetadataRepository.java
archiva-modules/plugins/metadata-store-jcr/src/test/java/org/apache/archiva/metadata/repository/jcr/JcrMetadataRepositoryTest.java

index b95fd1ee37e468afbeaad2c56e7ea2c29b92c9bc..fbe8f85e69837cf05deda306378c620fd45d1678 100644 (file)
@@ -216,7 +216,7 @@ public abstract class AbstractRestService
             }
             else
             {
-                sb.append( '.' ).append( artifact.getPackaging() );
+                sb.append( '.' ).append( artifact.getFileExtension() );
             }
 
             return sb.toString();
index 2ef6db202c3a4a017196497303bf22e9355de406..5228ae6216f971162747eb02122142759ab491cc 100644 (file)
@@ -204,7 +204,8 @@ public class DefaultBrowseService
 
         try
         {
-            return new VersionsList( new ArrayList<String>( getVersions( selectedRepos, groupId, artifactId ) ) );
+            Collection<String> versions = getVersions( selectedRepos, groupId, artifactId );
+            return new VersionsList( new ArrayList<String>( versions ) );
         }
         catch ( MetadataResolutionException e )
         {
@@ -227,8 +228,9 @@ public class DefaultBrowseService
 
             for ( String repoId : selectedRepos )
             {
-                versions.addAll(
-                    metadataResolver.resolveProjectVersions( repositorySession, repoId, groupId, artifactId ) );
+                Collection<String> projectVersions =
+                    metadataResolver.resolveProjectVersions( repositorySession, repoId, groupId, artifactId );
+                versions.addAll( projectVersions );
             }
 
             List<String> sortedVersions = new ArrayList<String>( versions );
index 3890a4cf3fa99be2ea6499cf3d0ee26f67e5736d..6ee2cec2a49fd24543dcd7428596618c573797c5 100644 (file)
@@ -717,7 +717,7 @@ public class DefaultRepositoriesService
             for ( ArtifactMetadata artifactMetadata : artifacts )
             {
                 // TODO: mismatch between artifact (snapshot) version and project (base) version here
-                if ( artifact.getVersion().equals( artifact.getVersion() ) )
+                if ( artifactMetadata.getVersion().equals( artifact.getVersion() ) )
                 {
                     metadataRepository.removeArtifact( artifactMetadata.getRepositoryId(),
                                                        artifactMetadata.getNamespace(), artifactMetadata.getProject(),
@@ -735,7 +735,10 @@ public class DefaultRepositoriesService
                     triggerAuditEvent( repositoryId, path, AuditEvent.REMOVE_FILE );
                 }
             }
+
             repositorySession.save();
+
+            repositorySession.close();
         }
 
         catch ( ContentNotFoundException e )
index ebf401d4da1cb789da0fd4ba330e3872f08b67c8..085595cf4091f092752243bab1335f4b73a824eb 100644 (file)
@@ -55,7 +55,7 @@ public abstract class AbstractArchivaRestTest
 
     // START SNIPPET: authz-header
     // guest with an empty password
-    public String guestAuthzHeader =
+    public static String guestAuthzHeader =
         "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "guest" + ":" ).getBytes() );
 
     // with an other login/password
index 111653350a28be06bd60ba51414f2af5cc226aac..4ecdddbe7918724cff0ce06b452615c37b255f67 100644 (file)
@@ -22,11 +22,13 @@ package org.apache.archiva.rest.services;
 import org.apache.archiva.admin.model.beans.ManagedRepository;
 import org.apache.archiva.common.utils.FileUtil;
 import org.apache.archiva.rest.api.model.Artifact;
+import org.apache.archiva.rest.api.model.VersionsList;
 import org.apache.archiva.rest.api.services.BrowseService;
 import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
 import org.apache.archiva.rest.api.services.RepositoriesService;
 import org.apache.cxf.jaxrs.client.ServerWebApplicationException;
 import org.fest.assertions.Assertions;
+import org.junit.Ignore;
 import org.junit.Test;
 
 import java.io.File;
@@ -133,17 +135,83 @@ public class RepositoriesServiceTest
 
         BrowseService browseService = getBrowseService( authorizationHeader, false );
 
+        List<Artifact> artifacts =
+            browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
+                                                    "2.2.2", SOURCE_REPO_ID );
+
+        log.info( "artifacts: {}", artifacts );
+
+        Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 2 );
+
+        VersionsList versionsList =
+            browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
+                                           SOURCE_REPO_ID );
+        Assertions.assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 2 );
+
+        log.info( "artifacts.size: {}", artifacts.size() );
+
+        try
+        {
+            File artifactFile = new File(
+                "target/test-origin-repo/org/apache/karaf/features/org.apache.karaf.features.core/2.2.2/org.apache.karaf.features.core-2.2.2.jar" );
+
+            assertTrue( "artifact not exists:" + artifactFile.getPath(), artifactFile.exists() );
+
+            Artifact artifact = new Artifact();
+            artifact.setGroupId( "org.apache.karaf.features" );
+            artifact.setArtifactId( "org.apache.karaf.features.core" );
+            artifact.setVersion( "2.2.2" );
+            artifact.setPackaging( "jar" );
+            artifact.setContext( SOURCE_REPO_ID );
+
+            RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
+
+            repositoriesService.deleteArtifact( artifact );
+
+            assertFalse( "artifact not deleted exists:" + artifactFile.getPath(), artifactFile.exists() );
+
+            artifacts =
+                browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
+                                                        "2.2.2", SOURCE_REPO_ID );
+
+            Assertions.assertThat( artifacts ).isNotNull().isEmpty();
+
+            versionsList = browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
+                                                          SOURCE_REPO_ID );
+
+            Assertions.assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 1 );
+
+        }
+        finally
+        {
+            cleanRepos();
+        }
+    }
+
+    @Test
+    @Ignore
+    public void deleteArtifactWithClassifier()
+        throws Exception
+    {
+        initSourceTargetRepo();
+
+        BrowseService browseService = getBrowseService( authorizationHeader, false );
+
         List<Artifact> artifacts =
             browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.0.1", SOURCE_REPO_ID );
 
         Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 3 );
 
+        VersionsList versionsList =
+            browseService.getVersionsList( "commons-logging", "commons-logging", SOURCE_REPO_ID );
+        Assertions.assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 6 );
+
         log.info( "artifacts.size: {}", artifacts.size() );
 
         try
         {
-            File artifactFile =
-                new File( "target/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1.jar" );
+            File artifactFile = new File(
+                "target/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1-javadoc.jar" );
 
             assertTrue( "artifact not exists:" + artifactFile.getPath(), artifactFile.exists() );
 
@@ -151,6 +219,7 @@ public class RepositoriesServiceTest
             artifact.setGroupId( "commons-logging" );
             artifact.setArtifactId( "commons-logging" );
             artifact.setVersion( "1.0.1" );
+            artifact.setClassifier( "javadoc" );
             artifact.setPackaging( "jar" );
             artifact.setContext( SOURCE_REPO_ID );
 
@@ -165,6 +234,8 @@ public class RepositoriesServiceTest
 
             Assertions.assertThat( artifacts ).isNotNull().isEmpty();
 
+            Assertions.assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 5 );
+
         }
         finally
         {
@@ -172,6 +243,7 @@ public class RepositoriesServiceTest
         }
     }
 
+
     @Test
     public void authorizedToDeleteArtifacts()
         throws Exception
index 4478e93dc47bf75e461bfe2120eb2cb7009b17c1..ba9396aa6ee17990ea4a79b107c367aa3c5a1298 100644 (file)
@@ -77,6 +77,7 @@
   <bean id="repository" class="org.apache.jackrabbit.core.RepositoryImpl" destroy-method="shutdown">
     <constructor-arg ref="config"/>
   </bean>
+
   <bean id="config" class="org.apache.jackrabbit.core.config.RepositoryConfig" factory-method="create">
     <constructor-arg value="${basedir}/src/test/repository.xml"/>
     <constructor-arg value="${appserver.base}/jcr"/>
index d1847ce836ecba950849ddb0cb9b79d57a485680..cbe3affaf10de26efa62d552ce46a5441df134ee 100644 (file)
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-api</artifactId>
     </dependency>
+    <dependency>
+      <groupId>org.easytesting</groupId>
+      <artifactId>fest-assert</artifactId>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
   <build>
     <plugins>
index bb9cc49cb46424645d47b39388a7b7e2a487bd6d..014f755e7af930c0d9d2d242abe5ae55fc8a03e4 100644 (file)
@@ -296,6 +296,7 @@ public class DefaultMetadataResolver
         try
         {
             MetadataRepository metadataRepository = session.getRepository();
+
             Collection<String> projectVersions = metadataRepository.getProjectVersions( repoId, namespace, projectId );
             Collection<String> storageProjectVersions =
                 repositoryStorage.listProjectVersions( repoId, namespace, projectId,
index afaf1f14900517f6c9242018b42638ffa51c2dab..da9099aaa430f1bed6eadb2ce4926d73c794fefe 100644 (file)
@@ -32,8 +32,11 @@ import org.apache.archiva.metadata.model.Organization;
 import org.apache.archiva.metadata.model.ProjectMetadata;
 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
 import org.apache.archiva.metadata.model.Scm;
+import org.fest.assertions.Assertions;
 import org.junit.Test;
 import org.junit.runner.RunWith;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
@@ -64,6 +67,8 @@ public abstract class AbstractMetadataRepositoryTest
 
     private static final String TEST_PROJECT_VERSION = "1.0";
 
+    private static final String TEST_PROJECT_VERSION_2_0 = "2.0";
+
     private static final String TEST_FACET_ID = "test-facet-id";
 
     private static final String TEST_NAME = "test/name";
@@ -78,6 +83,8 @@ public abstract class AbstractMetadataRepositoryTest
 
     private static final String TEST_METADATA_VALUE = "test-metadata";
 
+    protected Logger log = LoggerFactory.getLogger( getClass() );
+
     protected static Map<String, MetadataFacetFactory> createTestMetadataFacetFactories()
     {
         Map<String, MetadataFacetFactory> factories = new HashMap<String, MetadataFacetFactory>();
@@ -603,6 +610,7 @@ public abstract class AbstractMetadataRepositoryTest
     public void testGetMetadataFacetsWhenEmpty()
         throws Exception
     {
+
         List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
         assertTrue( facets.isEmpty() );
     }
@@ -1049,10 +1057,27 @@ public abstract class AbstractMetadataRepositoryTest
         assertEquals( Collections.singletonList( artifact ), new ArrayList<ArtifactMetadata>(
             repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) ) );
 
+        repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION_2_0, artifact );
+
+        Collection<String> versions = repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
+
+        log.info( "versions {}", versions );
+
+        Assertions.assertThat( versions ).isNotNull().isNotEmpty().hasSize( 2 ).contains( "1.0", "2.0" );
+
         repository.removeArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact.getId() );
 
+        versions = repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
+
+        log.info( "versions {}", versions );
+
+        Assertions.assertThat( versions ).isNotNull().isNotEmpty().hasSize( 1 ).contains( "2.0" );
+
         assertTrue(
             repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ).isEmpty() );
+
+        Assertions.assertThat( repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
+                                                        TEST_PROJECT_VERSION_2_0 ) ).isNotEmpty().hasSize( 1 );
     }
 
     @Test
@@ -1093,6 +1118,27 @@ public abstract class AbstractMetadataRepositoryTest
         assertTrue( repository.getRootNamespaces( TEST_REPO_ID ).isEmpty() );
     }
 
+
+    @Test
+    public void deleteVersion()
+        throws Exception
+    {
+        ArtifactMetadata artifact = createArtifact();
+        artifact.addFacet( new TestMetadataFacet( "value" ) );
+
+        repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
+
+        repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
+
+        assertEquals( Collections.singletonList( artifact ), new ArrayList<ArtifactMetadata>(
+            repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) ) );
+
+        repository.removeArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact.getId() );
+
+        assertTrue(
+            repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ).isEmpty() );
+    }
+
     private static ProjectMetadata createProject()
     {
         return createProject( TEST_NAMESPACE );
index fd50666e15ceb8763421294969dd390aef67fd57..951c2e0b4423991c72d71f304656d121ce0cbd74 100644 (file)
       <artifactId>spring-test</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.easytesting</groupId>
+      <artifactId>fest-assert</artifactId>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
   <build>
     <plugins>
index ea7932b4109178960872aea57807c3801d6bc0d7..218bb3923d9bd0c324bed188dfd628a81fcf65d5 100644 (file)
       <artifactId>spring-test</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.easytesting</groupId>
+      <artifactId>fest-assert</artifactId>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
   <build>
     <testResources>
index 0ad272528c3ec3212a24be39c8ae053095e8af13..b0acf2950e2b473f0dff697dfb2c2d12763c188c 100644 (file)
@@ -35,6 +35,7 @@ import org.apache.archiva.metadata.model.Scm;
 import org.apache.archiva.metadata.repository.MetadataRepository;
 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
 import org.apache.archiva.metadata.repository.MetadataResolutionException;
+import org.apache.commons.lang.StringUtils;
 import org.apache.jackrabbit.commons.JcrUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -615,24 +616,6 @@ public class JcrMetadataRepository
         return artifacts;
     }
 
-    public void removeArtifact( String repositoryId, String namespace, String projectId, String projectVersion,
-                                String id )
-        throws MetadataRepositoryException
-    {
-        try
-        {
-            Node root = getJcrSession().getRootNode();
-            String path = getArtifactPath( repositoryId, namespace, projectId, projectVersion, id );
-            if ( root.hasNode( path ) )
-            {
-                root.getNode( path ).remove();
-            }
-        }
-        catch ( RepositoryException e )
-        {
-            throw new MetadataRepositoryException( e.getMessage(), e );
-        }
-    }
 
     public void removeRepository( String repositoryId )
         throws MetadataRepositoryException
@@ -994,6 +977,40 @@ public class JcrMetadataRepository
         return getNodeNames( getProjectPath( repositoryId, namespace, projectId ), PROJECT_VERSION_NODE_TYPE );
     }
 
+    public void removeArtifact( String repositoryId, String namespace, String projectId, String projectVersion,
+                                String id )
+        throws MetadataRepositoryException
+    {
+        try
+        {
+            Node root = getJcrSession().getRootNode();
+            String path = getArtifactPath( repositoryId, namespace, projectId, projectVersion, id );
+            if ( root.hasNode( path ) )
+            {
+                root.getNode( path ).remove();
+            }
+
+            // remove version
+
+            path = getProjectPath( repositoryId, namespace, projectId );
+
+            Node nodeAtPath = root.getNode( path );
+
+            for ( Node node : JcrUtils.getChildNodes( nodeAtPath ) )
+            {
+                if ( node.isNodeType( PROJECT_VERSION_NODE_TYPE ) && StringUtils.equals( node.getName(),
+                                                                                         projectVersion ) )
+                {
+                    node.remove();
+                }
+            }
+        }
+        catch ( RepositoryException e )
+        {
+            throw new MetadataRepositoryException( e.getMessage(), e );
+        }
+    }
+
     public Collection<ArtifactMetadata> getArtifacts( String repositoryId, String namespace, String projectId,
                                                       String projectVersion )
         throws MetadataResolutionException