示例#1
0
        public void Test_ToFromBinary_ReflectionBasedTests <T>(Type type) where T : class, IBinarizable
        {
            // Create an instance of the type and exercise the IBinarizable serialization methods on the default object,
            // casting it to IBinarizable (which it must implement).

            var instance = Activator.CreateInstance(type) as T;

            instance.Should().NotBeNull();

            var writer = new TestBinaryWriter();

            instance.WriteBinary(writer);

            var reader      = new TestBinaryReader(writer._stream.BaseStream as MemoryStream);
            var newInstance = Activator.CreateInstance(type) as T;

            newInstance.Should().NotBeNull();

            newInstance.ReadBinary(reader);

            // Check the two match. If there are no state members for the comparison BeEquivalent will throw
            // a System.InvalidOperationException exception.Catch it and return success in this case
            try
            {
                newInstance.Should().BeEquivalentTo(instance, "Binarizable serialization/deserialization failed to produce correct result");
            }
            catch (InvalidOperationException e)
            {
                if (e.Message != "No members were found for comparison. Please specify some members to include in the comparison or choose a more meaningful assertion.")
                {
                    throw;
                }
            }
        }
示例#2
0
        private void PerformSerializationVersionsTestOnField(FieldInfo field, Type type)
        {
            var versionNumbers = field.FieldType.FullName == "System.Byte[]" ? (byte[])field.GetValue(null) : null;

            if (versionNumbers == null)
            {
                // This is not a VERSION_NUMBERS field, move along
                return;
            }

            // Test all the defined version numbers may be serialised and deserialised
            foreach (var versionNumber in versionNumbers)
            {
                var writer = new TestBinaryWriter();
                writer.WriteByte(versionNumber);
                var reader = new TestBinaryReader(writer._stream.BaseStream as MemoryStream);

                var item = Activator.CreateInstance(type) as IBinarizable;

                item.Should().NotBeNull();

                Action act = () => item.ReadBinary(reader);
                act.Should().NotThrow <TRexSerializationVersionException>();
            }
        }
示例#3
0
 /// <summary>
 /// Tests a class by instantiating a default instance and wrapping it in an IBinarizable implementing class which
 /// then exercises the IFromToBinary TRex interface on that class.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 public static void TestNonBinarizableClass <T>() where T : class, IFromToBinary, new()
 {
     Assert.Throws <TRexNonBinarizableException>(() =>
     {
         var instance = new TestBinarizable_Class <T>();
         var bw       = new TestBinaryWriter();
         instance.WriteBinary(bw);
     });
 }
示例#4
0
        public static void RoundTripSerialise <T>(T instance) where T : IFromToBinary, new()
        {
            var writer = new TestBinaryWriter();

            instance.ToBinary(writer);

            var instance2 = new T();

            instance2.FromBinary(new TestBinaryReader(writer._stream.BaseStream as MemoryStream));

            instance.Should().BeEquivalentTo(instance2);
        }
示例#5
0
        public void Test_FromTo_BoundingIntegerExtent2D()
        {
            var extent = new TestBinarizable_Struct_Extension <BoundingIntegerExtent2D>
            {
                member = new BoundingIntegerExtent2D(0, 1, 100, 101)
            };

            var bw = new TestBinaryWriter();

            extent.WriteBinary(bw);

            var br     = new TestBinaryReader(bw._stream.BaseStream as MemoryStream);
            var result = new TestBinarizable_Struct_Extension <BoundingIntegerExtent2D>();

            result.ReadBinary(br);

            Assert.True(extent.member.Equals(result.member), "Bounding integer 2D extent not same after round trip serialization");
        }
示例#6
0
        /// <summary>
        /// Given an instance of a class to test serialise it to an Ignite IBinaryObject, then deserialize it to
        /// the source type, comparing the before and after versions for equality
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="U"></typeparam>
        /// <param name="instance"></param>
        /// <param name="failureMsg"></param>
        public static T TestAnExtensionBinarizableSerialization <T, U>(T instance, string failureMsg = "") where U : class, new() where T : TestBinarizable_Extension <U>, new()
        {
            var bw = new TestBinaryWriter();

            instance.WriteBinary(bw);

            var br     = new TestBinaryReader(bw._stream.BaseStream as MemoryStream);
            var result = new T();

            result.ReadBinary(br);

            if (failureMsg != "")
            {
                result.member.Should().BeEquivalentTo(instance.member, $"{typeof(T).FullName}: {failureMsg}");
            }
            else
            {
                result.member.Should().BeEquivalentTo(instance.member, $"{typeof(T).FullName}: not the same after round trip serialisation");
            }

            return(result);
        }
示例#7
0
        private void PerformSerializationVersionTestOnField(FieldInfo field, Type type)
        {
            uint versionNumber = field.FieldType.FullName == "System.Byte" ? (byte)field.GetValue(null)
        : field.FieldType.FullName == "System.UInt32" ? (ushort)field.GetValue(null)
        : field.FieldType.FullName == "System.UInt16" ? (uint)field.GetValue(null)
        : uint.MaxValue;

            if (versionNumber == uint.MaxValue)
            {
                // This is not a VERSION_NUMBER field, move along
                return;
            }

            var expectedVersions = new[] { versionNumber };

            var writer = new TestBinaryWriter();

            writer.WriteByte((byte)(versionNumber + 1));
            var reader = new TestBinaryReader(writer._stream.BaseStream as MemoryStream);

            var item = Activator.CreateInstance(type);

            item.Should().NotBeNull();

            Action act = null;

            if (item is IVersionCheckedBinarizableSerializationBase versionCheckedItem)
            {
                act = () => versionCheckedItem.InternalFromBinary(reader);
            }
            else if (item is IBinarizable binarizableItem)
            {
                act = () => binarizableItem.ReadBinary(reader);
            }

            act.Should().Throw <TRexSerializationVersionException>().WithMessage("Invalid version read during deserialization*"); //TRexSerializationVersionException.ErrorMessage(expectedVersions, versionNumber + 1));
        }