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