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 java.util.Iterator; 34 import java.util.List; 35 import java.util.ListIterator; 36 import junit.framework.TestCase; 37 38 /** 39 * Tests for {@link UnmodifiableLazyStringList}. 40 * 41 * @author jonp@google.com (Jon Perlow) 42 */ 43 public class UnmodifiableLazyStringListTest extends TestCase { 44 45 private static final String STRING_A = "A"; 46 private static final String STRING_B = "B"; 47 private static final String STRING_C = "C"; 48 49 private static final ByteString BYTE_STRING_A = ByteString.copyFromUtf8("A"); 50 private static final ByteString BYTE_STRING_B = ByteString.copyFromUtf8("B"); 51 private static final ByteString BYTE_STRING_C = ByteString.copyFromUtf8("C"); 52 testReadOnlyMethods()53 public void testReadOnlyMethods() { 54 LazyStringArrayList rawList = createSampleList(); 55 UnmodifiableLazyStringList list = new UnmodifiableLazyStringList(rawList); 56 assertEquals(3, list.size()); 57 assertSame(STRING_A, list.get(0)); 58 assertSame(STRING_B, list.get(1)); 59 assertSame(STRING_C, list.get(2)); 60 assertEquals(BYTE_STRING_A, list.getByteString(0)); 61 assertEquals(BYTE_STRING_B, list.getByteString(1)); 62 assertEquals(BYTE_STRING_C, list.getByteString(2)); 63 64 List<ByteString> byteStringList = list.asByteStringList(); 65 assertSame(list.getByteString(0), byteStringList.get(0)); 66 assertSame(list.getByteString(1), byteStringList.get(1)); 67 assertSame(list.getByteString(2), byteStringList.get(2)); 68 } 69 testModifyMethods()70 public void testModifyMethods() { 71 LazyStringArrayList rawList = createSampleList(); 72 UnmodifiableLazyStringList list = new UnmodifiableLazyStringList(rawList); 73 74 try { 75 list.remove(0); 76 fail(); 77 } catch (UnsupportedOperationException e) { 78 // expected 79 } 80 assertEquals(3, list.size()); 81 82 try { 83 list.add(STRING_B); 84 fail(); 85 } catch (UnsupportedOperationException e) { 86 // expected 87 } 88 assertEquals(3, list.size()); 89 90 try { 91 list.set(1, STRING_B); 92 fail(); 93 } catch (UnsupportedOperationException e) { 94 // expected 95 } 96 assertEquals(3, list.size()); 97 98 List<ByteString> byteStringList = list.asByteStringList(); 99 try { 100 byteStringList.remove(0); 101 fail(); 102 } catch (UnsupportedOperationException e) { 103 // expected 104 } 105 assertEquals(3, list.size()); 106 assertEquals(3, byteStringList.size()); 107 108 try { 109 byteStringList.add(BYTE_STRING_B); 110 fail(); 111 } catch (UnsupportedOperationException e) { 112 // expected 113 } 114 assertEquals(3, list.size()); 115 assertEquals(3, byteStringList.size()); 116 117 try { 118 byteStringList.set(1, BYTE_STRING_B); 119 fail(); 120 } catch (UnsupportedOperationException e) { 121 // expected 122 } 123 assertEquals(3, list.size()); 124 assertEquals(3, byteStringList.size()); 125 } 126 testIterator()127 public void testIterator() { 128 LazyStringArrayList rawList = createSampleList(); 129 UnmodifiableLazyStringList list = new UnmodifiableLazyStringList(rawList); 130 131 Iterator<String> iter = list.iterator(); 132 int count = 0; 133 while (iter.hasNext()) { 134 iter.next(); 135 count++; 136 try { 137 iter.remove(); 138 fail(); 139 } catch (UnsupportedOperationException e) { 140 // expected 141 } 142 } 143 assertEquals(3, count); 144 145 List<ByteString> byteStringList = list.asByteStringList(); 146 Iterator<ByteString> byteIter = byteStringList.iterator(); 147 count = 0; 148 while (byteIter.hasNext()) { 149 byteIter.next(); 150 count++; 151 try { 152 byteIter.remove(); 153 fail(); 154 } catch (UnsupportedOperationException e) { 155 // expected 156 } 157 } 158 assertEquals(3, count); 159 } 160 testListIterator()161 public void testListIterator() { 162 LazyStringArrayList rawList = createSampleList(); 163 UnmodifiableLazyStringList list = new UnmodifiableLazyStringList(rawList); 164 165 ListIterator<String> iter = list.listIterator(); 166 int count = 0; 167 while (iter.hasNext()) { 168 iter.next(); 169 count++; 170 try { 171 iter.remove(); 172 fail(); 173 } catch (UnsupportedOperationException e) { 174 // expected 175 } 176 try { 177 iter.set("bar"); 178 fail(); 179 } catch (UnsupportedOperationException e) { 180 // expected 181 } 182 try { 183 iter.add("bar"); 184 fail(); 185 } catch (UnsupportedOperationException e) { 186 // expected 187 } 188 } 189 assertEquals(3, count); 190 191 List<ByteString> byteStringList = list.asByteStringList(); 192 ListIterator<ByteString> byteIter = byteStringList.listIterator(); 193 count = 0; 194 while (byteIter.hasNext()) { 195 byteIter.next(); 196 count++; 197 try { 198 byteIter.remove(); 199 fail(); 200 } catch (UnsupportedOperationException e) { 201 // expected 202 } 203 try { 204 byteIter.set(BYTE_STRING_A); 205 fail(); 206 } catch (UnsupportedOperationException e) { 207 // expected 208 } 209 try { 210 byteIter.add(BYTE_STRING_A); 211 fail(); 212 } catch (UnsupportedOperationException e) { 213 // expected 214 } 215 } 216 assertEquals(3, count); 217 } 218 createSampleList()219 private LazyStringArrayList createSampleList() { 220 LazyStringArrayList rawList = new LazyStringArrayList(); 221 rawList.add(STRING_A); 222 rawList.add(STRING_B); 223 rawList.add(STRING_C); 224 return rawList; 225 } 226 } 227