From 21e9e87af495bc170c923320dea08c8eff4c7aee Mon Sep 17 00:00:00 2001 From: Ben Hale Date: Thu, 8 Dec 2011 17:46:29 +0000 Subject: [PATCH] Change thrown exception types to conform to Wagon Changed the exception types thrown from some of the S3 interactions to better conform to the expected Wagon behavior. This was done to work around the fact that even though the Wagon API has a method to determine if a file exists, the `deploy` operation does not call it before trying to get a file. In the case that you are deploying a project to a repository for the first time, many files won't already exist causing issues. The `SimpleStorageServiceWagon` now throws `ResourceDoesNotExistException`s where it makes sense. Currently the wagon probably throws the wrong type of exceptions in some cases as its exception handling is very broad in some cases. This is a subject for future improvement. --- .../aws/maven/SimpleStorageServiceWagon.java | 14 ++++---- .../resources/META-INF/plexus/components.xml | 2 +- src/main/resources/logback.xml | 4 +-- ...leStorageServiceWagonIntegrationTests.java | 33 ++++++++----------- 4 files changed, 24 insertions(+), 29 deletions(-) diff --git a/src/main/java/org/springframework/build/aws/maven/SimpleStorageServiceWagon.java b/src/main/java/org/springframework/build/aws/maven/SimpleStorageServiceWagon.java index fb1695bb..724857b5 100644 --- a/src/main/java/org/springframework/build/aws/maven/SimpleStorageServiceWagon.java +++ b/src/main/java/org/springframework/build/aws/maven/SimpleStorageServiceWagon.java @@ -28,6 +28,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.apache.maven.wagon.ResourceDoesNotExistException; import org.apache.maven.wagon.TransferFailedException; import org.apache.maven.wagon.authentication.AuthenticationException; import org.apache.maven.wagon.authentication.AuthenticationInfo; @@ -113,17 +114,17 @@ protected boolean doesRemoteResourceExist(String resourceName) { } @Override - protected boolean isRemoteResourceNewer(String resourceName, long timestamp) throws TransferFailedException { + protected boolean isRemoteResourceNewer(String resourceName, long timestamp) throws ResourceDoesNotExistException { try { Date lastModified = getObjectMetadata(resourceName).getLastModified(); return lastModified == null ? true : lastModified.getTime() > timestamp; } catch (AmazonServiceException e) { - throw new TransferFailedException(String.format("'%s' does not exist", resourceName), e); + throw new ResourceDoesNotExistException(String.format("'%s' does not exist", resourceName), e); } } @Override - protected List listDirectory(String directory) throws TransferFailedException { + protected List listDirectory(String directory) throws ResourceDoesNotExistException { List directoryContents = new ArrayList(); try { @@ -147,12 +148,13 @@ protected List listDirectory(String directory) throws TransferFailedExce return directoryContents; } catch (AmazonServiceException e) { - throw new TransferFailedException(String.format("'%s' does not exist", directory), e); + throw new ResourceDoesNotExistException(String.format("'%s' does not exist", directory), e); } } @Override - protected void getResource(String resourceName, File destination, TransferProgress transferProgress) throws TransferFailedException { + protected void getResource(String resourceName, File destination, TransferProgress transferProgress) throws TransferFailedException, + ResourceDoesNotExistException { InputStream in = null; OutputStream out = null; try { @@ -163,7 +165,7 @@ protected void getResource(String resourceName, File destination, TransferProgre IoUtils.copy(in, out); } catch (AmazonServiceException e) { - throw new TransferFailedException(String.format("'%s' does not exist", resourceName), e); + throw new ResourceDoesNotExistException(String.format("'%s' does not exist", resourceName), e); } catch (FileNotFoundException e) { throw new TransferFailedException(String.format("Cannot write file to '%s'", destination), e); } catch (IOException e) { diff --git a/src/main/resources/META-INF/plexus/components.xml b/src/main/resources/META-INF/plexus/components.xml index b78c6b53..f4fe5784 100644 --- a/src/main/resources/META-INF/plexus/components.xml +++ b/src/main/resources/META-INF/plexus/components.xml @@ -2,7 +2,7 @@ org.apache.maven.wagon.Wagon - s3-aws + s3 org.springframework.build.aws.maven.SimpleStorageServiceWagon per-lookup diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml index 9c06e890..56278af6 100644 --- a/src/main/resources/logback.xml +++ b/src/main/resources/logback.xml @@ -5,8 +5,8 @@ %-5level %msg%n - - + + diff --git a/src/test/java/org/springframework/build/aws/maven/SimpleStorageServiceWagonIntegrationTests.java b/src/test/java/org/springframework/build/aws/maven/SimpleStorageServiceWagonIntegrationTests.java index 623fbf18..bbff53b9 100644 --- a/src/test/java/org/springframework/build/aws/maven/SimpleStorageServiceWagonIntegrationTests.java +++ b/src/test/java/org/springframework/build/aws/maven/SimpleStorageServiceWagonIntegrationTests.java @@ -34,6 +34,7 @@ import java.util.Date; import java.util.List; +import org.apache.maven.wagon.ResourceDoesNotExistException; import org.apache.maven.wagon.TransferFailedException; import org.apache.maven.wagon.authentication.AuthenticationException; import org.apache.maven.wagon.repository.Repository; @@ -97,7 +98,7 @@ public void doesRemoteResourceExistDoesNotExist() { } @Test - public void isRemoteResourceNewerNewer() throws TransferFailedException { + public void isRemoteResourceNewerNewer() throws ResourceDoesNotExistException { when(this.objectMetadata.getLastModified()).thenReturn(new Date()); when(this.amazonS3.getObjectMetadata(SimpleStorageServiceWagonIntegrationTests.BUCKET_NAME, BASE_DIRECTORY + FILE_NAME)).thenReturn( this.objectMetadata); @@ -106,7 +107,7 @@ public void isRemoteResourceNewerNewer() throws TransferFailedException { } @Test - public void isRemoteResourceNewerOlder() throws TransferFailedException { + public void isRemoteResourceNewerOlder() throws ResourceDoesNotExistException { when(this.objectMetadata.getLastModified()).thenReturn(new Date()); when(this.amazonS3.getObjectMetadata(SimpleStorageServiceWagonIntegrationTests.BUCKET_NAME, BASE_DIRECTORY + FILE_NAME)).thenReturn( this.objectMetadata); @@ -115,22 +116,22 @@ public void isRemoteResourceNewerOlder() throws TransferFailedException { } @Test - public void isRemoteResourceNewerNoLastModified() throws TransferFailedException { + public void isRemoteResourceNewerNoLastModified() throws ResourceDoesNotExistException { when(this.amazonS3.getObjectMetadata(SimpleStorageServiceWagonIntegrationTests.BUCKET_NAME, BASE_DIRECTORY + FILE_NAME)).thenReturn( this.objectMetadata); assertTrue(this.wagon.isRemoteResourceNewer(FILE_NAME, 0)); } - @Test(expected = TransferFailedException.class) - public void isRemoteResourceNewerDoesNotExist() throws TransferFailedException { + @Test(expected = ResourceDoesNotExistException.class) + public void isRemoteResourceNewerDoesNotExist() throws ResourceDoesNotExistException { when(this.amazonS3.getObjectMetadata(SimpleStorageServiceWagonIntegrationTests.BUCKET_NAME, BASE_DIRECTORY + FILE_NAME)).thenThrow( new AmazonServiceException("")); this.wagon.isRemoteResourceNewer(FILE_NAME, 0); } @Test - public void listDirectoryTopLevel() throws TransferFailedException { + public void listDirectoryTopLevel() throws ResourceDoesNotExistException { ListObjectsRequest listObjectsRequest = new ListObjectsRequest() // .withBucketName(BUCKET_NAME) // .withPrefix(BASE_DIRECTORY) // @@ -148,7 +149,7 @@ public void listDirectoryTopLevel() throws TransferFailedException { } @Test - public void listDirectoryTopNested() throws TransferFailedException { + public void listDirectoryTopNested() throws ResourceDoesNotExistException { ListObjectsRequest listObjectsRequest = new ListObjectsRequest() // .withBucketName(BUCKET_NAME) // .withPrefix(BASE_DIRECTORY + "release/") // @@ -165,8 +166,8 @@ public void listDirectoryTopNested() throws TransferFailedException { assertFalse(directoryContents.contains("frogs.txt")); } - @Test(expected = TransferFailedException.class) - public void listDirectoryDoesNotExist() throws TransferFailedException { + @Test(expected = ResourceDoesNotExistException.class) + public void listDirectoryDoesNotExist() throws ResourceDoesNotExistException { ListObjectsRequest listObjectsRequest = new ListObjectsRequest() // .withBucketName(BUCKET_NAME) // .withPrefix(BASE_DIRECTORY + "frogs") // @@ -177,7 +178,7 @@ public void listDirectoryDoesNotExist() throws TransferFailedException { } @Test - public void getResource() throws TransferFailedException, FileNotFoundException { + public void getResource() throws TransferFailedException, FileNotFoundException, ResourceDoesNotExistException { when(this.amazonS3.getObject(SimpleStorageServiceWagonIntegrationTests.BUCKET_NAME, BASE_DIRECTORY + FILE_NAME)).thenReturn(this.s3Object); when(this.s3Object.getObjectContent()).thenReturn(new FileInputStream("src/test/resources/test.txt")); @@ -190,22 +191,14 @@ public void getResource() throws TransferFailedException, FileNotFoundException assertTrue(target.exists()); } - @Test(expected = TransferFailedException.class) - public void getResourceSourceDoesNotExist() throws TransferFailedException { + @Test(expected = ResourceDoesNotExistException.class) + public void getResourceSourceDoesNotExist() throws TransferFailedException, ResourceDoesNotExistException { when(this.amazonS3.getObject(SimpleStorageServiceWagonIntegrationTests.BUCKET_NAME, BASE_DIRECTORY + FILE_NAME)).thenThrow( new AmazonServiceException("")); File target = new File("target/robots.txt"); this.wagon.getResource(FILE_NAME, target, this.transferProgress); } - @Test(expected = TransferFailedException.class) - public void getResourceTargetDoesNotExist() throws TransferFailedException { - when(this.amazonS3.getObject(SimpleStorageServiceWagonIntegrationTests.BUCKET_NAME, BASE_DIRECTORY + FILE_NAME)).thenThrow( - new AmazonServiceException("")); - File target = new File("target/test/robots.txt"); - this.wagon.getResource(FILE_NAME, target, this.transferProgress); - } - @Test public void putResource() throws TransferFailedException { File file = new File("src/test/resources/test.txt");