From 318d66e1b170a3b57d54d7175cfb3e495f6d7fda Mon Sep 17 00:00:00 2001 From: Luc Maisonobe Date: Sun, 14 Nov 2010 15:32:48 +0000 Subject: [PATCH] allow either specific or generic formats to be null git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1035009 13f79535-47bb-0310-9956-ffa450edef68 --- .../math/exception/util/MessageFactory.java | 14 +++++++---- .../exception/util/MessageFactoryTest.java | 25 ++++++++++++++++++- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/apache/commons/math/exception/util/MessageFactory.java b/src/main/java/org/apache/commons/math/exception/util/MessageFactory.java index 31b5608da0..73884f40de 100644 --- a/src/main/java/org/apache/commons/math/exception/util/MessageFactory.java +++ b/src/main/java/org/apache/commons/math/exception/util/MessageFactory.java @@ -50,8 +50,8 @@ public static String buildMessage(Locale locale, * an argument list. * * @param locale Locale in which the message should be translated. - * @param specific Format specifier. - * @param general Format specifier. + * @param specific Format specifier (may be null). + * @param general Format specifier (may be null). * @param arguments Format arguments. They will be substituted first in * the {@code specific} format specifier, then the remaining arguments * will be substituted in the {@code general} format specifier. @@ -63,7 +63,6 @@ public static String buildMessage(Locale locale, Object ... arguments) { final StringBuilder sb = new StringBuilder(); - final MessageFormat generalFmt = new MessageFormat(general.getLocalizedString(locale), locale); Object[] generalArgs = arguments; if (specific != null) { @@ -80,11 +79,16 @@ public static String buildMessage(Locale locale, // build the message sb.append(specificFmt.format(specificArgs)); - sb.append(": "); } - sb.append(generalFmt.format(generalArgs)); + if (general != null) { + if (specific != null) { + sb.append(": "); + } + final MessageFormat generalFmt = new MessageFormat(general.getLocalizedString(locale), locale); + sb.append(generalFmt.format(generalArgs)); + } return sb.toString(); } diff --git a/src/test/java/org/apache/commons/math/exception/util/MessageFactoryTest.java b/src/test/java/org/apache/commons/math/exception/util/MessageFactoryTest.java index 1bc43f0597..5fa0668f33 100644 --- a/src/test/java/org/apache/commons/math/exception/util/MessageFactoryTest.java +++ b/src/test/java/org/apache/commons/math/exception/util/MessageFactoryTest.java @@ -24,7 +24,7 @@ public class MessageFactoryTest { @Test - public void testSpecificGeneric() { + public void testSpecificGeneral() { Localizable specific = new DummyLocalizable("specific {0} - {1} - {2}"); Localizable general = new DummyLocalizable("general {0} / {1}"); String message = MessageFactory.buildMessage(Locale.FRENCH, specific, general, @@ -32,4 +32,27 @@ public void testSpecificGeneric() { Assert.assertEquals("specific 0 - 1 - 2: general a / b", message); } + @Test + public void testNullSpecific() { + Localizable general = new DummyLocalizable("general {0} / {1}"); + String message = MessageFactory.buildMessage(Locale.FRENCH, null, general, + 'a', 'b'); + Assert.assertEquals("general a / b", message); + } + + @Test + public void testNullGeneral() { + Localizable specific = new DummyLocalizable("specific {0} - {1} - {2}"); + String message = MessageFactory.buildMessage(Locale.FRENCH, specific, null, + 0, 1, 2); + Assert.assertEquals("specific 0 - 1 - 2", message); + } + + + @Test + public void testNull() { + String message = MessageFactory.buildMessage(Locale.FRENCH, null, null, "nothing"); + Assert.assertEquals("", message); + } + }