コード例 #1
0
 public void AddMessageTest()
 {
     StaticApplicationContext ctx = new StaticApplicationContext();
     ctx.Refresh();
     ctx.AddMessage("code1", CultureInfo.CurrentUICulture, "this is {0}");
     Assert.AreEqual("this is Spring.NET", ctx.GetMessage("code1", "Spring.NET"));
 }
コード例 #2
0
        public void AddMessageTest()
        {
            StaticApplicationContext ctx = new StaticApplicationContext();

            ctx.Refresh();
            ctx.AddMessage("code1", CultureInfo.CurrentUICulture, "this is {0}");
            Assert.AreEqual("this is Spring.NET", ctx.GetMessage("code1", "Spring.NET"));
        }
コード例 #3
0
        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);
        }
コード例 #4
0
 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);
 }
コード例 #5
0
        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);
        }
コード例 #6
0
        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);
            }
        }
コード例 #7
0
        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);
        }
コード例 #8
0
 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);
     }
 }
コード例 #9
0
        public void DoTestMessageAccess(bool hasParentContext, bool useCodeAsDefaultMessage)
        {
            StaticApplicationContext ac = new StaticApplicationContext();
            if (hasParentContext)
            {
                StaticApplicationContext parent = new StaticApplicationContext();
                parent.Refresh();
                ac.ParentContext = parent;
            }

            MutablePropertyValues pvs = new MutablePropertyValues();
            pvs.Add("resourceManagers", resourceManagerList);
            if (useCodeAsDefaultMessage)
            {
                pvs.Add("UseCodeAsDefaultMessage", true);
            }
            ac.RegisterSingleton("messageSource", typeof(ResourceSetMessageSource), pvs);
            ac.Refresh();


            // Localizaiton fallbacks
            GetMessageLocalizationFallbacks(ac);

            // MessageSourceAccessor functionality
            MessageSourceAccessor accessor = new MessageSourceAccessor(ac);
            Assert.AreEqual("message3", accessor.GetMessage("code3", CultureInfo.CurrentUICulture, (object[])null));

            // IMessageSourceResolveable
            Assert.AreEqual("message3", ac.GetMessage("code3", CultureInfo.CurrentUICulture, (object[])null));
            IMessageSourceResolvable resolvable = new DefaultMessageSourceResolvable("code3");

            Assert.AreEqual("message3", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture));
            resolvable = new DefaultMessageSourceResolvable(new string[] { "code4", "code3" });
            Assert.AreEqual("message3", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture));

            Assert.AreEqual("message3", ac.GetMessage("code3", CultureInfo.CurrentUICulture, (object[])null));
            resolvable = new DefaultMessageSourceResolvable(new string[] { "code4", "code3" });
            Assert.AreEqual("message3", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture));

            object[] arguments = new object[] { "Hello", new DefaultMessageSourceResolvable(new string[] { "code1" }) };
            Assert.AreEqual("Hello, message1", ac.GetMessage("hello", CultureInfo.CurrentUICulture, arguments));


            // test default message without and with args
            Assert.AreEqual("default", ac.GetMessage(null, "default", CultureInfo.CurrentUICulture, null));
            Assert.AreEqual("default", ac.GetMessage(null, "default", CultureInfo.CurrentUICulture, arguments));

            /* not supported 
            Assert.AreEqual("{0}, default", ac.GetMessage(null, "{0}, default", CultureInfo.CurrentUICulture, null));
             */

            Assert.AreEqual("Hello, default", ac.GetMessage(null, "{0}, default", CultureInfo.CurrentUICulture, arguments));

            // test resolvable with default message, without and with args
            resolvable = new DefaultMessageSourceResolvable(null, null, "default");
            Assert.AreEqual("default", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture));
            resolvable = new DefaultMessageSourceResolvable(null, arguments, "default");
            Assert.AreEqual("default", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture));

            /* not supported 
                resolvable = new DefaultMessageSourceResolvable(null, null, "{0}, default");
                Assert.AreEqual("{0}, default", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture));
            */

            resolvable = new DefaultMessageSourceResolvable(null, arguments, "{0}, default");
            Assert.AreEqual("Hello, default", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture));


            // test message args
            Assert.AreEqual("Arg1, Arg2", ac.GetMessage("hello", CultureInfo.CurrentUICulture, new object[] { "Arg1", "Arg2" }));

            /* not supported 
                Assert.AreEqual("{0}, {1}", ac.GetMessage("hello", CultureInfo.CurrentUICulture, null));
            */


            /* not supported 
                Assert.AreEqual("Hello\nWorld", ac.GetMessage("escaped"));
            }
            else
            {
                Assert.AreEqual("Hello\\nWorld", ac.GetMessage("escaped"));
            }
            */


            try
            {
                Assert.AreEqual("MyNonExistantMessage", ac.GetMessage("MyNonExistantMessage"));
                if (!useCodeAsDefaultMessage)
                {
                    Assert.Fail("Should have thrown NoSuchMessagException");
                }
            }
            catch (NoSuchMessageException e)
            {
                if (useCodeAsDefaultMessage)
                {
                    Assert.Fail("Should have returned code as default message.");
                    e.ToString();
                }
            }

        }
