Exemple #1
0
        public void TestAddPortableFactoryWithDuplicateId()
        {
            Assert.Throws <InvalidOperationException>(() =>
            {
                var config = new SerializationOptions();
                config.AddPortableFactory(1, new KitchenSinkPortableFactory());
                config.AddPortableFactory(1, typeof(KitchenSinkPortableFactory));

                var service = new SerializationServiceBuilder(new NullLoggerFactory()).SetConfig(config).Build();
            });
        }
Exemple #2
0
        public void TestClassDefinitionConfigWithErrors()
        {
            var serializationConfig = new SerializationOptions();

            serializationConfig.AddPortableFactory(SerializationTestsConstants.PORTABLE_FACTORY_ID,
                                                   new TestPortableFactory());
            serializationConfig.PortableVersion = 1;
            serializationConfig.ClassDefinitions.Add(
                new ClassDefinitionBuilder(SerializationTestsConstants.PORTABLE_FACTORY_ID,
                                           SerializationTestsConstants.RAW_DATA_PORTABLE, 1)
                .AddLongField("l")
                .AddCharArrayField("c")
                .AddPortableField("p", CreateNamedPortableClassDefinition(1))
                .Build());

            try
            {
                new SerializationServiceBuilder(new NullLoggerFactory()).SetConfig(serializationConfig).Build();
                Assert.Fail("Should throw SerializationException!");
            }
            catch (SerializationException)
            {
            }

            new SerializationServiceBuilder(new NullLoggerFactory()).SetConfig(serializationConfig).SetCheckClassDefErrors(false).Build();

            // -- OR --

            serializationConfig.ValidateClassDefinitions = false;
            new SerializationServiceBuilder(new NullLoggerFactory()).SetConfig(serializationConfig).Build();
        }
Exemple #3
0
        public void TestClassDefinitionConfig()
        {
            int portableVersion     = 1;
            var serializationConfig = new SerializationOptions();

            serializationConfig.AddPortableFactory(SerializationTestsConstants.PORTABLE_FACTORY_ID,
                                                   new TestPortableFactory());
            serializationConfig.PortableVersion = portableVersion;
            serializationConfig
            .ClassDefinitions.Add(
                new ClassDefinitionBuilder(SerializationTestsConstants.PORTABLE_FACTORY_ID,
                                           SerializationTestsConstants.RAW_DATA_PORTABLE, portableVersion)
                .AddLongField("l")
                .AddCharArrayField("c")
                .AddPortableField("p", CreateNamedPortableClassDefinition(portableVersion))
                .Build());
            serializationConfig
            .ClassDefinitions.Add(
                new ClassDefinitionBuilder(SerializationTestsConstants.PORTABLE_FACTORY_ID,
                                           SerializationTestsConstants.NAMED_PORTABLE, portableVersion)
                .AddStringField("name").AddIntField("myint").Build());

            var serializationService = new SerializationServiceBuilder(new NullLoggerFactory())
                                       .SetConfig(serializationConfig)
                                       .AddDataSerializableFactory(SerializationTestsConstants.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));
        }
Exemple #4
0
        public void TestAddPortableFactory2WithBadId()
        {
            Assert.Throws <ArgumentException>(() =>
            {
                var config = new SerializationOptions();
                config.AddPortableFactory(-1, new KitchenSinkPortableFactory());

                var service = new SerializationServiceBuilder(new NullLoggerFactory()).SetConfig(config).Build();
            });
        }
Exemple #5
0
        public void TestAddPortableFactoryWithNoEmptyConstructor()
        {
            Assert.Throws <ServiceFactoryException>(() =>
            {
                var config = new SerializationOptions();
                config.AddPortableFactory(1, typeof(PortableFactory));

                var service = new SerializationServiceBuilder(new NullLoggerFactory()).SetConfig(config).Build();
            });
        }
Exemple #6
0
        public void TestAddPortableFactoryWhichDoesNotImplementPortableFactory()
        {
            Assert.Throws <ServiceFactoryException>(() =>
            {
                var config = new SerializationOptions();
                config.AddPortableFactory(1, typeof(SerializableFactory));

                var service = new SerializationServiceBuilder(new NullLoggerFactory()).SetConfig(config).Build();
            });
        }
Exemple #7
0
        public void TestAddPortableFactory2()
        {
            var service1 = new SerializationServiceBuilder(new NullLoggerFactory()).Build();
            var data     = service1.ToData(new KitchenSinkPortable());

            var config = new SerializationOptions();

            config.AddPortableFactory(1, typeof(KitchenSinkPortableFactory));

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

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

            Assert.IsInstanceOf <KitchenSinkPortable>(obj);
        }
Exemple #8
0
        public virtual void TestInputOutputWithPortableReader(Endianness endianness)
        {
            var portable = KitchenSinkPortable.Generate();

            var config = new SerializationOptions();

            config.AddPortableFactory(KitchenSinkPortableFactory.FactoryId, typeof(KitchenSinkPortableFactory));

            using var ss = new SerializationServiceBuilder(new NullLoggerFactory()).SetConfig(config)
                           .SetEndianness(endianness).Build();

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

            var actual = new KitchenSinkPortable();

            actual.ReadPortable(reader);

            Assert.AreEqual(portable, actual);
        }
Exemple #9
0
        public virtual void TestDataInputOutputWithPortable(Endianness endianness)
        {
            var portable = KitchenSinkPortable.Generate();

            var config = new SerializationOptions();

            config.AddPortableFactory(KitchenSinkPortableFactory.FactoryId, typeof(KitchenSinkPortableFactory));

            using var ss = new SerializationServiceBuilder(new NullLoggerFactory()).SetConfig(config)
                           .SetEndianness(endianness).Build();

            IObjectDataOutput output = ss.CreateObjectDataOutput(1024);

            output.WriteObject(portable);
            var data = output.ToByteArray();

            IObjectDataInput input = ss.CreateObjectDataInput(data);
            var readObject         = input.ReadObject <IPortable>();

            Assert.AreEqual(portable, readObject);
        }