Esempio n. 1
0
        protected static PortablePerson CreatePerson()
        {
            PortablePerson person = new PortablePerson(GenerateName(), DateTime.Now);

            person.Address = new Address(GenerateStreet(), GenerateCity(), GenerateCountry(), GenerateZip());
            return(person);
        }
        /// <summary>
        /// Creates a populated instance of a TestValue class to be used in tests.
        /// </summary>
        /// <param name="isRefEnabled">
        /// Flag to indicate if object identity/reference is enabled.
        /// </param>
        /// <returns>
        /// A populated instance of a TestValue class to be used in tests.
        /// </returns>
        public static TestValue CreateTestValue(bool isRefEnabled)
        {
            PortablePerson person = isRefEnabled ? CreatePersonNoChildren() : CreatePerson();

            Object[] aObj = new Object[] { 1, "two", person };
            String[] aStr = new String[] { "one", "two", "three", "four" };

            IList lstObj = new ArrayList();

            lstObj.Add(1);
            lstObj.Add("two");
            lstObj.Add(person);

            IList lstStr = new ArrayList();

            lstStr.Add("one");
            lstStr.Add("two");
            lstStr.Add("three");
            lstStr.Add("four");

            ILongArray oSparseArray = new LongSortedList();

            oSparseArray[2] = "two";
            oSparseArray[4] = 4;
            oSparseArray[5] = person;

            ILongArray oUniformSparseArray = new LongSortedList();

            oUniformSparseArray[2] = "two";
            oUniformSparseArray[4] = "four";

            return(new TestValue(aObj, aStr,
                                 lstObj, lstStr,
                                 oSparseArray, oUniformSparseArray));
        }
        public void TestPofUpdater()
        {
            INamedCache cache = CacheFactory.GetCache(CacheName);

            cache.Clear();

            PortablePerson original = new PortablePerson();

            original.Name    = "Aleksandar Seovic";
            original.Address = new Address("street", "Belgrade", "SRB", "11000");

            cache.Insert("p1", original);

            PofUpdater updName = new PofUpdater(0);
            PofUpdater updCity = new PofUpdater(new SimplePofPath(new int[] { 1, 1 }));

            cache.Invoke("p1", new UpdaterProcessor(updName, "Novak Seovic"));
            cache.Invoke("p1", new UpdaterProcessor(updCity, "Lutz"));

            PortablePerson modified = (PortablePerson)cache["p1"];

            Assert.AreEqual("Novak Seovic", modified.Name);
            Assert.AreEqual("Lutz", modified.Address.City);

            CacheFactory.Shutdown();
        }
        public void TestNestedPofValueAccessor()
        {
            PortablePerson person = CreatePerson();

            // perform the test twice, once with references disable, once with them enabled
            for (bool isRefEnabled = false; ;)
            {
                Binary binPerson = Serialize(person, isRefEnabled);

                IPofValue pv = PofValueParser.Parse(binPerson, GetPofContext(isRefEnabled));
                Assert.AreEqual(person.Address.Street, pv.GetChild(1).GetChild(0).GetValue());
                Assert.AreEqual(person.Address.City, pv.GetChild(1).GetChild(1).GetValue());
                Assert.AreEqual(person.Address.State, pv.GetChild(1).GetChild(2).GetValue());
                Assert.AreEqual(person.Address.ZIP, pv.GetChild(1).GetChild(3).GetValue());

                if (isRefEnabled)
                {
                    // test the case where we try to access an object that contains a
                    // uniform collection of user defined objects when object reference is enabled
                    try
                    {
                        pv.GetChild(4);
                        Assert.Fail("Should've thrown NotSupportedException");
                    }
                    catch (NotSupportedException)
                    {
                    }
                    break;
                }
                isRefEnabled = true;
            }
        }
        public void TestPofValueAccessor()
        {
            // perform the test twice, once with references disable, once with them enabled
            for (bool isRefEnabled = false; ;)
            {
                PortablePerson person    = isRefEnabled ? CreatePersonNoChildren() : CreatePerson();
                Binary         binPerson = Serialize(person, isRefEnabled);

                IPofValue pv = PofValueParser.Parse(binPerson, GetPofContext(isRefEnabled));
                Assert.AreEqual(person.Address, pv.GetChild(1).GetValue());
                Assert.AreEqual(person.Name, pv.GetChild(0).GetValue());
                Assert.AreEqual(person.DOB, pv.GetChild(2).GetValue());

                // test NilPofValue
                IPofValue nv = pv.GetChild(100);
                Assert.IsTrue(nv is PofSparseArray.NilPofValue);
                Assert.IsNull(nv.GetValue());

                // test PofNavigationException
                try
                {
                    pv.GetChild(0).GetChild(0);
                    Assert.Fail("Should've thrown PofNavigationException");
                }
                catch (PofNavigationException)
                {
                }

                if (isRefEnabled)
                {
                    break;
                }
                isRefEnabled = true;
            }
        }
