]> source.dussan.org Git - archiva.git/commitdiff
MRM-43
authorEdwin L. Punzalan <epunzalan@apache.org>
Thu, 9 Feb 2006 03:19:34 +0000 (03:19 +0000)
committerEdwin L. Punzalan <epunzalan@apache.org>
Thu, 9 Feb 2006 03:19:34 +0000 (03:19 +0000)
More junit coverage

git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@376168 13f79535-47bb-0310-9956-ffa450edef68

maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/DefaultProxyManager.java
maven-repository-proxy/src/test/java/org/apache/maven/repository/proxy/DefaultProxyManagerTest.java
maven-repository-proxy/src/test/remote-repo1/checksumed-sha1/repository/file.txt.sha1
maven-repository-proxy/src/test/remote-repo1/commons-logging/commons-logging/1.0/commons-logging-1.0.jar.md5 [new file with mode: 0644]

index a8339961284e9ef6db5902c078eab598770d1d15..0d226f3eb5b4073b21783ae5052ac9162d21e568 100644 (file)
@@ -33,8 +33,8 @@ import org.apache.maven.wagon.authentication.AuthenticationException;
 import org.apache.maven.wagon.authorization.AuthorizationException;
 import org.apache.maven.wagon.observers.ChecksumObserver;
 import org.codehaus.plexus.logging.AbstractLogEnabled;
-import org.codehaus.plexus.util.FileUtils;
 import org.codehaus.plexus.util.IOUtil;
+import org.codehaus.plexus.util.FileUtils;
 
 import java.io.File;
 import java.io.FileInputStream;
@@ -218,7 +218,7 @@ public class DefaultProxyManager
                     {
                         tries++;
 
-                        getLogger().info( "trying " + path + " from " + repository.getId() );
+                        getLogger().info( "Trying " + path + " from " + repository.getId() + "...");
 
                         wagon.get( path, temp );
 
@@ -238,23 +238,7 @@ public class DefaultProxyManager
                     }
                     disconnectWagon( wagon );
 
-                    if ( !temp.renameTo( target ) )
-                    {
-                        getLogger().warn( "Unable to rename tmp file to its final name... resorting to copy command." );
-
-                        try
-                        {
-                            FileUtils.copyFile( temp, target );
-                        }
-                        catch ( IOException e )
-                        {
-                            throw new ProxyException( "Cannot copy tmp file to its final location", e );
-                        }
-                        finally
-                        {
-                            temp.delete();
-                        }
-                    }
+                    copyTempToTarget( temp, target );
 
                     return target;
                 }
@@ -371,6 +355,7 @@ public class DefaultProxyManager
      * @return true when the checksum succeeds and false when the checksum failed.
      */
     private boolean doChecksumCheck( Map checksumMap, String path, Wagon wagon )
+        throws ProxyException
     {
         releaseChecksums( wagon, checksumMap );
         for ( Iterator checksums = checksumMap.keySet().iterator(); checksums.hasNext(); )
@@ -382,7 +367,7 @@ public class DefaultProxyManager
 
             try
             {
-                File tempChecksumFile = new File( checksumFile.getAbsolutePath() + "." + checksumExt );
+                File tempChecksumFile = new File( checksumFile.getAbsolutePath() + ".tmp" );
 
                 wagon.get( checksumPath, tempChecksumFile );
 
@@ -391,7 +376,15 @@ public class DefaultProxyManager
                 {
                     remoteChecksum = remoteChecksum.substring( 0, remoteChecksum.indexOf( ' ' ) );
                 }
-                return remoteChecksum.toUpperCase().equals( checksum.getActualChecksum().toUpperCase() );
+
+                boolean checksumCheck = false;
+                if ( remoteChecksum.toUpperCase().equals( checksum.getActualChecksum().toUpperCase() ) )
+                {
+                    copyTempToTarget( tempChecksumFile, checksumFile );
+
+                    checksumCheck = true;
+                }
+                return checksumCheck;
             }
             catch ( ChecksumFailedException e )
             {
@@ -399,27 +392,27 @@ public class DefaultProxyManager
             }
             catch ( TransferFailedException e )
             {
-                getLogger().warn( "An error occurred during the download of " + checksumPath + ": " + e.getMessage() );
+                getLogger().debug( "An error occurred during the download of " + checksumPath + ": " + e.getMessage(), e );
                 // do nothing try the next checksum
             }
             catch ( ResourceDoesNotExistException e )
             {
-                getLogger().warn( "An error occurred during the download of " + checksumPath + ": " + e.getMessage() );
+                getLogger().debug( "An error occurred during the download of " + checksumPath + ": " + e.getMessage(), e );
                 // do nothing try the next checksum
             }
             catch ( AuthorizationException e )
             {
-                getLogger().warn( "An error occurred during the download of " + checksumPath + ": " + e.getMessage() );
+                getLogger().debug( "An error occurred during the download of " + checksumPath + ": " + e.getMessage(), e );
                 // do nothing try the next checksum
             }
             catch ( IOException e )
             {
-                getLogger().info( "An error occurred while reading the temporary checksum file." );
+                getLogger().debug( "An error occurred while reading the temporary checksum file.", e );
                 return false;
             }
         }
 
