public void TestSerialization()
        {
            SimplePofContext cxt = new SimplePofContext();

            cxt.RegisterUserType(1001, typeof(PersonV1), new PofAnnotationSerializer(1001, typeof(PersonV1)));
            cxt.RegisterUserType(1002, typeof(Child), new PofAnnotationSerializer(1002, typeof(Child), true));

            PersonV1 value = new PersonV1("Frank", "Spencer", 57);
            Child    child = new Child("Betty", "Spencer", 55);

            Binary binValue = EH.ToBinary(child, cxt);
            var    reader   = new DataReader(binValue.GetStream());
            int    typeId   = reader.ReadByte() == 21 ? reader.ReadPackedInt32() : -1;

            PersonV1 teleported  = EH.FromBinary(SerializationHelper.ToBinary(value, cxt), cxt) as PersonV1;
            Child    teleported2 = EH.FromBinary(binValue, cxt) as Child;

            Assert.AreEqual(1002, typeId);
            Assert.IsNotNull(teleported);
            Assert.IsNotNull(teleported2);
            Assert.AreEqual("Frank", teleported.m_firstName);
            Assert.AreEqual("Spencer", teleported.m_lastName);
            Assert.AreEqual(57, teleported.m_age);
            Assert.AreEqual("Betty", teleported2.m_firstName);
            Assert.AreEqual("Spencer", teleported2.m_lastName);
            Assert.AreEqual(55, teleported2.m_age);
        }
Exemple #2
0
 protected virtual void ConfigurePofContext()
 {
     pofSerializer.RegisterUserType(1,
                                    typeof(PortablePerson),
                                    new PortableObjectSerializer(1));
     pofSerializer.RegisterUserType(2,
                                    typeof(Address),
                                    new PortableObjectSerializer(2));
 }
        public void TestReferencesInUniformMap()
        {
            var localCtx = new SimplePofContext();

            localCtx.RegisterUserType(101, typeof(PortablePerson),
                                      new PortableObjectSerializer(101));
            localCtx.RegisterUserType(102, typeof(PortablePersonReference),
                                      new PortableObjectSerializer(102));
            localCtx.RegisterUserType(201, typeof(CompositeKey),
                                      new PortableObjectSerializer(201));

            IDictionary  mapPerson = new Dictionary <CompositeKey, IPortableObject>();
            const String lastName  = "Smith";
            CompositeKey key1      = new CompositeKey(lastName, "ivan"),
                         key2      = new CompositeKey(lastName, "goran");
            var ivan = new PortablePersonReference("Ivan", new DateTime(78, 4, 25));

            ivan.Children = null;
            mapPerson.Add(key1, ivan);
            mapPerson.Add(key2, ivan);

            initPOFWriter();
            m_pofWriter = new PofStreamWriter(m_writer, localCtx);
            m_pofWriter.EnableReference();
            m_pofWriter.WriteDictionary(0, mapPerson, typeof(CompositeKey),
                                        typeof(PortablePersonReference));

            var mapPersonR = new Dictionary <CompositeKey, IPortableObject>();

            initPOFReader();
            m_pofReader = new PofStreamReader(m_reader, localCtx);
            m_pofReader.ReadDictionary(0, (IDictionary)mapPersonR);

            // compare mapPerson with result
            Assert.IsTrue(mapPersonR[key1].Equals(mapPerson[key1]));
            Assert.IsTrue(mapPersonR[key2].Equals(mapPerson[key2]));

            ICollection colValR = mapPersonR.Values;
            IEnumerator iter    = colValR.GetEnumerator();

            iter.MoveNext();
            var val1 = (PortablePersonReference)iter.Current;

            iter.MoveNext();
            var val2 = (PortablePersonReference)iter.Current;

            Assert.IsTrue(val1 == val2);
        }
        public void TestEvolvableObjectSerialization()
        {
            SimplePofContext ctx_v1 = new SimplePofContext();

            ctx_v1.RegisterUserType(1, typeof(EvolvablePortablePerson), new PortableObjectSerializer(1));
            ctx_v1.RegisterUserType(2, typeof(Address), new PortableObjectSerializer(2));

            SimplePofContext ctx_v2 = new SimplePofContext();

            ctx_v2.RegisterUserType(1, typeof(EvolvablePortablePerson2), new PortableObjectSerializer(1));
            ctx_v2.RegisterUserType(2, typeof(Address), new PortableObjectSerializer(2));

            EvolvablePortablePerson2 aleks = new EvolvablePortablePerson2("Aleksandar Seovic", new DateTime(1974, 8, 24));
            EvolvablePortablePerson2 ana   = new EvolvablePortablePerson2("Ana Maria Seovic", new DateTime(2004, 8, 14, 7, 43, 0));

            aleks.Address      = ana.Address = new Address("208 Myrtle Ridge Rd", "Lutz", "FL", "33549");
            aleks.Nationality  = ana.Nationality = "Serbian";
            aleks.PlaceOfBirth = new Address(null, "Belgrade", "Serbia", "11000");
            ana.PlaceOfBirth   = new Address("128 Asbury Ave, #401", "Evanston", "IL", "60202");
            aleks.Children     = new EvolvablePortablePerson2[] { ana };

            Stream stream_v2 = new MemoryStream();

            ctx_v2.Serialize(new DataWriter(stream_v2), aleks);

            stream_v2.Position = 0;
            EvolvablePortablePerson aleks_v1 = (EvolvablePortablePerson)ctx_v1.Deserialize(new DataReader(stream_v2));

            EvolvablePortablePerson marija_v1 = new EvolvablePortablePerson("Marija Seovic", new DateTime(1978, 2, 20));

            marija_v1.Address  = aleks_v1.Address;
            marija_v1.Children = aleks_v1.Children;
            aleks_v1.Spouse    = marija_v1;

            Stream stream_v1 = new MemoryStream();

            ctx_v1.Serialize(new DataWriter(stream_v1), aleks_v1);

            stream_v1.Position = 0;
            EvolvablePortablePerson2 aleks_v2 = (EvolvablePortablePerson2)ctx_v2.Deserialize(new DataReader(stream_v1));

            Assert.AreEqual(aleks.Name, aleks_v2.Name);
            Assert.AreEqual(aleks.Nationality, aleks_v2.Nationality);
            Assert.AreEqual(aleks.DOB, aleks_v2.DOB);
            Assert.AreEqual(aleks_v1.Spouse.Name, aleks_v2.Spouse.Name);
            Assert.AreEqual(aleks.Address.City, aleks_v2.Address.City);
            Assert.AreEqual(aleks.PlaceOfBirth.City, aleks_v2.PlaceOfBirth.City);
        }
