diff --git a/WordPressUtils/src/androidTest/java/org/wordpress/android/util/JSONUtilsTest.java b/WordPressUtils/src/androidTest/java/org/wordpress/android/util/JSONUtilsTest.java new file mode 100644 index 000000000000..f7c747ff7024 --- /dev/null +++ b/WordPressUtils/src/androidTest/java/org/wordpress/android/util/JSONUtilsTest.java @@ -0,0 +1,32 @@ +package org.wordpress.android.util; + +import android.test.InstrumentationTestCase; + +import org.json.JSONArray; +import org.json.JSONObject; + +public class JSONUtilsTest extends InstrumentationTestCase { + public void testQueryJSONNullSource1() { + JSONUtils.queryJSON((JSONObject) null, "", ""); + } + + public void testQueryJSONNullSource2() { + JSONUtils.queryJSON((JSONArray) null, "", ""); + } + + public void testQueryJSONNullQuery1() { + JSONUtils.queryJSON(new JSONObject(), null, ""); + } + + public void testQueryJSONNullQuery2() { + JSONUtils.queryJSON(new JSONArray(), null, ""); + } + + public void testQueryJSONNullReturnValue1() { + JSONUtils.queryJSON(new JSONObject(), "", null); + } + + public void testQueryJSONNullReturnValue2() { + JSONUtils.queryJSON(new JSONArray(), "", null); + } +} diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/JSONUtils.java b/WordPressUtils/src/main/java/org/wordpress/android/util/JSONUtils.java index 4c92478cb4d7..8738acd24384 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/JSONUtils.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/JSONUtils.java @@ -8,23 +8,30 @@ import org.wordpress.android.util.AppLog.T; import java.util.ArrayList; -import java.util.Iterator; public class JSONUtils { - private static String QUERY_SEPERATOR="."; - private static String QUERY_ARRAY_INDEX_START="["; - private static String QUERY_ARRAY_INDEX_END="]"; - private static String QUERY_ARRAY_FIRST="first"; - private static String QUERY_ARRAY_LAST="last"; + private static String QUERY_SEPERATOR = "."; + private static String QUERY_ARRAY_INDEX_START = "["; + private static String QUERY_ARRAY_INDEX_END = "]"; + private static String QUERY_ARRAY_FIRST = "first"; + private static String QUERY_ARRAY_LAST = "last"; private static final String JSON_NULL_STR = "null"; + private static final String TAG = "JSONUtils"; - private static final String TAG="JSONUtils"; /** * Given a JSONObject and a key path (e.g property.child) and a default it will * traverse the object graph and pull out the desired property */ public static U queryJSON(JSONObject source, String query, U defaultObject) { + if (source == null) { + AppLog.e(T.UTILS, "Parameter source is null, can't query a null object"); + return defaultObject; + } + if (query == null) { + AppLog.e(T.UTILS, "Parameter query is null"); + return defaultObject; + } int nextSeperator = query.indexOf(QUERY_SEPERATOR); int nextIndexStart = query.indexOf(QUERY_ARRAY_INDEX_START); if (nextSeperator == -1 && nextIndexStart == -1) { @@ -56,9 +63,6 @@ public static U queryJSON(JSONObject source, String query, U defaultObject) String nextQuery = query.substring(endQuery); String key = query.substring(0, endQuery); try { - if (source == null) { - return defaultObject; - } if (nextQuery.indexOf(QUERY_SEPERATOR) == 0) { return queryJSON(source.getJSONObject(key), nextQuery.substring(1), defaultObject); } else if (nextQuery.indexOf(QUERY_ARRAY_INDEX_START) == 0) { @@ -89,7 +93,15 @@ public static U queryJSON(JSONObject source, String query, U defaultObject) * Acceptable indexes include negative numbers to reference items from the end of * the list as well as "last" and "first" as more explicit references to "0" and "-1" */ - public static U queryJSON(JSONArray source, String query, U defaultObject){ + public static U queryJSON(JSONArray source, String query, U defaultObject) { + if (source == null) { + AppLog.e(T.UTILS, "Parameter source is null, can't query a null object"); + return defaultObject; + } + if (query == null) { + AppLog.e(T.UTILS, "Parameter query is null"); + return defaultObject; + } // query must start with [ have an index and then have ] int indexStart = query.indexOf(QUERY_ARRAY_INDEX_START); int indexEnd = query.indexOf(QUERY_ARRAY_INDEX_END);