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

Side by Side Diff: third_party/protobuf/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/DynamicMessageTest.java

Issue 2495533002: third_party/protobuf: Update to HEAD (83d681ee2c) (Closed)
Patch Set: Make chrome settings proto generated file a component Created 4 years 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
1 // Protocol Buffers - Google's data interchange format 1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved. 2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/ 3 // 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.test;
32 import com.google.protobuf.*;
32 33
33 import com.google.protobuf.Descriptors.EnumDescriptor;
34 import com.google.protobuf.Descriptors.FieldDescriptor;
35 import com.google.protobuf.Descriptors.OneofDescriptor;
36 import protobuf_unittest.UnittestProto.TestAllExtensions; 34 import protobuf_unittest.UnittestProto.TestAllExtensions;
37 import protobuf_unittest.UnittestProto.TestAllTypes; 35 import protobuf_unittest.UnittestProto.TestAllTypes;
38 import protobuf_unittest.UnittestProto.TestEmptyMessage; 36 import protobuf_unittest.UnittestProto.TestEmptyMessage;
39 import protobuf_unittest.UnittestProto.TestPackedTypes; 37 import protobuf_unittest.UnittestProto.TestPackedTypes;
40 38
41 import junit.framework.TestCase; 39 import junit.framework.TestCase;
42
43 import java.util.Arrays; 40 import java.util.Arrays;
44 41
45 /** 42 /**
46 * Unit test for {@link DynamicMessage}. See also {@link MessageTest}, which 43 * Unit test for {@link DynamicMessage}. See also {@link MessageTest}, which
47 * tests some {@link DynamicMessage} functionality. 44 * tests some {@link DynamicMessage} functionality.
48 * 45 *
49 * @author kenton@google.com Kenton Varda 46 * @author kenton@google.com Kenton Varda
50 */ 47 */
51 public class DynamicMessageTest extends TestCase { 48 public class DynamicMessageTest extends TestCase {
52 TestUtil.ReflectionTester reflectionTester = 49 TestUtil.ReflectionTester reflectionTester =
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
238 packedReflectionTester.assertPackedFieldsSetViaReflection(message3); 235 packedReflectionTester.assertPackedFieldsSetViaReflection(message3);
239 } 236 }
240 237
241 public void testDynamicMessageCopy() throws Exception { 238 public void testDynamicMessageCopy() throws Exception {
242 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 239 TestAllTypes.Builder builder = TestAllTypes.newBuilder();
243 TestUtil.setAllFields(builder); 240 TestUtil.setAllFields(builder);
244 TestAllTypes message = builder.build(); 241 TestAllTypes message = builder.build();
245 242
246 DynamicMessage copy = DynamicMessage.newBuilder(message).build(); 243 DynamicMessage copy = DynamicMessage.newBuilder(message).build();
247 reflectionTester.assertAllFieldsSetViaReflection(copy); 244 reflectionTester.assertAllFieldsSetViaReflection(copy);
248
249 // Test oneof behavior
250 FieldDescriptor bytesField =
251 TestAllTypes.getDescriptor().findFieldByName("oneof_bytes");
252 FieldDescriptor uint32Field =
253 TestAllTypes.getDescriptor().findFieldByName("oneof_uint32");
254 assertTrue(copy.hasField(bytesField));
255 assertFalse(copy.hasField(uint32Field));
256 DynamicMessage copy2 =
257 DynamicMessage.newBuilder(message).setField(uint32Field, 123).build();
258 assertFalse(copy2.hasField(bytesField));
259 assertTrue(copy2.hasField(uint32Field));
260 assertEquals(123, copy2.getField(uint32Field));
261 } 245 }
262 246
263 public void testToBuilder() throws Exception { 247 public void testToBuilder() throws Exception {
264 DynamicMessage.Builder builder = 248 DynamicMessage.Builder builder =
265 DynamicMessage.newBuilder(TestAllTypes.getDescriptor()); 249 DynamicMessage.newBuilder(TestAllTypes.getDescriptor());
266 reflectionTester.setAllFieldsViaReflection(builder); 250 reflectionTester.setAllFieldsViaReflection(builder);
267 int unknownFieldNum = 9; 251 int unknownFieldNum = 9;
268 long unknownFieldVal = 90; 252 long unknownFieldVal = 90;
269 builder.setUnknownFields(UnknownFieldSet.newBuilder() 253 builder.setUnknownFields(UnknownFieldSet.newBuilder()
270 .addField(unknownFieldNum, 254 .addField(unknownFieldNum,
271 UnknownFieldSet.Field.newBuilder() 255 UnknownFieldSet.Field.newBuilder()
272 .addVarint(unknownFieldVal).build()) 256 .addVarint(unknownFieldVal).build())
273 .build()); 257 .build());
274 DynamicMessage message = builder.build(); 258 DynamicMessage message = builder.build();
275 259
276 DynamicMessage derived = message.toBuilder().build(); 260 DynamicMessage derived = message.toBuilder().build();
277 reflectionTester.assertAllFieldsSetViaReflection(derived); 261 reflectionTester.assertAllFieldsSetViaReflection(derived);
278 assertEquals(Arrays.asList(unknownFieldVal), 262 assertEquals(Arrays.asList(unknownFieldVal),
279 derived.getUnknownFields().getField(unknownFieldNum).getVarintList()); 263 derived.getUnknownFields().getField(unknownFieldNum).getVarintList());
280 } 264 }
281
282 public void testDynamicOneofMessage() throws Exception {
283 DynamicMessage.Builder builder =
284 DynamicMessage.newBuilder(TestAllTypes.getDescriptor());
285 OneofDescriptor oneof = TestAllTypes.getDescriptor().getOneofs().get(0);
286 assertFalse(builder.hasOneof(oneof));
287 assertSame(null, builder.getOneofFieldDescriptor(oneof));
288
289 reflectionTester.setAllFieldsViaReflection(builder);
290 assertTrue(builder.hasOneof(oneof));
291 FieldDescriptor field = oneof.getField(3);
292 assertSame(field, builder.getOneofFieldDescriptor(oneof));
293
294 DynamicMessage message = builder.buildPartial();
295 assertTrue(message.hasOneof(oneof));
296
297 DynamicMessage.Builder mergedBuilder =
298 DynamicMessage.newBuilder(TestAllTypes.getDescriptor());
299 FieldDescriptor mergedField = oneof.getField(0);
300 mergedBuilder.setField(mergedField, 123);
301 assertTrue(mergedBuilder.hasField(mergedField));
302 mergedBuilder.mergeFrom(message);
303 assertTrue(mergedBuilder.hasField(field));
304 assertFalse(mergedBuilder.hasField(mergedField));
305
306 builder.clearOneof(oneof);
307 assertSame(null, builder.getOneofFieldDescriptor(oneof));
308 message = builder.build();
309 assertSame(null, message.getOneofFieldDescriptor(oneof));
310 }
311
312 // Regression test for a bug that makes setField() not work for repeated
313 // enum fields.
314 public void testSettersForRepeatedEnumField() throws Exception {
315 DynamicMessage.Builder builder =
316 DynamicMessage.newBuilder(TestAllTypes.getDescriptor());
317 FieldDescriptor repeatedEnumField =
318 TestAllTypes.getDescriptor().findFieldByName(
319 "repeated_nested_enum");
320 EnumDescriptor enumDescriptor = TestAllTypes.NestedEnum.getDescriptor();
321 builder.setField(repeatedEnumField, enumDescriptor.getValues());
322 DynamicMessage message = builder.build();
323 assertEquals(
324 enumDescriptor.getValues(), message.getField(repeatedEnumField));
325 }
326 } 265 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698