Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(135)

Side by Side Diff: third_party/protobuf/java/core/src/test/java/com/google/protobuf/RepeatedFieldBuilderV3Test.java

Issue 2600753002: Reverts third_party/protobuf: Update to HEAD (f52e188fe4) (Closed)
Patch Set: Created 3 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
31 package com.google.protobuf;
32
33 import protobuf_unittest.UnittestProto.TestAllTypes;
34 import protobuf_unittest.UnittestProto.TestAllTypesOrBuilder;
35 import java.util.Collections;
36 import java.util.List;
37 import junit.framework.TestCase;
38
39 /**
40 * Tests for {@link RepeatedFieldBuilderV3}. This tests basic functionality.
41 * More extensive testing is provided via other tests that exercise the
42 * builder.
43 *
44 * @author jonp@google.com (Jon Perlow)
45 */
46 public class RepeatedFieldBuilderV3Test extends TestCase {
47
48 public void testBasicUse() {
49 TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent();
50 RepeatedFieldBuilderV3<TestAllTypes, TestAllTypes.Builder,
51 TestAllTypesOrBuilder> builder = newRepeatedFieldBuilderV3(mockParent);
52 builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(0).build());
53 builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(1).build());
54 assertEquals(0, builder.getMessage(0).getOptionalInt32());
55 assertEquals(1, builder.getMessage(1).getOptionalInt32());
56
57 List<TestAllTypes> list = builder.build();
58 assertEquals(2, list.size());
59 assertEquals(0, list.get(0).getOptionalInt32());
60 assertEquals(1, list.get(1).getOptionalInt32());
61 assertIsUnmodifiable(list);
62
63 // Make sure it doesn't change.
64 List<TestAllTypes> list2 = builder.build();
65 assertSame(list, list2);
66 assertEquals(0, mockParent.getInvalidationCount());
67 }
68
69 public void testGoingBackAndForth() {
70 TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent();
71 RepeatedFieldBuilderV3<TestAllTypes, TestAllTypes.Builder,
72 TestAllTypesOrBuilder> builder = newRepeatedFieldBuilderV3(mockParent);
73 builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(0).build());
74 builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(1).build());
75 assertEquals(0, builder.getMessage(0).getOptionalInt32());
76 assertEquals(1, builder.getMessage(1).getOptionalInt32());
77
78 // Convert to list
79 List<TestAllTypes> list = builder.build();
80 assertEquals(2, list.size());
81 assertEquals(0, list.get(0).getOptionalInt32());
82 assertEquals(1, list.get(1).getOptionalInt32());
83 assertIsUnmodifiable(list);
84
85 // Update 0th item
86 assertEquals(0, mockParent.getInvalidationCount());
87 builder.getBuilder(0).setOptionalString("foo");
88 assertEquals(1, mockParent.getInvalidationCount());
89 list = builder.build();
90 assertEquals(2, list.size());
91 assertEquals(0, list.get(0).getOptionalInt32());
92 assertEquals("foo", list.get(0).getOptionalString());
93 assertEquals(1, list.get(1).getOptionalInt32());
94 assertIsUnmodifiable(list);
95 assertEquals(1, mockParent.getInvalidationCount());
96 }
97
98 public void testVariousMethods() {
99 TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent();
100 RepeatedFieldBuilderV3<TestAllTypes, TestAllTypes.Builder,
101 TestAllTypesOrBuilder> builder = newRepeatedFieldBuilderV3(mockParent);
102 builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(1).build());
103 builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(2).build());
104 builder.addBuilder(0, TestAllTypes.getDefaultInstance())
105 .setOptionalInt32(0);
106 builder.addBuilder(TestAllTypes.getDefaultInstance()).setOptionalInt32(3);
107
108 assertEquals(0, builder.getMessage(0).getOptionalInt32());
109 assertEquals(1, builder.getMessage(1).getOptionalInt32());
110 assertEquals(2, builder.getMessage(2).getOptionalInt32());
111 assertEquals(3, builder.getMessage(3).getOptionalInt32());
112
113 assertEquals(0, mockParent.getInvalidationCount());
114 List<TestAllTypes> messages = builder.build();
115 assertEquals(4, messages.size());
116 assertSame(messages, builder.build()); // expect same list
117
118 // Remove a message.
119 builder.remove(2);
120 assertEquals(1, mockParent.getInvalidationCount());
121 assertEquals(3, builder.getCount());
122 assertEquals(0, builder.getMessage(0).getOptionalInt32());
123 assertEquals(1, builder.getMessage(1).getOptionalInt32());
124 assertEquals(3, builder.getMessage(2).getOptionalInt32());
125
126 // Remove a builder.
127 builder.remove(0);
128 assertEquals(1, mockParent.getInvalidationCount());
129 assertEquals(2, builder.getCount());
130 assertEquals(1, builder.getMessage(0).getOptionalInt32());
131 assertEquals(3, builder.getMessage(1).getOptionalInt32());
132
133 // Test clear.
134 builder.clear();
135 assertEquals(1, mockParent.getInvalidationCount());
136 assertEquals(0, builder.getCount());
137 assertTrue(builder.isEmpty());
138 }
139
140 public void testLists() {
141 TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent();
142 RepeatedFieldBuilderV3<TestAllTypes, TestAllTypes.Builder,
143 TestAllTypesOrBuilder> builder = newRepeatedFieldBuilderV3(mockParent);
144 builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(1).build());
145 builder.addMessage(0,
146 TestAllTypes.newBuilder().setOptionalInt32(0).build());
147 assertEquals(0, builder.getMessage(0).getOptionalInt32());
148 assertEquals(1, builder.getMessage(1).getOptionalInt32());
149
150 // Use list of builders.
151 List<TestAllTypes.Builder> builders = builder.getBuilderList();
152 assertEquals(0, builders.get(0).getOptionalInt32());
153 assertEquals(1, builders.get(1).getOptionalInt32());
154 builders.get(0).setOptionalInt32(10);
155 builders.get(1).setOptionalInt32(11);
156
157 // Use list of protos
158 List<TestAllTypes> protos = builder.getMessageList();
159 assertEquals(10, protos.get(0).getOptionalInt32());
160 assertEquals(11, protos.get(1).getOptionalInt32());
161
162 // Add an item to the builders and verify it's updated in both
163 builder.addMessage(TestAllTypes.newBuilder().setOptionalInt32(12).build());
164 assertEquals(3, builders.size());
165 assertEquals(3, protos.size());
166 }
167
168 private void assertIsUnmodifiable(List<?> list) {
169 if (list == Collections.emptyList()) {
170 // OKAY -- Need to check this b/c EmptyList allows you to call clear.
171 } else {
172 try {
173 list.clear();
174 fail("List wasn't immutable");
175 } catch (UnsupportedOperationException e) {
176 // good
177 }
178 }
179 }
180
181 private RepeatedFieldBuilderV3<TestAllTypes, TestAllTypes.Builder,
182 TestAllTypesOrBuilder>
183 newRepeatedFieldBuilderV3(GeneratedMessage.BuilderParent parent) {
184 return new RepeatedFieldBuilderV3<TestAllTypes, TestAllTypes.Builder,
185 TestAllTypesOrBuilder>(Collections.<TestAllTypes>emptyList(), false,
186 parent, false);
187 }
188 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698