Exemple #5
0
        public void TestIdFromPofHelper()
        {
            SimplePofContext context = new SimplePofContext();

            context.RegisterUserType(9999, typeof(MyCollection), new MySerializer());
            Assert.AreEqual(9999, PofHelper.GetPofTypeId(MyCollection.Instance.GetType(), context));
        }
Exemple #6
0
        public void TestIdFromContext()
        {
            SimplePofContext context = new SimplePofContext();

            context.RegisterUserType(9999, typeof(MyCollection), new MySerializer());
            Assert.AreEqual(9999, context.GetUserTypeIdentifier(MyCollection.Instance.GetType()));
        }
        public void TestInheritance()
        {
            var cxt = new SimplePofContext();

            cxt.RegisterUserType(1001, typeof(PersonV1), new PofAnnotationSerializer(1001, typeof(PersonV1)));
            cxt.RegisterUserType(1005, typeof(BewilderedPerson), new PofAnnotationSerializer(1005, typeof(BewilderedPerson), true));

            var value      = new BewilderedPerson("Frank", "Spencer", 57, "dizzy");
            var teleported = EH.FromBinary(EH.ToBinary(value, cxt), cxt) as BewilderedPerson;

            Assert.IsNotNull(teleported);
            Assert.AreEqual("Frank", teleported.m_firstName);
            Assert.AreEqual("Spencer", teleported.m_lastName);
            Assert.AreEqual(57, teleported.m_age);
            Assert.AreEqual("dizzy", teleported.m_state);
        }
        public void TestEvolvable()
        {
            var cxt1 = new SimplePofContext();
            var cxt2 = new SimplePofContext();

            cxt1.RegisterUserType(1001, typeof(PersonV1), new PofAnnotationSerializer(1001, typeof(PersonV1)));
            cxt2.RegisterUserType(1001, typeof(PersonV2), new PofAnnotationSerializer(1001, typeof(PersonV2)));

            var personV1 = new PersonV1("Frank", "Spencer", 57);
            // verify we can go forward 1 => 2
            var teleportedV2 = EH.FromBinary(EH.ToBinary(personV1, cxt1), cxt2) as PersonV2;

            // verify we can go back 2 => 1
            teleportedV2.m_fMale = true;
            var teleportedV1       = EH.FromBinary(EH.ToBinary(teleportedV2, cxt2), cxt1) as PersonV1;
            var teleportedV2FromV1 = EH.FromBinary(EH.ToBinary(teleportedV1, cxt1), cxt2) as PersonV2;

            // v1 => v2
            Assert.IsNotNull(teleportedV2);
            Assert.AreEqual("Frank", teleportedV2.m_firstName);
            Assert.AreEqual("Spencer", teleportedV2.m_lastName);
            Assert.AreEqual(57, teleportedV2.m_age);
            // v2 => v1
            Assert.IsNotNull(teleportedV1);
            Assert.IsNotNull(teleportedV1.FutureData);
            Assert.AreEqual("Frank", teleportedV1.m_firstName);
            Assert.AreEqual("Spencer", teleportedV1.m_lastName);
            Assert.AreEqual(57, teleportedV1.m_age);
            Assert.IsNotNull(teleportedV2FromV1);
            Assert.AreEqual("Frank", teleportedV2FromV1.m_firstName);
            Assert.AreEqual("Spencer", teleportedV2FromV1.m_lastName);
            Assert.AreEqual(57, teleportedV2FromV1.m_age);
            Assert.IsTrue(teleportedV2FromV1.m_fMale);
        }
