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 SelectionFieldMetadataValidationTest()
        {
            SelectionItem selectionItemShangHai = new SelectionItem { Name = "ShangHai", Value = "ShangHai" };
            SelectionItem selectionItemChengDu = new SelectionItem { Name = "ChengDu", Value = "ChengDu" };
            SelectionItem selectionItemHangZhou = new SelectionItem { Name = "HangZhou", Value = "HangZhou" };
            SelectionItem selectionItemBlank = new SelectionItem { Name = "", Value = "" };
            IFieldMetadata fieldMetadata = new EnumerationFieldMetadata
            {
                Name = "City",
                SelectionMode = SelectionModes.Single,
                Items = new SelectionItem[] { selectionItemBlank, selectionItemChengDu, selectionItemShangHai, selectionItemHangZhou }
            };

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

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

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

            try
            {
                fieldMetadata.Validate(new EnumerationValueCollection { "ChengDu", "ShangHai" }.FieldValue());
                Assert.Fail("当前Selection只能选择单值");
            }
            catch (InvalidFieldValueException) { }
        }