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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. JsonExample setZ(Boolean b);
  74. JsonExample setD(long l);
  75. List<Item> getItems();
  76. void setItems(List<Item> a);
  77. Item getI();
  78. void setI(Item i);
  79. String y();
  80. void y(String s);
  81. Function getF();
  82. void setF(Function f);
  83. String getN();
  84. }
  85. boolean functionRun = false;
  86. public void testJsonBuilder() {
  87. 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}]}";
  88. JsonExample c = GQ.create(JsonExample.class);
  89. assertEquals(0, c.getA());
  90. c.parse(json, true);
  91. assertNull(c.getN());
  92. assertEquals(0, c.getM());
  93. assertEquals(1, c.getA());
  94. assertNotNull(c.getB());
  95. assertEquals(2, c.getB().getA());
  96. assertEquals(3, c.getB().getB().getA());
  97. assertTrue(c.getZ());
  98. assertEquals("hola", c.getT()[0]);
  99. assertEquals("adios", c.getT()[1]);
  100. assertEquals("url", c.getUrl());
  101. c.setT(new String[]{"foo", "bar"})
  102. .setZ(false).setD(1234);
  103. assertFalse(c.getZ());
  104. assertEquals("foo", c.getT()[0]);
  105. assertEquals("bar", c.getT()[1]);
  106. assertEquals(1234l, c.getD());
  107. c.y("y");
  108. assertEquals("y", c.y());
  109. assertEquals(1, c.getItems().size());
  110. c.setF(new Function() {
  111. public void f() {
  112. functionRun = true;
  113. }
  114. });
  115. assertFalse(functionRun);
  116. c.getF().f();
  117. assertTrue(functionRun);
  118. Item i1 = GQ.create(Item.class);
  119. i1.setDate(new Date(2000));
  120. c.setI(i1);
  121. assertEquals(2000l, c.getI().getDate().getTime());
  122. Item i2 = GQ.create(Item.class);
  123. i2.setDate(new Date(3000));
  124. Item[] items = new Item[]{i1, i2};
  125. c.setItems(Arrays.asList(items));
  126. assertEquals(2000l, c.getItems().get(0).getDate().getTime());
  127. assertEquals(3000l, c.getItems().get(1).getDate().getTime());
  128. assertFalse(c.toJson().startsWith("{\"jsonExample\":"));
  129. assertTrue(c.toJsonWithName().startsWith("{\"jsonExample\":"));
  130. assertTrue(c.toJson().contains("\"items\":[{\"date\":"));
  131. assertTrue(c.toQueryString().replace("\"bar\"", "bar").contains("t[]=bar"));
  132. assertTrue(c.toQueryString().contains("a=1"));
  133. assertTrue(c.toQueryString().contains("\"a\":2"));
  134. assertEquals(1, c.<Number>get("a").intValue());
  135. }
  136. public interface GAddress extends JsonBuilder {
  137. String street();
  138. String city();
  139. }
  140. public interface GUser extends JsonBuilder {
  141. @Name("_id")
  142. String getId();
  143. int getAge();
  144. String getName();
  145. List<String> getPhones();
  146. GAddress address();
  147. }
  148. public static final String JSON_USER_EXAMPLE = " { "
  149. + " '_id': 'aaabbbccc', "
  150. + " 'email': 'foo@bar.com', "
  151. + " 'age': 27, "
  152. + " 'name': 'Foo Bar', "
  153. + " 'phones': [ "
  154. + " '9166566',"
  155. + " '65443333'"
  156. + " ],"
  157. + " 'address': {"
  158. + " 'street': 'Street Foo N6', "
  159. + " 'number': '670'"
  160. + " }"
  161. + "}";
  162. public void test_parse_json() {
  163. GUser entity = GQ.create(GUser.class);
  164. entity.parse(JSON_USER_EXAMPLE, true);
  165. assertNotNull(entity.get("email"));
  166. assertEquals("aaabbbccc", entity.getId());
  167. assertEquals(27, entity.getAge());
  168. assertEquals("Foo Bar", entity.getName());
  169. assertNotNull(entity.address());
  170. assertEquals("Street Foo N6", entity.address().street());
  171. assertNotNull(entity.address().get("number"));
  172. }
  173. // Nested strict not implemented in JS
  174. @DoNotRunWith(Platform.Prod)
  175. public void test_parse_strict_json() {
  176. GUser entity = GQ.create(GUser.class);
  177. entity.parse(JSON_USER_EXAMPLE, true);
  178. entity.strip();
  179. assertEquals("aaabbbccc", entity.getId());
  180. assertNull(entity.get("email"));
  181. assertEquals(27, entity.getAge());
  182. assertEquals("Foo Bar", entity.getName());
  183. assertNotNull(entity.address());
  184. assertEquals("Street Foo N6", entity.address().street());
  185. // Recursion not implemented in client side
  186. if (GWT.isScript()) {
  187. assertNull(entity.address().get("phone"));
  188. }
  189. }
  190. public void test_return_empty_list_when_array_isEmpty() {
  191. //GIVEN a JSON representation of a user without phones
  192. GUser user = GQ.create(GUser.class);
  193. user.set("email", "a@b.com");
  194. user.set("name", "Random Name");
  195. user.set("phones", Collections.emptyList());
  196. String json = user.toJson();
  197. //WHEN fetching that user
  198. GUser retrievedUser = GQ.create(GUser.class);
  199. retrievedUser.parse(json, true);
  200. //THEN
  201. assertEquals(0, retrievedUser.getPhones().size());
  202. }
  203. public void test_return_null_when_list_is_not_specified() {
  204. //GIVEN a JSON representation of a user
  205. GUser user = GQ.create(GUser.class);
  206. user.set("email", "a@b.com");
  207. user.set("name", "Random Name");
  208. String json = user.toJson();
  209. //WHEN fetching that user
  210. GUser retrievedUser = GQ.create(GUser.class);
  211. retrievedUser.parse(json, true);
  212. //THEN
  213. List<String> phones = retrievedUser.getPhones();
  214. assertNull(phones);
  215. }
  216. }