Esempio n. 6
0
        public void testSerialization()
        {
            var ctx    = new SafeConfigurablePofContext();
            var uuid   = new UUID();
            var buffer = new MemoryStream(1024);

            ctx.Serialize(new DataWriter(buffer), uuid);

            buffer.Position = 0;
            Object o = ctx.Deserialize(new DataReader(buffer));

            Assert.AreEqual(o, uuid);

            var person = new PortablePerson("Aleksandar Seovic",
                                            new DateTime(74, 7, 24));

            person.Address  = new Address("208 Myrtle Ridge Rd", "Lutz", "FL", "33549");
            person.Children = new Person[]
            {
                new PortablePerson("Aleksandar Seovic JR.", new DateTime(174, 1, 1))
            };

            buffer.Position = 0;
            ctx.Serialize(new DataWriter(buffer), person);

            buffer.Position = 0;
            o = ctx.Deserialize(new DataReader(buffer));
            Assert.AreEqual(o, person);
        }
        public void PofCircularReference()
        {
            CacheFactory.DefaultPofConfigPath = "//Coherence.Tests/Tangosol.Resources/s4hc-test-reference-config.xml";
            CacheFactory.DefaultPofConfig     = XmlHelper.LoadResource(ResourceLoader.GetResource(
                                                                           "assembly://Coherence.Tests/Tangosol.Resources/s4hc-test-reference-config.xml"), "POF configuration");
            IConfigurableCacheFactory ccf = CacheFactory.ConfigurableCacheFactory;

            ccf.Config = XmlHelper.LoadXml(
                "assembly://Coherence.Tests/Tangosol.Resources/s4hc-cache-config-reference.xml");
            ICacheService service = (ICacheService)ccf.EnsureService("ExtendTcpCacheService");
            INamedCache   cache   = service.EnsureCache("dist-extend-reference");

            var joe  = new PortablePerson("Joe Smith", new DateTime(78, 4, 25));
            var jane = new PortablePerson("Jane Smith", new DateTime(80, 5, 22));

            joe.Spouse  = jane;
            jane.Spouse = joe;

            cache.Add("Key1", joe);
            cache.Invoke("Key1", new ConditionalPut(new AlwaysFilter(), joe, false));

            CacheFactory.DefaultPofConfigPath = "//Coherence.Tests/Tangosol.Resources/s4hc-test-config.xml";
            CacheFactory.DefaultPofConfig     = XmlHelper.LoadResource(ResourceLoader.GetResource(
                                                                           "assembly://Coherence.Tests/Tangosol.Resources/s4hc-test-config.xml"), "POF configuration");
            CacheFactory.Shutdown();
        }
        /// <summary>
        /// Creates a populated instance of a PortablePerson class with no
        /// children to be used in tests.
        /// </summary>
        /// <returns>
        /// A populated instance of a PortablePerson class to be used in tests.
        /// </returns>
        public static PortablePerson CreatePersonNoChildren()
        {
            PortablePerson p = new PortablePerson("Aleksandar Seovic", new DateTime(1974, 8, 24));

            p.Address = new Address("123 Main St", "Tampa", "FL", "12345");
            p.Spouse  = new PortablePerson("Marija Seovic", new DateTime(1978, 2, 20));
            return(p);
        }
        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 TestReferencesWithComplexObject()
        {
            var ivan  = new PortablePersonReference("Ivan", new DateTime(78, 4, 25));
            var goran = new PortablePersonReference("Goran", new DateTime(82, 3, 3));
            var anna  = new PortablePersonReference("Anna", new DateTime(80, 4, 12));
            var tom   = new PortablePerson("Tom", new DateTime(103, 7, 5));
            var ellen = new PortablePerson("Ellen", new DateTime(105, 3, 15));

            ivan.Children     = null;
            goran.Children    = new PortablePerson[2];
            goran.Children[0] = tom;
            goran.Children[1] = ellen;
            anna.Children     = new PortablePerson[2];
            anna.Children[0]  = tom;
            anna.Children[1]  = ellen;
            ivan.Siblings     = new PortablePersonReference[1];
            ivan.Siblings[0]  = goran;
            goran.Siblings    = new PortablePersonReference[1];
            goran.Siblings[0] = ivan;
            goran.Spouse      = anna;
            anna.Spouse       = goran;

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

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

            Binary      bin       = Serialize(mapPerson, true);
            IPofValue   pv        = PofValueParser.Parse(bin, GetPofContext(true));
            IDictionary mapResult = pv.GetDictionary(null);

            Assert.AreEqual(2, mapResult.Count);
            PortablePersonReference ivanR  = (PortablePersonReference)mapResult[key1];
            PortablePersonReference goranR = (PortablePersonReference)mapResult[key2];

            Assert.AreEqual(goran.Name, goranR.Name);
            Assert.IsTrue(ivanR.Siblings[0] == goranR);
            Assert.IsTrue(goranR.Spouse.Children[0] == goranR.Children[0]);

            bin = Serialize(ivan, true);
            pv  = PofValueParser.Parse(bin, GetPofContext(true));

            ivanR  = (PortablePersonReference)pv.Root.GetValue();
            goranR = ivanR.Siblings[0];
            Assert.IsTrue(goranR.Siblings[0] == ivanR);

            ivanR  = (PortablePersonReference)pv.GetValue(PofConstants.T_UNKNOWN);
            goranR = ivanR.Siblings[0];
            Assert.IsTrue(goranR.Siblings[0] == ivanR);
        }
        /// <summary>
        /// Creates a populated instance of a PortablePerson class to be used
        /// in tests.
        /// </summary>
        /// <returns>
        /// A populated instance of a PortablePerson class to be used in tests.
        /// </returns>
        public static PortablePerson CreatePerson()
        {
            PortablePerson p = new PortablePerson("Aleksandar Seovic", new DateTime(1974, 8, 24));

            p.Address  = new Address("123 Main St", "Tampa", "FL", "12345");
            p.Spouse   = new PortablePerson("Marija Seovic", new DateTime(1978, 2, 20));
            p.Children = new PortablePerson[] {
                new PortablePerson("Ana Maria Seovic", new DateTime(2004, 8, 14)),
                new PortablePerson("Novak Seovic", new DateTime(2007, 12, 28))
            };
            return(p);
        }
