aboutsummaryrefslogtreecommitdiff
path: root/gson/src/test/java/com/google/gson/JsonArrayAsListTest.java
blob: a1786ce671deb19df9054cf2ce640daeef84569c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package com.google.gson;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import com.google.gson.common.MoreAsserts;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.Test;

/**
 * Tests for {@link JsonArray#asList()}.
 */
public class JsonArrayAsListTest {
  @Test
  public void testGet() {
    JsonArray a = new JsonArray();
    a.add(1);

    List<JsonElement> list = a.asList();
    assertEquals(new JsonPrimitive(1), list.get(0));

    try {
      list.get(-1);
      fail();
    } catch (IndexOutOfBoundsException e) {
    }

    try {
      list.get(2);
      fail();
    } catch (IndexOutOfBoundsException e) {
    }

    a.add((JsonElement) null);
    assertEquals(JsonNull.INSTANCE, list.get(1));
  }

  @Test
  public void testSize() {
    JsonArray a = new JsonArray();
    a.add(1);

    List<JsonElement> list = a.asList();
    assertEquals(1, list.size());
    list.add(new JsonPrimitive(2));
    assertEquals(2, list.size());
  }

  @Test
  public void testSet() {
    JsonArray a = new JsonArray();
    a.add(1);

    List<JsonElement> list = a.asList();
    JsonElement old = list.set(0, new JsonPrimitive(2));
    assertEquals(new JsonPrimitive(1), old);
    assertEquals(new JsonPrimitive(2), list.get(0));
    assertEquals(new JsonPrimitive(2), a.get(0));

    try {
      list.set(-1, new JsonPrimitive(1));
      fail();
    } catch (IndexOutOfBoundsException e) {
    }

    try {
      list.set(2, new JsonPrimitive(1));
      fail();
    } catch (IndexOutOfBoundsException e) {
    }

    try {
      list.set(0, null);
      fail();
    } catch (NullPointerException e) {
      assertEquals("Element must be non-null", e.getMessage());
    }
  }

  @Test
  public void testAdd() {
    JsonArray a = new JsonArray();
    a.add(1);

    List<JsonElement> list = a.asList();
    list.add(0, new JsonPrimitive(2));
    list.add(1, new JsonPrimitive(3));
    assertTrue(list.add(new JsonPrimitive(4)));
    assertTrue(list.add(JsonNull.INSTANCE));

    List<JsonElement> expectedList = Arrays.<JsonElement>asList(
        new JsonPrimitive(2),
        new JsonPrimitive(3),
        new JsonPrimitive(1),
        new JsonPrimitive(4),
        JsonNull.INSTANCE
    );
    assertEquals(expectedList, list);

    try {
      list.set(-1, new JsonPrimitive(1));
      fail();
    } catch (IndexOutOfBoundsException e) {
    }

    try {
      list.set(list.size(), new JsonPrimitive(1));
      fail();
    } catch (IndexOutOfBoundsException e) {
    }

    try {
      list.add(0, null);
      fail();
    } catch (NullPointerException e) {
      assertEquals("Element must be non-null", e.getMessage());
    }
    try {
      list.add(null);
      fail();
    } catch (NullPointerException e) {
      assertEquals("Element must be non-null", e.getMessage());
    }
  }

  @Test
  public void testAddAll() {
    JsonArray a = new JsonArray();
    a.add(1);

    List<JsonElement> list = a.asList();
    list.addAll(Arrays.asList(new JsonPrimitive(2), new JsonPrimitive(3)));

    List<JsonElement> expectedList = Arrays.<JsonElement>asList(
        new JsonPrimitive(1),
        new JsonPrimitive(2),
        new JsonPrimitive(3)
    );
    assertEquals(expectedList, list);

    try {
      list.addAll(0, Collections.<JsonElement>singletonList(null));
      fail();
    } catch (NullPointerException e) {
      assertEquals("Element must be non-null", e.getMessage());
    }
    try {
      list.addAll(Collections.<JsonElement>singletonList(null));
      fail();
    } catch (NullPointerException e) {
      assertEquals("Element must be non-null", e.getMessage());
    }
  }

