예제 #1
0
        public void DynamicParseUnititialized()
        {
            MessageDescriptor descriptor = TestRequired.Descriptor;
            var e = Assert.Throws <InvalidProtocolBufferException>(() => DynamicMessage.ParseFrom(descriptor, ByteString.Empty));

            Assert.AreEqual("Message missing required fields: a, b, c", e.Message);
        }
예제 #2
0
        public void InterleavedFieldsAndExtensions()
        {
            // Tests that fields are written in order even when extension ranges
            // are interleaved with field numbers.
            ByteString data =
                TestFieldOrderings.CreateBuilder()
                .SetMyInt(1)
                .SetMyString("foo")
                .SetMyFloat(1.0F)
                .SetExtension(Unittest.MyExtensionInt, 23)
                .SetExtension(Unittest.MyExtensionString, "bar")
                .Build().ToByteString();

            AssertFieldsInOrder(data);

            MessageDescriptor descriptor   = TestFieldOrderings.Descriptor;
            ByteString        dynamic_data =
                DynamicMessage.CreateBuilder(TestFieldOrderings.Descriptor)
                .SetField(descriptor.FindDescriptor <FieldDescriptor>("my_int"), 1L)
                .SetField(descriptor.FindDescriptor <FieldDescriptor>("my_string"), "foo")
                .SetField(descriptor.FindDescriptor <FieldDescriptor>("my_float"), 1.0F)
                .SetField(Unittest.MyExtensionInt.Descriptor, 23)
                .SetField(Unittest.MyExtensionString.Descriptor, "bar")
                .WeakBuild().ToByteString();

            AssertFieldsInOrder(dynamic_data);
        }
예제 #3
0
 public override object this[FieldDescriptor field]
 {
     get
     {
         if (field.IsExtension)
         {
             VerifyContainingType(field);
             object value = extensions[field];
             if (value == null)
             {
                 // Lacking an ExtensionRegistry, we have no way to determine the
                 // extension's real type, so we return a DynamicMessage.
                 // TODO(jonskeet): Work out what this means
                 return(DynamicMessage.GetDefaultInstance(field.MessageType));
             }
             else
             {
                 return(value);
             }
         }
         else
         {
             return(base[field]);
         }
     }
 }
예제 #4
0
        public void DynamicBuildPartial()
        {
            // We're mostly testing that no exception is thrown.
            DynamicMessage message = DynamicMessage.CreateBuilder(TestRequired.Descriptor).BuildPartial();

            Assert.IsFalse(message.Initialized);
        }
        public void DynamicOneofMessage()
        {
            DynamicMessage.Builder builder =
                DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
            OneofDescriptor oneof = TestAllTypes.Descriptor.Oneofs[0];

            Assert.False(builder.HasOneof(oneof));
            Assert.AreSame(null, builder.OneofFieldDescriptor(oneof));

            reflectionTester.SetAllFieldsViaReflection(builder);
            Assert.True(builder.HasOneof(oneof));
            FieldDescriptor field = oneof.Field(3);

            Assert.AreSame(field, builder.OneofFieldDescriptor(oneof));
            Assert.AreEqual(TestUtil.ToBytes("604"), builder[field]);

            DynamicMessage message = builder.BuildPartial();

            Assert.IsTrue(message.HasOneof(oneof));

            DynamicMessage.Builder mergedBuilder =
                DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
            FieldDescriptor mergedField = oneof.Field(0);

            mergedBuilder[mergedField] = 123U;
            Assert.IsTrue(mergedBuilder.HasField(mergedField));
            mergedBuilder.MergeFrom(message);
            Assert.IsTrue(mergedBuilder.HasField(field));
            Assert.IsFalse(mergedBuilder.HasField(mergedField));

            mergedBuilder.ClearOneof(oneof);
            Assert.AreSame(null, mergedBuilder.OneofFieldDescriptor(oneof));
            message = mergedBuilder.Build();
            Assert.AreSame(null, message.OneofFieldDescriptor(oneof));
        }
        public void DynamicMessageCopy()
        {
            TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
            TestUtil.SetAllFields(builder);
            TestAllTypes message = builder.Build();

            DynamicMessage copy = DynamicMessage.CreateBuilder(message).Build();

            reflectionTester.AssertAllFieldsSetViaReflection(copy);

            // Oneof
            FieldDescriptor bytesField =
                TestAllTypes.Descriptor.FindFieldByName("oneof_bytes");
            FieldDescriptor uint32Field =
                TestAllTypes.Descriptor.FindFieldByName("oneof_uint32");

            Assert.True(copy.HasField(bytesField));
            Assert.False(copy.HasField(uint32Field));

            DynamicMessage.Builder dynamicBuilder = DynamicMessage.CreateBuilder(message);
            dynamicBuilder[uint32Field] = 123U;
            DynamicMessage copy2 = dynamicBuilder.Build();

            Assert.IsFalse(copy2.HasField(bytesField));
            Assert.IsTrue(copy2.HasField(uint32Field));
            Assert.AreEqual(123U, copy2[uint32Field]);
        }
