public void SunnyDay()
        {
            StaticApplicationContext ac = new StaticApplicationContext();

            MutablePropertyValues pvs = new MutablePropertyValues();

            pvs.Add("age", "${maxResults}");
            pvs.Add("name", "${name}");
            ac.RegisterSingleton("tb1", typeof(TestObject), pvs);

            IList variableSources             = new ArrayList();
            CommandLineArgsVariableSource vs1 = new CommandLineArgsVariableSource(
                new string[] { "program.exe", "file.txt", "/name:Aleks Seovic", "/framework:Spring.NET" });

            variableSources.Add(vs1);

            ConfigSectionVariableSource vs2 = new ConfigSectionVariableSource();

            vs2.SectionName = "DaoConfiguration";
            variableSources.Add(vs2);


            pvs = new MutablePropertyValues();
            pvs.Add("VariableSources", variableSources);

            ac.RegisterSingleton("configurer", typeof(VariablePlaceholderConfigurer), pvs);
            ac.Refresh();

            TestObject tb1 = (TestObject)ac.GetObject("tb1");

            Assert.AreEqual(1000, tb1.Age);
            Assert.AreEqual("Aleks Seovic", tb1.Name);
        }
        public void BailsOnUnresolvableVariable()
        {
            StaticApplicationContext ac = new StaticApplicationContext();

            MutablePropertyValues pvs = new MutablePropertyValues();

            pvs.Add("nickname", "${nickname}");
            ac.RegisterSingleton("tb1", typeof(TestObject), pvs);

            IList variableSources = new ArrayList();

            variableSources.Add(new DictionaryVariableSource(new string[] { }));
            pvs = new MutablePropertyValues();
            pvs.Add("VariableSources", variableSources);
            ac.RegisterSingleton("configurer", typeof(VariablePlaceholderConfigurer), pvs);

            try
            {
                ac.Refresh();
                Assert.Fail("something changed wrt VariablePlaceholder resolution");
            }
            catch (ObjectDefinitionStoreException ex)
            {
                Assert.IsTrue(ex.Message.IndexOf("nickname") > -1);
            }
        }
        public void UsesCustomVariablePrefixSuffix()
        {
            StaticApplicationContext ac = new StaticApplicationContext();

            MutablePropertyValues pvs = new MutablePropertyValues();

            pvs.Add("age", "%[maxResults]%");
            pvs.Add("name", "%[name]%");
            ac.RegisterSingleton("tb1", typeof(TestObject), pvs);

            IList variableSources = new ArrayList();

            variableSources.Add(new DictionaryVariableSource(new string[] { "maxResults", "35", "name", "Erich" }));


            pvs = new MutablePropertyValues();
            pvs.Add("VariableSources", variableSources);
            pvs.Add("PlaceholderPrefix", "%[");
            pvs.Add("PlaceholderSuffix", "]%");

            ac.RegisterSingleton("configurer", typeof(VariablePlaceholderConfigurer), pvs);
            ac.Refresh();

            TestObject tb1 = (TestObject)ac.GetObject("tb1");

            Assert.AreEqual(35, tb1.Age);
            Assert.AreEqual("Erich", tb1.Name);
        }
        public void TestSchedulerAutoStartsOnContextRefreshedEventByDefault()
        {
            StaticApplicationContext context = new StaticApplicationContext();

            context.RegisterObjectDefinition("scheduler", new RootObjectDefinition(typeof(SchedulerFactoryObject)));
            IScheduler scheduler = (IScheduler)context.GetObject("scheduler", typeof(IScheduler));

            Assert.IsFalse(scheduler.IsStarted);
            context.Refresh();
            Assert.IsTrue(scheduler.IsStarted);
        }
