Class SitecoreIdConfiguration
Inheritance: Glass.Mapper.Configuration.IdConfiguration
        public void MapToProperty_ItemIdAsGuid_ReturnsIdAsGuid()
        {
            //Assign
            string targetPath = "/sitecore/content/target";

            using (Db database = new Db
            {
                new Sitecore.FakeDb.DbItem("Target")
            })
            {
                var mapper = new SitecoreIdMapper();
                var config = new SitecoreIdConfiguration();
                var property = typeof(Stub).GetProperty("GuidId");
                var item = database.GetItem("/sitecore/content/target");

                Assert.IsNotNull(item, "Item is null, check in Sitecore that item exists");

                config.PropertyInfo = property;

                mapper.Setup(new DataMapperResolverArgs(null, config));

                var dataContext = new SitecoreDataMappingContext(null, item, null);
                var expected = item.ID.Guid;

                //Act
                var value = mapper.MapToProperty(dataContext);

                //Assert
                Assert.AreEqual(expected, value);
            }
        }
        /// <summary>
        /// Called to map each property automatically
        /// </summary>
        /// <param name="property"></param>
        /// <returns></returns>
        protected override AbstractPropertyConfiguration AutoMapProperty(System.Reflection.PropertyInfo property)
        {
            string           name = property.Name;
            SitecoreInfoType infoType;

            if (name.Contains(".")) //explicit interface implementation
            {
                name = name.Split('.', StringSplitOptions.RemoveEmptyEntries).Last();
            }

            if (name.ToLowerInvariant() == "id")
            {
                var idConfig = new SitecoreIdConfiguration();
                idConfig.PropertyInfo = property;
                return(idConfig);
            }

            if (name.ToLowerInvariant() == "parent")
            {
                var parentConfig = new SitecoreParentConfiguration();
                parentConfig.PropertyInfo = property;
                return(parentConfig);
            }
            if (name.ToLowerInvariant() == "children")
            {
                var childrenConfig = new SitecoreChildrenConfiguration();
                childrenConfig.PropertyInfo = property;
                return(childrenConfig);
            }
            if (name.ToLowerInvariant() == "item" && property.PropertyType == typeof(Item))
            {
                var itemConfig = new SitecoreItemConfiguration();
                itemConfig.PropertyInfo = property;
                return(itemConfig);
            }

            if (Enum.TryParse(name, true, out infoType))
            {
                SitecoreInfoConfiguration infoConfig = new SitecoreInfoConfiguration();
                infoConfig.PropertyInfo = property;
                infoConfig.Type         = infoType;
                return(infoConfig);
            }

            SitecoreFieldConfiguration fieldConfig = new SitecoreFieldConfiguration();

            fieldConfig.FieldName    = name;
            fieldConfig.PropertyInfo = property;
            return(fieldConfig);
        }
