1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
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
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.
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.
31 package com.google.protobuf;
33 import junit.framework.TestCase;
35 import java.util.Iterator;
36 import java.util.List;
37 import java.util.ListIterator;
40 * Tests for {@link UnmodifiableLazyStringList}.
42 * @author jonp@google.com (Jon Perlow)
44 public class UnmodifiableLazyStringListTest extends TestCase {
46 private static String STRING_A = "A";
47 private static String STRING_B = "B";
48 private static String STRING_C = "C";
50 private static ByteString BYTE_STRING_A = ByteString.copyFromUtf8("A");
51 private static ByteString BYTE_STRING_B = ByteString.copyFromUtf8("B");
52 private static ByteString BYTE_STRING_C = ByteString.copyFromUtf8("C");
54 public void testReadOnlyMethods() {
55 LazyStringArrayList rawList = createSampleList();
56 UnmodifiableLazyStringList list = new UnmodifiableLazyStringList(rawList);
57 assertEquals(3, list.size());
58 assertSame(STRING_A, list.get(0));
59 assertSame(STRING_B, list.get(1));
60 assertSame(STRING_C, list.get(2));
61 assertEquals(BYTE_STRING_A, list.getByteString(0));
62 assertEquals(BYTE_STRING_B, list.getByteString(1));
63 assertEquals(BYTE_STRING_C, list.getByteString(2));
65 List<ByteString> byteStringList = list.asByteStringList();
66 assertSame(list.getByteString(0), byteStringList.get(0));
67 assertSame(list.getByteString(1), byteStringList.get(1));
68 assertSame(list.getByteString(2), byteStringList.get(2));
71 public void testModifyMethods() {
72 LazyStringArrayList rawList = createSampleList();
73 UnmodifiableLazyStringList list = new UnmodifiableLazyStringList(rawList);
78 } catch (UnsupportedOperationException e) {
81 assertEquals(3, list.size());
86 } catch (UnsupportedOperationException e) {
89 assertEquals(3, list.size());
92 list.set(1, STRING_B);
94 } catch (UnsupportedOperationException e) {
97 assertEquals(3, list.size());
99 List<ByteString> byteStringList = list.asByteStringList();
101 byteStringList.remove(0);
103 } catch (UnsupportedOperationException e) {
106 assertEquals(3, list.size());
107 assertEquals(3, byteStringList.size());
110 byteStringList.add(BYTE_STRING_B);
112 } catch (UnsupportedOperationException e) {
115 assertEquals(3, list.size());
116 assertEquals(3, byteStringList.size());
119 byteStringList.set(1, BYTE_STRING_B);
121 } catch (UnsupportedOperationException e) {
124 assertEquals(3, list.size());
125 assertEquals(3, byteStringList.size());
128 public void testIterator() {
129 LazyStringArrayList rawList = createSampleList();
130 UnmodifiableLazyStringList list = new UnmodifiableLazyStringList(rawList);
132 Iterator<String> iter = list.iterator();
134 while (iter.hasNext()) {
140 } catch (UnsupportedOperationException e) {
144 assertEquals(3, count);
146 List<ByteString> byteStringList = list.asByteStringList();
147 Iterator<ByteString> byteIter = byteStringList.iterator();
149 while (byteIter.hasNext()) {
155 } catch (UnsupportedOperationException e) {
159 assertEquals(3, count);
162 public void testListIterator() {
163 LazyStringArrayList rawList = createSampleList();
164 UnmodifiableLazyStringList list = new UnmodifiableLazyStringList(rawList);
166 ListIterator<String> iter = list.listIterator();
168 while (iter.hasNext()) {
174 } catch (UnsupportedOperationException e) {
180 } catch (UnsupportedOperationException e) {
186 } catch (UnsupportedOperationException e) {
190 assertEquals(3, count);
192 List<ByteString> byteStringList = list.asByteStringList();
193 ListIterator<ByteString> byteIter = byteStringList.listIterator();
195 while (byteIter.hasNext()) {
201 } catch (UnsupportedOperationException e) {
205 byteIter.set(BYTE_STRING_A);
207 } catch (UnsupportedOperationException e) {
211 byteIter.add(BYTE_STRING_A);
213 } catch (UnsupportedOperationException e) {
217 assertEquals(3, count);
220 private LazyStringArrayList createSampleList() {
221 LazyStringArrayList rawList = new LazyStringArrayList();
222 rawList.add(STRING_A);
223 rawList.add(STRING_B);
224 rawList.add(STRING_C);