added unit tests for standard exception parsing

This commit is contained in:
Adrian Cole 2010-12-22 18:01:54 +01:00
parent e235427b5f
commit 62b5366f01
4 changed files with 96 additions and 26 deletions

View File

@ -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))

View File

@ -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.

View File

@ -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;
}
}

View File

@ -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.<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);
}
}