示例#1
0
        public void LoadInstanceReturnsNullGivenEmptyXmlElementForReferenceType()
        {
            var    definition = new XElement("Definition");
            object instance   = TestableTelemetryConfigurationFactory.LoadInstance(definition, typeof(string), "Test Value");

            Assert.Null(instance);
        }
示例#2
0
        public void LoadInstanceReturnsOriginalValueGivenNullXmlElement()
        {
            var    original = "Test Value";
            object loaded   = TestableTelemetryConfigurationFactory.LoadInstance(null, original.GetType(), original);

            Assert.Same(original, loaded);
        }
示例#3
0
        public void LoadInstanceConvertsValueToExpectedTypeGivenXmlDefinitionWithNoChildElements()
        {
            var    definition = new XElement("Definition", "42");
            object instance   = TestableTelemetryConfigurationFactory.LoadInstance(definition, typeof(int), null);

            Assert.Equal(42, instance);
        }
示例#4
0
        public void LoadInstanceReturnsInstanceOfTypeSpecifiedInTypeAttributeOfGivenXmlDefinition()
        {
            var    definition = new XElement("Definition", new XAttribute("Type", typeof(StubClassWithProperties).AssemblyQualifiedName));
            object instance   = TestableTelemetryConfigurationFactory.LoadInstance(definition, typeof(StubClassWithProperties), null);

            Assert.Equal(typeof(StubClassWithProperties), instance.GetType());
        }
示例#5
0
        public void LoadInstanceReturnsDefaultValueGivenValueEmptyXmlElementForValueType()
        {
            var    definition = new XElement("Definition");
            object instance   = TestableTelemetryConfigurationFactory.LoadInstance(definition, typeof(int), 12);

            Assert.Equal(0, instance);
        }
示例#6
0
        public void LoadInstanceThrowsInvalidOperationExceptionWhenDefinitionElementDoesNotHaveTypeAttributeAndInstanceIsNotInitialized()
        {
            var elementWithoutType = new XElement("Add", new XElement("PropertyName"));
            var exception          = Assert.Throws <InvalidOperationException>(() => TestableTelemetryConfigurationFactory.LoadInstance(elementWithoutType, typeof(IComparable), null));

            Assert.Contains(elementWithoutType.Name.ToString(), exception.Message, StringComparison.OrdinalIgnoreCase);
            Assert.Contains("Type", exception.Message, StringComparison.OrdinalIgnoreCase);
        }
示例#7
0
        public void LoadInstanceTrimsValueOfGivenXmlElementToIgnoreWhitespaceUsersMayAddToConfiguration()
        {
            string expected   = Guid.NewGuid().ToString();
            var    definition = new XElement("InstrumentationKey", "\n" + expected + "\n");

            object actual = TestableTelemetryConfigurationFactory.LoadInstance(definition, typeof(string), null);

            Assert.Equal(expected, actual);
        }
示例#8
0
        public void LoadInstanceSetsInstancePropertiesOfTimeSpanTypeFromChildElementValuesOfDefinitionWithInvalidFormatThrowsException()
        {
            var definition = new XElement(
                "Definition",
                new XAttribute("Type", typeof(StubClassWithProperties).AssemblyQualifiedName),
                new XElement("TimeSpanProperty", "TestValue"));

            Assert.Throws <FormatException>(() => TestableTelemetryConfigurationFactory.LoadInstance(definition, typeof(StubClassWithProperties), null));
        }
示例#9
0
        public void LoadInstanceThrowsInvalidOperationExceptionWhenDefinitionElementContainsInvalidContentAndNoTypeAttribute()
        {
            var definition = new XElement("InvalidElement", "InvalidText");
            var exception  = Assert.Throws <InvalidOperationException>(
                () => TestableTelemetryConfigurationFactory.LoadInstance(definition, typeof(ITelemetryChannel), null));

            Assert.Contains("InvalidElement", exception.Message, StringComparison.OrdinalIgnoreCase);
            Assert.Contains("InvalidText", exception.Message, StringComparison.OrdinalIgnoreCase);
            Assert.NotNull(exception.InnerException);
        }