Esempio n. 12
0
        protected virtual object CreatePopulatedObjectInstance()
        {
            PortablePerson person = new PortablePerson("Aleksandar Seovic", new DateTime(1974, 8, 24));

            person.Address  = new Address("555 Main St", "Tampa", "FL", "33555");
            person.Spouse   = new PortablePerson("Marija Seovic", new DateTime(1978, 2, 20));
            person.Children = new Person[]
            {
                new PortablePerson("Ana Maria Seovic", new DateTime(2004, 8, 14)),
                new PortablePerson("Novak Seovic", new DateTime(2007, 12, 28))
            };
            return(person);
        }
        public void TestPofValueMutator()
        {
            PortablePerson person    = CreatePerson();
            Binary         binPerson = Serialize(person);

            IPofValue pv = PofValueParser.Parse(binPerson, GetPofContext());

            pv.GetChild(0).SetValue("Seovic Aleksandar");
            Assert.AreEqual(pv.GetChild(0).GetValue(), "Seovic Aleksandar");

            pv.GetChild(0).SetValue("Marija Seovic");
            pv.GetChild(1).GetChild(0).SetValue("456 Main St");
            pv.GetChild(1).GetChild(1).SetValue("Lutz");
            pv.GetChild(1).GetChild(3).SetValue("33549");
            pv.GetChild(2).SetValue(new DateTime(1978, 2, 20));
            pv.GetChild(3).SetValue(new PortablePerson("Aleksandar Seovic", new DateTime(1974, 8, 24)));
            pv.GetChild(4).SetValue(person.Children);
            binPerson = pv.ApplyChanges();

            PortablePerson p2 = (PortablePerson)Deserialize(binPerson);

            Assert.AreEqual("Marija Seovic", p2.Name);
            Assert.AreEqual("456 Main St", p2.Address.Street);
            Assert.AreEqual("Lutz", p2.Address.City);
            Assert.AreEqual("33549", p2.Address.ZIP);
            Assert.AreEqual(new DateTime(1978, 2, 20), p2.DOB);
            Assert.AreEqual("Aleksandar Seovic", p2.Spouse.Name);
            Assert.AreEqual(person.Children, p2.Children);

            pv = PofValueParser.Parse(binPerson, GetPofContext());
            pv.GetChild(0).SetValue("Ana Maria Seovic");
            pv.GetChild(2).SetValue(new DateTime(2004, 8, 14));
            pv.GetChild(3).SetValue(null);
            pv.GetChild(4).SetValue(null);
            binPerson = pv.ApplyChanges();

            PortablePerson p3 = (PortablePerson)Deserialize(binPerson);

            Assert.AreEqual("Ana Maria Seovic", p3.Name);
            Assert.AreEqual(p2.Address, p3.Address);
            Assert.AreEqual(new DateTime(2004, 8, 14), p3.DOB);
            Assert.IsNull(p3.Spouse);
            Assert.IsNull(p3.Children);
        }
        public void TestSessionModification()
        {
            bool                locked;
            TimeSpan            lockAge;
            object              lockId;
            SessionStateActions actions;

            SessionStateStoreData data =
                m_store.GetItemExclusive(null, SESSION_ID, out locked, out lockAge, out lockId, out actions);

            Assert.IsTrue(locked);
            Assert.IsTrue(lockAge >= TimeSpan.Zero);
            Assert.IsNotNull(lockId);
            Assert.AreEqual(SessionStateActions.None, actions);

            ISessionStateItemCollection session = data.Items;

            AssertSessionDefaults(session);

            session["int"]    = 2;
            session["string"] = "modified string";
            session["date"]   = DateTime.Now;
            session["bool"]   = true;
            session["blob"]   = CreateBlob(1024);
            session["person"] = new PortablePerson("Novak Seovic", new DateTime(2007, 12, 28));

            m_store.SetAndReleaseItemExclusive(null, SESSION_ID, data, lockId, false);

            data = m_store.GetItem(null, SESSION_ID, out locked, out lockAge, out lockId, out actions);

            Assert.IsFalse(locked);
            Assert.AreEqual(TimeSpan.Zero, lockAge);
            Assert.IsNull(lockId);
            Assert.AreEqual(SessionStateActions.None, actions);

            session = data.Items;
            Assert.AreEqual(2, session["int"]);
            Assert.AreEqual("modified string", session["string"]);
            Assert.AreNotEqual(DateTime.Today, session["date"]);
            Assert.AreEqual(true, session["bool"]);
            Assert.AreNotEqual(BLOB, session["blob"]);
            Assert.AreNotEqual(PERSON, session["person"]);
        }
        public void TestPofValueInitialization()
        {
            PortablePerson person = CreatePerson();

            // perform the test twice, once with references disable, once with them enabled
            for (bool isRefEnabled = false; ;)
            {
                Binary    binPerson = Serialize(person, isRefEnabled);
                IPofValue pv        = PofValueParser.Parse(binPerson, GetPofContext(isRefEnabled));
                Assert.AreEqual(101, pv.TypeId);
                Assert.AreEqual(person, pv.GetValue());

                if (isRefEnabled)
                {
                    break;
                }
                isRefEnabled = true;
            }
        }
        public void TestCompositeKey()
        {
            ConfigurablePofContext.DefaultPofConfig = XmlHelper.LoadResource(
                ResourceLoader.GetResource(
                    "assembly://Coherence.Tests/Tangosol.Resources/s4hc-test-reference-config.xml"),
                "POF configuration");
            INamedCache cache = CacheFactory.GetCache("dist-pof-test");

            cache.Clear();
            CompositeKey key0 = new CompositeKey(new PortablePerson("Joe Smith", new DateTime(78, 4, 25)),
                                                 new PortablePerson("Joe Smith", new DateTime(78, 4, 25)));
            PortablePerson person1 = new PortablePerson("Joe Smith", new DateTime(78, 4, 25));
            CompositeKey   key1    = new CompositeKey(person1, person1);

            cache.Add(key0, "value0");
            cache.Add(key1, "value1");
            Assert.AreEqual(1, cache.Count);
            CacheFactory.Shutdown();
            ConfigurablePofContext.DefaultPofConfig = null;
        }
        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);
        }
        public void TestCacheOperations()
        {
            ConfigurablePofContext.DefaultPofConfig = XmlHelper.LoadResource(
                ResourceLoader.GetResource(
                    "assembly://Coherence.Tests/Tangosol.Resources/s4hc-test-reference-config.xml"),
                "POF configuration");
            INamedCache cache = CacheFactory.GetCache("dist-pof-test");

            PortablePerson ellen = new PortablePerson("Ellen", new DateTime(105, 3, 15));
            PortablePerson tom   = new PortablePerson("Tom", new DateTime(103, 7, 5))
            {
                Children = new PortablePerson[] { ellen }
            };

            cache.Add(tom.Name, tom);

            PortablePerson netObj = (PortablePerson)cache[tom.Name];

            Assert.IsTrue(tom.Equals(netObj));
            CacheFactory.Shutdown();
            ConfigurablePofContext.DefaultPofConfig = null;
        }
        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));
        }
