/// <summary>
        /// Serialize an object to a stream by writing its state using the
        /// specified <see cref="DataWriter"/> object.
        /// </summary>
        /// <param name="writer">
        /// The <b>DataWriter</b> with which to write the object's state.
        /// </param>
        /// <param name="o">
        /// The object to serialize.
        /// </param>
        /// <exception cref="IOException">
        /// If an I/O error occurs.
        /// </exception>
        public virtual void Serialize(DataWriter writer, object o)
        {
            PofStreamWriter pofWriter = new PofStreamWriter(writer, this);

            // COH-5065: due to the complexity of maintaining references
            // in future data, we won't support them for IEvolvable objects
            if (IsReferenceEnabled && !(o is IEvolvable))
            {
                pofWriter.EnableReference();
            }

            try
            {
                pofWriter.WriteObject(-1, o);
            }
            catch (ArgumentException e)
            {
                // Guarantee that exceptions from called methods are IOException
                throw new IOException(e.Message, e);
            }
            catch (NotSupportedException e)
            {
                // Guarantee that exceptions from called methods are IOException
                throw new IOException(e.Message, e);
            }
        }
        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 testSerialization()
        {
            String sPath = "config/reference-pof-config.xml";
            var    ctx   = new ConfigurablePofContext(sPath);
            var    bal   = new Balance();
            var    p     = new Product(bal);
            var    c     = new Customer("Customer", p, bal);

            bal.setBalance(2.0);
            bal.setCustomer(c);

            initPOFWriter();
            m_pofwriter = new PofStreamWriter(m_writer, ctx);
            m_pofwriter.EnableReference();
            m_pofwriter.WriteObject(0, c);

            initPOFReader();
            m_pofreader = new PofStreamReader(m_reader, ctx);
            var cResult = (Customer)m_pofreader.ReadObject(0);

            Assert.IsTrue(cResult.getProduct().getBalance() == cResult.getBalance());
        }
        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 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 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 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);
        }