|
| 1 | +package com.bugsnag; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertFalse; |
| 5 | +import static org.junit.Assert.assertNotNull; |
| 6 | +import static org.junit.Assert.assertTrue; |
| 7 | + |
| 8 | +import com.bugsnag.callbacks.Callback; |
| 9 | +import com.bugsnag.delivery.Delivery; |
| 10 | +import com.bugsnag.serialization.Serializer; |
| 11 | + |
| 12 | +import org.junit.Before; |
| 13 | +import org.junit.Test; |
| 14 | + |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +public class ExceptionTest { |
| 18 | + |
| 19 | + private Exception exception; |
| 20 | + private RuntimeException ogThrowable; |
| 21 | + |
| 22 | + /** |
| 23 | + * Initialises a config |
| 24 | + * |
| 25 | + * @throws Throwable if config couldn't be initialised |
| 26 | + */ |
| 27 | + @Before |
| 28 | + public void setUp() throws Throwable { |
| 29 | + Configuration config = new Configuration("api-key"); |
| 30 | + ogThrowable = new RuntimeException("Test"); |
| 31 | + exception = new Exception(config, ogThrowable); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void testDefaults() throws Throwable { |
| 36 | + assertEquals("java.lang.RuntimeException", exception.getErrorClass()); |
| 37 | + assertEquals("Test", exception.getMessage()); |
| 38 | + assertEquals(ogThrowable, exception.getThrowable()); |
| 39 | + assertFalse(exception.getStacktrace().isEmpty()); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void testClassOverride() throws Throwable { |
| 44 | + exception.setErrorClass("Hello"); |
| 45 | + assertEquals("Hello", exception.getErrorClass()); |
| 46 | + assertEquals("Test", exception.getMessage()); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void testReportCallback() throws Throwable { |
| 51 | + Bugsnag bugsnag = new Bugsnag("apikey"); |
| 52 | + bugsnag.setDelivery(new Delivery() { |
| 53 | + @Override |
| 54 | + public void deliver(Serializer serializer, Object object) { |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void close() { |
| 59 | + } |
| 60 | + }); |
| 61 | + assertTrue(bugsnag.notify(ogThrowable, new Callback() { |
| 62 | + @Override |
| 63 | + public void beforeNotify(Report report) { |
| 64 | + try { |
| 65 | + assertEquals(ogThrowable, report.getException()); |
| 66 | + assertEquals("Test", report.getExceptionMessage()); |
| 67 | + assertEquals("java.lang.RuntimeException", report.getExceptionName()); |
| 68 | + |
| 69 | + report.setExceptionName("Foo"); |
| 70 | + assertEquals("Foo", report.getExceptionName()); |
| 71 | + |
| 72 | + |
| 73 | + List<Exception> exceptions = report.getExceptions(); |
| 74 | + assertEquals(1, exceptions.size()); |
| 75 | + |
| 76 | + Exception exception = exceptions.get(0); |
| 77 | + assertNotNull(exception); |
| 78 | + assertEquals("Foo", exception.getErrorClass()); |
| 79 | + assertEquals("Test", exception.getMessage()); |
| 80 | + } catch (Throwable throwable) { |
| 81 | + report.cancel(); |
| 82 | + } |
| 83 | + } |
| 84 | + })); |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments