/// <summary>
		/// Outputs the supplied <see cref="Spring.Context.IMessageSourceResolvable"/>
		/// as a nicely formatted <see cref="System.String"/>.
		/// </summary>
		/// <param name="resolvable">
		/// The <see cref="Spring.Context.IMessageSourceResolvable"/> to output.
		/// </param>
		public string VisitMessageSourceResolvableString(
			IMessageSourceResolvable resolvable)
		{
			StringBuilder builder = new StringBuilder();
			builder.Append("codes=[");
			builder.Append(StringUtils.ArrayToDelimitedString(resolvable.GetCodes(), ","));
			builder.Append("]; arguments=[");
			if (resolvable.GetArguments() == null)
			{
				builder.Append("null");
			}
			else
			{
				object[] arguments = resolvable.GetArguments();
				for (int i = 0; i < arguments.Length; i++)
				{
					builder.Append("(");
					builder.Append(arguments[i].GetType().Name);
					builder.Append(")[");
					builder.Append(arguments[i]);
					builder.Append("]");
					if (i < arguments.Length - 1)
					{
						builder.Append(", ");
					}
				}
			}
			builder.Append("]; defaultMessage=[");
			builder.Append(resolvable.DefaultMessage);
			builder.Append("]");
			return builder.ToString();
		}
Ejemplo n.º 2
0
            public string GetMessage(IMessageSourceResolvable resolvable, CultureInfo culture)
            {
                if (StringUtils.HasText(resolvable.DefaultMessage))
                {
                    return(resolvable.DefaultMessage);
                }
                IList <string> codes = resolvable.GetCodes();
                string         code  = (codes != null && codes.Count > 0 ? codes[0] : string.Empty);

                throw new NoSuchMessageException(code, culture);
            }
Ejemplo n.º 3
0
        public void GetMessageResolvableValidMessageAndCodNullMessageFormat()
        {
            IMessageSourceResolvable res = mocks.StrictMock <IMessageSourceResolvable>();

            Expect.Call(res.DefaultMessage).Return("myDefaultMessage").Repeat.AtLeastOnce();
            Expect.Call(res.GetCodes()).Return(new string[] { "nullCode" });
            Expect.Call(res.GetArguments()).Return(null).Repeat.AtLeastOnce();
            mocks.ReplayAll();

            Assert.AreEqual("myDefaultMessage", GetMessage(res, null), "Resolve");
            mocks.VerifyAll();
        }
Ejemplo n.º 4
0
        public void GetMessageResolvableReturnsFirstCode()
        {
            IMessageSourceResolvable res = mocks.StrictMock <IMessageSourceResolvable>();

            Expect.Call(res.DefaultMessage).Return(null).Repeat.AtLeastOnce();
            Expect.Call(res.GetCodes()).Return(new string[] { "null" });
            Expect.Call(res.GetArguments()).Return(null).Repeat.AtLeastOnce();
            mocks.ReplayAll();
            UseCodeAsDefaultMessage = true;
            Assert.AreEqual("null", GetMessage(res, CultureInfo.CurrentCulture), "Code");
            mocks.VerifyAll();
        }
Ejemplo n.º 5
0
        public void GetMessageResolvableDefaultMessage()
        {
            IMessageSourceResolvable res = mocks.StrictMock <IMessageSourceResolvable>();

            Expect.Call(res.DefaultMessage).Return("MyDefaultMessage").Repeat.AtLeastOnce();
            Expect.Call(res.GetCodes()).Return(null);
            Expect.Call(res.GetArguments()).Return(null);
            mocks.ReplayAll();

            Assert.AreEqual("MyDefaultMessage", GetMessage(res, CultureInfo.CurrentCulture), "Default");

            mocks.VerifyAll();
        }
Ejemplo n.º 6
0
        public void DefaultResolvableFromExistingResolvable()
        {
            IMessageSourceResolvable res = A.Fake <IMessageSourceResolvable>();

            A.CallTo(() => res.DefaultMessage).Returns("defaultMessageFromMock");
            A.CallTo(() => res.GetCodes()).Returns(new string[] { "code1FromMock" });
            A.CallTo(() => res.GetArguments()).Returns(new object[] { "ArgumentFromMock" });

            DefaultMessageSourceResolvable dmr = new DefaultMessageSourceResolvable(res);

            Assert.AreEqual("defaultMessageFromMock", dmr.DefaultMessage, "default");
            Assert.AreEqual("code1FromMock", dmr.LastCode, "codes");
            Assert.AreEqual("ArgumentFromMock", (dmr.GetArguments())[0], "arguments");
        }
        public void DefaultResolvableFromExistingResolvable()
        {
            IMessageSourceResolvable res = mocks.StrictMock <IMessageSourceResolvable>();

            Expect.Call(res.DefaultMessage).Return("defaultMessageFromMock").Repeat.AtLeastOnce();
            Expect.Call(res.GetCodes()).Return(new string[] { "code1FromMock" });
            Expect.Call(res.GetArguments()).Return(new object[] { "ArgumentFromMock" }).Repeat.AtLeastOnce();
            mocks.ReplayAll();
            DefaultMessageSourceResolvable dmr = new DefaultMessageSourceResolvable(res);

            Assert.AreEqual("defaultMessageFromMock", dmr.DefaultMessage, "default");
            Assert.AreEqual("code1FromMock", dmr.LastCode, "codes");
            Assert.AreEqual("ArgumentFromMock", (dmr.GetArguments())[0], "arguments");
            mocks.VerifyAll();
        }
        public void GetResolvableDefaultsToParentMessageSource()
        {
            string MSGCODE = "nullCode";

            object[] MSGARGS = new object[] { "arg1", "arg2" };

            IMessageSourceResolvable res = A.Fake <IMessageSourceResolvable>();

            A.CallTo(() => res.GetArguments()).Returns(MSGARGS);
            A.CallTo(() => res.GetCodes()).Returns(new string[] { MSGCODE }).Once();

            IMessageSource parentSource = A.Fake <IMessageSource>();

            A.CallTo(() => parentSource.GetMessage(MSGCODE, null, CultureInfo.CurrentCulture, A <object[]> ._)).Returns("MockMessageSource");
            ParentMessageSource = parentSource;

            Assert.AreEqual("MockMessageSource", GetMessage(res, CultureInfo.CurrentCulture), "My Message");
        }