Exemple #9
0
        public void Setup()
        {
            V1 = new SimplePofContext();
            V1.RegisterUserType(1, typeof(Test.V1.Pet),
                                new PortableTypeSerializer(1, typeof(Test.V1.Pet)));
            V1.RegisterUserType(2, typeof(Test.V1.Dog),
                                new PortableTypeSerializer(2, typeof(Test.V1.Dog)));

            V2 = new SimplePofContext();
            V2.RegisterUserType(1, typeof(Test.V2.Pet),
                                new PortableTypeSerializer(1, typeof(Test.V2.Pet)));
            V2.RegisterUserType(2, typeof(Test.V2.Dog),
                                new PortableTypeSerializer(2, typeof(Test.V2.Dog)));
            V2.RegisterUserType(3, typeof(Test.V2.Animal),
                                new PortableTypeSerializer(3, typeof(Test.V2.Animal)));
            V2.RegisterUserType(5, typeof(Color), new EnumPofSerializer());
        }
        public void TestTypeRegistration()
        {
            SimplePofContext         ctx        = new SimplePofContext();
            PortableObjectSerializer serializer = new PortableObjectSerializer(1);

            ctx.RegisterUserType(1, this.GetType(), serializer);
            ctx.RegisterUserType(2, typeof(IPortableObject), new PortableObjectSerializer(2));

            Assert.AreEqual(1, ctx.GetUserTypeIdentifier(this));
            Assert.AreEqual(1, ctx.GetUserTypeIdentifier(this.GetType()));
            Assert.AreEqual(1, ctx.GetUserTypeIdentifier(this.GetType().AssemblyQualifiedName));

            Assert.AreEqual(this.GetType(), ctx.GetType(1));
            Assert.AreEqual(this.GetType().FullName, ctx.GetTypeName(1));
            Assert.AreEqual(serializer, ctx.GetPofSerializer(1));

            ctx.UnregisterUserType(1);
            ctx.UnregisterUserType(2);
        }
        public void TestBeginPropertyWithExcepton1()
        {
            SimplePofContext ctx = new SimplePofContext();

            ctx.RegisterUserType(1, typeof(PortablePerson), new PortableObjectSerializer(1));
            PortablePerson zoja   = new PortablePerson("Zoja", new DateTime(1982, 11, 11, 7, 15, 30));
            Stream         stream = new MemoryStream();
            IPofWriter     writer = new PofStreamWriter.UserTypeWriter(new DataWriter(stream), ctx, 1, -1);

            writer.WriteObject(-1, zoja);
        }
        public void TestSerialization()
        {
            SimplePofContext ctx = new SimplePofContext();

            ctx.RegisterUserType(1, typeof(PortablePerson), new PortableObjectSerializer(1));
            ctx.RegisterUserType(2, typeof(Address), new PortableObjectSerializer(2));

            PortablePerson aleks  = new PortablePerson("Aleksandar Seovic", new DateTime(1974, 8, 24));
            PortablePerson marija = new PortablePerson("Marija Seovic", new DateTime(1978, 2, 20));
            PortablePerson ana    = new PortablePerson("Ana Maria Seovic", new DateTime(2004, 8, 14, 7, 43, 0));

            aleks.Spouse   = marija;
            aleks.Address  = marija.Address = ana.Address = new Address("208 Myrtle Ridge Rd", "Lutz", "FL", "33549");
            aleks.Children = marija.Children = new PortablePerson[] { ana };

            Stream stream = new MemoryStream();

            ctx.Serialize(new DataWriter(stream), aleks);

            stream.Position = 0;
            PortablePerson aleks2 = (PortablePerson)ctx.Deserialize(new DataReader(stream));

            Assert.AreEqual(aleks.Name, aleks2.Name);
            Assert.AreEqual(aleks.DOB, aleks2.DOB);
            Assert.AreEqual(aleks.Spouse.Name, aleks2.Spouse.Name);
            Assert.AreEqual(aleks.Address.City, aleks2.Address.City);
            Assert.AreEqual(1, aleks2.Children.Length);
            Assert.AreEqual(ana.Name, aleks2.Children[0].Name);
            Assert.AreEqual(ana.Address.Street, aleks2.Children[0].Address.Street);

            SimplePofContext liteCtx = new SimplePofContext();

            liteCtx.RegisterUserType(1, typeof(PortablePersonLite), new PortableObjectSerializer(1));
            liteCtx.RegisterUserType(2, typeof(Address), new PortableObjectSerializer(2));

            stream.Position = 0;
            PortablePersonLite aleks3 = (PortablePersonLite)liteCtx.Deserialize(new DataReader(stream));

            Assert.AreEqual(aleks.Name, aleks3.Name);
            Assert.AreEqual(aleks.DOB, aleks3.DOB);
        }
Exemple #13
0
        public void SetUp()
        {
            SimplePofContext ctx = new SimplePofContext();

            ctx.RegisterUserType(101,
                                 typeof(DictionaryTestClass <Int32, String>),
                                 new PortableObjectSerializer(101));

            ctx.RegisterUserType(102,
                                 typeof(DictionaryTestClass <String, Int32>),
                                 new PortableObjectSerializer(102));

            ctx.RegisterUserType(103,
                                 typeof(CollectionTestClass <Int32>),
                                 new PortableObjectSerializer(103));

            ctx.RegisterUserType(104,
                                 typeof(DictionaryTestClass <int, object>),
                                 new PortableObjectSerializer(104));
            m_serializer = ctx;
        }
        public void TestSerializationWithNonPortableType()
        {
            SimplePofContext ctx = new SimplePofContext();

            ctx.RegisterUserType(1, typeof(PersonLite), new PortableObjectSerializer(1));

            PersonLite ana = new PersonLite("Ana Maria Seovic", new DateTime(2006, 8, 14));

            Stream stream = new MemoryStream();

            ctx.Serialize(new DataWriter(stream), ana);
        }
