Пример #1
0
        public void should_read_SerializableType_string()
        {
            writer.WriteRaw(serializableTypeStringExpected);
            ResetStream();
            Assert.That(stream.ToArray(), Is.EqualTo(serializableTypeStringExpected));

            var st     = iftFactory(typeof(string)).ToSerializableType();
            var result = reader.ReadSerializableType();

            Assert.That(result, Is.EqualTo(st));
        }
Пример #2
0
        public void Stream()
        {
            var typeMap = scope.Resolve <TypeMap>();

            using (var ms = new MemoryStream())
                using (var sw = new ZetboxStreamWriter(typeMap, new BinaryWriter(ms)))
                    using (var sr = new ZetboxStreamReader(typeMap, new BinaryReader(ms)))
                    {
                        var ctx = GetContext();

                        ctx.Attach(obj);
                        obj.ToStream(sw, null, false);

                        Assert.That(ms.Length, Is.GreaterThan(0));

                        ms.Seek(0, SeekOrigin.Begin);

                        var t = sr.ReadSerializableType();
                        Assert.That(t.GetSystemType().IsAssignableFrom(typeof(ANewObjectClass)), string.Format("{0} not assignable to {1}", typeof(ANewObjectClass), t));

                        BaseClientDataObjectMockImpl result = new BaseClientDataObjectMockImpl(null);
                        result.FromStream(sr);

                        Assert.That(result.GetType(), Is.EqualTo(obj.GetType()));
                        Assert.That(result.ID, Is.EqualTo(obj.ID));
                        Assert.That(result.ObjectState, Is.EqualTo(obj.ObjectState));
                    }
        }
Пример #3
0
        public void should_use_interfacetype_on_the_stream()
        {
            var typeMap = scope.Resolve <TypeMap>();
            var ms      = new MemoryStream();
            var sw      = new ZetboxStreamWriter(typeMap, new BinaryWriter(ms));
            var sr      = new ZetboxStreamReader(typeMap, new BinaryReader(ms));

            obj.ToStream(sw, null, false);
            ms.Seek(0, SeekOrigin.Begin);

            var t = sr.ReadSerializableType();

            Assert.That(t, Is.EqualTo(_iftFactory(typeof(TestObjClass)).ToSerializableType()));
        }
Пример #4
0
        private List <IStreamable> ReceiveObjectList(IZetboxContext ctx, ZetboxStreamReader sr)
        {
            List <IStreamable> result = new List <IStreamable>();
            var cont = sr.ReadBoolean();

            while (cont)
            {
                var objType = sr.ReadSerializableType();

                IStreamable obj = (IStreamable)ctx.Internals().CreateUnattached(_iftFactory(objType.GetSystemType()));
                obj.FromStream(sr);

                result.Add((IStreamable)obj);
                cont = sr.ReadBoolean();
            }
            return(result);
        }
Пример #5
0
        public void FromStream_creates_correct_Object()
        {
            var typeMap = scope.Resolve<TypeMap>();
            var ms = new MemoryStream();
            var sw = new ZetboxStreamWriter(typeMap, new BinaryWriter(ms));
            var sr = new ZetboxStreamReader(typeMap, new BinaryReader(ms));

            TestObjClassSerializationMock.ToStream<TestObjClass, TestEnum>(sw, _iftFactory);
            sw.Flush();

            Assert.That(ms.Length, Is.GreaterThan(0));
            ms.Seek(0, SeekOrigin.Begin);

            var t = sr.ReadSerializableType();
            Assert.That(t.GetSystemType().IsAssignableFrom(typeof(TestObjClass)), string.Format("{0} not assignable to {1}", typeof(TestObjClass), t));

            var obj = new TestObjClassImpl();
            obj.FromStream(sr);
            ctx.Attach(obj);

            TestObjClassSerializationMock.AssertCorrectContentsEnum<TestObjClass>(obj);
        }
