• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //     * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 //     * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 package com.google.protobuf;
32 
33 import static java.util.Arrays.asList;
34 
35 import junit.framework.TestCase;
36 
37 import java.util.Collections;
38 import java.util.ConcurrentModificationException;
39 import java.util.Iterator;
40 import java.util.List;
41 
42 /**
43  * Tests for {@link ProtobufArrayList}.
44  */
45 public class ProtobufArrayListTest extends TestCase {
46 
47   private static final ProtobufArrayList<Integer> UNARY_LIST = newImmutableProtoArrayList(1);
48   private static final ProtobufArrayList<Integer> TERTIARY_LIST =
49       newImmutableProtoArrayList(1, 2, 3);
50 
51   private ProtobufArrayList<Integer> list;
52 
53   @Override
setUp()54   protected void setUp() throws Exception {
55     list = new ProtobufArrayList<Integer>();
56   }
57 
testEmptyListReturnsSameInstance()58   public void testEmptyListReturnsSameInstance() {
59     assertSame(ProtobufArrayList.emptyList(), ProtobufArrayList.emptyList());
60   }
61 
testEmptyListIsImmutable()62   public void testEmptyListIsImmutable() {
63     assertImmutable(ProtobufArrayList.<Integer>emptyList());
64   }
65 
testModificationWithIteration()66   public void testModificationWithIteration() {
67     list.addAll(asList(1, 2, 3, 4));
68     Iterator<Integer> iterator = list.iterator();
69     assertEquals(4, list.size());
70     assertEquals(1, (int) list.get(0));
71     assertEquals(1, (int) iterator.next());
72 
73     list.remove(0);
74     try {
75       iterator.next();
76       fail();
77     } catch (ConcurrentModificationException e) {
78       // expected
79     }
80 
81     iterator = list.iterator();
82     list.set(0, 1);
83     try {
84       iterator.next();
85       fail();
86     } catch (ConcurrentModificationException e) {
87       // expected
88     }
89 
90     iterator = list.iterator();
91     list.add(0, 0);
92     try {
93       iterator.next();
94       fail();
95     } catch (ConcurrentModificationException e) {
96       // expected
97     }
98   }
99 
testMakeImmutable()100   public void testMakeImmutable() {
101     list.add(2);
102     list.add(4);
103     list.add(6);
104     list.add(8);
105     list.makeImmutable();
106     assertImmutable(list);
107   }
108 
testRemove()109   public void testRemove() {
110     list.add(2);
111     list.add(4);
112     list.add(6);
113 
114     list.remove(1);
115     assertEquals(asList(2, 6), list);
116 
117     list.remove(1);
118     assertEquals(asList(2), list);
119 
120     list.remove(0);
121     assertEquals(asList(), list);
122   }
123 
testGet()124   public void testGet() {
125     list.add(2);
126     list.add(6);
127 
128     assertEquals(2, (int) list.get(0));
129     assertEquals(6, (int) list.get(1));
130   }
131 
testSet()132   public void testSet() {
133     list.add(2);
134     list.add(6);
135 
136     list.set(0, 1);
137     assertEquals(1, (int) list.get(0));
138     list.set(1, 2);
139     assertEquals(2, (int) list.get(1));
140   }
141 
assertImmutable(List<Integer> list)142   private void assertImmutable(List<Integer> list) {
143     if (list.contains(1)) {
144       throw new RuntimeException("Cannot test the immutability of lists that contain 1.");
145     }
146 
147     try {
148       list.add(1);
149       fail();
150     } catch (UnsupportedOperationException e) {
151       // expected
152     }
153 
154     try {
155       list.add(0, 1);
156       fail();
157     } catch (UnsupportedOperationException e) {
158       // expected
159     }
160 
161     try {
162       list.addAll(Collections.<Integer>emptyList());
163       fail();
164     } catch (UnsupportedOperationException e) {
165       // expected
166     }
167 
168     try {
169       list.addAll(Collections.singletonList(1));
170       fail();
171     } catch (UnsupportedOperationException e) {
172       // expected
173     }
174 
175     try {
176       list.addAll(new ProtobufArrayList<Integer>());
177       fail();
178     } catch (UnsupportedOperationException e) {
179       // expected
180     }
181 
182     try {
183       list.addAll(UNARY_LIST);
184       fail();
185     } catch (UnsupportedOperationException e) {
186       // expected
187     }
188 
189     try {
190       list.addAll(0, Collections.singleton(1));
191       fail();
192     } catch (UnsupportedOperationException e) {
193       // expected
194     }
195 
196     try {
197       list.addAll(0, UNARY_LIST);
198       fail();
199     } catch (UnsupportedOperationException e) {
200       // expected
201     }
202 
203     try {
204       list.addAll(0, Collections.<Integer>emptyList());
205       fail();
206     } catch (UnsupportedOperationException e) {
207       // expected
208     }
209 
210     try {
211       list.clear();
212       fail();
213     } catch (UnsupportedOperationException e) {
214       // expected
215     }
216 
217     try {
218       list.remove(1);
219       fail();
220     } catch (UnsupportedOperationException e) {
221       // expected
222     }
223 
224     try {
225       list.remove(new Object());
226       fail();
227     } catch (UnsupportedOperationException e) {
228       // expected
229     }
230 
231     try {
232       list.removeAll(Collections.emptyList());
233       fail();
234     } catch (UnsupportedOperationException e) {
235       // expected
236     }
237 
238     try {
239       list.removeAll(Collections.singleton(1));
240       fail();
241     } catch (UnsupportedOperationException e) {
242       // expected
243     }
244 
245     try {
246       list.removeAll(UNARY_LIST);
247       fail();
248     } catch (UnsupportedOperationException e) {
249       // expected
250     }
251 
252     try {
253       list.retainAll(Collections.emptyList());
254       fail();
255     } catch (UnsupportedOperationException e) {
256       // expected
257     }
258 
259     try {
260       list.retainAll(Collections.singleton(1));
261       fail();
262     } catch (UnsupportedOperationException e) {
263       // expected
264     }
265 
266     try {
267       list.retainAll(UNARY_LIST);
268       fail();
269     } catch (UnsupportedOperationException e) {
270       // expected
271     }
272 
273     try {
274       list.set(0, 0);
275       fail();
276     } catch (UnsupportedOperationException e) {
277       // expected
278     }
279   }
280 
newImmutableProtoArrayList(int... elements)281   private static ProtobufArrayList<Integer> newImmutableProtoArrayList(int... elements) {
282     ProtobufArrayList<Integer> list = new ProtobufArrayList<Integer>();
283     for (int element : elements) {
284       list.add(element);
285     }
286     list.makeImmutable();
287     return list;
288   }
289 }
290