Ejemplo n.º 9
0
        public void GetResolvableDefaultsToParentMessageSource()
        {
            string MSGCODE = "nullCode";

            object[] MSGARGS = new object[] { "arg1", "arg2" };

            IMessageSourceResolvable res = mocks.StrictMock <IMessageSourceResolvable>();

            Expect.Call(res.GetArguments()).Return(MSGARGS);
            Expect.Call(res.GetCodes()).Return(new string[] { MSGCODE }).Repeat.Once();

            IMessageSource parentSource = mocks.StrictMock <IMessageSource>();

            Expect.Call(parentSource.GetMessage(MSGCODE, null, CultureInfo.CurrentCulture, MSGARGS)).Return(
                "MockMessageSource");
            ParentMessageSource = parentSource;

            mocks.ReplayAll();
            Assert.AreEqual("MockMessageSource", GetMessage(res, CultureInfo.CurrentCulture), "My Message");
            mocks.VerifyAll();
        }
		/// <summary>
		/// Creates a new instance of the
		/// <see cref="DefaultMessageSourceResolvable"/> class
		/// from another resolvable.
		/// </summary>
		/// <remarks>
		/// <p>
		/// This is the <i>copy constructor</i> for the
		/// <see cref="DefaultMessageSourceResolvable"/> class.
		/// </p>
		/// </remarks>
		/// <param name="resolvable">
		/// The <see cref="Spring.Context.IMessageSourceResolvable"/> to be copied.
		/// </param>
		/// <exception cref="System.NullReferenceException">
		/// If the supplied <paramref name="resolvable"/> is <see langword="null"/>.
		/// </exception>
		public DefaultMessageSourceResolvable(IMessageSourceResolvable resolvable)
			: this(resolvable.GetCodes(), resolvable.GetArguments(), resolvable.DefaultMessage)
		{
		}
Ejemplo n.º 11
0
 public string GetMessage(IMessageSourceResolvable resolvable, CultureInfo culture)
 {
     if (StringUtils.HasText(resolvable.DefaultMessage))
     {
         return resolvable.DefaultMessage;
     }
     IList<string> codes = resolvable.GetCodes();
     string code = (codes != null && codes.Count > 0 ? codes[0] : string.Empty);
     throw new NoSuchMessageException(code, culture);
 }
Ejemplo n.º 12
0
 /// <summary>
 /// Creates a new instance of the
 /// <see cref="DefaultMessageSourceResolvable"/> class
 /// from another resolvable.
 /// </summary>
 /// <remarks>
 /// <p>
 /// This is the <i>copy constructor</i> for the
 /// <see cref="DefaultMessageSourceResolvable"/> class.
 /// </p>
 /// </remarks>
 /// <param name="resolvable">
 /// The <see cref="Spring.Context.IMessageSourceResolvable"/> to be copied.
 /// </param>
 /// <exception cref="System.NullReferenceException">
 /// If the supplied <paramref name="resolvable"/> is <see langword="null"/>.
 /// </exception>
 public DefaultMessageSourceResolvable(IMessageSourceResolvable resolvable)
     : this(resolvable.GetCodes(), resolvable.GetArguments(), resolvable.DefaultMessage)
 {
 }
 /// <summary>
 /// Resolve the message using all of the attributes contained within
 /// the supplied <see cref="Spring.Context.IMessageSourceResolvable"/>
 /// argument.
 /// </summary>
 /// <param name="resolvable">The value object storing those attributes that are required to
 /// properly resolve a message.</param>
 /// <param name="culture">The <see cref="System.Globalization.CultureInfo"/> that represents
 /// the culture for which the resource is localized.</param>
 /// <returns>
 /// The resolved message if the lookup was successful.
 /// </returns>
 /// <exception cref="Spring.Context.NoSuchMessageException">
 /// If the message could not be resolved.
 /// </exception>
 public string GetMessage(IMessageSourceResolvable resolvable, CultureInfo culture)
 {
     string[] codes = resolvable.GetCodes();
     if (codes == null) codes = new string[0];
     for (int i = 0; i < codes.Length; i++)
     {
         string msg = GetMessageInternal(codes[i], resolvable.GetArguments(), culture);
         if (msg != null) return msg;
     }
     if (resolvable.DefaultMessage != null)
         return RenderDefaultMessage(resolvable.DefaultMessage, resolvable.GetArguments(), culture);
     if (codes.Length > 0)
     {
         string fallback = GetDefaultMessage(codes[0]);
         if (fallback != null) return fallback;
     }
     throw new NoSuchMessageException(codes.Length > 0 ? codes[codes.Length - 1] : null, culture);
 }