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 junit.framework.TestCase; 34 35 import java.util.Iterator; 36 import java.util.List; 37 import java.util.ListIterator; 38 39 /** 40 * Tests for {@link UnmodifiableLazyStringList}. 41 * 42 * @author jonp@google.com (Jon Perlow) 43 */ 44 public class UnmodifiableLazyStringListTest extends TestCase { 45 46 private static String STRING_A = "A"; 47 private static String STRING_B = "B"; 48 private static String STRING_C = "C"; 49 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"); 53 testReadOnlyMethods()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)); 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 } 70 testModifyMethods()71 public void testModifyMethods() { 72 LazyStringArrayList rawList = createSampleList(); 73 UnmodifiableLazyStringList list = new UnmodifiableLazyStringList(rawList); 74 75 try { 76 list.remove(0); 77 fail(); 78 } catch (UnsupportedOperationException e) { 79 // expected 80 } 81 assertEquals(3, list.size()); 82 83 try { 84 list.add(STRING_B); 85 fail(); 86 } catch (UnsupportedOperationException e) { 87 // expected 88 } 89 assertEquals(3, list.size()); 90 91 try { 92 list.set(1, STRING_B); 93 fail(); 94 } catch (UnsupportedOperationException e) { 95 // expected 96 } 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 } 127 testIterator()128 public void testIterator() { 129 LazyStringArrayList rawList = createSampleList(); 130 UnmodifiableLazyStringList list = new UnmodifiableLazyStringList(rawList); 131 132 Iterator<String> iter = list.iterator(); 133 int count = 0; 134 while (iter.hasNext()) { 135 iter.next(); 136 count++; 137 try { 138 iter.remove(); 139 fail(); 140 } catch (UnsupportedOperationException e) { 141 // expected 142 } 143 } 144 assertEquals(3, count); 145 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 } 161 testListIterator()162 public void testListIterator() { 163 LazyStringArrayList rawList = createSampleList(); 164 UnmodifiableLazyStringList list = new UnmodifiableLazyStringList(rawList); 165 166 ListIterator<String> iter = list.listIterator(); 167 int count = 0; 168 while (iter.hasNext()) { 169 iter.next(); 170 count++; 171 try { 172 iter.remove(); 173 fail(); 174 } catch (UnsupportedOperationException e) { 175 // expected 176 } 177 try { 178 iter.set("bar"); 179 fail(); 180 } catch (UnsupportedOperationException e) { 181 // expected 182 } 183 try { 184 iter.add("bar"); 185 fail(); 186 } catch (UnsupportedOperationException e) { 187 // expected 188 } 189 } 190 assertEquals(3, count); 191 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 } 219 createSampleList()220 private LazyStringArrayList createSampleList() { 221 LazyStringArrayList rawList = new LazyStringArrayList(); 222 rawList.add(STRING_A); 223 rawList.add(STRING_B); 224 rawList.add(STRING_C); 225 return rawList; 226 } 227 } 228