示例#10
0
        public void LoadInstanceCreatesNewInstanceOfExpectedTypeWhenPropertiesAreSpecifiedOnlyAsAttributes()
        {
            var definition = new XElement("Definition", new XAttribute("Int32Property", 42));

            object instance = TestableTelemetryConfigurationFactory.LoadInstance(definition, typeof(StubClassWithProperties), null);

            var loaded = Assert.IsType <StubClassWithProperties>(instance);

            Assert.Equal(42, loaded.Int32Property);
        }
示例#11
0
        public void LoadInstanceHandlesEnumPropertiesWithEnumerationValueName()
        {
            var definition = new XElement(
                "Definition",
                new XElement("EnumProperty", "Informational"));

            var    original = new StubClassWithProperties();
            object instance = TestableTelemetryConfigurationFactory.LoadInstance(definition, typeof(StubClassWithProperties), original, null);

            Assert.Equal(System.Diagnostics.Tracing.EventLevel.Informational, original.EnumProperty);
        }
示例#12
0
        public void LoadInstanceSetsInstancePropertiesOfTimeSpanTypeFromChildElementValuesOfDefinitionWithOneInteger()
        {
            var definition = new XElement(
                "Definition",
                new XAttribute("Type", typeof(StubClassWithProperties).AssemblyQualifiedName),
                new XElement("TimeSpanProperty", "7"));

            object instance = TestableTelemetryConfigurationFactory.LoadInstance(definition, typeof(StubClassWithProperties), null);

            Assert.Equal(TimeSpan.FromDays(7), ((StubClassWithProperties)instance).TimeSpanProperty);
        }
示例#13
0
        public void LoadInstanceSetsInstancePropertiesFromChildElementValuesOfDefinition()
        {
            var definition = new XElement(
                "Definition",
                new XAttribute("Type", typeof(StubClassWithProperties).AssemblyQualifiedName),
                new XElement("StringProperty", "TestValue"));

            object instance = TestableTelemetryConfigurationFactory.LoadInstance(definition, typeof(StubClassWithProperties), null);

            Assert.Equal("TestValue", ((StubClassWithProperties)instance).StringProperty);
        }
示例#14
0
        public void LoadInstanceInitializesGivenInstanceAndDoesNotRequireSpecifyingTypeAttributeToSimplifyConfiguration()
        {
            var definition = new XElement(
                "Definition",
                new XElement("StringProperty", "TestValue"));

            var    original = new StubClassWithProperties();
            object instance = TestableTelemetryConfigurationFactory.LoadInstance(definition, typeof(StubClassWithProperties), original);

            Assert.Equal("TestValue", original.StringProperty);
        }
        public void LoadInstanceHandlesEnumPropertiesWithNumericValue()
        {
            var definition = new XElement(
                "Definition",
                new XElement("EnumProperty", "3"));

            var    original = new StubClassWithProperties();
            object instance = TestableTelemetryConfigurationFactory.LoadInstance(definition, typeof(StubClassWithProperties), original, null);

            Assert.Equal(EventLevel.Warning, original.EnumProperty);
        }
        public void LoadInstanceReturnsNullWhenDefinitionElementDoesNotHaveTypeAttributeAndInstanceIsNotInitialized()
        {
            var elementWithoutType = new XElement("Add", new XElement("PropertyName"));

            Assert.Null(TestableTelemetryConfigurationFactory.LoadInstance(elementWithoutType, typeof(IComparable), null, null));
        }
        public void LoadInstanceReturnsNullWhenDefinitionElementContainsInvalidContentAndNoTypeAttribute()
        {
            var definition = new XElement("InvalidElement", "InvalidText");

            Assert.Null(TestableTelemetryConfigurationFactory.LoadInstance(definition, typeof(ITelemetryChannel), null, null));
        }