protected static void OnTestInitialize(DiImplementationType diImplementationType) { TestsHelper.SetupLogger(); CustomAutoServiceCodeGenerator.ReferencedAssemblyPathsToAddOnCodeGeneration.Clear(); _diImplementationType = diImplementationType; }
private object CreateNativeModule(DiImplementationType diImplementationType) { string classFullName = null; var assemblyPath = Path.Combine(DiManagerHelpers.DynamicallyLoadedDllsFolder, "TestProjects.Modules.dll"); var constructorParameters = new ParameterInfo[0]; var probingPaths = new List <string> { DiManagerHelpers.ThirdPartyLibsFolder, DiManagerHelpers.DynamicallyLoadedDllsFolder }; switch (diImplementationType) { case DiImplementationType.Autofac: classFullName = "Modules.Autofac.AutofacModule1"; constructorParameters = new[] { new ParameterInfo(typeof(int), 15) }; break; case DiImplementationType.Ninject: classFullName = "Modules.Ninject.NinjectModule1"; constructorParameters = new[] { new ParameterInfo(typeof(int), 15) }; break; } using (new AssemblyResolver(probingPaths)) { return(CreateObject <object>(classFullName, assemblyPath, constructorParameters)); } }
public void TestFileBasedDiContainerBuilderWithNoDiContainerProvided(DiImplementationType diImplementationType, bool isLoggerNotSetTest) { RuntTests(isLoggerNotSetTest, () => { var diContainerBuilder = new DiContainerBuilder.DiContainerBuilder(); using (var containerInfo = diContainerBuilder.StartFileBasedDi( new FileBasedConfigurationParameters(new FileBasedConfigurationFileContentsProvider( Path.Combine(Helpers.TestsEntryAssemblyFolder, "IoCConfiguration_Overview.xml")), Helpers.TestsEntryAssemblyFolder, new LoadedAssembliesForTests()) { AttributeValueTransformers = new [] { new FileFolderPathAttributeValueTransformer() }, ConfigurationFileXmlDocumentLoaded = (sender, e) => { Helpers.EnsureConfigurationDirectoryExistsOrThrow(e.XmlDocument.SelectElement("/iocConfiguration/appDataDir").GetAttribute("path")); ConfigurationFileXmlDocumentLoadedEventHandler(diImplementationType, e); } }, out _) .WithoutPresetDiContainer() .AddAdditionalDiModules(new DiModule1()) .AddNativeModules(CreateNativeModule(diImplementationType)) .RegisterModules() .Start()) { ValidateFileBasedDiContainer(diImplementationType, containerInfo.DiContainer); } }); }
public void NonExistentServiceTypeReference(DiImplementationType diImplementationType) { TestExpectedConfigurationParseException(diImplementationType, xmlDocument => xmlDocument.SelectElement("/iocConfiguration/dependencyInjection/autoGeneratedServices/autoServiceCustom", xmlElement => xmlElement.GetAttribute("interface") == typeof(IAuthorsRepository).FullName) .SetAttribute("interface", typeof(IAuthorsRepository).FullName + "Invalid"), "Type 'IoC.Configuration.Tests.AutoServiceCustom.SimpleDataRepository.DataRepositories.IAuthorsRepositoryInvalid' was not found."); }
public void ImplementedServiceDoesNotHavePublicAccess(DiImplementationType diImplementationType) { TestExpectedConfigurationParseException(diImplementationType, xmlDocument => xmlDocument.SelectElement("/iocConfiguration/dependencyInjection/autoGeneratedServices/autoServiceCustom", xmlElement => xmlElement.GetAttribute("interface") == typeof(DogAbstr).FullName) .SetAttribute("interface", typeof(AbstractDogWithNonPublicAccessLevel).FullName), "Service type 'IoC.Configuration.Tests.AutoServiceCustom.ClassesForFailureTests.AbstractDogWithNonPublicAccessLevel' specified in element 'autoServiceCustom' should have public access."); }
public void TypeRefInSettingsTest(DiImplementationType diImplementationType) { LoadConfigurationFileAndRunTest(diImplementationType, (diContainer, configuration) => { var settings = diContainer.Resolve <ISettings>(); settings.GetSettingValueOrThrow <Generic1_1 <Interface1_Impl1> >("GenericSetting1"); }); }
private void ValidateFileBasedDiContainer(DiImplementationType diImplementationType, IDiContainer diContainer) { Assert.IsNotNull(diContainer.Resolve <ISettings>()); Assert.IsNotNull(diContainer.Resolve <IPluginsSetup>()); Assert.IsNotNull(diContainer.Resolve <IPluginDataRepository>()); Assert.IsNotNull(diContainer.Resolve <IOnApplicationsStarted>()); ValidateDiContainer(diImplementationType, diContainer); }
protected static void ClassInitializeCommon(DiImplementationType diImplementationType, Action <ConfigurationFileXmlDocumentLoadedEventArgs> configurationFileXmlDocumentLoadedEventHandler) { // JetBrains.ReSharper.TestRunner.Merged TestsHelper.SetupLogger(); Log4Tests.LogLevel = LogLevel.Debug; _diImplementationType = diImplementationType; _mainLifeTimeScopeTerminatedExecuted = false; var diContainerBuilder = new DiContainerBuilder.DiContainerBuilder(); var fileBasedConfigurationParameters = new FileBasedConfigurationParameters(new FileBasedConfigurationFileContentsProvider( Path.Combine(Helpers.TestsEntryAssemblyFolder, "IoCConfiguration_Overview.xml")), Helpers.TestsEntryAssemblyFolder, new LoadedAssembliesForTests()) { AttributeValueTransformers = new[] { new FileFolderPathAttributeValueTransformer() }, ConfigurationFileXmlDocumentLoaded = (sender, e) => { Helpers.EnsureConfigurationDirectoryExistsOrThrow(e.XmlDocument.SelectElement("/iocConfiguration/appDataDir").GetAttribute("path")); Helpers.ReplaceActiveDiManagerInConfigurationFile(e.XmlDocument, _diImplementationType); configurationFileXmlDocumentLoadedEventHandler?.Invoke(e); } }; _containerInfo = diContainerBuilder.StartFileBasedDi( fileBasedConfigurationParameters, out _) .WithoutPresetDiContainer() .AddAdditionalDiModules(new TestModule2()) .RegisterModules() .Start(); _diContainer = _containerInfo.DiContainer; var injectedStartupActionsHelper = _diContainer.Resolve <ClassToTestServicesInjection <IStartupAction> >(); _startupActionsToTest = injectedStartupActionsHelper.Implementations.Where(x => x is IStartupActionState && new[] { "DynamicallyLoadedAssembly1.Implementations.StartupAction1", "DynamicallyLoadedAssembly1.Implementations.StartupAction2" } .Contains(x.GetType().FullName)).ToList(); var injectedPluginsHelper = _diContainer.Resolve <ClassToTestServicesInjection <IPlugin> >(); _pluginsToTest = injectedPluginsHelper.Implementations.Where(x => x is IPluginState && new[] { "TestPluginAssembly1.Implementations.Plugin1", "TestPluginAssembly2.Implementations.Plugin2" } .Contains(x.GetType().FullName)).ToList(); _diContainer.MainLifeTimeScope.LifeTimeScopeTerminated += (sender, e) => { _mainLifeTimeScopeTerminatedExecuted = true; }; }
public void ICustomAutoServiceCodeGenerator_Validate_ThrowsException(DiImplementationType diImplementationType) { TestExpectedConfigurationParseException(diImplementationType, xmlDocument => xmlDocument.SelectElement("/iocConfiguration/dependencyInjection/autoGeneratedServices/autoServiceCustom", xmlElement => xmlElement.GetAttribute("interface") == typeof(DogAbstr).FullName) .SelectChildElement("autoServiceCodeGenerator/constructedValue/parameters/boolean", xmlElement => xmlElement.GetAttribute("name") == "simulateValidateFails") .SetAttribute("value", "true"), "Call to 'IoC.Configuration.Tests.AutoServiceCustom.ClassesForFailureTests.CustomAutoServiceCodeGeneratorForFailureTests.Validate(IoC.Configuration.ICustomAutoGeneratedServiceInfo)' failed."); }
public void TestCodeBasedDiContainerBuilder(DiImplementationType diImplementationType, IEnumerable <string> assemblyProbingPaths, bool isPresetDiContainer) { var diImplementationInfo = DiManagerHelpers.GetDiImplementationInfo(diImplementationType); IDiContainer presetDiContainer = null; var diContainerBuilder = new DiContainerBuilder.DiContainerBuilder(); var diContainerConfigurator = diContainerBuilder.StartCodeBasedDi(diImplementationInfo.DiManagerClassName, diImplementationInfo.DiManagerAssemblyPath, new ParameterInfo[0], Helpers.TestsEntryAssemblyFolder, assemblyProbingPaths.ToArray()); ICodeBasedDiModulesConfigurator diModulesConfigurator = null; if (isPresetDiContainer) { presetDiContainer = CreateDiContainer(diImplementationType); diModulesConfigurator = diContainerConfigurator.WithDiContainer(presetDiContainer); } else { diModulesConfigurator = diContainerConfigurator.WithoutPresetDiContainer(); } string nativeModuleClassName = null; var nativeModuleClassAssemblyFilePath = Path.Combine(DiManagerHelpers.DynamicallyLoadedDllsFolder, "TestProjects.Modules.dll"); switch (diImplementationType) { case DiImplementationType.Autofac: nativeModuleClassName = "Modules.Autofac.AutofacModule1"; break; case DiImplementationType.Ninject: nativeModuleClassName = "Modules.Ninject.NinjectModule1"; break; } using (var containerInfo = diModulesConfigurator .AddNativeModule(nativeModuleClassName, nativeModuleClassAssemblyFilePath, new[] { new ParameterInfo(typeof(int), 18) }) .AddDiModules(new DiModule1()) .RegisterModules() .Start()) { if (isPresetDiContainer) { Assert.AreSame(presetDiContainer, containerInfo.DiContainer); } ValidateDiContainer(diImplementationType, containerInfo.DiContainer); } }
protected void LoadConfigurationFile(DiImplementationType diImplementationType, Action <IDiContainer, IConfiguration> onConfigurationLoaded, [CanBeNull] IDiModule[] additionalModulesToLoad = null, [CanBeNull] Action <XmlDocument> modifyConfigurationFileOnLoad = null) { var loadData = Helpers.LoadConfigurationFile(diImplementationType, GetConfigurationRelativePath(), additionalModulesToLoad, modifyConfigurationFileOnLoad); using (var containerInfo = loadData.containerInfo) { onConfigurationLoaded(containerInfo.DiContainer, containerInfo.DiContainer.Resolve <IConfiguration>()); } }
public void InvalidPropertyReturnValue_1(DiImplementationType diImplementationType) { Helpers.TestExpectedConfigurationParseException(() => LoadConfigurationFile(diImplementationType, (xmlDocument) => { xmlDocument.SelectElement("/iocConfiguration/dependencyInjection/autoGeneratedServices/autoService/autoProperty/object", xmlElement => xmlElement.GetAttribute(ConfigurationFileAttributeNames.TypeRef) == "Guid") .SetAttributeValue(ConfigurationFileAttributeNames.Value, "1"); }), typeof(IReturnValueElement)); }
public void NonExistentServiceTypeReference(DiImplementationType diImplementationType) { Helpers.TestExpectedConfigurationParseException(() => LoadConfigurationFile(diImplementationType, (xmlDocument) => { xmlDocument.SelectElement("/iocConfiguration/dependencyInjection/autoGeneratedServices/autoService", xmlElement => xmlElement.GetAttribute("interfaceRef") == "IProjectGuids") .SetAttribute("interfaceRef", "IProjectGuidsInvalid"); }), typeof(IAutoGeneratedServiceElement)); }
public void NonPublicService(DiImplementationType diImplementationType) { Helpers.TestExpectedConfigurationParseException(() => LoadConfigurationFile(diImplementationType, (xmlDocument) => { xmlDocument.SelectElement("/iocConfiguration/dependencyInjection/autoGeneratedServices") .InsertChildElement(ConfigurationFileElementNames.AutoService) .SetAttribute(ConfigurationFileAttributeNames.Interface, typeof(INonPublicInterface).FullName); }), typeof(IAutoGeneratedServiceElement)); }
public DiImplementationInfo(DiImplementationType diImplementationType, [NotNull] string diManagerFolder, [NotNull] string diManagerAssemblyPath, [NotNull] string diManagerClassName, [NotNull] string diContainerClassName) { DiImplementationType = diImplementationType; DiManagerFolder = diManagerFolder; DiManagerAssemblyPath = diManagerAssemblyPath; DiManagerClassName = diManagerClassName; DiContainerClassName = diContainerClassName; }
public void ProxyServiceIsNotAssignableFromServiceToProxy(DiImplementationType diImplementationType) { Helpers.TestExpectedConfigurationParseException(() => LoadConfigurationFile(diImplementationType, (xmlDocument) => { xmlDocument.SelectElement("/iocConfiguration/dependencyInjection/services/proxyService", xmlElement => xmlElement.GetAttribute(ConfigurationFileAttributeNames.Type) == typeof(IAppManager).FullName) .SetAttributeValue(ConfigurationFileAttributeNames.Type, typeof(IAppData).FullName); }), typeof(IServiceToProxyImplementationElement), typeof(ProxyServiceElement)); }
public void NonInterfaceServiceType(DiImplementationType diImplementationType) { Helpers.TestExpectedConfigurationParseException(() => LoadConfigurationFile(diImplementationType, (xmlDocument) => { xmlDocument.SelectElement("/iocConfiguration/dependencyInjection/autoGeneratedServices/autoService", xmlElement => xmlElement.GetAttribute("interfaceRef") == "IProjectGuids") .Remove("interfaceRef") .SetAttribute("interface", "IoC.Configuration.Tests.AutoService.Services.ActionValidator1"); }), typeof(IAutoGeneratedServiceElement)); }
public void InvalidParameterNameInParameterValueElement(DiImplementationType diImplementationType) { Helpers.TestExpectedConfigurationParseException(() => LoadConfigurationFile(diImplementationType, (xmlDocument) => { xmlDocument.SelectElement("/iocConfiguration/dependencyInjection/autoGeneratedServices/autoService", xmlElement => xmlElement.GetAttribute(ConfigurationFileAttributeNames.Interface) == typeof(IAppInfoFactory).FullName) .SelectChildElement("autoMethod/default/constructedValue/parameters/parameterValue") .SetAttribute(ConfigurationFileAttributeNames.ParamName, "appId_Invalid"); }), typeof(ParameterElement)); }
public void GenericTypeTest1(DiImplementationType diImplementationType) { LoadConfigurationFileAndRunTest(diImplementationType, (diContainer, configuration) => { Assert.IsNotNull(configuration.DependencyInjection.Services.AllServices.FirstOrDefault(x => x.ServiceTypeInfo.Type == typeof(IGeneric2_1 <Generic3_1 <int> >))); var implementation = diContainer.Resolve <IGeneric2_1 <Generic3_1 <int> > >(); Assert.IsInstanceOf <Generic2_1 <Generic3_1 <int> > >(implementation); Assert.AreEqual(17, implementation.Value.Value); }, false); }
public void TypeRefInServiceAndImplementationsTest(DiImplementationType diImplementationType) { LoadConfigurationFileAndRunTest(diImplementationType, (diContainer, configuration) => { var serviceElement = configuration.DependencyInjection.Services.AllServices.First(x => x.ServiceTypeInfo.Type == typeof(IGeneric1_1 <Interface1_Impl1>)); var servicesInjectionTester = diContainer.Resolve <ClassToTestServicesInjection <IGeneric1_1 <Interface1_Impl1> > >(); Assert.AreEqual(3, servicesInjectionTester.Implementations.Count); Assert.AreEqual(3, servicesInjectionTester.Implementations.Where(x => x is Generic1_1 <Interface1_Impl1>).ToList().Count); }); }
public void InvalidMethodReturnValue_2(DiImplementationType diImplementationType) { Helpers.TestExpectedConfigurationParseException(() => LoadConfigurationFile(diImplementationType, (xmlDocument) => { xmlDocument.SelectElement("/iocConfiguration/dependencyInjection/autoGeneratedServices/autoService/autoMethod/default/object", xmlElement => xmlElement.GetAttribute(ConfigurationFileAttributeNames.TypeRef) == "Guid") .Remove(ConfigurationFileAttributeNames.TypeRef) .SetAttributeValue(ConfigurationFileAttributeNames.Type, typeof(System.Int32).FullName) .SetAttribute(ConfigurationFileAttributeNames.Value, "1"); }), typeof(IReturnValueElement)); }
public void InvalidMethodParameterType(DiImplementationType diImplementationType) { Helpers.TestExpectedConfigurationParseException(() => LoadConfigurationFile(diImplementationType, (xmlDocument) => { xmlDocument.SelectElement("/iocConfiguration/dependencyInjection/autoGeneratedServices/autoService/autoMethod/methodSignature/object", xmlElement => xmlElement.GetAttribute(ConfigurationFileAttributeNames.ParamName) == "projectGuid" && xmlElement.GetAttribute(ConfigurationFileAttributeNames.Type) == typeof(Guid).FullName) .SetAttribute(ConfigurationFileAttributeNames.Type, typeof(int).FullName); }), typeof(IAutoGeneratedServiceMethodElement)); }
public void PropertyAddedMultipleTimes(DiImplementationType diImplementationType) { Helpers.TestExpectedConfigurationParseException(() => LoadConfigurationFile(diImplementationType, (xmlDocument) => { var autoPropertyElement = xmlDocument.SelectElement("/iocConfiguration/dependencyInjection/autoGeneratedServices/autoService/autoProperty", xmlElement => xmlElement.GetAttribute(ConfigurationFileAttributeNames.Name) == "Project1"); var autoPropertyElementClone = autoPropertyElement.CloneNode(true); autoPropertyElement.ParentNode.AppendChild(autoPropertyElementClone); }), typeof(IAutoGeneratedServicePropertyElement), typeof(IAutoGeneratedServiceElement)); }
public void NonExistentSettingValueAsImplementation(DiImplementationType diImplementationType) { Helpers.TestExpectedConfigurationParseException(() => LoadConfigurationFile(diImplementationType, (xmlDocument) => { xmlDocument.SelectElement("/iocConfiguration/dependencyInjection/services/service/valueImplementation/settingValue", (xmlElement) => { return(xmlElement.GetAttribute(ConfigurationFileAttributeNames.SettingName) == "defaultAppId"); }) .SetAttributeValue(ConfigurationFileAttributeNames.SettingName, "defaultAppId_Invalid"); }), typeof(SettingValueElement)); }
public void InvalidPropertyName(DiImplementationType diImplementationType) { Helpers.TestExpectedConfigurationParseException(() => LoadConfigurationFile(diImplementationType, (xmlDocument) => { xmlDocument.SelectElement("/iocConfiguration/dependencyInjection/autoGeneratedServices/autoService", xmlElement => xmlElement.GetAttribute(ConfigurationFileAttributeNames.InterfaceRef) == "IProjectGuids") .SelectChildElement("autoProperty", (xmlElement) => xmlElement.GetAttribute(ConfigurationFileAttributeNames.Name) == "Project1") .SetAttribute(ConfigurationFileAttributeNames.Name, "NonExistentProperty"); }), typeof(IAutoGeneratedServicePropertyElement)); }
public void ICustomAutoServiceCodeGenerator_GenerateCSharp_ThrowsException(DiImplementationType diImplementationType) { TestExpectedConfigurationParseException(diImplementationType, xmlDocument => { xmlDocument.SelectElement("/iocConfiguration/dependencyInjection/autoGeneratedServices/autoServiceCustom", xmlElement => xmlElement.GetAttribute("interface") == typeof(DogAbstr).FullName) .SelectChildElement("autoServiceCodeGenerator/constructedValue/parameters/boolean", xmlElement => xmlElement.GetAttribute("name") == "simulateGenerateCSharpFailure") .SetAttribute("value", "true"); }, "Call to 'IoC.Configuration.ConfigurationFile.IAutoGeneratedServiceElementBase.GenerateAutoImplementedServiceClassCSharp' failed."); }
public void TypeRefInModuleTest(DiImplementationType diImplementationType) { LoadConfigurationFileAndRunTest(diImplementationType, (diContainer, configuration) => { var expectedModuleType = _modulesAssembly.GetType("Modules.IoC.DiModule2"); var diModule = configuration.DependencyInjection.Modules.Modules.FirstOrDefault(x => x.DiModule.GetType() == expectedModuleType).DiModule; Assert.IsNotNull(diModule); Assert.AreSame(typeof(Interface1_Impl1), diModule.GetType().GetProperty("Property1").GetValue(diModule).GetType()); }); }
public void CollectionTypeIsInvalidForParameter(DiImplementationType diImplementationType) { Helpers.TestExpectedConfigurationParseException(() => LoadConfigurationFile(diImplementationType, (xmlDocument) => { xmlDocument.SelectElement("/iocConfiguration/settings/constructedValue/parameters/collection", (xmlElement) => { return(xmlElement.GetAttribute(ConfigurationFileAttributeNames.Name) == "intValues"); }) .SetAttributeValue(ConfigurationFileAttributeNames.CollectionType, "enumerable"); }), typeof(SettingElement)); }
public void TypeRefInPluginModuleTest(DiImplementationType diImplementationType) { LoadConfigurationFileAndRunTest(diImplementationType, (diContainer, configuration) => { var expectedModuleType = _plugin1ModulesAssembly.GetType("ModulesForPlugin1.IoC.DiModule2"); var diModule = configuration.PluginsSetup.GetPluginSetup(Plugin1Name).DependencyInjection.Modules.Modules.FirstOrDefault(x => x.DiModule.GetType() == expectedModuleType).DiModule; Assert.IsNotNull(diModule); Assert.AreSame(_testPluginAssembly1.GetType("TestPluginAssembly1.Implementations.Door"), diModule.GetType().GetProperty("Property1").GetValue(diModule).GetType()); }); }
public void ClassMemberWithParametersDoesNotExist_ClassMemberElementTest(DiImplementationType diImplementationType) { Helpers.TestExpectedConfigurationParseException(() => LoadConfigurationFile(diImplementationType, (xmlDocument) => { xmlDocument.SelectElement("/iocConfiguration/dependencyInjection/autoGeneratedServices/autoService/autoProperty/collection/classMember", (xmlElement) => { return(xmlElement.GetAttribute(ConfigurationFileAttributeNames.DeclaringClass) == typeof(IAppInfoFactory).FullName); }) .SelectChildElement("parameters").RemoveChildElement("string"); }), typeof(CollectionItemValueElement)); }