  @Test
  public void testRemoveIndex() {
    JsonArray a = new JsonArray();
    a.add(1);

    List<JsonElement> list = a.asList();
    assertEquals(new JsonPrimitive(1), list.remove(0));
    assertEquals(0, list.size());
    assertEquals(0, a.size());

    try {
      list.remove(0);
      fail();
    } catch (IndexOutOfBoundsException e) {
    }
  }

  @Test
  public void testRemoveElement() {
    JsonArray a = new JsonArray();
    a.add(1);

    List<JsonElement> list = a.asList();
    assertTrue(list.remove(new JsonPrimitive(1)));
    assertEquals(0, list.size());
    assertEquals(0, a.size());

    assertFalse(list.remove(new JsonPrimitive(1)));
    assertFalse(list.remove(null));
  }

  @Test
  public void testClear() {
    JsonArray a = new JsonArray();
    a.add(1);

    List<JsonElement> list = a.asList();
    list.clear();
    assertEquals(0, list.size());
    assertEquals(0, a.size());
  }

  @Test
  public void testContains() {
    JsonArray a = new JsonArray();
    a.add(1);

    List<JsonElement> list = a.asList();
    assertTrue(list.contains(new JsonPrimitive(1)));
    assertFalse(list.contains(new JsonPrimitive(2)));
    assertFalse(list.contains(null));

    @SuppressWarnings({"unlikely-arg-type", "CollectionIncompatibleType"})
    boolean containsInt = list.contains(1); // should only contain JsonPrimitive(1)
    assertFalse(containsInt);
  }

  @Test
  public void testIndexOf() {
    JsonArray a = new JsonArray();
    // Add the same value twice to test indexOf vs. lastIndexOf
    a.add(1);
    a.add(1);

    List<JsonElement> list = a.asList();
    assertEquals(0, list.indexOf(new JsonPrimitive(1)));
    assertEquals(-1, list.indexOf(new JsonPrimitive(2)));
    assertEquals(-1, list.indexOf(null));

    @SuppressWarnings({"unlikely-arg-type", "CollectionIncompatibleType"})
    int indexOfInt = list.indexOf(1); // should only contain JsonPrimitive(1)
    assertEquals(-1, indexOfInt);

    assertEquals(1, list.lastIndexOf(new JsonPrimitive(1)));
    assertEquals(-1, list.lastIndexOf(new JsonPrimitive(2)));
    assertEquals(-1, list.lastIndexOf(null));
  }

  @Test
  public void testToArray() {
    JsonArray a = new JsonArray();
    a.add(1);

    List<JsonElement> list = a.asList();
    assertArrayEquals(new Object[] {new JsonPrimitive(1)}, list.toArray());

    JsonElement[] array = list.toArray(new JsonElement[0]);
    assertArrayEquals(new Object[] {new JsonPrimitive(1)}, array);

    array = new JsonElement[1];
    assertSame(array, list.toArray(array));
    assertArrayEquals(new Object[] {new JsonPrimitive(1)}, array);

    array = new JsonElement[] {null, new JsonPrimitive(2)};
    assertSame(array, list.toArray(array));
    // Should have set existing array element to null
    assertArrayEquals(new Object[] {new JsonPrimitive(1), null}, array);
  }

  @Test
  public void testEqualsHashCode() {
    JsonArray a = new JsonArray();
    a.add(1);

    List<JsonElement> list = a.asList();
    MoreAsserts.assertEqualsAndHashCode(list, Collections.singletonList(new JsonPrimitive(1)));
    assertFalse(list.equals(Collections.emptyList()));
    assertFalse(list.equals(Collections.singletonList(new JsonPrimitive(2))));
  }

  /** Verify that {@code JsonArray} updates are visible to view and vice versa */
  @Test
  public void testViewUpdates() {
    JsonArray a = new JsonArray();
    List<JsonElement> list = a.asList();

    a.add(1);
    assertEquals(1, list.size());
    assertEquals(new JsonPrimitive(1), list.get(0));

    list.add(new JsonPrimitive(2));
    assertEquals(2, a.size());
    assertEquals(new JsonPrimitive(2), a.get(1));
  }
}