Exemple #15
0
        public void TestPofTypeID()
        {
            Assert.AreEqual(PofConstants.T_BOOLEAN, PofHelper.GetPofTypeId(true.GetType(), new SimplePofContext()));
            Assert.AreEqual(PofConstants.T_CHAR, PofHelper.GetPofTypeId('t'.GetType(), new SimplePofContext()));
            Assert.AreEqual(PofConstants.T_INT16,
                            PofHelper.GetPofTypeId(Int16.MinValue.GetType(), new SimplePofContext()));
            Assert.AreEqual(PofConstants.T_INT32, PofHelper.GetPofTypeId((-1).GetType(), new SimplePofContext()));
            Assert.AreEqual(PofConstants.T_INT64,
                            PofHelper.GetPofTypeId(Int64.MaxValue.GetType(), new SimplePofContext()));
            Assert.AreEqual(PofConstants.T_DATETIME,
                            PofHelper.GetPofTypeId(new DateTime(11, 11, 11, 11, 11, 11).GetType(),
                                                   new SimplePofContext()));
            Assert.AreEqual(PofConstants.T_CHAR_STRING, PofHelper.GetPofTypeId("test".GetType(), new SimplePofContext()));

            double[] uniformArray = new double[] { Double.MaxValue, 0, -1, Double.NegativeInfinity };
            Assert.AreEqual(PofConstants.T_UNIFORM_ARRAY,
                            PofHelper.GetPofTypeId(uniformArray.GetType(), new SimplePofContext()));

            Object[] objArray = new object[] { new DateTime(11, 11, 11), 13, Double.NaN };
            Assert.AreEqual(PofConstants.T_ARRAY, PofHelper.GetPofTypeId(objArray.GetType(), new SimplePofContext()));

            ArrayList al = new ArrayList();

            al.Add(new DateTime(11, 11, 11));
            al.Add(5.55);
            Assert.AreEqual(PofConstants.T_COLLECTION, PofHelper.GetPofTypeId(al.GetType(), new SimplePofContext()));

            Hashtable ht = new Hashtable();

            ht.Add("now", new DateTime(2006, 8, 11, 12, 49, 0));
            Assert.AreEqual(PofConstants.T_MAP, PofHelper.GetPofTypeId(ht.GetType(), new SimplePofContext()));

            ICollection ll = new LinkedList <double>();

            Assert.AreEqual(PofConstants.T_COLLECTION, PofHelper.GetPofTypeId(ll.GetType(), new SimplePofContext()));

            Binary bin = new Binary(new byte[] { 1, 2, 3 });

            Assert.AreEqual(PofConstants.T_OCTET_STRING, PofHelper.GetPofTypeId(bin.GetType(), new SimplePofContext()));

            SimplePofContext ctx = new SimplePofContext();

            Assert.AreEqual(PofConstants.T_OCTET_STRING, PofHelper.GetPofTypeId(typeof(byte[]), ctx));
            Assert.AreEqual(PofConstants.T_ARRAY, PofHelper.GetPofTypeId(typeof(object[]), ctx));
            Assert.AreEqual(PofConstants.T_ARRAY, PofHelper.GetPofTypeId(typeof(string[]), ctx));

            ctx.RegisterUserType(1000, typeof(string[]), new PortableObjectSerializer(1000));

            Assert.AreEqual(PofConstants.T_OCTET_STRING, PofHelper.GetPofTypeId(typeof(byte[]), ctx));
            Assert.AreEqual(PofConstants.T_ARRAY, PofHelper.GetPofTypeId(typeof(object[]), ctx));
            Assert.AreEqual(1000, PofHelper.GetPofTypeId(typeof(string[]), ctx));
        }
