From 69ee104b5539f5c214e2d69ee9fa27f99fc40128 Mon Sep 17 00:00:00 2001 From: "Edwin L. Punzalan" Date: Thu, 9 Feb 2006 03:19:34 +0000 Subject: [PATCH] MRM-43 More junit coverage git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@376168 13f79535-47bb-0310-9956-ffa450edef68 --- .../repository/proxy/DefaultProxyManager.java | 72 ++++++++++++------- .../proxy/DefaultProxyManagerTest.java | 45 +++++++++++- .../checksumed-sha1/repository/file.txt.sha1 | 2 +- .../1.0/commons-logging-1.0.jar.md5 | 1 + 4 files changed, 90 insertions(+), 30 deletions(-) create mode 100644 maven-repository-proxy/src/test/remote-repo1/commons-logging/commons-logging/1.0/commons-logging-1.0.jar.md5 diff --git a/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/DefaultProxyManager.java b/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/DefaultProxyManager.java index a83399612..0d226f3eb 100644 --- a/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/DefaultProxyManager.java +++ b/maven-repository-proxy/src/main/java/org/apache/maven/repository/proxy/DefaultProxyManager.java @@ -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 * diff --git a/maven-repository-proxy/src/test/java/org/apache/maven/repository/proxy/DefaultProxyManagerTest.java b/maven-repository-proxy/src/test/java/org/apache/maven/repository/proxy/DefaultProxyManagerTest.java index 6512e7383..d9faab189 100644 --- a/maven-repository-proxy/src/test/java/org/apache/maven/repository/proxy/DefaultProxyManagerTest.java +++ b/maven-repository-proxy/src/test/java/org/apache/maven/repository/proxy/DefaultProxyManagerTest.java @@ -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() ) ); 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 index 402476b0d..d12e1a485 100644 --- 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 @@ -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 index 000000000..7c997d231 --- /dev/null +++ b/maven-repository-proxy/src/test/remote-repo1/commons-logging/commons-logging/1.0/commons-logging-1.0.jar.md5 @@ -0,0 +1 @@ +240b26992977c9ad119efb91cb21f8f8 \ No newline at end of file -- 2.39.5