PageRenderTime 31ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/core/src/test/java/org/jclouds/json/JsonTest.java

https://github.com/mattstep/jclouds
Java | 214 lines | 165 code | 29 blank | 20 comment | 15 complexity | 98bd958d263d8825bb3760cae8e8a160 MD5 | raw file
  1. /**
  2. * Licensed to jclouds, Inc. (jclouds) under one or more
  3. * contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. jclouds licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.jclouds.json;
  20. import static com.google.common.io.BaseEncoding.base16;
  21. import static com.google.common.primitives.Bytes.asList;
  22. import static org.testng.Assert.assertEquals;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.Properties;
  26. import org.jclouds.json.config.GsonModule;
  27. import org.jclouds.json.config.GsonModule.DefaultExclusionStrategy;
  28. import org.testng.annotations.Test;
  29. import com.google.common.collect.ImmutableList;
  30. import com.google.common.collect.ImmutableMap;
  31. import com.google.common.collect.Maps;
  32. import com.google.gson.FieldAttributes;
  33. import com.google.inject.AbstractModule;
  34. import com.google.inject.Guice;
  35. import com.google.inject.TypeLiteral;
  36. @Test
  37. public class JsonTest {
  38. private Json json = Guice.createInjector(new GsonModule()).getInstance(Json.class);
  39. private static class ObjectNoDefaultConstructor {
  40. private final String stringValue;
  41. private final int intValue;
  42. public ObjectNoDefaultConstructor(String stringValue, int intValue) {
  43. this.stringValue = stringValue;
  44. this.intValue = intValue;
  45. }
  46. @Override
  47. public int hashCode() {
  48. final int prime = 31;
  49. int result = 1;
  50. result = prime * result + intValue;
  51. result = prime * result + ((stringValue == null) ? 0 : stringValue.hashCode());
  52. return result;
  53. }
  54. @Override
  55. public boolean equals(Object obj) {
  56. if (this == obj)
  57. return true;
  58. if (obj == null)
  59. return false;
  60. if (getClass() != obj.getClass())
  61. return false;
  62. ObjectNoDefaultConstructor other = (ObjectNoDefaultConstructor) obj;
  63. if (intValue != other.intValue)
  64. return false;
  65. if (stringValue == null) {
  66. if (other.stringValue != null)
  67. return false;
  68. } else if (!stringValue.equals(other.stringValue))
  69. return false;
  70. return true;
  71. }
  72. }
  73. public void testObjectNoDefaultConstructor() {
  74. ObjectNoDefaultConstructor obj = new ObjectNoDefaultConstructor("foo", 1);
  75. assertEquals(json.toJson(obj), "{\"stringValue\":\"foo\",\"intValue\":1}");
  76. ObjectNoDefaultConstructor obj2 = json.fromJson(json.toJson(obj), ObjectNoDefaultConstructor.class);
  77. assertEquals(obj2, obj);
  78. assertEquals(json.toJson(obj2), json.toJson(obj));
  79. }
  80. static class ExcludeStringValue implements DefaultExclusionStrategy {
  81. public boolean shouldSkipClass(Class<?> clazz) {
  82. return false;
  83. }
  84. public boolean shouldSkipField(FieldAttributes f) {
  85. return f.getName().equals("stringValue");
  86. }
  87. }
  88. public void testExcluder() {
  89. Json excluder = Guice.createInjector(new GsonModule(), new AbstractModule() {
  90. protected void configure() {
  91. bind(DefaultExclusionStrategy.class).to(ExcludeStringValue.class);
  92. }
  93. }).getInstance(Json.class);
  94. ObjectNoDefaultConstructor obj = new ObjectNoDefaultConstructor("foo", 1);
  95. assertEquals(excluder.toJson(obj), "{\"intValue\":1}");
  96. }
  97. private static class EnumInside {
  98. private static enum Test {
  99. FOO, BAR;
  100. }
  101. private Test enumValue;
  102. }
  103. private static class ByteList {
  104. List<Byte> checksum;
  105. }
  106. public void testByteList() {
  107. ByteList bl = new ByteList();
  108. bl.checksum = asList(base16().lowerCase().decode("1dda05ed139664f1f89b9dec482b77c0"));
  109. assertEquals(json.toJson(bl), "{\"checksum\":\"1dda05ed139664f1f89b9dec482b77c0\"}");
  110. assertEquals(json.fromJson(json.toJson(bl), ByteList.class).checksum, bl.checksum);
  111. }
  112. public void testPropertiesSerializesDefaults() {
  113. Properties props = new Properties();
  114. props.put("string", "string");
  115. props.put("number", "1");
  116. props.put("boolean", "true");
  117. assertEquals(json.toJson(props), "{\"string\":\"string\",\"boolean\":\"true\",\"number\":\"1\"}");
  118. Properties props3 = new Properties(props);
  119. assertEquals(json.toJson(props3), "{\"string\":\"string\",\"boolean\":\"true\",\"number\":\"1\"}");
  120. Properties props2 = json.fromJson(json.toJson(props), Properties.class);
  121. assertEquals(props2, props);
  122. assertEquals(json.toJson(props2), json.toJson(props));
  123. }
  124. public void testMapStringObjectWithAllValidValuesOneDeep() {
  125. Map<String, Object> map = Maps.newHashMap();
  126. map.put("string", "string");
  127. map.put("number", 1.0);
  128. map.put("boolean", true);
  129. map.put("map", ImmutableMap.of("key", "value"));
  130. map.put("list", ImmutableList.of("key", "value"));
  131. assertEquals(json.toJson(map),
  132. "{\"string\":\"string\",\"map\":{\"key\":\"value\"},\"list\":[\"key\",\"value\"],\"boolean\":true,\"number\":1.0}");
  133. Map<String, Object> map2 = json.fromJson(json.toJson(map), new TypeLiteral<Map<String, Object>>() {
  134. }.getType());
  135. assertEquals(map2, map);
  136. assertEquals(json.toJson(map2), json.toJson(map));
  137. }
  138. public void testMapStringObjectWithNumericalKeysConvertToStrings() {
  139. Map<String, Object> map = ImmutableMap.<String, Object> of("map", ImmutableMap.of(1, "value"));
  140. assertEquals(json.toJson(map), "{\"map\":{\"1\":\"value\"}}");
  141. Map<String, Object> map2 = json.fromJson(json.toJson(map), new TypeLiteral<Map<String, Object>>() {
  142. }.getType());
  143. // note conversion.. ensures valid
  144. assertEquals(map2, ImmutableMap.<String, Object> of("map", ImmutableMap.of("1", "value")));
  145. assertEquals(json.toJson(map2), json.toJson(map));
  146. }
  147. public void testMapStringObjectWithBooleanKeysConvertToStrings() {
  148. Map<String, Object> map = ImmutableMap.<String, Object> of("map", ImmutableMap.of(true, "value"));
  149. assertEquals(json.toJson(map), "{\"map\":{\"true\":\"value\"}}");
  150. Map<String, Object> map2 = json.fromJson(json.toJson(map), new TypeLiteral<Map<String, Object>>() {
  151. }.getType());
  152. // note conversion.. ensures valid
  153. assertEquals(map2, ImmutableMap.<String, Object> of("map", ImmutableMap.of("true", "value")));
  154. assertEquals(json.toJson(map2), json.toJson(map));
  155. }
  156. public void testDeserializeEnum() {
  157. assertEquals(json.fromJson("{enumValue : \"FOO\"}", EnumInside.class).enumValue, EnumInside.Test.FOO);
  158. }
  159. @Test(expectedExceptions = IllegalArgumentException.class)
  160. public void testDeserializeEnumWhenBadValue() {
  161. assertEquals(json.fromJson("{enumValue : \"s\"}", EnumInside.class).enumValue, EnumInside.Test.FOO);
  162. }
  163. private static class EnumInsideWithParser {
  164. private static enum Test {
  165. FOO, BAR, UNRECOGNIZED;
  166. @SuppressWarnings("unused")
  167. public static Test fromValue(String state) {
  168. try {
  169. return valueOf(state);
  170. } catch (IllegalArgumentException e) {
  171. return UNRECOGNIZED;
  172. }
  173. }
  174. }
  175. private Test enumValue;
  176. }
  177. public void testDeserializeEnumWithParser() {
  178. assertEquals(json.fromJson("{enumValue : \"FOO\"}", EnumInsideWithParser.class).enumValue,
  179. EnumInsideWithParser.Test.FOO);
  180. }
  181. public void testDeserializeEnumWithParserAndBadValue() {
  182. assertEquals(json.fromJson("{enumValue : \"sd\"}", EnumInsideWithParser.class).enumValue,
  183. EnumInsideWithParser.Test.UNRECOGNIZED);
  184. }
  185. }