/core/src/test/java/org/jclouds/util/Throwables2Test.java

https://github.com/richardcloudsoft/legacy-jclouds · Java · 269 lines · 210 code · 38 blank · 21 comment · 0 complexity · f9137bc1d06071e12e6dfdd64632a5fc MD5 · raw file

  1. /**
  2. * Licensed to jclouds, Inc. (jclouds) under one or more
  3. * contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. jclouds licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.jclouds.util;
  20. import static org.easymock.EasyMock.createMock;
  21. import static org.easymock.EasyMock.createNiceMock;
  22. import static org.jclouds.reflect.Reflection2.typeToken;
  23. import static org.jclouds.util.Throwables2.getFirstThrowableOfType;
  24. import static org.jclouds.util.Throwables2.propagateIfPossible;
  25. import static org.testng.Assert.assertEquals;
  26. import java.io.IOException;
  27. import java.net.SocketException;
  28. import java.util.concurrent.ExecutionException;
  29. import java.util.concurrent.Future;
  30. import java.util.concurrent.TimeoutException;
  31. import org.jclouds.concurrent.TransformParallelException;
  32. import org.jclouds.http.HttpCommand;
  33. import org.jclouds.http.HttpResponseException;
  34. import org.jclouds.rest.AuthorizationException;
  35. import org.jclouds.rest.InsufficientResourcesException;
  36. import org.jclouds.rest.ResourceNotFoundException;
  37. import org.testng.annotations.Test;
  38. import com.google.common.collect.ImmutableList;
  39. import com.google.common.collect.ImmutableMap;
  40. import com.google.common.collect.ImmutableSet;
  41. import com.google.common.reflect.TypeToken;
  42. import com.google.inject.CreationException;
  43. import com.google.inject.ProvisionException;
  44. import com.google.inject.spi.Message;
  45. /**
  46. * @author Adrian Cole
  47. */
  48. @Test
  49. public class Throwables2Test {
  50. public void testGetFirstThrowableOfTypeSubclass() {
  51. SocketException aex = createMock(SocketException.class);
  52. assertEquals(getFirstThrowableOfType(aex, IOException.class), aex);
  53. }
  54. public void testGetFirstThrowableOfTypeOuter() {
  55. AuthorizationException aex = createMock(AuthorizationException.class);
  56. assertEquals(getFirstThrowableOfType(aex, AuthorizationException.class), aex);
  57. }
  58. public void testGetCause() {
  59. AuthorizationException aex = createMock(AuthorizationException.class);
  60. Message message = new Message(ImmutableList.of(), "test", aex);
  61. ProvisionException pex = new ProvisionException(ImmutableSet.of(message));
  62. assertEquals(getFirstThrowableOfType(pex, AuthorizationException.class), aex);
  63. }
  64. public void testGetFirstThrowableOfTypeInner() {
  65. AuthorizationException aex = createMock(AuthorizationException.class);
  66. Message message = new Message(ImmutableList.of(), "test", aex);
  67. ProvisionException pex = new ProvisionException(ImmutableSet.of(message));
  68. assertEquals(getFirstThrowableOfType(pex, AuthorizationException.class), aex);
  69. }
  70. public void testGetFirstThrowableOfTypeFail() {
  71. TimeoutException aex = createMock(TimeoutException.class);
  72. Message message = new Message(ImmutableList.of(), "test", aex);
  73. ProvisionException pex = new ProvisionException(ImmutableSet.of(message));
  74. assertEquals(getFirstThrowableOfType(pex, AuthorizationException.class), null);
  75. }
  76. public void testGetFirstThrowableOfTypeWhenCauseIsNull() {
  77. Message message = new Message(ImmutableList.of(), "test", null);
  78. ProvisionException pex = new ProvisionException(ImmutableSet.of(message));
  79. assertEquals(getFirstThrowableOfType(pex, AuthorizationException.class), null);
  80. }
  81. public void testGetCauseCreation() {
  82. AuthorizationException aex = createMock(AuthorizationException.class);
  83. Message message = new Message(ImmutableList.of(), "test", aex);
  84. CreationException pex = new CreationException(ImmutableSet.of(message));
  85. assertEquals(getFirstThrowableOfType(pex, AuthorizationException.class), aex);
  86. }
  87. public void testGetFirstThrowableOfTypeInnerCreation() {
  88. AuthorizationException aex = createMock(AuthorizationException.class);
  89. Message message = new Message(ImmutableList.of(), "test", aex);
  90. CreationException pex = new CreationException(ImmutableSet.of(message));
  91. assertEquals(getFirstThrowableOfType(pex, AuthorizationException.class), aex);
  92. }
  93. public void testGetFirstThrowableOfTypeFailCreation() {
  94. TimeoutException aex = createMock(TimeoutException.class);
  95. Message message = new Message(ImmutableList.of(), "test", aex);
  96. CreationException pex = new CreationException(ImmutableSet.of(message));
  97. assertEquals(getFirstThrowableOfType(pex, AuthorizationException.class), null);
  98. }
  99. public void testGetFirstThrowableOfTypeWhenCauseIsNullCreation() {
  100. Message message = new Message(ImmutableList.of(), "test", null);
  101. CreationException pex = new CreationException(ImmutableSet.of(message));
  102. assertEquals(getFirstThrowableOfType(pex, AuthorizationException.class), null);
  103. }
  104. public void testGetCauseTransformParallel() {
  105. Exception aex = createMock(AuthorizationException.class);
  106. TransformParallelException pex = new TransformParallelException(ImmutableMap.<Object, Future<?>> of(),
  107. ImmutableMap.of("bad", aex), "test");
  108. assertEquals(getFirstThrowableOfType(pex, AuthorizationException.class), aex);
  109. }
  110. public void testGetFirstThrowableOfTypeInnerTransformParallel() {
  111. Exception aex = createMock(AuthorizationException.class);
  112. TransformParallelException pex = new TransformParallelException(ImmutableMap.<Object, Future<?>> of(),
  113. ImmutableMap.of("bad", (Exception) new ExecutionException(aex)), "test");
  114. assertEquals(getFirstThrowableOfType(pex, AuthorizationException.class), aex);
  115. }
  116. public void testGetFirstThrowableOfTypeOuterTransformParallel() {
  117. Exception aex = createMock(AuthorizationException.class);
  118. TransformParallelException pex = new TransformParallelException(ImmutableMap.<Object, Future<?>> of(),
  119. ImmutableMap.of("bad", (Exception) aex), "test");
  120. assertEquals(getFirstThrowableOfType(new ExecutionException(pex), AuthorizationException.class), aex);
  121. }
  122. public void testGetFirstThrowableOfTypeFailTransformParallel() {
  123. Exception aex = createMock(TimeoutException.class);
  124. TransformParallelException pex = new TransformParallelException(ImmutableMap.<Object, Future<?>> of(),
  125. ImmutableMap.of("bad", aex), "test");
  126. assertEquals(getFirstThrowableOfType(pex, AuthorizationException.class), null);
  127. }
  128. @Test(expectedExceptions = TestException.class)
  129. public void testPropagateExceptionThatsInList() throws Throwable {
  130. Exception e = new TestException();
  131. propagateIfPossible(e, ImmutableSet.<TypeToken<? extends Throwable>> of(typeToken(TestException.class)));
  132. }
  133. @Test(expectedExceptions = TestException.class)
  134. public void testPropagateWrappedExceptionThatsInList() throws Throwable {
  135. Exception e = new TestException();
  136. propagateIfPossible(new RuntimeException(e),
  137. ImmutableSet.<TypeToken<? extends Throwable>> of(typeToken(TestException.class)));
  138. }
  139. public void testPropagateIfPossibleDoesnThrowExceptionNotInList() throws Throwable {
  140. Exception e = new TestException();
  141. propagateIfPossible(e, ImmutableSet.<TypeToken<? extends Throwable>> of());
  142. }
  143. @Test(expectedExceptions = IllegalStateException.class)
  144. public void testPropagateStandardExceptionIllegalStateException() throws Throwable {
  145. Exception e = new IllegalStateException();
  146. propagateIfPossible(new RuntimeException(e), ImmutableSet.<TypeToken<? extends Throwable>> of());
  147. }
  148. @Test(expectedExceptions = IllegalArgumentException.class)
  149. public void testPropagateStandardExceptionIllegalArgumentException() throws Throwable {
  150. Exception e = new IllegalArgumentException();
  151. propagateIfPossible(new RuntimeException(e), ImmutableSet.<TypeToken<? extends Throwable>> of());
  152. }
  153. @Test(expectedExceptions = UnsupportedOperationException.class)
  154. public void testPropagateStandardExceptionUnsupportedOperationException() throws Throwable {
  155. Exception e = new UnsupportedOperationException();
  156. propagateIfPossible(new RuntimeException(e), ImmutableSet.<TypeToken<? extends Throwable>> of());
  157. }
  158. @Test(expectedExceptions = AssertionError.class)
  159. public void testPropagateStandardExceptionAssertionError() throws Throwable {
  160. AssertionError e = new AssertionError();
  161. propagateIfPossible(new RuntimeException(e), ImmutableSet.<TypeToken<? extends Throwable>> of());
  162. }
  163. @Test(expectedExceptions = AuthorizationException.class)
  164. public void testPropagateStandardExceptionAuthorizationException() throws Throwable {
  165. Exception e = new AuthorizationException();
  166. propagateIfPossible(new RuntimeException(e), ImmutableSet.<TypeToken<? extends Throwable>> of());
  167. }
  168. @Test(expectedExceptions = AuthorizationException.class)
  169. public void testPropagateProvisionExceptionAuthorizationException() throws Throwable {
  170. Exception e = new AuthorizationException();
  171. propagateIfPossible(
  172. new ProvisionException(ImmutableSet.of(new Message(ImmutableList.of(), "Error in custom provider", e))),
  173. ImmutableSet.<TypeToken<? extends Throwable>> of());
  174. }
  175. @Test(expectedExceptions = AuthorizationException.class)
  176. public void testPropagateCreationExceptionAuthorizationException() throws Throwable {
  177. Exception e = new AuthorizationException();
  178. propagateIfPossible(
  179. new CreationException(ImmutableSet.of(new Message(ImmutableList.of(), "Error in custom provider", e))),
  180. ImmutableSet.<TypeToken<? extends Throwable>> of());
  181. }
  182. @Test(expectedExceptions = InsufficientResourcesException.class)
  183. public void testPropagateStandardExceptionInsufficientResourcesException() throws Throwable {
  184. Exception e = new InsufficientResourcesException();
  185. propagateIfPossible(new RuntimeException(e), ImmutableSet.<TypeToken<? extends Throwable>> of());
  186. }
  187. @Test(expectedExceptions = ResourceNotFoundException.class)
  188. public void testPropagateStandardExceptionResourceNotFoundException() throws Throwable {
  189. Exception e = new ResourceNotFoundException();
  190. propagateIfPossible(new RuntimeException(e), ImmutableSet.<TypeToken<? extends Throwable>> of());
  191. }
  192. @Test(expectedExceptions = IllegalStateException.class)
  193. public void testPropagateStandardExceptionIllegalStateExceptionNestedInHttpResponseException() throws Throwable {
  194. Exception e = new IllegalStateException();
  195. propagateIfPossible(new HttpResponseException("goo", createNiceMock(HttpCommand.class), null, e),
  196. ImmutableSet.<TypeToken<? extends Throwable>> of());
  197. }
  198. @Test(expectedExceptions = IllegalArgumentException.class)
  199. public void testPropagateStandardExceptionIllegalArgumentExceptionNestedInHttpResponseException() throws Throwable {
  200. Exception e = new IllegalArgumentException();
  201. propagateIfPossible(new HttpResponseException("goo", createNiceMock(HttpCommand.class), null, e),
  202. ImmutableSet.<TypeToken<? extends Throwable>> of());
  203. }
  204. @Test(expectedExceptions = UnsupportedOperationException.class)
  205. public void testPropagateStandardExceptionUnsupportedOperationExceptionNestedInHttpResponseException()
  206. throws Throwable {
  207. Exception e = new UnsupportedOperationException();
  208. propagateIfPossible(new HttpResponseException("goo", createNiceMock(HttpCommand.class), null, e),
  209. ImmutableSet.<TypeToken<? extends Throwable>> of());
  210. }
  211. @Test(expectedExceptions = AuthorizationException.class)
  212. public void testPropagateStandardExceptionAuthorizationExceptionNestedInHttpResponseException() throws Throwable {
  213. Exception e = new AuthorizationException();
  214. propagateIfPossible(new HttpResponseException("goo", createNiceMock(HttpCommand.class), null, e),
  215. ImmutableSet.<TypeToken<? extends Throwable>> of());
  216. }
  217. @Test(expectedExceptions = ResourceNotFoundException.class)
  218. public void testPropagateStandardExceptionResourceNotFoundExceptionNestedInHttpResponseException() throws Throwable {
  219. Exception e = new ResourceNotFoundException();
  220. propagateIfPossible(new HttpResponseException("goo", createNiceMock(HttpCommand.class), null, e),
  221. ImmutableSet.<TypeToken<? extends Throwable>> of());
  222. }
  223. @Test(expectedExceptions = HttpResponseException.class)
  224. public void testPropagateStandardExceptionHttpResponseException() throws Throwable {
  225. Exception e = new HttpResponseException("goo", createNiceMock(HttpCommand.class), null);
  226. propagateIfPossible(new RuntimeException(e), ImmutableSet.<TypeToken<? extends Throwable>> of());
  227. }
  228. static class TestException extends Exception {
  229. private static final long serialVersionUID = 1L;
  230. }
  231. }