Example #5
0
        public void WithUnresolvableEnvironmentProperty()
        {
            StaticApplicationContext ac  = new StaticApplicationContext();
            MutablePropertyValues    pvs = new MutablePropertyValues();

            pvs.Add("touchy", "${PROCESSOR_ARCHITECTURE}");
            ac.RegisterSingleton("to", typeof(TestObject), pvs);

            pvs = new MutablePropertyValues();
            pvs.Add("environmentVariableMode", EnvironmentVariableMode.Never);
            ac.RegisterSingleton("configurer", typeof(PropertyPlaceholderConfigurer), pvs);
            ac.Refresh();
        }
        public void WithUnresolvableEnvironmentProperty()
        {
            StaticApplicationContext ac  = new StaticApplicationContext();
            MutablePropertyValues    pvs = new MutablePropertyValues();

            pvs.Add("touchy", "${PROCESSOR_ARCHITECTURE}");
            ac.RegisterSingleton("to", typeof(TestObject), pvs);

            pvs = new MutablePropertyValues();
            pvs.Add("environmentVariableMode", EnvironmentVariableMode.Never);
            ac.RegisterSingleton("configurer", typeof(PropertyPlaceholderConfigurer), pvs);
            Assert.Throws <ObjectDefinitionStoreException>(() => ac.Refresh(), "Error registering object with name 'to' defined in '' : Could not resolve placeholder 'PROCESSOR_ARCHITECTURE'.");
        }
        public void TestSchedulerAutoStartupFalse()
        {
            StaticApplicationContext context        = new StaticApplicationContext();
            ObjectDefinitionBuilder  beanDefinition = ObjectDefinitionBuilder
                                                      .GenericObjectDefinition(typeof(SchedulerFactoryObject))
                                                      .AddPropertyValue("autoStartup", false);

            context.RegisterObjectDefinition("scheduler", beanDefinition.ObjectDefinition);
            IScheduler scheduler = (IScheduler)context.GetObject("scheduler", typeof(IScheduler));

            Assert.IsFalse(scheduler.IsStarted);
            context.Refresh();
            Assert.IsFalse(scheduler.IsStarted);
        }
        public void WithEnvironmentVariableFallback()
        {
            StaticApplicationContext ac  = new StaticApplicationContext();
            MutablePropertyValues    pvs = new MutablePropertyValues();

            pvs.Add("touchy", "${PROCESSOR_ARCHITECTURE}");
            ac.RegisterSingleton("to", typeof(TestObject), pvs);

            pvs = new MutablePropertyValues();
            ac.RegisterSingleton("configurer", typeof(PropertyPlaceholderConfigurer), pvs);
            ac.Refresh();

            TestObject to = (TestObject)ac["to"];

            Assert.AreEqual(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE"),
                            to.Touchy);
        }
Example #9
0
        public void OverridePropertyExpression()
        {
            StaticApplicationContext ac = new StaticApplicationContext();

            MutablePropertyValues pvs = new MutablePropertyValues();

            pvs.Add("Age", new ExpressionHolder("26+1"));
            ac.RegisterSingleton("tb1", typeof(TestObject), pvs);

            pvs = new MutablePropertyValues();
            pvs.Add("Properties", "<spring-config><add key=\"tb1.Age\" value=\"26-1\"/></spring-config>");
            ac.RegisterSingleton("configurer", typeof(PropertyOverrideConfigurer), pvs);

            ac.Refresh();
            TestObject tb1 = (TestObject)ac.GetObject("tb1");

            Assert.AreEqual(25, tb1.Age);
        }
        public void WithUnresolvablePlaceholder()
        {
            StaticApplicationContext ac  = new StaticApplicationContext();
            MutablePropertyValues    pvs = new MutablePropertyValues();

            pvs.Add("name", "${ref}");
            ac.RegisterSingleton("tb", typeof(TestObject), pvs);
            ac.RegisterSingleton("configurer", typeof(PropertyPlaceholderConfigurer), null);
            try
            {
                ac.Refresh();
                Assert.Fail("Should have thrown ObjectDefinitionStoreException");
            }
            catch (ObjectDefinitionStoreException ex)
            {
                // expected
                Assert.IsTrue(ex.Message.IndexOf("ref") != -1);
            }
        }
        public void WithEnvironmentPropertyNotUsed()
        {
            StaticApplicationContext ac  = new StaticApplicationContext();
            MutablePropertyValues    pvs = new MutablePropertyValues();

            pvs.Add("touchy", "${PROCESSOR_ARCHITECTURE}");
            ac.RegisterSingleton("to", typeof(TestObject), pvs);

            pvs = new MutablePropertyValues();
            NameValueCollection nvc = new NameValueCollection();

            nvc.Add("PROCESSOR_ARCHITECTURE", "G5");
            pvs.Add("properties", nvc);
            ac.RegisterSingleton("configurer", typeof(PropertyPlaceholderConfigurer), pvs);
            ac.Refresh();

            TestObject to = (TestObject)ac["to"];

            Assert.AreEqual("G5", to.Touchy, "Fallback mode is not respecting previously set values.");
        }
