]> source.dussan.org Git - archiva.git/commitdiff
Renaming method and adding additional test
authorMartin Stockhammer <martin_s@apache.org>
Mon, 23 Mar 2020 18:21:50 +0000 (19:21 +0100)
committerMartin Stockhammer <martin_s@apache.org>
Mon, 23 Mar 2020 18:21:50 +0000 (19:21 +0100)
archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/ManagedRepositoryContent.java
archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/mock/ManagedRepositoryContentMock.java
archiva-modules/archiva-base/archiva-repository-scanner/src/test/java/org/apache/archiva/repository/scanner/mock/ManagedRepositoryContentMock.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/test/java/org/apache/archiva/repository/maven/content/ManagedDefaultRepositoryContentTest.java

index 318a6a6c010e5a7daf640708a11d408f1cdc619f..20d0a670354c8590fed578cdf638553cd4c80aa2 100644 (file)
@@ -267,7 +267,7 @@ public interface ManagedRepositoryContent extends RepositoryContent
      * @param destination the coordinates of the destination
      * @throws IllegalArgumentException if the destination is not valid
      */
-    void copyArtifact( Path sourceFile, ContentItem destination ) throws IllegalArgumentException;
+    void addArtifact( Path sourceFile, Artifact destination ) throws IllegalArgumentException, ContentAccessException;
 
 
     /**
index 21230c869a06dafd5f2ee3f14a5c752e1bbf4ba6..bcb330f5eb3b0678c36ecd2f924f3d7d6bd72526 100644 (file)
@@ -181,7 +181,7 @@ public class ManagedRepositoryContentMock implements ManagedRepositoryContent
     }
 
     @Override
-    public void copyArtifact( Path sourceFile, ContentItem destination ) throws IllegalArgumentException
+    public void addArtifact( Path sourceFile, Artifact destination ) throws IllegalArgumentException
     {
 
     }
index b79e47e0a5543c4ccddc7897eb52392782f5cd5c..34a3e809d89182acdb57089b10ce54d7ab4df0f9 100644 (file)
@@ -182,7 +182,7 @@ public class ManagedRepositoryContentMock implements ManagedRepositoryContent
     }
 
     @Override
-    public void copyArtifact( Path sourceFile, ContentItem destination ) throws IllegalArgumentException
+    public void addArtifact( Path sourceFile, Artifact destination ) throws IllegalArgumentException
     {
 
     }
index 17069d590613b62c1dfd07972de976b0b7d94909..81d240eeac210ef814b8af2818c788874145ffc6 100644 (file)
@@ -186,7 +186,7 @@ public class ManagedRepositoryContentMock implements ManagedRepositoryContent
     }
 
     @Override
-    public void copyArtifact( Path sourceFile, ContentItem destination ) throws IllegalArgumentException
+    public void addArtifact( Path sourceFile, Artifact destination ) throws IllegalArgumentException
     {
 
     }
index 4da98e1062cdbc874c95e362d56d9f582e7205af..b79753cc717ce5becec716c192ce00bf26bea543 100644 (file)
@@ -864,21 +864,27 @@ public class ManagedDefaultRepositoryContent
         return getItem( selector ).getAsset( ).exists( );
     }
 
-    /*
-        TBD
+    /**
+     * Moves the file to the artifact destination
      */
     @Override
-    public void copyArtifact( Path sourceFile, ContentItem destination ) throws IllegalArgumentException
+    public void addArtifact( Path sourceFile, Artifact destination ) throws IllegalArgumentException, ContentAccessException
     {
-
+        try
+        {
+            StorageAsset asset = destination.getAsset( );
+            if (!asset.exists()) {
+                asset.create();
+            }
+            asset.replaceDataFromFile( sourceFile );
+        }
+        catch ( IOException e )
+        {
+            log.error( "Could not push data to asset source={} destination={}. {}", sourceFile, destination.getAsset().getFilePath(), e.getMessage( ) );
+            throw new ContentAccessException( e.getMessage( ), e );
+        }
     }
 
