PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/java/com/atlassian/fugue/retry/ExceptionHandlersTest.java

https://bitbucket.org/atlassian/fugue
Java | 90 lines | 58 code | 17 blank | 15 comment | 0 complexity | 410b5c5d871d534460b5b15a5aecb2b2 MD5 | raw file
  1. /*
  2. Copyright 2010 Atlassian
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.atlassian.fugue.retry;
  14. import static junit.framework.Assert.assertEquals;
  15. import static org.hamcrest.MatcherAssert.assertThat;
  16. import static org.hamcrest.Matchers.is;
  17. import static org.mockito.Mockito.mock;
  18. import static org.mockito.Mockito.verify;
  19. import static org.mockito.MockitoAnnotations.initMocks;
  20. import java.lang.reflect.Constructor;
  21. import java.lang.reflect.InvocationTargetException;
  22. import org.hamcrest.Matchers;
  23. import org.junit.Before;
  24. import org.junit.Test;
  25. import org.mockito.Mock;
  26. import org.slf4j.Logger;
  27. public class ExceptionHandlersTest {
  28. @Mock private Logger log;
  29. @Mock private RuntimeException exception;
  30. @Before public void setUp() {
  31. initMocks(this);
  32. }
  33. @Test public void loggingExceptionAction() {
  34. ExceptionHandler loggingExceptionHandler = ExceptionHandlers.loggingExceptionHandler(log);
  35. loggingExceptionHandler.handle(exception);
  36. verify(log).warn("Exception encountered: ", exception);
  37. }
  38. @Test public void chainCallOrder() {
  39. final StringBuffer sb = new StringBuffer();
  40. ExceptionHandler first = new ExceptionHandler() {
  41. public void handle(RuntimeException e) {
  42. sb.append("1");
  43. }
  44. };
  45. ExceptionHandler second = new ExceptionHandler() {
  46. public void handle(RuntimeException e) {
  47. sb.append("2");
  48. }
  49. };
  50. ExceptionHandler handler = ExceptionHandlers.chain(first, second);
  51. handler.handle(exception);
  52. assertEquals("12", sb.toString());
  53. }
  54. @Test public void loggingExceptionHandler() {
  55. Logger logger = mock(Logger.class);
  56. ExceptionHandler exceptionHandler = ExceptionHandlers.loggingExceptionHandler(logger);
  57. assertThat(((ExceptionHandlers.LoggingExceptionHandler) exceptionHandler).logger(), is(logger));
  58. }
  59. @Test public void loggingExceptionHandlerNull() {
  60. ExceptionHandler exceptionHandler = ExceptionHandlers.loggingExceptionHandler(null);
  61. assertThat(exceptionHandler.getClass(), Matchers.<Class<? extends ExceptionHandler>> is(ExceptionHandlers.LoggingExceptionHandler.class));
  62. assertThat(((ExceptionHandlers.LoggingExceptionHandler) exceptionHandler).logger(), is(ExceptionHandlers.logger()));
  63. }
  64. @Test(expected = InvocationTargetException.class) public void nonInstantiable() throws NoSuchMethodException, InvocationTargetException,
  65. IllegalAccessException, InstantiationException {
  66. Constructor<ExceptionHandlers> declaredConstructor = ExceptionHandlers.class.getDeclaredConstructor();
  67. declaredConstructor.setAccessible(true);
  68. declaredConstructor.newInstance();
  69. }
  70. }