public void RegisterConfiguredComponents_ConstructorInjection() { var builder = EmbeddedConfiguration.ConfigureContainerWithXml("ComponentRegistrar_SingletonWithTwoServices.xml"); var container = builder.Build(); var cpt = (SimpleComponent)container.Resolve<ITestComponent>(); Assert.Equal(1.234, cpt.Input); }
public void RegisterConfiguredComponents_RegistersMetadata() { var builder = EmbeddedConfiguration.ConfigureContainerWithJson("ComponentRegistrar_ComponentWithMetadata.json"); var container = builder.Build(); Assert.True(container.ComponentRegistry.TryGetRegistration(new KeyedService("a", typeof(object)), out IComponentRegistration registration), "The expected service wasn't registered."); Assert.Equal(42.42, (double)registration.Metadata["answer"]); }
public void RegisterConfiguredComponents_AutoActivationNotEnabledOnComponent() { var builder = EmbeddedConfiguration.ConfigureContainerWithJson("ComponentRegistrar_EnableAutoActivation.json"); var container = builder.Build(); Assert.True(container.ComponentRegistry.TryGetRegistration(new KeyedService("b", typeof(object)), out IComponentRegistration registration), "The expected component was not registered."); Assert.False(registration.Services.Any(a => a.GetType().Name == "AutoActivateService"), "Auto activate service was registered on the component when it shouldn't be."); }
public void RegisterConfiguredComponents_MetadataMissingName() { var builder = EmbeddedConfiguration.ConfigureContainerWithXml("ComponentRegistrar_MetadataMissingName.xml"); var exception = Assert.Throws <InvalidOperationException>(() => builder.Build()); Assert.Equal("The 'metadata' collection should be ordinal (like an array) with items that have numeric names to indicate the index in the collection. 'components:0:metadata' didn't have a numeric name so couldn't be parsed. Check https://autofac.readthedocs.io/en/latest/configuration/xml.html for configuration examples.", exception.Message); }
public void RegisterConfiguredComponents_LifetimeScope_InstancePerDependency() { var builder = EmbeddedConfiguration.ConfigureContainerWithJson("ComponentRegistrar_InstancePerDependency.json"); var container = builder.Build(); Assert.NotSame(container.Resolve <SimpleComponent>(), container.Resolve <SimpleComponent>()); }
public void RegisterConfiguredComponents_ExternalOwnership() { var builder = EmbeddedConfiguration.ConfigureContainerWithJson("ComponentRegistrar_ExternalOwnership.json"); var container = builder.Build(); Assert.True(container.ComponentRegistry.TryGetRegistration(new TypedService(typeof(SimpleComponent)), out IComponentRegistration registration), "The expected component was not registered."); Assert.Equal(InstanceOwnership.ExternallyOwned, registration.Ownership); }
public void RegisterConfiguredComponents_LifetimeScope_Singleton() { var builder = EmbeddedConfiguration.ConfigureContainerWithXml("ComponentRegistrar_SingletonWithTwoServices.xml"); var container = builder.Build(); Assert.Same(container.Resolve <ITestComponent>(), container.Resolve <ITestComponent>()); }
public void InjectsSingleValueWithConversion() { var container = EmbeddedConfiguration.ConfigureContainerWithXml("ConfigurationExtensions_EnumerableParameters.xml").Build(); var poco = container.Resolve <D>(); Assert.True(poco.Num == 123); }
public void GetParameters_ParameterConversionUsesTypeConverterAttribute() { var container = EmbeddedConfiguration.ConfigureContainerWithJson("ConfigurationExtensions_Parameters.json").Build(); var obj = container.Resolve <HasConvertibleParametersAndProperties>(); Assert.NotNull(obj.Parameter); Assert.Equal(1, obj.Parameter.Value); }
public void RegisterConfiguredComponents_SingleComponentWithTwoServices() { var builder = EmbeddedConfiguration.ConfigureContainerWithXml("ComponentRegistrar_SingletonWithTwoServices.xml"); var container = builder.Build(); container.AssertRegistered<ITestComponent>("The ITestComponent wasn't registered."); container.AssertRegistered<object>("The object wasn't registered."); container.AssertNotRegistered<SimpleComponent>("The base SimpleComponent type was incorrectly registered."); Assert.Same(container.Resolve<ITestComponent>(), container.Resolve<object>()); }
public void RegisterConfiguredComponents_PropertyInjectionWithProvidedValues() { var builder = EmbeddedConfiguration.ConfigureContainerWithXml("ComponentRegistrar_SingletonWithTwoServices.xml"); var container = builder.Build(); var cpt = (SimpleComponent)container.Resolve <ITestComponent>(); Assert.Equal("hello", cpt.Message); Assert.True(cpt.ABool, "The Boolean property value was not properly parsed/converted."); }
public void GetProperties_DictionaryPropertyUsesTypeConverterAttribute() { var container = EmbeddedConfiguration.ConfigureContainerWithJson("ConfigurationExtensions_Parameters.json").Build(); var obj = container.Resolve <HasDictionaryProperty>(); Assert.NotNull(obj.Convertible); Assert.Equal(2, obj.Convertible.Count); Assert.Equal(1, obj.Convertible["a"].Value); Assert.Equal(2, obj.Convertible["b"].Value); }
public void ParameterStringListInjectionMultipleConstructors() { var container = EmbeddedConfiguration.ConfigureContainerWithXml("ConfigurationExtensions_EnumerableParameters.xml").Build(); var poco = container.Resolve <L>(); Assert.True(poco.List.Count == 2); Assert.Equal(poco.List[0], "Val1"); Assert.Equal(poco.List[1], "Val2"); }
public void InjectsConstructorParameter() { var container = EmbeddedConfiguration.ConfigureContainerWithXml("ConfigurationExtensions_EnumerableParameters.xml").Build(); var poco = container.Resolve <E>(); Assert.True(poco.List.Count == 2); Assert.Equal(poco.List[0], 1); Assert.Equal(poco.List[1], 2); }
public void FillsNonGenericListWithString() { var container = EmbeddedConfiguration.ConfigureContainerWithXml("ConfigurationExtensions_EnumerableParameters.xml").Build(); var poco = container.Resolve <C>(); Assert.True(poco.List.Count == 2); Assert.Equal(poco.List[0], "1"); Assert.Equal(poco.List[1], "2"); }
public void RegisterConfiguredComponents_PropertyInjectionEnabledOnComponent() { var builder = EmbeddedConfiguration.ConfigureContainerWithJson("ComponentRegistrar_EnablePropertyInjection.json"); builder.RegisterType <SimpleComponent>().As <ITestComponent>(); var container = builder.Build(); var e = container.Resolve <ComponentConsumer>(); Assert.NotNull(e.Component); }
public void ConvertsTypeInList() { var container = EmbeddedConfiguration.ConfigureContainerWithXml("ConfigurationExtensions_EnumerableParameters.xml").Build(); var poco = container.Resolve <B>(); Assert.True(poco.List.Count == 2); Assert.Equal(1.234, poco.List[0]); Assert.Equal(2.345, poco.List[1]); }
public void ParameterStringListInjectionOptionalParameter() { var container = EmbeddedConfiguration.ConfigureContainerWithXml("ConfigurationExtensions_EnumerableParameters.xml").Build(); var poco = container.Resolve <K>(); Assert.True(poco.List.Count == 2); Assert.Equal("Val1", poco.List[0]); Assert.Equal("Val2", poco.List[1]); }
public void RegisterConfiguredModules_ComplexPropertyType() { var builder = EmbeddedConfiguration.ConfigureContainerWithJson("ModuleConfiguration_ComplexType.json"); var container = builder.Build(); var poco = container.Resolve <ComplexPropertyComponent>(); Assert.Equal(2, poco.List.Count()); Assert.Equal("Val3", poco.List[0]); Assert.Equal("Val4", poco.List[1]); }
public void InjectsGenericCollection() { var container = EmbeddedConfiguration.ConfigureContainerWithXml("ConfigurationExtensions_EnumerableParameters.xml").Build(); var poco = container.Resolve <I>(); Assert.NotNull(poco.Collection); Assert.True(poco.Collection.Count == 2); Assert.Equal(poco.Collection.First(), 1); Assert.Equal(poco.Collection.Last(), 2); }
public void RegisterConfiguredComponents_MemberOf() { var builder = EmbeddedConfiguration.ConfigureContainerWithJson("ComponentRegistrar_MemberOf.json"); builder.RegisterCollection <ITestComponent>("named-collection").As <IList <ITestComponent> >(); var container = builder.Build(); var collection = container.Resolve <IList <ITestComponent> >(); var first = collection[0]; Assert.IsType <SimpleComponent>(first); }
public void RegisterConfiguredComponents_AllowsMultipleRegistrationsOfSameType() { var builder = EmbeddedConfiguration.ConfigureContainerWithJson("ComponentRegistrar_SameTypeRegisteredMultipleTimes.json"); var container = builder.Build(); var collection = container.Resolve<IEnumerable<SimpleComponent>>(); Assert.Equal(2, collection.Count()); // Test using Any() because we aren't necessarily guaranteed the order of resolution. Assert.True(collection.Any(a => a.Input == 5.123), "The first registration (5.123) wasn't found."); Assert.True(collection.Any(a => a.Input == 10.234), "The second registration (10.234) wasn't found."); }
public void GetProperties_ListPropertyEmpty() { var config = EmbeddedConfiguration.LoadJson("ConfigurationExtensions_Parameters.json"); var component = config.GetSection("components").GetChildren().Where(kvp => kvp["type"] == typeof(HasEnumerableProperty).FullName).First(); var property = typeof(HasEnumerableProperty).GetProperty("Empty"); var provider = (Func <object>)null; var parameter = component.GetProperties("properties").Cast <Parameter>().FirstOrDefault(rp => rp.CanSupplyValue(property.SetMethod.GetParameters().First(), new ContainerBuilder().Build(), out provider)); // Gotcha in ConfigurationModel - if the list/dictionary is empty // then configuration won't see it or add the key to the list. Assert.Null(parameter); }
public void GetProperties_ListConversionUsesTypeConverterAttribute() { var container = EmbeddedConfiguration.ConfigureContainerWithJson("ConfigurationExtensions_Parameters.json").Build(); var obj = container.Resolve <HasEnumerableProperty>(); Assert.NotNull(obj.Convertible); var convertible = obj.Convertible.ToArray(); Assert.Equal(2, convertible.Length); Assert.Equal(1, convertible[0].Value); Assert.Equal(2, convertible[1].Value); }
public void GetProperties_SimpleProperties(string propertyName, object expectedValue) { var config = EmbeddedConfiguration.LoadJson("ConfigurationExtensions_Parameters.json"); var component = config.GetSection("components").GetChildren().Where(kvp => kvp["type"] == typeof(HasSimpleParametersAndProperties).FullName).First(); var property = typeof(HasSimpleParametersAndProperties).GetProperties().First(pi => pi.Name == propertyName); var provider = (Func <object>)null; var parameter = component.GetProperties("properties").Cast <Parameter>().FirstOrDefault(rp => rp.CanSupplyValue(property.SetMethod.GetParameters().First(), new ContainerBuilder().Build(), out provider)); Assert.NotNull(parameter); Assert.NotNull(provider); Assert.Equal(expectedValue, provider()); }
public void InjectsConcreteDictionary() { var container = EmbeddedConfiguration.ConfigureContainerWithXml("ConfigurationExtensions_DictionaryParameters.xml").Build(); var poco = container.Resolve <D>(); Assert.True(poco.Dictionary.Count == 2); Assert.True(poco.Dictionary.ContainsKey("Key1")); Assert.True(poco.Dictionary.ContainsKey("Key2")); Assert.Equal("Val1", poco.Dictionary["Key1"]); Assert.Equal("Val2", poco.Dictionary["Key2"]); }
public void RegisterConfiguredComponents_PropertyInjectionEnabledOnComponent() { var builder = EmbeddedConfiguration.ConfigureContainerWithJson("ComponentRegistrar_EnablePropertyInjection.json"); builder.RegisterType<SimpleComponent>().As<ITestComponent>(); builder.RegisterInstance("hello").As<string>(); var container = builder.Build(); var e = container.Resolve<ComponentConsumer>(); Assert.NotNull(e.Component); // Issue #2 - Ensure properties in base classes can be set by config. Assert.Equal("hello", e.Message); }
public void NumericKeysZeroBasedListConvertedToDictionary() { var container = EmbeddedConfiguration.ConfigureContainerWithXml("ConfigurationExtensions_DictionaryParameters.xml").Build(); var poco = container.Resolve <E>(); Assert.True(poco.Dictionary.Count == 2); Assert.True(poco.Dictionary.ContainsKey(0)); Assert.True(poco.Dictionary.ContainsKey(1)); Assert.Equal("Val1", poco.Dictionary[0]); Assert.Equal("Val2", poco.Dictionary[1]); }
public void ConvertsDictionaryValue() { var container = EmbeddedConfiguration.ConfigureContainerWithXml("ConfigurationExtensions_DictionaryParameters.xml").Build(); var poco = container.Resolve <F>(); Assert.True(poco.Dictionary.Count == 2); Assert.True(poco.Dictionary.ContainsKey("Key1")); Assert.True(poco.Dictionary.ContainsKey("Key2")); Assert.Equal(1, poco.Dictionary["Key1"]); Assert.Equal(2, poco.Dictionary["Key2"]); }
public void InjectsGenericIEnumerable() { var container = EmbeddedConfiguration.ConfigureContainerWithXml("ConfigurationExtensions_EnumerableParameters.xml").Build(); var poco = container.Resolve <H>(); Assert.NotNull(poco.Enumerable); var enumerable = poco.Enumerable.ToList(); Assert.True(enumerable.Count == 2); Assert.Equal(enumerable[0], 1); Assert.Equal(enumerable[1], 2); }