Skip to content
This repository has been archived by the owner on Apr 5, 2022. It is now read-only.

Commit

Permalink
Merge pull request #36 from habuma/master
Browse files Browse the repository at this point in the history
Fixed null/existence checking in FqlResult. SOCIALFB-75
  • Loading branch information
habuma committed Apr 16, 2012
2 parents ca1289e + 300f7e0 commit 0e4c129
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 12 deletions.
Expand Up @@ -42,7 +42,7 @@ public FqlResult(Map<String, Object> resultMap) {
* @return the value of the field as a String
*/
public String getString(String fieldName) {
return resultMap.containsKey(fieldName) ? String.valueOf(resultMap.get(fieldName)) : null;
return hasValue(fieldName) ? String.valueOf(resultMap.get(fieldName)) : null;
}

/**
Expand All @@ -53,7 +53,7 @@ public String getString(String fieldName) {
*/
public Integer getInteger(String fieldName) {
try {
return resultMap.containsKey(fieldName) ? Integer.valueOf(String.valueOf(resultMap.get(fieldName))) : null;
return hasValue(fieldName) ? Integer.valueOf(String.valueOf(resultMap.get(fieldName))) : null;
} catch (NumberFormatException e) {
throw new FqlException("Field '" + fieldName +"' is not a number.", e);
}
Expand All @@ -67,7 +67,7 @@ public Integer getInteger(String fieldName) {
*/
public Long getLong(String fieldName) {
try {
return resultMap.containsKey(fieldName) ? Long.valueOf(String.valueOf(resultMap.get(fieldName))) : null;
return hasValue(fieldName) ? Long.valueOf(String.valueOf(resultMap.get(fieldName))) : null;
} catch (NumberFormatException e) {
throw new FqlException("Field '" + fieldName +"' is not a number.", e);
}
Expand All @@ -81,7 +81,7 @@ public Long getLong(String fieldName) {
*/
public Float getFloat(String fieldName) {
try {
return resultMap.containsKey(fieldName) ? Float.valueOf(String.valueOf(resultMap.get(fieldName))) : null;
return hasValue(fieldName) ? Float.valueOf(String.valueOf(resultMap.get(fieldName))) : null;
} catch (NumberFormatException e) {
throw new FqlException("Field '" + fieldName +"' is not a number.", e);
}
Expand All @@ -93,7 +93,7 @@ public Float getFloat(String fieldName) {
* @return the value of the field as a Boolean
*/
public Boolean getBoolean(String fieldName) {
return resultMap.containsKey(fieldName) ? Boolean.valueOf(String.valueOf(resultMap.get(fieldName))) : null;
return hasValue(fieldName) ? Boolean.valueOf(String.valueOf(resultMap.get(fieldName))) : null;
}

/**
Expand All @@ -105,9 +105,8 @@ public Boolean getBoolean(String fieldName) {
*/
public Date getTime(String fieldName) {
try {
if (resultMap.get(fieldName) != null) {
long timeInMilliseconds = Long.valueOf(String.valueOf(resultMap.get(fieldName))) * 1000;
return new Date(timeInMilliseconds);
if (hasValue(fieldName)) {
return new Date(Long.valueOf(String.valueOf(resultMap.get(fieldName))) * 1000);
} else {
return null;
}
Expand All @@ -133,7 +132,7 @@ public Object getObject(String fieldName) {
* @throws FqlException if the value of the field is not a nested object.
*/
public <T> T getObject(String fieldName, FqlResultMapper<T> mapper) {
if (!resultMap.containsKey(fieldName)) {
if (!hasValue(fieldName)) {
return null;
}
try {
Expand All @@ -153,7 +152,7 @@ public <T> T getObject(String fieldName, FqlResultMapper<T> mapper) {
* @throws FqlException if the value of the field is not a list.
*/
public <T> List<T> getList(String fieldName, FqlResultMapper<T> mapper) {
if (!resultMap.containsKey(fieldName)) {
if (!hasValue(fieldName)) {
return null;
}
try {
Expand All @@ -169,4 +168,22 @@ public <T> List<T> getList(String fieldName, FqlResultMapper<T> mapper) {
}
}

/**
* Checks for the existence of a field in the result set, whether null or non-null.
* @param fieldName the name of the field to check existence of.
* @return true if the field exists in the result set, even if the value is null; false if the field is not in the result set.
*/
public boolean hasField(String fieldName) {
return resultMap.containsKey(fieldName);
}

/**
* Checks that a field exists and contains a non-null value.
* @param fieldName the name of the field to check existence/value of.
* @return true if the field exists in the result set and has a non-null value; false otherwise.
*/
public boolean hasValue(String fieldName) {
return resultMap.containsKey(fieldName) && resultMap.get(fieldName) != null;
}

}
Expand Up @@ -130,8 +130,6 @@ public RestrictionObject mapObject(FqlResult result) {
assertEquals("18", restriction.age);
assertEquals("18+", restriction.ageDistribution);
}



@Test
public void query_resultsWithAnObjectArrayField() {
Expand Down Expand Up @@ -169,6 +167,62 @@ public FamilyMemberObject mapObject(FqlResult result) {
assertEquals(12345678904L, member4.uid);
assertEquals("sister", member4.relationship);
}

@Test
public void nullChecks() {
mockServer.expect(requestTo("https://graph.facebook.com/fql?q=select+stuff+from+somewhere+where+uid%3Dme%28%29"))
.andExpect(method(GET))
.andExpect(header("Authorization", "OAuth someAccessToken"))
.andRespond(withResponse(jsonResource("testdata/fql-with-nulls"), responseHeaders));

facebook.fqlOperations().query("select stuff from somewhere where uid=me()", new FqlResultMapper<Object>() {
public Object mapObject(FqlResult result) {
assertNotNull(result.getString("string"));
assertNull(result.getString("string_null"));
assertNotNull(result.getInteger("number"));
assertNull(result.getInteger("number_null"));
assertNotNull(result.getLong("number"));
assertNull(result.getLong("number_null"));
assertNotNull(result.getFloat("float"));
assertNull(result.getFloat("float_null"));
assertNotNull(result.getBoolean("boolean"));
assertNull(result.getBoolean("boolean_null"));
assertNotNull(result.getTime("time"));
assertNull(result.getTime("time_null"));
assertNotNull(result.getList("list", new FqlResultMapper<String>() {
public String mapObject(FqlResult objectValues) {
return null;
}
}));
assertNotNull(result.getList("list_empty", new FqlResultMapper<String>() {
public String mapObject(FqlResult objectValues) {
return null;
}
}));
assertNull(result.getList("list_null", new FqlResultMapper<String>() {
public String mapObject(FqlResult objectValues) {
return null;
}
}));
assertNotNull(result.getObject("object"));
assertEquals("someValue", result.getObject("object", new FqlResultMapper<String>() {
public String mapObject(FqlResult objectValues) {
return objectValues.getString("fieldA");
}
}));
assertNull(result.getObject("object_null"));
assertNull(result.getObject("object_null", new FqlResultMapper<String>() {
public String mapObject(FqlResult objectValues) {
fail("mapObjects() shouldn't be called for a null value");
return null;
}
}));
return null;
}
});


}

private static class StatusObject {
public int uid;
Expand Down
@@ -0,0 +1,33 @@
{
"data":[
{
"string":"Test",
"string_null":null,
"number":12345,
"numer_null":null,
"float":12345.67,
"float_null":null,
"boolean":true,
"boolean_null":null,
"time": 1323184190,
"time_null":null,
"list": [
{
"fieldA":"someValue",
"fieldB":12345678901
},
{
"fieldA":"someOtherValue",
"fieldB":12345678902
}
],
"list_empty":[],
"list_null":null,
"object":{
"fieldA":"someValue",
"fieldB":1234567901
},
"object_null":null
}
]
}

0 comments on commit 0e4c129

Please sign in to comment.