|
|
|
@ -38,6 +38,7 @@ import com.google.protobuf.Descriptors.OneofDescriptor; |
|
|
|
|
import com.google.protobuf.FieldPresenceTestProto.TestAllTypes; |
|
|
|
|
import com.google.protobuf.FieldPresenceTestProto.TestOptionalFieldsOnly; |
|
|
|
|
import com.google.protobuf.FieldPresenceTestProto.TestRepeatedFieldsOnly; |
|
|
|
|
import com.google.protobuf.testing.proto.TestProto3Optional; |
|
|
|
|
import protobuf_unittest.UnittestProto; |
|
|
|
|
import junit.framework.TestCase; |
|
|
|
|
|
|
|
|
@ -101,6 +102,113 @@ public class FieldPresenceTest extends TestCase { |
|
|
|
|
UnittestProto.TestAllTypes.Builder.class, TestAllTypes.Builder.class, "OneofBytes"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void testHasMethodForProto3Optional() throws Exception { |
|
|
|
|
assertFalse(TestProto3Optional.getDefaultInstance().hasOptionalInt32()); |
|
|
|
|
assertFalse(TestProto3Optional.getDefaultInstance().hasOptionalInt64()); |
|
|
|
|
assertFalse(TestProto3Optional.getDefaultInstance().hasOptionalUint32()); |
|
|
|
|
assertFalse(TestProto3Optional.getDefaultInstance().hasOptionalUint64()); |
|
|
|
|
assertFalse(TestProto3Optional.getDefaultInstance().hasOptionalSint32()); |
|
|
|
|
assertFalse(TestProto3Optional.getDefaultInstance().hasOptionalSint64()); |
|
|
|
|
assertFalse(TestProto3Optional.getDefaultInstance().hasOptionalFixed32()); |
|
|
|
|
assertFalse(TestProto3Optional.getDefaultInstance().hasOptionalFixed64()); |
|
|
|
|
assertFalse(TestProto3Optional.getDefaultInstance().hasOptionalFloat()); |
|
|
|
|
assertFalse(TestProto3Optional.getDefaultInstance().hasOptionalDouble()); |
|
|
|
|
assertFalse(TestProto3Optional.getDefaultInstance().hasOptionalBool()); |
|
|
|
|
assertFalse(TestProto3Optional.getDefaultInstance().hasOptionalString()); |
|
|
|
|
assertFalse(TestProto3Optional.getDefaultInstance().hasOptionalBytes()); |
|
|
|
|
|
|
|
|
|
TestProto3Optional.Builder builder = TestProto3Optional.newBuilder().setOptionalInt32(0); |
|
|
|
|
assertTrue(builder.hasOptionalInt32()); |
|
|
|
|
assertTrue(builder.build().hasOptionalInt32()); |
|
|
|
|
|
|
|
|
|
TestProto3Optional.Builder otherBuilder = TestProto3Optional.newBuilder().setOptionalInt32(1); |
|
|
|
|
otherBuilder.mergeFrom(builder.build()); |
|
|
|
|
assertTrue(otherBuilder.hasOptionalInt32()); |
|
|
|
|
assertEquals(0, otherBuilder.getOptionalInt32()); |
|
|
|
|
|
|
|
|
|
TestProto3Optional.Builder builder3 = |
|
|
|
|
TestProto3Optional.newBuilder().setOptionalNestedEnumValue(5); |
|
|
|
|
assertTrue(builder3.hasOptionalNestedEnum()); |
|
|
|
|
|
|
|
|
|
TestProto3Optional.Builder builder4 = |
|
|
|
|
TestProto3Optional.newBuilder().setOptionalNestedEnum(TestProto3Optional.NestedEnum.FOO); |
|
|
|
|
assertTrue(builder4.hasOptionalNestedEnum()); |
|
|
|
|
|
|
|
|
|
TestProto3Optional proto = TestProto3Optional.parseFrom(builder.build().toByteArray()); |
|
|
|
|
assertTrue(proto.hasOptionalInt32()); |
|
|
|
|
assertTrue(proto.toBuilder().hasOptionalInt32()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static void assertProto3OptionalReflection(String name) throws Exception { |
|
|
|
|
FieldDescriptor fieldDescriptor = TestProto3Optional.getDescriptor().findFieldByName(name); |
|
|
|
|
OneofDescriptor oneofDescriptor = fieldDescriptor.getContainingOneof(); |
|
|
|
|
assertNotNull(fieldDescriptor.getContainingOneof()); |
|
|
|
|
assertTrue(fieldDescriptor.hasOptionalKeyword()); |
|
|
|
|
assertTrue(fieldDescriptor.hasPresence()); |
|
|
|
|
|
|
|
|
|
assertFalse(TestProto3Optional.getDefaultInstance().hasOneof(oneofDescriptor)); |
|
|
|
|
assertNull(TestProto3Optional.getDefaultInstance().getOneofFieldDescriptor(oneofDescriptor)); |
|
|
|
|
|
|
|
|
|
TestProto3Optional.Builder builder = TestProto3Optional.newBuilder(); |
|
|
|
|
builder.setField(fieldDescriptor, fieldDescriptor.getDefaultValue()); |
|
|
|
|
assertTrue(builder.hasField(fieldDescriptor)); |
|
|
|
|
assertEquals(fieldDescriptor.getDefaultValue(), builder.getField(fieldDescriptor)); |
|
|
|
|
assertTrue(builder.build().hasField(fieldDescriptor)); |
|
|
|
|
assertEquals(fieldDescriptor.getDefaultValue(), builder.build().getField(fieldDescriptor)); |
|
|
|
|
assertTrue(builder.hasOneof(oneofDescriptor)); |
|
|
|
|
assertEquals(fieldDescriptor, builder.getOneofFieldDescriptor(oneofDescriptor)); |
|
|
|
|
assertTrue(builder.build().hasOneof(oneofDescriptor)); |
|
|
|
|
assertEquals(fieldDescriptor, builder.build().getOneofFieldDescriptor(oneofDescriptor)); |
|
|
|
|
|
|
|
|
|
TestProto3Optional.Builder otherBuilder = TestProto3Optional.newBuilder(); |
|
|
|
|
otherBuilder.mergeFrom(builder.build()); |
|
|
|
|
assertTrue(otherBuilder.hasField(fieldDescriptor)); |
|
|
|
|
assertEquals(fieldDescriptor.getDefaultValue(), otherBuilder.getField(fieldDescriptor)); |
|
|
|
|
|
|
|
|
|
TestProto3Optional proto = TestProto3Optional.parseFrom(builder.build().toByteArray()); |
|
|
|
|
assertTrue(proto.hasField(fieldDescriptor)); |
|
|
|
|
assertTrue(proto.toBuilder().hasField(fieldDescriptor)); |
|
|
|
|
|
|
|
|
|
DynamicMessage.Builder dynamicBuilder = |
|
|
|
|
DynamicMessage.newBuilder(TestProto3Optional.getDescriptor()); |
|
|
|
|
dynamicBuilder.setField(fieldDescriptor, fieldDescriptor.getDefaultValue()); |
|
|
|
|
assertTrue(dynamicBuilder.hasField(fieldDescriptor)); |
|
|
|
|
assertEquals(fieldDescriptor.getDefaultValue(), dynamicBuilder.getField(fieldDescriptor)); |
|
|
|
|
assertTrue(dynamicBuilder.build().hasField(fieldDescriptor)); |
|
|
|
|
assertEquals( |
|
|
|
|
fieldDescriptor.getDefaultValue(), dynamicBuilder.build().getField(fieldDescriptor)); |
|
|
|
|
assertTrue(dynamicBuilder.hasOneof(oneofDescriptor)); |
|
|
|
|
assertEquals(fieldDescriptor, dynamicBuilder.getOneofFieldDescriptor(oneofDescriptor)); |
|
|
|
|
assertTrue(dynamicBuilder.build().hasOneof(oneofDescriptor)); |
|
|
|
|
assertEquals(fieldDescriptor, dynamicBuilder.build().getOneofFieldDescriptor(oneofDescriptor)); |
|
|
|
|
|
|
|
|
|
DynamicMessage.Builder otherDynamicBuilder = |
|
|
|
|
DynamicMessage.newBuilder(TestProto3Optional.getDescriptor()); |
|
|
|
|
otherDynamicBuilder.mergeFrom(dynamicBuilder.build()); |
|
|
|
|
assertTrue(otherDynamicBuilder.hasField(fieldDescriptor)); |
|
|
|
|
assertEquals(fieldDescriptor.getDefaultValue(), otherDynamicBuilder.getField(fieldDescriptor)); |
|
|
|
|
|
|
|
|
|
DynamicMessage dynamicProto = |
|
|
|
|
DynamicMessage.parseFrom(TestProto3Optional.getDescriptor(), builder.build().toByteArray()); |
|
|
|
|
assertTrue(dynamicProto.hasField(fieldDescriptor)); |
|
|
|
|
assertTrue(dynamicProto.toBuilder().hasField(fieldDescriptor)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void testProto3Optional_reflection() throws Exception { |
|
|
|
|
assertProto3OptionalReflection("optional_int32"); |
|
|
|
|
assertProto3OptionalReflection("optional_int64"); |
|
|
|
|
assertProto3OptionalReflection("optional_uint32"); |
|
|
|
|
assertProto3OptionalReflection("optional_uint64"); |
|
|
|
|
assertProto3OptionalReflection("optional_sint32"); |
|
|
|
|
assertProto3OptionalReflection("optional_sint64"); |
|
|
|
|
assertProto3OptionalReflection("optional_fixed32"); |
|
|
|
|
assertProto3OptionalReflection("optional_fixed64"); |
|
|
|
|
assertProto3OptionalReflection("optional_float"); |
|
|
|
|
assertProto3OptionalReflection("optional_double"); |
|
|
|
|
assertProto3OptionalReflection("optional_bool"); |
|
|
|
|
assertProto3OptionalReflection("optional_string"); |
|
|
|
|
assertProto3OptionalReflection("optional_bytes"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void testOneofEquals() throws Exception { |
|
|
|
|
TestAllTypes.Builder builder = TestAllTypes.newBuilder(); |
|
|
|
|