Exemple #16
0
        public void TestConfigPofContextException()
        {
            LikeFilter filter = new LikeFilter("field", "goran", '\\', false);

            SimplePofContext    ctx        = new SimplePofContext();
            BinaryPofSerializer serializer = new BinaryPofSerializer(1);

            ctx.RegisterUserType(1, filter.GetType(), serializer);

            Stream stream = new MemoryStream();

            ctx.Serialize(new DataWriter(stream), filter);
        }
        public void testCircularReferences()
        {
            var ctx = new SimplePofContext();

            ctx.RegisterUserType(101, typeof(PortablePerson),
                                 new PortableObjectSerializer(101));
            ctx.RegisterUserType(102, typeof(PortablePersonReference),
                                 new PortableObjectSerializer(102));

            var ivan = new PortablePersonReference("Ivan", new DateTime(78, 4, 25));

            ivan.Children    = new PortablePerson[1];
            ivan.Children[0] = new PortablePerson("Mary Jane", new DateTime(97, 8, 14));
            ivan.Spouse      = new PortablePerson("Eda", new DateTime(79, 6, 25));

            var goran = new PortablePersonReference("Goran", new DateTime(82, 3, 3));

            goran.Children    = new PortablePerson[2];
            goran.Children[0] = new PortablePerson("Tom", new DateTime(103, 7, 5));
            goran.Children[1] = new PortablePerson("Ellen", new DateTime(105, 3, 15));
            goran.Spouse      = new PortablePerson("Tiffany", new DateTime(82, 3, 25));
            goran.Friend      = ivan;
            ivan.Friend       = goran;

            initPOFWriter();
            m_pofWriter = new PofStreamWriter(m_writer, ctx);
            m_pofWriter.EnableReference();
            m_pofWriter.WriteObject(0, ivan);

            initPOFReader();
            m_pofReader = new PofStreamReader(m_reader, ctx);

            var ivanR = (PortablePersonReference)m_pofReader.ReadObject(0);

            Assert.IsTrue(ivanR.Name.Equals(ivan.Name));
            Assert.IsTrue(ivanR.Children.Length == 1);
            Assert.IsTrue(ivanR.Friend.Equals(goran));
        }
        public void TestDeserializationException()
        {
            SimplePofContext ctx = new SimplePofContext();

            ctx.RegisterUserType(1, typeof(PortablePerson), new PortableObjectSerializer(1));
            ctx.RegisterUserType(2, typeof(Address), new PortableObjectSerializer(2));

            PortablePerson aleks  = new PortablePerson("Aleksandar Seovic", new DateTime(1974, 8, 24));
            PortablePerson marija = new PortablePerson("Marija Seovic", new DateTime(1978, 2, 20));
            PortablePerson ana    = new PortablePerson("Ana Maria Seovic", new DateTime(2004, 8, 14, 7, 43, 0));

            aleks.Spouse   = marija;
            aleks.Address  = marija.Address = ana.Address = new Address("208 Myrtle Ridge Rd", "Lutz", "FL", "33549");
            aleks.Children = marija.Children = new PortablePerson[] { ana };

            Stream stream = new MemoryStream();

            ctx.Serialize(new DataWriter(stream), aleks);

            stream.Position = 0;
            stream.Close();
            PortablePerson aleks2 = (PortablePerson)ctx.Deserialize(new DataReader(stream));
        }
        public void TestGenerics()
        {
            SimplePofContext cxt = new SimplePofContext();

            cxt.RegisterUserType(1001, typeof(PersonV1), new PofAnnotationSerializer(1001, typeof(PersonV1)));

            PersonV1 value = new PersonV1("Frank", "Spencer", 57);

            PersonV1 teleported = EH.FromBinary(EH.ToBinary(value, cxt), cxt) as PersonV1;

            Assert.IsNotNull(teleported);
            Assert.AreEqual("Frank", teleported.m_firstName);
            Assert.AreEqual("Spencer", teleported.m_lastName);
            Assert.AreEqual(57, teleported.m_age);
        }
        public void TestSerializationWithSkippingReadingType()
        {
            SimplePofContext ctx = new SimplePofContext();

            ctx.RegisterUserType(1, typeof(SkippingPersonLite), new PortableObjectSerializer(1));

            SkippingPersonLite ana = new SkippingPersonLite("Ana Maria Seovic", new DateTime(2006, 8, 14));

            Stream stream = new MemoryStream();

            ctx.Serialize(new DataWriter(stream), ana);

            stream.Position = 0;
            SkippingPersonLite ana2 = (SkippingPersonLite)ctx.Deserialize(new DataReader(stream));
        }
        public void TestAncestry()
        {
            SimplePofContext cxt = new SimplePofContext();

            cxt.RegisterUserType(1002, typeof(GrandFather), new PofAnnotationSerializer(1002, typeof(GrandFather), true));
            cxt.RegisterUserType(1003, typeof(Father), new PofAnnotationSerializer(1003, typeof(Father), true));
            cxt.RegisterUserType(1004, typeof(Child), new PofAnnotationSerializer(1004, typeof(Child), true));

            var son = new Child("Bart", "Simpson", 10);
            var dad = new Father("Homer", "Simpson", 50, son);
            var gf  = new GrandFather("Abe", "Simpson", 100, dad);

            var teleported = (GrandFather)EH.FromBinary(EH.ToBinary(gf, cxt), cxt);

            Assert.AreEqual("Abe", teleported.m_firstName);
            Assert.AreEqual("Simpson", teleported.m_lastName);
            Assert.AreEqual(100, teleported.m_age);
            Assert.AreEqual("Homer", teleported.m_father.m_firstName);
            Assert.AreEqual("Simpson", teleported.m_father.m_lastName);
            Assert.AreEqual(50, teleported.m_father.m_age);
            Assert.AreEqual("Bart", teleported.m_father.m_child.m_firstName);
            Assert.AreEqual("Simpson", teleported.m_father.m_child.m_lastName);
            Assert.AreEqual(10, teleported.m_father.m_child.m_age);
        }
Exemple #22
0
        public void TestDotNetTypeID()
        {
            SimplePofContext ctx = new SimplePofContext();

            object o = new byte[0];

            Assert.AreEqual(PofConstants.N_BYTE_ARRAY, PofHelper.GetDotNetTypeId(o, ctx));

            o = new object[0];
            Assert.AreEqual(PofConstants.N_OBJECT_ARRAY, PofHelper.GetDotNetTypeId(o, ctx));

            o = new string[0];
            Assert.AreEqual(PofConstants.N_OBJECT_ARRAY, PofHelper.GetDotNetTypeId(o, ctx));

            ctx.RegisterUserType(1000, o.GetType(), new PortableObjectSerializer(1000));
            o = new object[0];
            Assert.AreEqual(PofConstants.N_OBJECT_ARRAY, PofHelper.GetDotNetTypeId(o, ctx));

            o = new string[0];
            Assert.AreEqual(PofConstants.N_USER_TYPE, PofHelper.GetDotNetTypeId(o, ctx));
        }
