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 |
| 36 import junit.framework.TestCase; |
| 37 |
35 import java.util.Collections; | 38 import java.util.Collections; |
36 import java.util.List; | 39 import java.util.List; |
37 import junit.framework.TestCase; | |
38 | 40 |
39 /** | 41 /** |
40 * Tests for {@link RepeatedFieldBuilderV3}. This tests basic functionality. | 42 * Tests for {@link RepeatedFieldBuilder}. This tests basic functionality. |
41 * More extensive testing is provided via other tests that exercise the | 43 * More extensive testing is provided via other tests that exercise the |
42 * builder. | 44 * builder. |
43 * | 45 * |
44 * @author jonp@google.com (Jon Perlow) | 46 * @author jonp@google.com (Jon Perlow) |
45 */ | 47 */ |
46 public class RepeatedFieldBuilderV3Test extends TestCase { | 48 public class RepeatedFieldBuilderTest extends TestCase { |
47 | 49 |
48 public void testBasicUse() { | 50 public void testBasicUse() { |
49 TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent(); | 51 TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent(); |
50 RepeatedFieldBuilderV3<TestAllTypes, TestAllTypes.Builder, | 52 RepeatedFieldBuilder<TestAllTypes, TestAllTypes.Builder, |
51 TestAllTypesOrBuilder> builder = newRepeatedFieldBuilderV3(mockParent); | 53 TestAllTypesOrBuilder> builder = newRepeatedFieldBuilder(mockParent); |
52 builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(0).build()); | 54 builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(0).build()); |
53 builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(1).build()); | 55 builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(1).build()); |
54 assertEquals(0, builder.getMessage(0).getOptionalInt32()); | 56 assertEquals(0, builder.getMessage(0).getOptionalInt32()); |
55 assertEquals(1, builder.getMessage(1).getOptionalInt32()); | 57 assertEquals(1, builder.getMessage(1).getOptionalInt32()); |
56 | 58 |
57 List<TestAllTypes> list = builder.build(); | 59 List<TestAllTypes> list = builder.build(); |
58 assertEquals(2, list.size()); | 60 assertEquals(2, list.size()); |
59 assertEquals(0, list.get(0).getOptionalInt32()); | 61 assertEquals(0, list.get(0).getOptionalInt32()); |
60 assertEquals(1, list.get(1).getOptionalInt32()); | 62 assertEquals(1, list.get(1).getOptionalInt32()); |
61 assertIsUnmodifiable(list); | 63 assertIsUnmodifiable(list); |
62 | 64 |
63 // Make sure it doesn't change. | 65 // Make sure it doesn't change. |
64 List<TestAllTypes> list2 = builder.build(); | 66 List<TestAllTypes> list2 = builder.build(); |
65 assertSame(list, list2); | 67 assertSame(list, list2); |
66 assertEquals(0, mockParent.getInvalidationCount()); | 68 assertEquals(0, mockParent.getInvalidationCount()); |
67 } | 69 } |
68 | 70 |
69 public void testGoingBackAndForth() { | 71 public void testGoingBackAndForth() { |
70 TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent(); | 72 TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent(); |
71 RepeatedFieldBuilderV3<TestAllTypes, TestAllTypes.Builder, | 73 RepeatedFieldBuilder<TestAllTypes, TestAllTypes.Builder, |
72 TestAllTypesOrBuilder> builder = newRepeatedFieldBuilderV3(mockParent); | 74 TestAllTypesOrBuilder> builder = newRepeatedFieldBuilder(mockParent); |
73 builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(0).build()); | 75 builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(0).build()); |
74 builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(1).build()); | 76 builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(1).build()); |
75 assertEquals(0, builder.getMessage(0).getOptionalInt32()); | 77 assertEquals(0, builder.getMessage(0).getOptionalInt32()); |
76 assertEquals(1, builder.getMessage(1).getOptionalInt32()); | 78 assertEquals(1, builder.getMessage(1).getOptionalInt32()); |
77 | 79 |
78 // Convert to list | 80 // Convert to list |
79 List<TestAllTypes> list = builder.build(); | 81 List<TestAllTypes> list = builder.build(); |
80 assertEquals(2, list.size()); | 82 assertEquals(2, list.size()); |
81 assertEquals(0, list.get(0).getOptionalInt32()); | 83 assertEquals(0, list.get(0).getOptionalInt32()); |
82 assertEquals(1, list.get(1).getOptionalInt32()); | 84 assertEquals(1, list.get(1).getOptionalInt32()); |
83 assertIsUnmodifiable(list); | 85 assertIsUnmodifiable(list); |
84 | 86 |
85 // Update 0th item | 87 // Update 0th item |
86 assertEquals(0, mockParent.getInvalidationCount()); | 88 assertEquals(0, mockParent.getInvalidationCount()); |
87 builder.getBuilder(0).setOptionalString("foo"); | 89 builder.getBuilder(0).setOptionalString("foo"); |
88 assertEquals(1, mockParent.getInvalidationCount()); | 90 assertEquals(1, mockParent.getInvalidationCount()); |
89 list = builder.build(); | 91 list = builder.build(); |
90 assertEquals(2, list.size()); | 92 assertEquals(2, list.size()); |
91 assertEquals(0, list.get(0).getOptionalInt32()); | 93 assertEquals(0, list.get(0).getOptionalInt32()); |
92 assertEquals("foo", list.get(0).getOptionalString()); | 94 assertEquals("foo", list.get(0).getOptionalString()); |
93 assertEquals(1, list.get(1).getOptionalInt32()); | 95 assertEquals(1, list.get(1).getOptionalInt32()); |
94 assertIsUnmodifiable(list); | 96 assertIsUnmodifiable(list); |
95 assertEquals(1, mockParent.getInvalidationCount()); | 97 assertEquals(1, mockParent.getInvalidationCount()); |
96 } | 98 } |
97 | 99 |
98 public void testVariousMethods() { | 100 public void testVariousMethods() { |
99 TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent(); | 101 TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent(); |
100 RepeatedFieldBuilderV3<TestAllTypes, TestAllTypes.Builder, | 102 RepeatedFieldBuilder<TestAllTypes, TestAllTypes.Builder, |
101 TestAllTypesOrBuilder> builder = newRepeatedFieldBuilderV3(mockParent); | 103 TestAllTypesOrBuilder> builder = newRepeatedFieldBuilder(mockParent); |
102 builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(1).build()); | 104 builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(1).build()); |
103 builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(2).build()); | 105 builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(2).build()); |
104 builder.addBuilder(0, TestAllTypes.getDefaultInstance()) | 106 builder.addBuilder(0, TestAllTypes.getDefaultInstance()) |
105 .setOptionalInt32(0); | 107 .setOptionalInt32(0); |
106 builder.addBuilder(TestAllTypes.getDefaultInstance()).setOptionalInt32(3); | 108 builder.addBuilder(TestAllTypes.getDefaultInstance()).setOptionalInt32(3); |
107 | 109 |
108 assertEquals(0, builder.getMessage(0).getOptionalInt32()); | 110 assertEquals(0, builder.getMessage(0).getOptionalInt32()); |
109 assertEquals(1, builder.getMessage(1).getOptionalInt32()); | 111 assertEquals(1, builder.getMessage(1).getOptionalInt32()); |
110 assertEquals(2, builder.getMessage(2).getOptionalInt32()); | 112 assertEquals(2, builder.getMessage(2).getOptionalInt32()); |
111 assertEquals(3, builder.getMessage(3).getOptionalInt32()); | 113 assertEquals(3, builder.getMessage(3).getOptionalInt32()); |
(...skipping 20 matching lines...) Expand all Loading... |
132 | 134 |
133 // Test clear. | 135 // Test clear. |
134 builder.clear(); | 136 builder.clear(); |
135 assertEquals(1, mockParent.getInvalidationCount()); | 137 assertEquals(1, mockParent.getInvalidationCount()); |
136 assertEquals(0, builder.getCount()); | 138 assertEquals(0, builder.getCount()); |
137 assertTrue(builder.isEmpty()); | 139 assertTrue(builder.isEmpty()); |
138 } | 140 } |
139 | 141 |
140 public void testLists() { | 142 public void testLists() { |
141 TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent(); | 143 TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent(); |
142 RepeatedFieldBuilderV3<TestAllTypes, TestAllTypes.Builder, | 144 RepeatedFieldBuilder<TestAllTypes, TestAllTypes.Builder, |
143 TestAllTypesOrBuilder> builder = newRepeatedFieldBuilderV3(mockParent); | 145 TestAllTypesOrBuilder> builder = newRepeatedFieldBuilder(mockParent); |
144 builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(1).build()); | 146 builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(1).build()); |
145 builder.addMessage(0, | 147 builder.addMessage(0, |
146 TestAllTypes.newBuilder().setOptionalInt32(0).build()); | 148 TestAllTypes.newBuilder().setOptionalInt32(0).build()); |
147 assertEquals(0, builder.getMessage(0).getOptionalInt32()); | 149 assertEquals(0, builder.getMessage(0).getOptionalInt32()); |
148 assertEquals(1, builder.getMessage(1).getOptionalInt32()); | 150 assertEquals(1, builder.getMessage(1).getOptionalInt32()); |
149 | 151 |
150 // Use list of builders. | 152 // Use list of builders. |
151 List<TestAllTypes.Builder> builders = builder.getBuilderList(); | 153 List<TestAllTypes.Builder> builders = builder.getBuilderList(); |
152 assertEquals(0, builders.get(0).getOptionalInt32()); | 154 assertEquals(0, builders.get(0).getOptionalInt32()); |
153 assertEquals(1, builders.get(1).getOptionalInt32()); | 155 assertEquals(1, builders.get(1).getOptionalInt32()); |
(...skipping 17 matching lines...) Expand all Loading... |
171 } else { | 173 } else { |
172 try { | 174 try { |
173 list.clear(); | 175 list.clear(); |
174 fail("List wasn't immutable"); | 176 fail("List wasn't immutable"); |
175 } catch (UnsupportedOperationException e) { | 177 } catch (UnsupportedOperationException e) { |
176 // good | 178 // good |
177 } | 179 } |
178 } | 180 } |
179 } | 181 } |
180 | 182 |
181 private RepeatedFieldBuilderV3<TestAllTypes, TestAllTypes.Builder, | 183 private RepeatedFieldBuilder<TestAllTypes, TestAllTypes.Builder, |
182 TestAllTypesOrBuilder> | 184 TestAllTypesOrBuilder> |
183 newRepeatedFieldBuilderV3(GeneratedMessage.BuilderParent parent) { | 185 newRepeatedFieldBuilder(GeneratedMessage.BuilderParent parent) { |
184 return new RepeatedFieldBuilderV3<TestAllTypes, TestAllTypes.Builder, | 186 return new RepeatedFieldBuilder<TestAllTypes, TestAllTypes.Builder, |
185 TestAllTypesOrBuilder>(Collections.<TestAllTypes>emptyList(), false, | 187 TestAllTypesOrBuilder>(Collections.<TestAllTypes>emptyList(), false, |
186 parent, false); | 188 parent, false); |
187 } | 189 } |
188 } | 190 } |
OLD | NEW |