OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // http://code.google.com/p/protobuf/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
11 // * Redistributions in binary form must reproduce the above | 11 // * Redistributions in binary form must reproduce the above |
12 // copyright notice, this list of conditions and the following disclaimer | 12 // copyright notice, this list of conditions and the following disclaimer |
13 // in the documentation and/or other materials provided with the | 13 // in the documentation and/or other materials provided with the |
14 // distribution. | 14 // distribution. |
15 // * Neither the name of Google Inc. nor the names of its | 15 // * Neither the name of Google Inc. nor the names of its |
16 // contributors may be used to endorse or promote products derived from | 16 // contributors may be used to endorse or promote products derived from |
17 // this software without specific prior written permission. | 17 // this software without specific prior written permission. |
18 // | 18 // |
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | 30 |
31 package com.google.protobuf; | 31 package com.google.protobuf.test; |
| 32 import com.google.protobuf.*; |
32 | 33 |
33 import junit.framework.TestCase; | 34 import junit.framework.TestCase; |
34 | 35 |
35 import java.util.Iterator; | 36 import java.util.Iterator; |
36 import java.util.List; | |
37 import java.util.ListIterator; | 37 import java.util.ListIterator; |
38 | 38 |
39 /** | 39 /** |
40 * Tests for {@link UnmodifiableLazyStringList}. | 40 * Tests for {@link UnmodifiableLazyStringList}. |
41 * | 41 * |
42 * @author jonp@google.com (Jon Perlow) | 42 * @author jonp@google.com (Jon Perlow) |
43 */ | 43 */ |
44 public class UnmodifiableLazyStringListTest extends TestCase { | 44 public class UnmodifiableLazyStringListTest extends TestCase { |
45 | 45 |
46 private static String STRING_A = "A"; | 46 private static String STRING_A = "A"; |
47 private static String STRING_B = "B"; | 47 private static String STRING_B = "B"; |
48 private static String STRING_C = "C"; | 48 private static String STRING_C = "C"; |
49 | 49 |
50 private static ByteString BYTE_STRING_A = ByteString.copyFromUtf8("A"); | 50 private static ByteString BYTE_STRING_A = ByteString.copyFromUtf8("A"); |
51 private static ByteString BYTE_STRING_B = ByteString.copyFromUtf8("B"); | 51 private static ByteString BYTE_STRING_B = ByteString.copyFromUtf8("B"); |
52 private static ByteString BYTE_STRING_C = ByteString.copyFromUtf8("C"); | 52 private static ByteString BYTE_STRING_C = ByteString.copyFromUtf8("C"); |
53 | 53 |
54 public void testReadOnlyMethods() { | 54 public void testReadOnlyMethods() { |
55 LazyStringArrayList rawList = createSampleList(); | 55 LazyStringArrayList rawList = createSampleList(); |
56 UnmodifiableLazyStringList list = new UnmodifiableLazyStringList(rawList); | 56 UnmodifiableLazyStringList list = new UnmodifiableLazyStringList(rawList); |
57 assertEquals(3, list.size()); | 57 assertEquals(3, list.size()); |
58 assertSame(STRING_A, list.get(0)); | 58 assertSame(STRING_A, list.get(0)); |
59 assertSame(STRING_B, list.get(1)); | 59 assertSame(STRING_B, list.get(1)); |
60 assertSame(STRING_C, list.get(2)); | 60 assertSame(STRING_C, list.get(2)); |
61 assertEquals(BYTE_STRING_A, list.getByteString(0)); | 61 assertEquals(BYTE_STRING_A, list.getByteString(0)); |
62 assertEquals(BYTE_STRING_B, list.getByteString(1)); | 62 assertEquals(BYTE_STRING_B, list.getByteString(1)); |
63 assertEquals(BYTE_STRING_C, list.getByteString(2)); | 63 assertEquals(BYTE_STRING_C, list.getByteString(2)); |
64 | |
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)); | |
69 } | 64 } |
70 | 65 |
71 public void testModifyMethods() { | 66 public void testModifyMethods() { |
72 LazyStringArrayList rawList = createSampleList(); | 67 LazyStringArrayList rawList = createSampleList(); |
73 UnmodifiableLazyStringList list = new UnmodifiableLazyStringList(rawList); | 68 UnmodifiableLazyStringList list = new UnmodifiableLazyStringList(rawList); |
74 | 69 |
75 try { | 70 try { |
76 list.remove(0); | 71 list.remove(0); |
77 fail(); | 72 fail(); |
78 } catch (UnsupportedOperationException e) { | 73 } catch (UnsupportedOperationException e) { |
79 // expected | 74 // expected |
80 } | 75 } |
81 assertEquals(3, list.size()); | 76 assertEquals(3, list.size()); |
82 | 77 |
83 try { | 78 try { |
84 list.add(STRING_B); | 79 list.add(STRING_B); |
85 fail(); | 80 fail(); |
86 } catch (UnsupportedOperationException e) { | 81 } catch (UnsupportedOperationException e) { |
87 // expected | 82 // expected |
88 } | 83 } |
89 assertEquals(3, list.size()); | 84 assertEquals(3, list.size()); |
90 | 85 |
91 try { | 86 try { |
92 list.set(1, STRING_B); | 87 list.set(1, STRING_B); |
93 fail(); | 88 fail(); |
94 } catch (UnsupportedOperationException e) { | 89 } catch (UnsupportedOperationException e) { |
95 // expected | 90 // expected |
96 } | 91 } |
97 assertEquals(3, list.size()); | |
98 | |
99 List<ByteString> byteStringList = list.asByteStringList(); | |
100 try { | |
101 byteStringList.remove(0); | |
102 fail(); | |
103 } catch (UnsupportedOperationException e) { | |
104 // expected | |
105 } | |
106 assertEquals(3, list.size()); | |
107 assertEquals(3, byteStringList.size()); | |
108 | |
109 try { | |
110 byteStringList.add(BYTE_STRING_B); | |
111 fail(); | |
112 } catch (UnsupportedOperationException e) { | |
113 // expected | |
114 } | |
115 assertEquals(3, list.size()); | |
116 assertEquals(3, byteStringList.size()); | |
117 | |
118 try { | |
119 byteStringList.set(1, BYTE_STRING_B); | |
120 fail(); | |
121 } catch (UnsupportedOperationException e) { | |
122 // expected | |
123 } | |
124 assertEquals(3, list.size()); | |
125 assertEquals(3, byteStringList.size()); | |
126 } | 92 } |
127 | 93 |
128 public void testIterator() { | 94 public void testIterator() { |
129 LazyStringArrayList rawList = createSampleList(); | 95 LazyStringArrayList rawList = createSampleList(); |
130 UnmodifiableLazyStringList list = new UnmodifiableLazyStringList(rawList); | 96 UnmodifiableLazyStringList list = new UnmodifiableLazyStringList(rawList); |
131 | 97 |
132 Iterator<String> iter = list.iterator(); | 98 Iterator<String> iter = list.iterator(); |
133 int count = 0; | 99 int count = 0; |
134 while (iter.hasNext()) { | 100 while (iter.hasNext()) { |
135 iter.next(); | 101 iter.next(); |
136 count++; | 102 count++; |
137 try { | 103 try { |
138 iter.remove(); | 104 iter.remove(); |
139 fail(); | 105 fail(); |
140 } catch (UnsupportedOperationException e) { | 106 } catch (UnsupportedOperationException e) { |
141 // expected | 107 // expected |
142 } | 108 } |
143 } | 109 } |
144 assertEquals(3, count); | 110 assertEquals(3, count); |
145 | 111 |
146 List<ByteString> byteStringList = list.asByteStringList(); | |
147 Iterator<ByteString> byteIter = byteStringList.iterator(); | |
148 count = 0; | |
149 while (byteIter.hasNext()) { | |
150 byteIter.next(); | |
151 count++; | |
152 try { | |
153 byteIter.remove(); | |
154 fail(); | |
155 } catch (UnsupportedOperationException e) { | |
156 // expected | |
157 } | |
158 } | |
159 assertEquals(3, count); | |
160 } | 112 } |
161 | 113 |
162 public void testListIterator() { | 114 public void testListIterator() { |
163 LazyStringArrayList rawList = createSampleList(); | 115 LazyStringArrayList rawList = createSampleList(); |
164 UnmodifiableLazyStringList list = new UnmodifiableLazyStringList(rawList); | 116 UnmodifiableLazyStringList list = new UnmodifiableLazyStringList(rawList); |
165 | 117 |
166 ListIterator<String> iter = list.listIterator(); | 118 ListIterator<String> iter = list.listIterator(); |
167 int count = 0; | 119 int count = 0; |
168 while (iter.hasNext()) { | 120 while (iter.hasNext()) { |
169 iter.next(); | 121 iter.next(); |
(...skipping 12 matching lines...) Expand all Loading... |
182 } | 134 } |
183 try { | 135 try { |
184 iter.add("bar"); | 136 iter.add("bar"); |
185 fail(); | 137 fail(); |
186 } catch (UnsupportedOperationException e) { | 138 } catch (UnsupportedOperationException e) { |
187 // expected | 139 // expected |
188 } | 140 } |
189 } | 141 } |
190 assertEquals(3, count); | 142 assertEquals(3, count); |
191 | 143 |
192 List<ByteString> byteStringList = list.asByteStringList(); | |
193 ListIterator<ByteString> byteIter = byteStringList.listIterator(); | |
194 count = 0; | |
195 while (byteIter.hasNext()) { | |
196 byteIter.next(); | |
197 count++; | |
198 try { | |
199 byteIter.remove(); | |
200 fail(); | |
201 } catch (UnsupportedOperationException e) { | |
202 // expected | |
203 } | |
204 try { | |
205 byteIter.set(BYTE_STRING_A); | |
206 fail(); | |
207 } catch (UnsupportedOperationException e) { | |
208 // expected | |
209 } | |
210 try { | |
211 byteIter.add(BYTE_STRING_A); | |
212 fail(); | |
213 } catch (UnsupportedOperationException e) { | |
214 // expected | |
215 } | |
216 } | |
217 assertEquals(3, count); | |
218 } | 144 } |
219 | 145 |
220 private LazyStringArrayList createSampleList() { | 146 private LazyStringArrayList createSampleList() { |
221 LazyStringArrayList rawList = new LazyStringArrayList(); | 147 LazyStringArrayList rawList = new LazyStringArrayList(); |
222 rawList.add(STRING_A); | 148 rawList.add(STRING_A); |
223 rawList.add(STRING_B); | 149 rawList.add(STRING_B); |
224 rawList.add(STRING_C); | 150 rawList.add(STRING_C); |
225 return rawList; | 151 return rawList; |
226 } | 152 } |
227 } | 153 } |
OLD | NEW |