Example #12
0
        public void OverridePropertyValue()
        {
            StaticApplicationContext ac = new StaticApplicationContext();

            MutablePropertyValues pvs = new MutablePropertyValues();

            pvs.Add("Age", 27);
            pvs.Add("Name", "Bruno");
            ac.RegisterSingleton("tb1", typeof(TestObject), pvs);

            pvs = new MutablePropertyValues();
            pvs.Add("Properties", "<spring-config><add key=\"tb1.Age\" value=\"99\"/><add key=\"tb1.Name\" value=\"test\"/></spring-config>");
            ac.RegisterSingleton("configurer", typeof(PropertyOverrideConfigurer), pvs);

            ac.Refresh();
            TestObject tb1 = (TestObject)ac.GetObject("tb1");

            Assert.AreEqual(99, tb1.Age);
            Assert.AreEqual("test", tb1.Name);
        }
        public void WithExpressionProperty()
        {
            StaticApplicationContext ac  = new StaticApplicationContext();
            MutablePropertyValues    pvs = new MutablePropertyValues();

            pvs.Add("age", new ExpressionHolder("${age}"));
            ac.RegisterSingleton("to1", typeof(TestObject), pvs);

            pvs = new MutablePropertyValues();
            NameValueCollection nvc = new NameValueCollection();

            nvc.Add("age", "'0x7FFFFFFF'");
            pvs.Add("properties", nvc);
            ac.RegisterSingleton("configurer", typeof(PropertyPlaceholderConfigurer), pvs);
            ac.Refresh();


            TestObject to1 = (TestObject)ac.GetObject("to1");;

            Assert.AreEqual(2147483647, to1.Age);
        }
        public void WithOverridingEnvironmentProperty()
        {
            StaticApplicationContext ac  = new StaticApplicationContext();
            MutablePropertyValues    pvs = new MutablePropertyValues();

            pvs.Add("touchy", "${PROCESSOR_ARCHITECTURE}");
            ac.RegisterSingleton("to", typeof(TestObject), pvs);

            pvs = new MutablePropertyValues();
            NameValueCollection nvc = new NameValueCollection();

            nvc.Add("PROCESSOR_ARCHITECTURE", "G5");
            pvs.Add("properties", nvc);
            pvs.Add("environmentVariableMode", EnvironmentVariableMode.Override);
            ac.RegisterSingleton("configurer", typeof(PropertyPlaceholderConfigurer), pvs);
            ac.Refresh();

            TestObject to = (TestObject)ac["to"];

            Assert.AreEqual(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE"),
                            to.Touchy);
        }
