You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DataBindingTestJre.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright 2013, The gwtquery team.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.google.gwt.query.client.dbinding;
  17. import com.google.gwt.core.shared.GWT;
  18. import com.google.gwt.junit.DoNotRunWith;
  19. import com.google.gwt.junit.Platform;
  20. import com.google.gwt.junit.client.GWTTestCase;
  21. import com.google.gwt.query.client.Function;
  22. import com.google.gwt.query.client.GQ;
  23. import com.google.gwt.query.client.IsProperties;
  24. import com.google.gwt.query.client.builders.JsonBuilder;
  25. import com.google.gwt.query.client.builders.Name;
  26. import java.util.Arrays;
  27. import java.util.Collections;
  28. import java.util.Date;
  29. import java.util.List;
  30. /**
  31. * Tests for Deferred which can run either in JVM and GWT
  32. */
  33. public class DataBindingTestJre extends GWTTestCase {
  34. public String getModuleName() {
  35. return null;
  36. }
  37. public void testPropertiesCreate() {
  38. IsProperties p1 = GQ.create();
  39. p1.set("a", "1");
  40. p1.set("b", 1);
  41. p1.set("c", "null");
  42. p1.set("d", null);
  43. p1.set("e", true);
  44. assertEquals("1", p1.get("a"));
  45. assertEquals(Double.valueOf(1), p1.get("b"));
  46. assertEquals("null", p1.get("c"));
  47. assertNull(p1.get("d"));
  48. assertTrue((Boolean)p1.get("e"));
  49. p1 = GQ.create(p1.toJson());
  50. assertEquals("1", p1.get("a"));
  51. assertEquals(Double.valueOf(1), p1.get("b"));
  52. assertEquals("null", p1.get("c"));
  53. assertNull(p1.get("d"));
  54. }
  55. public interface Item extends JsonBuilder {
  56. public static enum Type {BIG, SMALL}
  57. Date getDate();
  58. void setDate(Date d);
  59. Type getType();
  60. void setType(Type t);
  61. }
  62. public interface JsonExample extends JsonBuilder {
  63. int getA();
  64. JsonExample getB();
  65. @Name("M")
  66. int getM();
  67. @Name("u")
  68. String getUrl();
  69. long getD();
  70. Boolean getZ();
  71. String[] getT();
  72. JsonExample setT(String[] strings);
  73. Item[] getIt();
  74. JsonExample setIt(Item[] items);
  75. JsonExample setZ(Boolean b);
  76. JsonExample setD(long l);
  77. List<Item> getItems();
  78. void setItems(List<Item> a);
  79. Item getI();
  80. void setI(Item i);
  81. String y();
  82. void y(String s);
  83. Function getF();
  84. void setF(Function f);
  85. String getN();
  86. }
  87. boolean functionRun = false;
  88. public void testJsonBuilder() {
  89. String json = "{n: null, M:0, a:1, b:{a:2,b:{a:3}},u:url, d:'2','t':['hola','adios'], 'z': true, 'items':[{'date':100}]}";
  90. JsonExample c = GQ.create(JsonExample.class);
  91. assertEquals(0, c.getA());
  92. c.parse(json, true);
  93. assertNull(c.getN());
  94. assertEquals(0, c.getM());
  95. assertEquals(1, c.getA());
  96. assertNotNull(c.getB());
  97. assertEquals(2, c.getB().getA());
  98. assertEquals(3, c.getB().getB().getA());
  99. assertTrue(c.getZ());
  100. assertEquals("hola", c.getT()[0]);
  101. assertEquals("adios", c.getT()[1]);
  102. assertEquals("url", c.getUrl());
  103. c.setT(new String[]{"foo", "bar"})
  104. .setZ(false).setD(1234);
  105. assertFalse(c.getZ());
  106. assertEquals("foo", c.getT()[0]);
  107. assertEquals("bar", c.getT()[1]);
  108. assertEquals(1234l, c.getD());
  109. c.y("y");
  110. assertEquals("y", c.y());
  111. assertEquals(1, c.getItems().size());
  112. c.setF(new Function() {
  113. public void f() {
  114. functionRun = true;
  115. }
  116. });
  117. assertFalse(functionRun);
  118. c.getF().f();
  119. assertTrue(functionRun);
  120. Item i1 = GQ.create(Item.class);
  121. i1.setDate(new Date(2000));
  122. c.setI(i1);
  123. assertEquals(2000l, c.getI().getDate().getTime());
  124. Item i2 = GQ.create(Item.class);
  125. i2.setDate(new Date(3000));
  126. Item[] items = new Item[]{i1, i2};
  127. c.setIt(items);
  128. assertEquals(2000l, c.getIt()[0].getDate().getTime());
  129. assertEquals(3000l, c.getIt()[1].getDate().getTime());
  130. c.setItems(Arrays.asList(items));
  131. assertEquals(2000l, c.getItems().get(0).getDate().getTime());
  132. assertEquals(3000l, c.getItems().get(1).getDate().getTime());
  133. assertFalse(c.toJson().startsWith("{\"jsonExample\":"));
  134. assertTrue(c.toJsonWithName().startsWith("{\"jsonExample\":"));
  135. assertTrue(c.toJson().contains("\"items\":[{\"date\":"));
  136. assertTrue(c.toQueryString().replace("\"bar\"", "bar").contains("t[]=bar"));
  137. assertTrue(c.toQueryString().contains("a=1"));
  138. assertTrue(c.toQueryString().contains("\"a\":2"));
  139. assertEquals(1, c.<Number>get("a").intValue());
  140. }
  141. public interface GAddress extends JsonBuilder {
  142. String street();
  143. String city();
  144. }
  145. public interface GUser extends JsonBuilder {
  146. @Name("_id")
  147. String getId();
  148. int getAge();
  149. String getName();
  150. List<String> getPhones();
  151. GAddress address();
  152. }
  153. public static final String JSON_USER_EXAMPLE = " { "
  154. + " '_id': 'aaabbbccc', "
  155. + " 'email': 'foo@bar.com', "
  156. + " 'age': 27, "
  157. + " 'name': 'Foo Bar', "
  158. + " 'phones': [ "
  159. + " '9166566',"
  160. + " '65443333'"
  161. + " ],"
  162. + " 'address': {"
  163. + " 'street': 'Street Foo N6', "
  164. + " 'number': '670'"
  165. + " }"
  166. + "}";
  167. public void test_parse_json() {
  168. GUser entity = GQ.create(GUser.class);
  169. entity.parse(JSON_USER_EXAMPLE, true);
  170. assertNotNull(entity.get("email"));
  171. assertEquals("aaabbbccc", entity.getId());
  172. assertEquals(27, entity.getAge());
  173. assertEquals("Foo Bar", entity.getName());
  174. assertNotNull(entity.address());
  175. assertEquals("Street Foo N6", entity.address().street());
  176. assertNotNull(entity.address().get("number"));
  177. }
  178. // Nested strict not implemented in JS
  179. @DoNotRunWith(Platform.Prod)
  180. public void test_parse_strict_json() {
  181. GUser entity = GQ.create(GUser.class);
  182. entity.parse(JSON_USER_EXAMPLE, true);
  183. entity.strip();
  184. assertEquals("aaabbbccc", entity.getId());
  185. assertNull(entity.get("email"));
  186. assertEquals(27, entity.getAge());
  187. assertEquals("Foo Bar", entity.getName());
  188. assertNotNull(entity.address());
  189. assertEquals("Street Foo N6", entity.address().street());
  190. // Recursion not implemented in client side
  191. if (GWT.isScript()) {
  192. assertNull(entity.address().get("phone"));
  193. }
  194. }
  195. public void test_return_empty_list_when_array_isEmpty() {
  196. //GIVEN a JSON representation of a user without phones
  197. GUser user = GQ.create(GUser.class);
  198. user.set("email", "a@b.com");
  199. user.set("name", "Random Name");
  200. user.set("phones", Collections.emptyList());
  201. String json = user.toJson();
  202. //WHEN fetching that user
  203. GUser retrievedUser = GQ.create(GUser.class);
  204. retrievedUser.parse(json, true);
  205. //THEN
  206. assertEquals(0, retrievedUser.getPhones().size());
  207. }
  208. public void test_return_null_when_list_is_not_specified() {
  209. //GIVEN a JSON representation of a user
  210. GUser user = GQ.create(GUser.class);
  211. user.set("email", "a@b.com");
  212. user.set("name", "Random Name");
  213. String json = user.toJson();
  214. //WHEN fetching that user
  215. GUser retrievedUser = GQ.create(GUser.class);
  216. retrievedUser.parse(json, true);
  217. //THEN
  218. List<String> phones = retrievedUser.getPhones();
  219. assertNull(phones);
  220. }
  221. }