예제 #7
0
        public void DynamicMergeFrom()
        {
            DynamicMessage result = (DynamicMessage)DynamicMessage.CreateBuilder(MergeDest)
                                    .MergeFrom((DynamicMessage)DynamicMessage.CreateBuilder(MergeSource).Build())
                                    .Build();

            Assert.AreEqual(MergeResultText, result.ToString());
        }
예제 #8
0
        public void MergeFromDynamic()
        {
            TestAllTypes result = (TestAllTypes)TestAllTypes.CreateBuilder(MergeDest)
                                  .MergeFrom(DynamicMessage.CreateBuilder(MergeSource).Build())
                                  .Build();

            Assert.AreEqual(MergeResultText, result.ToString());
        }
예제 #9
0
 public void DynamicUninitializedException()
 {
     try {
         DynamicMessage.CreateBuilder(TestRequired.Descriptor).Build();
         Assert.Fail("Should have thrown an exception.");
     } catch (UninitializedMessageException e) {
         Assert.AreEqual("Message missing required fields: a, b, c", e.Message);
     }
 }
예제 #10
0
        public void DynamicMessageAccessors()
        {
            IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);

            reflectionTester.SetAllFieldsViaReflection(builder);
            IMessage message = builder.WeakBuild();

            reflectionTester.AssertAllFieldsSetViaReflection(message);
        }
        /// <summary>
        /// Asserts that the given protos are equal and have the same hash code.
        /// </summary>
        private static void CheckEqualsIsConsistent(IMessage message)
        {
            // Object should be equal to itself.
            Assert.AreEqual(message, message);

            // Object should be equal to a dynamic copy of itself.
            DynamicMessage dynamic = DynamicMessage.CreateBuilder(message).Build();

            CheckEqualsIsConsistent(message, dynamic);
        }
예제 #12
0
 public void DynamicParseUnititialized()
 {
     try {
         MessageDescriptor descriptor = TestRequired.Descriptor;
         DynamicMessage.ParseFrom(descriptor, ByteString.Empty);
         Assert.Fail("Should have thrown an exception.");
     } catch (InvalidProtocolBufferException e) {
         Assert.AreEqual("Message missing required fields: a, b, c", e.Message);
     }
 }
예제 #13
0
        public void DynamicMessageCopy()
        {
            TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
            TestUtil.SetAllFields(builder);
            TestAllTypes message = builder.Build();

            DynamicMessage copy = DynamicMessage.CreateBuilder(message).Build();

            reflectionTester.AssertAllFieldsSetViaReflection(copy);
        }
예제 #14
0
        public void DynamicMessageRepeatedSetters()
        {
            IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);

            reflectionTester.SetAllFieldsViaReflection(builder);
            reflectionTester.ModifyRepeatedFieldsViaReflection(builder);
            IMessage message = builder.WeakBuild();

            reflectionTester.AssertRepeatedFieldsModifiedViaReflection(message);
        }
예제 #15
0
 public void DoubleBuildError()
 {
     DynamicMessage.Builder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
     builder.Build();
     try {
         builder.Build();
         Assert.Fail("Should have thrown exception.");
     } catch (InvalidOperationException) {
         // Success.
     }
 }
예제 #16
0
        public void DynamicMessageSerializedSize()
        {
            TestAllTypes message = TestUtil.GetAllSet();

            IBuilder dynamicBuilder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);

            reflectionTester.SetAllFieldsViaReflection(dynamicBuilder);
            IMessage dynamicMessage = dynamicBuilder.WeakBuild();

            Assert.AreEqual(message.SerializedSize, dynamicMessage.SerializedSize);
        }
예제 #17
0
        public void DynamicMessageExtensionAccessors()
        {
            // We don't need to extensively test DynamicMessage's handling of
            // extensions because, frankly, it doesn't do anything special with them.
            // It treats them just like any other fields.
            IBuilder builder = DynamicMessage.CreateBuilder(TestAllExtensions.Descriptor);

            extensionsReflectionTester.SetAllFieldsViaReflection(builder);
            IMessage message = builder.WeakBuild();

            extensionsReflectionTester.AssertAllFieldsSetViaReflection(message);
        }
