]> source.dussan.org Git - archiva.git/commitdiff
MRM-859 - Use File.createTempFile() when downloading files from a remote repository
authorJames William Dumay <jdumay@apache.org>
Thu, 3 Jul 2008 14:16:56 +0000 (14:16 +0000)
committerJames William Dumay <jdumay@apache.org>
Thu, 3 Jul 2008 14:16:56 +0000 (14:16 +0000)
git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@673685 13f79535-47bb-0310-9956-ffa450edef68

archiva-modules/archiva-base/archiva-proxy/src/main/java/org/apache/maven/archiva/proxy/DefaultRepositoryProxyConnectors.java
archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/maven/archiva/proxy/AbstractProxyTestCase.java
archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/maven/archiva/proxy/CacheFailuresTransferTest.java
archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/maven/archiva/proxy/ChecksumTransferTest.java
archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/maven/archiva/proxy/ErrorHandlingTest.java
archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/maven/archiva/proxy/ManagedDefaultTransferTest.java
archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/maven/archiva/proxy/MetadataTransferTest.java

index 36cccd26fc39ba2a73d1c9b487da7371920b001c..398734973a0407fc029428f810d5eef46cf71fc2 100644 (file)
@@ -691,8 +691,9 @@ public class DefaultRepositoryProxyConnectors
 
         try
         {
-            temp = new File( localFile.getAbsolutePath() + ".tmp" );
-
+            localFile.getParentFile().mkdirs();
+            temp = File.createTempFile(localFile.getName() + ".", null, localFile.getParentFile());
+            
             boolean success = false;
 
             if ( !localFile.exists() )
@@ -729,6 +730,10 @@ public class DefaultRepositoryProxyConnectors
 
             return localFile;
         }
+        catch (IOException e)
+        {
+            throw new ProxyException("Could not create temporary file at " + localFile.getAbsolutePath(), e);
+        }
         catch ( ResourceDoesNotExistException e )
         {
             throw new NotFoundException(
@@ -743,10 +748,7 @@ public class DefaultRepositoryProxyConnectors
         }
         finally
         {
-            if ( temp != null )
-            {
-                temp.delete();
-            }
+            FileUtils.deleteQuietly(temp);
         }
     }
 
@@ -857,11 +859,18 @@ public class DefaultRepositoryProxyConnectors
             }
             catch ( IOException e )
             {
-                throw new ProxyException( "Cannot copy tmp file to its final location", e );
+                if (target.exists())
+                {
+                    log.debug("Tried to copy file " + temp.getName() + " to " + target.getAbsolutePath() + " but file with this name already exists.");
+                }
+                else
+                {
+                    throw new ProxyException( "Cannot copy tmp file " + temp.getAbsolutePath() + " to its final location", e );
+                }
             }
             finally
             {
-                temp.delete();
+                FileUtils.deleteQuietly(temp);
             }
         }
     }
index 1eb5651e99c0c8fc9f567efa1857dfb9945ea2b5..db4a91dc27450860e5102befd4298711ba8ed77f 100644 (file)
@@ -31,6 +31,7 @@ import java.util.Date;
 import java.util.Locale;
 
 import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang.ArrayUtils;
 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
@@ -43,10 +44,9 @@ import org.apache.maven.archiva.policies.ReleasesPolicy;
 import org.apache.maven.archiva.policies.SnapshotsPolicy;
 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
 import org.apache.maven.wagon.Wagon;
-import org.codehaus.plexus.spring.PlexusClassPathXmlApplicationContext;
 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
+import org.easymock.ArgumentsMatcher;
 import org.easymock.MockControl;