Example #3
0
        /// <summary>
        /// Called to map each property automatically
        /// </summary>
        /// <param name="property"></param>
        /// <returns></returns>
        protected override AbstractPropertyConfiguration AutoMapProperty(System.Reflection.PropertyInfo property)
        {
            string           name = property.Name;
            SitecoreInfoType infoType;

            if (name.ToLowerInvariant() == "id")
            {
                var idConfig = new SitecoreIdConfiguration();
                idConfig.PropertyInfo = property;
                return(idConfig);
            }

            if (name.ToLowerInvariant() == "parent")
            {
                var parentConfig = new SitecoreParentConfiguration();
                parentConfig.PropertyInfo = property;
                return(parentConfig);
            }
            if (name.ToLowerInvariant() == "children")
            {
                var childrenConfig = new SitecoreChildrenConfiguration();
                childrenConfig.PropertyInfo = property;
                return(childrenConfig);
            }
            if (name.ToLowerInvariant() == "item" && property.PropertyType == typeof(Item))
            {
                var itemConfig = new SitecoreItemConfiguration();
                itemConfig.PropertyInfo = property;
                return(itemConfig);
            }

            if (Enum.TryParse(name, true, out infoType))
            {
                SitecoreInfoConfiguration infoConfig = new SitecoreInfoConfiguration();
                infoConfig.PropertyInfo = property;
                infoConfig.Type         = infoType;
                return(infoConfig);
            }

            SitecoreFieldConfiguration fieldConfig = new SitecoreFieldConfiguration();

            fieldConfig.FieldName    = name;
            fieldConfig.PropertyInfo = property;
            return(fieldConfig);
        }
        public void MapToProperty_ItemIdAsGuid_ReturnsIdAsGuid()
        {
            //Assign
            var mapper = new SitecoreIdMapper();
            var config = new SitecoreIdConfiguration();
            var property = typeof (Stub).GetProperty("GuidId");
            var item = _db.GetItem("/sitecore/content/Tests/DataMappers/SitecoreIdMapper/EmptyItem");

            Assert.IsNotNull(item, "Item is null, check in Sitecore that item exists");

            config.PropertyInfo = property;
            
            mapper.Setup(new DataMapperResolverArgs(null,config));

            var dataContext = new SitecoreDataMappingContext(null, item, null);
            var expected = item.ID.Guid;

            //Act
            var value = mapper.MapToProperty(dataContext);

            //Assert
            Assert.AreEqual(expected, value);
        }
        public void CanHandle_IncorrectConfigType_ReturnsFalse()
        {
            //Assign
            var config = new SitecoreIdConfiguration();
            var type2 = typeof(string);
            var type1 = typeof(int);
            var mapper = new StubMapper(type1, type2);

            config.PropertyInfo = typeof(Stub).GetProperty("Property");

            //Act
            var result = mapper.CanHandle(config, null);

            //Assert
            Assert.IsFalse(result);
        }
        protected override AbstractPropertyConfiguration AutoMapProperty(System.Reflection.PropertyInfo property)
        {
            string name = property.Name;
            SitecoreInfoType infoType;

            if (name.ToLowerInvariant() == "id")
            {
                SitecoreIdConfiguration idConfig = new SitecoreIdConfiguration();
                idConfig.PropertyInfo = property;
                return idConfig;
            }

            if (name.ToLowerInvariant() == "parent")
            {
                SitecoreParentConfiguration parentConfig = new SitecoreParentConfiguration();
                parentConfig.PropertyInfo = property;
                return parentConfig;
            }
            if (name.ToLowerInvariant() == "children")
            {
                SitecoreChildrenConfiguration childrenConfig = new SitecoreChildrenConfiguration();
                childrenConfig.PropertyInfo = property;
                return childrenConfig;
            }

            if (Enum.TryParse(name, true, out infoType))
            {
                SitecoreInfoConfiguration infoConfig = new SitecoreInfoConfiguration();
                infoConfig.PropertyInfo = property;
                infoConfig.Type = infoType;
                return infoConfig;
            }

            SitecoreFieldConfiguration fieldConfig = new SitecoreFieldConfiguration();
            fieldConfig.FieldName = name;
            fieldConfig.PropertyInfo = property;
            return fieldConfig;
        }
       // [ExpectedException(typeof(NotSupportedException))]
        public void MapToProperty_ItemIdAsString_ThrowsException()
        {
            //Assign
            string targetPath = "/sitecore/content/target";

            using (Db database = new Db
            {
                new Sitecore.FakeDb.DbItem("Target")
            })
            {
                var mapper = new SitecoreIdMapper();
                var config = new SitecoreIdConfiguration();
                var property = typeof(Stub).GetProperty("StringId");

                config.PropertyInfo = property;

                
                var item = database.GetItem("/sitecore/content/target");

                Assert.IsNotNull(item, "Item is null, check in Sitecore that item exists");

                //Act
                Assert.Throws<NotSupportedException>(() =>
                {
                    mapper.Setup(new DataMapperResolverArgs(null, config));

                });

                //Assert
                //Exception, not asserts
            }
        }
        public void MapToProperty_ItemIdAsString_ThrowsException()
        {
            //Assign
            var mapper = new SitecoreIdMapper();
            var config = new SitecoreIdConfiguration();
            var property = typeof(Stub).GetProperty("StringId");

            config.PropertyInfo = property;

            mapper.Setup(new DataMapperResolverArgs(null,config));

            var item = _db.GetItem("/sitecore/content/Tests/DataMappers/SitecoreIdMapper/EmptyItem");

            Assert.IsNotNull(item, "Item is null, check in Sitecore that item exists");
            var dataContext = new SitecoreDataMappingContext(null, item, null);
            var expected = item.ID;

            //Act
            var value = mapper.MapToProperty(dataContext);

            //Assert
            //Exception, not asserts
        }