From 62b5366f0138f459c54575cde2faf3169fecb8ca Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Wed, 22 Dec 2010 18:01:54 +0100 Subject: [PATCH] added unit tests for standard exception parsing --- .../concurrent/internal/SyncProxy.java | 27 +------ ...ransformingHttpCommandExecutorService.java | 2 +- .../src/main/java/org/jclouds/util/Utils.java | 22 ++++++ .../test/java/org/jclouds/util/UtilsTest.java | 71 ++++++++++++++++++- 4 files changed, 96 insertions(+), 26 deletions(-) diff --git a/core/src/main/java/org/jclouds/concurrent/internal/SyncProxy.java b/core/src/main/java/org/jclouds/concurrent/internal/SyncProxy.java index 9594a7ecff..741c4b01be 100644 --- a/core/src/main/java/org/jclouds/concurrent/internal/SyncProxy.java +++ b/core/src/main/java/org/jclouds/concurrent/internal/SyncProxy.java @@ -35,14 +35,10 @@ import javax.inject.Inject; import javax.inject.Named; import org.jclouds.concurrent.Timeout; -import org.jclouds.http.HttpResponseException; import org.jclouds.internal.ClassMethodArgs; -import org.jclouds.rest.AuthorizationException; -import org.jclouds.rest.ResourceNotFoundException; import org.jclouds.rest.annotations.Delegate; import org.jclouds.util.Utils; -import com.google.common.base.Throwables; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Maps; import com.google.common.util.concurrent.ListenableFuture; @@ -135,32 +131,15 @@ public class SyncProxy implements InvocationHandler { return ((ListenableFuture) methodMap.get(method).invoke(delegate, args)).get(timeoutMap.get(method), TimeUnit.NANOSECONDS); } catch (ProvisionException e) { - throw throwTypedExceptionOrCause(method.getExceptionTypes(), e); + throw Utils.returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(method.getExceptionTypes(), e); } catch (ExecutionException e) { - throw throwTypedExceptionOrCause(method.getExceptionTypes(), e); + throw Utils.returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(method.getExceptionTypes(), e); } catch (Exception e) { - throw throwTypedExceptionOrCause(method.getExceptionTypes(), e); + throw Utils.returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(method.getExceptionTypes(), e); } } } - // Note this needs to be kept up-to-date with all top-level exceptions jclouds works against - @SuppressWarnings("unchecked") - public static Exception throwTypedExceptionOrCause(Class[] exceptionTypes, Exception exception) throws Exception { - for (Class type : exceptionTypes) { - Throwable throwable = Utils.getFirstThrowableOfType(exception, type); - if (throwable != null) { - return (Exception) throwable; - } - } - Throwables.propagateIfInstanceOf(exception, IllegalStateException.class); - Throwables.propagateIfInstanceOf(exception, AuthorizationException.class); - Throwables.propagateIfInstanceOf(exception, ResourceNotFoundException.class); - Throwables.propagateIfInstanceOf(exception, HttpResponseException.class); - Throwables.throwCause(exception, true); - return exception; - } - @Override public boolean equals(Object obj) { if (obj == null || !(obj instanceof SyncProxy)) diff --git a/core/src/main/java/org/jclouds/http/TransformingHttpCommandExecutorService.java b/core/src/main/java/org/jclouds/http/TransformingHttpCommandExecutorService.java index 4ae1a3f745..0c4dd9b218 100644 --- a/core/src/main/java/org/jclouds/http/TransformingHttpCommandExecutorService.java +++ b/core/src/main/java/org/jclouds/http/TransformingHttpCommandExecutorService.java @@ -38,7 +38,7 @@ public interface TransformingHttpCommandExecutorService { * @param * type that is required from the value. * @param command - * what to execute + * holds the state of the request, including metadata such as redirect counts * @param responseTransformer * how to transform the response from the above command * @return value of the intended response. diff --git a/core/src/main/java/org/jclouds/util/Utils.java b/core/src/main/java/org/jclouds/util/Utils.java index a01198d1c7..8c0a6d9c50 100644 --- a/core/src/main/java/org/jclouds/util/Utils.java +++ b/core/src/main/java/org/jclouds/util/Utils.java @@ -63,8 +63,10 @@ import javax.annotation.Resource; import org.jclouds.PropertiesBuilder; import org.jclouds.crypto.Pems; import org.jclouds.domain.Credentials; +import org.jclouds.http.HttpResponseException; import org.jclouds.logging.Logger; import org.jclouds.rest.AuthorizationException; +import org.jclouds.rest.ResourceNotFoundException; import org.jclouds.rest.RestContextBuilder; import org.xml.sax.Attributes; @@ -73,6 +75,7 @@ import com.google.common.base.Function; import com.google.common.base.Predicate; import com.google.common.base.Splitter; import com.google.common.base.Supplier; +import com.google.common.base.Throwables; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap.Builder; @@ -531,4 +534,23 @@ public class Utils { return new Credentials(identity, credential); } + // Note this needs to be kept up-to-date with all top-level exceptions jclouds works against + @SuppressWarnings({ "unchecked", "rawtypes" }) + public static Exception returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(Class[] exceptionTypes, + Exception exception) throws Exception { + for (Class type : exceptionTypes) { + Throwable throwable = getFirstThrowableOfType(exception, type); + if (throwable != null) { + return (Exception) throwable; + } + } + Throwables.propagateIfInstanceOf(exception, IllegalStateException.class); + Throwables.propagateIfInstanceOf(exception, IllegalArgumentException.class); + Throwables.propagateIfInstanceOf(exception, AuthorizationException.class); + Throwables.propagateIfInstanceOf(exception, ResourceNotFoundException.class); + Throwables.propagateIfInstanceOf(exception, HttpResponseException.class); + Throwables.throwCause(exception, true); + return exception; + } + } diff --git a/core/src/test/java/org/jclouds/util/UtilsTest.java b/core/src/test/java/org/jclouds/util/UtilsTest.java index 3e8704cd92..550cf545fd 100644 --- a/core/src/test/java/org/jclouds/util/UtilsTest.java +++ b/core/src/test/java/org/jclouds/util/UtilsTest.java @@ -20,6 +20,7 @@ package org.jclouds.util; import static org.easymock.classextension.EasyMock.createMock; +import static org.easymock.classextension.EasyMock.createNiceMock; import static org.testng.Assert.assertEquals; import java.io.UnsupportedEncodingException; @@ -27,7 +28,10 @@ import java.util.Map; import java.util.concurrent.TimeoutException; import org.jclouds.domain.Credentials; +import org.jclouds.http.HttpCommand; +import org.jclouds.http.HttpResponseException; import org.jclouds.rest.AuthorizationException; +import org.jclouds.rest.ResourceNotFoundException; import org.testng.annotations.Test; import com.google.common.collect.ImmutableList; @@ -105,7 +109,7 @@ public class UtilsTest { ProvisionException pex = new ProvisionException(ImmutableSet.of(message)); assertEquals(Utils.getFirstThrowableOfType(pex, AuthorizationException.class), null); } - + public void testGetFirstThrowableOfTypeWhenCauseIsNull() { Message message = new Message(ImmutableList.of(), "test", null); ProvisionException pex = new ProvisionException(ImmutableSet.of(message)); @@ -132,4 +136,69 @@ public class UtilsTest { assertEquals(Sets.newLinkedHashSet(providers), ImmutableSet. of()); } + static class TestException extends Exception { + + /** + * + */ + private static final long serialVersionUID = 1L; + + } + + @Test + public void testReturnExceptionThatsInList() throws Exception { + Exception e = new TestException(); + assertEquals( + Utils.returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(new Class[] { TestException.class }, e), + e); + assertEquals(Utils.returnFirstExceptionIfInListOrThrowStandardExceptionOrCause( + new Class[] { TestException.class }, new RuntimeException(e)), e); + } + + @Test(expectedExceptions = TestException.class) + public void testThrowExceptionNotInList() throws Exception { + Exception e = new TestException(); + Utils.returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(new Class[] {}, e); + } + + @Test(expectedExceptions = IllegalStateException.class) + public void testPropagateStandardExceptionIllegalStateException() throws Exception { + Exception e = new IllegalStateException(); + assertEquals( + Utils.returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(new Class[] {}, new RuntimeException(e)), + e); + } + + @Test(expectedExceptions = IllegalArgumentException.class) + public void testPropagateStandardExceptionIllegalArgumentException() throws Exception { + Exception e = new IllegalArgumentException(); + assertEquals( + Utils.returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(new Class[] {}, new RuntimeException(e)), + e); + } + + @Test(expectedExceptions = AuthorizationException.class) + public void testPropagateStandardExceptionAuthorizationException() throws Exception { + Exception e = new AuthorizationException(); + assertEquals( + Utils.returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(new Class[] {}, new RuntimeException(e)), + e); + } + + @Test(expectedExceptions = ResourceNotFoundException.class) + public void testPropagateStandardExceptionResourceNotFoundException() throws Exception { + Exception e = new ResourceNotFoundException(); + assertEquals( + Utils.returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(new Class[] {}, new RuntimeException(e)), + e); + } + + @Test(expectedExceptions = HttpResponseException.class) + public void testPropagateStandardExceptionHttpResponseException() throws Exception { + Exception e = new HttpResponseException("goo", createNiceMock(HttpCommand.class), null); + assertEquals( + Utils.returnFirstExceptionIfInListOrThrowStandardExceptionOrCause(new Class[] {}, new RuntimeException(e)), + e); + } + }