public void TestAddDataSerializableFactoryClass()
        {
            var service1 = new SerializationServiceBuilder().Build();
            var data = service1.ToData(new DataSerializableBasicType());

            var config = new SerializationConfig();
            config.AddDataSerializableFactoryClass(1, typeof(MyDataSerializableFactory));
            var service = new SerializationServiceBuilder().SetConfig(config).Build();

            var obj = service.ToObject<object>(data);

            Assert.IsInstanceOf<DataSerializableBasicType>(obj);
        }
        public virtual void TestInputOutputWithPortableReader(ByteOrder byteOrder)
        {
            var portable = KitchenSinkPortable.Generate();

            var config = new SerializationConfig();
            config.AddPortableFactoryClass(KitchenSinkPortableFactory.FactoryId, typeof (KitchenSinkPortableFactory));

            var ss = new SerializationServiceBuilder().SetConfig(config).
                SetUseNativeByteOrder(false).SetByteOrder(byteOrder).Build();

            var data = ss.ToData(portable);
            var reader = ss.CreatePortableReader(data);

            var actual = new KitchenSinkPortable();
            actual.ReadPortable(reader);

            Assert.AreEqual(portable, actual);

            ss.Destroy();
        }
        public virtual void Test()
        {
            var portableFactory = new PortableFactoryFunc(i =>
            {
                if (i == 1) return new PortablePerson();
                if (i == 2) return new PortableAddress();
                throw new ArgumentException();
            });

            var ss = new SerializationServiceBuilder()
                .AddPortableFactory(FactoryId, portableFactory)
                .AddDataSerializableFactory(FactoryId, new ArrayDataSerializableFactory(new Func<IIdentifiedDataSerializable>[]
                {
                    () => new Address(),
                    () => new Person(),
                }))
                .Build();
            var k = 10;
            var tasks = new Task[k];
            for (var i = 0; i < k; i++)
            {
                tasks[i] = Task.Factory.StartNew(() =>
                {
                    for (var j = 0; j < 10000; j++)
                    {
                        var key = "key" + Rnd();
                        var dataKey = ss.ToData(key);
                        Assert.AreEqual(key, ss.ToObject<string>(dataKey));
                        var value = 123L + Rnd();
                        var dataValue = ss.ToData(value);
                        Assert.AreEqual(value, ss.ToObject<long>(dataValue));
                        var address = new Address("here here" + Rnd(), 13131 + Rnd());
                        var dataAddress = ss.ToData(address);
                        Assert.AreEqual(address, ss.ToObject<Address>(dataAddress));
                        var person = new Person(13 + Rnd(), 199L + Rnd(), 56.89d, "mehmet", address);
                        var dataPerson = ss.ToData(person);
                        Assert.AreEqual(person, ss.ToObject<Person>(dataPerson));
                        var portableAddress = new PortableAddress("there there " + Rnd(), 90909 + Rnd());
                        var dataPortableAddress = ss.ToData(portableAddress);
                        Assert.AreEqual(portableAddress, ss.ToObject<PortableAddress>(dataPortableAddress));
                        var portablePerson = new PortablePerson(63 + Rnd(), 167L + Rnd(), "ahmet", portableAddress);
                        var dataPortablePerson = ss.ToData(portablePerson);
                        Assert.AreEqual(portablePerson, ss.ToObject<PortablePerson>(dataPortablePerson));
                    }
                });
            }
            Task.WaitAll(tasks, new TimeSpan(0, 0, 0, 30));
        }
        public void Test_Issue2172_WritePortableArray()
        {
            var ss = new SerializationServiceBuilder().SetInitialOutputBufferSize(16).Build();
            var testObject2s = new TestObject2[100];
            for (var i = 0; i < testObject2s.Length; i++)
            {
                testObject2s[i] = new TestObject2();
            }

            var testObject1 = new TestObject1(testObject2s);
            ss.ToData(testObject1);
        }
        public void TestWriteReadWithNullPortableArray()
        {
            var builder0 = new ClassDefinitionBuilder(TestSerializationConstants.PORTABLE_FACTORY_ID, 1);
            var builder1 = new ClassDefinitionBuilder(TestSerializationConstants.PORTABLE_FACTORY_ID, 2);
            builder0.AddPortableArrayField("list", builder1.Build());

            var ss = new SerializationServiceBuilder()
                .AddClassDefinition(builder0.Build())
                .AddClassDefinition(builder1.Build())
                .Build();

            var data = ss.ToData(new TestObject1());

            var ss2 = new SerializationServiceBuilder()
                .AddPortableFactory(1, new PortableFactoryFunc(classId =>
                {
                    switch (classId)
                    {
                        case 1:
                            return new TestObject1();
                        case 2:
                            return new TestObject2();
                    }
                    return null;
                })).Build();
            var obj = ss2.ToObject<object>(data);
            Assert.IsNotNull(obj);
            Assert.IsInstanceOf<TestObject1>(obj);
        }
        public void TestWriteObjectWithPortable()
        {
            var ss = new SerializationServiceBuilder()
                .AddPortableFactory(TestSerializationConstants.PORTABLE_FACTORY_ID,
                    new PortableFactoryFunc(i => new NamedPortableV2()))
                .AddDataSerializableFactory(TestSerializationConstants.DATA_SERIALIZABLE_FACTORY_ID,
                    GetDataSerializableFactory())
                .Build();

            var ss2 = new SerializationServiceBuilder()
                .AddPortableFactory(TestSerializationConstants.PORTABLE_FACTORY_ID,
                    new PortableFactoryFunc(i => new NamedPortable()))
                .AddDataSerializableFactory(TestSerializationConstants.DATA_SERIALIZABLE_FACTORY_ID,
                    GetDataSerializableFactory())
                .SetPortableVersion(5)
                .Build();

            var o1 = new ComplexDataSerializable(new NamedPortableV2("test", 123),
                new ByteArrayDataSerializable(new byte[3]), null);
            var data = ss.ToData(o1);

            object o2 = ss2.ToObject<ComplexDataSerializable>(data);
            Assert.AreEqual(o1, o2);
        }
        public void TestWriteObjectWithCustomSerializable()
        {
            var config = new SerializationConfig();
            var sc = new SerializerConfig()
                .SetImplementation(new CustomSerializer())
                .SetTypeClass(typeof (CustomSerializableType));
            config.AddSerializerConfig(sc);
            var serializationService =
                new SerializationServiceBuilder().SetPortableVersion(1)
                    .AddPortableFactory(TestSerializationConstants.PORTABLE_FACTORY_ID, new TestPortableFactory())
                    .SetConfig(config).Build();

            var foo = new CustomSerializableType {Value = "foo"};

            var objectCarryingPortable1 = new ObjectCarryingPortable(foo);
            var data = serializationService.ToData(objectCarryingPortable1);
            var objectCarryingPortable2 = serializationService.ToObject<ObjectCarryingPortable>(data);
            Assert.AreEqual(objectCarryingPortable1, objectCarryingPortable2);
        }
        public void TestWriteDataWithPortable()
        {
            var ss = new SerializationServiceBuilder()
                .AddPortableFactory(TestSerializationConstants.PORTABLE_FACTORY_ID,
                    new PortableFactoryFunc(i => new NamedPortableV2()))
                .AddDataSerializableFactory(TestSerializationConstants.DATA_SERIALIZABLE_FACTORY_ID,
                    GetDataSerializableFactory())
                .Build();

            var ss2 = new SerializationServiceBuilder()
                .AddPortableFactory(TestSerializationConstants.PORTABLE_FACTORY_ID,
                    new PortableFactoryFunc(i => new NamedPortable()))
                .AddDataSerializableFactory(TestSerializationConstants.DATA_SERIALIZABLE_FACTORY_ID,
                    GetDataSerializableFactory())
                .SetPortableVersion(5)
                .Build();

            IPortable p1 = new NamedPortableV2("test", 456);
            object o1 = new DataDataSerializable(ss.ToData(p1));

            var data = ss.ToData(o1);

            var o2 = ss2.ToObject<DataDataSerializable>(data);
            Assert.AreEqual(o1, o2);

            var p2 = ss2.ToObject<IPortable>(o2.Data);
            Assert.AreEqual(p1, p2);
        }
        public void TestSerializationService_CreatePortableReader()
        {
            var serializationService = new SerializationServiceBuilder().Build();

            var timestamp1 = TestSupport.RandomLong();
            var child = new ChildPortableObject(timestamp1);
            var timestamp2 = TestSupport.RandomLong();
            var parent = new ParentPortableObject(timestamp2, child);
            var timestamp3 = timestamp1 + timestamp2;
            var grandParent = new GrandParentPortableObject(timestamp3, parent);

            var data = serializationService.ToData(grandParent);
            var reader = serializationService.CreatePortableReader(data);

            Assert.AreEqual(grandParent.timestamp, reader.ReadLong("timestamp"));
            Assert.AreEqual(parent.timestamp, reader.ReadLong("child.timestamp"));
            Assert.AreEqual(child.timestamp, reader.ReadLong("child.child.timestamp"));
        }
        public void TestClassDefinitionConfig()
        {
            var serializationConfig = new SerializationConfig();
            serializationConfig.AddPortableFactory(TestSerializationConstants.PORTABLE_FACTORY_ID,
                new TestPortableFactory());
            serializationConfig.SetPortableVersion(1);
            serializationConfig
                .AddClassDefinition(
                    new ClassDefinitionBuilder(TestSerializationConstants.PORTABLE_FACTORY_ID,
                        TestSerializationConstants.RAW_DATA_PORTABLE)
                        .AddLongField("l")
                        .AddCharArrayField("c")
                        .AddPortableField("p", CreateNamedPortableClassDefinition())
                        .Build())
                .AddClassDefinition(
                    new ClassDefinitionBuilder(TestSerializationConstants.PORTABLE_FACTORY_ID,
                        TestSerializationConstants.NAMED_PORTABLE)
                        .AddUTFField("name").AddIntField("myint").Build());

            var serializationService = new SerializationServiceBuilder()
                .SetConfig(serializationConfig)
                .AddDataSerializableFactory(TestSerializationConstants.DATA_SERIALIZABLE_FACTORY_ID,
                    GetDataSerializableFactory())
                .Build();
            var p = new RawDataPortable(DateTime.Now.ToFileTime(), "test chars".ToCharArray(),
                new NamedPortable("named portable", 34567),
                9876, "Testing raw portable", new ByteArrayDataSerializable(Encoding.UTF8.GetBytes("test bytes")));

            var data = serializationService.ToData(p);
            Assert.AreEqual(p, serializationService.ToObject<RawDataPortable>(data));
        }
        public void TestAddPortableFactoryClass()
        {
            var service1 = new SerializationServiceBuilder().Build();
            var data = service1.ToData(new KitchenSinkPortable());

            var config = new SerializationConfig();
            config.AddPortableFactoryClass(1, typeof(KitchenSinkPortableFactory));

            var service = new SerializationServiceBuilder().SetConfig(config).Build();

            var obj = service.ToObject<object>(data);
            Assert.IsInstanceOf<KitchenSinkPortable>(obj);
        }