/jetty/modules/util/src/test/java/org/mortbay/util/ajax/JSONPojoConvertorFactoryTest.java

https://github.com/derickbailey/qedserver · Java · 338 lines · 270 code · 52 blank · 16 comment · 9 complexity · 8a9e6473ffcfcfd4c7492940402fd079 MD5 · raw file

  1. // ========================================================================
  2. // Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd.
  3. // ------------------------------------------------------------------------
  4. // All rights reserved. This program and the accompanying materials
  5. // are made available under the terms of the Eclipse Public License v1.0
  6. // and Apache License v2.0 which accompanies this distribution.
  7. // The Eclipse Public License is available at
  8. // http://www.eclipse.org/legal/epl-v10.html
  9. // The Apache License v2.0 is available at
  10. // http://www.opensource.org/licenses/apache2.0.php
  11. // You may elect to redistribute this code under either of these licenses.
  12. // ========================================================================
  13. package org.mortbay.util.ajax;
  14. import junit.framework.TestCase;
  15. /**
  16. * Test to convert POJOs to JSON and vice versa with automatic convertor creation.
  17. */
  18. public class JSONPojoConvertorFactoryTest extends TestCase {
  19. public void testFoo()
  20. {
  21. JSON jsonOut = new JSON();
  22. JSON jsonIn = new JSON();
  23. jsonOut.addConvertor(Object.class, new JSONPojoConvertorFactory(jsonOut));
  24. jsonIn.addConvertor(Object.class, new JSONPojoConvertorFactory(jsonIn));
  25. // ensure that JSONPojoConvertorFactory does not create infinite loop
  26. String os = jsonOut.toJSON(new Object());
  27. Object so = jsonIn.fromJSON(os);
  28. Foo foo = new Foo();
  29. foo._name = "Foo @ " + System.currentTimeMillis();
  30. foo._int1 = 1;
  31. foo._int2 = new Integer(2);
  32. foo._long1 = 1000001l;
  33. foo._long2 = new Long(1000002l);
  34. foo._float1 = 10.11f;
  35. foo._float2 = new Float(10.22f);
  36. foo._double1 = 10000.11111d;
  37. foo._double2 = new Double(10000.22222d);
  38. Bar bar = new Bar("Hello", true, new Baz("World", Boolean.FALSE, foo), new Baz[]{
  39. new Baz("baz0", Boolean.TRUE, null), new Baz("baz1", Boolean.FALSE, null)
  40. });
  41. String s = jsonOut.toJSON(bar);
  42. Object obj = jsonIn.parse(new JSON.StringSource(s));
  43. assertTrue(obj instanceof Bar);
  44. Bar br = (Bar)obj;
  45. Baz bz = br.getBaz();
  46. Foo f = bz.getFoo();
  47. assertEquals(f, foo);
  48. assertTrue(br.getBazs().length==2);
  49. assertEquals(br.getBazs()[0].getMessage(), "baz0");
  50. assertEquals(br.getBazs()[1].getMessage(), "baz1");
  51. }
  52. public static class Bar
  53. {
  54. private String _title, _nullTest;
  55. private Baz _baz;
  56. private boolean _boolean1;
  57. private Baz[] _bazs;
  58. public Bar()
  59. {
  60. }
  61. public Bar(String title, boolean boolean1, Baz baz)
  62. {
  63. setTitle(title);
  64. setBoolean1(boolean1);
  65. setBaz(baz);
  66. }
  67. public Bar(String title, boolean boolean1, Baz baz, Baz[] bazs)
  68. {
  69. this(title, boolean1, baz);
  70. setBazs(bazs);
  71. }
  72. public String toString()
  73. {
  74. return new StringBuffer().append("\n=== ").append(getClass().getName()).append(" ===")
  75. .append("\ntitle: ").append(getTitle())
  76. .append("\nboolean1: ").append(isBoolean1())
  77. .append("\nnullTest: ").append(getNullTest())
  78. .append("\nbaz: ").append(getBaz()).toString();
  79. }
  80. public void setTitle(String title)
  81. {
  82. _title = title;
  83. }
  84. public String getTitle()
  85. {
  86. return _title;
  87. }
  88. public void setNullTest(String nullTest)
  89. {
  90. assert(nullTest==null);
  91. _nullTest = nullTest;
  92. }
  93. public String getNullTest()
  94. {
  95. return _nullTest;
  96. }
  97. public void setBaz(Baz baz)
  98. {
  99. _baz = baz;
  100. }
  101. public Baz getBaz()
  102. {
  103. return _baz;
  104. }
  105. public void setBoolean1(boolean boolean1)
  106. {
  107. _boolean1 = boolean1;
  108. }
  109. public boolean isBoolean1()
  110. {
  111. return _boolean1;
  112. }
  113. public void setBazs(Baz[] bazs)
  114. {
  115. _bazs = bazs;
  116. }
  117. public Baz[] getBazs()
  118. {
  119. return _bazs;
  120. }
  121. }
  122. public static class Baz
  123. {
  124. private String _message;
  125. private Foo _foo;
  126. private Boolean _boolean2;
  127. public Baz()
  128. {
  129. }
  130. public Baz(String message, Boolean boolean2, Foo foo)
  131. {
  132. setMessage(message);
  133. setBoolean2(boolean2);
  134. setFoo(foo);
  135. }
  136. public String toString()
  137. {
  138. return new StringBuffer().append("\n=== ").append(getClass().getName()).append(" ===")
  139. .append("\nmessage: ").append(getMessage())
  140. .append("\nboolean2: ").append(isBoolean2())
  141. .append("\nfoo: ").append(getFoo()).toString();
  142. }
  143. public void setMessage(String message)
  144. {
  145. _message = message;
  146. }
  147. public String getMessage()
  148. {
  149. return _message;
  150. }
  151. public void setFoo(Foo foo)
  152. {
  153. _foo = foo;
  154. }
  155. public Foo getFoo()
  156. {
  157. return _foo;
  158. }
  159. public void setBoolean2(Boolean boolean2)
  160. {
  161. _boolean2 = boolean2;
  162. }
  163. public Boolean isBoolean2()
  164. {
  165. return _boolean2;
  166. }
  167. }
  168. public static class Foo
  169. {
  170. private String _name;
  171. private int _int1;
  172. private Integer _int2;
  173. private long _long1;
  174. private Long _long2;
  175. private float _float1;
  176. private Float _float2;
  177. private double _double1;
  178. private Double _double2;
  179. public Foo()
  180. {
  181. }
  182. public String toString()
  183. {
  184. return new StringBuffer().append("\n=== ").append(getClass().getName()).append(" ===")
  185. .append("\nname: ").append(_name)
  186. .append("\nint1: ").append(_int1)
  187. .append("\nint2: ").append(_int2)
  188. .append("\nlong1: ").append(_long1)
  189. .append("\nlong2: ").append(_long2)
  190. .append("\nfloat1: ").append(_float1)
  191. .append("\nfloat2: ").append(_float2)
  192. .append("\ndouble1: ").append(_double1)
  193. .append("\ndouble2: ").append(_double2)
  194. .toString();
  195. }
  196. public boolean equals(Object another)
  197. {
  198. if(another instanceof Foo)
  199. {
  200. Foo foo = (Foo)another;
  201. return getName().equals(foo.getName())
  202. && getInt1()==foo.getInt1()
  203. && getInt2().equals(foo.getInt2())
  204. && getLong1()==foo.getLong1()
  205. && getLong2().equals(foo.getLong2())
  206. && getFloat1()==foo.getFloat1()
  207. && getFloat2().equals(foo.getFloat2())
  208. && getDouble1()==foo.getDouble1()
  209. && getDouble2().equals(foo.getDouble2());
  210. }
  211. return false;
  212. }
  213. public String getName()
  214. {
  215. return _name;
  216. }
  217. public void setName(String name)
  218. {
  219. _name = name;
  220. }
  221. public int getInt1()
  222. {
  223. return _int1;
  224. }
  225. public void setInt1(int int1)
  226. {
  227. _int1 = int1;
  228. }
  229. public Integer getInt2()
  230. {
  231. return _int2;
  232. }
  233. public void setInt2(Integer int2)
  234. {
  235. _int2 = int2;
  236. }
  237. public long getLong1()
  238. {
  239. return _long1;
  240. }
  241. public void setLong1(long long1)
  242. {
  243. _long1 = long1;
  244. }
  245. public Long getLong2()
  246. {
  247. return _long2;
  248. }
  249. public void setLong2(Long long2)
  250. {
  251. _long2 = long2;
  252. }
  253. public float getFloat1()
  254. {
  255. return _float1;
  256. }
  257. public void setFloat1(float float1)
  258. {
  259. _float1 = float1;
  260. }
  261. public Float getFloat2()
  262. {
  263. return _float2;
  264. }
  265. public void setFloat2(Float float2)
  266. {
  267. _float2 = float2;
  268. }
  269. public double getDouble1()
  270. {
  271. return _double1;
  272. }
  273. public void setDouble1(double double1)
  274. {
  275. _double1 = double1;
  276. }
  277. public Double getDouble2()
  278. {
  279. return _double2;
  280. }
  281. public void setDouble2(Double double2)
  282. {
  283. _double2 = double2;
  284. }
  285. }
  286. }