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"));
 }
        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"));
        }
        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();
                }
            }
        }
        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();
                }
            }

        }