/// <summary>
        /// Merges the current value set with that of the supplied object.
        /// </summary>
        /// <remarks>The supplied object is considered the parent, and values in the
        /// callee's value set must override those of the supplied object.
        /// </remarks>
        /// <param name="parent">The parent object to merge with</param>
        /// <returns>The result of the merge operation</returns>
        /// <exception cref="ArgumentNullException">If the supplied parent is <code>null</code></exception>
        /// <exception cref="InvalidOperationException">If merging is not enabled for this instance,
        /// (i.e. <code>MergeEnabled</code> equals <code>false</code>.</exception>
        public object Merge(object parent)
        {
            if (!this.mergeEnabled)
            {
                throw new InvalidOperationException(
                          "Not allowed to merge when the 'MergeEnabled' property is set to 'false'");
            }
            if (parent == null)
            {
                return(this);
            }
            ISet pSet = parent as ISet;

            if (pSet == null)
            {
                throw new InvalidOperationException("Cannot merge with object of type [" + parent.GetType() + "]");
            }
            ISet merged = new ManagedSet();

            foreach (object element in pSet)
            {
                merged.Add(element);
            }
            foreach (object o in this)
            {
                merged.Add(o);
            }
            return(merged);
        }
 public void MergeEmptyChild()
 {
     ManagedSet parent = new ManagedSet();
     parent.Add("one");
     parent.Add("two");
     ManagedSet child = new ManagedSet();
     child.MergeEnabled = true;
     ISet mergedSet = (ISet) child.Merge(parent);
     Assert.AreEqual(2, mergedSet.Count);
 }
 public void MergeSunnyDay()
 {
     ManagedSet parent = new ManagedSet();
     parent.Add("one");
     parent.Add("two");
     ManagedSet child = new ManagedSet();
     child.Add("three");
     child.MergeEnabled = true;
     ISet mergedList = (ISet) child.Merge(parent);
     Assert.AreEqual(3, mergedList.Count);
 }
 public void MergeWithNonCompatibleParentType()
 {
     ManagedSet child = new ManagedSet();
     child.Add("one");
     child.MergeEnabled = true;
     child.Merge("hello");
 }
 public void MergeWithNullParent()
 {
     ManagedSet child = new ManagedSet();
     child.Add("one");
     child.MergeEnabled = true;
     Assert.AreSame(child, child.Merge(null));
 }
        /// <summary>
        /// Visits the ManagedSet property ElementTypeName and
        /// calls <see cref="ResolveValue"/> for list element.
        /// </summary>
        protected virtual void VisitManagedSet(ManagedSet setVal)
        {
            string elementTypeName = setVal.ElementTypeName;

            if (elementTypeName != null)
            {
                string resolvedName = ResolveStringValue(elementTypeName);
                if (!elementTypeName.Equals(resolvedName))
                {
                    setVal.ElementTypeName = resolvedName;
                }
            }

            ISet clone = (ISet)setVal.Clone();

            foreach (object oldValue in clone)
            {
                object newValue = ResolveValue(oldValue);
                if (!ObjectUtils.NullSafeEquals(newValue, oldValue))
                {
                    setVal.Remove(oldValue);
                    setVal.Add(newValue);
                }
            }
        }
 public void MergeWithNonCompatibleParentType()
 {
     ManagedSet child = new ManagedSet();
     child.Add("one");
     child.MergeEnabled = true;
     Assert.Throws<InvalidOperationException>(() => child.Merge("hello"));
 }
 public void MergeChildValueOverrideTheParents()
 {
     ManagedSet parent = new ManagedSet();
     parent.Add("one");
     parent.Add("two");
     ManagedSet child = new ManagedSet();
     child.Add("one");
     child.MergeEnabled = true;
     ISet mergedList = (ISet) child.Merge(parent);
     Assert.AreEqual(2, mergedList.Count);
 }
