]> source.dussan.org Git - archiva.git/commitdiff
PR: MRM-43
authorEdwin L. Punzalan <epunzalan@apache.org>
Thu, 9 Feb 2006 02:26:30 +0000 (02:26 +0000)
committerEdwin L. Punzalan <epunzalan@apache.org>
Thu, 9 Feb 2006 02:26:30 +0000 (02:26 +0000)
added more unit test coverage

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

maven-repository-proxy/pom.xml
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-md5/repository/file.txt [new file with mode: 0644]
maven-repository-proxy/src/test/remote-repo1/checksumed-md5/repository/file.txt.md5 [new file with mode: 0644]
maven-repository-proxy/src/test/remote-repo1/checksumed-sha1/repository/file.txt [new file with mode: 0644]
maven-repository-proxy/src/test/remote-repo1/checksumed-sha1/repository/file.txt.sha1 [new file with mode: 0644]
maven-repository-proxy/src/test/remote-repo1/commons-logging/commons-logging/1.0/commons-logging-1.0.jar [new file with mode: 0644]
maven-repository-proxy/src/test/remote-repo1/commons-logging/commons-logging/1.0/commons-logging-1.0.pom [new file with mode: 0644]
maven-repository-proxy/src/test/remote-repo1/not-standard/repository/file.txt [new file with mode: 0644]

index a0b22c33a1387c3a82fe94f692fb81696d522d37..294fcabdb07427cb21460ae57db824837bfb16fa 100644 (file)
       <groupId>org.apache.maven</groupId>\r
       <artifactId>maven-artifact-manager</artifactId>\r
     </dependency>\r
+    <!-- below should be in extensions -->\r
+    <dependency>\r
+      <groupId>org.apache.maven.wagon</groupId>\r
+      <artifactId>wagon-file</artifactId>\r
+      <version>1.0-alpha-7-SNAPSHOT</version>\r
+      <scope>test</scope>\r
+    </dependency>\r
   </dependencies>\r
 </project>\r
index e4836fea8bdf92a7d95a8137dff8eac058f04202..a8339961284e9ef6db5902c078eab598770d1d15 100644 (file)
@@ -22,8 +22,6 @@ import org.apache.maven.artifact.manager.ChecksumFailedException;
 import org.apache.maven.artifact.manager.WagonManager;
 import org.apache.maven.artifact.repository.ArtifactRepository;
 import org.apache.maven.repository.ArtifactUtils;
-import org.apache.maven.repository.digest.DefaultDigester;
-import org.apache.maven.repository.digest.Digester;
 import org.apache.maven.repository.proxy.configuration.ProxyConfiguration;
 import org.apache.maven.repository.proxy.repository.ProxyRepository;
 import org.apache.maven.wagon.ConnectionException;
@@ -35,9 +33,13 @@ 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 java.io.File;
+import java.io.FileInputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.security.NoSuchAlgorithmException;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -134,6 +136,7 @@ public class DefaultProxyManager
         ArtifactRepository repoCache = config.getRepositoryCache();
 
         File artifactFile = new File( repoCache.getBasedir(), repoCache.pathOf( artifact ) );
+        artifact.setFile( artifactFile );
 
         if ( !artifactFile.exists() )
         {
@@ -207,7 +210,6 @@ public class DefaultProxyManager
                 if ( connected )
                 {
                     File temp = new File( target.getAbsolutePath() + ".tmp" );
-                    temp.deleteOnExit();
 
                     int tries = 0;
                     boolean success = false;
@@ -216,11 +218,12 @@ public class DefaultProxyManager
                     {
                         tries++;
 
+                        getLogger().info( "trying " + path + " from " + repository.getId() );
+
                         wagon.get( path, temp );
 
                         if ( useChecksum )
                         {
-                            releaseChecksums( wagon, checksums );
                             success = doChecksumCheck( checksums, path, wagon );
                         }
                         else
@@ -235,6 +238,24 @@ 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();
+                        }
+                    }
+
                     return target;
                 }
                 //try next repository
