public void CanCustomizeHowReferencesAreResolved()
        {
            var xml =
                Assembly.GetExecutingAssembly()
                .GetManifestResourceStream(@"SquaredInfinity.Foundation.Serialization.UnitTests.Serialization.FlexiXml.Repository.Resources.repository.xml")
                .ReadToEnd();

            var xDoc = XDocument.Parse(xml);

            var s = new FlexiXmlSerializer();

            var options = new FlexiXmlSerializationOptions();

            options.UniqueIdAttributeName = XName.Get("name");
            options.UniqueIdReferenceAttributeName = XName.Get("ref");

            var o = s.Deserialize<Configuration>(xDoc, options);

            Assert.IsNotNull(o);

            Assert.AreEqual(2, o.Repository.Formatters.Count);
            Assert.AreEqual(2, o.Emails.Count);

            Assert.AreSame(o.Repository.Formatters[0], o.Emails[0].BodyFormatter);
            Assert.AreSame(o.Repository.Formatters[1], o.Emails[0].SubjectFormatter);

            Assert.AreSame(o.Repository.Formatters[0], o.Emails[1].BodyFormatter);
            Assert.AreSame(o.Repository.Formatters[1], o.Emails[1].SubjectFormatter);
        }
        public void CanDeserializeCollections()
        {
            var xml =
                Assembly.GetExecutingAssembly()
                .GetManifestResourceStream(@"SquaredInfinity.Foundation.Serialization.FlexiXml.Resources.Deserialization.collection_tests.xml")
                .ReadToEnd();

            var s = new FlexiXmlSerializer();

            var root = s.Deserialize<Root>(xml);

            Assert.IsNotNull(root);

            Assert.AreEqual(13, (root.CollectionReadOnlyPropertyCreatedInConstructor as CollectionItemCollection).IntProperty);
            Assert.AreEqual("string value 1", (root.CollectionReadOnlyPropertyCreatedInConstructor as CollectionItemCollection).StringProperty);
            Assert.AreEqual(3, root.CollectionReadOnlyPropertyCreatedInConstructor.Count);
            Assert.AreEqual(14, root.CollectionReadOnlyPropertyCreatedInConstructor[0].IntProperty);
            Assert.AreEqual(15, root.CollectionReadOnlyPropertyCreatedInConstructor[1].IntProperty);
            Assert.AreEqual(16, root.CollectionReadOnlyPropertyCreatedInConstructor[2].IntProperty);

            Assert.AreEqual(23, (root.Collection as CollectionItemCollection).IntProperty);
            Assert.AreEqual("string value 2", (root.Collection as CollectionItemCollection).StringProperty);
            Assert.AreEqual(3, root.Collection.Count);
            Assert.AreEqual(24, root.Collection[0].IntProperty);
            Assert.AreEqual(25, root.Collection[1].IntProperty);
            Assert.AreEqual(26, root.Collection[2].IntProperty);

            Assert.AreEqual(33, root.NonGenericCollection.IntProperty);
            Assert.AreEqual("string value 3", root.NonGenericCollection.StringProperty);
            Assert.AreEqual(3, root.NonGenericCollection.Count);
            Assert.AreEqual(34, root.NonGenericCollection[0].IntProperty);
            Assert.AreEqual(35, root.NonGenericCollection[1].IntProperty);
            Assert.AreEqual(36, root.NonGenericCollection[2].IntProperty);
        }
        public void XmlRootNameCanBeAnything()
        {
            var xml =
                Assembly.GetExecutingAssembly()
                .GetManifestResourceStream(@"SquaredInfinity.Foundation.Serialization.FlexiXml.Resources.Deserialization.XmlRootNameCanBeAnything.xml")
                .ReadToEnd();

            var s = new FlexiXmlSerializer();

            var root = s.Deserialize<Root>(xml);

            Assert.IsNotNull(root);

            Assert.AreEqual("string value 1", root.StringProperty);
        }
        public void AttachedElementsCanBeUsedToMapProperties()
        {
            var xml =
                Assembly.GetExecutingAssembly()
                .GetManifestResourceStream(@"SquaredInfinity.Foundation.Serialization.FlexiXml.Resources.Deserialization.AttachedElementsCanBeUsedToMapProperties.xml")
                .ReadToEnd();

            var s = new FlexiXmlSerializer();

            var root = s.Deserialize<Root>(xml);

            Assert.IsNotNull(root);

            Assert.AreEqual("string value 1", root.StringProperty);
            Assert.AreEqual(13, root.IntProperty);
            Assert.AreEqual(14, root.NullableIntProperty);
            Assert.AreEqual(new Guid("9763C96B-5AC4-4ACA-AE4E-5D71EDDBE0DD"), root.GuidProperty);
        }
        public void UsesKnownTypesLookup()
        {
            // in this test Entity on root is serialized as TestEntityA, but KnownTypes mappign is set (all in xml) to TestEntityB
            // Entity should therefore resolve to TestEntityB

            var xml =
                Assembly.GetExecutingAssembly()
                .GetManifestResourceStream(@"SquaredInfinity.Foundation.Serialization.FlexiXml.KnownTypesLookup.KnownTypesLookup.xml")
                .ReadToEnd();

            var s = new FlexiXmlSerializer();

            var op = new FlexiXmlSerializationOptions();
            op.TypeInformation = TypeInformation.LookupOnly;

            var root = s.Deserialize<TestRoot>(xml, op);

            Assert.IsNotNull(root);

            Assert.AreEqual(typeof(TestEntityB), root.Entity.GetType());
            Assert.AreEqual(13, root.Entity.Id);

        }
        public void TypeWithInterfaceProperty__CanDeserialize()
        {
            var o = new InterfaceTypeProperty();
            o.MyDispsableProperty = new InterfaceTypeProperty();

            var s = new FlexiXmlSerializer();

            var xml = s.Serialize(o);

            var o2 = s.Deserialize<InterfaceTypeProperty>(xml);

            Assert.IsNotNull(o2);

            Assert.IsNotNull(o2.MyDispsableProperty);

            Assert.AreNotSame(o2, o2.MyDispsableProperty);
        }
        public void SupportsDictionarySerialization()
        {
            var o = new SimpleDictionary();
            o.Add(1, "one");
            o.Add(2, "two");
            o.CustomProperty = "Numbers";

            var s = new FlexiXmlSerializer();

            var xml = s.Serialize(o);

            var has_keys_element =
                (from e in xml.Elements()
                 where e.Name == "Keys"
                 select e).Any();

            var has_values_element =
                (from e in xml.Elements()
                 where e.Name == "Values"
                 select e).Any();

            // keys and values are skipped from serialization by default
            Assert.IsFalse(has_keys_element);
            Assert.IsFalse(has_values_element);

            var o2 = s.Deserialize<SimpleDictionary>(xml);

            Assert.IsNotNull(o2);
            Assert.AreNotSame(o, o2);
            Assert.AreEqual(o.CustomProperty, o2.CustomProperty);
            Assert.AreEqual(2, o2.Count);

            Assert.AreEqual(1, o2.Keys.First());
            Assert.AreEqual("one", o2.Values.First());

            Assert.AreEqual(2, o2.Keys.Skip(1).First());
            Assert.AreEqual("two", o2.Values.Skip(1).First());
        }