Class SitecoreIdConfiguration
Inheritance: Glass.Mapper.Configuration.IdConfiguration
コード例 #1
0
        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);
            }
        }
コード例 #2
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.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);
        }
コード例 #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);
        }
コード例 #4
0
        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);
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        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;
        }
コード例 #7
0
       // [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
            }
        }
コード例 #8
0
        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
        }