PageRenderTime 43ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/java/core/src/test/java/com/google/protobuf/UnmodifiableLazyStringListTest.java

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