Esempio n. 20
0
        public void TestConfigWithDefaultSerializer()
        {
            ConfigurablePofContext ctx = new ConfigurablePofContext("assembly://Coherence.Tests/Tangosol.Resources/s4hc-test-config-defaultserializer.xml");

            Assert.IsNotNull(ctx);

            IXmlElement config = ctx.Config;

            Assert.IsNotNull(config);

            // uses default serializer
            Address address         = new Address();
            string  addressTypeName = "Tangosol.Address";
            Type    addressType     = typeof(Address);

            int typeId = ctx.GetUserTypeIdentifier(addressType);

            Assert.AreEqual(typeId, 1201);
            string typeName = ctx.GetTypeName(typeId);

            Assert.AreEqual(typeName, addressTypeName);
            Type type = ctx.GetType(typeId);

            Assert.AreEqual(type, addressType);
            IPofSerializer serializer = ctx.GetPofSerializer(typeId);

            Assert.IsInstanceOf(typeof(DummyDefaultPofSerializer), serializer);
            Assert.IsTrue(ctx.IsUserType(address));

            // has its own serializer so default won't be used
            PortablePersonLite portablePersonLite         = new PortablePersonLite();
            string             portablePersonLiteTypeName = "Tangosol.PortablePersonLite";
            Type portablePersonLiteType = typeof(PortablePersonLite);

            typeId = ctx.GetUserTypeIdentifier(portablePersonLiteType);
            Assert.AreEqual(typeId, 1002);
            typeName = ctx.GetTypeName(typeId);
            Assert.AreEqual(typeName, portablePersonLiteTypeName);
            type = ctx.GetType(typeId);
            Assert.AreEqual(type, portablePersonLiteType);
            serializer = ctx.GetPofSerializer(typeId);
            Assert.IsInstanceOf(typeof(XmlPofSerializer), serializer);
            Assert.IsTrue(ctx.IsUserType(portablePersonLite));

            // is defined in included file with it's own default serializer
            // different from previous
            PortablePerson portablePerson         = new PortablePerson();
            string         portablePersonTypeName = "Tangosol.PortablePerson";
            Type           portablePersonType     = typeof(PortablePerson);

            typeId = ctx.GetUserTypeIdentifier(portablePersonType);
            Assert.AreEqual(typeId, 1005);
            typeName = ctx.GetTypeName(typeId);
            Assert.AreEqual(typeName, portablePersonTypeName);
            type = ctx.GetType(typeId);
            Assert.AreEqual(type, portablePersonType);
            serializer = ctx.GetPofSerializer(typeId);
            Assert.IsInstanceOf(typeof(BinaryPofSerializer), serializer);
            Assert.IsTrue(ctx.IsUserType(portablePerson));

            // does not have serializer defined and default is not specified
            // in the config, therefore will use PortableObjectSerializer
            serializer = ctx.GetPofSerializer(14); //UUID
            Assert.IsInstanceOf(typeof(PortableObjectSerializer), serializer);
        }
        public void TestDuplicateObjectReferences()
        {
            // loop twice, one for SimplePofContext, one for
            // ConfigurablePofContext.
            for (int loop = 0; loop < 2; loop++)
            {
                if (loop == 0)
                {
                    m_ctx = new SimplePofContext();
                    ((SimplePofContext)m_ctx).RegisterUserType(101,
                                                               typeof(PortablePerson),
                                                               new PortableObjectSerializer(101));
                    ((SimplePofContext)m_ctx).RegisterUserType(201,
                                                               typeof(CompositeKey),
                                                               new PortableObjectSerializer(201));
                    ((SimplePofContext)m_ctx).IsReferenceEnabled = true;
                }
                else
                {
                    const String sPath = "config/reference-pof-config.xml";
                    m_ctx = new ConfigurablePofContext(sPath);
                }

                initPOFWriter();
                var joe = new PortablePerson("Joe Smith",
                                             new DateTime(78, 4, 25));
                var differentJoe = new PortablePerson("Joe Smith",
                                                      new DateTime(78, 4, 25));
                var key = new CompositeKey(joe, joe);

                Assert.IsTrue(key.PrimaryKey == key.SecondaryKey);

                // test a collection of duplicate object references
                ICollection <PortablePerson> list = new List <PortablePerson>();
                list.Add(joe);
                list.Add(joe);
                list.Add(differentJoe);
                list.Add(differentJoe);

                m_pofWriter.EnableReference();
                m_pofWriter.WriteObject(0, key);
                m_pofWriter.WriteObject(0, list);

                initPOFReader();
                var result = (CompositeKey)m_pofReader.ReadObject(0);
                Assert.IsTrue(result.PrimaryKey == result.SecondaryKey);

                ICollection <object> result2 = new List <object>();
                m_pofReader.ReadCollection(0, result2);

                PortablePerson person = null;
                int            i      = 0;
                for (IEnumerator iter = result2.GetEnumerator();
                     iter.MoveNext();)
                {
                    var personNext = (PortablePerson)iter.Current;
                    if (person == null)
                    {
                        person = personNext;
                        i++;
                    }
                    else
                    {
                        Assert.IsTrue(person.Equals(personNext));
                        if (i == 1 || i == 3)
                        {
                            Assert.IsTrue(person == personNext);
                        }
                        else
                        {
                            person = personNext;
                        }
                        i++;
                    }
                }
            }
        }