@@ -253,8 +274,8 @@ public class DefaultProxyManager
             }
             catch ( UnsupportedProtocolException e )
             {
-                getLogger().info( "Skipping repository " + repository.getUrl() + ": no wagonManager configured for protocol " +
-                    repository.getProtocol() );
+                getLogger().info( "Skipping repository " + repository.getUrl() + ": no wagonManager configured " +
+                    "for protocol " + repository.getProtocol() );
             }
             finally
             {
@@ -351,45 +372,26 @@ public class DefaultProxyManager
      */
     private boolean doChecksumCheck( Map checksumMap, String path, Wagon wagon )
     {
+        releaseChecksums( wagon, checksumMap );
         for ( Iterator checksums = checksumMap.keySet().iterator(); checksums.hasNext(); )
         {
             String checksumExt = (String) checksums.next();
             ChecksumObserver checksum = (ChecksumObserver) checksumMap.get( checksumExt );
-            String remotePath = path + "." + checksumExt;
-            File checksumFile = new File( config.getRepositoryCache().getBasedir(), remotePath );
+            String checksumPath = path + "." + checksumExt;
+            File checksumFile = new File( config.getRepositoryCache().getBasedir(), checksumPath );
 
             try
             {
                 File tempChecksumFile = new File( checksumFile.getAbsolutePath() + "." + checksumExt );
 
-                wagon.get( remotePath + "." + checksumExt, tempChecksumFile );
+                wagon.get( checksumPath, tempChecksumFile );
 
-                String algorithm;
-                if ( "md5".equals( checksumExt ) )
-                {
-                    algorithm = "MD5";
-                }
-                else
-                {
-                    algorithm = "SHA-1";
-                }
-
-                Digester digester = new DefaultDigester();
-                try
-                {
-                    return digester.verifyChecksum( tempChecksumFile, checksum.getActualChecksum(), algorithm );
-                }
-                catch ( NoSuchAlgorithmException e )
-                {
-                    getLogger().info( "Failed to initialize checksum: " + algorithm + "\n  " + e.getMessage() );
-                    return false;
-                }
-                catch ( IOException e )
+                String remoteChecksum = readTextFile( tempChecksumFile ).trim();
+                if ( remoteChecksum.indexOf( ' ' ) > 0 )
                 {
-                    getLogger().info( "Failed to verify checksum: " + algorithm + "\n  " + e.getMessage() );
-                    return false;
+                    remoteChecksum = remoteChecksum.substring( 0, remoteChecksum.indexOf( ' ' ) );
                 }
-
+                return remoteChecksum.toUpperCase().equals( checksum.getActualChecksum().toUpperCase() );
             }
             catch ( ChecksumFailedException e )
             {
@@ -397,19 +399,24 @@ public class DefaultProxyManager
             }
             catch ( TransferFailedException e )
             {
-                getLogger().warn( "An error occurred during the download of " + remotePath + ": " + e.getMessage() );
+                getLogger().warn( "An error occurred during the download of " + checksumPath + ": " + e.getMessage() );
                 // do nothing try the next checksum
             }
             catch ( ResourceDoesNotExistException e )
             {
-                getLogger().warn( "An error occurred during the download of " + remotePath + ": " + e.getMessage() );
+                getLogger().warn( "An error occurred during the download of " + checksumPath + ": " + e.getMessage() );
                 // do nothing try the next checksum
             }
             catch ( AuthorizationException e )
             {
-                getLogger().warn( "An error occurred during the download of " + remotePath + ": " + e.getMessage() );
+                getLogger().warn( "An error occurred during the download of " + checksumPath + ": " + e.getMessage() );
                 // do nothing try the next checksum
             }
+            catch ( IOException e )
+            {
+                getLogger().info( "An error occurred while reading the temporary checksum file." );
+                return false;
+            }
         }
 
         getLogger().info( "Skipping checksum validation for " + path + ": No remote checksums available." );
@@ -426,6 +433,34 @@ public class DefaultProxyManager
         }
     }
 
