/thirdparty/breakpad/third_party/protobuf/protobuf/java/src/test/java/com/google/protobuf/UnmodifiableLazyStringListTest.java

http://github.com/tomahawk-player/tomahawk · Java · 152 lines · 93 code · 18 blank · 41 comment · 2 complexity · 4fef49ffba6d58486712db043448f69a MD5 · raw file

  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://code.google.com/p/protobuf/
  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.ListIterator;
  34. /**
  35. * Tests for {@link UnmodifiableLazyStringList}.
  36. *
  37. * @author jonp@google.com (Jon Perlow)
  38. */
  39. public class UnmodifiableLazyStringListTest extends TestCase {
  40. private static String STRING_A = "A";
  41. private static String STRING_B = "B";
  42. private static String STRING_C = "C";
  43. private static ByteString BYTE_STRING_A = ByteString.copyFromUtf8("A");
  44. private static ByteString BYTE_STRING_B = ByteString.copyFromUtf8("B");
  45. private static ByteString BYTE_STRING_C = ByteString.copyFromUtf8("C");
  46. public void testReadOnlyMethods() {
  47. LazyStringArrayList rawList = createSampleList();
  48. UnmodifiableLazyStringList list = new UnmodifiableLazyStringList(rawList);
  49. assertEquals(3, list.size());
  50. assertSame(STRING_A, list.get(0));
  51. assertSame(STRING_B, list.get(1));
  52. assertSame(STRING_C, list.get(2));
  53. assertEquals(BYTE_STRING_A, list.getByteString(0));
  54. assertEquals(BYTE_STRING_B, list.getByteString(1));
  55. assertEquals(BYTE_STRING_C, list.getByteString(2));
  56. }
  57. public void testModifyMethods() {
  58. LazyStringArrayList rawList = createSampleList();
  59. UnmodifiableLazyStringList list = new UnmodifiableLazyStringList(rawList);
  60. try {
  61. list.remove(0);
  62. fail();
  63. } catch (UnsupportedOperationException e) {
  64. // expected
  65. }
  66. assertEquals(3, list.size());
  67. try {
  68. list.add(STRING_B);
  69. fail();
  70. } catch (UnsupportedOperationException e) {
  71. // expected
  72. }
  73. assertEquals(3, list.size());
  74. try {
  75. list.set(1, STRING_B);
  76. fail();
  77. } catch (UnsupportedOperationException e) {
  78. // expected
  79. }
  80. }
  81. public void testIterator() {
  82. LazyStringArrayList rawList = createSampleList();
  83. UnmodifiableLazyStringList list = new UnmodifiableLazyStringList(rawList);
  84. Iterator<String> iter = list.iterator();
  85. int count = 0;
  86. while (iter.hasNext()) {
  87. iter.next();
  88. count++;
  89. try {
  90. iter.remove();
  91. fail();
  92. } catch (UnsupportedOperationException e) {
  93. // expected
  94. }
  95. }
  96. assertEquals(3, count);
  97. }
  98. public void testListIterator() {
  99. LazyStringArrayList rawList = createSampleList();
  100. UnmodifiableLazyStringList list = new UnmodifiableLazyStringList(rawList);
  101. ListIterator<String> iter = list.listIterator();
  102. int count = 0;
  103. while (iter.hasNext()) {
  104. iter.next();
  105. count++;
  106. try {
  107. iter.remove();
  108. fail();
  109. } catch (UnsupportedOperationException e) {
  110. // expected
  111. }
  112. try {
  113. iter.set("bar");
  114. fail();
  115. } catch (UnsupportedOperationException e) {
  116. // expected
  117. }
  118. try {
  119. iter.add("bar");
  120. fail();
  121. } catch (UnsupportedOperationException e) {
  122. // expected
  123. }
  124. }
  125. assertEquals(3, count);
  126. }
  127. private LazyStringArrayList createSampleList() {
  128. LazyStringArrayList rawList = new LazyStringArrayList();
  129. rawList.add(STRING_A);
  130. rawList.add(STRING_B);
  131. rawList.add(STRING_C);
  132. return rawList;
  133. }
  134. }