Exemple #1
0
        /// <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);
        }
        /// <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 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=\"Oragon.Spring.Objects.TestObject, Oragon.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);
        }