Permalink
Browse files

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.
  • Loading branch information...
1 parent 3613725 commit 21e9e87af495bc170c923320dea08c8eff4c7aee @nebhale nebhale committed Dec 8, 2011
@@ -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<String> listDirectory(String directory) throws TransferFailedException {
+ protected List<String> listDirectory(String directory) throws ResourceDoesNotExistException {
List<String> directoryContents = new ArrayList<String>();
try {
@@ -147,12 +148,13 @@ protected boolean isRemoteResourceNewer(String resourceName, long timestamp) thr
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) {
@@ -2,7 +2,7 @@
<components>
<component>
<role>org.apache.maven.wagon.Wagon</role>
- <role-hint>s3-aws</role-hint>
+ <role-hint>s3</role-hint>
<implementation>org.springframework.build.aws.maven.SimpleStorageServiceWagon</implementation>
<instantiation-strategy>per-lookup</instantiation-strategy>
</component>
@@ -5,8 +5,8 @@
<pattern>%-5level %msg%n</pattern>
</encoder>
</appender>
-
- <root level="info">
+
+ <root level="warn">
<appender-ref ref="CONSOLE" />
</root>
@@ -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");

0 comments on commit 21e9e87

Please sign in to comment.