-        getLogger().info( "Skipping checksum validation for " + path + ": No remote checksums available." );
+        getLogger().debug( "Skipping checksum validation for " + path + ": No remote checksums available." );
 
         return true;
     }
@@ -461,6 +454,33 @@ public class DefaultProxyManager
         return text;
     }
 
+    private void copyTempToTarget( File temp, File target )
+        throws ProxyException
+    {
+        if ( target.exists() && !target.delete() )
+        {
+            throw new ProxyException( "Unable to overwrite existing target file: " + target.getAbsolutePath() );
+        }
+
+        if ( !temp.renameTo( target ) )
+        {
+            getLogger().warn( "Unable to rename tmp file to its final name... resorting to copy command." );
+
+            try
+            {
+                FileUtils.copyFile( temp, target );
+            }
+            catch ( IOException e )
+            {
+                throw new ProxyException( "Cannot copy tmp file to its final location", e );
+            }
+            finally
+            {
+                temp.delete();
+            }
+        }
+    }
+
     /**
      * Used to disconnect the wagonManager from its repository
      *
index 6512e7383c57c5d4f34cd4626b38e8101f5f1122..d9faab189f7a069caf241dac3b624bcf9a08f2e7 100644 (file)
@@ -62,22 +62,61 @@ public class DefaultProxyManagerTest
         }
     }
 
-    public void testCache()
+    public void testArtifactDownload()
         throws Exception
     {
+        //test download
         File file = proxy.get( "/commons-logging/commons-logging/1.0/commons-logging-1.0.jar" );
         assertTrue( "File must be downloaded.", file.exists() );
         assertTrue( "Downloaded file should be present in the cache.",
                     file.getAbsolutePath().startsWith( proxy.getConfiguration().getRepositoryCachePath() ) );
 
+        //test cache
         file = proxy.get( "/commons-logging/commons-logging/1.0/commons-logging-1.0.jar" );
 
-        file = proxy.get( "/not-standard/repository/file.txt" );
+        try
+        {
+            file = proxy.get( "/commons-logging/commons-logging/2.0/commons-logging-2.0.jar" );
+            fail( "Expected ResourceDoesNotExistException exception not thrown" );
+        }
+        catch ( ResourceDoesNotExistException e )
+        {
+            assertTrue( true );
+        }
+    }
+
+    public void testArtifactChecksum()
+        throws Exception
+    {
+        //force the downlod from the remote repository, use getRemoteFile()
+        File file = proxy.getRemoteFile( "/commons-logging/commons-logging/1.0/commons-logging-1.0.jar.md5" );
         assertTrue( "File must be downloaded.", file.exists() );
         assertTrue( "Downloaded file should be present in the cache.",
                     file.getAbsolutePath().startsWith( proxy.getConfiguration().getRepositoryCachePath() ) );
+    }
 
-        file = proxy.get( "/checksumed-md5/repository/file.txt" );
+    public void testNonArtifactWithNoChecksum()
+        throws Exception
+    {
+        File file = proxy.get( "/not-standard/repository/file.txt" );
+        assertTrue( "File must be downloaded.", file.exists() );
+        assertTrue( "Downloaded file should be present in the cache.",
+                    file.getAbsolutePath().startsWith( proxy.getConfiguration().getRepositoryCachePath() ) );
+    }
+
+    public void testNonArtifactWithMD5Checksum()
+        throws Exception
+    {
+        File file = proxy.get( "/checksumed-md5/repository/file.txt" );
+        assertTrue( "File must be downloaded.", file.exists() );
+        assertTrue( "Downloaded file should be present in the cache.",
+                    file.getAbsolutePath().startsWith( proxy.getConfiguration().getRepositoryCachePath() ) );
+    }
+
+    public void testNonArtifactWithSHA1Checksum()
+        throws Exception
+    {
+        File file = proxy.get( "/checksumed-sha1/repository/file.txt" );
         assertTrue( "File must be downloaded.", file.exists() );
         assertTrue( "Downloaded file should be present in the cache.",
                     file.getAbsolutePath().startsWith( proxy.getConfiguration().getRepositoryCachePath() ) );
index 402476b0dcb52235a5b93decadf6a449a8ab6914..d12e1a48553ef383683e6393f0031cc931eb0036 100644 (file)
@@ -1 +1 @@
-ABCDE
\ No newline at end of file
+afb037c2bd96fe1ef1cfd220e82682d088d60d3e
\ No newline at end of file
diff --git a/maven-repository-proxy/src/test/remote-repo1/commons-logging/commons-logging/1.0/commons-logging-1.0.jar.md5 b/maven-repository-proxy/src/test/remote-repo1/commons-logging/commons-logging/1.0/commons-logging-1.0.jar.md5
new file mode 100644 (file)
index 0000000..7c997d2
--- /dev/null
@@ -0,0 +1 @@
+240b26992977c9ad119efb91cb21f8f8
\ No newline at end of file