public void Execute_ResultAlreadySet_DoesNotRunLoader() { //Arrange var resolver = Substitute.For <IDependencyResolver>(); var context = Context.Create(resolver); var typeContext = new TestTypeCreationContext(); typeContext.Options = new TestGetOptions() { Type = typeof(StubClass) }; var args = new ConfigurationResolverArgs(context, typeContext, null); args.Result = new StubTypeConfiguration(); var task = new ConfigurationOnDemandResolverTask <StubTypeConfiguration>(); Assert.AreEqual(0, context.TypeConfigurations.Count); //Act task.Execute(args); //Assert Assert.AreEqual(0, context.TypeConfigurations.Count); }
public void Execute_RunsLoader_TypeAddedToContext() { //Arrange var resolver = Substitute.For <IDependencyResolver>(); var context = Context.Create(resolver); context.Config = new Config(); var typeContext = new TestTypeCreationContext(); typeContext.Options = new TestGetOptions() { Type = typeof(StubClass) }; var args = new ConfigurationResolverArgs(context, typeContext, null); var task = new ConfigurationOnDemandResolverTask <StubTypeConfiguration>(); Assert.AreEqual(0, context.TypeConfigurations.Count); //Act task.Execute(args); //Assert Assert.AreEqual(1, context.TypeConfigurations.Count); Assert.IsNotNull(context.TypeConfigurations[typeof(StubClass)]); Assert.AreEqual(typeof(StubClass), args.Result.Type); }
public void Execute_OnDemandDisabled_ThrowsException() { //Arrange var resolver = Substitute.For <IDependencyResolver>(); var context = Context.Create(resolver); context.Config = new Config(); context.Config.OnDemandMappingEnabled = false; var typeContext = new TestTypeCreationContext(); typeContext.Options = new TestGetOptions() { Type = typeof(StubClass) }; var args = new ConfigurationResolverArgs(context, typeContext, null); var task = new ConfigurationOnDemandResolverTask <StubTypeConfiguration>(); Assert.AreEqual(0, context.TypeConfigurations.Count); //Act Assert.Throws <MapperException>( () => { task.Execute(args); }, Constants.Errors.OnDemandDisabled); }
public void Execute_MultipleRequestsUnloadedType_AddsOnlyOnceToContext() { //Arrange var resolver = Substitute.For <IDependencyResolver>(); var context = Context.Create(resolver); var typeContext = new TestTypeCreationContext(); context.Config = new Config(); var task = new ConfigurationOnDemandResolverTask <StubTypeConfiguration>(); var argsList = new List <ConfigurationResolverArgs>(); Action taskFunc = () => { typeContext.Options = new TestGetOptions() { Type = typeof(StubClass) }; var args = new ConfigurationResolverArgs(context, typeContext, null); argsList.Add(args); task.Execute(args); }; Assert.AreEqual(0, context.TypeConfigurations.Count); //Act var tasks = new [] { new Task(taskFunc), new Task(taskFunc), new Task(taskFunc), new Task(taskFunc), new Task(taskFunc), new Task(taskFunc), }; Parallel.ForEach(tasks, x => x.Start()); Task.WaitAll(tasks); //Assert Assert.AreEqual(1, context.TypeConfigurations.Count); Assert.IsTrue(argsList.All(x => x.Result.Type == typeof(StubClass))); }
public void Execute_ResultAlreadySet_DoesNotRunLoader() { //Arrange var resolver = NSubstitute.Substitute.For<IDependencyResolver>(); var context = Context.Create(resolver); var typeContext = NSubstitute.Substitute.For<AbstractTypeCreationContext>(); typeContext.RequestedType = typeof (StubClass); var args = new ConfigurationResolverArgs(context, typeContext, typeContext.RequestedType, null); args.Result = new StubTypeConfiguration(); var task = new ConfigurationOnDemandResolverTask<StubTypeConfiguration>(); Assert.AreEqual(0, context.TypeConfigurations.Count); //Act task.Execute(args); //Assert Assert.AreEqual(0, context.TypeConfigurations.Count); }
public void Execute_MultipleRequestsUnloadedType_AddsOnlyOnceToContext() { //Arrange var resolver = NSubstitute.Substitute.For<IDependencyResolver>(); var context = Context.Create(resolver); var typeContext = NSubstitute.Substitute.For<AbstractTypeCreationContext>(); var task = new ConfigurationOnDemandResolverTask<StubTypeConfiguration>(); var argsList = new List<ConfigurationResolverArgs>(); Action taskFunc = () => { typeContext.RequestedType = typeof(StubClass); var args = new ConfigurationResolverArgs(context, typeContext, typeContext.RequestedType, null); argsList.Add(args); task.Execute(args); }; Assert.AreEqual(0, context.TypeConfigurations.Count); //Act var tasks = new [] { new Task(taskFunc), new Task(taskFunc), new Task(taskFunc), new Task(taskFunc), new Task(taskFunc), new Task(taskFunc), }; Parallel.ForEach(tasks, x => x.Start()); Task.WaitAll(tasks); //Assert Assert.AreEqual(1, context.TypeConfigurations.Count); Assert.IsTrue(argsList.All(x => x.Result.Type == typeof(StubClass))); }
public void Execute_RunsLoader_TypeAddedToContext() { //Arrange var resolver = NSubstitute.Substitute.For<IDependencyResolver>(); var context = Context.Create(resolver); var typeContext = NSubstitute.Substitute.For<AbstractTypeCreationContext>(); var args = new ConfigurationResolverArgs(context, typeContext); typeContext.RequestedType = typeof(StubClass); var task = new ConfigurationOnDemandResolverTask<StubTypeConfiguration>(); Assert.AreEqual(0, context.TypeConfigurations.Count); //Act task.Execute(args); //Assert Assert.AreEqual(1, context.TypeConfigurations.Count); Assert.IsNotNull(context.TypeConfigurations[typeof(StubClass)]); Assert.AreEqual(typeof(StubClass), args.Result.Type); }