From c8f264d6f8e63709858b3a2e58d6562bae213ee4 Mon Sep 17 00:00:00 2001 From: Nikita Korshak Date: Thu, 15 Aug 2019 10:18:25 +0300 Subject: [PATCH 1/2] check params and init client before setting base url --- CHANGELOG.md | 3 +++ .../android/telemetry/MapboxTelemetry.java | 4 ++-- .../android/telemetry/MapboxTelemetryTest.java | 18 ++++++++++++++++-- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5044b584a..bdff4f560 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ Mapbox welcomes participation and contributions from everyone. ## Mapbox Android Telemetry +## v4.6.1 +- Fix crash `setBaseUrl()` on null `TelemetryClient` [#423](https://github.com/mapbox/mapbox-events-android/pull/423) + ## v4.6.0 - Add public api to change base url for telemetry endpoint [#420](https://github.com/mapbox/mapbox-events-android/pull/420) - Telemetry metrics [#397](https://github.com/mapbox/mapbox-events-android/pull/397) diff --git a/libtelemetry/src/full/java/com/mapbox/android/telemetry/MapboxTelemetry.java b/libtelemetry/src/full/java/com/mapbox/android/telemetry/MapboxTelemetry.java index facf3eefa..777da2343 100644 --- a/libtelemetry/src/full/java/com/mapbox/android/telemetry/MapboxTelemetry.java +++ b/libtelemetry/src/full/java/com/mapbox/android/telemetry/MapboxTelemetry.java @@ -479,8 +479,8 @@ public Thread newThread(Runnable runnable) { } @SuppressWarnings("WeakerAccess") - public void setBaseUrl(String eventsHost) { - if (isValidUrl(eventsHost)) { + public synchronized void setBaseUrl(String eventsHost) { + if (isValidUrl(eventsHost) && checkNetworkAndParameters()) { telemetryClient.setBaseUrl(eventsHost); } } diff --git a/libtelemetry/src/testFull/java/com/mapbox/android/telemetry/MapboxTelemetryTest.java b/libtelemetry/src/testFull/java/com/mapbox/android/telemetry/MapboxTelemetryTest.java index f299d89d8..890d2a239 100644 --- a/libtelemetry/src/testFull/java/com/mapbox/android/telemetry/MapboxTelemetryTest.java +++ b/libtelemetry/src/testFull/java/com/mapbox/android/telemetry/MapboxTelemetryTest.java @@ -403,13 +403,27 @@ public void checkFlushIsCalled() { } @Test - public void checksSetBaseUrlWithValidHost() throws Exception { + public void checksSetBaseUrlWithValidHostAndWithConnection() throws Exception { + Context mockedContext = obtainNetworkConnectedMockedContext(); TelemetryClient mockedTelemetryClient = mock(TelemetryClient.class); - MapboxTelemetry theMapboxTelemetry = obtainMapboxTelemetryWith(mockedTelemetryClient); + Callback mockedHttpCallback = mock(Callback.class); + MapboxTelemetry theMapboxTelemetry = obtainMapboxTelemetryWith(mockedContext, mockedTelemetryClient, + mockedHttpCallback); theMapboxTelemetry.setBaseUrl(DEFAULT_STAGING_EVENTS_HOST); verify(mockedTelemetryClient, times(1)).setBaseUrl(eq(DEFAULT_STAGING_EVENTS_HOST)); } + @Test + public void checksSetBaseUrlWithValidHostAndWithoutConnection() throws Exception { + Context mockedContext = obtainNetworkNotConnectedMockedContext(); + TelemetryClient mockedTelemetryClient = mock(TelemetryClient.class); + Callback mockedHttpCallback = mock(Callback.class); + MapboxTelemetry theMapboxTelemetry = obtainMapboxTelemetryWith(mockedContext, mockedTelemetryClient, + mockedHttpCallback); + theMapboxTelemetry.setBaseUrl(DEFAULT_STAGING_EVENTS_HOST); + verify(mockedTelemetryClient, never()).setBaseUrl(eq(DEFAULT_STAGING_EVENTS_HOST)); + } + @Test public void checksSetBaseUrlWithNullHost() throws Exception { TelemetryClient mockedTelemetryClient = mock(TelemetryClient.class); From e977ddb8e8b796ba3bab03c4348f8085551f4e30 Mon Sep 17 00:00:00 2001 From: Nikita Korshak Date: Tue, 8 Oct 2019 17:19:50 +0300 Subject: [PATCH 2/2] change setBaseUrl() to return boolean --- .../android/telemetry/MapboxTelemetry.java | 4 +++- .../android/telemetry/MapboxTelemetryTest.java | 18 +++++++++--------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/libtelemetry/src/full/java/com/mapbox/android/telemetry/MapboxTelemetry.java b/libtelemetry/src/full/java/com/mapbox/android/telemetry/MapboxTelemetry.java index 777da2343..3ba47cfe5 100644 --- a/libtelemetry/src/full/java/com/mapbox/android/telemetry/MapboxTelemetry.java +++ b/libtelemetry/src/full/java/com/mapbox/android/telemetry/MapboxTelemetry.java @@ -479,10 +479,12 @@ public Thread newThread(Runnable runnable) { } @SuppressWarnings("WeakerAccess") - public synchronized void setBaseUrl(String eventsHost) { + public synchronized boolean setBaseUrl(String eventsHost) { if (isValidUrl(eventsHost) && checkNetworkAndParameters()) { telemetryClient.setBaseUrl(eventsHost); + return true; } + return false; } private static boolean isValidUrl(String eventsHost) { diff --git a/libtelemetry/src/testFull/java/com/mapbox/android/telemetry/MapboxTelemetryTest.java b/libtelemetry/src/testFull/java/com/mapbox/android/telemetry/MapboxTelemetryTest.java index 890d2a239..cd7efccf4 100644 --- a/libtelemetry/src/testFull/java/com/mapbox/android/telemetry/MapboxTelemetryTest.java +++ b/libtelemetry/src/testFull/java/com/mapbox/android/telemetry/MapboxTelemetryTest.java @@ -409,7 +409,7 @@ public void checksSetBaseUrlWithValidHostAndWithConnection() throws Exception { Callback mockedHttpCallback = mock(Callback.class); MapboxTelemetry theMapboxTelemetry = obtainMapboxTelemetryWith(mockedContext, mockedTelemetryClient, mockedHttpCallback); - theMapboxTelemetry.setBaseUrl(DEFAULT_STAGING_EVENTS_HOST); + assertTrue(theMapboxTelemetry.setBaseUrl(DEFAULT_STAGING_EVENTS_HOST)); verify(mockedTelemetryClient, times(1)).setBaseUrl(eq(DEFAULT_STAGING_EVENTS_HOST)); } @@ -420,7 +420,7 @@ public void checksSetBaseUrlWithValidHostAndWithoutConnection() throws Exception Callback mockedHttpCallback = mock(Callback.class); MapboxTelemetry theMapboxTelemetry = obtainMapboxTelemetryWith(mockedContext, mockedTelemetryClient, mockedHttpCallback); - theMapboxTelemetry.setBaseUrl(DEFAULT_STAGING_EVENTS_HOST); + assertFalse(theMapboxTelemetry.setBaseUrl(DEFAULT_STAGING_EVENTS_HOST)); verify(mockedTelemetryClient, never()).setBaseUrl(eq(DEFAULT_STAGING_EVENTS_HOST)); } @@ -428,7 +428,7 @@ public void checksSetBaseUrlWithValidHostAndWithoutConnection() throws Exception public void checksSetBaseUrlWithNullHost() throws Exception { TelemetryClient mockedTelemetryClient = mock(TelemetryClient.class); MapboxTelemetry theMapboxTelemetry = obtainMapboxTelemetryWith(mockedTelemetryClient); - theMapboxTelemetry.setBaseUrl(null); + assertFalse(theMapboxTelemetry.setBaseUrl(null)); verify(mockedTelemetryClient, never()).setBaseUrl(any(String.class)); } @@ -436,7 +436,7 @@ public void checksSetBaseUrlWithNullHost() throws Exception { public void checksSetBaseUrlWithEmptyHost() throws Exception { TelemetryClient mockedTelemetryClient = mock(TelemetryClient.class); MapboxTelemetry theMapboxTelemetry = obtainMapboxTelemetryWith(mockedTelemetryClient); - theMapboxTelemetry.setBaseUrl(""); + assertFalse(theMapboxTelemetry.setBaseUrl("")); verify(mockedTelemetryClient, never()).setBaseUrl(any(String.class)); } @@ -444,7 +444,7 @@ public void checksSetBaseUrlWithEmptyHost() throws Exception { public void checksSetBaseUrlWithInvalidHostOne() throws Exception { TelemetryClient mockedTelemetryClient = mock(TelemetryClient.class); MapboxTelemetry theMapboxTelemetry = obtainMapboxTelemetryWith(mockedTelemetryClient); - theMapboxTelemetry.setBaseUrl("h@st.com"); + assertFalse(theMapboxTelemetry.setBaseUrl("h@st.com")); verify(mockedTelemetryClient, never()).setBaseUrl(any(String.class)); } @@ -452,7 +452,7 @@ public void checksSetBaseUrlWithInvalidHostOne() throws Exception { public void checksSetBaseUrlWithInvalidHostTwo() throws Exception { TelemetryClient mockedTelemetryClient = mock(TelemetryClient.class); MapboxTelemetry theMapboxTelemetry = obtainMapboxTelemetryWith(mockedTelemetryClient); - theMapboxTelemetry.setBaseUrl("new host.com"); + assertFalse(theMapboxTelemetry.setBaseUrl("new host.com")); verify(mockedTelemetryClient, never()).setBaseUrl(any(String.class)); } @@ -460,7 +460,7 @@ public void checksSetBaseUrlWithInvalidHostTwo() throws Exception { public void checksSetBaseUrlWithInvalidHostThree() throws Exception { TelemetryClient mockedTelemetryClient = mock(TelemetryClient.class); MapboxTelemetry theMapboxTelemetry = obtainMapboxTelemetryWith(mockedTelemetryClient); - theMapboxTelemetry.setBaseUrl("host..com"); + assertFalse(theMapboxTelemetry.setBaseUrl("host..com")); verify(mockedTelemetryClient, never()).setBaseUrl(any(String.class)); } @@ -468,7 +468,7 @@ public void checksSetBaseUrlWithInvalidHostThree() throws Exception { public void checksSetBaseUrlWithInvalidHostFour() throws Exception { TelemetryClient mockedTelemetryClient = mock(TelemetryClient.class); MapboxTelemetry theMapboxTelemetry = obtainMapboxTelemetryWith(mockedTelemetryClient); - theMapboxTelemetry.setBaseUrl("host.c"); + assertFalse(theMapboxTelemetry.setBaseUrl("host.c")); verify(mockedTelemetryClient, never()).setBaseUrl(any(String.class)); } @@ -476,7 +476,7 @@ public void checksSetBaseUrlWithInvalidHostFour() throws Exception { public void checksSetBaseUrlWithInvalidHostFive() throws Exception { TelemetryClient mockedTelemetryClient = mock(TelemetryClient.class); MapboxTelemetry theMapboxTelemetry = obtainMapboxTelemetryWith(mockedTelemetryClient); - theMapboxTelemetry.setBaseUrl("host.com."); + assertFalse(theMapboxTelemetry.setBaseUrl("host.com.")); verify(mockedTelemetryClient, never()).setBaseUrl(any(String.class)); }