+    private String readTextFile( File file )
+        throws IOException
+    {
+        String text = "";
+
+        InputStream fis = new FileInputStream( file );
+        try
+        {
+            byte[] buffer = new byte[ 64 ];
+            int numRead;
+            do
+            {
+                numRead = fis.read( buffer );
+                if ( numRead > 0 )
+                {
+                    text += new String( buffer );
+                }
+            }
+            while ( numRead != -1 );
+        }
+        finally
+        {
+            IOUtil.close( fis );
+        }
+
+        return text;
+    }
+
     /**
      * Used to disconnect the wagonManager from its repository
      *
index af80b7dce78792721325763a75916f31589facfe..6512e7383c57c5d4f34cd4626b38e8101f5f1122 100644 (file)
@@ -3,7 +3,12 @@ package org.apache.maven.repository.proxy;
 import org.codehaus.plexus.PlexusTestCase;
 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
 import org.apache.maven.repository.proxy.configuration.ProxyConfiguration;
+import org.apache.maven.repository.proxy.repository.ProxyRepository;
 import org.apache.maven.wagon.ResourceDoesNotExistException;
+import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
+import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
+
+import java.io.File;
 
 /*
  * Copyright 2005-2006 The Apache Software Foundation.
@@ -55,25 +60,27 @@ public class DefaultProxyManagerTest
         {
             fail( "Expected Exception not thrown." );
         }
-
-        try
-        {
-            proxy.getRemoteFile( "/invalid" );
-            fail( "Expected empty configuration error." );
-        }
-        catch ( ProxyException e )
-        {
-            assertEquals( "Expected Exception not thrown.", "No proxy configuration defined.", e.getMessage() );
-        }
-        catch ( ResourceDoesNotExistException e )
-        {
-            fail( "Expected Exception not thrown." );
-        }
     }
 
     public void testCache()
+        throws Exception
     {
-        
+        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() ) );
+
+        file = proxy.get( "/commons-logging/commons-logging/1.0/commons-logging-1.0.jar" );
+
+        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() ) );
+
+        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() ) );
     }
 
     protected void tearDown()
@@ -91,6 +98,14 @@ public class DefaultProxyManagerTest
 
         config.setRepositoryCachePath( "target/proxy-cache" );
 
+        ArtifactRepositoryLayout defLayout = new DefaultRepositoryLayout();
+
+        File repo1File = new File( "src/test/remote-repo1" );
+
+        ProxyRepository repo1 = new ProxyRepository( "test-repo", "file://" + repo1File.getAbsolutePath(), defLayout );
+
+        config.addRepository( repo1 );
+
         return config;
     }
 }
diff --git a/maven-repository-proxy/src/test/remote-repo1/checksumed-md5/repository/file.txt b/maven-repository-proxy/src/test/remote-repo1/checksumed-md5/repository/file.txt
new file mode 100644 (file)
index 0000000..08657d7
--- /dev/null
@@ -0,0 +1 @@
+test file only
\ No newline at end of file
diff --git a/maven-repository-proxy/src/test/remote-repo1/checksumed-md5/repository/file.txt.md5 b/maven-repository-proxy/src/test/remote-repo1/checksumed-md5/repository/file.txt.md5
new file mode 100644 (file)
index 0000000..3839dd6
--- /dev/null
@@ -0,0 +1 @@
+a473f827aa9d5df4e84c802e054c50f7
\ No newline at end of file
diff --git a/maven-repository-proxy/src/test/remote-repo1/checksumed-sha1/repository/file.txt b/maven-repository-proxy/src/test/remote-repo1/checksumed-sha1/repository/file.txt
new file mode 100644 (file)
index 0000000..08657d7
--- /dev/null
@@ -0,0 +1 @@
+test file only
\ No newline at end of file
diff --git a/maven-repository-proxy/src/test/remote-repo1/checksumed-sha1/repository/file.txt.sha1 b/maven-repository-proxy/src/test/remote-repo1/checksumed-sha1/repository/file.txt.sha1
new file mode 100644 (file)
index 0000000..402476b
--- /dev/null
@@ -0,0 +1 @@
+ABCDE
\ 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 b/maven-repository-proxy/src/test/remote-repo1/commons-logging/commons-logging/1.0/commons-logging-1.0.jar
new file mode 100644 (file)
index 0000000..33232cd
Binary files /dev/null and b/maven-repository-proxy/src/test/remote-repo1/commons-logging/commons-logging/1.0/commons-logging-1.0.jar differ
diff --git a/maven-repository-proxy/src/test/remote-repo1/commons-logging/commons-logging/1.0/commons-logging-1.0.pom b/maven-repository-proxy/src/test/remote-repo1/commons-logging/commons-logging/1.0/commons-logging-1.0.pom
new file mode 100644 (file)
index 0000000..402a9df
--- /dev/null
@@ -0,0 +1,6 @@
+<project>
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>commons-logging</groupId>
+  <artifactId>commons-logging</artifactId>
+  <version>1.0</version>
+</project>
diff --git a/maven-repository-proxy/src/test/remote-repo1/not-standard/repository/file.txt b/maven-repository-proxy/src/test/remote-repo1/not-standard/repository/file.txt
new file mode 100644 (file)
index 0000000..08657d7
--- /dev/null
@@ -0,0 +1 @@
+test file only
\ No newline at end of file