/// <summary>
        /// Adds or updates an annotation with the specified name and value.
        /// </summary>
        /// <remarks>
        /// If an annotation with the given name already exists then the value of that annotation
        /// is updated to the given value. If the given value is null then the annotation will be
        /// removed.
        /// </remarks>
        /// <param name="name">The name of the annotation property.</param>
        /// <param name="value">The value of the annotation property.</param>
        public void AddAnnotation(string name, object value)
        {
            Check.NotEmpty(name, nameof(name));
            MetadataProperty metadataProperty = this.Annotations.FirstOrDefault <MetadataProperty>((Func <MetadataProperty, bool>)(a => a.Name == name));

            if (metadataProperty != null)
            {
                if (value == null)
                {
                    this.RemoveAnnotation(name);
                }
                else
                {
                    metadataProperty.Value = value;
                }
            }
            else
            {
                if (value == null)
                {
                    return;
                }
                this.GetMetadataProperties().Add(MetadataProperty.CreateAnnotation(name, value));
            }
        }
Example #2
0
        public void CreateAnnotation_creates_correct_metdata_property()
        {
            var value      = new object();
            var annotation = MetadataProperty.CreateAnnotation("name", value);

            Assert.Equal("name", annotation.Name);
            Assert.Equal(value, annotation.Value);
            Assert.Null(annotation.TypeUsage);
            Assert.Equal(PropertyKind.Extended, annotation.PropertyKind);
            Assert.True(annotation.IsAnnotation);
        }
Example #3
0
            public void Contains_is_implemented()
            {
                var typeUsage    = TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));
                var metadataItem = new EntityType("E", "N", DataSpace.CSpace);
                var annotation   = MetadataProperty.CreateAnnotation("AN", "AV");

                metadataItem.MetadataProperties.Source.Add(MetadataProperty.Create("PN", typeUsage, "PV"));

                Assert.False(metadataItem.Annotations.Contains(annotation));

                metadataItem.Annotations.Add(annotation);

                Assert.True(metadataItem.Annotations.Contains(annotation));
            }
Example #4
0
        public void Can_set_and_get_annotation_value()
        {
            var value1 = new object();
            var value2 = new object();

            var annotation = MetadataProperty.CreateAnnotation("name", value1);

            Assert.Equal("name", annotation.Name);
            Assert.Same(value1, annotation.Value);

            annotation.Value = value2;

            Assert.Equal("name", annotation.Name);
            Assert.Same(value2, annotation.Value);
        }
Example #5
0
            public void Add_is_implemented()
            {
                var typeUsage    = TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));
                var metadataItem = new EntityType("E", "N", DataSpace.CSpace);

                metadataItem.MetadataProperties.Source.Add(MetadataProperty.Create("PN", typeUsage, "PV"));
                var count = metadataItem.MetadataProperties.Count;

                var annotation = MetadataProperty.CreateAnnotation("AN", "AV");

                metadataItem.Annotations.Add(annotation);

                Assert.Same(annotation, metadataItem.MetadataProperties.ElementAt(count));
                Assert.Same(annotation, metadataItem.Annotations.Single());
            }
Example #6
0
            public void Count_is_implemented()
            {
                var typeUsage    = TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));
                var metadataItem = new EntityType("E", "N", DataSpace.CSpace);

                metadataItem.MetadataProperties.Source.Add(MetadataProperty.Create("PN", typeUsage, "PV"));

                var count = metadataItem.MetadataProperties.Count;

                Assert.Equal(0, metadataItem.Annotations.Count);

                metadataItem.Annotations.Add(MetadataProperty.CreateAnnotation("AN", "AV"));

                Assert.Equal(count + 1, metadataItem.MetadataProperties.Count);
                Assert.Equal(1, metadataItem.Annotations.Count);
            }
Example #7
0
            public void CopyTo_is_implemented()
            {
                var typeUsage    = TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));
                var metadataItem = new EntityType("E", "N", DataSpace.CSpace);
                var annotation1  = MetadataProperty.CreateAnnotation("AN1", "AV1");
                var annotation2  = MetadataProperty.CreateAnnotation("AN2", "AV2");

                metadataItem.MetadataProperties.Source.Add(MetadataProperty.Create("PN", typeUsage, "PV"));
                metadataItem.Annotations.Add(annotation1);
                metadataItem.Annotations.Add(annotation2);

                var annotations = new MetadataProperty[2];

                metadataItem.Annotations.CopyTo(annotations, 0);

                Assert.Same(annotation1, annotations[0]);
                Assert.Same(annotation2, annotations[1]);
            }
