View Javadoc
1   /*
2      Copyright 2011 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.deprecated;
17  
18  import org.junit.Assert;
19  import org.junit.Test;
20  import org.junit.runner.RunWith;
21  import org.mockito.Mock;
22  import org.mockito.Mockito;
23  import org.mockito.runners.MockitoJUnitRunner;
24  
25  import java.util.function.Function;
26  
27  import static org.mockito.Mockito.verifyZeroInteractions;
28  
29  @RunWith(MockitoJUnitRunner.class) public final class ThrowablesTest {
30    @Mock private Function<Throwable, RuntimeException> function;
31  
32    @SuppressWarnings("deprecation") @Test public void testPropagateWithFunctionForRuntimeException() throws Exception {
33      final Exception original = new RuntimeException();
34      try {
35        propogate(original, function);
36        Assert.fail("Should have thrown an exception");
37      } catch (final Exception e) {
38        Assert.assertSame(original, e);
39      }
40  
41      verifyZeroInteractions(function);
42    }
43  
44    @SuppressWarnings("deprecation") @Test public void testPropagateWithFunctionForNonRuntimeException() throws Exception {
45      final RuntimeException runtime = new RuntimeException();
46      Mockito.when(function.apply(Mockito.<Throwable> any())).thenReturn(runtime);
47  
48      final Throwable original = new Exception();
49      try {
50        propogate(original, function);
51        Assert.fail("Should have thrown an exception");
52      } catch (final Exception e) {
53        Assert.assertSame(runtime, e);
54      }
55  
56      Mockito.verify(function).apply(original);
57    }
58  
59    @SuppressWarnings("deprecation") @Test public void testPropagateWithTypeForRuntimeException() throws Exception {
60      final Exception original = new RuntimeException();
61      try {
62        propogate(original, MyRuntimeException.class);
63        Assert.fail("Should have thrown an exception");
64      } catch (final Exception e) {
65        Assert.assertSame(original, e);
66      }
67    }
68  
69    @SuppressWarnings("deprecation") @Test public void testPropagateWithTypeForNonRuntimeException() throws Exception {
70      final Exception original = new Exception();
71      try {
72        propogate(original, MyRuntimeException.class);
73        Assert.fail("Should have thrown an exception");
74      } catch (final Exception e) {
75        Assert.assertTrue(e instanceof MyRuntimeException);
76        Assert.assertSame(original, e.getCause());
77      }
78    }
79  
80    static final class MyRuntimeException extends RuntimeException {
81      private static final long serialVersionUID = 5698445063323657007L;
82  
83      public MyRuntimeException(final Throwable throwable) {
84        super(throwable);
85      }
86    }
87  
88    @SuppressWarnings("deprecation") private <R extends RuntimeException> void propogate(final Throwable t, final Function<Throwable, R> f) {
89      throw Throwables.propagate(t, f);
90    }
91  
92    @SuppressWarnings("deprecation") private <R extends RuntimeException> void propogate(final Throwable t, Class<R> c) {
93      throw Throwables.propagate(t, c);
94    }
95  }