try
{
- temp = new File( localFile.getAbsolutePath() + ".tmp" );
-
+ localFile.getParentFile().mkdirs();
+ temp = File.createTempFile(localFile.getName() + ".", null, localFile.getParentFile());
+
boolean success = false;
if ( !localFile.exists() )
return localFile;
}
+ catch (IOException e)
+ {
+ throw new ProxyException("Could not create temporary file at " + localFile.getAbsolutePath(), e);
+ }
catch ( ResourceDoesNotExistException e )
{
throw new NotFoundException(
}
finally
{
- if ( temp != null )
- {
- temp.delete();
- }
+ FileUtils.deleteQuietly(temp);
}
}
}
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);
}
}
}
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;
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
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;
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();
File downloadedFile = proxyHandler.fetchFromProxies( managedDefaultRepository, artifact );
wagonMockControl.verify();
-
+
// Second attempt to download same artifact use cache
wagonMockControl.reset();
wagonMockControl.replay();
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();
// 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();
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();
throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
{
wagonMock.get( path, createExpectedTempFile( expectedFile ) );
+ wagonMockControl.setMatcher(customWagonGetMatcher);
wagonMockControl.setThrowable( throwable, 1 );
}
throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
{
wagonMock.getIfNewer( path, createExpectedTempFile( expectedFile ), expectedFile.lastModified() );
+ wagonMockControl.setMatcher(customWagonGetIfNewerMatcher);
wagonMockControl.setThrowable( exception, 1 );
}
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();
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();
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();