Example #8
0
            public void GetEnumerator_is_implemented()
            {
                var typeUsage    = TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));
                var metadataItem = new EntityType("E", "N", DataSpace.CSpace);
                var annotation1  = MetadataProperty.CreateAnnotation("AN1", "AV1");
                var annotation2  = MetadataProperty.CreateAnnotation("AN2", "AV2");

                metadataItem.MetadataProperties.Source.Add(MetadataProperty.Create("PN", typeUsage, "PV"));
                metadataItem.Annotations.Add(annotation1);
                metadataItem.Annotations.Add(annotation2);

                var enumerator = metadataItem.Annotations.GetEnumerator();

                Assert.True(enumerator.MoveNext());
                Assert.Same(annotation1, enumerator.Current);
                Assert.True(enumerator.MoveNext());
                Assert.Same(annotation2, enumerator.Current);
                Assert.False(enumerator.MoveNext());
            }
Example #9
0
        public void WriteExtendedProperties_does_not_write_annotations_that_do_not_have_a_name_containing_a_colon()
        {
            var property = MetadataProperty.CreateAnnotation("Foo", "true");

            var mockEntityType = new Mock <EntityType>("E", "ns", DataSpace.CSpace);

            mockEntityType.Setup(m => m.MetadataProperties).Returns(
                new ReadOnlyMetadataCollection <MetadataProperty>(new List <MetadataProperty>()
            {
                property
            }));

            var fixture = new Fixture(2.0);

            fixture.UnderlyingWriter.WriteStartElement(XmlConstants.EntityType);
            fixture.Writer.WriteExtendedProperties(mockEntityType.Object);

            Assert.Equal(@"<EntityType", fixture.ToString());
        }
Example #10
0
        public void WriteExtendedProperties_writes_annotations_using_ToString_if_serializer_not_available()
        {
            var property = MetadataProperty.CreateAnnotation(XmlConstants.UseClrTypesAnnotationWithPrefix, "true");

            var mockEntityType = new Mock <EntityType>("E", "ns", DataSpace.CSpace);

            mockEntityType.Setup(m => m.MetadataProperties).Returns(
                new ReadOnlyMetadataCollection <MetadataProperty>(new List <MetadataProperty>()
            {
                property
            }));

            var fixture = new Fixture(2.0);

            fixture.UnderlyingWriter.WriteStartElement(XmlConstants.EntityType);
            fixture.Writer.WriteExtendedProperties(mockEntityType.Object);

            Assert.Equal(@"<EntityType p1:UseClrTypes=""true""", fixture.ToString());
        }
Example #11
0
        public void WriteExtendedProperties_writes_annotations_on_properties()
        {
            var annotation = MetadataProperty.CreateAnnotation(XmlConstants.CustomAnnotationPrefix + "Foo", "Goo");

            var mockProperty = new Mock <MetadataProperty>();

            mockProperty.Setup(m => m.MetadataProperties).Returns(
                new ReadOnlyMetadataCollection <MetadataProperty>(new List <MetadataProperty>()
            {
                annotation
            }));

            var fixture = new Fixture(2.0);

            fixture.UnderlyingWriter.WriteStartElement(XmlConstants.Property);
            fixture.Writer.WriteExtendedProperties(mockProperty.Object);

            Assert.Equal(@"<Property p1:Foo=""Goo""", fixture.ToString());
        }
Example #12
0
        public void WriteExtendedProperties_does_not_write_store_generated_annotations()
        {
            var annotation = MetadataProperty.CreateAnnotation(XmlConstants.StoreGeneratedPatternAnnotation, "Identity");

            var mockProperty = new Mock <MetadataProperty>();

            mockProperty.Setup(m => m.MetadataProperties).Returns(
                new ReadOnlyMetadataCollection <MetadataProperty>(new List <MetadataProperty>()
            {
                annotation
            }));

            var fixture = new Fixture(2.0);

            fixture.UnderlyingWriter.WriteStartElement(XmlConstants.Property);
            fixture.Writer.WriteExtendedProperties(mockProperty.Object);

            Assert.Equal(@"<Property", fixture.ToString());
        }
        public void WriteComplexTypeElementHeader_writes_annotations()
        {
            var property = MetadataProperty.CreateAnnotation(XmlConstants.ClrTypeAnnotation, typeof(Random));

            var mockComplexType = new Mock <ComplexType>("C", "ns", DataSpace.CSpace);

            mockComplexType.Setup(m => m.Name).Returns("C");
            mockComplexType.Setup(m => m.MetadataProperties).Returns(
                new ReadOnlyMetadataCollection <MetadataProperty>(new List <MetadataProperty>()
            {
                property
            }));

            var fixture = new Fixture(2.0);

            fixture.Writer.WriteComplexTypeElementHeader(mockComplexType.Object);

            Assert.Equal(
                @"<ComplexType Name=""C"" p1:ClrType=""System.Random, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089""",
                fixture.ToString());
        }
