PageRenderTime 36ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/tomahawk-player/tomahawk
Java | 190 lines | 120 code | 23 blank | 47 comment | 3 complexity | 9fab44da533c7d673f18481e2acb3283 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, GPL-3.0, GPL-2.0
  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 protobuf_unittest.UnittestProto.TestAllTypes;
  32. import protobuf_unittest.UnittestProto.TestAllTypesOrBuilder;
  33. import junit.framework.TestCase;
  34. import java.util.Collections;
  35. import java.util.List;
  36. /**
  37. * Tests for {@link RepeatedFieldBuilder}. This tests basic functionality.
  38. * More extensive testing is provided via other tests that exercise the
  39. * builder.
  40. *
  41. * @author jonp@google.com (Jon Perlow)
  42. */
  43. public class RepeatedFieldBuilderTest extends TestCase {
  44. public void testBasicUse() {
  45. TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent();
  46. RepeatedFieldBuilder<TestAllTypes, TestAllTypes.Builder,
  47. TestAllTypesOrBuilder> builder = newRepeatedFieldBuilder(mockParent);
  48. builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(0).build());
  49. builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(1).build());
  50. assertEquals(0, builder.getMessage(0).getOptionalInt32());
  51. assertEquals(1, builder.getMessage(1).getOptionalInt32());
  52. List<TestAllTypes> list = builder.build();
  53. assertEquals(2, list.size());
  54. assertEquals(0, list.get(0).getOptionalInt32());
  55. assertEquals(1, list.get(1).getOptionalInt32());
  56. assertIsUnmodifiable(list);
  57. // Make sure it doesn't change.
  58. List<TestAllTypes> list2 = builder.build();
  59. assertSame(list, list2);
  60. assertEquals(0, mockParent.getInvalidationCount());
  61. }
  62. public void testGoingBackAndForth() {
  63. TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent();
  64. RepeatedFieldBuilder<TestAllTypes, TestAllTypes.Builder,
  65. TestAllTypesOrBuilder> builder = newRepeatedFieldBuilder(mockParent);
  66. builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(0).build());
  67. builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(1).build());
  68. assertEquals(0, builder.getMessage(0).getOptionalInt32());
  69. assertEquals(1, builder.getMessage(1).getOptionalInt32());
  70. // Convert to list
  71. List<TestAllTypes> list = builder.build();
  72. assertEquals(2, list.size());
  73. assertEquals(0, list.get(0).getOptionalInt32());
  74. assertEquals(1, list.get(1).getOptionalInt32());
  75. assertIsUnmodifiable(list);
  76. // Update 0th item
  77. assertEquals(0, mockParent.getInvalidationCount());
  78. builder.getBuilder(0).setOptionalString("foo");
  79. assertEquals(1, mockParent.getInvalidationCount());
  80. list = builder.build();
  81. assertEquals(2, list.size());
  82. assertEquals(0, list.get(0).getOptionalInt32());
  83. assertEquals("foo", list.get(0).getOptionalString());
  84. assertEquals(1, list.get(1).getOptionalInt32());
  85. assertIsUnmodifiable(list);
  86. assertEquals(1, mockParent.getInvalidationCount());
  87. }
  88. public void testVariousMethods() {
  89. TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent();
  90. RepeatedFieldBuilder<TestAllTypes, TestAllTypes.Builder,
  91. TestAllTypesOrBuilder> builder = newRepeatedFieldBuilder(mockParent);
  92. builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(1).build());
  93. builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(2).build());
  94. builder.addBuilder(0, TestAllTypes.getDefaultInstance())
  95. .setOptionalInt32(0);
  96. builder.addBuilder(TestAllTypes.getDefaultInstance()).setOptionalInt32(3);
  97. assertEquals(0, builder.getMessage(0).getOptionalInt32());
  98. assertEquals(1, builder.getMessage(1).getOptionalInt32());
  99. assertEquals(2, builder.getMessage(2).getOptionalInt32());
  100. assertEquals(3, builder.getMessage(3).getOptionalInt32());
  101. assertEquals(0, mockParent.getInvalidationCount());
  102. List<TestAllTypes> messages = builder.build();
  103. assertEquals(4, messages.size());
  104. assertSame(messages, builder.build()); // expect same list
  105. // Remove a message.
  106. builder.remove(2);
  107. assertEquals(1, mockParent.getInvalidationCount());
  108. assertEquals(3, builder.getCount());
  109. assertEquals(0, builder.getMessage(0).getOptionalInt32());
  110. assertEquals(1, builder.getMessage(1).getOptionalInt32());
  111. assertEquals(3, builder.getMessage(2).getOptionalInt32());
  112. // Remove a builder.
  113. builder.remove(0);
  114. assertEquals(1, mockParent.getInvalidationCount());
  115. assertEquals(2, builder.getCount());
  116. assertEquals(1, builder.getMessage(0).getOptionalInt32());
  117. assertEquals(3, builder.getMessage(1).getOptionalInt32());
  118. // Test clear.
  119. builder.clear();
  120. assertEquals(1, mockParent.getInvalidationCount());
  121. assertEquals(0, builder.getCount());
  122. assertTrue(builder.isEmpty());
  123. }
  124. public void testLists() {
  125. TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent();
  126. RepeatedFieldBuilder<TestAllTypes, TestAllTypes.Builder,
  127. TestAllTypesOrBuilder> builder = newRepeatedFieldBuilder(mockParent);
  128. builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(1).build());
  129. builder.addMessage(0,
  130. TestAllTypes.newBuilder().setOptionalInt32(0).build());
  131. assertEquals(0, builder.getMessage(0).getOptionalInt32());
  132. assertEquals(1, builder.getMessage(1).getOptionalInt32());
  133. // Use list of builders.
  134. List<TestAllTypes.Builder> builders = builder.getBuilderList();
  135. assertEquals(0, builders.get(0).getOptionalInt32());
  136. assertEquals(1, builders.get(1).getOptionalInt32());
  137. builders.get(0).setOptionalInt32(10);
  138. builders.get(1).setOptionalInt32(11);
  139. // Use list of protos
  140. List<TestAllTypes> protos = builder.getMessageList();
  141. assertEquals(10, protos.get(0).getOptionalInt32());
  142. assertEquals(11, protos.get(1).getOptionalInt32());
  143. // Add an item to the builders and verify it's updated in both
  144. builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(12).build());
  145. assertEquals(3, builders.size());
  146. assertEquals(3, protos.size());
  147. }
  148. private void assertIsUnmodifiable(List<?> list) {
  149. if (list == Collections.emptyList()) {
  150. // OKAY -- Need to check this b/c EmptyList allows you to call clear.
  151. } else {
  152. try {
  153. list.clear();
  154. fail("List wasn't immutable");
  155. } catch (UnsupportedOperationException e) {
  156. // good
  157. }
  158. }
  159. }
  160. private RepeatedFieldBuilder<TestAllTypes, TestAllTypes.Builder,
  161. TestAllTypesOrBuilder>
  162. newRepeatedFieldBuilder(GeneratedMessage.BuilderParent parent) {
  163. return new RepeatedFieldBuilder<TestAllTypes, TestAllTypes.Builder,
  164. TestAllTypesOrBuilder>(Collections.<TestAllTypes>emptyList(), false,
  165. parent, false);
  166. }
  167. }