public void ConfiguresApplicationAndModulesFromTemplate() { StaticApplicationContext appContext = CreateTestContext(); HttpApplicationConfigurer h; RootObjectDefinition rod; h = new HttpApplicationConfigurer(); rod = new RootObjectDefinition(); rod.PropertyValues.Add("TestObject", new RuntimeObjectReference("testObject")); h.ApplicationTemplate = rod; rod = new RootObjectDefinition(); rod.PropertyValues.Add("TestObject", new RuntimeObjectReference("testObject1")); h.ModuleTemplates.Add("TestModule1", rod); rod = new RootObjectDefinition(); rod.PropertyValues.Add("TestObject", new RuntimeObjectReference("testObject2")); h.ModuleTemplates.Add("TestModule2", rod); TestModule m1 = new TestModule(); TestModule m2 = new TestModule(); TestApplication appObject = new TestApplication(new ModuleEntry[] { new ModuleEntry("TestModule1", m1) , new ModuleEntry("TestModule2", m2), }); HttpApplicationConfigurer.Configure(appContext, appObject); // app configured Assert.AreEqual(appContext.GetObject("testObject"), appObject.TestObject); // modules configured Assert.AreEqual(appContext.GetObject("testObject1"), m1.TestObject); Assert.AreEqual(appContext.GetObject("testObject2"), m2.TestObject); }
public void RegisterObjectPrototype() { StaticApplicationContext ctx = new StaticApplicationContext(); ctx.RegisterPrototype("my object", typeof(MockContextAwareObject), null); MockContextAwareObject ctx1 = (MockContextAwareObject) ctx.GetObject("my object"); MockContextAwareObject ctx2 = (MockContextAwareObject) ctx.GetObject("my object"); Assert.IsTrue(ctx1 != ctx2); Assert.IsTrue(!ctx.IsSingleton("my object")); }
public void RegisterObjectPrototype() { StaticApplicationContext ctx = new StaticApplicationContext(); ctx.RegisterPrototype("my object", typeof(MockContextAwareObject), null); MockContextAwareObject ctx1 = (MockContextAwareObject)ctx.GetObject("my object"); MockContextAwareObject ctx2 = (MockContextAwareObject)ctx.GetObject("my object"); Assert.IsTrue(ctx1 != ctx2); Assert.IsTrue(!ctx.IsSingleton("my object")); }
public void Throw_on_get_object_by_wrong_type() { var ctx = new StaticApplicationContext(); ctx.ObjectFactory.RegisterSingleton("test", new SimpleType()); Assert.Throws<ObjectNotOfRequiredTypeException>(() => ctx.GetObject<string>("test")); }
public void Get_named_object() { var ctx = new StaticApplicationContext(); ctx.ObjectFactory.RegisterSingleton("test", new SimpleType()); Assert.That(ctx.GetObject<SimpleType>("test"), Is.Not.Null); }
public void AddPropertyValue() { StaticApplicationContext ac = new StaticApplicationContext(); ac.RegisterSingleton("tb1", typeof(TestObject), new MutablePropertyValues()); ac.RegisterSingleton("tb2", typeof(TestObject), new MutablePropertyValues()); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.Add("Properties", "<spring-config><add key=\"tb1.Age\" value=\"99\"/><add key=\"tb2.Name\" value=\"test\"/></spring-config>"); ac.RegisterSingleton("configurer1", typeof(PropertyOverrideConfigurer), pvs); pvs = new MutablePropertyValues(); pvs.Add("Properties", "<spring-config><add key=\"tb2.Age\" value=\"99\"/><add key=\"tb2.Name\" value=\"test\"/></spring-config>"); pvs.Add("order", "0"); ac.RegisterSingleton("configurer2", typeof(PropertyOverrideConfigurer), pvs); ac.Refresh(); TestObject tb1 = (TestObject)ac.GetObject("tb1"); TestObject tb2 = (TestObject)ac.GetObject("tb2"); Assert.AreEqual(99, tb1.Age); Assert.AreEqual(99, tb2.Age); Assert.AreEqual(null, tb1.Name); Assert.AreEqual("test", tb2.Name); }
private static StaticApplicationContext CreateTestContext() { object testObject; StaticApplicationContext appContext = new StaticApplicationContext(); appContext.RegisterSingleton("testObject", typeof(object), null); appContext.RegisterSingleton("testObject1", typeof(object), null); appContext.RegisterSingleton("testObject2", typeof(object), null); appContext.Refresh(); testObject = appContext.GetObject("testObject"); Assert.IsNotNull(testObject); return(appContext); }
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 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); }
private static StaticApplicationContext CreateTestContext() { object testObject; StaticApplicationContext appContext = new StaticApplicationContext(); appContext.RegisterSingleton("testObject", typeof(object), null); appContext.RegisterSingleton("testObject1", typeof(object), null); appContext.RegisterSingleton("testObject2", typeof(object), null); appContext.Refresh(); testObject = appContext.GetObject("testObject"); Assert.IsNotNull(testObject); return appContext; }
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 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 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 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 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 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 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 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 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); }