Exemple #23
0
        public void TestSynchronizedDictionarySerialization()
        {
            SynchronizedDictionary ht = new SynchronizedDictionary();

            SimplePofContext    ctx        = new SimplePofContext();
            BinaryPofSerializer serializer = new BinaryPofSerializer(1);

            ctx.RegisterUserType(1, ht.GetType(), serializer);

            Assert.AreEqual(1, ctx.GetUserTypeIdentifier(ht));
            Assert.AreEqual(1, ctx.GetUserTypeIdentifier(ht.GetType()));

            Assert.AreEqual(ht.GetType(), ctx.GetType(1));
            Assert.AreEqual(ht.GetType().FullName, ctx.GetTypeName(1));
            Assert.AreEqual(serializer, ctx.GetPofSerializer(1));

            ht.Add(1, 1);
            ht.Add(2, 2);
            ht.Add(3, 3);
            ht.Add(4, 4);
            ht.Add(5, 5);
            ht.Add(6, 6);
            ht.Add(7, 7);

            Stream stream = new MemoryStream();

            ctx.Serialize(new DataWriter(stream), ht);

            stream.Position = 0;
            SynchronizedDictionary ht2 = (SynchronizedDictionary)ctx.Deserialize(new DataReader(stream));

            Assert.AreEqual(ht[1], ht2[1]);
            Assert.AreEqual(ht[2], ht2[2]);
            Assert.AreEqual(ht[3], ht2[3]);
            Assert.AreEqual(ht[4], ht2[4]);
            Assert.AreEqual(ht[5], ht2[5]);
            Assert.AreEqual(ht[6], ht2[6]);
            Assert.AreEqual(ht[7], ht2[7]);
        }
        public void TestXmlPofSerializer()
        {
            MemoryStream     stream        = new MemoryStream();
            DataWriter       writer        = new DataWriter(stream);
            XmlPofSerializer xmlSerializer = new XmlPofSerializer(1);
            SimplePofContext context       = new SimplePofContext();

            context.RegisterUserType(1, typeof(DataSet), xmlSerializer);

            // create test DataSet
            DataSet set = CreateTestDataSet();

            // serialize DataSet
            context.Serialize(writer, set);

            // deserialize DataSet
            stream.Seek(0, SeekOrigin.Begin);
            DataReader reader   = new DataReader(stream);
            object     deserObj = context.Deserialize(reader);

            Assert.IsInstanceOf(typeof(DataSet), deserObj);
            DataSet deserDS = (DataSet)deserObj;

            // Assert tables
            Assert.AreEqual(set.Tables.Count, deserDS.Tables.Count);
            for (int i = 0; i < set.Tables.Count; i++)
            {
                DataTable table      = set.Tables[i];
                DataTable deserTable = deserDS.Tables[i];
                Assert.AreEqual(table.TableName, deserTable.TableName);
                Assert.AreEqual(table.Columns.Count, deserTable.Columns.Count);

                for (int j = 0, c = table.Columns.Count; j < c; j++)
                {
                    DataColumn column1 = table.Columns[j];
                    DataColumn column2 = deserTable.Columns[j];
                    Assert.AreEqual(column1.ColumnName, column2.ColumnName);
                    Assert.AreEqual(column1.DataType, column2.DataType);
                    Assert.AreEqual(column1.Unique, column2.Unique);
                }

                Assert.AreEqual(table.Rows.Count, deserTable.Rows.Count);
                for (int k = 0, r = table.Rows.Count; k < r; k++)
                {
                    DataRow row1 = table.Rows[k];
                    DataRow row2 = deserTable.Rows[k];
                    for (int m = 0, c = table.Columns.Count; m < c; m++)
                    {
                        Assert.AreEqual(row1[m], row2[m]);
                    }
                }
            }

            // Assert relations
            Assert.AreEqual(set.Relations.Count, deserDS.Relations.Count);
            for (int i = 0; i < set.Relations.Count; i++)
            {
                DataColumn[] parentColumns1 = set.Relations[i].ParentColumns;
                DataColumn[] parentColumns2 = deserDS.Relations[i].ParentColumns;
                Assert.AreEqual(parentColumns1.Length, parentColumns2.Length);
                for (int j = 0; j < parentColumns1.Length; j++)
                {
                    Assert.AreEqual(parentColumns1[j].ColumnName, parentColumns2[j].ColumnName);
                }

                DataColumn[] childColumns1 = set.Relations[i].ChildColumns;
                DataColumn[] childColumns2 = deserDS.Relations[i].ChildColumns;
                Assert.AreEqual(childColumns1.Length, childColumns2.Length);
                for (int j = 0; j < childColumns1.Length; j++)
                {
                    Assert.AreEqual(childColumns1[j].ColumnName, childColumns2[j].ColumnName);
                }
            }
        }
