From c7ae834d09e0a1081c618f456bc4f8636fc06264 Mon Sep 17 00:00:00 2001 From: fractalwrench Date: Fri, 20 Apr 2018 17:28:34 +0100 Subject: [PATCH 1/7] test: add test coverage for notifyBlocking adds test coverage for non-deprecated overloads of notifyBlocking in the client class --- .../com/bugsnag/android/ClientNotifyTest.java | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 sdk/src/androidTest/java/com/bugsnag/android/ClientNotifyTest.java diff --git a/sdk/src/androidTest/java/com/bugsnag/android/ClientNotifyTest.java b/sdk/src/androidTest/java/com/bugsnag/android/ClientNotifyTest.java new file mode 100644 index 0000000000..1c36bd8e51 --- /dev/null +++ b/sdk/src/androidTest/java/com/bugsnag/android/ClientNotifyTest.java @@ -0,0 +1,87 @@ +package com.bugsnag.android; + +import static org.junit.Assert.assertEquals; + +import android.support.test.filters.SmallTest; +import android.support.test.runner.AndroidJUnit4; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.util.Map; + +@RunWith(AndroidJUnit4.class) +@SmallTest +public class ClientNotifyTest { + + private Client client; + private FakeClient apiClient; + + /** + * Generates a configuration and clears sharedPrefs values to begin the test with a clean slate + * @throws Exception if initialisation failed + */ + @Before + public void setUp() throws Exception { + client = BugsnagTestUtils.generateClient(); + apiClient = new FakeClient(); + client.setErrorReportApiClient(apiClient); + } + + @Test + public void testNotifyBlocking() { + client.notifyBlocking(new RuntimeException("Testing")); + assertEquals(Severity.WARNING, apiClient.report.getError().getSeverity()); + } + + @Test + public void testNotifyBlocking2() { + client.notifyBlocking(new RuntimeException("Testing"), new Callback() { + @Override + public void beforeNotify(Report report) { + report.getError().setUserName("Foo"); + } + }); + Error error = apiClient.report.getError(); + assertEquals(Severity.WARNING, error.getSeverity()); + assertEquals("Foo", error.getUser().getName()); + } + + @Test + public void testNotifyBlocking3() { + client.notifyBlocking(new RuntimeException("Testing"), Severity.INFO); + assertEquals(Severity.INFO, apiClient.report.getError().getSeverity()); + } + + @Test + public void testNotifyBlocking4() { + StackTraceElement[] stacktrace = { + new StackTraceElement("MyClass", "MyMethod", "MyFile", 5) + }; + + client.notifyBlocking("Name", "Message", stacktrace, new Callback() { + @Override + public void beforeNotify(Report report) { + report.getError().setSeverity(Severity.ERROR); + } + }); + Error error = apiClient.report.getError(); + assertEquals(Severity.ERROR, error.getSeverity()); + assertEquals("Name", error.getExceptionName()); + assertEquals("Message", error.getExceptionMessage()); + } + + static class FakeClient implements ErrorReportApiClient { + Report report; + + @Override + public void postReport(String urlString, + Report report, + Map headers) + throws NetworkException, BadResponseException { + this.report = report; + } + } + +} From 7e6e457675cbbdd88057c7641f4510e190a11ea4 Mon Sep 17 00:00:00 2001 From: fractalwrench Date: Mon, 23 Apr 2018 09:00:31 +0100 Subject: [PATCH 2/7] refactor: rename test methods renames client.notifyBlocking tests to signify intent --- .../java/com/bugsnag/android/ClientNotifyTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sdk/src/androidTest/java/com/bugsnag/android/ClientNotifyTest.java b/sdk/src/androidTest/java/com/bugsnag/android/ClientNotifyTest.java index 1c36bd8e51..29993c1ecf 100644 --- a/sdk/src/androidTest/java/com/bugsnag/android/ClientNotifyTest.java +++ b/sdk/src/androidTest/java/com/bugsnag/android/ClientNotifyTest.java @@ -30,13 +30,13 @@ public void setUp() throws Exception { } @Test - public void testNotifyBlocking() { + public void testNotifyBlockingDefaultSeverity() { client.notifyBlocking(new RuntimeException("Testing")); assertEquals(Severity.WARNING, apiClient.report.getError().getSeverity()); } @Test - public void testNotifyBlocking2() { + public void testNotifyBlockingCallback() { client.notifyBlocking(new RuntimeException("Testing"), new Callback() { @Override public void beforeNotify(Report report) { @@ -49,13 +49,13 @@ public void beforeNotify(Report report) { } @Test - public void testNotifyBlocking3() { + public void testNotifyBlockingCustomSeverity() { client.notifyBlocking(new RuntimeException("Testing"), Severity.INFO); assertEquals(Severity.INFO, apiClient.report.getError().getSeverity()); } @Test - public void testNotifyBlocking4() { + public void testNotifyBlockingCustomStackTrace() { StackTraceElement[] stacktrace = { new StackTraceElement("MyClass", "MyMethod", "MyFile", 5) }; From 3ff991b238ddbe4a35887d0c4d4f8363d753aa9a Mon Sep 17 00:00:00 2001 From: fractalwrench Date: Mon, 23 Apr 2018 17:08:04 +0100 Subject: [PATCH 3/7] test: add async notify test coverage improves code coverage for async notify methods --- .../com/bugsnag/android/ClientNotifyTest.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/sdk/src/androidTest/java/com/bugsnag/android/ClientNotifyTest.java b/sdk/src/androidTest/java/com/bugsnag/android/ClientNotifyTest.java index 29993c1ecf..70aab48189 100644 --- a/sdk/src/androidTest/java/com/bugsnag/android/ClientNotifyTest.java +++ b/sdk/src/androidTest/java/com/bugsnag/android/ClientNotifyTest.java @@ -1,6 +1,8 @@ package com.bugsnag.android; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import android.support.test.filters.SmallTest; import android.support.test.runner.AndroidJUnit4; @@ -10,6 +12,8 @@ import org.junit.runner.RunWith; import java.util.Map; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; @RunWith(AndroidJUnit4.class) @SmallTest @@ -20,6 +24,7 @@ public class ClientNotifyTest { /** * Generates a configuration and clears sharedPrefs values to begin the test with a clean slate + * * @throws Exception if initialisation failed */ @Before @@ -72,7 +77,55 @@ public void beforeNotify(Report report) { assertEquals("Message", error.getExceptionMessage()); } + @Test + public void testNotifyAsyncMetadata() throws Exception { + MetaData metaData = new MetaData(); + metaData.addToTab("animals", "dog", true); + + client.notify(new RuntimeException("Foo"), metaData); + apiClient.awaitReport(); + assertNotNull(apiClient.report); + MetaData data = apiClient.report.getError().getMetaData(); + assertTrue((Boolean) data.getTab("animals").get("dog")); + } + + @Test + public void testNotifyAsyncSeverity() throws Exception { + client.notify(new RuntimeException("Foo"), Severity.INFO); + apiClient.awaitReport(); + assertNotNull(apiClient.report); + assertEquals(Severity.INFO, apiClient.report.getError().getSeverity()); + } + + @Test + public void testNotifyAsyncSeverityMetadata() throws Exception { + MetaData metaData = new MetaData(); + metaData.addToTab("animals", "bird", "chicken"); + + client.notify(new RuntimeException("Foo"), Severity.ERROR, metaData); + apiClient.awaitReport(); + assertNotNull(apiClient.report); + MetaData data = apiClient.report.getError().getMetaData(); + assertEquals("chicken", data.getTab("animals").get("bird")); + assertEquals(Severity.ERROR, apiClient.report.getError().getSeverity()); + } + + @Test + public void testNotifyAsyncCallback() throws Exception { + client.notify(new RuntimeException("Foo"), new Callback() { + @Override + public void beforeNotify(Report report) { + report.getError().setContext("Manual"); + } + }); + apiClient.awaitReport(); + assertNotNull(apiClient.report); + assertEquals("Manual", apiClient.report.getError().getContext()); + } + static class FakeClient implements ErrorReportApiClient { + + CountDownLatch latch = new CountDownLatch(1); Report report; @Override @@ -81,6 +134,11 @@ public void postReport(String urlString, Map headers) throws NetworkException, BadResponseException { this.report = report; + latch.countDown(); + } + + void awaitReport() throws InterruptedException { + latch.await(100, TimeUnit.MILLISECONDS); } } From 566d06dd148d3649e9b35b403ba1c5853f9c9931 Mon Sep 17 00:00:00 2001 From: fractalwrench Date: Tue, 24 Apr 2018 17:04:48 +0100 Subject: [PATCH 4/7] test: increase countdownlatch wait increases countdownlatch wait to pass tests on ci --- .../androidTest/java/com/bugsnag/android/ClientNotifyTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/src/androidTest/java/com/bugsnag/android/ClientNotifyTest.java b/sdk/src/androidTest/java/com/bugsnag/android/ClientNotifyTest.java index 70aab48189..218184446d 100644 --- a/sdk/src/androidTest/java/com/bugsnag/android/ClientNotifyTest.java +++ b/sdk/src/androidTest/java/com/bugsnag/android/ClientNotifyTest.java @@ -138,7 +138,7 @@ public void postReport(String urlString, } void awaitReport() throws InterruptedException { - latch.await(100, TimeUnit.MILLISECONDS); + latch.await(1000, TimeUnit.MILLISECONDS); } } From 18a3f1c47ef886983931abd543f93528fcb89615 Mon Sep 17 00:00:00 2001 From: fractalwrench Date: Wed, 25 Apr 2018 10:13:36 +0100 Subject: [PATCH 5/7] increase test countdownlatch timeout again for travis ci old emulator --- .../androidTest/java/com/bugsnag/android/ClientNotifyTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/src/androidTest/java/com/bugsnag/android/ClientNotifyTest.java b/sdk/src/androidTest/java/com/bugsnag/android/ClientNotifyTest.java index 218184446d..6ab2ba809f 100644 --- a/sdk/src/androidTest/java/com/bugsnag/android/ClientNotifyTest.java +++ b/sdk/src/androidTest/java/com/bugsnag/android/ClientNotifyTest.java @@ -138,7 +138,7 @@ public void postReport(String urlString, } void awaitReport() throws InterruptedException { - latch.await(1000, TimeUnit.MILLISECONDS); + latch.await(2000, TimeUnit.MILLISECONDS); } } From 2461988ca13fc47ab30ab0d930694db94def877b Mon Sep 17 00:00:00 2001 From: fractalwrench Date: Fri, 27 Apr 2018 15:26:59 +0100 Subject: [PATCH 6/7] test: assert notify is async ensure that notify calls are async by checking the report is null after sending, but before using the countdownlatch --- .../java/com/bugsnag/android/ClientNotifyTest.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/sdk/src/androidTest/java/com/bugsnag/android/ClientNotifyTest.java b/sdk/src/androidTest/java/com/bugsnag/android/ClientNotifyTest.java index 6ab2ba809f..c73ab3dc97 100644 --- a/sdk/src/androidTest/java/com/bugsnag/android/ClientNotifyTest.java +++ b/sdk/src/androidTest/java/com/bugsnag/android/ClientNotifyTest.java @@ -2,6 +2,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import android.support.test.filters.SmallTest; @@ -83,6 +84,8 @@ public void testNotifyAsyncMetadata() throws Exception { metaData.addToTab("animals", "dog", true); client.notify(new RuntimeException("Foo"), metaData); + assertNull(apiClient.report); + apiClient.awaitReport(); assertNotNull(apiClient.report); MetaData data = apiClient.report.getError().getMetaData(); @@ -92,6 +95,8 @@ public void testNotifyAsyncMetadata() throws Exception { @Test public void testNotifyAsyncSeverity() throws Exception { client.notify(new RuntimeException("Foo"), Severity.INFO); + assertNull(apiClient.report); + apiClient.awaitReport(); assertNotNull(apiClient.report); assertEquals(Severity.INFO, apiClient.report.getError().getSeverity()); @@ -103,6 +108,8 @@ public void testNotifyAsyncSeverityMetadata() throws Exception { metaData.addToTab("animals", "bird", "chicken"); client.notify(new RuntimeException("Foo"), Severity.ERROR, metaData); + assertNull(apiClient.report); + apiClient.awaitReport(); assertNotNull(apiClient.report); MetaData data = apiClient.report.getError().getMetaData(); @@ -118,6 +125,8 @@ public void beforeNotify(Report report) { report.getError().setContext("Manual"); } }); + assertNull(apiClient.report); + apiClient.awaitReport(); assertNotNull(apiClient.report); assertEquals("Manual", apiClient.report.getError().getContext()); @@ -133,6 +142,11 @@ public void postReport(String urlString, Report report, Map headers) throws NetworkException, BadResponseException { + try { + Thread.sleep(1); // simulate async request + } catch (InterruptedException e) { + e.printStackTrace(); + } this.report = report; latch.countDown(); } From 5a6694e6556eeda6a1c14e11c04e7d3c89816d7c Mon Sep 17 00:00:00 2001 From: fractalwrench Date: Fri, 27 Apr 2018 15:40:20 +0100 Subject: [PATCH 7/7] appease the checkstyle gods --- .../java/com/bugsnag/android/ClientNotifyTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sdk/src/androidTest/java/com/bugsnag/android/ClientNotifyTest.java b/sdk/src/androidTest/java/com/bugsnag/android/ClientNotifyTest.java index c73ab3dc97..975a73ead5 100644 --- a/sdk/src/androidTest/java/com/bugsnag/android/ClientNotifyTest.java +++ b/sdk/src/androidTest/java/com/bugsnag/android/ClientNotifyTest.java @@ -144,8 +144,8 @@ public void postReport(String urlString, throws NetworkException, BadResponseException { try { Thread.sleep(1); // simulate async request - } catch (InterruptedException e) { - e.printStackTrace(); + } catch (InterruptedException ignored) { + ignored.printStackTrace(); } this.report = report; latch.countDown();