Example #15
0
        public void OverridePropertyReference()
        {
            StaticApplicationContext ac = new StaticApplicationContext();

            MutablePropertyValues pvs = new MutablePropertyValues();

            pvs.Add("Spouse", new RuntimeObjectReference("spouse1"));
            ac.RegisterSingleton("tb1", typeof(TestObject), pvs);

            ac.RegisterSingleton("spouse1", typeof(TestObject), new MutablePropertyValues());
            ac.RegisterSingleton("spouse2", typeof(TestObject), new MutablePropertyValues());

            pvs = new MutablePropertyValues();
            pvs.Add("Properties", "<spring-config><add key=\"tb1.Spouse\" value=\"spouse2\"/></spring-config>");
            ac.RegisterSingleton("configurer", typeof(PropertyOverrideConfigurer), pvs);

            ac.Refresh();
            TestObject tb1     = (TestObject)ac.GetObject("tb1");
            TestObject spouse2 = (TestObject)ac.GetObject("spouse2");

            Assert.AreEqual(spouse2, tb1.Spouse);
        }
        public void WithCircularReference()
        {
            StaticApplicationContext ac  = new StaticApplicationContext();
            MutablePropertyValues    pvs = new MutablePropertyValues();

            pvs.Add("age", "${age}");
            pvs.Add("name", "name${var}");
            pvs.Add("spouse", new RuntimeObjectReference("${ref}"));
            ac.RegisterSingleton("tb1", typeof(TestObject), pvs);
            pvs = new MutablePropertyValues();
            pvs.Add("age", "${age}");
            pvs.Add("name", "name${age}");
            ac.RegisterSingleton("tb2", typeof(TestObject), pvs);
            pvs = new MutablePropertyValues();
            pvs.Add("Properties", "<spring-config><add key=\"age\" value=\"99\"/><add key=\"var\" value=\"${m}var\"/><add key=\"ref\" value=\"tb2\"/><add key=\"m\" value=\"${var}\"/></spring-config>");
            ac.RegisterSingleton("configurer1", typeof(PropertyPlaceholderConfigurer), pvs);
            pvs = new MutablePropertyValues();
            pvs.Add("Properties", "<spring-config><add key=\"age\" value=\"98\"/></spring-config>");
            pvs.Add("order", "0");
            ac.RegisterSingleton("configurer2", typeof(PropertyPlaceholderConfigurer), pvs);
            Assert.Throws <ObjectDefinitionStoreException>(() => ac.Refresh());
        }
        public void WhitespaceHandling()
        {
            StaticApplicationContext ac = new StaticApplicationContext();

            MutablePropertyValues pvs = new MutablePropertyValues();

            pvs.Add("name", "${name}");
            pvs.Add("nickname", "${nickname}");
            ac.RegisterSingleton("tb1", typeof(TestObject), pvs);

            IList variableSources = new ArrayList();

            variableSources.Add(new DictionaryVariableSource(new string[] { "name", string.Empty, "nickname", null }));
            pvs = new MutablePropertyValues();
            pvs.Add("VariableSources", variableSources);
            ac.RegisterSingleton("configurer", typeof(VariablePlaceholderConfigurer), pvs);
            ac.Refresh();

            TestObject tb1 = (TestObject)ac.GetObject("tb1");

            Assert.AreEqual(string.Empty, tb1.Name);
            Assert.AreEqual(null, tb1.Nickname);
        }
        public void IgnoresUnresolvableVariable()
        {
            StaticApplicationContext ac = new StaticApplicationContext();

            MutablePropertyValues pvs = new MutablePropertyValues();

            pvs.Add("name", "${name}");
            pvs.Add("nickname", "${nickname}");
            ac.RegisterSingleton("tb1", typeof(TestObject), pvs);

            VariablePlaceholderConfigurer vpc = new VariablePlaceholderConfigurer();

            vpc.IgnoreUnresolvablePlaceholders = true;
            vpc.VariableSource = new DictionaryVariableSource(new string[] { "name", "Erich" });
            ac.AddObjectFactoryPostProcessor(vpc);

            ac.Refresh();

            TestObject tb1 = (TestObject)ac.GetObject("tb1");

            Assert.AreEqual("Erich", tb1.Name);
            Assert.AreEqual("${nickname}", tb1.Nickname);
        }
        public void ChainedResolution()
        {
            StaticApplicationContext ac = new StaticApplicationContext();

            MutablePropertyValues pvs = new MutablePropertyValues();

            pvs.Add("name", "${name}");
            pvs.Add("nickname", "${nickname}");
            ac.RegisterSingleton("tb1", typeof(TestObject), pvs);

            IList variableSources = new ArrayList();

            variableSources.Add(new DictionaryVariableSource(new string[] { "name", "name-value" }));
            variableSources.Add(new DictionaryVariableSource(new string[] { "nickname", "nickname-value" }));
            VariablePlaceholderConfigurer vphc = new VariablePlaceholderConfigurer(variableSources);

            ac.AddObjectFactoryPostProcessor(vphc);
            ac.Refresh();

            TestObject tb1 = (TestObject)ac.GetObject("tb1");

            Assert.AreEqual("name-value", tb1.Name);
            Assert.AreEqual("nickname-value", tb1.Nickname);
        }
        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);
        }