-import org.springframework.beans.factory.BeanFactory;
 
 /**
  * AbstractProxyTestCase
@@ -88,6 +88,53 @@ public abstract class AbstractProxyTestCase
     protected static final String REPOPATH_LEGACY_MANAGED = "src/test/repositories/legacy-managed";
 
     protected static final String REPOPATH_LEGACY_MANAGED_TARGET = "target/test-repository/legacy-managed";
+    
+    protected static final ArgumentsMatcher customWagonGetIfNewerMatcher = new ArgumentsMatcher() {
+
+        public boolean matches(Object[] expected, Object[] actual) {
+            if (expected.length < 1 || actual.length < 1)
+            {
+                return false;
+            }
+            return MockControl.ARRAY_MATCHER.matches(ArrayUtils.remove(expected, 1), ArrayUtils.remove(actual, 1));
+        }
+
+        public String toString(Object[] arguments) {
+            return ArrayUtils.toString(arguments);
+        }
+    };
+    
+    protected static final ArgumentsMatcher customWagonGetMatcher = new ArgumentsMatcher() {
+
+            public boolean matches(Object[] expected, Object[] actual) 
+            {
+                if (expected.length == 2 && actual.length == 2)
+                {
+                    if (expected[0] == null && actual[0] == null)
+                    {
+                        return true;
+                    }
+                    
+                    if (expected[0] == null)
+                    {
+                        return actual[0] == null;
+                    }
+                    
+                    if (actual[0] == null)
+                    {
+                        return expected[0] == null;
+                    }
+                    
+                    return expected[0].equals(actual[0]);
+                }
+                return false;
+            }
+
+            public String toString(Object[] arguments) 
+            {
+                return ArrayUtils.toString(arguments);
+            }
+        };
 
     protected MockControl wagonMockControl;
 
index f2796727ba5f0ac1683c9513da4039c0e2bf4b47..70aec32fc0766ed88182319e74b93b6d5545b1b8 100644 (file)
@@ -63,8 +63,11 @@ public class CacheFailuresTransferTest
                        SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES );
         saveConnector( ID_DEFAULT_MANAGED, "badproxied2", ChecksumPolicy.FIX, ReleasesPolicy.ALWAYS,
                        SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.YES );
-
+        
         wagonMock.get( path, new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" ) );
+        
+        wagonMockControl.setMatcher(customWagonGetMatcher);
+        
         wagonMockControl.setThrowable( new ResourceDoesNotExistException( "resource does not exist." ), 2 );
 
         wagonMockControl.replay();
@@ -72,7 +75,7 @@ public class CacheFailuresTransferTest
         File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
 
         wagonMockControl.verify();
-
+        
                // Second attempt to download same artifact use cache
         wagonMockControl.reset();
         wagonMockControl.replay();
@@ -105,6 +108,8 @@ public class CacheFailuresTransferTest
                        SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO );
 
         wagonMock.get( path, new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" ) );
+        
+        wagonMockControl.setMatcher(customWagonGetMatcher);
         wagonMockControl.setThrowable( new ResourceDoesNotExistException( "resource does not exist." ), 2 );
 
         wagonMockControl.replay();
@@ -116,10 +121,12 @@ public class CacheFailuresTransferTest
                // Second attempt to download same artifact DOES NOT use cache
         wagonMockControl.reset();
         wagonMock.get( path, new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" ) );
+        
+        wagonMockControl.setMatcher(customWagonGetMatcher);
         wagonMockControl.setThrowable( new ResourceDoesNotExistException( "resource does not exist." ), 2 );
         wagonMockControl.replay();
 
-               downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
+       downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
 
         wagonMockControl.verify();
 
index e51447ec3ca8e1bcd79516d6dec874b25dfe8b17..4528145dc14eb5c3958d13fb324acb48c7a145f1 100644 (file)
@@ -397,10 +397,13 @@ public class ChecksumTransferTest
                        SnapshotsPolicy.ALWAYS, CachedFailuresPolicy.NO );
 
         wagonMock.get( path, new File( expectedFile.getAbsolutePath() + ".tmp" ) );
+        wagonMockControl.setMatcher(customWagonGetMatcher);
         wagonMockControl.setVoidCallable();
         wagonMock.get( path + ".sha1", new File( expectedFile.getAbsolutePath() + ".sha1.tmp" ) );
+        wagonMockControl.setMatcher(customWagonGetMatcher);
         wagonMockControl.setVoidCallable();
         wagonMock.get( path + ".md5", new File( expectedFile.getAbsolutePath() + ".md5.tmp" ) );
+        wagonMockControl.setMatcher(customWagonGetMatcher);
         wagonMockControl.setThrowable( new ResourceDoesNotExistException( "Resource does not exist." ) );
         wagonMockControl.replay();
 
index 0e62f8273b329262cefdc59e65eabfb4e1fd332d..c0c60a720b1d00fcf8cb06dc1ab9052ef330e66a 100644 (file)
@@ -537,6 +537,7 @@ public class ErrorHandlingTest
         throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
     {
         wagonMock.get( path, createExpectedTempFile( expectedFile ) );
+        wagonMockControl.setMatcher(customWagonGetMatcher);
         wagonMockControl.setThrowable( throwable, 1 );
     }
 
@@ -544,6 +545,7 @@ public class ErrorHandlingTest
         throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
     {
         wagonMock.getIfNewer( path, createExpectedTempFile( expectedFile ), expectedFile.lastModified() );
+        wagonMockControl.setMatcher(customWagonGetIfNewerMatcher);
         wagonMockControl.setThrowable( exception, 1 );
     }
 
index 8e920f700606a246aa24cb242a8aad75b7f50baf..9819f7cc0e3288f755a94af835642c6b497983f9 100644 (file)
@@ -306,6 +306,7 @@ public class ManagedDefaultTransferTest
         saveRemoteRepositoryConfig( "badproxied", "Bad Proxied", "test://bad.machine.com/repo/", "default" );
 
         wagonMock.get( path, new File( expectedFile.getAbsolutePath() + ".tmp" ) );
+        wagonMockControl.setMatcher(customWagonGetMatcher);
         wagonMockControl.setThrowable( new ResourceDoesNotExistException( "transfer failed" ) );
         wagonMockControl.replay();
 
@@ -344,9 +345,13 @@ public class ManagedDefaultTransferTest
 
         File tmpFile = new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" );
         wagonMock.get( path, tmpFile );
+        
+        wagonMockControl.setMatcher(customWagonGetMatcher);
         wagonMockControl.setThrowable( new ResourceDoesNotExistException( "Can't find resource." ) );
 
         wagonMock.get( path, tmpFile );
+        
+        wagonMockControl.setMatcher(customWagonGetMatcher);
         wagonMockControl.setThrowable( new ResourceDoesNotExistException( "Can't find resource." ) );
 
         wagonMockControl.replay();
index 2496e3b68f14e81e0b66ea664cd6d57fea0b7c95..3cab9107b95ddca5a49dcde700e4836992952bca 100644 (file)
@@ -120,6 +120,9 @@ public class MetadataTransferTest
         File expectedFile = new File( managedDefaultDir.getAbsoluteFile(),
                                       metadataTools.getRepositorySpecificName( "badproxied1", requestedResource ) );
         wagonMock.get( requestedResource, new File( expectedFile.getParentFile(), expectedFile.getName() + ".tmp" ) );
+        
+        wagonMockControl.setMatcher(customWagonGetMatcher);
+        
         wagonMockControl.setThrowable( new TransferFailedException( "can't connect" ) );
 
         wagonMockControl.replay();