-    /**
-     * TBD
-     * @param path the path string that points to the item
-     * @return
-     * @throws LayoutException
-     */
     @Override
     public ContentItem toItem( String path ) throws LayoutException
     {
index 2d7098d223ab1af20515c10ad615202d67083d31..e8fab792157b3711ec793102b50cd6eef90266bc 100644 (file)
@@ -19,7 +19,6 @@ package org.apache.archiva.repository.maven.content;
  */
 
 import org.apache.archiva.common.filelock.FileLockManager;
-import org.apache.archiva.common.utils.PathUtil;
 import org.apache.archiva.common.utils.VersionComparator;
 import org.apache.archiva.configuration.ArchivaConfiguration;
 import org.apache.archiva.configuration.FileType;
@@ -50,16 +49,18 @@ import org.junit.Test;
 
 import javax.inject.Inject;
 import javax.inject.Named;
-import javax.naming.Name;
 import java.io.IOException;
+import java.io.OutputStream;
+import java.io.Reader;
 import java.net.URISyntaxException;
+import java.nio.charset.Charset;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
+import java.nio.file.attribute.FileTime;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
-import java.util.Comparator;
 import java.util.List;
 import java.util.Set;
 import java.util.stream.Collectors;
@@ -1569,4 +1570,66 @@ public class ManagedDefaultRepositoryContentTest
 
     }
 
+
+    @Test
+    public void testCopyArtifact() throws IOException, URISyntaxException
+    {
+        ManagedRepository repo = createManagedRepoWithContent( "delete-repository" );
+        ManagedRepositoryContent myRepoContent = repo.getContent( );
+        Path repoRoot = repo.getAsset( "" ).getFilePath( );
+
+        Path tmpFile = Files.createTempFile( "archiva-mvn-repotest", "jar" );
+        try( OutputStream outputStream = Files.newOutputStream( tmpFile ))
+        {
+            for ( int i = 0; i < 255; i++ )
+            {
+                outputStream.write( "test.test.test\n".getBytes( Charset.forName( "UTF-8" ) ) );
+            }
+        }
+
+        Path file = repoRoot.resolve( "org/apache/maven/samplejar/2.0/samplejar-2.0.jar" );
+        FileTime lmt = Files.getLastModifiedTime( file );
+        ArchivaItemSelector selector = ArchivaItemSelector.builder( )
+            .withNamespace( "org.apache.maven" )
+            .withProjectId( "samplejar" )
+            .withVersion( "2.0" )
+            .withArtifactId( "samplejar" )
+            .withArtifactVersion( "2.0" )
+            .withExtension( "jar" )
+            .build( );
+        Artifact artifact = myRepoContent.getArtifact( selector );
+        myRepoContent.addArtifact( tmpFile, artifact );
+        FileTime lmtAfter = Files.getLastModifiedTime( file );
+        assertNotEquals( lmtAfter, lmt );
+        Reader ln = Files.newBufferedReader( file, Charset.forName( "UTF-8" ) );
+        char[] content = new char[50];
+        ln.read( content );
+        assertTrue( new String( content ).startsWith( "test.test.test" ) );
+
+        tmpFile = Files.createTempFile( "archiva-mvn-repotest", "jar" );
+        try( OutputStream outputStream = Files.newOutputStream( tmpFile ))
+        {
+            for ( int i = 0; i < 255; i++ )
+            {
+                outputStream.write( "test.test.test\n".getBytes( Charset.forName( "UTF-8" ) ) );
+            }
+        }
+        file = repoRoot.resolve( "org/apache/maven/samplejar/2.0/samplejar-2.0.test" );
+        assertFalse( Files.exists( file ) );
+        assertTrue( Files.exists( tmpFile ) );
+        selector = ArchivaItemSelector.builder( )
+            .withNamespace( "org.apache.maven" )
+            .withProjectId( "samplejar" )
+            .withVersion( "2.0" )
+            .withArtifactId( "samplejar" )
+            .withArtifactVersion( "2.0" )
+            .withExtension( "test" )
+            .build( );
+        artifact = myRepoContent.getArtifact( selector );
+        myRepoContent.addArtifact( tmpFile, artifact );
+        ln = Files.newBufferedReader( file, Charset.forName( "UTF-8" ) );
+        ln.read( content );
+        assertTrue( new String( content ).startsWith( "test.test.test" ) );
+    }
+
 }