|
| 1 | +package com.mapbox.api.geocoding.v5; |
| 2 | + |
| 3 | +import com.google.gson.Gson; |
| 4 | +import com.google.gson.GsonBuilder; |
| 5 | +import com.google.gson.annotations.SerializedName; |
| 6 | +import com.google.gson.reflect.TypeToken; |
| 7 | + |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.Collections; |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +import static org.junit.Assert.assertEquals; |
| 15 | +import static org.junit.Assert.assertNull; |
| 16 | + |
| 17 | +public class SingleElementSafeListTypeAdapterTest { |
| 18 | + |
| 19 | + @Test |
| 20 | + public void parseEmptyJson() { |
| 21 | + final Gson gson = createGson(); |
| 22 | + assertNull(gson.fromJson("", List.class)); |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + public void parseStringArrayWithMultipleElements() { |
| 27 | + final Gson gson = createGson(); |
| 28 | + final List parsed = gson.fromJson("[\"a\", \"b\", \"c\"]", List.class); |
| 29 | + assertEquals(Arrays.asList("a", "b", "c"), parsed); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void parseStringArrayWithSingleElement() { |
| 34 | + final Gson gson = createGson(); |
| 35 | + final List parsed = gson.fromJson("\"a\"", List.class); |
| 36 | + assertEquals(Collections.singletonList("a"), parsed); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void parseNullJson() { |
| 41 | + final Gson gson = createGson(); |
| 42 | + assertNull(gson.fromJson("null", List.class)); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void parseArrayOfNulls() { |
| 47 | + final Gson gson = createGson(); |
| 48 | + final List parsed = gson.fromJson("[null, null, null]", List.class); |
| 49 | + assertEquals(Arrays.asList(null, null, null), parsed); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void parseBooleanArrayWithMultipleElements() { |
| 54 | + final Gson gson = createGson(); |
| 55 | + final List parsed = gson.fromJson("[true, false]", List.class); |
| 56 | + assertEquals(Arrays.asList(true, false), parsed); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void parseBooleanArrayWithSingleElement() { |
| 61 | + final Gson gson = createGson(); |
| 62 | + final List parsed = gson.fromJson("true", List.class); |
| 63 | + assertEquals(Collections.singletonList(true), parsed); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void parseNumberArrayWithMultipleElements() { |
| 68 | + final Gson gson = createGson(); |
| 69 | + final List parsed = gson.fromJson("[1, 2, 3]", List.class); |
| 70 | + assertEquals(Arrays.asList(1.0, 2.0, 3.0), parsed); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void parseNumberArrayWithSingleElement() { |
| 75 | + final Gson gson = createGson(); |
| 76 | + final List parsed = gson.fromJson("5", List.class); |
| 77 | + assertEquals(Collections.singletonList(5.0), parsed); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void parseCustomTypeArrayWithMultipleElements() { |
| 82 | + final Gson gson = createGson(); |
| 83 | + |
| 84 | + final String inputJson = "[" + |
| 85 | + "{\"string_field\":\"abc\",\"boolean_field\":true,\"int_field\":1}," + |
| 86 | + "{\"string_field\":\"def\",\"boolean_field\":false,\"int_field\":11}" + |
| 87 | + "]"; |
| 88 | + |
| 89 | + final TypeToken typeToken = TypeToken.getParameterized(List.class, TestType.class); |
| 90 | + final List parsed = gson.fromJson(inputJson, typeToken.getType()); |
| 91 | + |
| 92 | + final List<TestType> expectedList = Arrays.asList( |
| 93 | + new TestType("abc", true, 1), |
| 94 | + new TestType("def", false, 11) |
| 95 | + ); |
| 96 | + assertEquals(expectedList, parsed); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void parseCustomTypeArrayWithSingleElement() { |
| 101 | + final Gson gson = createGson(); |
| 102 | + |
| 103 | + final String inputJson = "{\"string_field\":\"abc\",\"boolean_field\":true,\"int_field\":1}"; |
| 104 | + |
| 105 | + final TypeToken typeToken = TypeToken.getParameterized(List.class, TestType.class); |
| 106 | + final List parsed = gson.fromJson(inputJson, typeToken.getType()); |
| 107 | + |
| 108 | + final List<TestType> expectedList = |
| 109 | + Collections.singletonList(new TestType("abc", true, 1)); |
| 110 | + assertEquals(expectedList, parsed); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void serializeCustomTypeArrayWithMultipleElements() { |
| 115 | + final Gson gson = createGson(); |
| 116 | + |
| 117 | + final List<TestType> testData = Arrays.asList( |
| 118 | + new TestType("abc", true, 1), |
| 119 | + new TestType("def", false, 11) |
| 120 | + ); |
| 121 | + |
| 122 | + final String serialized = gson.toJson(testData); |
| 123 | + |
| 124 | + final String expectedJson = "[" + |
| 125 | + "{\"string_field\":\"abc\",\"boolean_field\":true,\"int_field\":1}," + |
| 126 | + "{\"string_field\":\"def\",\"boolean_field\":false,\"int_field\":11}" + |
| 127 | + "]"; |
| 128 | + |
| 129 | + assertEquals(expectedJson, serialized); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + public void serializeCustomTypeArrayWithSingleElement() { |
| 134 | + final Gson gson = createGson(); |
| 135 | + |
| 136 | + final TestType testData = new TestType("abc", true, 1); |
| 137 | + final String serialized = gson.toJson(testData); |
| 138 | + |
| 139 | + final String expectedJson = "{\"string_field\":\"abc\",\"boolean_field\":true,\"int_field\":1}"; |
| 140 | + assertEquals(expectedJson, serialized); |
| 141 | + } |
| 142 | + |
| 143 | + private static Gson createGson() { |
| 144 | + return new GsonBuilder() |
| 145 | + .registerTypeAdapterFactory(SingleElementSafeListTypeAdapter.FACTORY) |
| 146 | + .create(); |
| 147 | + } |
| 148 | + |
| 149 | + private static class TestType { |
| 150 | + |
| 151 | + @SerializedName("string_field") |
| 152 | + private final String stringField; |
| 153 | + |
| 154 | + @SerializedName("boolean_field") |
| 155 | + private final boolean booleanField; |
| 156 | + |
| 157 | + @SerializedName("int_field") |
| 158 | + private final int intField; |
| 159 | + |
| 160 | + TestType(String stringField, boolean booleanField, int intField) { |
| 161 | + this.stringField = stringField; |
| 162 | + this.booleanField = booleanField; |
| 163 | + this.intField = intField; |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public boolean equals(Object o) { |
| 168 | + if (this == o) return true; |
| 169 | + if (o == null || getClass() != o.getClass()) return false; |
| 170 | + |
| 171 | + TestType testType = (TestType) o; |
| 172 | + |
| 173 | + if (booleanField != testType.booleanField) return false; |
| 174 | + if (intField != testType.intField) return false; |
| 175 | + return stringField != null ? stringField.equals(testType.stringField) : testType.stringField == null; |
| 176 | + } |
| 177 | + |
| 178 | + @Override |
| 179 | + public int hashCode() { |
| 180 | + int result = stringField != null ? stringField.hashCode() : 0; |
| 181 | + result = 31 * result + (booleanField ? 1 : 0); |
| 182 | + result = 31 * result + intField; |
| 183 | + return result; |
| 184 | + } |
| 185 | + } |
| 186 | +} |
0 commit comments