My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes  
Changes to /trunk/java/src/test/java/com/google/protobuf/GeneratedMessageTest.java
r396 vs. r425 Compare: vs.  Format:
Revision r425
Go to: 
Project members, sign in to write a code review
/trunk/java/src/test/java/com/google/protobuf/GeneratedMessageTest.java   r396 /trunk/java/src/test/java/com/google/protobuf/GeneratedMessageTest.java   r425
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 // http://code.google.com/p/protobuf/ 3 // http://code.google.com/p/protobuf/
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.
11 // * Redistributions in binary form must reproduce the above 11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer 12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the 13 // in the documentation and/or other materials provided with the
14 // distribution. 14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its 15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from 16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission. 17 // this software without specific prior written permission.
18 // 18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
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 com.google.protobuf.Descriptors.Descriptor;
34 import com.google.protobuf.Descriptors.FieldDescriptor;
33 import com.google.protobuf.UnittestLite.TestAllExtensionsLite; 35 import com.google.protobuf.UnittestLite.TestAllExtensionsLite;
34 import com.google.protobuf.test.UnittestImport; 36 import com.google.protobuf.test.UnittestImport;
35 import protobuf_unittest.EnumWithNoOuter; 37 import protobuf_unittest.EnumWithNoOuter;
36 import protobuf_unittest.MessageWithNoOuter; 38 import protobuf_unittest.MessageWithNoOuter;
37 import protobuf_unittest.MultipleFilesTestProto; 39 import protobuf_unittest.MultipleFilesTestProto;
38 import protobuf_unittest.NestedExtension.MyNestedExtension; 40 import protobuf_unittest.NestedExtension.MyNestedExtension;
39 import protobuf_unittest.NestedExtensionLite.MyNestedExtensionLite; 41 import protobuf_unittest.NestedExtensionLite.MyNestedExtensionLite;
40 import protobuf_unittest.NonNestedExtension; 42 import protobuf_unittest.NonNestedExtension;
41 import protobuf_unittest.NonNestedExtension.MessageToBeExtended; 43 import protobuf_unittest.NonNestedExtension.MessageToBeExtended;
42 import protobuf_unittest.NonNestedExtension.MyNonNestedExtension; 44 import protobuf_unittest.NonNestedExtension.MyNonNestedExtension;
43 import protobuf_unittest.NonNestedExtensionLite; 45 import protobuf_unittest.NonNestedExtensionLite;
44 import protobuf_unittest.NonNestedExtensionLite.MessageLiteToBeExtended; 46 import protobuf_unittest.NonNestedExtensionLite.MessageLiteToBeExtended;
45 import protobuf_unittest.NonNestedExtensionLite.MyNonNestedExtensionLite; 47 import protobuf_unittest.NonNestedExtensionLite.MyNonNestedExtensionLite;
46 import protobuf_unittest.ServiceWithNoOuter; 48 import protobuf_unittest.ServiceWithNoOuter;
47 import protobuf_unittest.UnittestOptimizeFor.TestOptimizedForSize; 49 import protobuf_unittest.UnittestOptimizeFor.TestOptimizedForSize;
48 import protobuf_unittest.UnittestOptimizeFor.TestOptionalOptimizedForSize; 50 import protobuf_unittest.UnittestOptimizeFor.TestOptionalOptimizedForSize;
49 import protobuf_unittest.UnittestOptimizeFor.TestRequiredOptimizedForSize; 51 import protobuf_unittest.UnittestOptimizeFor.TestRequiredOptimizedForSize;
50 import protobuf_unittest.UnittestProto; 52 import protobuf_unittest.UnittestProto;
51 import protobuf_unittest.UnittestProto.ForeignEnum; 53 import protobuf_unittest.UnittestProto.ForeignEnum;
52 import protobuf_unittest.UnittestProto.ForeignMessage; 54 import protobuf_unittest.UnittestProto.ForeignMessage;
53 import protobuf_unittest.UnittestProto.ForeignMessageOrBuilder; 55 import protobuf_unittest.UnittestProto.ForeignMessageOrBuilder;
54 import protobuf_unittest.UnittestProto.TestAllExtensions; 56 import protobuf_unittest.UnittestProto.TestAllExtensions;
55 import protobuf_unittest.UnittestProto.TestAllTypes; 57 import protobuf_unittest.UnittestProto.TestAllTypes;
58 import protobuf_unittest.UnittestProto.TestAllTypes.NestedMessage;
56 import protobuf_unittest.UnittestProto.TestAllTypesOrBuilder; 59 import protobuf_unittest.UnittestProto.TestAllTypesOrBuilder;
57 import protobuf_unittest.UnittestProto.TestExtremeDefaultValues; 60 import protobuf_unittest.UnittestProto.TestExtremeDefaultValues;
58 import protobuf_unittest.UnittestProto.TestPackedTypes; 61 import protobuf_unittest.UnittestProto.TestPackedTypes;
59 import protobuf_unittest.UnittestProto.TestUnpackedTypes; 62 import protobuf_unittest.UnittestProto.TestUnpackedTypes;
60 63
61 import junit.framework.TestCase; 64 import junit.framework.TestCase;
62 65
63 import java.io.ByteArrayInputStream; 66 import java.io.ByteArrayInputStream;
64 import java.io.ByteArrayOutputStream; 67 import java.io.ByteArrayOutputStream;
65 import java.io.ObjectInputStream; 68 import java.io.ObjectInputStream;
66 import java.io.ObjectOutputStream; 69 import java.io.ObjectOutputStream;
67 import java.util.Arrays; 70 import java.util.Arrays;
68 import java.util.Collections; 71 import java.util.Collections;
69 import java.util.List; 72 import java.util.List;
70 73
71 /** 74 /**
72 * Unit test for generated messages and generated code. See also 75 * Unit test for generated messages and generated code. See also
73 * {@link MessageTest}, which tests some generated message functionality. 76 * {@link MessageTest}, which tests some generated message functionality.
74 * 77 *
75 * @author kenton@google.com Kenton Varda 78 * @author kenton@google.com Kenton Varda
76 */ 79 */
77 public class GeneratedMessageTest extends TestCase { 80 public class GeneratedMessageTest extends TestCase {
78 TestUtil.ReflectionTester reflectionTester = 81 TestUtil.ReflectionTester reflectionTester =
79 new TestUtil.ReflectionTester(TestAllTypes.getDescriptor(), null); 82 new TestUtil.ReflectionTester(TestAllTypes.getDescriptor(), null);
80 83
81 public void testDefaultInstance() throws Exception { 84 public void testDefaultInstance() throws Exception {
82 assertSame(TestAllTypes.getDefaultInstance(), 85 assertSame(TestAllTypes.getDefaultInstance(),
83 TestAllTypes.getDefaultInstance().getDefaultInstanceForType()); 86 TestAllTypes.getDefaultInstance().getDefaultInstanceForType());
84 assertSame(TestAllTypes.getDefaultInstance(), 87 assertSame(TestAllTypes.getDefaultInstance(),
85 TestAllTypes.newBuilder().getDefaultInstanceForType()); 88 TestAllTypes.newBuilder().getDefaultInstanceForType());
86 } 89 }
87 90
88 public void testMessageOrBuilder() throws Exception { 91 public void testMessageOrBuilder() throws Exception {
89 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 92 TestAllTypes.Builder builder = TestAllTypes.newBuilder();
90 TestUtil.setAllFields(builder); 93 TestUtil.setAllFields(builder);
91 TestAllTypes message = builder.build(); 94 TestAllTypes message = builder.build();
92 TestUtil.assertAllFieldsSet(message); 95 TestUtil.assertAllFieldsSet(message);
93 } 96 }
94 97
95 public void testUsingBuilderMultipleTimes() throws Exception { 98 public void testUsingBuilderMultipleTimes() throws Exception {
96 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 99 TestAllTypes.Builder builder = TestAllTypes.newBuilder();
97 // primitive field scalar and repeated 100 // primitive field scalar and repeated
98 builder.setOptionalSfixed64(100); 101 builder.setOptionalSfixed64(100);
99 builder.addRepeatedInt32(100); 102 builder.addRepeatedInt32(100);
100 // enum field scalar and repeated 103 // enum field scalar and repeated
101 builder.setOptionalImportEnum(UnittestImport.ImportEnum.IMPORT_BAR); 104 builder.setOptionalImportEnum(UnittestImport.ImportEnum.IMPORT_BAR);
102 builder.addRepeatedImportEnum(UnittestImport.ImportEnum.IMPORT_BAR); 105 builder.addRepeatedImportEnum(UnittestImport.ImportEnum.IMPORT_BAR);
103 // proto field scalar and repeated 106 // proto field scalar and repeated
104 builder.setOptionalForeignMessage(ForeignMessage.newBuilder().setC(1)); 107 builder.setOptionalForeignMessage(ForeignMessage.newBuilder().setC(1));
105 builder.addRepeatedForeignMessage(ForeignMessage.newBuilder().setC(1)); 108 builder.addRepeatedForeignMessage(ForeignMessage.newBuilder().setC(1));
106 109
107 TestAllTypes value1 = builder.build(); 110 TestAllTypes value1 = builder.build();
108 111
109 assertEquals(100, value1.getOptionalSfixed64()); 112 assertEquals(100, value1.getOptionalSfixed64());
110 assertEquals(100, value1.getRepeatedInt32(0)); 113 assertEquals(100, value1.getRepeatedInt32(0));
111 assertEquals(UnittestImport.ImportEnum.IMPORT_BAR, 114 assertEquals(UnittestImport.ImportEnum.IMPORT_BAR,
112 value1.getOptionalImportEnum()); 115 value1.getOptionalImportEnum());
113 assertEquals(UnittestImport.ImportEnum.IMPORT_BAR, 116 assertEquals(UnittestImport.ImportEnum.IMPORT_BAR,
114 value1.getRepeatedImportEnum(0)); 117 value1.getRepeatedImportEnum(0));
115 assertEquals(1, value1.getOptionalForeignMessage().getC()); 118 assertEquals(1, value1.getOptionalForeignMessage().getC());
116 assertEquals(1, value1.getRepeatedForeignMessage(0).getC()); 119 assertEquals(1, value1.getRepeatedForeignMessage(0).getC());
117 120
118 // Make sure that builder didn't update previously created values 121 // Make sure that builder didn't update previously created values
119 builder.setOptionalSfixed64(200); 122 builder.setOptionalSfixed64(200);
120 builder.setRepeatedInt32(0, 200); 123 builder.setRepeatedInt32(0, 200);
121 builder.setOptionalImportEnum(UnittestImport.ImportEnum.IMPORT_FOO); 124 builder.setOptionalImportEnum(UnittestImport.ImportEnum.IMPORT_FOO);
122 builder.setRepeatedImportEnum(0, UnittestImport.ImportEnum.IMPORT_FOO); 125 builder.setRepeatedImportEnum(0, UnittestImport.ImportEnum.IMPORT_FOO);
123 builder.setOptionalForeignMessage(ForeignMessage.newBuilder().setC(2)); 126 builder.setOptionalForeignMessage(ForeignMessage.newBuilder().setC(2));
124 builder.setRepeatedForeignMessage(0, ForeignMessage.newBuilder().setC(2)); 127 builder.setRepeatedForeignMessage(0, ForeignMessage.newBuilder().setC(2));
125 128
126 TestAllTypes value2 = builder.build(); 129 TestAllTypes value2 = builder.build();
127 130
128 // Make sure value1 didn't change. 131 // Make sure value1 didn't change.
129 assertEquals(100, value1.getOptionalSfixed64()); 132 assertEquals(100, value1.getOptionalSfixed64());
130 assertEquals(100, value1.getRepeatedInt32(0)); 133 assertEquals(100, value1.getRepeatedInt32(0));
131 assertEquals(UnittestImport.ImportEnum.IMPORT_BAR, 134 assertEquals(UnittestImport.ImportEnum.IMPORT_BAR,
132 value1.getOptionalImportEnum()); 135 value1.getOptionalImportEnum());
133 assertEquals(UnittestImport.ImportEnum.IMPORT_BAR, 136 assertEquals(UnittestImport.ImportEnum.IMPORT_BAR,
134 value1.getRepeatedImportEnum(0)); 137 value1.getRepeatedImportEnum(0));
135 assertEquals(1, value1.getOptionalForeignMessage().getC()); 138 assertEquals(1, value1.getOptionalForeignMessage().getC());
136 assertEquals(1, value1.getRepeatedForeignMessage(0).getC()); 139 assertEquals(1, value1.getRepeatedForeignMessage(0).getC());
137 140
138 // Make sure value2 is correct 141 // Make sure value2 is correct
139 assertEquals(200, value2.getOptionalSfixed64()); 142 assertEquals(200, value2.getOptionalSfixed64());
140 assertEquals(200, value2.getRepeatedInt32(0)); 143 assertEquals(200, value2.getRepeatedInt32(0));
141 assertEquals(UnittestImport.ImportEnum.IMPORT_FOO, 144 assertEquals(UnittestImport.ImportEnum.IMPORT_FOO,
142 value2.getOptionalImportEnum()); 145 value2.getOptionalImportEnum());
143 assertEquals(UnittestImport.ImportEnum.IMPORT_FOO, 146 assertEquals(UnittestImport.ImportEnum.IMPORT_FOO,
144 value2.getRepeatedImportEnum(0)); 147 value2.getRepeatedImportEnum(0));
145 assertEquals(2, value2.getOptionalForeignMessage().getC()); 148 assertEquals(2, value2.getOptionalForeignMessage().getC());
146 assertEquals(2, value2.getRepeatedForeignMessage(0).getC()); 149 assertEquals(2, value2.getRepeatedForeignMessage(0).getC());
147 } 150 }
148 151
149 public void testProtosShareRepeatedArraysIfDidntChange() throws Exception { 152 public void testProtosShareRepeatedArraysIfDidntChange() throws Exception {
150 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 153 TestAllTypes.Builder builder = TestAllTypes.newBuilder();
151 builder.addRepeatedInt32(100); 154 builder.addRepeatedInt32(100);
152 builder.addRepeatedImportEnum(UnittestImport.ImportEnum.IMPORT_BAR); 155 builder.addRepeatedImportEnum(UnittestImport.ImportEnum.IMPORT_BAR);
153 builder.addRepeatedForeignMessage(ForeignMessage.getDefaultInstance()); 156 builder.addRepeatedForeignMessage(ForeignMessage.getDefaultInstance());
154 157
155 TestAllTypes value1 = builder.build(); 158 TestAllTypes value1 = builder.build();
156 TestAllTypes value2 = value1.toBuilder().build(); 159 TestAllTypes value2 = value1.toBuilder().build();
157 160
158 assertSame(value1.getRepeatedInt32List(), value2.getRepeatedInt32List()); 161 assertSame(value1.getRepeatedInt32List(), value2.getRepeatedInt32List());
159 assertSame(value1.getRepeatedImportEnumList(), 162 assertSame(value1.getRepeatedImportEnumList(),
160 value2.getRepeatedImportEnumList()); 163 value2.getRepeatedImportEnumList());
161 assertSame(value1.getRepeatedForeignMessageList(), 164 assertSame(value1.getRepeatedForeignMessageList(),
162 value2.getRepeatedForeignMessageList()); 165 value2.getRepeatedForeignMessageList());
163 } 166 }
164 167
165 public void testRepeatedArraysAreImmutable() throws Exception { 168 public void testRepeatedArraysAreImmutable() throws Exception {
166 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 169 TestAllTypes.Builder builder = TestAllTypes.newBuilder();
167 builder.addRepeatedInt32(100); 170 builder.addRepeatedInt32(100);
168 builder.addRepeatedImportEnum(UnittestImport.ImportEnum.IMPORT_BAR); 171 builder.addRepeatedImportEnum(UnittestImport.ImportEnum.IMPORT_BAR);
169 builder.addRepeatedForeignMessage(ForeignMessage.getDefaultInstance()); 172 builder.addRepeatedForeignMessage(ForeignMessage.getDefaultInstance());
170 assertIsUnmodifiable(builder.getRepeatedInt32List()); 173 assertIsUnmodifiable(builder.getRepeatedInt32List());
171 assertIsUnmodifiable(builder.getRepeatedImportEnumList()); 174 assertIsUnmodifiable(builder.getRepeatedImportEnumList());
172 assertIsUnmodifiable(builder.getRepeatedForeignMessageList()); 175 assertIsUnmodifiable(builder.getRepeatedForeignMessageList());
173 assertIsUnmodifiable(builder.getRepeatedFloatList()); 176 assertIsUnmodifiable(builder.getRepeatedFloatList());
174 177
175 178
176 TestAllTypes value = builder.build(); 179 TestAllTypes value = builder.build();
177 assertIsUnmodifiable(value.getRepeatedInt32List()); 180 assertIsUnmodifiable(value.getRepeatedInt32List());
178 assertIsUnmodifiable(value.getRepeatedImportEnumList()); 181 assertIsUnmodifiable(value.getRepeatedImportEnumList());
179 assertIsUnmodifiable(value.getRepeatedForeignMessageList()); 182 assertIsUnmodifiable(value.getRepeatedForeignMessageList());
180 assertIsUnmodifiable(value.getRepeatedFloatList()); 183 assertIsUnmodifiable(value.getRepeatedFloatList());
181 } 184 }
185
186 public void testParsedMessagesAreImmutable() throws Exception {
187 TestAllTypes value = TestAllTypes.PARSER.parseFrom(
188 TestUtil.getAllSet().toByteString());
189 assertIsUnmodifiable(value.getRepeatedInt32List());
190 assertIsUnmodifiable(value.getRepeatedInt64List());
191 assertIsUnmodifiable(value.getRepeatedUint32List());
192 assertIsUnmodifiable(value.getRepeatedUint64List());
193 assertIsUnmodifiable(value.getRepeatedSint32List());
194 assertIsUnmodifiable(value.getRepeatedSint64List());
195 assertIsUnmodifiable(value.getRepeatedFixed32List());
196 assertIsUnmodifiable(value.getRepeatedFixed64List());
197 assertIsUnmodifiable(value.getRepeatedSfixed32List());
198 assertIsUnmodifiable(value.getRepeatedSfixed64List());
199 assertIsUnmodifiable(value.getRepeatedFloatList());
200 assertIsUnmodifiable(value.getRepeatedDoubleList());
201 assertIsUnmodifiable(value.getRepeatedBoolList());
202 assertIsUnmodifiable(value.getRepeatedStringList());
203 assertIsUnmodifiable(value.getRepeatedBytesList());
204 assertIsUnmodifiable(value.getRepeatedGroupList());
205 assertIsUnmodifiable(value.getRepeatedNestedMessageList());
206 assertIsUnmodifiable(value.getRepeatedForeignMessageList());
207 assertIsUnmodifiable(value.getRepeatedImportMessageList());
208 assertIsUnmodifiable(value.getRepeatedNestedEnumList());
209 assertIsUnmodifiable(value.getRepeatedForeignEnumList());
210 assertIsUnmodifiable(value.getRepeatedImportEnumList());
211 }
182 212
183 private void assertIsUnmodifiable(List<?> list) { 213 private void assertIsUnmodifiable(List<?> list) {
184 if (list == Collections.emptyList()) { 214 if (list == Collections.emptyList()) {
185 // OKAY -- Need to check this b/c EmptyList allows you to call clear. 215 // OKAY -- Need to check this b/c EmptyList allows you to call clear.
186 } else { 216 } else {
187 try { 217 try {
188 list.clear(); 218 list.clear();
189 fail("List wasn't immutable"); 219 fail("List wasn't immutable");
190 } catch (UnsupportedOperationException e) { 220 } catch (UnsupportedOperationException e) {
191 // good 221 // good
192 } 222 }
193 } 223 }
194 } 224 }
195 225
196 public void testSettersRejectNull() throws Exception { 226 public void testSettersRejectNull() throws Exception {
197 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 227 TestAllTypes.Builder builder = TestAllTypes.newBuilder();
198 try { 228 try {
199 builder.setOptionalString(null); 229 builder.setOptionalString(null);
200 fail("Exception was not thrown"); 230 fail("Exception was not thrown");
201 } catch (NullPointerException e) { 231 } catch (NullPointerException e) {
202 // We expect this exception. 232 // We expect this exception.
203 } 233 }
204 try { 234 try {
205 builder.setOptionalBytes(null); 235 builder.setOptionalBytes(null);
206 fail("Exception was not thrown"); 236 fail("Exception was not thrown");
207 } catch (NullPointerException e) { 237 } catch (NullPointerException e) {
208 // We expect this exception. 238 // We expect this exception.
209 } 239 }
210 try { 240 try {
211 builder.setOptionalNestedMessage((TestAllTypes.NestedMessage) null); 241 builder.setOptionalNestedMessage((TestAllTypes.NestedMessage) null);
212 fail("Exception was not thrown"); 242 fail("Exception was not thrown");
213 } catch (NullPointerException e) { 243 } catch (NullPointerException e) {
214 // We expect this exception. 244 // We expect this exception.
215 } 245 }
216 try { 246 try {
217 builder.setOptionalNestedMessage( 247 builder.setOptionalNestedMessage(
218 (TestAllTypes.NestedMessage.Builder) null); 248 (TestAllTypes.NestedMessage.Builder) null);
219 fail("Exception was not thrown"); 249 fail("Exception was not thrown");
220 } catch (NullPointerException e) { 250 } catch (NullPointerException e) {
221 // We expect this exception. 251 // We expect this exception.
222 } 252 }
223 try { 253 try {
224 builder.setOptionalNestedEnum(null); 254 builder.setOptionalNestedEnum(null);
225 fail("Exception was not thrown"); 255 fail("Exception was not thrown");
226 } catch (NullPointerException e) { 256 } catch (NullPointerException e) {
227 // We expect this exception. 257 // We expect this exception.
228 } 258 }
229 try { 259 try {
230 builder.addRepeatedString(null); 260 builder.addRepeatedString(null);
231 fail("Exception was not thrown"); 261 fail("Exception was not thrown");
232 } catch (NullPointerException e) { 262 } catch (NullPointerException e) {
233 // We expect this exception. 263 // We expect this exception.
234 } 264 }
235 try { 265 try {
236 builder.addRepeatedBytes(null); 266 builder.addRepeatedBytes(null);
237 fail("Exception was not thrown"); 267 fail("Exception was not thrown");
238 } catch (NullPointerException e) { 268 } catch (NullPointerException e) {
239 // We expect this exception. 269 // We expect this exception.
240 } 270 }
241 try { 271 try {
242 builder.addRepeatedNestedMessage((TestAllTypes.NestedMessage) null); 272 builder.addRepeatedNestedMessage((TestAllTypes.NestedMessage) null);
243 fail("Exception was not thrown"); 273 fail("Exception was not thrown");
244 } catch (NullPointerException e) { 274 } catch (NullPointerException e) {
245 // We expect this exception. 275 // We expect this exception.
246 } 276 }
247 try { 277 try {
248 builder.addRepeatedNestedMessage( 278 builder.addRepeatedNestedMessage(
249 (TestAllTypes.NestedMessage.Builder) null); 279 (TestAllTypes.NestedMessage.Builder) null);
250 fail("Exception was not thrown"); 280 fail("Exception was not thrown");
251 } catch (NullPointerException e) { 281 } catch (NullPointerException e) {
252 // We expect this exception. 282 // We expect this exception.
253 } 283 }
254 try { 284 try {
255 builder.addRepeatedNestedEnum(null); 285 builder.addRepeatedNestedEnum(null);
256 fail("Exception was not thrown"); 286 fail("Exception was not thrown");
257 } catch (NullPointerException e) { 287 } catch (NullPointerException e) {
258 // We expect this exception. 288 // We expect this exception.
259 } 289 }
260 } 290 }
261 291
262 public void testRepeatedSetters() throws Exception { 292 public void testRepeatedSetters() throws Exception {
263 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 293 TestAllTypes.Builder builder = TestAllTypes.newBuilder();
264 TestUtil.setAllFields(builder); 294 TestUtil.setAllFields(builder);
265 TestUtil.modifyRepeatedFields(builder); 295 TestUtil.modifyRepeatedFields(builder);
266 TestAllTypes message = builder.build(); 296 TestAllTypes message = builder.build();
267 TestUtil.assertRepeatedFieldsModified(message); 297 TestUtil.assertRepeatedFieldsModified(message);
268 } 298 }
269 299
270 public void testRepeatedSettersRejectNull() throws Exception { 300 public void testRepeatedSettersRejectNull() throws Exception {
271 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 301 TestAllTypes.Builder builder = TestAllTypes.newBuilder();
272 302
273 builder.addRepeatedString("one"); 303 builder.addRepeatedString("one");
274 builder.addRepeatedString("two"); 304 builder.addRepeatedString("two");
275 try { 305 try {
276 builder.setRepeatedString(1, null); 306 builder.setRepeatedString(1, null);
277 fail("Exception was not thrown"); 307 fail("Exception was not thrown");
278 } catch (NullPointerException e) { 308 } catch (NullPointerException e) {
279 // We expect this exception. 309 // We expect this exception.
280 } 310 }
281 311
282 builder.addRepeatedBytes(TestUtil.toBytes("one")); 312 builder.addRepeatedBytes(TestUtil.toBytes("one"));
283 builder.addRepeatedBytes(TestUtil.toBytes("two")); 313 builder.addRepeatedBytes(TestUtil.toBytes("two"));
284 try { 314 try {
285 builder.setRepeatedBytes(1, null); 315 builder.setRepeatedBytes(1, null);
286 fail("Exception was not thrown"); 316 fail("Exception was not thrown");
287 } catch (NullPointerException e) { 317 } catch (NullPointerException e) {
288 // We expect this exception. 318 // We expect this exception.
289 } 319 }
290 320
291 builder.addRepeatedNestedMessage( 321 builder.addRepeatedNestedMessage(
292 TestAllTypes.NestedMessage.newBuilder().setBb(218).build()); 322 TestAllTypes.NestedMessage.newBuilder().setBb(218).build());
293 builder.addRepeatedNestedMessage( 323 builder.addRepeatedNestedMessage(
294 TestAllTypes.NestedMessage.newBuilder().setBb(456).build()); 324 TestAllTypes.NestedMessage.newBuilder().setBb(456).build());
295 try { 325 try {
296 builder.setRepeatedNestedMessage(1, (TestAllTypes.NestedMessage) null); 326 builder.setRepeatedNestedMessage(1, (TestAllTypes.NestedMessage) null);
297 fail("Exception was not thrown"); 327 fail("Exception was not thrown");
298 } catch (NullPointerException e) { 328 } catch (NullPointerException e) {
299 // We expect this exception. 329 // We expect this exception.
300 } 330 }
301 try { 331 try {
302 builder.setRepeatedNestedMessage( 332 builder.setRepeatedNestedMessage(
303 1, (TestAllTypes.NestedMessage.Builder) null); 333 1, (TestAllTypes.NestedMessage.Builder) null);
304 fail("Exception was not thrown"); 334 fail("Exception was not thrown");
305 } catch (NullPointerException e) { 335 } catch (NullPointerException e) {
306 // We expect this exception. 336 // We expect this exception.
307 } 337 }
308 338
309 builder.addRepeatedNestedEnum(TestAllTypes.NestedEnum.FOO); 339 builder.addRepeatedNestedEnum(TestAllTypes.NestedEnum.FOO);
310 builder.addRepeatedNestedEnum(TestAllTypes.NestedEnum.BAR); 340 builder.addRepeatedNestedEnum(TestAllTypes.NestedEnum.BAR);
311 try { 341 try {
312 builder.setRepeatedNestedEnum(1, null); 342 builder.setRepeatedNestedEnum(1, null);
313 fail("Exception was not thrown"); 343 fail("Exception was not thrown");
314 } catch (NullPointerException e) { 344 } catch (NullPointerException e) {
315 // We expect this exception. 345 // We expect this exception.
316 } 346 }
317 } 347 }
318 348
319 public void testRepeatedAppend() throws Exception { 349 public void testRepeatedAppend() throws Exception {
320 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 350 TestAllTypes.Builder builder = TestAllTypes.newBuilder();
321 351
322 builder.addAllRepeatedInt32(Arrays.asList(1, 2, 3, 4)); 352 builder.addAllRepeatedInt32(Arrays.asList(1, 2, 3, 4));
323 builder.addAllRepeatedForeignEnum(Arrays.asList(ForeignEnum.FOREIGN_BAZ)); 353 builder.addAllRepeatedForeignEnum(Arrays.asList(ForeignEnum.FOREIGN_BAZ));
324 354
325 ForeignMessage foreignMessage = 355 ForeignMessage foreignMessage =
326 ForeignMessage.newBuilder().setC(12).build(); 356 ForeignMessage.newBuilder().setC(12).build();
327 builder.addAllRepeatedForeignMessage(Arrays.asList(foreignMessage)); 357 builder.addAllRepeatedForeignMessage(Arrays.asList(foreignMessage));
328 358
329 TestAllTypes message = builder.build(); 359 TestAllTypes message = builder.build();
330 assertEquals(message.getRepeatedInt32List(), Arrays.asList(1, 2, 3, 4)); 360 assertEquals(message.getRepeatedInt32List(), Arrays.asList(1, 2, 3, 4));
331 assertEquals(message.getRepeatedForeignEnumList(), 361 assertEquals(message.getRepeatedForeignEnumList(),
332 Arrays.asList(ForeignEnum.FOREIGN_BAZ)); 362 Arrays.asList(ForeignEnum.FOREIGN_BAZ));
333 assertEquals(1, message.getRepeatedForeignMessageCount()); 363 assertEquals(1, message.getRepeatedForeignMessageCount());
334 assertEquals(12, message.getRepeatedForeignMessage(0).getC()); 364 assertEquals(12, message.getRepeatedForeignMessage(0).getC());
335 } 365 }
336 366
337 public void testRepeatedAppendRejectsNull() throws Exception { 367 public void testRepeatedAppendRejectsNull() throws Exception {
338 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 368 TestAllTypes.Builder builder = TestAllTypes.newBuilder();
339 369
340 ForeignMessage foreignMessage = 370 ForeignMessage foreignMessage =
341 ForeignMessage.newBuilder().setC(12).build(); 371 ForeignMessage.newBuilder().setC(12).build();
342 try { 372 try {
343 builder.addAllRepeatedForeignMessage( 373 builder.addAllRepeatedForeignMessage(
344 Arrays.asList(foreignMessage, (ForeignMessage) null)); 374 Arrays.asList(foreignMessage, (ForeignMessage) null));
345 fail("Exception was not thrown"); 375 fail("Exception was not thrown");
346 } catch (NullPointerException e) { 376 } catch (NullPointerException e) {
347 // We expect this exception. 377 // We expect this exception.
348 } 378 }
349 379
350 try { 380 try {
351 builder.addAllRepeatedForeignEnum( 381 builder.addAllRepeatedForeignEnum(
352 Arrays.asList(ForeignEnum.FOREIGN_BAZ, null)); 382 Arrays.asList(ForeignEnum.FOREIGN_BAZ, null));
353 fail("Exception was not thrown"); 383 fail("Exception was not thrown");
354 } catch (NullPointerException e) { 384 } catch (NullPointerException e) {
355 // We expect this exception. 385 // We expect this exception.
356 } 386 }
357 387
358 try { 388 try {
359 builder.addAllRepeatedString(Arrays.asList("one", null)); 389 builder.addAllRepeatedString(Arrays.asList("one", null));
360 fail("Exception was not thrown"); 390 fail("Exception was not thrown");
361 } catch (NullPointerException e) { 391 } catch (NullPointerException e) {
362 // We expect this exception. 392 // We expect this exception.
363 } 393 }
364 394
365 try { 395 try {
366 builder.addAllRepeatedBytes(Arrays.asList(TestUtil.toBytes("one"), null)); 396 builder.addAllRepeatedBytes(Arrays.asList(TestUtil.toBytes("one"), null));
367 fail("Exception was not thrown"); 397 fail("Exception was not thrown");
368 } catch (NullPointerException e) { 398 } catch (NullPointerException e) {
369 // We expect this exception. 399 // We expect this exception.
370 } 400 }
371 } 401 }
372 402
373 public void testSettingForeignMessageUsingBuilder() throws Exception { 403 public void testSettingForeignMessageUsingBuilder() throws Exception {
374 TestAllTypes message = TestAllTypes.newBuilder() 404 TestAllTypes message = TestAllTypes.newBuilder()
375 // Pass builder for foreign message instance. 405 // Pass builder for foreign message instance.
376 .setOptionalForeignMessage(ForeignMessage.newBuilder().setC(123)) 406 .setOptionalForeignMessage(ForeignMessage.newBuilder().setC(123))
377 .build(); 407 .build();
378 TestAllTypes expectedMessage = TestAllTypes.newBuilder() 408 TestAllTypes expectedMessage = TestAllTypes.newBuilder()
379 // Create expected version passing foreign message instance explicitly. 409 // Create expected version passing foreign message instance explicitly.
380 .setOptionalForeignMessage( 410 .setOptionalForeignMessage(
381 ForeignMessage.newBuilder().setC(123).build()) 411 ForeignMessage.newBuilder().setC(123).build())
382 .build(); 412 .build();
383 // TODO(ngd): Upgrade to using real #equals method once implemented 413 // TODO(ngd): Upgrade to using real #equals method once implemented
384 assertEquals(expectedMessage.toString(), message.toString()); 414 assertEquals(expectedMessage.toString(), message.toString());
385 } 415 }
386 416
387 public void testSettingRepeatedForeignMessageUsingBuilder() throws Exception { 417 public void testSettingRepeatedForeignMessageUsingBuilder() throws Exception {
388 TestAllTypes message = TestAllTypes.newBuilder() 418 TestAllTypes message = TestAllTypes.newBuilder()
389 // Pass builder for foreign message instance. 419 // Pass builder for foreign message instance.
390 .addRepeatedForeignMessage(ForeignMessage.newBuilder().setC(456)) 420 .addRepeatedForeignMessage(ForeignMessage.newBuilder().setC(456))
391 .build(); 421 .build();
392 TestAllTypes expectedMessage = TestAllTypes.newBuilder() 422 TestAllTypes expectedMessage = TestAllTypes.newBuilder()
393 // Create expected version passing foreign message instance explicitly. 423 // Create expected version passing foreign message instance explicitly.
394 .addRepeatedForeignMessage( 424 .addRepeatedForeignMessage(
395 ForeignMessage.newBuilder().setC(456).build()) 425 ForeignMessage.newBuilder().setC(456).build())
396 .build(); 426 .build();
397 assertEquals(expectedMessage.toString(), message.toString()); 427 assertEquals(expectedMessage.toString(), message.toString());
398 } 428 }
399 429
400 public void testDefaults() throws Exception { 430 public void testDefaults() throws Exception {
401 TestUtil.assertClear(TestAllTypes.getDefaultInstance()); 431 TestUtil.assertClear(TestAllTypes.getDefaultInstance());
402 TestUtil.assertClear(TestAllTypes.newBuilder().build()); 432 TestUtil.assertClear(TestAllTypes.newBuilder().build());
403 433
404 TestExtremeDefaultValues message = 434 TestExtremeDefaultValues message =
405 TestExtremeDefaultValues.getDefaultInstance(); 435 TestExtremeDefaultValues.getDefaultInstance();
406 assertEquals("\u1234", message.getUtf8String()); 436 assertEquals("\u1234", message.getUtf8String());
407 assertEquals(Double.POSITIVE_INFINITY, message.getInfDouble()); 437 assertEquals(Double.POSITIVE_INFINITY, message.getInfDouble());
408 assertEquals(Double.NEGATIVE_INFINITY, message.getNegInfDouble()); 438 assertEquals(Double.NEGATIVE_INFINITY, message.getNegInfDouble());
409 assertTrue(Double.isNaN(message.getNanDouble())); 439 assertTrue(Double.isNaN(message.getNanDouble()));
410 assertEquals(Float.POSITIVE_INFINITY, message.getInfFloat()); 440 assertEquals(Float.POSITIVE_INFINITY, message.getInfFloat());
411 assertEquals(Float.NEGATIVE_INFINITY, message.getNegInfFloat()); 441 assertEquals(Float.NEGATIVE_INFINITY, message.getNegInfFloat());
412 assertTrue(Float.isNaN(message.getNanFloat())); 442 assertTrue(Float.isNaN(message.getNanFloat()));
413 assertEquals("? ? ?? ?? ??? ??/ ??-", message.getCppTrigraph()); 443 assertEquals("? ? ?? ?? ??? ??/ ??-", message.getCppTrigraph());
414 } 444 }
415 445
416 public void testClear() throws Exception { 446 public void testClear() throws Exception {
417 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 447 TestAllTypes.Builder builder = TestAllTypes.newBuilder();
418 TestUtil.assertClear(builder); 448 TestUtil.assertClear(builder);
419 TestUtil.setAllFields(builder); 449 TestUtil.setAllFields(builder);
420 builder.clear(); 450 builder.clear();
421 TestUtil.assertClear(builder); 451 TestUtil.assertClear(builder);
422 } 452 }
423 453
424 public void testReflectionGetters() throws Exception { 454 public void testReflectionGetters() throws Exception {
425 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 455 TestAllTypes.Builder builder = TestAllTypes.newBuilder();
426 TestUtil.setAllFields(builder); 456 TestUtil.setAllFields(builder);
427 reflectionTester.assertAllFieldsSetViaReflection(builder); 457 reflectionTester.assertAllFieldsSetViaReflection(builder);
428 458
429 TestAllTypes message = builder.build(); 459 TestAllTypes message = builder.build();
430 reflectionTester.assertAllFieldsSetViaReflection(message); 460 reflectionTester.assertAllFieldsSetViaReflection(message);
431 } 461 }
432 462
433 public void testReflectionSetters() throws Exception { 463 public void testReflectionSetters() throws Exception {
434 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 464 TestAllTypes.Builder builder = TestAllTypes.newBuilder();
435 reflectionTester.setAllFieldsViaReflection(builder); 465 reflectionTester.setAllFieldsViaReflection(builder);
436 TestUtil.assertAllFieldsSet(builder); 466 TestUtil.assertAllFieldsSet(builder);
437 467
438 TestAllTypes message = builder.build(); 468 TestAllTypes message = builder.build();
439 TestUtil.assertAllFieldsSet(message); 469 TestUtil.assertAllFieldsSet(message);
440 } 470 }
441 471
442 public void testReflectionSettersRejectNull() throws Exception { 472 public void testReflectionSettersRejectNull() throws Exception {
443 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 473 TestAllTypes.Builder builder = TestAllTypes.newBuilder();
444 reflectionTester.assertReflectionSettersRejectNull(builder); 474 reflectionTester.assertReflectionSettersRejectNull(builder);
445 } 475 }
446 476
447 public void testReflectionRepeatedSetters() throws Exception { 477 public void testReflectionRepeatedSetters() throws Exception {
448 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 478 TestAllTypes.Builder builder = TestAllTypes.newBuilder();
449 reflectionTester.setAllFieldsViaReflection(builder); 479 reflectionTester.setAllFieldsViaReflection(builder);
450 reflectionTester.modifyRepeatedFieldsViaReflection(builder); 480 reflectionTester.modifyRepeatedFieldsViaReflection(builder);
451 TestUtil.assertRepeatedFieldsModified(builder); 481 TestUtil.assertRepeatedFieldsModified(builder);
452 482
453 TestAllTypes message = builder.build(); 483 TestAllTypes message = builder.build();
454 TestUtil.assertRepeatedFieldsModified(message); 484 TestUtil.assertRepeatedFieldsModified(message);
455 } 485 }
456 486
457 public void testReflectionRepeatedSettersRejectNull() throws Exception { 487 public void testReflectionRepeatedSettersRejectNull() throws Exception {
458 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 488 TestAllTypes.Builder builder = TestAllTypes.newBuilder();
459 reflectionTester.assertReflectionRepeatedSettersRejectNull(builder); 489 reflectionTester.assertReflectionRepeatedSettersRejectNull(builder);
460 } 490 }
461 491
462 public void testReflectionDefaults() throws Exception { 492 public void testReflectionDefaults() throws Exception {
463 reflectionTester.assertClearViaReflection( 493 reflectionTester.assertClearViaReflection(
464 TestAllTypes.getDefaultInstance()); 494 TestAllTypes.getDefaultInstance());
465 reflectionTester.assertClearViaReflection( 495 reflectionTester.assertClearViaReflection(
466 TestAllTypes.newBuilder().build()); 496 TestAllTypes.newBuilder().build());
467 } 497 }
468 498
469 public void testEnumInterface() throws Exception { 499 public void testEnumInterface() throws Exception {
470 assertTrue(TestAllTypes.getDefaultInstance().getDefaultNestedEnum() 500 assertTrue(TestAllTypes.getDefaultInstance().getDefaultNestedEnum()
471 instanceof ProtocolMessageEnum); 501 instanceof ProtocolMessageEnum);
472 } 502 }
473 503
474 public void testEnumMap() throws Exception { 504 public void testEnumMap() throws Exception {
475 Internal.EnumLiteMap<ForeignEnum> map = ForeignEnum.internalGetValueMap(); 505 Internal.EnumLiteMap<ForeignEnum> map = ForeignEnum.internalGetValueMap();
476 506
477 for (ForeignEnum value : ForeignEnum.values()) { 507 for (ForeignEnum value : ForeignEnum.values()) {
478 assertEquals(value, map.findValueByNumber(value.getNumber())); 508 assertEquals(value, map.findValueByNumber(value.getNumber()));
479 } 509 }
480 510
481 assertTrue(map.findValueByNumber(12345) == null); 511 assertTrue(map.findValueByNumber(12345) == null);
482 } 512 }
483 513
484 public void testParsePackedToUnpacked() throws Exception { 514 public void testParsePackedToUnpacked() throws Exception {
485 TestUnpackedTypes.Builder builder = TestUnpackedTypes.newBuilder(); 515 TestUnpackedTypes.Builder builder = TestUnpackedTypes.newBuilder();
486 TestUnpackedTypes message = 516 TestUnpackedTypes message =
487 builder.mergeFrom(TestUtil.getPackedSet().toByteString()).build(); 517 builder.mergeFrom(TestUtil.getPackedSet().toByteString()).build();
488 TestUtil.assertUnpackedFieldsSet(message); 518 TestUtil.assertUnpackedFieldsSet(message);
489 } 519 }
490 520
491 public void testParseUnpackedToPacked() throws Exception { 521 public void testParseUnpackedToPacked() throws Exception {
492 TestPackedTypes.Builder builder = TestPackedTypes.newBuilder(); 522 TestPackedTypes.Builder builder = TestPackedTypes.newBuilder();
493 TestPackedTypes message = 523 TestPackedTypes message =
494 builder.mergeFrom(TestUtil.getUnpackedSet().toByteString()).build(); 524 builder.mergeFrom(TestUtil.getUnpackedSet().toByteString()).build();
495 TestUtil.assertPackedFieldsSet(message); 525 TestUtil.assertPackedFieldsSet(message);
496 } 526 }
497 527
498 // ================================================================= 528 // =================================================================
499 // Extensions. 529 // Extensions.
500 530
501 TestUtil.ReflectionTester extensionsReflectionTester = 531 TestUtil.ReflectionTester extensionsReflectionTester =
502 new TestUtil.ReflectionTester(TestAllExtensions.getDescriptor(), 532 new TestUtil.ReflectionTester(TestAllExtensions.getDescriptor(),
503 TestUtil.getExtensionRegistry()); 533 TestUtil.getExtensionRegistry());
504 534
505 public void testExtensionMessageOrBuilder() throws Exception { 535 public void testExtensionMessageOrBuilder() throws Exception {
506 TestAllExtensions.Builder builder = TestAllExtensions.newBuilder(); 536 TestAllExtensions.Builder builder = TestAllExtensions.newBuilder();
507 TestUtil.setAllExtensions(builder); 537 TestUtil.setAllExtensions(builder);
508 TestAllExtensions message = builder.build(); 538 TestAllExtensions message = builder.build();
509 TestUtil.assertAllExtensionsSet(message); 539 TestUtil.assertAllExtensionsSet(message);
510 } 540 }
511 541
512 public void testExtensionRepeatedSetters() throws Exception { 542 public void testExtensionRepeatedSetters() throws Exception {
513 TestAllExtensions.Builder builder = TestAllExtensions.newBuilder(); 543 TestAllExtensions.Builder builder = TestAllExtensions.newBuilder();
514 TestUtil.setAllExtensions(builder); 544 TestUtil.setAllExtensions(builder);
515 TestUtil.modifyRepeatedExtensions(builder); 545 TestUtil.modifyRepeatedExtensions(builder);
516 TestAllExtensions message = builder.build(); 546 TestAllExtensions message = builder.build();
517 TestUtil.assertRepeatedExtensionsModified(message); 547 TestUtil.assertRepeatedExtensionsModified(message);
518 } 548 }
519 549
520 public void testExtensionDefaults() throws Exception { 550 public void testExtensionDefaults() throws Exception {
521 TestUtil.assertExtensionsClear(TestAllExtensions.getDefaultInstance()); 551 TestUtil.assertExtensionsClear(TestAllExtensions.getDefaultInstance());
522 TestUtil.assertExtensionsClear(TestAllExtensions.newBuilder().build()); 552 TestUtil.assertExtensionsClear(TestAllExtensions.newBuilder().build());
523 } 553 }
524 554
525 public void testExtensionReflectionGetters() throws Exception { 555 public void testExtensionReflectionGetters() throws Exception {
526 TestAllExtensions.Builder builder = TestAllExtensions.newBuilder(); 556 TestAllExtensions.Builder builder = TestAllExtensions.newBuilder();
527 TestUtil.setAllExtensions(builder); 557 TestUtil.setAllExtensions(builder);
528 extensionsReflectionTester.assertAllFieldsSetViaReflection(builder); 558 extensionsReflectionTester.assertAllFieldsSetViaReflection(builder);
529 559
530 TestAllExtensions message = builder.build(); 560 TestAllExtensions message = builder.build();
531 extensionsReflectionTester.assertAllFieldsSetViaReflection(message); 561 extensionsReflectionTester.assertAllFieldsSetViaReflection(message);
532 } 562 }
533 563
534 public void testExtensionReflectionSetters() throws Exception { 564 public void testExtensionReflectionSetters() throws Exception {
535 TestAllExtensions.Builder builder = TestAllExtensions.newBuilder(); 565 TestAllExtensions.Builder builder = TestAllExtensions.newBuilder();
536 extensionsReflectionTester.setAllFieldsViaReflection(builder); 566 extensionsReflectionTester.setAllFieldsViaReflection(builder);
537 TestUtil.assertAllExtensionsSet(builder); 567 TestUtil.assertAllExtensionsSet(builder);
538 568
539 TestAllExtensions message = builder.build(); 569 TestAllExtensions message = builder.build();
540 TestUtil.assertAllExtensionsSet(message); 570 TestUtil.assertAllExtensionsSet(message);
541 } 571 }
542 572
543 public void testExtensionReflectionSettersRejectNull() throws Exception { 573 public void testExtensionReflectionSettersRejectNull() throws Exception {
544 TestAllExtensions.Builder builder = TestAllExtensions.newBuilder(); 574 TestAllExtensions.Builder builder = TestAllExtensions.newBuilder();
545 extensionsReflectionTester.assertReflectionSettersRejectNull(builder); 575 extensionsReflectionTester.assertReflectionSettersRejectNull(builder);
546 } 576 }
547 577
548 public void testExtensionReflectionRepeatedSetters() throws Exception { 578 public void testExtensionReflectionRepeatedSetters() throws Exception {
549 TestAllExtensions.Builder builder = TestAllExtensions.newBuilder(); 579 TestAllExtensions.Builder builder = TestAllExtensions.newBuilder();
550 extensionsReflectionTester.setAllFieldsViaReflection(builder); 580 extensionsReflectionTester.setAllFieldsViaReflection(builder);
551 extensionsReflectionTester.modifyRepeatedFieldsViaReflection(builder); 581 extensionsReflectionTester.modifyRepeatedFieldsViaReflection(builder);
552 TestUtil.assertRepeatedExtensionsModified(builder); 582 TestUtil.assertRepeatedExtensionsModified(builder);
553 583
554 TestAllExtensions message = builder.build(); 584 TestAllExtensions message = builder.build();
555 TestUtil.assertRepeatedExtensionsModified(message); 585 TestUtil.assertRepeatedExtensionsModified(message);
556 } 586 }
557 587
558 public void testExtensionReflectionRepeatedSettersRejectNull() 588 public void testExtensionReflectionRepeatedSettersRejectNull()
559 throws Exception { 589 throws Exception {
560 TestAllExtensions.Builder builder = TestAllExtensions.newBuilder(); 590 TestAllExtensions.Builder builder = TestAllExtensions.newBuilder();
561 extensionsReflectionTester.assertReflectionRepeatedSettersRejectNull( 591 extensionsReflectionTester.assertReflectionRepeatedSettersRejectNull(
562 builder); 592 builder);
563 } 593 }
564 594
565 public void testExtensionReflectionDefaults() throws Exception { 595 public void testExtensionReflectionDefaults() throws Exception {
566 extensionsReflectionTester.assertClearViaReflection( 596 extensionsReflectionTester.assertClearViaReflection(
567 TestAllExtensions.getDefaultInstance()); 597 TestAllExtensions.getDefaultInstance());
568 extensionsReflectionTester.assertClearViaReflection( 598 extensionsReflectionTester.assertClearViaReflection(
569 TestAllExtensions.newBuilder().build()); 599 TestAllExtensions.newBuilder().build());
570 } 600 }
571 601
572 public void testClearExtension() throws Exception { 602 public void testClearExtension() throws Exception {
573 // clearExtension() is not actually used in TestUtil, so try it manually. 603 // clearExtension() is not actually used in TestUtil, so try it manually.
574 assertFalse( 604 assertFalse(
575 TestAllExtensions.newBuilder() 605 TestAllExtensions.newBuilder()
576 .setExtension(UnittestProto.optionalInt32Extension, 1) 606 .setExtension(UnittestProto.optionalInt32Extension, 1)
577 .clearExtension(UnittestProto.optionalInt32Extension) 607 .clearExtension(UnittestProto.optionalInt32Extension)
578 .hasExtension(UnittestProto.optionalInt32Extension)); 608 .hasExtension(UnittestProto.optionalInt32Extension));
579 assertEquals(0, 609 assertEquals(0,
580 TestAllExtensions.newBuilder() 610 TestAllExtensions.newBuilder()
581 .addExtension(UnittestProto.repeatedInt32Extension, 1) 611 .addExtension(UnittestProto.repeatedInt32Extension, 1)
582 .clearExtension(UnittestProto.repeatedInt32Extension) 612 .clearExtension(UnittestProto.repeatedInt32Extension)
583 .getExtensionCount(UnittestProto.repeatedInt32Extension)); 613 .getExtensionCount(UnittestProto.repeatedInt32Extension));
584 } 614 }
585 615
586 public void testExtensionCopy() throws Exception { 616 public void testExtensionCopy() throws Exception {
587 TestAllExtensions original = TestUtil.getAllExtensionsSet(); 617 TestAllExtensions original = TestUtil.getAllExtensionsSet();
588 TestAllExtensions copy = TestAllExtensions.newBuilder(original).build(); 618 TestAllExtensions copy = TestAllExtensions.newBuilder(original).build();
589 TestUtil.assertAllExtensionsSet(copy); 619 TestUtil.assertAllExtensionsSet(copy);
590 } 620 }
591 621
592 public void testExtensionMergeFrom() throws Exception { 622 public void testExtensionMergeFrom() throws Exception {
593 TestAllExtensions original = 623 TestAllExtensions original =
594 TestAllExtensions.newBuilder() 624 TestAllExtensions.newBuilder()
595 .setExtension(UnittestProto.optionalInt32Extension, 1).build(); 625 .setExtension(UnittestProto.optionalInt32Extension, 1).build();
596 TestAllExtensions merged = 626 TestAllExtensions merged =
597 TestAllExtensions.newBuilder().mergeFrom(original).build(); 627 TestAllExtensions.newBuilder().mergeFrom(original).build();
598 assertTrue(merged.hasExtension(UnittestProto.optionalInt32Extension)); 628 assertTrue(merged.hasExtension(UnittestProto.optionalInt32Extension));
599 assertEquals( 629 assertEquals(
600 1, (int) merged.getExtension(UnittestProto.optionalInt32Extension)); 630 1, (int) merged.getExtension(UnittestProto.optionalInt32Extension));
601 } 631 }
602 632
603 // ================================================================= 633 // =================================================================
604 // Lite Extensions. 634 // Lite Extensions.
605 635
606 // We test lite extensions directly because they have a separate 636 // We test lite extensions directly because they have a separate
607 // implementation from full extensions. In contrast, we do not test 637 // implementation from full extensions. In contrast, we do not test
608 // lite fields directly since they are implemented exactly the same as 638 // lite fields directly since they are implemented exactly the same as
609 // regular fields. 639 // regular fields.
610 640
611 public void testLiteExtensionMessageOrBuilder() throws Exception { 641 public void testLiteExtensionMessageOrBuilder() throws Exception {
612 TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.newBuilder(); 642 TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.newBuilder();
613 TestUtil.setAllExtensions(builder); 643 TestUtil.setAllExtensions(builder);
614 TestUtil.assertAllExtensionsSet(builder); 644 TestUtil.assertAllExtensionsSet(builder);
615 645
616 TestAllExtensionsLite message = builder.build(); 646 TestAllExtensionsLite message = builder.build();
617 TestUtil.assertAllExtensionsSet(message); 647 TestUtil.assertAllExtensionsSet(message);
618 } 648 }
619 649
620 public void testLiteExtensionRepeatedSetters() throws Exception { 650 public void testLiteExtensionRepeatedSetters() throws Exception {
621 TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.newBuilder(); 651 TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.newBuilder();
622 TestUtil.setAllExtensions(builder); 652 TestUtil.setAllExtensions(builder);
623 TestUtil.modifyRepeatedExtensions(builder); 653 TestUtil.modifyRepeatedExtensions(builder);
624 TestUtil.assertRepeatedExtensionsModified(builder); 654 TestUtil.assertRepeatedExtensionsModified(builder);
625 655
626 TestAllExtensionsLite message = builder.build(); 656 TestAllExtensionsLite message = builder.build();
627 TestUtil.assertRepeatedExtensionsModified(message); 657 TestUtil.assertRepeatedExtensionsModified(message);
628 } 658 }
629 659
630 public void testLiteExtensionDefaults() throws Exception { 660 public void testLiteExtensionDefaults() throws Exception {
631 TestUtil.assertExtensionsClear(TestAllExtensionsLite.getDefaultInstance()); 661 TestUtil.assertExtensionsClear(TestAllExtensionsLite.getDefaultInstance());
632 TestUtil.assertExtensionsClear(TestAllExtensionsLite.newBuilder().build()); 662 TestUtil.assertExtensionsClear(TestAllExtensionsLite.newBuilder().build());
633 } 663 }
634 664
635 public void testClearLiteExtension() throws Exception { 665 public void testClearLiteExtension() throws Exception {
636 // clearExtension() is not actually used in TestUtil, so try it manually. 666 // clearExtension() is not actually used in TestUtil, so try it manually.
637 assertFalse( 667 assertFalse(
638 TestAllExtensionsLite.newBuilder() 668 TestAllExtensionsLite.newBuilder()
639 .setExtension(UnittestLite.optionalInt32ExtensionLite, 1) 669 .setExtension(UnittestLite.optionalInt32ExtensionLite, 1)
640 .clearExtension(UnittestLite.optionalInt32ExtensionLite) 670 .clearExtension(UnittestLite.optionalInt32ExtensionLite)
641 .hasExtension(UnittestLite.optionalInt32ExtensionLite)); 671 .hasExtension(UnittestLite.optionalInt32ExtensionLite));
642 assertEquals(0, 672 assertEquals(0,
643 TestAllExtensionsLite.newBuilder() 673 TestAllExtensionsLite.newBuilder()
644 .addExtension(UnittestLite.repeatedInt32ExtensionLite, 1) 674 .addExtension(UnittestLite.repeatedInt32ExtensionLite, 1)
645 .clearExtension(UnittestLite.repeatedInt32ExtensionLite) 675 .clearExtension(UnittestLite.repeatedInt32ExtensionLite)
646 .getExtensionCount(UnittestLite.repeatedInt32ExtensionLite)); 676 .getExtensionCount(UnittestLite.repeatedInt32ExtensionLite));
647 } 677 }
648 678
649 public void testLiteExtensionCopy() throws Exception { 679 public void testLiteExtensionCopy() throws Exception {
650 TestAllExtensionsLite original = TestUtil.getAllLiteExtensionsSet(); 680 TestAllExtensionsLite original = TestUtil.getAllLiteExtensionsSet();
651 TestAllExtensionsLite copy = 681 TestAllExtensionsLite copy =
652 TestAllExtensionsLite.newBuilder(original).build(); 682 TestAllExtensionsLite.newBuilder(original).build();
653 TestUtil.assertAllExtensionsSet(copy); 683 TestUtil.assertAllExtensionsSet(copy);
654 } 684 }
655 685
656 public void testLiteExtensionMergeFrom() throws Exception { 686 public void testLiteExtensionMergeFrom() throws Exception {
657 TestAllExtensionsLite original = 687 TestAllExtensionsLite original =
658 TestAllExtensionsLite.newBuilder() 688 TestAllExtensionsLite.newBuilder()
659 .setExtension(UnittestLite.optionalInt32ExtensionLite, 1).build(); 689 .setExtension(UnittestLite.optionalInt32ExtensionLite, 1).build();
660 TestAllExtensionsLite merged = 690 TestAllExtensionsLite merged =
661 TestAllExtensionsLite.newBuilder().mergeFrom(original).build(); 691 TestAllExtensionsLite.newBuilder().mergeFrom(original).build();
662 assertTrue(merged.hasExtension(UnittestLite.optionalInt32ExtensionLite)); 692 assertTrue(merged.hasExtension(UnittestLite.optionalInt32ExtensionLite));
663 assertEquals( 693 assertEquals(
664 1, (int) merged.getExtension(UnittestLite.optionalInt32ExtensionLite)); 694 1, (int) merged.getExtension(UnittestLite.optionalInt32ExtensionLite));
665 } 695 }
666 696
667 // ================================================================= 697 // =================================================================
668 // multiple_files_test 698 // multiple_files_test
669 699
670 public void testMultipleFilesOption() throws Exception { 700 public void testMultipleFilesOption() throws Exception {
671 // We mostly just want to check that things compile. 701 // We mostly just want to check that things compile.
672 MessageWithNoOuter message = 702 MessageWithNoOuter message =
673 MessageWithNoOuter.newBuilder() 703 MessageWithNoOuter.newBuilder()
674 .setNested(MessageWithNoOuter.NestedMessage.newBuilder().setI(1)) 704 .setNested(MessageWithNoOuter.NestedMessage.newBuilder().setI(1))
675 .addForeign(TestAllTypes.newBuilder().setOptionalInt32(1)) 705 .addForeign(TestAllTypes.newBuilder().setOptionalInt32(1))
676 .setNestedEnum(MessageWithNoOuter.NestedEnum.BAZ) 706 .setNestedEnum(MessageWithNoOuter.NestedEnum.BAZ)
677 .setForeignEnum(EnumWithNoOuter.BAR) 707 .setForeignEnum(EnumWithNoOuter.BAR)
678 .build(); 708 .build();
679 assertEquals(message, MessageWithNoOuter.parseFrom(message.toByteString())); 709 assertEquals(message, MessageWithNoOuter.parseFrom(message.toByteString()));
680 710
681 assertEquals(MultipleFilesTestProto.getDescriptor(), 711 assertEquals(MultipleFilesTestProto.getDescriptor(),
682 MessageWithNoOuter.getDescriptor().getFile()); 712 MessageWithNoOuter.getDescriptor().getFile());
683 713
684 Descriptors.FieldDescriptor field = 714 Descriptors.FieldDescriptor field =
685 MessageWithNoOuter.getDescriptor().findFieldByName("foreign_enum"); 715 MessageWithNoOuter.getDescriptor().findFieldByName("foreign_enum");
686 assertEquals(EnumWithNoOuter.BAR.getValueDescriptor(), 716 assertEquals(EnumWithNoOuter.BAR.getValueDescriptor(),
687 message.getField(field)); 717 message.getField(field));
688 718
689 assertEquals(MultipleFilesTestProto.getDescriptor(), 719 assertEquals(MultipleFilesTestProto.getDescriptor(),
690 ServiceWithNoOuter.getDescriptor().getFile()); 720 ServiceWithNoOuter.getDescriptor().getFile());
691 721
692 assertFalse( 722 assertFalse(
693 TestAllExtensions.getDefaultInstance().hasExtension( 723 TestAllExtensions.getDefaultInstance().hasExtension(
694 MultipleFilesTestProto.extensionWithOuter)); 724 MultipleFilesTestProto.extensionWithOuter));
695 } 725 }
696 726
697 public void testOptionalFieldWithRequiredSubfieldsOptimizedForSize() 727 public void testOptionalFieldWithRequiredSubfieldsOptimizedForSize()
698 throws Exception { 728 throws Exception {
699 TestOptionalOptimizedForSize message = 729 TestOptionalOptimizedForSize message =
700 TestOptionalOptimizedForSize.getDefaultInstance(); 730 TestOptionalOptimizedForSize.getDefaultInstance();
701 assertTrue(message.isInitialized()); 731 assertTrue(message.isInitialized());
702 732
703 message = TestOptionalOptimizedForSize.newBuilder().setO( 733 message = TestOptionalOptimizedForSize.newBuilder().setO(
704 TestRequiredOptimizedForSize.newBuilder().buildPartial() 734 TestRequiredOptimizedForSize.newBuilder().buildPartial()
705 ).buildPartial(); 735 ).buildPartial();
706 assertFalse(message.isInitialized()); 736 assertFalse(message.isInitialized());
707 737
708 message = TestOptionalOptimizedForSize.newBuilder().setO( 738 message = TestOptionalOptimizedForSize.newBuilder().setO(
709 TestRequiredOptimizedForSize.newBuilder().setX(5).buildPartial() 739 TestRequiredOptimizedForSize.newBuilder().setX(5).buildPartial()
710 ).buildPartial(); 740 ).buildPartial();
711 assertTrue(message.isInitialized()); 741 assertTrue(message.isInitialized());
712 } 742 }
713 743
714 public void testUninitializedExtensionInOptimizedForSize() 744 public void testUninitializedExtensionInOptimizedForSize()
715 throws Exception { 745 throws Exception {
716 TestOptimizedForSize.Builder builder = TestOptimizedForSize.newBuilder(); 746 TestOptimizedForSize.Builder builder = TestOptimizedForSize.newBuilder();
717 builder.setExtension(TestOptimizedForSize.testExtension2, 747 builder.setExtension(TestOptimizedForSize.testExtension2,
718 TestRequiredOptimizedForSize.newBuilder().buildPartial()); 748 TestRequiredOptimizedForSize.newBuilder().buildPartial());
719 assertFalse(builder.isInitialized()); 749 assertFalse(builder.isInitialized());
720 assertFalse(builder.buildPartial().isInitialized()); 750 assertFalse(builder.buildPartial().isInitialized());
721 751
722 builder = TestOptimizedForSize.newBuilder(); 752 builder = TestOptimizedForSize.newBuilder();
723 builder.setExtension(TestOptimizedForSize.testExtension2, 753 builder.setExtension(TestOptimizedForSize.testExtension2,
724 TestRequiredOptimizedForSize.newBuilder().setX(10).buildPartial()); 754 TestRequiredOptimizedForSize.newBuilder().setX(10).buildPartial());
725 assertTrue(builder.isInitialized()); 755 assertTrue(builder.isInitialized());
726 assertTrue(builder.buildPartial().isInitialized()); 756 assertTrue(builder.buildPartial().isInitialized());
727 } 757 }
728 758
729 public void testToBuilder() throws Exception { 759 public void testToBuilder() throws Exception {
730 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 760 TestAllTypes.Builder builder = TestAllTypes.newBuilder();
731 TestUtil.setAllFields(builder); 761 TestUtil.setAllFields(builder);
732 TestAllTypes message = builder.build(); 762 TestAllTypes message = builder.build();
733 TestUtil.assertAllFieldsSet(message); 763 TestUtil.assertAllFieldsSet(message);
734 TestUtil.assertAllFieldsSet(message.toBuilder().build()); 764 TestUtil.assertAllFieldsSet(message.toBuilder().build());
735 } 765 }
736 766
737 public void testFieldConstantValues() throws Exception { 767 public void testFieldConstantValues() throws Exception {
738 assertEquals(TestAllTypes.NestedMessage.BB_FIELD_NUMBER, 1); 768 assertEquals(TestAllTypes.NestedMessage.BB_FIELD_NUMBER, 1);
739 assertEquals(TestAllTypes.OPTIONAL_INT32_FIELD_NUMBER, 1); 769 assertEquals(TestAllTypes.OPTIONAL_INT32_FIELD_NUMBER, 1);
740 assertEquals(TestAllTypes.OPTIONALGROUP_FIELD_NUMBER, 16); 770 assertEquals(TestAllTypes.OPTIONALGROUP_FIELD_NUMBER, 16);
741 assertEquals(TestAllTypes.OPTIONAL_NESTED_MESSAGE_FIELD_NUMBER, 18); 771 assertEquals(TestAllTypes.OPTIONAL_NESTED_MESSAGE_FIELD_NUMBER, 18);
742 assertEquals(TestAllTypes.OPTIONAL_NESTED_ENUM_FIELD_NUMBER, 21); 772 assertEquals(TestAllTypes.OPTIONAL_NESTED_ENUM_FIELD_NUMBER, 21);
743 assertEquals(TestAllTypes.REPEATED_INT32_FIELD_NUMBER, 31); 773 assertEquals(TestAllTypes.REPEATED_INT32_FIELD_NUMBER, 31);
744 assertEquals(TestAllTypes.REPEATEDGROUP_FIELD_NUMBER, 46); 774 assertEquals(TestAllTypes.REPEATEDGROUP_FIELD_NUMBER, 46);
745 assertEquals(TestAllTypes.REPEATED_NESTED_MESSAGE_FIELD_NUMBER, 48); 775 assertEquals(TestAllTypes.REPEATED_NESTED_MESSAGE_FIELD_NUMBER, 48);
746 assertEquals(TestAllTypes.REPEATED_NESTED_ENUM_FIELD_NUMBER, 51); 776 assertEquals(TestAllTypes.REPEATED_NESTED_ENUM_FIELD_NUMBER, 51);
747 } 777 }
748 778
749 public void testExtensionConstantValues() throws Exception { 779 public void testExtensionConstantValues() throws Exception {
750 assertEquals(UnittestProto.TestRequired.SINGLE_FIELD_NUMBER, 1000); 780 assertEquals(UnittestProto.TestRequired.SINGLE_FIELD_NUMBER, 1000);
751 assertEquals(UnittestProto.TestRequired.MULTI_FIELD_NUMBER, 1001); 781 assertEquals(UnittestProto.TestRequired.MULTI_FIELD_NUMBER, 1001);
752 assertEquals(UnittestProto.OPTIONAL_INT32_EXTENSION_FIELD_NUMBER, 1); 782 assertEquals(UnittestProto.OPTIONAL_INT32_EXTENSION_FIELD_NUMBER, 1);
753 assertEquals(UnittestProto.OPTIONALGROUP_EXTENSION_FIELD_NUMBER, 16); 783 assertEquals(UnittestProto.OPTIONALGROUP_EXTENSION_FIELD_NUMBER, 16);
754 assertEquals( 784 assertEquals(
755 UnittestProto.OPTIONAL_NESTED_MESSAGE_EXTENSION_FIELD_NUMBER, 18); 785 UnittestProto.OPTIONAL_NESTED_MESSAGE_EXTENSION_FIELD_NUMBER, 18);
756 assertEquals(UnittestProto.OPTIONAL_NESTED_ENUM_EXTENSION_FIELD_NUMBER, 21); 786 assertEquals(UnittestProto.OPTIONAL_NESTED_ENUM_EXTENSION_FIELD_NUMBER, 21);
757 assertEquals(UnittestProto.REPEATED_INT32_EXTENSION_FIELD_NUMBER, 31); 787 assertEquals(UnittestProto.REPEATED_INT32_EXTENSION_FIELD_NUMBER, 31);
758 assertEquals(UnittestProto.REPEATEDGROUP_EXTENSION_FIELD_NUMBER, 46); 788 assertEquals(UnittestProto.REPEATEDGROUP_EXTENSION_FIELD_NUMBER, 46);
759 assertEquals( 789 assertEquals(
760 UnittestProto.REPEATED_NESTED_MESSAGE_EXTENSION_FIELD_NUMBER, 48); 790 UnittestProto.REPEATED_NESTED_MESSAGE_EXTENSION_FIELD_NUMBER, 48);
761 assertEquals(UnittestProto.REPEATED_NESTED_ENUM_EXTENSION_FIELD_NUMBER, 51); 791 assertEquals(UnittestProto.REPEATED_NESTED_ENUM_EXTENSION_FIELD_NUMBER, 51);
762 } 792 }
763 793
764 public void testRecursiveMessageDefaultInstance() throws Exception { 794 public void testRecursiveMessageDefaultInstance() throws Exception {
765 UnittestProto.TestRecursiveMessage message = 795 UnittestProto.TestRecursiveMessage message =
766 UnittestProto.TestRecursiveMessage.getDefaultInstance(); 796 UnittestProto.TestRecursiveMessage.getDefaultInstance();
767 assertTrue(message != null); 797 assertTrue(message != null);
768 assertTrue(message.getA() != null); 798 assertTrue(message.getA() != null);
769 assertTrue(message.getA() == message); 799 assertTrue(message.getA() == message);
770 } 800 }
771 801
772 public void testSerialize() throws Exception { 802 public void testSerialize() throws Exception {
773 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 803 ByteArrayOutputStream baos = new ByteArrayOutputStream();
774 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 804 TestAllTypes.Builder builder = TestAllTypes.newBuilder();
775 TestUtil.setAllFields(builder); 805 TestUtil.setAllFields(builder);
776 TestAllTypes expected = builder.build(); 806 TestAllTypes expected = builder.build();
777 ObjectOutputStream out = new ObjectOutputStream(baos); 807 ObjectOutputStream out = new ObjectOutputStream(baos);
778 try { 808 try {
779 out.writeObject(expected); 809 out.writeObject(expected);
780 } finally { 810 } finally {
781 out.close(); 811 out.close();
782 } 812 }
783 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); 813 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
784 ObjectInputStream in = new ObjectInputStream(bais); 814 ObjectInputStream in = new ObjectInputStream(bais);
785 TestAllTypes actual = (TestAllTypes) in.readObject(); 815 TestAllTypes actual = (TestAllTypes) in.readObject();
786 assertEquals(expected, actual); 816 assertEquals(expected, actual);
787 } 817 }
788 818
789 public void testSerializePartial() throws Exception { 819 public void testSerializePartial() throws Exception {
790 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 820 ByteArrayOutputStream baos = new ByteArrayOutputStream();
791 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 821 TestAllTypes.Builder builder = TestAllTypes.newBuilder();
792 TestAllTypes expected = builder.buildPartial(); 822 TestAllTypes expected = builder.buildPartial();
793 ObjectOutputStream out = new ObjectOutputStream(baos); 823 ObjectOutputStream out = new ObjectOutputStream(baos);
794 try { 824 try {
795 out.writeObject(expected); 825 out.writeObject(expected);
796 } finally { 826 } finally {
797 out.close(); 827 out.close();
798 } 828 }
799 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); 829 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
800 ObjectInputStream in = new ObjectInputStream(bais); 830 ObjectInputStream in = new ObjectInputStream(bais);
801 TestAllTypes actual = (TestAllTypes) in.readObject(); 831 TestAllTypes actual = (TestAllTypes) in.readObject();
802 assertEquals(expected, actual); 832 assertEquals(expected, actual);
803 } 833 }
804 834
805 public void testEnumValues() { 835 public void testEnumValues() {
806 assertEquals( 836 assertEquals(
807 TestAllTypes.NestedEnum.BAR.getNumber(), 837 TestAllTypes.NestedEnum.BAR.getNumber(),
808 TestAllTypes.NestedEnum.BAR_VALUE); 838 TestAllTypes.NestedEnum.BAR_VALUE);
809 assertEquals( 839 assertEquals(
810 TestAllTypes.NestedEnum.BAZ.getNumber(), 840 TestAllTypes.NestedEnum.BAZ.getNumber(),
811 TestAllTypes.NestedEnum.BAZ_VALUE); 841 TestAllTypes.NestedEnum.BAZ_VALUE);
812 assertEquals( 842 assertEquals(
813 TestAllTypes.NestedEnum.FOO.getNumber(), 843 TestAllTypes.NestedEnum.FOO.getNumber(),
814 TestAllTypes.NestedEnum.FOO_VALUE); 844 TestAllTypes.NestedEnum.FOO_VALUE);
815 } 845 }
816 846
817 public void testNonNestedExtensionInitialization() { 847 public void testNonNestedExtensionInitialization() {
818 assertTrue(NonNestedExtension.nonNestedExtension 848 assertTrue(NonNestedExtension.nonNestedExtension
819 .getMessageDefaultInstance() instanceof MyNonNestedExtension); 849 .getMessageDefaultInstance() instanceof MyNonNestedExtension);
820 assertEquals("nonNestedExtension", 850 assertEquals("nonNestedExtension",
821 NonNestedExtension.nonNestedExtension.getDescriptor().getName()); 851 NonNestedExtension.nonNestedExtension.getDescriptor().getName());
822 } 852 }
823 853
824 public void testNestedExtensionInitialization() { 854 public void testNestedExtensionInitialization() {
825 assertTrue(MyNestedExtension.recursiveExtension.getMessageDefaultInstance() 855 assertTrue(MyNestedExtension.recursiveExtension.getMessageDefaultInstance()
826 instanceof MessageToBeExtended); 856 instanceof MessageToBeExtended);
827 assertEquals("recursiveExtension", 857 assertEquals("recursiveExtension",
828 MyNestedExtension.recursiveExtension.getDescriptor().getName()); 858 MyNestedExtension.recursiveExtension.getDescriptor().getName());
829 } 859 }
830 860
831 public void testNonNestedExtensionLiteInitialization() { 861 public void testNonNestedExtensionLiteInitialization() {
832 assertTrue(NonNestedExtensionLite.nonNestedExtensionLite 862 assertTrue(NonNestedExtensionLite.nonNestedExtensionLite
833 .getMessageDefaultInstance() instanceof MyNonNestedExtensionLite); 863 .getMessageDefaultInstance() instanceof MyNonNestedExtensionLite);
834 } 864 }
835 865
836 public void testNestedExtensionLiteInitialization() { 866 public void testNestedExtensionLiteInitialization() {
837 assertTrue(MyNestedExtensionLite.recursiveExtensionLite 867 assertTrue(MyNestedExtensionLite.recursiveExtensionLite
838 .getMessageDefaultInstance() instanceof MessageLiteToBeExtended); 868 .getMessageDefaultInstance() instanceof MessageLiteToBeExtended);
839 } 869 }
840 870
841 public void testInvalidations() throws Exception { 871 public void testInvalidations() throws Exception {
842 GeneratedMessage.enableAlwaysUseFieldBuildersForTesting(); 872 GeneratedMessage.enableAlwaysUseFieldBuildersForTesting();
843 TestAllTypes.NestedMessage nestedMessage1 = 873 TestAllTypes.NestedMessage nestedMessage1 =
844 TestAllTypes.NestedMessage.newBuilder().build(); 874 TestAllTypes.NestedMessage.newBuilder().build();
845 TestAllTypes.NestedMessage nestedMessage2 = 875 TestAllTypes.NestedMessage nestedMessage2 =
846 TestAllTypes.NestedMessage.newBuilder().build(); 876 TestAllTypes.NestedMessage.newBuilder().build();
847 877
848 // Set all three flavors (enum, primitive, message and singular/repeated) 878 // Set all three flavors (enum, primitive, message and singular/repeated)
849 // and verify no invalidations fired 879 // and verify no invalidations fired
850 TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent(); 880 TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent();
851 881
852 TestAllTypes.Builder builder = (TestAllTypes.Builder) 882 TestAllTypes.Builder builder = (TestAllTypes.Builder)
853 ((GeneratedMessage) TestAllTypes.getDefaultInstance()). 883 ((GeneratedMessage) TestAllTypes.getDefaultInstance()).
854 newBuilderForType(mockParent); 884 newBuilderForType(mockParent);
855 builder.setOptionalInt32(1); 885 builder.setOptionalInt32(1);
856 builder.setOptionalNestedEnum(TestAllTypes.NestedEnum.BAR); 886 builder.setOptionalNestedEnum(TestAllTypes.NestedEnum.BAR);
857 builder.setOptionalNestedMessage(nestedMessage1); 887 builder.setOptionalNestedMessage(nestedMessage1);
858 builder.addRepeatedInt32(1); 888 builder.addRepeatedInt32(1);
859 builder.addRepeatedNestedEnum(TestAllTypes.NestedEnum.BAR); 889 builder.addRepeatedNestedEnum(TestAllTypes.NestedEnum.BAR);
860 builder.addRepeatedNestedMessage(nestedMessage1); 890 builder.addRepeatedNestedMessage(nestedMessage1);
861 assertEquals(0, mockParent.getInvalidationCount()); 891 assertEquals(0, mockParent.getInvalidationCount());
862 892
863 // Now tell it we want changes and make sure it's only fired once 893 // Now tell it we want changes and make sure it's only fired once
864 // And do this for each flavor 894 // And do this for each flavor
865 895
866 // primitive single 896 // primitive single
867 builder.buildPartial(); 897 builder.buildPartial();
868 builder.setOptionalInt32(2); 898 builder.setOptionalInt32(2);
869 builder.setOptionalInt32(3); 899 builder.setOptionalInt32(3);
870 assertEquals(1, mockParent.getInvalidationCount()); 900 assertEquals(1, mockParent.getInvalidationCount());
871 901
872 // enum single 902 // enum single
873 builder.buildPartial(); 903 builder.buildPartial();
874 builder.setOptionalNestedEnum(TestAllTypes.NestedEnum.BAZ); 904 builder.setOptionalNestedEnum(TestAllTypes.NestedEnum.BAZ);
875 builder.setOptionalNestedEnum(TestAllTypes.NestedEnum.BAR); 905 builder.setOptionalNestedEnum(TestAllTypes.NestedEnum.BAR);
876 assertEquals(2, mockParent.getInvalidationCount()); 906 assertEquals(2, mockParent.getInvalidationCount());
877 907
878 // message single 908 // message single
879 builder.buildPartial(); 909 builder.buildPartial();
880 builder.setOptionalNestedMessage(nestedMessage2); 910 builder.setOptionalNestedMessage(nestedMessage2);
881 builder.setOptionalNestedMessage(nestedMessage1); 911 builder.setOptionalNestedMessage(nestedMessage1);
882 assertEquals(3, mockParent.getInvalidationCount()); 912 assertEquals(3, mockParent.getInvalidationCount());
883 913
884 // primitive repated 914 // primitive repeated
885 builder.buildPartial(); 915 builder.buildPartial();
886 builder.addRepeatedInt32(2); 916 builder.addRepeatedInt32(2);
887 builder.addRepeatedInt32(3); 917 builder.addRepeatedInt32(3);
888 assertEquals(4, mockParent.getInvalidationCount()); 918 assertEquals(4, mockParent.getInvalidationCount());
889 919
890 // enum repeated 920 // enum repeated
891 builder.buildPartial(); 921 builder.buildPartial();
892 builder.addRepeatedNestedEnum(TestAllTypes.NestedEnum.BAZ); 922 builder.addRepeatedNestedEnum(TestAllTypes.NestedEnum.BAZ);
893 builder.addRepeatedNestedEnum(TestAllTypes.NestedEnum.BAZ); 923 builder.addRepeatedNestedEnum(TestAllTypes.NestedEnum.BAZ);
894 assertEquals(5, mockParent.getInvalidationCount()); 924 assertEquals(5, mockParent.getInvalidationCount());
895 925
896 // message repeated 926 // message repeated
897 builder.buildPartial(); 927 builder.buildPartial();
898 builder.addRepeatedNestedMessage(nestedMessage2); 928 builder.addRepeatedNestedMessage(nestedMessage2);
899 builder.addRepeatedNestedMessage(nestedMessage1); 929 builder.addRepeatedNestedMessage(nestedMessage1);
900 assertEquals(6, mockParent.getInvalidationCount()); 930 assertEquals(6, mockParent.getInvalidationCount());
901 931
902 } 932 }
903 933
904 public void testInvalidations_Extensions() throws Exception { 934 public void testInvalidations_Extensions() throws Exception {
905 TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent(); 935 TestUtil.MockBuilderParent mockParent = new TestUtil.MockBuilderParent();
906 936
907 TestAllExtensions.Builder builder = (TestAllExtensions.Builder) 937 TestAllExtensions.Builder builder = (TestAllExtensions.Builder)
908 ((GeneratedMessage) TestAllExtensions.getDefaultInstance()). 938 ((GeneratedMessage) TestAllExtensions.getDefaultInstance()).
909 newBuilderForType(mockParent); 939 newBuilderForType(mockParent);
910 940
911 builder.addExtension(UnittestProto.repeatedInt32Extension, 1); 941 builder.addExtension(UnittestProto.repeatedInt32Extension, 1);
912 builder.setExtension(UnittestProto.repeatedInt32Extension, 0, 2); 942 builder.setExtension(UnittestProto.repeatedInt32Extension, 0, 2);
913 builder.clearExtension(UnittestProto.repeatedInt32Extension); 943 builder.clearExtension(UnittestProto.repeatedInt32Extension);
914 assertEquals(0, mockParent.getInvalidationCount()); 944 assertEquals(0, mockParent.getInvalidationCount());
915 945
916 // Now tell it we want changes and make sure it's only fired once 946 // Now tell it we want changes and make sure it's only fired once
917 builder.buildPartial(); 947 builder.buildPartial();
918 builder.addExtension(UnittestProto.repeatedInt32Extension, 2); 948 builder.addExtension(UnittestProto.repeatedInt32Extension, 2);
919 builder.addExtension(UnittestProto.repeatedInt32Extension, 3); 949 builder.addExtension(UnittestProto.repeatedInt32Extension, 3);
920 assertEquals(1, mockParent.getInvalidationCount()); 950 assertEquals(1, mockParent.getInvalidationCount());
921 951
922 builder.buildPartial(); 952 builder.buildPartial();
923 builder.setExtension(UnittestProto.repeatedInt32Extension, 0, 4); 953 builder.setExtension(UnittestProto.repeatedInt32Extension, 0, 4);
924 builder.setExtension(UnittestProto.repeatedInt32Extension, 1, 5); 954 builder.setExtension(UnittestProto.repeatedInt32Extension, 1, 5);
925 assertEquals(2, mockParent.getInvalidationCount()); 955 assertEquals(2, mockParent.getInvalidationCount());
926 956
927 builder.buildPartial(); 957 builder.buildPartial();
928 builder.clearExtension(UnittestProto.repeatedInt32Extension); 958 builder.clearExtension(UnittestProto.repeatedInt32Extension);
929 builder.clearExtension(UnittestProto.repeatedInt32Extension); 959 builder.clearExtension(UnittestProto.repeatedInt32Extension);
930 assertEquals(3, mockParent.getInvalidationCount()); 960 assertEquals(3, mockParent.getInvalidationCount());
931 } 961 }
932 962
933 public void testBaseMessageOrBuilder() { 963 public void testBaseMessageOrBuilder() {
934 // Mostly just makes sure the base interface exists and has some methods. 964 // Mostly just makes sure the base interface exists and has some methods.
935 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 965 TestAllTypes.Builder builder = TestAllTypes.newBuilder();
936 TestAllTypes message = builder.buildPartial(); 966 TestAllTypes message = builder.buildPartial();
937 TestAllTypesOrBuilder builderAsInterface = (TestAllTypesOrBuilder) builder; 967 TestAllTypesOrBuilder builderAsInterface = (TestAllTypesOrBuilder) builder;
938 TestAllTypesOrBuilder messageAsInterface = (TestAllTypesOrBuilder) message; 968 TestAllTypesOrBuilder messageAsInterface = (TestAllTypesOrBuilder) message;
939 969
940 assertEquals( 970 assertEquals(
941 messageAsInterface.getDefaultBool(), 971 messageAsInterface.getDefaultBool(),
942 messageAsInterface.getDefaultBool()); 972 messageAsInterface.getDefaultBool());
943 assertEquals( 973 assertEquals(
944 messageAsInterface.getOptionalDouble(), 974 messageAsInterface.getOptionalDouble(),
945 messageAsInterface.getOptionalDouble()); 975 messageAsInterface.getOptionalDouble());
946 } 976 }
947 977
948 public void testMessageOrBuilderGetters() { 978 public void testMessageOrBuilderGetters() {
949 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 979 TestAllTypes.Builder builder = TestAllTypes.newBuilder();
950 980
951 // single fields 981 // single fields
952 assertSame(ForeignMessage.getDefaultInstance(), 982 assertSame(ForeignMessage.getDefaultInstance(),
953 builder.getOptionalForeignMessageOrBuilder()); 983 builder.getOptionalForeignMessageOrBuilder());
954 ForeignMessage.Builder subBuilder = 984 ForeignMessage.Builder subBuilder =
955 builder.getOptionalForeignMessageBuilder(); 985 builder.getOptionalForeignMessageBuilder();
956 assertSame(subBuilder, builder.getOptionalForeignMessageOrBuilder()); 986 assertSame(subBuilder, builder.getOptionalForeignMessageOrBuilder());
957 987
958 // repeated fields 988 // repeated fields
959 ForeignMessage m0 = ForeignMessage.newBuilder().buildPartial(); 989 ForeignMessage m0 = ForeignMessage.newBuilder().buildPartial();
960 ForeignMessage m1 = ForeignMessage.newBuilder().buildPartial(); 990 ForeignMessage m1 = ForeignMessage.newBuilder().buildPartial();
961 ForeignMessage m2 = ForeignMessage.newBuilder().buildPartial(); 991 ForeignMessage m2 = ForeignMessage.newBuilder().buildPartial();
962 builder.addRepeatedForeignMessage(m0); 992 builder.addRepeatedForeignMessage(m0);
963 builder.addRepeatedForeignMessage(m1); 993 builder.addRepeatedForeignMessage(m1);
964 builder.addRepeatedForeignMessage(m2); 994 builder.addRepeatedForeignMessage(m2);
965 assertSame(m0, builder.getRepeatedForeignMessageOrBuilder(0)); 995 assertSame(m0, builder.getRepeatedForeignMessageOrBuilder(0));
966 assertSame(m1, builder.getRepeatedForeignMessageOrBuilder(1)); 996 assertSame(m1, builder.getRepeatedForeignMessageOrBuilder(1));
967 assertSame(m2, builder.getRepeatedForeignMessageOrBuilder(2)); 997 assertSame(m2, builder.getRepeatedForeignMessageOrBuilder(2));
968 ForeignMessage.Builder b0 = builder.getRepeatedForeignMessageBuilder(0); 998 ForeignMessage.Builder b0 = builder.getRepeatedForeignMessageBuilder(0);
969 ForeignMessage.Builder b1 = builder.getRepeatedForeignMessageBuilder(1); 999 ForeignMessage.Builder b1 = builder.getRepeatedForeignMessageBuilder(1);
970 assertSame(b0, builder.getRepeatedForeignMessageOrBuilder(0)); 1000 assertSame(b0, builder.getRepeatedForeignMessageOrBuilder(0));
971 assertSame(b1, builder.getRepeatedForeignMessageOrBuilder(1)); 1001 assertSame(b1, builder.getRepeatedForeignMessageOrBuilder(1));
972 assertSame(m2, builder.getRepeatedForeignMessageOrBuilder(2)); 1002 assertSame(m2, builder.getRepeatedForeignMessageOrBuilder(2));
973 1003
974 List<? extends ForeignMessageOrBuilder> messageOrBuilderList = 1004 List<? extends ForeignMessageOrBuilder> messageOrBuilderList =
975 builder.getRepeatedForeignMessageOrBuilderList(); 1005 builder.getRepeatedForeignMessageOrBuilderList();
976 assertSame(b0, messageOrBuilderList.get(0)); 1006 assertSame(b0, messageOrBuilderList.get(0));
977 assertSame(b1, messageOrBuilderList.get(1)); 1007 assertSame(b1, messageOrBuilderList.get(1));
978 assertSame(m2, messageOrBuilderList.get(2)); 1008 assertSame(m2, messageOrBuilderList.get(2));
979 } 1009 }
1010
1011 public void testGetFieldBuilder() {
1012 Descriptor descriptor = TestAllTypes.getDescriptor();
1013
1014 FieldDescriptor fieldDescriptor =
1015 descriptor.findFieldByName("optional_nested_message");
1016 FieldDescriptor foreignFieldDescriptor =
1017 descriptor.findFieldByName("optional_foreign_message");
1018 FieldDescriptor importFieldDescriptor =
1019 descriptor.findFieldByName("optional_import_message");
1020
1021 // Mutate the message with new field builder
1022 // Mutate nested message
1023 TestAllTypes.Builder builder1 = TestAllTypes.newBuilder();
1024 Message.Builder fieldBuilder1 = builder1.newBuilderForField(fieldDescriptor)
1025 .mergeFrom((Message) builder1.getField(fieldDescriptor));
1026 FieldDescriptor subFieldDescriptor1 =
1027 fieldBuilder1.getDescriptorForType().findFieldByName("bb");
1028 fieldBuilder1.setField(subFieldDescriptor1, 1);
1029 builder1.setField(fieldDescriptor, fieldBuilder1.build());
1030
1031 // Mutate foreign message
1032 Message.Builder foreignFieldBuilder1 = builder1.newBuilderForField(
1033 foreignFieldDescriptor)
1034 .mergeFrom((Message) builder1.getField(foreignFieldDescriptor));
1035 FieldDescriptor subForeignFieldDescriptor1 =
1036 foreignFieldBuilder1.getDescriptorForType().findFieldByName("c");
1037 foreignFieldBuilder1.setField(subForeignFieldDescriptor1, 2);
1038 builder1.setField(foreignFieldDescriptor, foreignFieldBuilder1.build());
1039
1040 // Mutate import message
1041 Message.Builder importFieldBuilder1 = builder1.newBuilderForField(
1042 importFieldDescriptor)
1043 .mergeFrom((Message) builder1.getField(importFieldDescriptor));
1044 FieldDescriptor subImportFieldDescriptor1 =
1045 importFieldBuilder1.getDescriptorForType().findFieldByName("d");
1046 importFieldBuilder1.setField(subImportFieldDescriptor1, 3);
1047 builder1.setField(importFieldDescriptor, importFieldBuilder1.build());
1048
1049 Message newMessage1 = builder1.build();
1050
1051 // Mutate the message with existing field builder
1052 // Mutate nested message
1053 TestAllTypes.Builder builder2 = TestAllTypes.newBuilder();
1054 Message.Builder fieldBuilder2 = builder2.getFieldBuilder(fieldDescriptor);
1055 FieldDescriptor subFieldDescriptor2 =
1056 fieldBuilder2.getDescriptorForType().findFieldByName("bb");
1057 fieldBuilder2.setField(subFieldDescriptor2, 1);
1058 builder2.setField(fieldDescriptor, fieldBuilder2.build());
1059
1060 // Mutate foreign message
1061 Message.Builder foreignFieldBuilder2 = builder2.newBuilderForField(
1062 foreignFieldDescriptor)
1063 .mergeFrom((Message) builder2.getField(foreignFieldDescriptor));
1064 FieldDescriptor subForeignFieldDescriptor2 =
1065 foreignFieldBuilder2.getDescriptorForType().findFieldByName("c");
1066 foreignFieldBuilder2.setField(subForeignFieldDescriptor2, 2);
1067 builder2.setField(foreignFieldDescriptor, foreignFieldBuilder2.build());
1068
1069 // Mutate import message
1070 Message.Builder importFieldBuilder2 = builder2.newBuilderForField(
1071 importFieldDescriptor)
1072 .mergeFrom((Message) builder2.getField(importFieldDescriptor));
1073 FieldDescriptor subImportFieldDescriptor2 =
1074 importFieldBuilder2.getDescriptorForType().findFieldByName("d");
1075 importFieldBuilder2.setField(subImportFieldDescriptor2, 3);
1076 builder2.setField(importFieldDescriptor, importFieldBuilder2.build());
1077
1078 Message newMessage2 = builder2.build();
1079
1080 // These two messages should be equal.
1081 assertEquals(newMessage1, newMessage2);
1082 }
1083
1084 public void testGetFieldBuilderWithInitializedValue() {
1085 Descriptor descriptor = TestAllTypes.getDescriptor();
1086 FieldDescriptor fieldDescriptor =
1087 descriptor.findFieldByName("optional_nested_message");
1088
1089 // Before setting field, builder is initialized by default value.
1090 TestAllTypes.Builder builder = TestAllTypes.newBuilder();
1091 NestedMessage.Builder fieldBuilder =
1092 (NestedMessage.Builder) builder.getFieldBuilder(fieldDescriptor);
1093 assertEquals(0, fieldBuilder.getBb());
1094
1095 // Setting field value with new field builder instance.
1096 builder = TestAllTypes.newBuilder();
1097 NestedMessage.Builder newFieldBuilder =
1098 builder.getOptionalNestedMessageBuilder();
1099 newFieldBuilder.setBb(2);
1100 // Then get the field builder instance by getFieldBuilder().
1101 fieldBuilder =
1102 (NestedMessage.Builder) builder.getFieldBuilder(fieldDescriptor);
1103 // It should contain new value.
1104 assertEquals(2, fieldBuilder.getBb());
1105 // These two builder should be equal.
1106 assertSame(fieldBuilder, newFieldBuilder);
1107 }
1108
1109 public void testGetFieldBuilderNotSupportedException() {
1110 Descriptor descriptor = TestAllTypes.getDescriptor();
1111 TestAllTypes.Builder builder = TestAllTypes.newBuilder();
1112 try {
1113 builder.getFieldBuilder(descriptor.findFieldByName("optional_int32"));
1114 fail("Exception was not thrown");
1115 } catch (UnsupportedOperationException e) {
1116 // We expect this exception.
1117 }
1118 try {
1119 builder.getFieldBuilder(
1120 descriptor.findFieldByName("optional_nested_enum"));
1121 fail("Exception was not thrown");
1122 } catch (UnsupportedOperationException e) {
1123 // We expect this exception.
1124 }
1125 try {
1126 builder.getFieldBuilder(descriptor.findFieldByName("repeated_int32"));
1127 fail("Exception was not thrown");
1128 } catch (UnsupportedOperationException e) {
1129 // We expect this exception.
1130 }
1131 try {
1132 builder.getFieldBuilder(
1133 descriptor.findFieldByName("repeated_nested_enum"));
1134 fail("Exception was not thrown");
1135 } catch (UnsupportedOperationException e) {
1136 // We expect this exception.
1137 }
1138 try {
1139 builder.getFieldBuilder(
1140 descriptor.findFieldByName("repeated_nested_message"));
1141 fail("Exception was not thrown");
1142 } catch (UnsupportedOperationException e) {
1143 // We expect this exception.
1144 }
1145 }
980 } 1146 }
Powered by Google Project Hosting