|
20 | 20 | import java.nio.charset.Charset;
|
21 | 21 | import java.util.EnumSet;
|
22 | 22 | import java.util.Set;
|
| 23 | +import java.util.concurrent.ExecutionException; |
23 | 24 | import java.util.concurrent.Future;
|
24 | 25 |
|
25 | 26 | import org.junit.Before;
|
@@ -317,16 +318,47 @@ public void deleteCallbackWithLambdas() throws Exception {
|
317 | 318 | }
|
318 | 319 |
|
319 | 320 | @Test
|
320 |
| - public void notFound() throws Exception { |
| 321 | + public void identicalExceptionThroughGetAndCallback() throws Exception { |
| 322 | + final HttpClientErrorException[] callbackException = new HttpClientErrorException[1]; |
| 323 | + |
| 324 | + ListenableFuture<?> future = template.execute(baseUrl + "/status/notfound", HttpMethod.GET, null, null); |
| 325 | + future.addCallback(new ListenableFutureCallback<Object>() { |
| 326 | + @Override |
| 327 | + public void onSuccess(Object result) { |
| 328 | + fail("onSuccess not expected"); |
| 329 | + } |
| 330 | + @Override |
| 331 | + public void onFailure(Throwable t) { |
| 332 | + assertTrue(t instanceof HttpClientErrorException); |
| 333 | + callbackException[0] = (HttpClientErrorException) t; |
| 334 | + } |
| 335 | + }); |
| 336 | + |
| 337 | + try { |
| 338 | + future.get(); |
| 339 | + fail("Exception expected"); |
| 340 | + } |
| 341 | + catch (ExecutionException ex) { |
| 342 | + Throwable cause = ex.getCause(); |
| 343 | + assertTrue(cause instanceof HttpClientErrorException); |
| 344 | + assertSame(callbackException[0], cause); |
| 345 | + } |
| 346 | + } |
| 347 | + |
| 348 | + @Test |
| 349 | + public void notFoundGet() throws Exception { |
321 | 350 | try {
|
322 | 351 | Future<?> future = template.execute(baseUrl + "/status/notfound", HttpMethod.GET, null, null);
|
323 | 352 | future.get();
|
324 | 353 | fail("HttpClientErrorException expected");
|
325 | 354 | }
|
326 |
| - catch (HttpClientErrorException ex) { |
327 |
| - assertEquals(HttpStatus.NOT_FOUND, ex.getStatusCode()); |
328 |
| - assertNotNull(ex.getStatusText()); |
329 |
| - assertNotNull(ex.getResponseBodyAsString()); |
| 355 | + catch (ExecutionException ex) { |
| 356 | + assertTrue(ex.getCause() instanceof HttpClientErrorException); |
| 357 | + HttpClientErrorException cause = (HttpClientErrorException)ex.getCause(); |
| 358 | + |
| 359 | + assertEquals(HttpStatus.NOT_FOUND, cause.getStatusCode()); |
| 360 | + assertNotNull(cause.getStatusText()); |
| 361 | + assertNotNull(cause.getResponseBodyAsString()); |
330 | 362 | }
|
331 | 363 | }
|
332 | 364 |
|
@@ -372,10 +404,13 @@ public void serverError() throws Exception {
|
372 | 404 | future.get();
|
373 | 405 | fail("HttpServerErrorException expected");
|
374 | 406 | }
|
375 |
| - catch (HttpServerErrorException ex) { |
376 |
| - assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, ex.getStatusCode()); |
377 |
| - assertNotNull(ex.getStatusText()); |
378 |
| - assertNotNull(ex.getResponseBodyAsString()); |
| 407 | + catch (ExecutionException ex) { |
| 408 | + assertTrue(ex.getCause() instanceof HttpServerErrorException); |
| 409 | + HttpServerErrorException cause = (HttpServerErrorException)ex.getCause(); |
| 410 | + |
| 411 | + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, cause.getStatusCode()); |
| 412 | + assertNotNull(cause.getStatusText()); |
| 413 | + assertNotNull(cause.getResponseBodyAsString()); |
379 | 414 | }
|
380 | 415 | }
|
381 | 416 |
|
|
0 commit comments