|
23 | 23 |
|
24 | 24 | import com.stackify.api.ErrorItem; |
25 | 25 | import com.stackify.api.TraceFrame; |
26 | | -import com.stackify.api.common.lang.Throwables; |
27 | 26 |
|
28 | 27 | /** |
29 | 28 | * Throwables JUnit Test |
|
32 | 31 | */ |
33 | 32 | public class ThrowablesTest { |
34 | 33 |
|
| 34 | + /** |
| 35 | + * testGetCausalChainWithNull |
| 36 | + */ |
| 37 | + @Test(expected = NullPointerException.class) |
| 38 | + public void testGetCausalChainWithNull() { |
| 39 | + Throwables.getCausalChain(null); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * testGetCausalChainWithoutCause |
| 44 | + */ |
| 45 | + @Test |
| 46 | + public void testGetCausalChainWithoutCause() { |
| 47 | + Throwable t = new NullPointerException(); |
| 48 | + |
| 49 | + List<Throwable> throwables = Throwables.getCausalChain(t); |
| 50 | + Assert.assertNotNull(throwables); |
| 51 | + Assert.assertEquals(1, throwables.size()); |
| 52 | + Assert.assertEquals(t, throwables.get(0)); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * testGetCausalChainWithCause |
| 57 | + */ |
| 58 | + @Test |
| 59 | + public void testGetCausalChainWithCause() { |
| 60 | + Throwable c = new NullPointerException(); |
| 61 | + Throwable t = new RuntimeException(c); |
| 62 | + |
| 63 | + List<Throwable> throwables = Throwables.getCausalChain(t); |
| 64 | + Assert.assertNotNull(throwables); |
| 65 | + Assert.assertEquals(2, throwables.size()); |
| 66 | + Assert.assertEquals(t, throwables.get(0)); |
| 67 | + Assert.assertEquals(c, throwables.get(1)); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * testGetCausalChainWithSelfCausation |
| 72 | + */ |
| 73 | + @Test |
| 74 | + public void testGetCausalChainWithSelfCausation() { |
| 75 | + Throwable c2 = new RuntimeException(); |
| 76 | + Throwable c1 = new RuntimeException(c2); |
| 77 | + c2.initCause(c1); |
| 78 | + Throwable t = new RuntimeException(c1); |
| 79 | + |
| 80 | + List<Throwable> throwables = Throwables.getCausalChain(t); |
| 81 | + Assert.assertNotNull(throwables); |
| 82 | + Assert.assertEquals(3, throwables.size()); |
| 83 | + Assert.assertEquals(t, throwables.get(0)); |
| 84 | + Assert.assertEquals(c1, throwables.get(1)); |
| 85 | + Assert.assertEquals(c2, throwables.get(2)); |
| 86 | + } |
| 87 | + |
35 | 88 | /** |
36 | 89 | * testToErrorDetail |
37 | 90 | */ |
@@ -127,4 +180,36 @@ public void testToErrorDetailWithRecursiveCause() { |
127 | 180 | Assert.assertTrue(stackTrace.isEmpty()); |
128 | 181 | Assert.assertNull(errorDetail.getInnerError()); |
129 | 182 | } |
| 183 | + |
| 184 | + /** |
| 185 | + * testToErrorDetailWithSubcause |
| 186 | + */ |
| 187 | + @Test |
| 188 | + public void testToErrorDetailWithSubcause() { |
| 189 | + String message = "message"; |
| 190 | + String causeMessage = "causeMessage"; |
| 191 | + String subcauseMessage = "subcauseMessage"; |
| 192 | + Throwable subcause = new NullPointerException(subcauseMessage); |
| 193 | + Throwable cause = new UnsupportedOperationException(causeMessage, subcause); |
| 194 | + Throwable throwable = new RuntimeException(message, cause); |
| 195 | + |
| 196 | + ErrorItem errorDetail = Throwables.toErrorItem(throwable); |
| 197 | + |
| 198 | + Assert.assertNotNull(errorDetail); |
| 199 | + |
| 200 | + Assert.assertEquals(message, errorDetail.getMessage()); |
| 201 | + Assert.assertEquals("java.lang.RuntimeException", errorDetail.getErrorType()); |
| 202 | + |
| 203 | + List<TraceFrame> stackTrace = errorDetail.getStackTrace(); |
| 204 | + Assert.assertNotNull(stackTrace); |
| 205 | + Assert.assertTrue(!stackTrace.isEmpty()); |
| 206 | + |
| 207 | + Assert.assertEquals(causeMessage, errorDetail.getInnerError().getMessage()); |
| 208 | + Assert.assertEquals("java.lang.UnsupportedOperationException", errorDetail.getInnerError().getErrorType()); |
| 209 | + |
| 210 | + Assert.assertEquals(subcauseMessage, errorDetail.getInnerError().getInnerError().getMessage()); |
| 211 | + Assert.assertEquals("java.lang.NullPointerException", errorDetail.getInnerError().getInnerError().getErrorType()); |
| 212 | + |
| 213 | + Assert.assertNull(errorDetail.getInnerError().getInnerError().getInnerError()); |
| 214 | + } |
130 | 215 | } |
0 commit comments