public void ObjectNameChild() { ObjectDefinitionBuilder odb = ObjectDefinitionBuilder.ChildObjectDefinition(odf, typeof(TestObject).FullName); ChildObjectDefinition rod = odb.ObjectDefinition as ChildObjectDefinition; Assert.IsNotNull(rod); Assert.IsFalse(rod.HasObjectType); Assert.AreEqual(typeof(TestObject).FullName, rod.ParentName); }
public void Ctor_ParentNameOnly() { const string expectedParentName = "foo"; ChildObjectDefinition def = new ChildObjectDefinition(expectedParentName); Assert.AreEqual(expectedParentName, def.ParentName, "ParentName property not initialized correctly by ctor."); Assert.IsNotNull(def.PropertyValues, "PropertyValues must be init'd to a non-null collection if not explicitly supplied."); Assert.AreEqual(0, def.PropertyValues.PropertyValues.Count, "PropertyValues must be init'd to an empty collection if not explicitly supplied."); }
public void Ctor_ParentNameOnly() { const string expectedParentName = "foo"; ChildObjectDefinition def = new ChildObjectDefinition(expectedParentName); Assert.AreEqual(expectedParentName, def.ParentName, "ParentName property not initialized correctly by ctor."); Assert.IsNotNull(def.PropertyValues, "PropertyValues must be init'd to a non-null collection if not explicitly supplied."); Assert.AreEqual(0, def.PropertyValues.PropertyValues.Count, "PropertyValues must be init'd to an empty collection if not explicitly supplied."); }
/// <summary> /// Factory style method for getting concrete /// <see cref="IConfigurableObjectDefinition"/> /// instances. /// </summary> /// /// <remarks>If no parent is specified, a RootObjectDefinition is created, otherwise a /// ChildObjectDefinition.</remarks> /// <param name="typeName">The <see cref="System.Type"/> of the defined object.</param> /// <param name="parent">The name of the parent object definition (if any).</param> /// <param name="domain">The <see cref="System.AppDomain"/> against which any class names /// will be resolved into <see cref="System.Type"/> instances.</param> /// <returns> /// An /// <see cref="IConfigurableObjectDefinition"/> /// instance. /// </returns> public virtual AbstractObjectDefinition CreateObjectDefinition(string typeName, string parent, AppDomain domain) { Type objectType = null; if (StringUtils.HasText(typeName) && domain != null) { try { objectType = TypeResolutionUtils.ResolveType(typeName); } // try later.... catch { } } if (StringUtils.IsNullOrEmpty(parent)) { if (objectType != null) { return(new RootObjectDefinition(objectType)); } else { RootObjectDefinition rootObjectDefinition = new RootObjectDefinition(); rootObjectDefinition.ObjectTypeName = typeName; return(rootObjectDefinition); } } else { if (objectType != null) { ChildObjectDefinition childObjectDefinition = new ChildObjectDefinition(parent); childObjectDefinition.ObjectType = objectType; return(childObjectDefinition); } else { ChildObjectDefinition childObjectDefinition = new ChildObjectDefinition(parent); childObjectDefinition.ObjectTypeName = typeName; return(childObjectDefinition); } } }
/// <summary> /// Factory style method for getting concrete /// <see cref="IConfigurableObjectDefinition"/> /// instances. /// </summary> /// /// <remarks>If no parent is specified, a RootObjectDefinition is created, otherwise a /// ChildObjectDefinition.</remarks> /// <param name="typeName">The <see cref="System.Type"/> of the defined object.</param> /// <param name="parent">The name of the parent object definition (if any).</param> /// <param name="domain">The <see cref="System.AppDomain"/> against which any class names /// will be resolved into <see cref="System.Type"/> instances.</param> /// <returns> /// An /// <see cref="IConfigurableObjectDefinition"/> /// instance. /// </returns> public virtual AbstractObjectDefinition CreateObjectDefinition(string typeName, string parent, AppDomain domain) { Type objectType = null; if (StringUtils.HasText(typeName) && domain != null) { try { objectType = TypeResolutionUtils.ResolveType(typeName); } // try later.... catch { } } if (StringUtils.IsNullOrEmpty(parent)) { if (objectType != null) { return new RootObjectDefinition(objectType); } else { RootObjectDefinition rootObjectDefinition = new RootObjectDefinition(); rootObjectDefinition.ObjectTypeName = typeName; return rootObjectDefinition; } } else { if (objectType != null) { ChildObjectDefinition childObjectDefinition = new ChildObjectDefinition(parent); childObjectDefinition.ObjectType = objectType; return childObjectDefinition; } else { ChildObjectDefinition childObjectDefinition = new ChildObjectDefinition(parent); childObjectDefinition.ObjectTypeName = typeName; return childObjectDefinition; } } }
public void GetObjectDefinitionResolvesAliases() { const string TheParentsAlias = "theParentsAlias"; const int ExpectedAge = 31; const string ExpectedName = "Rick Evans"; RootObjectDefinition parentDef = new RootObjectDefinition(typeof(TestObject)); parentDef.IsAbstract = true; parentDef.PropertyValues.Add("name", ExpectedName); parentDef.PropertyValues.Add("age", ExpectedAge); ChildObjectDefinition childDef = new ChildObjectDefinition(TheParentsAlias); DefaultListableObjectFactory fac = new DefaultListableObjectFactory(); fac.RegisterObjectDefinition("parent", parentDef); fac.RegisterAlias("parent", TheParentsAlias); IObjectDefinition od = fac.GetObjectDefinition(TheParentsAlias); Assert.IsNotNull(od); }
public void ChildReferencesParentByAnAliasOfTheParent() { const string TheParentsAlias = "theParentsAlias"; const int ExpectedAge = 31; const string ExpectedName = "Rick Evans"; RootObjectDefinition parentDef = new RootObjectDefinition(typeof(TestObject)); parentDef.IsAbstract = true; parentDef.PropertyValues.Add("name", ExpectedName); parentDef.PropertyValues.Add("age", ExpectedAge); ChildObjectDefinition childDef = new ChildObjectDefinition(TheParentsAlias); DefaultListableObjectFactory fac = new DefaultListableObjectFactory(); fac.RegisterObjectDefinition("parent", parentDef); fac.RegisterAlias("parent", TheParentsAlias); fac.RegisterObjectDefinition("child", childDef); TestObject child = (TestObject)fac.GetObject("child"); Assert.AreEqual(ExpectedName, child.Name); Assert.AreEqual(ExpectedAge, child.Age); }
public void OverrideFromOther() { RootObjectDefinition rod = new RootObjectDefinition(typeof(TestObject), AutoWiringMode.Constructor); rod.IsSingleton = false; rod.InitMethodName = "Umberto Eco"; rod.DestroyMethodName = "Pedulismus"; rod.ConstructorArgumentValues = new ConstructorArgumentValues(); rod.ConstructorArgumentValues.AddGenericArgumentValue("Wulf", "Sternhammer"); rod.DependencyCheck = DependencyCheckingMode.Objects; rod.DependsOn = new string[] { "Jimmy", "Joyce" }; rod.IsAbstract = true; rod.IsLazyInit = true; rod.FactoryMethodName = "IfINeedYouIllJustUseYourSimpleName"; rod.FactoryObjectName = "PerspirationShop"; rod.ResourceDescription = "Ohhh"; rod.PropertyValues = new MutablePropertyValues(); rod.PropertyValues.Add("Age", 100); rod.EventHandlerValues = new EventValues(); InstanceEventHandlerValue handler = new InstanceEventHandlerValue(new TestObject(), "Ping"); handler.EventName = "Bing"; rod.EventHandlerValues.AddHandler(handler); ChildObjectDefinition cod = new ChildObjectDefinition("parent"); cod.ObjectTypeName = "ChildObjectTypeName"; cod.IsSingleton = true; cod.AutowireMode = AutoWiringMode.ByType; cod.InitMethodName = "InitChild"; cod.DestroyMethodName = "DestroyChild"; cod.ConstructorArgumentValues = new ConstructorArgumentValues(); cod.ConstructorArgumentValues.AddNamedArgumentValue("ctorarg", "ctorarg-value"); cod.DependencyCheck = DependencyCheckingMode.None; cod.DependsOn = new string[] { "Aleks", "Mark" }; cod.IsAbstract = false; cod.IsLazyInit = false; cod.FactoryMethodName = "ChildFactoryMethodName"; cod.FactoryObjectName = "ChildFactoryObjectName"; cod.ResourceDescription = "ChildResourceDescription"; cod.PropertyValues = new MutablePropertyValues(); cod.PropertyValues.Add("Prop1", "Val1"); cod.PropertyValues.Add("Age", 50); cod.EventHandlerValues = new EventValues(); handler = new InstanceEventHandlerValue(new TestObject(), "Pong"); handler.EventName = "Bong"; cod.EventHandlerValues.AddHandler(handler); rod.OverrideFrom(cod); // ... Assert.AreEqual("ChildObjectTypeName", rod.ObjectTypeName); Assert.AreEqual(AutoWiringMode.ByType, rod.AutowireMode); Assert.AreEqual(true, rod.IsSingleton); Assert.AreEqual("InitChild", rod.InitMethodName); Assert.AreEqual("DestroyChild", rod.DestroyMethodName); Assert.AreEqual(DependencyCheckingMode.None, rod.DependencyCheck); Assert.AreEqual(4, rod.DependsOn.Length); Assert.AreEqual(false, rod.IsAbstract); Assert.AreEqual(false, rod.IsLazyInit); Assert.AreEqual("ChildFactoryMethodName", rod.FactoryMethodName); Assert.AreEqual("ChildFactoryObjectName", rod.FactoryObjectName); Assert.AreEqual("ChildResourceDescription", rod.ResourceDescription); Assert.AreEqual(2, rod.ConstructorArgumentValues.ArgumentCount); Assert.AreEqual(2, rod.PropertyValues.PropertyValues.Length); Assert.AreEqual("Val1", rod.PropertyValues.GetPropertyValue("Prop1").Value); Assert.AreEqual(50, rod.PropertyValues.GetPropertyValue("Age").Value); Assert.AreEqual(2, rod.EventHandlerValues.Events.Count); }
public void OverrideFromOther() { RootObjectDefinition rod = new RootObjectDefinition( typeof( TestObject ), AutoWiringMode.Constructor ); rod.IsSingleton = false; rod.InitMethodName = "Umberto Eco"; rod.DestroyMethodName = "Pedulismus"; rod.ConstructorArgumentValues = new ConstructorArgumentValues(); rod.ConstructorArgumentValues.AddGenericArgumentValue( "Wulf", "Sternhammer" ); rod.DependencyCheck = DependencyCheckingMode.Objects; rod.DependsOn = new string[] { "Jimmy", "Joyce" }; rod.IsAbstract = true; rod.IsLazyInit = true; rod.FactoryMethodName = "IfINeedYouIllJustUseYourSimpleName"; rod.FactoryObjectName = "PerspirationShop"; rod.ResourceDescription = "Ohhh"; rod.PropertyValues = new MutablePropertyValues(); rod.PropertyValues.Add( "Age", 100 ); rod.EventHandlerValues = new EventValues(); InstanceEventHandlerValue handler = new InstanceEventHandlerValue( new TestObject(), "Ping" ); handler.EventName = "Bing"; rod.EventHandlerValues.AddHandler( handler ); ChildObjectDefinition cod = new ChildObjectDefinition( "parent" ); cod.ObjectTypeName = "ChildObjectTypeName"; cod.IsSingleton = true; cod.AutowireMode = AutoWiringMode.ByType; cod.InitMethodName = "InitChild"; cod.DestroyMethodName = "DestroyChild"; cod.ConstructorArgumentValues = new ConstructorArgumentValues(); cod.ConstructorArgumentValues.AddNamedArgumentValue( "ctorarg", "ctorarg-value" ); cod.DependencyCheck = DependencyCheckingMode.None; cod.DependsOn = new string[] { "Aleks", "Mark" }; cod.IsAbstract = false; cod.IsLazyInit = false; cod.FactoryMethodName = "ChildFactoryMethodName"; cod.FactoryObjectName = "ChildFactoryObjectName"; cod.ResourceDescription = "ChildResourceDescription"; cod.PropertyValues = new MutablePropertyValues(); cod.PropertyValues.Add( "Prop1", "Val1" ); cod.PropertyValues.Add( "Age", 50 ); cod.EventHandlerValues = new EventValues(); handler = new InstanceEventHandlerValue( new TestObject(), "Pong" ); handler.EventName = "Bong"; cod.EventHandlerValues.AddHandler( handler ); rod.OverrideFrom( cod ); // ... Assert.AreEqual( "ChildObjectTypeName", rod.ObjectTypeName ); Assert.AreEqual( AutoWiringMode.ByType, rod.AutowireMode ); Assert.AreEqual( true, rod.IsSingleton ); Assert.AreEqual( "InitChild", rod.InitMethodName ); Assert.AreEqual( "DestroyChild", rod.DestroyMethodName ); Assert.AreEqual( DependencyCheckingMode.None, rod.DependencyCheck ); Assert.AreEqual( 4, rod.DependsOn.Count); Assert.AreEqual( false, rod.IsAbstract ); Assert.AreEqual( false, rod.IsLazyInit ); Assert.AreEqual( "ChildFactoryMethodName", rod.FactoryMethodName ); Assert.AreEqual( "ChildFactoryObjectName", rod.FactoryObjectName ); Assert.AreEqual( "ChildResourceDescription", rod.ResourceDescription ); Assert.AreEqual( 2, rod.ConstructorArgumentValues.ArgumentCount ); Assert.AreEqual( 2, rod.PropertyValues.PropertyValues.Count ); Assert.AreEqual( "Val1", rod.PropertyValues.GetPropertyValue("Prop1").Value); Assert.AreEqual( 50, rod.PropertyValues.GetPropertyValue("Age").Value); Assert.AreEqual( 2, rod.EventHandlerValues.Events.Count ); }
public void SunnyDay() { StaticApplicationContext ac = new StaticApplicationContext(); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.Add("age", "${age}"); RootObjectDefinition def = new RootObjectDefinition("${fqn}", new ConstructorArgumentValues(), pvs); ac.RegisterObjectDefinition("tb3", def); pvs = new MutablePropertyValues(); pvs.Add("age", "${age}"); pvs.Add("name", "name${var}${"); pvs.Add("spouse", new RuntimeObjectReference("${ref}")); ac.RegisterSingleton("tb1", typeof (TestObject), pvs); ConstructorArgumentValues cas = new ConstructorArgumentValues(); cas.AddIndexedArgumentValue(1, "${age}"); cas.AddGenericArgumentValue("${var}name${age}"); pvs = new MutablePropertyValues(); ArrayList friends = new ManagedList(); friends.Add("na${age}me"); friends.Add(new RuntimeObjectReference("${ref}")); pvs.Add("friends", friends); ISet someSet = new ManagedSet(); someSet.Add("na${age}me"); someSet.Add(new RuntimeObjectReference("${ref}")); pvs.Add("someSet", someSet); IDictionary someDictionary = new ManagedDictionary(); someDictionary["key1"] = new RuntimeObjectReference("${ref}"); someDictionary["key2"] = "${age}name"; MutablePropertyValues innerPvs = new MutablePropertyValues(); someDictionary["key3"] = new RootObjectDefinition(typeof (TestObject), innerPvs); someDictionary["key4"] = new ChildObjectDefinition("tb1", innerPvs); pvs.Add("someMap", someDictionary); RootObjectDefinition definition = new RootObjectDefinition(typeof (TestObject), cas, pvs); ac.DefaultListableObjectFactory.RegisterObjectDefinition("tb2", definition); pvs = new MutablePropertyValues(); pvs.Add("Properties", "<spring-config><add key=\"age\" value=\"98\"/><add key=\"var\" value=\"${m}var\"/><add key=\"ref\" value=\"tb2\"/><add key=\"m\" value=\"my\"/><add key=\"fqn\" value=\"Spring.Objects.TestObject, Spring.Core.Tests\"/></spring-config>"); ac.RegisterSingleton("configurer", typeof (PropertyPlaceholderConfigurer), pvs); ac.Refresh(); TestObject tb1 = (TestObject) ac.GetObject("tb1"); TestObject tb2 = (TestObject) ac.GetObject("tb2"); TestObject tb3 = (TestObject) ac.GetObject("tb3"); Assert.AreEqual(98, tb1.Age); Assert.AreEqual(98, tb2.Age); Assert.AreEqual(98, tb3.Age); Assert.AreEqual("namemyvar${", tb1.Name); Assert.AreEqual("myvarname98", tb2.Name); Assert.AreEqual(tb2, tb1.Spouse); Assert.AreEqual(2, tb2.Friends.Count); IEnumerator ie = tb2.Friends.GetEnumerator(); ie.MoveNext(); Assert.AreEqual("na98me", ie.Current); ie.MoveNext(); Assert.AreEqual(tb2, ie.Current); Assert.AreEqual(2, tb2.SomeSet.Count); Assert.IsTrue(tb2.SomeSet.Contains("na98me")); Assert.IsTrue(tb2.SomeSet.Contains(tb2)); Assert.AreEqual(4, tb2.SomeMap.Count); Assert.AreEqual(tb2, tb2.SomeMap["key1"]); Assert.AreEqual("98name", tb2.SomeMap["key2"]); TestObject inner1 = (TestObject) tb2.SomeMap["key3"]; TestObject inner2 = (TestObject) tb2.SomeMap["key4"]; Assert.AreEqual(0, inner1.Age); Assert.AreEqual(null, inner1.Name); Assert.AreEqual(98, inner2.Age); Assert.AreEqual("namemyvar${", inner2.Name); }