Skip to content

Commit 15aeb9c

Browse files
committed
Merge branch 'develop' into feature/site-settings-review
2 parents 38c680b + 82f2229 commit 15aeb9c

File tree

2 files changed

+55
-11
lines changed

2 files changed

+55
-11
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.wordpress.android.util;
2+
3+
import android.test.InstrumentationTestCase;
4+
5+
import org.json.JSONArray;
6+
import org.json.JSONObject;
7+
8+
public class JSONUtilsTest extends InstrumentationTestCase {
9+
public void testQueryJSONNullSource1() {
10+
JSONUtils.queryJSON((JSONObject) null, "", "");
11+
}
12+
13+
public void testQueryJSONNullSource2() {
14+
JSONUtils.queryJSON((JSONArray) null, "", "");
15+
}
16+
17+
public void testQueryJSONNullQuery1() {
18+
JSONUtils.queryJSON(new JSONObject(), null, "");
19+
}
20+
21+
public void testQueryJSONNullQuery2() {
22+
JSONUtils.queryJSON(new JSONArray(), null, "");
23+
}
24+
25+
public void testQueryJSONNullReturnValue1() {
26+
JSONUtils.queryJSON(new JSONObject(), "", null);
27+
}
28+
29+
public void testQueryJSONNullReturnValue2() {
30+
JSONUtils.queryJSON(new JSONArray(), "", null);
31+
}
32+
}

WordPressUtils/src/main/java/org/wordpress/android/util/JSONUtils.java

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,30 @@
88
import org.wordpress.android.util.AppLog.T;
99

1010
import java.util.ArrayList;
11-
import java.util.Iterator;
1211

1312
public class JSONUtils {
14-
private static String QUERY_SEPERATOR=".";
15-
private static String QUERY_ARRAY_INDEX_START="[";
16-
private static String QUERY_ARRAY_INDEX_END="]";
17-
private static String QUERY_ARRAY_FIRST="first";
18-
private static String QUERY_ARRAY_LAST="last";
13+
private static String QUERY_SEPERATOR = ".";
14+
private static String QUERY_ARRAY_INDEX_START = "[";
15+
private static String QUERY_ARRAY_INDEX_END = "]";
16+
private static String QUERY_ARRAY_FIRST = "first";
17+
private static String QUERY_ARRAY_LAST = "last";
1918

2019
private static final String JSON_NULL_STR = "null";
20+
private static final String TAG = "JSONUtils";
2121

22-
private static final String TAG="JSONUtils";
2322
/**
2423
* Given a JSONObject and a key path (e.g property.child) and a default it will
2524
* traverse the object graph and pull out the desired property
2625
*/
2726
public static <U> U queryJSON(JSONObject source, String query, U defaultObject) {
27+
if (source == null) {
28+
AppLog.e(T.UTILS, "Parameter source is null, can't query a null object");
29+
return defaultObject;
30+
}
31+
if (query == null) {
32+
AppLog.e(T.UTILS, "Parameter query is null");
33+
return defaultObject;
34+
}
2835
int nextSeperator = query.indexOf(QUERY_SEPERATOR);
2936
int nextIndexStart = query.indexOf(QUERY_ARRAY_INDEX_START);
3037
if (nextSeperator == -1 && nextIndexStart == -1) {
@@ -56,9 +63,6 @@ public static <U> U queryJSON(JSONObject source, String query, U defaultObject)
5663
String nextQuery = query.substring(endQuery);
5764
String key = query.substring(0, endQuery);
5865
try {
59-
if (source == null) {
60-
return defaultObject;
61-
}
6266
if (nextQuery.indexOf(QUERY_SEPERATOR) == 0) {
6367
return queryJSON(source.getJSONObject(key), nextQuery.substring(1), defaultObject);
6468
} else if (nextQuery.indexOf(QUERY_ARRAY_INDEX_START) == 0) {
@@ -89,7 +93,15 @@ public static <U> U queryJSON(JSONObject source, String query, U defaultObject)
8993
* Acceptable indexes include negative numbers to reference items from the end of
9094
* the list as well as "last" and "first" as more explicit references to "0" and "-1"
9195
*/
92-
public static <U> U queryJSON(JSONArray source, String query, U defaultObject){
96+
public static <U> U queryJSON(JSONArray source, String query, U defaultObject) {
97+
if (source == null) {
98+
AppLog.e(T.UTILS, "Parameter source is null, can't query a null object");
99+
return defaultObject;
100+
}
101+
if (query == null) {
102+
AppLog.e(T.UTILS, "Parameter query is null");
103+
return defaultObject;
104+
}
93105
// query must start with [ have an index and then have ]
94106
int indexStart = query.indexOf(QUERY_ARRAY_INDEX_START);
95107
int indexEnd = query.indexOf(QUERY_ARRAY_INDEX_END);

0 commit comments

Comments
 (0)