コード例 #10
0
        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);
        }
コード例 #11
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();
        }
コード例 #12
0
        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);
        }
コード例 #13
0
        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);
        }
コード例 #14
0
        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);
        }
コード例 #15
0
        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);
        }
コード例 #16
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);
        }
コード例 #17
0
        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);

        }
コード例 #18
0
        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);
        }
コード例 #19
0
 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);
 }
コード例 #20
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);
            Assert.Throws<ObjectDefinitionStoreException>(() => ac.Refresh(), "Error registering object with name 'to' defined in '' : Could not resolve placeholder 'PROCESSOR_ARCHITECTURE'.");
		}
コード例 #21
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);
        }
コード例 #22
0
 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);
     ac.Refresh();
 }
コード例 #23
0
 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;
 }
コード例 #24
0
        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.");
        }
コード例 #25
0
        public void DoTestMessageAccess(bool hasParentContext, bool useCodeAsDefaultMessage)
        {
            StaticApplicationContext ac = new StaticApplicationContext();

            if (hasParentContext)
            {
                StaticApplicationContext parent = new StaticApplicationContext();
                parent.Refresh();
                ac.ParentContext = parent;
            }

            MutablePropertyValues pvs = new MutablePropertyValues();

            pvs.Add("resourceManagers", resourceManagerList);
            if (useCodeAsDefaultMessage)
            {
                pvs.Add("UseCodeAsDefaultMessage", true);
            }
            ac.RegisterSingleton("messageSource", typeof(ResourceSetMessageSource), pvs);
            ac.Refresh();


            // Localizaiton fallbacks
            GetMessageLocalizationFallbacks(ac);

            // MessageSourceAccessor functionality
            MessageSourceAccessor accessor = new MessageSourceAccessor(ac);

            Assert.AreEqual("message3", accessor.GetMessage("code3", CultureInfo.CurrentUICulture, (object[])null));

            // IMessageSourceResolveable
            Assert.AreEqual("message3", ac.GetMessage("code3", CultureInfo.CurrentUICulture, (object[])null));
            IMessageSourceResolvable resolvable = new DefaultMessageSourceResolvable("code3");

            Assert.AreEqual("message3", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture));
            resolvable = new DefaultMessageSourceResolvable(new string[] { "code4", "code3" });
            Assert.AreEqual("message3", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture));

            Assert.AreEqual("message3", ac.GetMessage("code3", CultureInfo.CurrentUICulture, (object[])null));
            resolvable = new DefaultMessageSourceResolvable(new string[] { "code4", "code3" });
            Assert.AreEqual("message3", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture));

            object[] arguments = new object[] { "Hello", new DefaultMessageSourceResolvable(new string[] { "code1" }) };
            Assert.AreEqual("Hello, message1", ac.GetMessage("hello", CultureInfo.CurrentUICulture, arguments));


            // test default message without and with args
            Assert.AreEqual("default", ac.GetMessage(null, "default", CultureInfo.CurrentUICulture, null));
            Assert.AreEqual("default", ac.GetMessage(null, "default", CultureInfo.CurrentUICulture, arguments));

            /* not supported
             * Assert.AreEqual("{0}, default", ac.GetMessage(null, "{0}, default", CultureInfo.CurrentUICulture, null));
             */

            Assert.AreEqual("Hello, default", ac.GetMessage(null, "{0}, default", CultureInfo.CurrentUICulture, arguments));

            // test resolvable with default message, without and with args
            resolvable = new DefaultMessageSourceResolvable(null, null, "default");
            Assert.AreEqual("default", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture));
            resolvable = new DefaultMessageSourceResolvable(null, arguments, "default");
            Assert.AreEqual("default", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture));

            /* not supported
             *  resolvable = new DefaultMessageSourceResolvable(null, null, "{0}, default");
             *  Assert.AreEqual("{0}, default", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture));
             */

            resolvable = new DefaultMessageSourceResolvable(null, arguments, "{0}, default");
            Assert.AreEqual("Hello, default", ac.GetMessage(resolvable, CultureInfo.CurrentUICulture));


            // test message args
            Assert.AreEqual("Arg1, Arg2", ac.GetMessage("hello", CultureInfo.CurrentUICulture, new object[] { "Arg1", "Arg2" }));

            /* not supported
             *  Assert.AreEqual("{0}, {1}", ac.GetMessage("hello", CultureInfo.CurrentUICulture, null));
             */


            /* not supported
             *  Assert.AreEqual("Hello\nWorld", ac.GetMessage("escaped"));
             * }
             * else
             * {
             *  Assert.AreEqual("Hello\\nWorld", ac.GetMessage("escaped"));
             * }
             */


            try
            {
                Assert.AreEqual("MyNonExistantMessage", ac.GetMessage("MyNonExistantMessage"));
                if (!useCodeAsDefaultMessage)
                {
                    Assert.Fail("Should have thrown NoSuchMessagException");
                }
            }
            catch (NoSuchMessageException e)
            {
                if (useCodeAsDefaultMessage)
                {
                    Assert.Fail("Should have returned code as default message.");
                    e.ToString();
                }
            }
        }
コード例 #26
0
        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);
        }