aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJulien Dramaix <julien.dramaix@gmail.com>2015-07-20 10:55:47 +0200
committerJulien Dramaix <julien.dramaix@gmail.com>2015-07-20 10:55:47 +0200
commitb1728c2e9d0de9aedd8e837cee87386b71cf1d24 (patch)
treeaa1873b998a0d59da8850749729f46731fe64556
parent5c5aa479ae313a274944effc47f67fa7a6a97396 (diff)
parent9cf3566409ab957139fd6cff2d9b3ea850dab7d1 (diff)
downloadgwtquery-b1728c2e9d0de9aedd8e837cee87386b71cf1d24.tar.gz
gwtquery-b1728c2e9d0de9aedd8e837cee87386b71cf1d24.zip
Merge pull request #356 from apanizo/master
Data binding over list attributes
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/vm/JsonBuilderHandler.java8
-rw-r--r--gwtquery-core/src/test/java/com/google/gwt/query/client/dbinding/DataBindingTestJre.java42
2 files changed, 47 insertions, 3 deletions
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/vm/JsonBuilderHandler.java b/gwtquery-core/src/main/java/com/google/gwt/query/vm/JsonBuilderHandler.java
index adbdd233..395a38a3 100644
--- a/gwtquery-core/src/main/java/com/google/gwt/query/vm/JsonBuilderHandler.java
+++ b/gwtquery-core/src/main/java/com/google/gwt/query/vm/JsonBuilderHandler.java
@@ -29,6 +29,7 @@ import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Hashtable;
@@ -66,11 +67,16 @@ public class JsonBuilderHandler implements InvocationHandler {
@SuppressWarnings("unchecked")
private <T> Object jsonArrayToList(JsonArray j, Class<T> ctype, boolean isArray) {
+ if (j == null) {
+ return null;
+ }
+
List<T> l = new ArrayList<T>();
for (int i = 0; j != null && i < j.length(); i++) {
l.add((T) getValue(j, i, null, null, ctype, null));
}
- return l.isEmpty() ? null : isArray ? l.toArray((T[]) Array.newInstance(ctype, l.size())) : l;
+
+ return l.isEmpty() ? Collections.emptyList() : isArray ? l.toArray((T[]) Array.newInstance(ctype, l.size())) : l;
}
private Double toDouble(String attr, JsonArray arr, int idx, JsonObject obj) {
diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/dbinding/DataBindingTestJre.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/dbinding/DataBindingTestJre.java
index 50123b80..f276f034 100644
--- a/gwtquery-core/src/test/java/com/google/gwt/query/client/dbinding/DataBindingTestJre.java
+++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/dbinding/DataBindingTestJre.java
@@ -26,6 +26,7 @@ import com.google.gwt.query.client.builders.JsonBuilder;
import com.google.gwt.query.client.builders.Name;
import java.util.Arrays;
+import java.util.Collections;
import java.util.Date;
import java.util.List;
@@ -165,6 +166,7 @@ public class DataBindingTestJre extends GWTTestCase {
int getAge();
String getName();
+ List<String> getPhones();
GAddress address();
}
@@ -173,9 +175,13 @@ public class DataBindingTestJre extends GWTTestCase {
+ " 'email': 'foo@bar.com', "
+ " 'age': 27, "
+ " 'name': 'Foo Bar', "
+ + " 'phones': [ "
+ + " '9166566',"
+ + " '65443333'"
+ + " ],"
+ " 'address': {"
+ " 'street': 'Street Foo N6', "
- + " 'phone': '670'"
+ + " 'number': '670'"
+ " }"
+ "}";
@@ -189,7 +195,7 @@ public class DataBindingTestJre extends GWTTestCase {
assertEquals("Foo Bar", entity.getName());
assertNotNull(entity.address());
assertEquals("Street Foo N6", entity.address().street());
- assertNotNull(entity.address().get("phone"));
+ assertNotNull(entity.address().get("number"));
}
// Nested strict not implemented in JS
@@ -211,4 +217,36 @@ public class DataBindingTestJre extends GWTTestCase {
assertNull(entity.address().get("phone"));
}
}
+
+ public void test_return_empty_list_when_array_isEmpty() {
+ //GIVEN a JSON representation of a user without phones
+ GUser user = GQ.create(GUser.class);
+ user.set("email", "a@b.com");
+ user.set("name", "Random Name");
+ user.set("phones", Collections.emptyList());
+ String json = user.toJson();
+
+ //WHEN fetching that user
+ GUser retrievedUser = GQ.create(GUser.class);
+ retrievedUser.parse(json, true);
+
+ //THEN
+ assertEquals(0, retrievedUser.getPhones().size());
+ }
+
+ public void test_return_null_when_list_is_not_specified() {
+ //GIVEN a JSON representation of a user
+ GUser user = GQ.create(GUser.class);
+ user.set("email", "a@b.com");
+ user.set("name", "Random Name");
+ String json = user.toJson();
+
+ //WHEN fetching that user
+ GUser retrievedUser = GQ.create(GUser.class);
+ retrievedUser.parse(json, true);
+
+ //THEN
+ List<String> phones = retrievedUser.getPhones();
+ assertNull(phones);
+ }
} \ No newline at end of file