OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 13 matching lines...) Expand all Loading... |
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; | 33 import com.google.protobuf.Descriptors.Descriptor; |
| 34 import com.google.protobuf.Descriptors.EnumDescriptor; |
| 35 import com.google.protobuf.Descriptors.EnumValueDescriptor; |
34 import com.google.protobuf.Descriptors.FieldDescriptor; | 36 import com.google.protobuf.Descriptors.FieldDescriptor; |
35 import com.google.protobuf.FieldPresenceTestProto.TestAllTypes; | 37 import com.google.protobuf.FieldPresenceTestProto.TestAllTypes; |
36 import com.google.protobuf.FieldPresenceTestProto.TestOptionalFieldsOnly; | 38 import com.google.protobuf.FieldPresenceTestProto.TestOptionalFieldsOnly; |
37 import com.google.protobuf.FieldPresenceTestProto.TestRepeatedFieldsOnly; | 39 import com.google.protobuf.FieldPresenceTestProto.TestRepeatedFieldsOnly; |
38 import protobuf_unittest.UnittestProto; | 40 import protobuf_unittest.UnittestProto; |
39 | 41 |
40 import junit.framework.TestCase; | 42 import junit.framework.TestCase; |
41 | 43 |
42 /** | 44 /** |
43 * Unit tests for protos that doesn't support field presence test for optional | 45 * Unit tests for protos that doesn't support field presence test for optional |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 public void testOneofEquals() throws Exception { | 147 public void testOneofEquals() throws Exception { |
146 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); | 148 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); |
147 TestAllTypes message1 = builder.build(); | 149 TestAllTypes message1 = builder.build(); |
148 // Set message2's oneof_uint32 field to defalut value. The two | 150 // Set message2's oneof_uint32 field to defalut value. The two |
149 // messages should be different when check with oneof case. | 151 // messages should be different when check with oneof case. |
150 builder.setOneofUint32(0); | 152 builder.setOneofUint32(0); |
151 TestAllTypes message2 = builder.build(); | 153 TestAllTypes message2 = builder.build(); |
152 assertFalse(message1.equals(message2)); | 154 assertFalse(message1.equals(message2)); |
153 } | 155 } |
154 | 156 |
| 157 public void testLazyField() throws Exception { |
| 158 // Test default constructed message. |
| 159 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); |
| 160 TestAllTypes message = builder.build(); |
| 161 assertFalse(message.hasOptionalLazyMessage()); |
| 162 assertEquals(0, message.getSerializedSize()); |
| 163 assertEquals(ByteString.EMPTY, message.toByteString()); |
| 164 |
| 165 // Set default instance to the field. |
| 166 builder.setOptionalLazyMessage(TestAllTypes.NestedMessage.getDefaultInstance
()); |
| 167 message = builder.build(); |
| 168 assertTrue(message.hasOptionalLazyMessage()); |
| 169 assertEquals(2, message.getSerializedSize()); |
| 170 |
| 171 // Test parse zero-length from wire sets the presence. |
| 172 TestAllTypes parsed = TestAllTypes.parseFrom(message.toByteString()); |
| 173 assertTrue(parsed.hasOptionalLazyMessage()); |
| 174 assertEquals(message.getOptionalLazyMessage(), parsed.getOptionalLazyMessage
()); |
| 175 } |
| 176 |
155 public void testFieldPresence() { | 177 public void testFieldPresence() { |
156 // Optional non-message fields set to their default value are treated the | 178 // Optional non-message fields set to their default value are treated the |
157 // same way as not set. | 179 // same way as not set. |
158 | 180 |
159 // Serialization will ignore such fields. | 181 // Serialization will ignore such fields. |
160 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); | 182 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); |
161 builder.setOptionalInt32(0); | 183 builder.setOptionalInt32(0); |
162 builder.setOptionalString(""); | 184 builder.setOptionalString(""); |
163 builder.setOptionalBytes(ByteString.EMPTY); | 185 builder.setOptionalBytes(ByteString.EMPTY); |
164 builder.setOptionalNestedEnum(TestAllTypes.NestedEnum.FOO); | 186 builder.setOptionalNestedEnum(TestAllTypes.NestedEnum.FOO); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 .setOptionalBytes(ByteString.copyFromUtf8("y")) | 248 .setOptionalBytes(ByteString.copyFromUtf8("y")) |
227 .setOptionalNestedEnum(TestAllTypes.NestedEnum.BAR) | 249 .setOptionalNestedEnum(TestAllTypes.NestedEnum.BAR) |
228 .build(); | 250 .build(); |
229 assertTrue(message.hasField(optionalInt32Field)); | 251 assertTrue(message.hasField(optionalInt32Field)); |
230 assertTrue(message.hasField(optionalStringField)); | 252 assertTrue(message.hasField(optionalStringField)); |
231 assertTrue(message.hasField(optionalBytesField)); | 253 assertTrue(message.hasField(optionalBytesField)); |
232 assertTrue(message.hasField(optionalNestedEnumField)); | 254 assertTrue(message.hasField(optionalNestedEnumField)); |
233 assertEquals(4, message.getAllFields().size()); | 255 assertEquals(4, message.getAllFields().size()); |
234 } | 256 } |
235 | 257 |
| 258 public void testFieldPresenceDynamicMessage() { |
| 259 Descriptor descriptor = TestAllTypes.getDescriptor(); |
| 260 FieldDescriptor optionalInt32Field = descriptor.findFieldByName("optional_in
t32"); |
| 261 FieldDescriptor optionalStringField = descriptor.findFieldByName("optional_s
tring"); |
| 262 FieldDescriptor optionalBytesField = descriptor.findFieldByName("optional_by
tes"); |
| 263 FieldDescriptor optionalNestedEnumField = descriptor.findFieldByName("option
al_nested_enum"); |
| 264 EnumDescriptor enumDescriptor = optionalNestedEnumField.getEnumType(); |
| 265 EnumValueDescriptor defaultEnumValueDescriptor = enumDescriptor.getValues().
get(0); |
| 266 EnumValueDescriptor nonDefaultEnumValueDescriptor = enumDescriptor.getValues
().get(1); |
| 267 |
| 268 DynamicMessage defaultInstance = DynamicMessage.getDefaultInstance(descripto
r); |
| 269 // Field not present. |
| 270 DynamicMessage message = defaultInstance.newBuilderForType().build(); |
| 271 assertFalse(message.hasField(optionalInt32Field)); |
| 272 assertFalse(message.hasField(optionalStringField)); |
| 273 assertFalse(message.hasField(optionalBytesField)); |
| 274 assertFalse(message.hasField(optionalNestedEnumField)); |
| 275 assertEquals(0, message.getAllFields().size()); |
| 276 |
| 277 // Field set to non-default value is seen as present. |
| 278 message = |
| 279 defaultInstance |
| 280 .newBuilderForType() |
| 281 .setField(optionalInt32Field, 1) |
| 282 .setField(optionalStringField, "x") |
| 283 .setField(optionalBytesField, ByteString.copyFromUtf8("y")) |
| 284 .setField(optionalNestedEnumField, nonDefaultEnumValueDescriptor) |
| 285 .build(); |
| 286 assertTrue(message.hasField(optionalInt32Field)); |
| 287 assertTrue(message.hasField(optionalStringField)); |
| 288 assertTrue(message.hasField(optionalBytesField)); |
| 289 assertTrue(message.hasField(optionalNestedEnumField)); |
| 290 assertEquals(4, message.getAllFields().size()); |
| 291 |
| 292 // Field set to default value is seen as not present. |
| 293 message = message.toBuilder() |
| 294 .setField(optionalInt32Field, 0) |
| 295 .setField(optionalStringField, "") |
| 296 .setField(optionalBytesField, ByteString.EMPTY) |
| 297 .setField(optionalNestedEnumField, defaultEnumValueDescriptor) |
| 298 .build(); |
| 299 assertFalse(message.hasField(optionalInt32Field)); |
| 300 assertFalse(message.hasField(optionalStringField)); |
| 301 assertFalse(message.hasField(optionalBytesField)); |
| 302 assertFalse(message.hasField(optionalNestedEnumField)); |
| 303 assertEquals(0, message.getAllFields().size()); |
| 304 } |
| 305 |
236 public void testMessageField() { | 306 public void testMessageField() { |
237 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); | 307 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); |
238 assertFalse(builder.hasOptionalNestedMessage()); | 308 assertFalse(builder.hasOptionalNestedMessage()); |
239 assertFalse(builder.build().hasOptionalNestedMessage()); | 309 assertFalse(builder.build().hasOptionalNestedMessage()); |
240 | 310 |
241 TestAllTypes.NestedMessage.Builder nestedBuilder = | 311 TestAllTypes.NestedMessage.Builder nestedBuilder = |
242 builder.getOptionalNestedMessageBuilder(); | 312 builder.getOptionalNestedMessageBuilder(); |
243 assertTrue(builder.hasOptionalNestedMessage()); | 313 assertTrue(builder.hasOptionalNestedMessage()); |
244 assertTrue(builder.build().hasOptionalNestedMessage()); | 314 assertTrue(builder.build().hasOptionalNestedMessage()); |
245 | 315 |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 DynamicMessage dynamicOptionalOnlyMessage = | 434 DynamicMessage dynamicOptionalOnlyMessage = |
365 DynamicMessage.getDefaultInstance( | 435 DynamicMessage.getDefaultInstance( |
366 TestOptionalFieldsOnly.getDescriptor()) | 436 TestOptionalFieldsOnly.getDescriptor()) |
367 .getParserForType().parseFrom(data); | 437 .getParserForType().parseFrom(data); |
368 assertEquals( | 438 assertEquals( |
369 0, dynamicOptionalOnlyMessage.getUnknownFields().toByteString().size()); | 439 0, dynamicOptionalOnlyMessage.getUnknownFields().toByteString().size()); |
370 assertEquals(optionalOnlyMessage.toByteString(), | 440 assertEquals(optionalOnlyMessage.toByteString(), |
371 dynamicOptionalOnlyMessage.toByteString()); | 441 dynamicOptionalOnlyMessage.toByteString()); |
372 } | 442 } |
373 } | 443 } |
OLD | NEW |