예제 #18
0
        public void testDynamicMessagePackedParsing()
        {
            TestPackedTypes.Builder builder = TestPackedTypes.CreateBuilder();
            TestUtil.SetPackedFields(builder);
            TestPackedTypes message = builder.Build();

            ByteString rawBytes = message.ToByteString();

            IMessage message2 = DynamicMessage.ParseFrom(TestPackedTypes.Descriptor, rawBytes);

            packedReflectionTester.AssertPackedFieldsSetViaReflection(message2);
        }
예제 #19
0
        public void RequiredDynamic()
        {
            MessageDescriptor descriptor = TestRequired.Descriptor;

            DynamicMessage.Builder builder = DynamicMessage.CreateBuilder(descriptor);

            Assert.IsFalse(builder.IsInitialized);
            builder[descriptor.FindDescriptor <FieldDescriptor>("a")] = 1;
            Assert.IsFalse(builder.IsInitialized);
            builder[descriptor.FindDescriptor <FieldDescriptor>("b")] = 1;
            Assert.IsFalse(builder.IsInitialized);
            builder[descriptor.FindDescriptor <FieldDescriptor>("c")] = 1;
            Assert.IsTrue(builder.IsInitialized);
        }
예제 #20
0
        public void DynamicMessagePackedSerialization()
        {
            IBuilder builder = DynamicMessage.CreateBuilder(TestPackedTypes.Descriptor);

            packedReflectionTester.SetPackedFieldsViaReflection(builder);
            IMessage message = builder.WeakBuild();

            ByteString      rawBytes = message.ToByteString();
            TestPackedTypes message2 = TestPackedTypes.ParseFrom(rawBytes);

            TestUtil.AssertPackedFieldsSet(message2);

            // In fact, the serialized forms should be exactly the same, byte-for-byte.
            Assert.AreEqual(TestUtil.GetPackedSet().ToByteString(), rawBytes);
        }
예제 #21
0
        public void RequiredDynamicForeign()
        {
            MessageDescriptor descriptor = TestRequiredForeign.Descriptor;

            DynamicMessage.Builder builder = DynamicMessage.CreateBuilder(descriptor);

            Assert.IsTrue(builder.IsInitialized);

            builder[descriptor.FindDescriptor <FieldDescriptor>("optional_message")] = TestRequiredUninitialized;
            Assert.IsFalse(builder.IsInitialized);

            builder[descriptor.FindDescriptor <FieldDescriptor>("optional_message")] = TestRequiredInitialized;
            Assert.IsTrue(builder.IsInitialized);

            builder.AddRepeatedField(descriptor.FindDescriptor <FieldDescriptor>("repeated_message"), TestRequiredUninitialized);
            Assert.IsFalse(builder.IsInitialized);

            builder.SetRepeatedField(descriptor.FindDescriptor <FieldDescriptor>("repeated_message"), 0, TestRequiredInitialized);
            Assert.IsTrue(builder.IsInitialized);
        }
예제 #22
0
        public void ToBuilder()
        {
            DynamicMessage.Builder builder =
                DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
            reflectionTester.SetAllFieldsViaReflection(builder);
            int   unknownFieldNum = 9;
            ulong unknownFieldVal = 90;

            builder.SetUnknownFields(UnknownFieldSet.CreateBuilder()
                                     .AddField(unknownFieldNum,
                                               UnknownField.CreateBuilder().AddVarint(unknownFieldVal).Build())
                                     .Build());
            DynamicMessage message = builder.Build();

            DynamicMessage derived = message.ToBuilder().Build();

            reflectionTester.AssertAllFieldsSetViaReflection(derived);

            IList <ulong> values = derived.UnknownFields.FieldDictionary[unknownFieldNum].VarintList;

            Assert.AreEqual(1, values.Count);
            Assert.AreEqual(unknownFieldVal, values[0]);
        }
예제 #23
0
        public void DynamicUninitializedException()
        {
            var e = Assert.Throws <UninitializedMessageException>(() => DynamicMessage.CreateBuilder(TestRequired.Descriptor).Build());

            Assert.AreEqual("Message missing required fields: a, b, c", e.Message);
        }
 public void DoubleBuildError()
 {
     DynamicMessage.Builder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
     builder.Build();
     Assert.Throws <InvalidOperationException>(() => builder.Build());
 }
예제 #25
0
 public void DynamicMessageDefaults()
 {
     reflectionTester.AssertClearViaReflection(DynamicMessage.GetDefaultInstance(TestAllTypes.Descriptor));
     reflectionTester.AssertClearViaReflection(DynamicMessage.CreateBuilder(TestAllTypes.Descriptor).Build());
 }
예제 #26
0
        public void DynamicMessageSettersRejectNull()
        {
            IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);

            reflectionTester.AssertReflectionSettersRejectNull(builder);
        }