| 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; | |
| 36 import com.google.protobuf.Descriptors.FieldDescriptor; | 34 import com.google.protobuf.Descriptors.FieldDescriptor; |
| 37 import com.google.protobuf.FieldPresenceTestProto.TestAllTypes; | 35 import com.google.protobuf.FieldPresenceTestProto.TestAllTypes; |
| 38 import com.google.protobuf.FieldPresenceTestProto.TestOptionalFieldsOnly; | 36 import com.google.protobuf.FieldPresenceTestProto.TestOptionalFieldsOnly; |
| 39 import com.google.protobuf.FieldPresenceTestProto.TestRepeatedFieldsOnly; | 37 import com.google.protobuf.FieldPresenceTestProto.TestRepeatedFieldsOnly; |
| 40 import protobuf_unittest.UnittestProto; | 38 import protobuf_unittest.UnittestProto; |
| 41 | 39 |
| 42 import junit.framework.TestCase; | 40 import junit.framework.TestCase; |
| 43 | 41 |
| 44 /** | 42 /** |
| 45 * Unit tests for protos that doesn't support field presence test for optional | 43 * 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... |
| 147 public void testOneofEquals() throws Exception { | 145 public void testOneofEquals() throws Exception { |
| 148 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); | 146 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); |
| 149 TestAllTypes message1 = builder.build(); | 147 TestAllTypes message1 = builder.build(); |
| 150 // Set message2's oneof_uint32 field to defalut value. The two | 148 // Set message2's oneof_uint32 field to defalut value. The two |
| 151 // messages should be different when check with oneof case. | 149 // messages should be different when check with oneof case. |
| 152 builder.setOneofUint32(0); | 150 builder.setOneofUint32(0); |
| 153 TestAllTypes message2 = builder.build(); | 151 TestAllTypes message2 = builder.build(); |
| 154 assertFalse(message1.equals(message2)); | 152 assertFalse(message1.equals(message2)); |
| 155 } | 153 } |
| 156 | 154 |
| 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 | |
| 177 public void testFieldPresence() { | 155 public void testFieldPresence() { |
| 178 // Optional non-message fields set to their default value are treated the | 156 // Optional non-message fields set to their default value are treated the |
| 179 // same way as not set. | 157 // same way as not set. |
| 180 | 158 |
| 181 // Serialization will ignore such fields. | 159 // Serialization will ignore such fields. |
| 182 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); | 160 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); |
| 183 builder.setOptionalInt32(0); | 161 builder.setOptionalInt32(0); |
| 184 builder.setOptionalString(""); | 162 builder.setOptionalString(""); |
| 185 builder.setOptionalBytes(ByteString.EMPTY); | 163 builder.setOptionalBytes(ByteString.EMPTY); |
| 186 builder.setOptionalNestedEnum(TestAllTypes.NestedEnum.FOO); | 164 builder.setOptionalNestedEnum(TestAllTypes.NestedEnum.FOO); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 .setOptionalBytes(ByteString.copyFromUtf8("y")) | 226 .setOptionalBytes(ByteString.copyFromUtf8("y")) |
| 249 .setOptionalNestedEnum(TestAllTypes.NestedEnum.BAR) | 227 .setOptionalNestedEnum(TestAllTypes.NestedEnum.BAR) |
| 250 .build(); | 228 .build(); |
| 251 assertTrue(message.hasField(optionalInt32Field)); | 229 assertTrue(message.hasField(optionalInt32Field)); |
| 252 assertTrue(message.hasField(optionalStringField)); | 230 assertTrue(message.hasField(optionalStringField)); |
| 253 assertTrue(message.hasField(optionalBytesField)); | 231 assertTrue(message.hasField(optionalBytesField)); |
| 254 assertTrue(message.hasField(optionalNestedEnumField)); | 232 assertTrue(message.hasField(optionalNestedEnumField)); |
| 255 assertEquals(4, message.getAllFields().size()); | 233 assertEquals(4, message.getAllFields().size()); |
| 256 } | 234 } |
| 257 | 235 |
| 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 | |
| 306 public void testMessageField() { | 236 public void testMessageField() { |
| 307 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); | 237 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); |
| 308 assertFalse(builder.hasOptionalNestedMessage()); | 238 assertFalse(builder.hasOptionalNestedMessage()); |
| 309 assertFalse(builder.build().hasOptionalNestedMessage()); | 239 assertFalse(builder.build().hasOptionalNestedMessage()); |
| 310 | 240 |
| 311 TestAllTypes.NestedMessage.Builder nestedBuilder = | 241 TestAllTypes.NestedMessage.Builder nestedBuilder = |
| 312 builder.getOptionalNestedMessageBuilder(); | 242 builder.getOptionalNestedMessageBuilder(); |
| 313 assertTrue(builder.hasOptionalNestedMessage()); | 243 assertTrue(builder.hasOptionalNestedMessage()); |
| 314 assertTrue(builder.build().hasOptionalNestedMessage()); | 244 assertTrue(builder.build().hasOptionalNestedMessage()); |
| 315 | 245 |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 434 DynamicMessage dynamicOptionalOnlyMessage = | 364 DynamicMessage dynamicOptionalOnlyMessage = |
| 435 DynamicMessage.getDefaultInstance( | 365 DynamicMessage.getDefaultInstance( |
| 436 TestOptionalFieldsOnly.getDescriptor()) | 366 TestOptionalFieldsOnly.getDescriptor()) |
| 437 .getParserForType().parseFrom(data); | 367 .getParserForType().parseFrom(data); |
| 438 assertEquals( | 368 assertEquals( |
| 439 0, dynamicOptionalOnlyMessage.getUnknownFields().toByteString().size()); | 369 0, dynamicOptionalOnlyMessage.getUnknownFields().toByteString().size()); |
| 440 assertEquals(optionalOnlyMessage.toByteString(), | 370 assertEquals(optionalOnlyMessage.toByteString(), |
| 441 dynamicOptionalOnlyMessage.toByteString()); | 371 dynamicOptionalOnlyMessage.toByteString()); |
| 442 } | 372 } |
| 443 } | 373 } |
| OLD | NEW |