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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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.client.GWTTestCase;
  19. import com.google.gwt.query.client.Function;
  20. import com.google.gwt.query.client.GQ;
  21. import com.google.gwt.query.client.IsProperties;
  22. import com.google.gwt.query.client.builders.JsonBuilder;
  23. import com.google.gwt.query.client.builders.Name;
  24. import java.util.Arrays;
  25. import java.util.Date;
  26. import java.util.List;
  27. /**
  28. * Tests for Deferred which can run either in JVM and GWT
  29. */
  30. public class DataBindingTestJre extends GWTTestCase {
  31. public String getModuleName() {
  32. return null;
  33. }
  34. public void testPropertiesCreate() {
  35. IsProperties p1 = GQ.create();
  36. p1.set("a", "1");
  37. p1.set("b", 1);
  38. p1.set("c", "null");
  39. p1.set("d", null);
  40. p1.set("e", true);
  41. assertEquals("1", p1.get("a"));
  42. assertEquals(Double.valueOf(1), p1.get("b"));
  43. assertEquals("null", p1.get("c"));
  44. assertNull(p1.get("d"));
  45. assertTrue((Boolean)p1.get("e"));
  46. p1 = GQ.create(p1.toJson());
  47. assertEquals("1", p1.get("a"));
  48. assertEquals(Double.valueOf(1), p1.get("b"));
  49. assertEquals("null", p1.get("c"));
  50. assertNull(p1.get("d"));
  51. }
  52. public interface Item extends JsonBuilder {
  53. public static enum Type {BIG, SMALL}
  54. Date getDate();
  55. void setDate(Date d);
  56. Type getType();
  57. void setType(Type t);
  58. }
  59. public interface JsonExample extends JsonBuilder {
  60. int getA();
  61. JsonExample getB();
  62. @Name("M")
  63. int getM();
  64. @Name("u")
  65. String getUrl();
  66. long getD();
  67. Boolean getZ();
  68. String[] getT();
  69. JsonExample setT(String[] strings);
  70. JsonExample setZ(Boolean b);
  71. JsonExample setD(long l);
  72. List<Item> getItems();
  73. void setItems(List<Item> a);
  74. Item getI();
  75. void setI(Item i);
  76. String y();
  77. void y(String s);
  78. Function getF();
  79. void setF(Function f);
  80. String getN();
  81. }
  82. boolean functionRun = false;
  83. public void testJsonBuilder() {
  84. 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}]}";
  85. JsonExample c = GQ.create(JsonExample.class);
  86. assertEquals(0, c.getA());
  87. c.parse(json, true);
  88. assertNull(c.getN());
  89. assertEquals(0, c.getM());
  90. assertEquals(1, c.getA());
  91. assertNotNull(c.getB());
  92. assertEquals(2, c.getB().getA());
  93. assertEquals(3, c.getB().getB().getA());
  94. assertTrue(c.getZ());
  95. assertEquals("hola", c.getT()[0]);
  96. assertEquals("adios", c.getT()[1]);
  97. assertEquals("url", c.getUrl());
  98. c.setT(new String[]{"foo", "bar"})
  99. .setZ(false).setD(1234);
  100. assertFalse(c.getZ());
  101. assertEquals("foo", c.getT()[0]);
  102. assertEquals("bar", c.getT()[1]);
  103. assertEquals(1234l, c.getD());
  104. c.y("y");
  105. assertEquals("y", c.y());
  106. assertEquals(1, c.getItems().size());
  107. c.setF(new Function() {
  108. public void f() {
  109. functionRun = true;
  110. }
  111. });
  112. assertFalse(functionRun);
  113. c.getF().f();
  114. assertTrue(functionRun);
  115. Item i1 = GQ.create(Item.class);
  116. i1.setDate(new Date(2000));
  117. c.setI(i1);
  118. assertEquals(2000l, c.getI().getDate().getTime());
  119. Item i2 = GQ.create(Item.class);
  120. i2.setDate(new Date(3000));
  121. Item[] items = new Item[]{i1, i2};
  122. c.setItems(Arrays.asList(items));
  123. assertEquals(2000l, c.getItems().get(0).getDate().getTime());
  124. assertEquals(3000l, c.getItems().get(1).getDate().getTime());
  125. assertFalse(c.toJson().startsWith("{\"jsonExample\":"));
  126. assertTrue(c.toJsonWithName().startsWith("{\"jsonExample\":"));
  127. assertTrue(c.toJson().contains("\"items\":[{\"date\":"));
  128. assertTrue(c.toQueryString().replace("\"bar\"", "bar").contains("t[]=bar"));
  129. assertTrue(c.toQueryString().contains("a=1"));
  130. assertTrue(c.toQueryString().contains("\"a\":2"));
  131. assertEquals(1, c.<Number>get("a").intValue());
  132. }
  133. public interface GAddress extends JsonBuilder {
  134. String street();
  135. String city();
  136. }
  137. public interface GUser extends JsonBuilder {
  138. @Name("_id")
  139. String getId();
  140. int getAge();
  141. String getName();
  142. GAddress address();
  143. }
  144. public static final String JSON_USER_EXAMPLE = " { "
  145. + " '_id': 'aaabbbccc', "
  146. + " 'email': 'foo@bar.com', "
  147. + " 'age': 27, "
  148. + " 'name': 'Foo Bar', "
  149. + " 'address': {"
  150. + " 'street': 'Street Foo N6', "
  151. + " 'phone': '670'"
  152. + " }"
  153. + "}";
  154. public void
  155. test_parse_json() {
  156. GUser entity = GQ.create(GUser.class);
  157. entity.parse(JSON_USER_EXAMPLE, true);
  158. assertNotNull(entity.get("email"));
  159. assertEquals("aaabbbccc", entity.getId());
  160. assertEquals(27, entity.getAge());
  161. assertEquals("Foo Bar", entity.getName());
  162. assertNotNull(entity.address());
  163. assertEquals("Street Foo N6", entity.address().street());
  164. assertNotNull(entity.address().get("phone"));
  165. }
  166. public void
  167. test_parse_strict_json() {
  168. GUser entity = GQ.create(GUser.class);
  169. entity.parse(JSON_USER_EXAMPLE, true);
  170. entity.strip();
  171. assertEquals("aaabbbccc", entity.getId());
  172. assertNull(entity.get("email"));
  173. assertEquals(27, entity.getAge());
  174. assertEquals("Foo Bar", entity.getName());
  175. assertNotNull(entity.address());
  176. assertEquals("Street Foo N6", entity.address().street());
  177. // Recursion not implemented in client side
  178. if (GWT.isScript()) {
  179. assertNull(entity.address().get("phone"));
  180. }
  181. }
  182. }