MRM-859 - Use File.createTempFile() when downloading files from a remote repository

git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@673685 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
James William Dumay 2008-07-03 14:16:56 +00:00
parent dee53b0c55
commit d07d790c54
7 changed files with 89 additions and 13 deletions

View File

@ -691,7 +691,8 @@ public class DefaultRepositoryProxyConnectors
try
{
temp = new File( localFile.getAbsolutePath() + ".tmp" );
localFile.getParentFile().mkdirs();
temp = File.createTempFile(localFile.getName() + ".", null, localFile.getParentFile());
boolean success = false;
@ -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);
}
}
}

View 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
@ -89,6 +89,53 @@ public abstract class AbstractProxyTestCase
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;
protected Wagon wagonMock;

View File

@ -65,6 +65,9 @@ public class CacheFailuresTransferTest
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();
@ -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,6 +121,8 @@ 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();

View 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();

View 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 );
}

View 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();

View 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();