Exemple #9
0
        public void VisitManagedSet()
        {
            IObjectDefinition od = new RootObjectDefinition();
            ManagedSet        ms = new ManagedSet();

            ms.ElementTypeName = "$Property";
            ms.Add("$Property");
            od.PropertyValues.Add("PropertyName", ms);

            ObjectDefinitionVisitor odv = new ObjectDefinitionVisitor(new ObjectDefinitionVisitor.ResolveHandler(ParseAndResolveVariables));

            odv.VisitObjectDefinition(od);

            ManagedSet set = od.PropertyValues.GetPropertyValue("PropertyName").Value as ManagedSet;

            Assert.AreEqual("Value", set.ElementTypeName);
            IEnumerator enumerator = set.GetEnumerator();

            enumerator.MoveNext();
            Assert.AreEqual("Value", enumerator.Current);
        }
        /// <summary>
        /// Visits the ManagedSet property ElementTypeName and 
        /// calls <see cref="ResolveValue"/> for list element.
        /// </summary>
        protected virtual void VisitManagedSet(ManagedSet setVal)
        {
            string elementTypeName = setVal.ElementTypeName;
            if (elementTypeName != null)
            {
                string resolvedName = ResolveStringValue(elementTypeName);
                if (!elementTypeName.Equals(resolvedName))
                {
                    setVal.ElementTypeName = resolvedName;
                }
            }

            ISet clone = (ISet)setVal.Clone();
            foreach (object oldValue in clone)
            {
                object newValue = ResolveValue(oldValue);
                if (!ObjectUtils.NullSafeEquals(newValue, oldValue))
                {
                    setVal.Remove(oldValue);
                    setVal.Add(newValue);
                }
            }
        }
        /// <summary>
        /// Gets a set definition.
        /// </summary>
        /// <param name="collectionEle">
        /// The element describing the set definition.
        /// </param>
        /// <param name="name">
        /// The name of the object (definition) associated with the set definition.
        /// </param>
        /// <param name="parserContext">
        /// The namespace-aware parser.
        /// </param>
        /// <returns>The set definition.</returns>
        protected Set ParseSetElement(XmlElement collectionEle, string name, ParserContext parserContext)
        {           
            string elementTypeName = GetAttributeValue(collectionEle, "element-type");
            XmlNodeList nl = collectionEle.ChildNodes;
            ManagedSet target = new ManagedSet(nl.Count);

            if (StringUtils.HasText(elementTypeName))
            {
                target.ElementTypeName = elementTypeName;
            }
            target.MergeEnabled = ParseMergeAttribute(collectionEle, parserContext.ParserHelper);

            foreach (XmlNode node in collectionEle.ChildNodes)
            {
                XmlElement ele = node as XmlElement;
                if (ele != null)
                {
                    object sub = ParsePropertySubElement(ele, name, parserContext);
                    target.Add(sub);
                }
            }
            return target;
        }
        public void VisitManagedSet()
        {
            IObjectDefinition od = new RootObjectDefinition();
            ManagedSet ms = new ManagedSet();
            ms.ElementTypeName = "$Property";
            ms.Add("$Property");
            od.PropertyValues.Add("PropertyName", ms);

            ObjectDefinitionVisitor odv = new ObjectDefinitionVisitor(new ObjectDefinitionVisitor.ResolveHandler(ParseAndResolveVariables));
            odv.VisitObjectDefinition(od);

            ManagedSet set = od.PropertyValues.GetPropertyValue("PropertyName").Value as ManagedSet;

            Assert.AreEqual("Value", set.ElementTypeName);
            IEnumerator enumerator = set.GetEnumerator();
            enumerator.MoveNext();
            Assert.AreEqual("Value", enumerator.Current);
        }
        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);
        }
        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);
        }
 /// <summary>
 /// Merges the current value set with that of the supplied object.
 /// </summary>
 /// <remarks>The supplied object is considered the parent, and values in the 
 /// callee's value set must override those of the supplied object.
 /// </remarks>
 /// <param name="parent">The parent object to merge with</param>
 /// <returns>The result of the merge operation</returns>
 /// <exception cref="ArgumentNullException">If the supplied parent is <code>null</code></exception>
 /// <exception cref="InvalidOperationException">If merging is not enabled for this instance,
 /// (i.e. <code>MergeEnabled</code> equals <code>false</code>.</exception>
 public object Merge(object parent)
 {
     if (!this.mergeEnabled)
     {
         throw new InvalidOperationException(
             "Not allowed to merge when the 'MergeEnabled' property is set to 'false'");
     }
     if (parent == null)
     {
         return this;
     }
     ISet pSet = parent as ISet;
     if (pSet == null)
     {
         throw new InvalidOperationException("Cannot merge with object of type [" + parent.GetType() + "]");
     }
     ISet merged = new ManagedSet();
     foreach (object element in pSet)
     {
         merged.Add(element);
     }
     foreach (object o in this)
     {
         merged.Add(o);
     }
     return merged;
 }