Пример #6
0
        public void FromStream_creates_correct_Object()
        {
            var typeMap = scope.Resolve <TypeMap>();
            var ms      = new MemoryStream();
            var sw      = new ZetboxStreamWriter(typeMap, new BinaryWriter(ms));
            var sr      = new ZetboxStreamReader(typeMap, new BinaryReader(ms));

            TestObjClassSerializationMock.ToStream <TestObjClass, TestEnum>(sw, _iftFactory);
            sw.Flush();

            Assert.That(ms.Length, Is.GreaterThan(0));
            ms.Seek(0, SeekOrigin.Begin);

            var t = sr.ReadSerializableType();

            Assert.That(t.GetSystemType().IsAssignableFrom(typeof(TestObjClass)), string.Format("{0} not assignable to {1}", typeof(TestObjClass), t));

            var obj = new TestObjClassImpl();

            obj.FromStream(sr);
            ctx.Attach(obj);

            TestObjClassSerializationMock.AssertCorrectContentsEnum <TestObjClass>(obj);
        }
Пример #7
0
        private List <IStreamable> ReceiveObjectList(IZetboxContext ctx, ZetboxStreamReader sr)
        {
            List <IStreamable> result = new List <IStreamable>();
            bool cont                  = sr.ReadBoolean();
            long dbgByteCounter        = 0;
            long dbgObjTypeByteCounter = 0;

            while (cont)
            {
                long dbgCurrentPos = sr.BaseStream.Position;
                var  objType       = sr.ReadSerializableType();
                dbgObjTypeByteCounter += sr.BaseStream.Position - dbgCurrentPos;

                IStreamable obj = (IStreamable)ctx.Internals().CreateUnattached(_iftFactory(objType.GetSystemType()));
                obj.FromStream(sr);
                result.Add((IStreamable)obj);

                cont = sr.ReadBoolean();
                long dbgSize = sr.BaseStream.Position - dbgCurrentPos;
                dbgByteCounter += dbgSize;
            }
            Logging.Facade.DebugFormat("ReceiveObjectList: {0:n0} objects; {1:n0} bytes total size; {2:n0} bytes avg. size / object; Total ObjType size: {3:n0}; {4:n2}% of total", result.Count, dbgByteCounter, (double)dbgByteCounter / (double)result.Count, dbgObjTypeByteCounter, (double)dbgObjTypeByteCounter / (double)dbgByteCounter * 100.0);
            return(result);
        }
