public void Execute_EnforeTemplateOnlyDoesNotInheritTemplate_AbortsPipeline() { //Arrange var task = new EnforcedTemplateCheck(); var database = Sitecore.Configuration.Factory.GetDatabase("master"); var path = "/sitecore/content/Tests/Pipelines/ObjectConstruction/EnforcedTemplateCheck/Target"; var item = database.GetItem(path); var config = new SitecoreTypeConfiguration(); config.EnforceTemplate = SitecoreEnforceTemplate.Template; config.TemplateId = new ID(Guid.NewGuid()); var typeContext = new SitecoreTypeCreationContext(); typeContext.Item = item; var args = new ObjectConstructionArgs(null, typeContext, config, null); //Act task.Execute(args); //Assert Assert.IsNull(args.Result); Assert.IsTrue(args.IsAborted); }
public void Execute_EnforeTemplateOnlyDoesNotInheritTemplate_AbortsPipeline() { //Arrange var task = new EnforcedTemplateCheck(); using (Db database = new Db { new DbTemplate(new ID(TemplateInferredTypeTaskFixture.StubInferred.TemplateId)), new Sitecore.FakeDb.DbItem("Target", ID.NewID, new ID(TemplateInferredTypeTaskFixture.StubInferred.TemplateId)) }) { var path = "/sitecore/content/Target"; var item = database.GetItem(path); var config = new SitecoreTypeConfiguration(); config.EnforceTemplate = SitecoreEnforceTemplate.Template; config.TemplateId = new ID(Guid.NewGuid()); var typeContext = new SitecoreTypeCreationContext(); typeContext.Item = item; var args = new ObjectConstructionArgs(null, typeContext, config, null, new ModelCounter()); //Act task.Execute(args); //Assert Assert.IsNull(args.Result); } }
public void Intercept_CreatesObjectLazily_CallsInvokeMethod() { //Assign var typeContext = Substitute.For<AbstractTypeCreationContext>(); var config = Substitute.For<AbstractTypeConfiguration>(); var service = Substitute.For<IAbstractService>(); var args = new ObjectConstructionArgs( null, typeContext, config, service, new ModelCounter() ); var invocation = Substitute.For<IInvocation>(); invocation.Method.Returns(typeof (StubClass).GetMethod("CalledMe")); service.InstantiateObject(typeContext).Returns(new StubClass()); var interceptor = new LazyObjectInterceptor(args); //Act interceptor.Intercept(invocation); //Assert Assert.IsTrue((bool)invocation.ReturnValue); }
public void Execute(ObjectConstructionArgs args) { if (args.Result != null) return; if (args.AbstractTypeCreationContext.ConstructorParameters == null || !args.AbstractTypeCreationContext.ConstructorParameters.Any()) { var resolver = args.Context.DependencyResolver as DependencyResolver; if (resolver != null) { var type = args.Configuration.Type; var container = resolver.Container; if (type.IsClass) { if(!container.Kernel.HasComponent(type)) container.Kernel.AddComponent(type.FullName, type, LifestyleType.Transient); args.Result = container.Resolve(type); if(args.Result != null) args.Configuration.MapPropertiesToObject(args.Result, args.Service, args.AbstractTypeCreationContext); } } } }
public void Execute_RequestInstanceOfClass_ReturnsInstance() { //Assign var task = new WindsorConstruction(); var context = Context.Create(DependencyResolver.CreateStandardResolver()); var typeConfig = Substitute.For<AbstractTypeConfiguration>(); typeConfig.Type = typeof (StubClass); var typeCreationContext = Substitute.For<AbstractTypeCreationContext>(); var service = Substitute.For<AbstractService>(); var args = new ObjectConstructionArgs(context, typeCreationContext, typeConfig, service); Assert.IsNull(args.Result); //Act task.Execute(args); //Assert Assert.IsNotNull(args.Result); Assert.IsTrue(args.Result is StubClass); }
public void Execute_RequestInstanceOfInterface_ReturnsNullInterfaceNotSupported() { //Assign var task = new WindsorConstruction(); var context = Context.Create(DependencyResolver.CreateStandardResolver()); var typeConfig = Substitute.For<AbstractTypeConfiguration>(); typeConfig.Type = typeof(StubInterface); var typeCreationContext = Substitute.For<AbstractTypeCreationContext>(); var args = new ObjectConstructionArgs(context, typeCreationContext, typeConfig, null); Assert.IsNull(args.Result); //Act task.Execute(args); //Assert Assert.IsNull(args.Result); }
public void Execute_ResultAlreadySet_NoInstanceCreated() { //Assign var task = new WindsorConstruction(); var context = Context.Create(DependencyResolver.CreateStandardResolver()); var typeConfig = Substitute.For<AbstractTypeConfiguration>(); typeConfig.Type = typeof(StubClass); var args = new ObjectConstructionArgs(context, null, typeConfig, null); var result = new StubClass2(); args.Result = result; Assert.IsNotNull(args.Result); //Act task.Execute(args); //Assert Assert.IsNotNull(args.Result); Assert.IsTrue(args.Result is StubClass2); Assert.AreEqual(result, args.Result); }
public void Intercept_WithMultiplePropertiesAndLazyLoading_ReturnsExpectedPropertyValues() { //Arrange var service = Substitute.For<IAbstractService>(); var config = new StubAbstractTypeConfiguration(); var context = Substitute.For<AbstractTypeCreationContext>(); config.Type = typeof(IStubTarget); context.IsLazy = true; var args = new ObjectConstructionArgs(null, context, config, service, new ModelCounter()) { Parameters = new Dictionary<string, object>() }; var interceptor = new InterfacePropertyInterceptor(args); var invocations = new List<IInvocation>(); for (var i = 0; i < 100; i++) { invocations.Add(CreateInterceptedProperty(config, i.ToString())); } //Act foreach (var invocation in invocations) { interceptor.Intercept(invocation); // Assert Assert.AreEqual(invocation.Method.Name.Substring(4), invocation.ReturnValue); } }
public void Execute_ProxyInterface_ProxyGetsCreated() { //Assign Type type = typeof(IStubInterface); var glassConfig = Substitute.For<IGlassConfiguration>(); var service = Substitute.For<IAbstractService>(); Context context = Context.Create(Substitute.For<IDependencyResolver>()); AbstractTypeCreationContext abstractTypeCreationContext = Substitute.For<AbstractTypeCreationContext>(); abstractTypeCreationContext.RequestedType = typeof (IStubInterface); var configuration = Substitute.For<AbstractTypeConfiguration>(); configuration.Type = type; ObjectConstructionArgs args = new ObjectConstructionArgs(context, abstractTypeCreationContext, configuration, service); //Act _task.Execute(args); //Assert Assert.IsNotNull(args.Result); Assert.IsTrue(args.IsAborted); Assert.IsTrue(args.Result is IStubInterface); Assert.IsFalse(args.Result.GetType() == typeof(IStubInterface)); }
public void Execute_LazyType_LazyTypeCreated() { //Assign Type type = typeof (StubClass); var service = Substitute.For<IAbstractService>(); Context context = Context.Create(Substitute.For<IDependencyResolver>()); AbstractTypeCreationContext abstractTypeCreationContext = Substitute.For<AbstractTypeCreationContext>(); abstractTypeCreationContext.RequestedType = typeof (StubClass); abstractTypeCreationContext.IsLazy = true; var configuration = Substitute.For<AbstractTypeConfiguration>(); configuration.Type = type; configuration.ConstructorMethods = Utilities.CreateConstructorDelegates(type); ObjectConstructionArgs args = new ObjectConstructionArgs(context, abstractTypeCreationContext, configuration, service, new ModelCounter()); //Act _task.Execute(args); //Assert Assert.IsNotNull(args.Result); Assert.IsTrue(args.Result is StubClass); Assert.IsFalse(args.Result.GetType() == typeof(StubClass)); }
public void Execute(ObjectConstructionArgs args) { if (args.Result == null && args.Configuration == Sc.Pipelines.ConfigurationResolver.SitecoreItemResolverTask.Config) { var scArgs = args.AbstractTypeCreationContext as SitecoreTypeCreationContext; args.Result = scArgs.Item; } }
public void Intercept_InterceptsMultiplePropertiesOnDifferentInterfaces_ReturnsExpectedPropertyValues() { //Arrange var service = Substitute.For<IAbstractService>(); var config1 = new StubAbstractTypeConfiguration(); var config2 = new StubAbstractTypeConfiguration(); var property1 = new StubAbstractPropertyConfiguration(); var property2 = new StubAbstractPropertyConfiguration(); var mapper1 = new StubAbstractDataMapper(); var mapper2 = new StubAbstractDataMapper(); var expected1 = "test random 1"; var expected2 = "some other random"; var propertyName1 = "Property1"; var propertyName2 = "Property2"; var info1 = new FakePropertyInfo(typeof(string), propertyName1, typeof(IStubTarget)); var info2 = new FakePropertyInfo(typeof(string), propertyName2, typeof(IStubTarget2)); config1.AddProperty(property1); config2.AddProperty(property2); property1.Mapper = mapper1; property2.Mapper = mapper2; property1.PropertyInfo = info1; property2.PropertyInfo = info2; mapper1.Value = expected1; mapper2.Value = expected2; var args = new ObjectConstructionArgs(null, null, config1, service); args.Parameters = new Dictionary<string, object>(); args.Parameters[CreateMultiInferaceTask.MultiInterfaceConfigsKey] = new[] {config2}; var interceptor = new MultiInterfacePropertyInterceptor(args); var invocation1 = Substitute.For<IInvocation>(); var invocation2 = Substitute.For<IInvocation>(); invocation1.Method.Returns(typeof(IStubTarget).GetProperty(propertyName1).GetGetMethod()); invocation2.Method.Returns(typeof(IStubTarget2).GetProperty(propertyName2).GetGetMethod()); //Act interceptor.Intercept(invocation1); interceptor.Intercept(invocation2); //Assert Assert.AreEqual(expected1, invocation1.ReturnValue); Assert.AreEqual(expected2, invocation2.ReturnValue); }
/// <summary> /// Executes the specified args. /// </summary> /// <param name="args">The args.</param> public void Execute(ObjectConstructionArgs args) { if (args.Result == null && args.Configuration != null && args.Configuration.Type.IsAssignableFrom(_dynamicType)) { SitecoreTypeCreationContext typeContext = args.AbstractTypeCreationContext as SitecoreTypeCreationContext; args.Result = new DynamicItem(typeContext.Item); } }
/// <summary> /// Executes the specified args. /// </summary> /// <param name="args">The args.</param> public void Execute(ObjectConstructionArgs args) { if (args.Result == null) { if (args.Configuration.Type.IsAssignableFrom(typeof(IDynamicMetaObjectProvider))) { SitecoreTypeCreationContext typeContext = args.AbstractTypeCreationContext as SitecoreTypeCreationContext; args.Result = new DynamicItem(typeContext.Item); } } }
public string Generate(ObjectConstructionArgs args) { var context = args.AbstractTypeCreationContext as UmbracoTypeCreationContext; return string.Format("{0}{1}{2}{3}{4}{5}", args.Context.Name, context.UmbracoService.GlassContext.Name, context.Content.Id.ToString(), context.Content.Version, context.RequestedType.FullName, context.IsLazy ); }
public void Execute_ItemConfig_ItemSet() { //Arrange var task = new SitecoreItemTask(); var context = new SitecoreTypeCreationContext(); context.Item = Mapper.Sc.Utilities.CreateFakeItem(new Dictionary<Guid, string>(), "test"); var args = new ObjectConstructionArgs(null, context, SitecoreItemResolverTask.Config, null); //Act task.Execute(args); //Assert Assert.AreEqual(context.Item, args.Result); }
public void Execute_ResultNotSitecoreItemConfig_NoValueSet() { //Arrange var task = new SitecoreItemTask(); var context = new SitecoreTypeCreationContext(); var args = new ObjectConstructionArgs(null, context, new SitecoreTypeConfiguration(), null); //Act task.Execute(args); //Assert Assert.IsNull(args.Result); }
public void Execute(ObjectConstructionArgs args) { if (args.Result != null || !SearchSwitcher.IsSearchContext || args.Configuration.Type.IsSealed) return; if (args.Configuration.Type.IsInterface) { args.Result = _generator.CreateInterfaceProxyWithoutTarget(args.Configuration.Type, new SearchInterceptor(args)); args.AbortPipeline(); } else { args.Result = _generator.CreateClassProxy(args.Configuration.Type,new SearchInterceptor(args)); args.AbortPipeline(); } }
public void Execute_ArgsNotNull_ReturnsAndDoesNothing() { //Arrange var args = new ObjectConstructionArgs(null,null, null, null, new ModelCounter()); var expected = new object(); args.Result = expected; var task = new CreateMultiInferaceTask(); //Act task.Execute(args); //Assert Assert.AreEqual(expected, args.Result); }
public string Generate(ObjectConstructionArgs args) { var context = args.AbstractTypeCreationContext as SitecoreTypeCreationContext; return string.Format("{0}{1}{2}{3}{4}{5}{6}{7}", context.SitecoreService.GlassContext.Name, Sitecore.Context.Site == null ? string.Empty : Sitecore.Context.Site.Name, context.Item.ID, context.Item["__Revision"], context.Item.Language.Name, context.Item.Database.Name, context.RequestedType.FullName, context.IsLazy ); }
public void Execute(ObjectConstructionArgs args) { if (args.Result == null) { var scConfig = args.Configuration as SitecoreTypeConfiguration; if (scConfig.EnforceTemplate != SitecoreEnforceTemplate.No) { var scArgs = args.AbstractTypeCreationContext as SitecoreTypeCreationContext; var key = "{0} {1} {2}".Formatted(scConfig.TemplateId, scArgs.Item.TemplateID, scConfig.EnforceTemplate); var result = false; if (_cache.ContainsKey(key)) { result = _cache[key]; } else { var item = scArgs.Item; if (scConfig.EnforceTemplate == SitecoreEnforceTemplate.TemplateAndBase) { result = item.TemplateID == scConfig.TemplateId || item.Template.BaseTemplates.Any(x => x.ID == scConfig.TemplateId); } else if(scConfig.EnforceTemplate == SitecoreEnforceTemplate.Template) { result = item.TemplateID == scConfig.TemplateId; } _cache[key] = result; } if (result) { return; } else { args.AbortPipeline(); } } } }
public void Excute_ResultNotNull_NoChanges() { //Arrange var task = new EnforcedTemplateCheck(); var args = new ObjectConstructionArgs(null,null,null,null); var expected = new object(); args.Result = expected; //Act task.Execute(args); //Assert Assert.AreEqual(expected, args.Result); }
public void Execute_EnforeTemplateNo_PipelineNotAborted() { //Arrange var task = new EnforcedTemplateCheck(); var config = new SitecoreTypeConfiguration(); config.EnforceTemplate = SitecoreEnforceTemplate.No; var args = new ObjectConstructionArgs(null, null, config, null, new ModelCounter()); //Act task.Execute(args); //Assert Assert.IsNull(args.Result); }
public void Execute_ResultNotNull_NoChangeToResult() { //Arrange var task = new SitecoreItemTask(); var context = new SitecoreTypeCreationContext(); var args = new ObjectConstructionArgs(null, context, new SitecoreTypeConfiguration(), null); var expected = "some value"; args.Result = expected; //Act task.Execute(args); //Assert Assert.AreEqual(expected, args.Result); }
public override void Execute(ObjectConstructionArgs args) { if (args.Result == null) { var scConfig = args.Configuration as SitecoreTypeConfiguration; if (scConfig != null && scConfig.EnforceTemplate != SitecoreEnforceTemplate.No) { var scArgs = args.AbstractTypeCreationContext as SitecoreTypeCreationContext; var key = "{0} {1} {2}".Formatted(scConfig.TemplateId, scArgs.Item.TemplateID, scConfig.EnforceTemplate); var result = false; if (_cache.ContainsKey(key)) { result = _cache[key]; } else { var item = scArgs.Item; if (scConfig.EnforceTemplate == SitecoreEnforceTemplate.TemplateAndBase) { result = TemplateAndBaseCheck(item.Template, scConfig.TemplateId); } else if (scConfig.EnforceTemplate == SitecoreEnforceTemplate.Template) { result = item.TemplateID == scConfig.TemplateId; } _cache.TryAdd(key, result); } if (!result) { base.Execute(args); } } else { base.Execute(args); } } }
public void Execute_ArgsNotNullOneInterface_ReturnsNull() { //Arrange var config1 = new StubAbstractTypeConfiguration(); config1.Type = typeof(IStubTarget); var args = new ObjectConstructionArgs(null, null, config1 , null, new ModelCounter()); var task = new CreateMultiInferaceTask(); //Act task.Execute(args); //Assert Assert.IsNull(args.Result); }
public void Execute_TypeIsInterface_NoObjectCreated() { //Assign Type type = typeof(IStubInterface); var service = Substitute.For<IAbstractService>(); Context context = Context.Create(Substitute.For<IDependencyResolver>()); AbstractTypeCreationContext abstractTypeCreationContext = Substitute.For<AbstractTypeCreationContext>(); abstractTypeCreationContext.RequestedType= type; var configuration = Substitute.For<AbstractTypeConfiguration>(); configuration.Type = type; ObjectConstructionArgs args = new ObjectConstructionArgs(context, abstractTypeCreationContext, configuration, service, new ModelCounter()); //Act _task.Execute(args); //Assert Assert.IsNull(args.Result); }
public void Intercept_InterceptsProperties_ReturnsExpectedPropertyValue() { //Arrange var service = Substitute.For<IAbstractService>(); var config = new StubAbstractTypeConfiguration(); var context = Substitute.For<AbstractTypeCreationContext>(); var property = new StubAbstractPropertyConfiguration(); var mapper = new StubAbstractDataMapper(); var expected = "test random 1"; var propertyName = "Property1"; var info = new FakePropertyInfo(typeof(string), propertyName, typeof(IStubTarget)); config.AddProperty(property); config.Type = typeof(IStubTarget); property.Mapper = mapper; property.PropertyInfo = info; mapper.Value = expected; var args = new ObjectConstructionArgs(null, context, config, service, new ModelCounter()) { Parameters = new Dictionary<string, object>() }; var interceptor = new InterfacePropertyInterceptor(args); var invocation = Substitute.For<IInvocation>(); invocation.Method.Returns(typeof(IStubTarget).GetProperty(propertyName).GetGetMethod()); //Act interceptor.Intercept(invocation); //Assert Assert.AreEqual(expected, invocation.ReturnValue); }
public void Execute_ArgsNotNullMultipleInterface_ReturnsMultiInterfaceProxy() { //Arrange var config1 = new StubAbstractTypeConfiguration(); var config2 = new StubAbstractTypeConfiguration(); config1.Type = typeof(IStubTarget); config2.Type = typeof(IStubTarget2); // var args = new ObjectConstructionArgs(null, null, new[] { config1, config2 }, null); var args = new ObjectConstructionArgs(null, null, config1, null, new ModelCounter()); args.Parameters[CreateMultiInferaceTask.MultiInterfaceConfigsKey] = new[] {config2}; var task = new CreateMultiInferaceTask(); //Act task.Execute(args); //Assert Assert.IsNotNull(args.Result); Assert.IsTrue(args.Result is IStubTarget); Assert.IsTrue(args.Result is IStubTarget2); }
public void Execute_ConcreteClass_ObjectNotCreated() { //Assign Type type = typeof(StubClass); var service = Substitute.For<IAbstractService>(); Context context = Context.Create(Substitute.For<IDependencyResolver>()); AbstractTypeCreationContext abstractTypeCreationContext = Substitute.For<AbstractTypeCreationContext>(); abstractTypeCreationContext.RequestedType = type; var configuration = Substitute.For<AbstractTypeConfiguration>(); configuration.Type = type; ObjectConstructionArgs args = new ObjectConstructionArgs(context, abstractTypeCreationContext, configuration, service); //Act _task.Execute(args); //Assert Assert.IsNull(args.Result); Assert.IsFalse(args.IsAborted); }