Example #14
0
            public void Remove_is_implemented()
            {
                var typeUsage    = TypeUsage.CreateDefaultTypeUsage(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String));
                var metadataItem = new EntityType("E", "N", DataSpace.CSpace);
                var annotation   = MetadataProperty.CreateAnnotation("AN", "AV");

                metadataItem.MetadataProperties.Source.Add(MetadataProperty.Create("PN", typeUsage, "PV"));
                metadataItem.Annotations.Add(annotation);
                var count = metadataItem.MetadataProperties.Count;

                Assert.Equal(1, metadataItem.Annotations.Count);

                metadataItem.Annotations.Remove(annotation);

                Assert.Equal(count - 1, metadataItem.MetadataProperties.Count);
                Assert.Equal(0, metadataItem.Annotations.Count);
                foreach (var p in metadataItem.MetadataProperties)
                {
                    Assert.False(p.IsAnnotation);
                }
            }
Example #15
0
        public void WriteExtendedProperties_writes_annotations_using_serializer_to_serialize_if_available()
        {
            var property = MetadataProperty.CreateAnnotation(XmlConstants.ClrTypeAnnotationWithPrefix, typeof(Random));

            var mockEntityType = new Mock <EntityType>("E", "ns", DataSpace.CSpace);

            mockEntityType.Setup(m => m.MetadataProperties).Returns(
                new ReadOnlyMetadataCollection <MetadataProperty>(new List <MetadataProperty>()
            {
                property
            }));

            var fixture = new Fixture(2.0);

            fixture.UnderlyingWriter.WriteStartElement(XmlConstants.EntityType);
            fixture.Writer.WriteExtendedProperties(mockEntityType.Object);

            Assert.Equal(
                @"<EntityType p1:ClrType=""System.Random, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089""",
                fixture.ToString());
        }
Example #16
0
        public void WriteEnumTypeElementHeader_writes_annotations()
        {
            var property = MetadataProperty.CreateAnnotation(XmlConstants.ClrTypeAnnotationWithPrefix, typeof(Random));

            var mockEnumType = new Mock <EnumType>();

            mockEnumType.Setup(m => m.Name).Returns("E");
            mockEnumType.Setup(m => m.MetadataProperties).Returns(
                new ReadOnlyMetadataCollection <MetadataProperty>(new List <MetadataProperty>()
            {
                property
            }));

            var fixture = new Fixture(2.0);

            fixture.Writer.WriteEnumTypeElementHeader(mockEnumType.Object);

            Assert.Equal(
                @"<EnumType Name=""E"" IsFlags=""false"" p1:ClrType=""System.Random, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"" UnderlyingType=""Int32""",
                fixture.ToString());
        }
Example #17
0
        /// <summary>
        /// Adds or updates an annotation with the specified name and value.
        /// </summary>
        /// <remarks>
        /// If an annotation with the given name already exists then the value of that annotation
        /// is updated to the given value. If the given value is null then the annotation will be
        /// removed.
        /// </remarks>
        /// <param name="name">The name of the annotation property.</param>
        /// <param name="value">The value of the annotation property.</param>
        public void AddAnnotation(string name, object value)
        {
            Check.NotEmpty(name, "name");

            var existingAnnotation = Annotations.FirstOrDefault(a => a.Name == name);

            if (existingAnnotation != null)
            {
                if (value == null)
                {
                    RemoveAnnotation(name);
                }
                else
                {
                    existingAnnotation.Value = value;
                }
            }
            else if (value != null)
            {
                GetMetadataProperties().Add(MetadataProperty.CreateAnnotation(name, value));
            }
        }
Example #18
0
        /// <summary>
        /// Adds an annotation with the specified name and value.
        /// </summary>
        /// <param name="name">The name of the annotation property.</param>
        /// <param name="value">The value of the annotation property.</param>
        public void AddAnnotation(string name, object value)
        {
            Check.NotEmpty(name, "name");

            MetadataProperties.Source.Add(MetadataProperty.CreateAnnotation(name, value));
        }