Пример #8
0
        public static void AssertCorrectContents <LOCALINTERFACE, ENUMTYPE>(ZetboxStreamReader sr, InterfaceType.Factory iftFactory)
            where LOCALINTERFACE : TestObjClass <LOCALINTERFACE, ENUMTYPE>
            where ENUMTYPE : struct
        {
            Assert.That(sr, Is.Not.Null, "no stream to inspect");

            var objType = sr.ReadSerializableType();

            Assert.That(objType, Is.EqualTo(GetSerializableType <LOCALINTERFACE, ENUMTYPE>(iftFactory)), "wrong interface type found");

            var testObjId = sr.ReadInt32();

            Assert.That(testObjId, Is.EqualTo(TestObjClassId), "wrong object ID found");

            DataObjectState?objectState = null;

            sr.ReadConverter(i => objectState = (DataObjectState)i);
            Assert.That(objectState, Is.EqualTo(TestObjectState), "wrong ObjectState found");

            int accessRights = sr.ReadInt32();

            Assert.That(accessRights, Is.GreaterThan(0), "wrong Access Rights found");

            // TestObjClass

            // BaseTestObjClass Reference
            int?testObjRefId;

            sr.Read(out testObjRefId);
            Assert.That(testObjRefId, Is.EqualTo(TestBaseClassId), "wrong BaseObjClass ID found");

            // StringProp
            string testStringProp;

            sr.Read(out testStringProp);
            Assert.That(testStringProp, Is.EqualTo(TestStringPropValue), "wrong StringProp Value found");

            //// SubClasses are not serialized, but fetched lazily
            //foreach (int subClassID in TestSubClassesIds)
            //{
            //    bool continuationMarkerForSubClasses = false;
            //    BinarySerializer.FromStream(out continuationMarkerForSubClasses, sr);
            //    Assert.That(continuationMarkerForSubClasses, Is.True, "wrong continuation marker for subClassId {0}", subClassID);

            //    int readSubClassId;
            //    BinarySerializer.FromStream(out readSubClassId, sr);
            //    Assert.That(readSubClassId, Is.EqualTo(readSubClassId), "wrong subClassId read", subClassID);
            //}

            //bool continuationMarkerAfterSubClasses = false;
            //BinarySerializer.FromStream(out continuationMarkerAfterSubClasses, sr);
            //Assert.That(continuationMarkerAfterSubClasses, Is.False, "wrong continuation marker after subClassIds");

            // TestEnumProp
            var testEnum = (TestEnum)sr.ReadInt32();

            Assert.That(testEnum, Is.EqualTo(TestEnum.TestSerializationValue), "wrong enum value found");

            // TestNames
            for (int i = 0; i < TestTestNamesIds.Length; i++)
            {
                var continuationMarkerForCes = sr.ReadBoolean();
                Assert.That(continuationMarkerForCes, Is.True, "wrong continuation marker for testName #{0}", i);

                var ceType = sr.ReadSerializableType();
                Assert.That(ceType, Is.EqualTo(GetSerializableCollectionEntryType <LOCALINTERFACE, ENUMTYPE>(iftFactory)), "wrong interface type found for collection entry #{0}", i);

                var readCeId = sr.ReadInt32();
                Assert.That(readCeId, Is.EqualTo(TestTestNamesIds[i]), "wrong id read for collection entry #{0}", i);

                DataObjectState?ceObjectState = null;
                sr.ReadConverter(read => ceObjectState = (DataObjectState)read);
                Assert.That(ceObjectState, Is.EqualTo(TestCollectionEntryState), "wrong ObjectState found for collection entry #{0}", i);

                var readCeAccessRights = sr.ReadInt32();
                Assert.That(readCeAccessRights, Is.GreaterThan(0), "wrong access rights for collection entry #{0}", i);

                var readValue = sr.ReadString();
                Assert.That(readValue, Is.EqualTo(TestTestNamesValues[i]), "wrong value read for collection entry #{0}", i);
            }

            var continuationMarkerAfterCes = sr.ReadBoolean();

            Assert.That(continuationMarkerAfterCes, Is.False, "wrong continuation marker after testNames collection entries");
        }
Пример #9
0
        public void should_use_interfacetype_on_the_stream()
        {
            var typeMap = scope.Resolve<TypeMap>();
            var ms = new MemoryStream();
            var sw = new ZetboxStreamWriter(typeMap, new BinaryWriter(ms));
            var sr = new ZetboxStreamReader(typeMap, new BinaryReader(ms));

            obj.ToStream(sw, null, false);
            ms.Seek(0, SeekOrigin.Begin);

            var t = sr.ReadSerializableType();
            Assert.That(t, Is.EqualTo(_iftFactory(typeof(TestObjClass)).ToSerializableType()));
        }
Пример #10
0
        public void Stream()
        {
            var typeMap = scope.Resolve<TypeMap>();
            using (var ms = new MemoryStream())
            using (var sw = new ZetboxStreamWriter(typeMap, new BinaryWriter(ms)))
            using (var sr = new ZetboxStreamReader(typeMap, new BinaryReader(ms)))
            {
                var ctx = GetContext();

                ctx.Attach(obj);
                obj.ToStream(sw, null, false);

                Assert.That(ms.Length, Is.GreaterThan(0));

                ms.Seek(0, SeekOrigin.Begin);

                var t = sr.ReadSerializableType();
                Assert.That(t.GetSystemType().IsAssignableFrom(typeof(ANewObjectClass)), string.Format("{0} not assignable to {1}", typeof(ANewObjectClass), t));

                BaseClientDataObjectMockImpl result = new BaseClientDataObjectMockImpl(null);
                result.FromStream(sr);

                Assert.That(result.GetType(), Is.EqualTo(obj.GetType()));
                Assert.That(result.ID, Is.EqualTo(obj.ID));
                Assert.That(result.ObjectState, Is.EqualTo(obj.ObjectState));
            }
        }