PageRenderTime 24ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/stetho/src/test/java/com/facebook/stetho/json/ObjectMapperTest.java

https://gitlab.com/Liang1Zhang/stetho
Java | 300 lines | 236 code | 52 blank | 12 comment | 26 complexity | bf8fc612af0f59cdc8120f3e4eaeee48 MD5 | raw file
  1. /*
  2. * Copyright (c) 2014-present, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. */
  9. package com.facebook.stetho.json;
  10. import android.os.Build;
  11. import com.facebook.stetho.json.annotation.JsonProperty;
  12. import com.facebook.stetho.json.annotation.JsonValue;
  13. import org.json.JSONArray;
  14. import org.json.JSONException;
  15. import org.json.JSONObject;
  16. import org.junit.Before;
  17. import org.junit.Test;
  18. import org.junit.runner.RunWith;
  19. import org.robolectric.RobolectricTestRunner;
  20. import org.robolectric.annotation.Config;
  21. import java.io.IOException;
  22. import java.util.ArrayList;
  23. import java.util.Collections;
  24. import java.util.List;
  25. import java.util.ListIterator;
  26. import java.util.Objects;
  27. import static org.junit.Assert.assertEquals;
  28. import static org.junit.Assert.assertTrue;
  29. /**
  30. * Tests for {@link ObjectMapper}
  31. */
  32. @Config(emulateSdk = Build.VERSION_CODES.JELLY_BEAN)
  33. @RunWith(RobolectricTestRunner.class)
  34. public class ObjectMapperTest {
  35. private ObjectMapper mObjectMapper;
  36. @Before
  37. public void setup() {
  38. mObjectMapper = new ObjectMapper();
  39. }
  40. @Test
  41. public void testJsonProperty() throws IOException, JSONException {
  42. JsonPropertyString c = new JsonPropertyString();
  43. c.testString = "test";
  44. String expected = "{\"testString\":\"test\"}";
  45. JSONObject jsonObject = mObjectMapper.convertValue(c, JSONObject.class);
  46. String str = jsonObject.toString();
  47. assertEquals(expected, str);
  48. JsonPropertyString jsonPropertyString = mObjectMapper.convertValue(
  49. new JSONObject(expected),
  50. JsonPropertyString.class);
  51. assertEquals(c, jsonPropertyString);
  52. }
  53. @Test
  54. public void testNestedProperty() throws JSONException {
  55. NestedJsonProperty njp = new NestedJsonProperty();
  56. njp.child1 = new JsonPropertyString();
  57. njp.child2 = new JsonPropertyInt();
  58. njp.child1.testString = "testString";
  59. njp.child2.i = 4;
  60. // The ordering of serialization changes depending on Java 7 vs Java 8.
  61. String expected7 = "{\"child1\":{\"testString\":\"testString\"},\"child2\":{\"i\":4}}";
  62. String expected8 = "{\"child2\":{\"i\":4},\"child1\":{\"testString\":\"testString\"}}";
  63. NestedJsonProperty parsed7 = mObjectMapper.convertValue(
  64. new JSONObject(expected7),
  65. NestedJsonProperty.class);
  66. assertEquals(njp, parsed7);
  67. NestedJsonProperty parsed8 = mObjectMapper.convertValue(
  68. new JSONObject(expected8),
  69. NestedJsonProperty.class);
  70. assertEquals(njp, parsed8);
  71. JSONObject jsonObject = mObjectMapper.convertValue(njp, JSONObject.class);
  72. assertTrue(expected7.equals(jsonObject.toString()) || expected8.equals(jsonObject.toString()));
  73. }
  74. @Test
  75. public void testEnumProperty() throws JSONException {
  76. JsonPropertyEnum jpe = new JsonPropertyEnum();
  77. jpe.enumValue = TestEnum.VALUE_TWO;
  78. String expected = "{\"enumValue\":\"two\"}";
  79. JsonPropertyEnum parsed = mObjectMapper.convertValue(
  80. new JSONObject(expected),
  81. JsonPropertyEnum.class);
  82. assertEquals(jpe, parsed);
  83. JSONObject jsonObject = mObjectMapper.convertValue(jpe, JSONObject.class);
  84. assertEquals(expected, jsonObject.toString());
  85. }
  86. @Test
  87. public void testListString() throws JSONException {
  88. JsonPropertyStringList jpsl = new JsonPropertyStringList();
  89. List<String> values = new ArrayList<String>();
  90. jpsl.stringList = values;
  91. values.add("one");
  92. values.add("two");
  93. values.add("three");
  94. String expected = "{\"stringList\":[\"one\",\"two\",\"three\"]}";
  95. JsonPropertyStringList jsonPropertyStringList = mObjectMapper.convertValue(
  96. new JSONObject(expected),
  97. JsonPropertyStringList.class);
  98. assertEquals(jpsl, jsonPropertyStringList);
  99. JSONObject jsonObject = mObjectMapper.convertValue(jpsl, JSONObject.class);
  100. String str = jsonObject.toString();
  101. assertEquals(expected, str);
  102. }
  103. @Test
  104. public void testSerializeMultitypedList() throws JSONException {
  105. List<Object> list = new ArrayList<Object>();
  106. list.add("foo");
  107. list.add(Collections.singletonList("bar"));
  108. JsonPropertyMultitypedList javaObj = new JsonPropertyMultitypedList();
  109. javaObj.multitypedList = list;
  110. String expected = "{\"multitypedList\":[\"foo\",[\"bar\"]]}";
  111. JSONObject jsonObj = mObjectMapper.convertValue(javaObj, JSONObject.class);
  112. String str = jsonObj.toString();
  113. assertEquals(expected, str);
  114. }
  115. @Test
  116. public void testSerializeListOfLists() throws JSONException {
  117. List<List<String>> listOfLists = new ArrayList<List<String>>();
  118. listOfLists.add(Collections.singletonList("foo"));
  119. ArrayList<String> sublist2 = new ArrayList<String>();
  120. sublist2.add("1");
  121. sublist2.add("2");
  122. listOfLists.add(sublist2);
  123. JsonPropertyListOfLists javaObj = new JsonPropertyListOfLists();
  124. javaObj.listOfLists = listOfLists;
  125. String expected = "{\"listOfLists\":[[\"foo\"],[\"1\",\"2\"]]}";
  126. JSONObject jsonObj = mObjectMapper.convertValue(javaObj, JSONObject.class);
  127. String str = jsonObj.toString();
  128. assertEquals(expected, str);
  129. }
  130. @Test
  131. public void testObjectToPrimitive() throws JSONException {
  132. ArrayOfPrimitivesContainer container = new ArrayOfPrimitivesContainer();
  133. ArrayList<Object> primitives = container.primitives;
  134. primitives.add(Long.MIN_VALUE);
  135. primitives.add(Long.MAX_VALUE);
  136. primitives.add(Integer.MIN_VALUE);
  137. primitives.add(Integer.MAX_VALUE);
  138. primitives.add(Float.MIN_VALUE);
  139. primitives.add(Float.MAX_VALUE);
  140. primitives.add(Double.MIN_VALUE);
  141. primitives.add(Double.MAX_VALUE);
  142. String json = mObjectMapper.convertValue(container, JSONObject.class).toString();
  143. JSONObject obj = new JSONObject(json);
  144. JSONArray array = obj.getJSONArray("primitives");
  145. ArrayList<Object> actual = new ArrayList<>();
  146. for (int i = 0, N = array.length(); i < N; i++) {
  147. actual.add(array.get(i));
  148. }
  149. assertEquals(primitives.toString(), actual.toString());
  150. }
  151. public static class ArrayOfPrimitivesContainer {
  152. @JsonProperty
  153. public final ArrayList<Object> primitives = new ArrayList<>();
  154. }
  155. public static class NestedJsonProperty {
  156. @JsonProperty(required = true)
  157. public JsonPropertyString child1;
  158. @JsonProperty
  159. public JsonPropertyInt child2;
  160. @Override
  161. public boolean equals(Object o) {
  162. if (o == null || !(o instanceof NestedJsonProperty)) {
  163. return false;
  164. }
  165. return Objects.equals(child1, ((NestedJsonProperty) o).child1) &&
  166. Objects.equals(child2, ((NestedJsonProperty) o).child2);
  167. }
  168. }
  169. public static class JsonPropertyString {
  170. @JsonProperty
  171. public String testString;
  172. @Override
  173. public boolean equals(Object o) {
  174. if (o == null || !(o instanceof JsonPropertyString)) {
  175. return false;
  176. }
  177. return Objects.equals(testString, ((JsonPropertyString) o).testString);
  178. }
  179. }
  180. public static class JsonPropertyInt {
  181. @JsonProperty
  182. public int i;
  183. @Override
  184. public boolean equals(Object o) {
  185. if (o == null || !(o instanceof JsonPropertyInt)) {
  186. return false;
  187. }
  188. return Objects.equals(i, ((JsonPropertyInt) o).i);
  189. }
  190. }
  191. public static class JsonPropertyEnum {
  192. @JsonProperty
  193. public TestEnum enumValue;
  194. @Override
  195. public boolean equals(Object o) {
  196. if (o == null || !(o instanceof JsonPropertyEnum)) {
  197. return false;
  198. }
  199. return Objects.equals(enumValue, ((JsonPropertyEnum) o).enumValue);
  200. }
  201. }
  202. public static class JsonPropertyStringList {
  203. @JsonProperty
  204. public List<String> stringList;
  205. @Override
  206. public boolean equals(Object o) {
  207. if (o == null || !(o instanceof JsonPropertyStringList)) {
  208. return false;
  209. }
  210. JsonPropertyStringList rhs = (JsonPropertyStringList) o;
  211. if (stringList == null || rhs.stringList == null) {
  212. return stringList == rhs.stringList;
  213. }
  214. if (stringList.size() != rhs.stringList.size()) {
  215. return false;
  216. }
  217. ListIterator<String> myIter = stringList.listIterator();
  218. ListIterator<String> rhsIter = rhs.stringList.listIterator();
  219. while (myIter.hasNext()) {
  220. if (!Objects.equals(myIter.next(), rhsIter.next())) {
  221. return false;
  222. }
  223. }
  224. return true;
  225. }
  226. }
  227. public enum TestEnum {
  228. VALUE_ONE("one"),
  229. VALUE_TWO("two"),
  230. VALUE_THREE("three");
  231. private final String mValue;
  232. private TestEnum(String str) {
  233. mValue = str;
  234. }
  235. @JsonValue
  236. public String getValue() {
  237. return mValue;
  238. }
  239. }
  240. private static class JsonPropertyMultitypedList {
  241. @JsonProperty
  242. public List<Object> multitypedList;
  243. }
  244. private static class JsonPropertyListOfLists {
  245. @JsonProperty
  246. public List<List<String>> listOfLists;
  247. }
  248. }