Beispiel #1
0
        public void TestSkipValue()
        {
            //PASSED
            initPOFWriter();

            pofwriter.WriteDate(0, new DateTime(2006, 8, 10, 12, 59, 11));
            pofwriter.WriteDateTime(0, new DateTime(2006, 8, 10, 12, 59, 11));
            pofwriter.WriteUniversalDateTime(0, new DateTime(2006, 8, 10, 12, 59, 11, 1));
            pofwriter.WriteCharArray(0, new char[] { 'g', 't', 's' });
            pofwriter.WriteArray(0, new object[] { 'g', "Gor", 55 });
            pofwriter.WriteArray(0, new object[] { new int[] { 1, 2 }, new int[] { 3, 2, 4 } });

            Hashtable ht = new Hashtable();

            ht.Add(1, "t"); ht.Add(2, "g");
            pofwriter.WriteObject(0, ht);

            pofwriter.WriteByte(0, 0x00F0);

            pofwriter.WriteInt32(0, 300);

            initPOFReader();
            PofHelper.SkipValue(reader);
            PofHelper.SkipValue(reader);
            PofHelper.SkipValue(reader);
            PofHelper.SkipValue(reader);
            PofHelper.SkipValue(reader);
            PofHelper.SkipValue(reader);
            PofHelper.SkipValue(reader);
            PofHelper.SkipValue(reader);
            Assert.AreEqual(PofConstants.T_INT32, PofHelper.ReadAsInt32(reader, PofConstants.T_INT32));
            Assert.AreEqual(300, PofHelper.ReadAsInt32(reader, PofConstants.T_INT32));
        }
        /// <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 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));
        }
        /// <summary>
        /// Return this value's serialized form.
        /// </summary>
        /// <returns>
        /// This value's serialized form.
        /// </returns>
        public virtual Binary GetSerializedValue()
        {
            if (IsDirty)
            {
                var buf    = new BinaryMemoryStream(Size);
                var writer = new PofStreamWriter(new DataWriter(buf), PofContext);
                writer.WriteObject(0, m_oValue);
                buf.Position = 0;

                if (IsUniformEncoded)
                {
                    var reader = new DataReader(buf);

                    // skip type id
                    reader.ReadPackedInt32();
                    var of = (int)buf.Position;
                    return(new Binary(buf.GetInternalByteArray(), of,
                                      (int)(buf.Length - of)));
                }
                return(buf.ToBinary());
            }
            return(m_binValue);
        }
        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]);
        }
Beispiel #7
0
        public void TestGetDotNetTypeId()
        {
            MemoryStream    stream    = new MemoryStream();
            DataWriter      writer    = new DataWriter(stream);
            PofStreamWriter pofWriter = new PofStreamWriter(writer, new SimplePofContext());

            Int32[][] multiarray = new Int32[][] { new int[] { 1, 2, 3, 4, 5, 6 },
                                                   new int[] { 100, 101, 102 } };

            object[][][] objarray = new object[][][]
            {
                new object[][]
                {
                    new object[] { 1, 2, 3 }, new object[] { "one", "two", "three" }
                },
                new object[][]
                {
                    new object[] { 11.11, 22.22 }, new object[] { true, false, DateTime.UtcNow }
                }
            };

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

            pofWriter.WriteObject(0, multiarray);
            pofWriter.WriteObject(0, objarray);
            pofWriter.WriteObject(0, bin);

            stream.Position = 0;
            DataReader      reader    = new DataReader(stream);
            PofStreamReader pofReader = new PofStreamReader(reader, new SimplePofContext());

            object[] result = (object[])pofReader.ReadObject(0);

            Assert.IsInstanceOf(typeof(int[]), result[0]);
            Assert.IsInstanceOf(typeof(int[]), result[1]);

            int[][] arrayresult = new int[][] { (int[])result[0], (int[])result[1] };

            Assert.AreEqual(multiarray[0].Length, arrayresult[0].Length);
            Assert.AreEqual(multiarray[1].Length, arrayresult[1].Length);

            for (int i = 0; i < multiarray.Length; i++)
            {
                for (int j = 0; j < multiarray[i].Length; j++)
                {
                    Assert.AreEqual(multiarray[i][j], arrayresult[i][j]);
                }
            }

            result = (object[])pofReader.ReadObject(0);
            Assert.IsInstanceOf(typeof(object[]), result[0]);
            Assert.IsInstanceOf(typeof(object[]), result[1]);
            object[] result0 = (object[])result[0];
            object[] result1 = (object[])result[1];
            Assert.IsInstanceOf(typeof(object[]), result0[0]);
            Assert.IsInstanceOf(typeof(object[]), result1[0]);
            Assert.IsInstanceOf(typeof(object[]), result0[1]);
            Assert.IsInstanceOf(typeof(object[]), result1[1]);

            object o = pofReader.ReadObject(0);

            Assert.IsInstanceOf(typeof(Binary), o);
        }