public void AllFieldMetadataOfIndividualObjectMetadataTest()
        {
            IMetadataApi metadataApi = SpringContext.Current.GetObject<IMetadataApi>();
            Guid userTypeId = metadataApi.AddType("User", "Membership", "User Extension Description", ObjectMetadataTypes.Custom, false, null);
            metadataApi.SaveField(userTypeId, new StringFieldMetadata() { Name = "Name", IsRequired = true, MaxLengthSpecified = true, MaxLength = 32, Ordinal = 1 });
            metadataApi.SaveField(userTypeId, new DateTimeFieldMetadata() { Name = "Birthday", DefaultValue = new DateTimeValue { DateTimeValueType = DateTimeValueTypes.Now }, Ordinal = 2 });
            metadataApi.SaveField(userTypeId, new IntegerFieldMetadata() { Name = "Level", IsRequired = true, DefaultSpecified = true, Default = 10, Ordinal = 3 });
            metadataApi.SaveField(userTypeId, new DecimalFieldMetadata() { Name = "Salary", IsRequired = true, MinValueSpecified = true, MinValue = 800m, DefaultSpecified = true, Default = 2500m, Ordinal = 4 });

            HierarchyNode hierarchyNodeQA = new HierarchyNode { Name = "QA", Value = "QA" };
            HierarchyNode hierarchyNodeDev = new HierarchyNode { Name = "Dev", Value = "Dev" };
            HierarchyNode hierarchyNodeLeader = new HierarchyNode { Name = "Leader", Value = "Leader", Node = new HierarchyNode[] { hierarchyNodeQA, hierarchyNodeDev } };
            HierarchyNode hierarchyNodeManager = new HierarchyNode { Name = "Manager", Value = "Manager", Node = new HierarchyNode[] { hierarchyNodeLeader } };
            HierarchyNode hierarchyNodeDirector = new HierarchyNode { Name = "Director", Value = "Director", Node = new HierarchyNode[] { hierarchyNodeManager } };
            HierarchyFieldMetadata hierarchyFieldMetadata = new HierarchyFieldMetadata { Name = "Position", Description = "Position in hierarchy", IsRequired = true, Ordinal = 5, SelectionMode = SelectionModes.Single, Node = new HierarchyNode[] { hierarchyNodeDirector } };
            metadataApi.SaveField(userTypeId, hierarchyFieldMetadata);

            SelectionItem selectionItemShangHai = new SelectionItem { Name = "ShangHai", Value = "ShangHai" };
            SelectionItem selectionItemJiangSu = new SelectionItem { Name = "JiangSu", Value = "JiangSu" };
            SelectionItem selectionItemSiChuan = new SelectionItem { Name = "SiChuan", Value = "SiChuan" };
            SelectionItem selectionItemBlank = new SelectionItem { Name = "", Value = "" };
            EnumerationFieldMetadata enumerationFieldMetadata = new EnumerationFieldMetadata { Name = "HuKou", Ordinal = 6, SelectionMode = SelectionModes.Single, Items = new SelectionItem[] { selectionItemBlank, selectionItemJiangSu, selectionItemShangHai, selectionItemSiChuan } };
            metadataApi.SaveField(userTypeId, enumerationFieldMetadata);

            // 测试获取各个属性
            AssertFieldMetadata(metadataApi, userTypeId);

            // 测试删除一个属性
            metadataApi.DeleteField(userTypeId, "HuKou");
            Assert.AreEqual(5, metadataApi.GetFields(userTypeId).Count());

            // 测试更新一个属性的默认值
            IntegerFieldMetadata fieldMetadataLevel = metadataApi.GetField(userTypeId, "Level") as IntegerFieldMetadata;
            fieldMetadataLevel.Default = 9;
            metadataApi.SaveField(userTypeId, fieldMetadataLevel);
            fieldMetadataLevel = metadataApi.GetField(userTypeId, "Level") as IntegerFieldMetadata;
            Assert.AreEqual(9, fieldMetadataLevel.Default);

            // 删除扩展类型,会自动删除其所有的属性定义
            metadataApi.DeleteType(userTypeId);
            Assert.AreEqual(0, metadataApi.GetFields(userTypeId).Count());
        }
        public void HierarchyFieldMetadataValidationTest()
        {
            HierarchyNode hierarchyNodeChengDu = new HierarchyNode { Name = "ChengDu", Value = "ChengDu" };
            HierarchyNode hierarchyNodeShangHai = new HierarchyNode { Name = "ShangHai", Value = "ShangHai" };
            HierarchyNode hierarchyNodeChina = new HierarchyNode { Name = "China", Value = "China", Node = new HierarchyNode[] { hierarchyNodeChengDu, hierarchyNodeShangHai } };
            HierarchyNode hierarchyNodeAsia = new HierarchyNode { Name = "Asia", Value = "Asia", Node = new HierarchyNode[] { hierarchyNodeChina } };
            HierarchyNode hierarchyNodeEarth = new HierarchyNode { Name = "Earth", Value = "Earth", Node = new HierarchyNode[] { hierarchyNodeAsia } };
            HierarchyFieldMetadata fieldMetadata = new HierarchyFieldMetadata
            {
                Name = "Geoghraph",
                SelectionMode = SelectionModes.Single,
                Node = new HierarchyNode[] { hierarchyNodeEarth }
            };

            // 空值合法
            fieldMetadata.Validate(null);

            // 合法值
            fieldMetadata.Validate(new HierarchyNodeValueCollection { "ChengDu" }.FieldValue());

            try
            {
                fieldMetadata.Validate(new HierarchyNodeValueCollection { "HangZhou" }.FieldValue());
                Assert.Fail("HangZhou没有在Hierarchy中定义");
            }
            catch (InvalidFieldValueException) { }

            try
            {
                fieldMetadata.Validate(new HierarchyNodeValueCollection { "ChengDu", "ShangHai" }.FieldValue());
                Assert.Fail("当前Hierarchy只能设定单值");
            }
            catch (InvalidFieldValueException) { }
        }