Esempio n. 22
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);
        }
 protected bool Equals(PortablePerson other)
 {
     return(Equals(address, other.address) && age == other.age && height == other.height &&
            string.Equals(name, other.name));
 }
Esempio n. 24
0
        public void TestWriteGenericList()
        {
            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);

            ICollection <string> list1 = new List <string>();
            ICollection <object> list2 = new List <object>();

            list1.Add("A"); list1.Add(null); list1.Add("7");
            list2.Add("A"); list2.Add(null); list2.Add(7);

            ICollection <IPortableObject> persons  = new List <IPortableObject>();
            ICollection <PortablePerson>  persons2 = new List <PortablePerson>();
            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(ivan);
            persons.Add(aleks);
            persons.Add(goran);
            persons.Add(null);
            persons2.Add(ivan);
            persons2.Add(null);
            persons2.Add(goran);


            pofwriter.WriteCollection(0, list1);
            pofwriter.WriteCollection(0, list2);
            pofwriter.WriteCollection(0, persons);
            pofwriter.WriteCollection(0, persons);
            pofwriter.WriteCollection(0, (ICollection)persons2, typeof(PortablePerson));

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

            ICollection <string> result1 = new List <string>();

            pofreader.ReadCollection(0, result1);
            Assert.AreEqual(3, result1.Count);
            for (int i = 0; i < result1.Count; i++)
            {
                Assert.AreEqual(((List <string>)list1)[i], ((List <string>)result1)[i]);
            }

            ICollection <object> result2 = new List <object>();

            pofreader.ReadCollection(0, result2);
            Assert.AreEqual(3, result2.Count);
            for (int i = 0; i < result2.Count; i++)
            {
                Assert.AreEqual(((List <object>)list2)[i], ((List <object>)result2)[i]);
            }

            ICollection <IPortableObject> result3 = new List <IPortableObject>();

            pofreader.ReadCollection(0, result3);
            Assert.AreEqual(4, result3.Count);
            Assert.IsFalse(((List <IPortableObject>)result3)[0] is EvolvablePortablePerson);
            Assert.IsTrue(((List <IPortableObject>)result3)[1] is EvolvablePortablePerson);
            Assert.IsFalse(((List <IPortableObject>)result3)[2] is EvolvablePortablePerson);
            Assert.AreEqual(((List <IPortableObject>)result3)[3], null);
            EvolvablePortablePerson epp = (EvolvablePortablePerson)((List <IPortableObject>)result3)[1];

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

            PortablePerson pp = (PortablePerson)((List <IPortableObject>)result3)[0];

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

            List <IPortableObject> result4 = (List <IPortableObject>)pofreader.ReadCollection <IPortableObject>(0, null);

            Assert.AreEqual(4, result4.Count);
            Assert.IsFalse(result4[0] is EvolvablePortablePerson);
            Assert.IsTrue(result4[1] is EvolvablePortablePerson);
            Assert.IsFalse(result4[2] is EvolvablePortablePerson);
            Assert.AreEqual(result4[3], null);
            epp = (EvolvablePortablePerson)result4[1];
            Assert.AreEqual(aleks.Name, epp.Name);
            Assert.AreEqual(aleks.Children[0].Name, epp.Children[0].Name);

            pp = (PortablePerson)result4[0];
            Assert.AreEqual(ivan.Name, pp.Name);
            Assert.IsNull(pp.Children);

            List <PortablePerson> result5 = (List <PortablePerson>)pofreader.ReadCollection <PortablePerson>(0, null);

            for (int i = 0; i < persons2.Count; i++)
            {
                Assert.AreEqual(((List <PortablePerson>)persons2)[i], result5[i]);
            }
        }