OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 14 matching lines...) Expand all Loading... |
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | 30 |
31 package com.google.protobuf; | 31 package com.google.protobuf; |
32 | 32 |
33 import protobuf_unittest.UnittestProto.TestAllTypes; | 33 import protobuf_unittest.UnittestProto.TestAllTypes; |
34 import protobuf_unittest.UnittestProto.TestAllTypesOrBuilder; | 34 import protobuf_unittest.UnittestProto.TestAllTypesOrBuilder; |
35 | 35 import java.util.Collections; |
| 36 import java.util.List; |
36 import junit.framework.TestCase; | 37 import junit.framework.TestCase; |
37 | 38 |
38 import java.util.Collections; | |
39 import java.util.List; | |
40 | |
41 /** | 39 /** |
42 * Tests for {@link RepeatedFieldBuilder}. This tests basic functionality. | 40 * Tests for {@link RepeatedFieldBuilderV3}. This tests basic functionality. |
43 * More extensive testing is provided via other tests that exercise the | 41 * More extensive testing is provided via other tests that exercise the |
44 * builder. | 42 * builder. |
45 * | 43 * |
46 * @author jonp@google.com (Jon Perlow) | 44 * @author jonp@google.com (Jon Perlow) |
47 */ | 45 */ |
48 public class RepeatedFieldBuilderTest extends TestCase { | 46 public class RepeatedFieldBuilderV3Test extends TestCase { |
49 | 47 |
50 public void testBasicUse() { | 48 public void testBasicUse() { |
51 TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent(); | 49 TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent(); |
52 RepeatedFieldBuilder<TestAllTypes, TestAllTypes.Builder, | 50 RepeatedFieldBuilderV3<TestAllTypes, TestAllTypes.Builder, |
53 TestAllTypesOrBuilder> builder = newRepeatedFieldBuilder(mockParent); | 51 TestAllTypesOrBuilder> builder = newRepeatedFieldBuilderV3(mockParent); |
54 builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(0).build()); | 52 builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(0).build()); |
55 builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(1).build()); | 53 builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(1).build()); |
56 assertEquals(0, builder.getMessage(0).getOptionalInt32()); | 54 assertEquals(0, builder.getMessage(0).getOptionalInt32()); |
57 assertEquals(1, builder.getMessage(1).getOptionalInt32()); | 55 assertEquals(1, builder.getMessage(1).getOptionalInt32()); |
58 | 56 |
59 List<TestAllTypes> list = builder.build(); | 57 List<TestAllTypes> list = builder.build(); |
60 assertEquals(2, list.size()); | 58 assertEquals(2, list.size()); |
61 assertEquals(0, list.get(0).getOptionalInt32()); | 59 assertEquals(0, list.get(0).getOptionalInt32()); |
62 assertEquals(1, list.get(1).getOptionalInt32()); | 60 assertEquals(1, list.get(1).getOptionalInt32()); |
63 assertIsUnmodifiable(list); | 61 assertIsUnmodifiable(list); |
64 | 62 |
65 // Make sure it doesn't change. | 63 // Make sure it doesn't change. |
66 List<TestAllTypes> list2 = builder.build(); | 64 List<TestAllTypes> list2 = builder.build(); |
67 assertSame(list, list2); | 65 assertSame(list, list2); |
68 assertEquals(0, mockParent.getInvalidationCount()); | 66 assertEquals(0, mockParent.getInvalidationCount()); |
69 } | 67 } |
70 | 68 |
71 public void testGoingBackAndForth() { | 69 public void testGoingBackAndForth() { |
72 TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent(); | 70 TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent(); |
73 RepeatedFieldBuilder<TestAllTypes, TestAllTypes.Builder, | 71 RepeatedFieldBuilderV3<TestAllTypes, TestAllTypes.Builder, |
74 TestAllTypesOrBuilder> builder = newRepeatedFieldBuilder(mockParent); | 72 TestAllTypesOrBuilder> builder = newRepeatedFieldBuilderV3(mockParent); |
75 builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(0).build()); | 73 builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(0).build()); |
76 builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(1).build()); | 74 builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(1).build()); |
77 assertEquals(0, builder.getMessage(0).getOptionalInt32()); | 75 assertEquals(0, builder.getMessage(0).getOptionalInt32()); |
78 assertEquals(1, builder.getMessage(1).getOptionalInt32()); | 76 assertEquals(1, builder.getMessage(1).getOptionalInt32()); |
79 | 77 |
80 // Convert to list | 78 // Convert to list |
81 List<TestAllTypes> list = builder.build(); | 79 List<TestAllTypes> list = builder.build(); |
82 assertEquals(2, list.size()); | 80 assertEquals(2, list.size()); |
83 assertEquals(0, list.get(0).getOptionalInt32()); | 81 assertEquals(0, list.get(0).getOptionalInt32()); |
84 assertEquals(1, list.get(1).getOptionalInt32()); | 82 assertEquals(1, list.get(1).getOptionalInt32()); |
85 assertIsUnmodifiable(list); | 83 assertIsUnmodifiable(list); |
86 | 84 |
87 // Update 0th item | 85 // Update 0th item |
88 assertEquals(0, mockParent.getInvalidationCount()); | 86 assertEquals(0, mockParent.getInvalidationCount()); |
89 builder.getBuilder(0).setOptionalString("foo"); | 87 builder.getBuilder(0).setOptionalString("foo"); |
90 assertEquals(1, mockParent.getInvalidationCount()); | 88 assertEquals(1, mockParent.getInvalidationCount()); |
91 list = builder.build(); | 89 list = builder.build(); |
92 assertEquals(2, list.size()); | 90 assertEquals(2, list.size()); |
93 assertEquals(0, list.get(0).getOptionalInt32()); | 91 assertEquals(0, list.get(0).getOptionalInt32()); |
94 assertEquals("foo", list.get(0).getOptionalString()); | 92 assertEquals("foo", list.get(0).getOptionalString()); |
95 assertEquals(1, list.get(1).getOptionalInt32()); | 93 assertEquals(1, list.get(1).getOptionalInt32()); |
96 assertIsUnmodifiable(list); | 94 assertIsUnmodifiable(list); |
97 assertEquals(1, mockParent.getInvalidationCount()); | 95 assertEquals(1, mockParent.getInvalidationCount()); |
98 } | 96 } |
99 | 97 |
100 public void testVariousMethods() { | 98 public void testVariousMethods() { |
101 TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent(); | 99 TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent(); |
102 RepeatedFieldBuilder<TestAllTypes, TestAllTypes.Builder, | 100 RepeatedFieldBuilderV3<TestAllTypes, TestAllTypes.Builder, |
103 TestAllTypesOrBuilder> builder = newRepeatedFieldBuilder(mockParent); | 101 TestAllTypesOrBuilder> builder = newRepeatedFieldBuilderV3(mockParent); |
104 builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(1).build()); | 102 builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(1).build()); |
105 builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(2).build()); | 103 builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(2).build()); |
106 builder.addBuilder(0, TestAllTypes.getDefaultInstance()) | 104 builder.addBuilder(0, TestAllTypes.getDefaultInstance()) |
107 .setOptionalInt32(0); | 105 .setOptionalInt32(0); |
108 builder.addBuilder(TestAllTypes.getDefaultInstance()).setOptionalInt32(3); | 106 builder.addBuilder(TestAllTypes.getDefaultInstance()).setOptionalInt32(3); |
109 | 107 |
110 assertEquals(0, builder.getMessage(0).getOptionalInt32()); | 108 assertEquals(0, builder.getMessage(0).getOptionalInt32()); |
111 assertEquals(1, builder.getMessage(1).getOptionalInt32()); | 109 assertEquals(1, builder.getMessage(1).getOptionalInt32()); |
112 assertEquals(2, builder.getMessage(2).getOptionalInt32()); | 110 assertEquals(2, builder.getMessage(2).getOptionalInt32()); |
113 assertEquals(3, builder.getMessage(3).getOptionalInt32()); | 111 assertEquals(3, builder.getMessage(3).getOptionalInt32()); |
(...skipping 20 matching lines...) Expand all Loading... |
134 | 132 |
135 // Test clear. | 133 // Test clear. |
136 builder.clear(); | 134 builder.clear(); |
137 assertEquals(1, mockParent.getInvalidationCount()); | 135 assertEquals(1, mockParent.getInvalidationCount()); |
138 assertEquals(0, builder.getCount()); | 136 assertEquals(0, builder.getCount()); |
139 assertTrue(builder.isEmpty()); | 137 assertTrue(builder.isEmpty()); |
140 } | 138 } |
141 | 139 |
142 public void testLists() { | 140 public void testLists() { |
143 TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent(); | 141 TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent(); |
144 RepeatedFieldBuilder<TestAllTypes, TestAllTypes.Builder, | 142 RepeatedFieldBuilderV3<TestAllTypes, TestAllTypes.Builder, |
145 TestAllTypesOrBuilder> builder = newRepeatedFieldBuilder(mockParent); | 143 TestAllTypesOrBuilder> builder = newRepeatedFieldBuilderV3(mockParent); |
146 builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(1).build()); | 144 builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(1).build()); |
147 builder.addMessage(0, | 145 builder.addMessage(0, |
148 TestAllTypes.newBuilder().setOptionalInt32(0).build()); | 146 TestAllTypes.newBuilder().setOptionalInt32(0).build()); |
149 assertEquals(0, builder.getMessage(0).getOptionalInt32()); | 147 assertEquals(0, builder.getMessage(0).getOptionalInt32()); |
150 assertEquals(1, builder.getMessage(1).getOptionalInt32()); | 148 assertEquals(1, builder.getMessage(1).getOptionalInt32()); |
151 | 149 |
152 // Use list of builders. | 150 // Use list of builders. |
153 List<TestAllTypes.Builder> builders = builder.getBuilderList(); | 151 List<TestAllTypes.Builder> builders = builder.getBuilderList(); |
154 assertEquals(0, builders.get(0).getOptionalInt32()); | 152 assertEquals(0, builders.get(0).getOptionalInt32()); |
155 assertEquals(1, builders.get(1).getOptionalInt32()); | 153 assertEquals(1, builders.get(1).getOptionalInt32()); |
(...skipping 17 matching lines...) Expand all Loading... |
173 } else { | 171 } else { |
174 try { | 172 try { |
175 list.clear(); | 173 list.clear(); |
176 fail("List wasn't immutable"); | 174 fail("List wasn't immutable"); |
177 } catch (UnsupportedOperationException e) { | 175 } catch (UnsupportedOperationException e) { |
178 // good | 176 // good |
179 } | 177 } |
180 } | 178 } |
181 } | 179 } |
182 | 180 |
183 private RepeatedFieldBuilder<TestAllTypes, TestAllTypes.Builder, | 181 private RepeatedFieldBuilderV3<TestAllTypes, TestAllTypes.Builder, |
184 TestAllTypesOrBuilder> | 182 TestAllTypesOrBuilder> |
185 newRepeatedFieldBuilder(GeneratedMessage.BuilderParent parent) { | 183 newRepeatedFieldBuilderV3(GeneratedMessage.BuilderParent parent) { |
186 return new RepeatedFieldBuilder<TestAllTypes, TestAllTypes.Builder, | 184 return new RepeatedFieldBuilderV3<TestAllTypes, TestAllTypes.Builder, |
187 TestAllTypesOrBuilder>(Collections.<TestAllTypes>emptyList(), false, | 185 TestAllTypesOrBuilder>(Collections.<TestAllTypes>emptyList(), false, |
188 parent, false); | 186 parent, false); |
189 } | 187 } |
190 } | 188 } |
OLD | NEW |