mirror of https://github.com/apache/jclouds.git
added unit tests for standard exception parsing
This commit is contained in:
parent
e235427b5f
commit
62b5366f01
|
@ -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))
|
||||
|
|
|
@ -38,7 +38,7 @@ public interface TransformingHttpCommandExecutorService {
|
|||
* @param <T>
|
||||
* 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.
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
@ -132,4 +136,69 @@ public class UtilsTest {
|
|||
assertEquals(Sets.newLinkedHashSet(providers), ImmutableSet.<String> 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue