Skip to content

Commit 0cef46e

Browse files
committed
improved handling of invalid JSON files
This also fixes an exception which was thrown if the url points to a non existing file.
1 parent 9b3d819 commit 0cef46e

4 files changed

Lines changed: 20 additions & 0 deletions

File tree

android-remote-notifications/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ android {
1818
compileOptions {
1919
encoding "UTF-8"
2020
}
21+
testOptions {
22+
unitTests.returnDefaultValues = true
23+
}
2124
}
2225

2326
dependencies {

android-remote-notifications/src/main/java/com/github/kaiwinter/androidremotenotifications/json/UnMarshaller.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.github.kaiwinter.androidremotenotifications.json;
22

3+
import android.util.Log;
4+
5+
import com.github.kaiwinter.androidremotenotifications.RemoteNotifications;
36
import com.github.kaiwinter.androidremotenotifications.model.UserNotification;
47
import com.github.kaiwinter.androidremotenotifications.model.buttonaction.ButtonAction;
58
import com.github.kaiwinter.androidremotenotifications.model.buttonaction.impl.ExitAppButtonAction;
@@ -10,6 +13,8 @@
1013
import com.github.kaiwinter.androidremotenotifications.model.impl.ToastNotification;
1114
import com.google.gson.Gson;
1215
import com.google.gson.GsonBuilder;
16+
import com.google.gson.JsonIOException;
17+
import com.google.gson.JsonSyntaxException;
1318
import com.google.gson.reflect.TypeToken;
1419
import com.google.gson.typeadapters.RuntimeTypeAdapterFactory;
1520

@@ -18,6 +23,7 @@
1823
import java.io.Reader;
1924
import java.lang.reflect.Type;
2025
import java.net.URL;
26+
import java.util.Collections;
2127
import java.util.Set;
2228

2329
/**
@@ -65,6 +71,9 @@ public static Set<UserNotification> getNotificationsFromJson(URL url) throws IOE
6571
Type listType = new TypeToken<Set<UserNotification>>() {
6672
}.getType();
6773
return createGson().fromJson(reader, listType);
74+
} catch (JsonSyntaxException | JsonIOException e) {
75+
Log.e(RemoteNotifications.TAG, e.getMessage() + ": " + url);
76+
return Collections.emptySet();
6877
} finally {
6978
if (reader != null) {
7079
reader.close();

android-remote-notifications/src/test/java/com/github/kaiwinter/androidremotenotifications/json/Notification2Json2NotificationTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ public void testNotificationFromURL() throws IOException {
2626
assertEquals(1, notificationsFromJson.size());
2727
}
2828

29+
@Test
30+
public void testNotificationFromURLInvalidAnswer() throws IOException {
31+
URL url = Notification2Json2NotificationTest.class.getResource("/com/github/kaiwinter/androidremotenotifications/invalid_answer");
32+
Set<UserNotification> notificationsFromJson = UnMarshaller.getNotificationsFromJson(url);
33+
assertEquals(0, notificationsFromJson.size());
34+
}
35+
2936
@Test
3037
public void testPersistentNotification() {
3138
NotificationConfiguration notificationConfiguration = new NotificationConfiguration();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
404 - not found

0 commit comments

Comments
 (0)