Exemple #25
0
        public void TestWriteGenericDictionary()
        {
            var ctx = new SimplePofContext();

            ctx.RegisterUserType(101, typeof(PortablePersonLite), new PortableObjectSerializer(101));
            ctx.RegisterUserType(102, typeof(PortablePerson), new PortableObjectSerializer(102));
            ctx.RegisterUserType(103, typeof(EvolvablePortablePerson), new PortableObjectSerializer(103));

            //initPOFWriter();
            stream    = new MemoryStream();
            writer    = new DataWriter(stream);
            pofwriter = new PofStreamWriter(writer, ctx);


            IDictionary <string, double> dict = new Dictionary <string, double>();

            dict.Add("A", 11.11); dict.Add("Z", 88.88); dict.Add("7", 100.1);
            IDictionary <string, string> dict2 = new Dictionary <string, string>();

            dict2.Add("ABC", "value"); dict2.Add("N", null);

            IDictionary <string, IPortableObject> persons = new Dictionary <string, IPortableObject>();
            var ivan = new PortablePerson("Ivan", new DateTime(1978, 4, 25));

            ivan.Children = null;
            var goran = new PortablePerson("Goran", new DateTime(1982, 3, 3));

            goran.Children = null;
            var aleks = new EvolvablePortablePerson("Aleks", new DateTime(1974, 8, 24));

            aleks.Children    = new EvolvablePortablePerson[1];
            aleks.Children[0] = new EvolvablePortablePerson("Ana Maria", new DateTime(2004, 8, 14));
            aleks.DataVersion = 2;

            persons.Add("key1", ivan);
            persons.Add("key2", aleks);
            persons.Add("key3", goran);

            pofwriter.WriteDictionary(0, dict);
            pofwriter.WriteDictionary(0, dict2);
            pofwriter.WriteDictionary(0, persons);
            pofwriter.WriteDictionary(0, persons);

            //initPOFReader();
            stream.Position = 0;
            reader          = new DataReader(stream);
            pofreader       = new PofStreamReader(reader, ctx);

            IDictionary <string, double> result = new Dictionary <string, double>();

            pofreader.ReadDictionary(0, result);
            Assert.AreEqual(3, result.Count);
            foreach (string key in dict.Keys)
            {
                Assert.AreEqual(dict[key], result[key]);
            }

            IDictionary <string, string> result2 = new Dictionary <string, string>();

            pofreader.ReadDictionary(0, result2);
            Assert.AreEqual(2, result2.Count);
            foreach (string key in dict.Keys)
            {
                Assert.AreEqual(dict[key], result[key]);
            }

            IDictionary <string, IPortableObject> result3 = new Dictionary <string, IPortableObject>();

            pofreader.ReadDictionary(0, result3);
            Assert.AreEqual(3, result3.Count);
            Assert.IsFalse(result3["key1"] is EvolvablePortablePerson);
            Assert.IsTrue(result3["key2"] is EvolvablePortablePerson);
            Assert.IsFalse(result3["key3"] is EvolvablePortablePerson);
            EvolvablePortablePerson epp = (EvolvablePortablePerson)result3["key2"];

            Assert.AreEqual(aleks.Name, epp.Name);
            Assert.AreEqual(aleks.Children[0].Name, epp.Children[0].Name);

            var pp = (PortablePerson)result3["key3"];

            Assert.AreEqual(goran.Name, pp.Name);
            Assert.IsNull(pp.Children);

            IDictionary <string, IPortableObject> result4 = pofreader.ReadDictionary <string, IPortableObject>(0, null);

            Assert.AreEqual(3, result4.Count);
            Assert.IsFalse(result4["key1"] is EvolvablePortablePerson);
            Assert.IsTrue(result4["key2"] is EvolvablePortablePerson);
            Assert.IsFalse(result4["key3"] is EvolvablePortablePerson);
            epp = (EvolvablePortablePerson)result4["key2"];
            Assert.AreEqual(aleks.Name, epp.Name);
            Assert.AreEqual(aleks.Children[0].Name, epp.Children[0].Name);

            pp = (PortablePerson)result4["key3"];
            Assert.AreEqual(goran.Name, pp.Name);
            Assert.IsNull(pp.Children);
        }
        public void TestReferencesInUniformArray()
        {
            var localCtx = new SimplePofContext();

            localCtx.RegisterUserType(101, typeof(PortablePerson),
                                      new PortableObjectSerializer(101));
            localCtx.RegisterUserType(102, typeof(PortablePersonReference),
                                      new PortableObjectSerializer(102));
            localCtx.RegisterUserType(201, typeof(CompositeKey),
                                      new PortableObjectSerializer(201));
            localCtx.IsReferenceEnabled = true;

            var ivan  = new PortablePersonReference("Ivan", new DateTime(78, 4, 25));
            var goran = new PortablePersonReference("Goran", new DateTime(82, 3, 3));

            ivan.Children     = null;
            goran.Children    = new PortablePerson[2];
            goran.Children[0] = new PortablePerson("Tom", new DateTime(103, 7, 5));
            goran.Children[1] = new PortablePerson("Ellen", new DateTime(105, 3, 15));
            ivan.Siblings     = new PortablePersonReference[1];
            ivan.Siblings[0]  = goran;
            goran.Siblings    = new PortablePersonReference[1];
            goran.Siblings[0] = ivan;

            IDictionary  mapPerson = new Dictionary <CompositeKey, IPortableObject>();
            const String lastName  = "Smith";
            CompositeKey key1      = new CompositeKey(lastName, "ivan"),
                         key2      = new CompositeKey(lastName, "goran");

            mapPerson.Add(key1, ivan);
            mapPerson.Add(key2, goran);

            initPOFWriter();
            m_pofWriter = new PofStreamWriter(m_writer, localCtx);
            if (localCtx.IsReferenceEnabled)
            {
                m_pofWriter.EnableReference();
            }
            m_pofWriter.WriteDictionary(0, mapPerson);

            initPOFReader();
            m_pofReader = new PofStreamReader(m_reader, localCtx);

            IDictionary mapResult = m_pofReader.ReadDictionary(0, null);

            Assert.IsTrue(2 == mapResult.Count);

            var         ivanR  = (PortablePersonReference)mapResult[key1];
            var         goranR = (PortablePersonReference)mapResult[key2];
            ICollection keySet = mapResult.Keys;
            IEnumerator iter   = keySet.GetEnumerator();

            iter.MoveNext();
            var key1R = (CompositeKey)iter.Current;

            iter.MoveNext();
            var key2R = (CompositeKey)iter.Current;

            Assert.IsFalse(key1R.PrimaryKey == key2R.PrimaryKey);
            Assert.IsTrue(ivanR.Siblings[0] == goranR);
            Assert.IsTrue(goran.Name.Equals(goranR.Name));
            Assert.IsNull(ivanR.Children);
        }
        public void TestReferencesInArray()
        {
            var ctx = new SimplePofContext();

            ctx.RegisterUserType(101, typeof(PortablePerson),
                                 new PortableObjectSerializer(101));
            ctx.RegisterUserType(102, typeof(PortablePersonReference),
                                 new PortableObjectSerializer(102));

            var ivan     = new PortablePersonReference("Ivan", new DateTime(78, 4, 25));
            var goran    = new PortablePersonReference("Goran", new DateTime(82, 3, 3));
            var jack     = new PortablePersonReference("Jack", new DateTime(80, 5, 25));
            var jim      = new PortablePersonReference("Jim", new DateTime(80, 5, 25));
            var siblings = new PortablePersonReference[2];

            siblings[0] = jack;
            siblings[1] = jim;

            ivan.Children     = null;
            jack.Children     = null;
            jim.Children      = null;
            goran.Children    = new PortablePerson[2];
            goran.Children[0] = new PortablePerson("Tom", new DateTime(103, 7, 5));
            goran.Children[1] = new PortablePerson("Ellen", new DateTime(105, 3, 15));
            ivan.Siblings     = siblings;
            goran.Siblings    = siblings;
            Assert.IsTrue(ivan.Siblings == goran.Siblings);

            var col1 = new Collection <IPortableObject>();

            col1.Add(ivan);
            col1.Add(goran);

            initPOFWriter();
            m_pofWriter = new PofStreamWriter(m_writer, ctx);
            m_pofWriter.EnableReference();
            m_pofWriter.WriteCollection(0, (ICollection)col1);
            m_pofWriter.WriteCollection(0, col1, typeof(PortablePersonReference));

            initPOFReader();
            m_pofReader = new PofStreamReader(m_reader, ctx);

            var result  = m_pofReader.ReadCollection(0, null);
            var result2 = m_pofReader.ReadCollection(0, null);

            Assert.IsTrue(2 == result.Count);

            IEnumerator iter = result.GetEnumerator();

            iter.MoveNext();
            var ivanR = (PortablePersonReference)iter.Current;

            iter.MoveNext();
            var goranR = (PortablePersonReference)iter.Current;

            Assert.IsFalse(ivanR.Siblings == goranR.Siblings);
            Assert.IsTrue(ivanR.Siblings[0] == goranR.Siblings[0]);
            Assert.IsTrue(ivanR.Siblings[1] == goranR.Siblings[1]);
            Assert.IsNull(ivanR.Children);

            iter = result2.GetEnumerator();
            iter.MoveNext();
            var ivanR2 = (PortablePersonReference)iter.Current;

            iter.MoveNext();
            var goranR2 = (PortablePersonReference)iter.Current;

            Assert.IsFalse(ivanR2.Siblings == goranR2.Siblings);
            Assert.IsTrue(ivanR2.Siblings[0] == goranR2.Siblings[0]);
            Assert.IsTrue(ivanR2.Siblings[1] == goranR2.Siblings[1]);
            Assert.IsNull(ivanR2.Children);
        }
        public void TestEvolvableObjectSerialization()
        {
            var ctxV1 = new SimplePofContext();

            ctxV1.RegisterUserType(1, typeof(EvolvablePortablePerson),
                                   new PortableObjectSerializer(1));
            ctxV1.RegisterUserType(2, typeof(Address),
                                   new PortableObjectSerializer(2));
            ctxV1.IsReferenceEnabled = true;

            var ctxV2 = new SimplePofContext();

            ctxV2.RegisterUserType(1, typeof(EvolvablePortablePerson2),
                                   new PortableObjectSerializer(1));
            ctxV2.RegisterUserType(2, typeof(Address),
                                   new PortableObjectSerializer(2));
            ctxV2.IsReferenceEnabled = true;

            var person12 = new EvolvablePortablePerson2(
                "Aleksandar Seovic", new DateTime(74, 7, 24));
            var person22 = new EvolvablePortablePerson2(
                "Ana Maria Seovic", new DateTime(104, 7, 14, 7, 43, 0));
            var person32 = new EvolvablePortablePerson2(
                "Art Seovic", new DateTime(107, 8, 12, 5, 20, 0));

            var addr    = new Address("208 Myrtle Ridge Rd", "Lutz", "FL", "33549");
            var addrPOB = new Address("128 Asbury Ave, #401", "Evanston", "IL", "60202");

            person12.Address = addr;
            person22.Address = addr;

            person12.Nationality  = person22.Nationality = "Serbian";
            person12.PlaceOfBirth = new Address(null, "Belgrade", "Serbia", "11000");
            person22.PlaceOfBirth = addrPOB;
            person32.PlaceOfBirth = addrPOB;
            person12.Children     = new EvolvablePortablePerson2[] { person22, person32 };

            initPOFWriter();
            m_pofWriter = new PofStreamWriter(m_writer, ctxV2);
            m_pofWriter.EnableReference();
            m_pofWriter.WriteObject(0, person12);

            initPOFReader();
            m_pofReader = new PofStreamReader(m_reader, ctxV1);
            var person11 = (EvolvablePortablePerson)m_pofReader.ReadObject(0);
            var person21 = new EvolvablePortablePerson(
                "Marija Seovic", new DateTime(78, 1, 20));

            person21.Address  = person11.Address;
            person21.Children = person11.Children;
            person11.Spouse   = person21;

            initPOFWriter();
            m_pofWriter = new PofStreamWriter(m_writer, ctxV1);
            m_pofWriter.EnableReference();
            m_pofWriter.WriteObject(0, person11);

            initPOFReader();
            m_pofReader = new PofStreamReader(m_reader, ctxV2);
            var person = (EvolvablePortablePerson2)m_pofReader.ReadObject(0);

            Assert.IsTrue(person12.Name.Equals(person.Name));
            Assert.IsTrue(person12.Nationality.Equals(person.Nationality));
            Assert.IsTrue(person12.DOB.Equals(person.DOB));
            Assert.IsTrue(person11.Spouse.Name.Equals(person.Spouse.Name));
            Assert.IsTrue(person12.Address.Equals(person.Address));
            Assert.IsTrue(person12.PlaceOfBirth.Equals(person.PlaceOfBirth));
            Assert.IsTrue(person.Address != person.Children[0].Address);
            Assert.IsTrue(person.Address != person.Spouse.Address);
            Assert.IsTrue(person.Children[0] != person.Spouse.Children[0]);
            Assert.IsTrue(person.Children[1] != person.Spouse.Children[1]);
        }
        public void TestTypeRegistrationWithNullType()
        {
            SimplePofContext ctx = new SimplePofContext();

            ctx.RegisterUserType(1, null, null);
        }
        public void TestTypeRegistrationWithNullSerializer()
        {
            SimplePofContext ctx = new SimplePofContext();

            ctx.RegisterUserType(1, this.GetType(), null);
        }