From 5288b179f0c99d0bd83c6be65915b9c6e25a2474 Mon Sep 17 00:00:00 2001 From: Luc Maisonobe Date: Sun, 14 Nov 2010 15:33:16 +0000 Subject: [PATCH] allow either specific or generic formats to be null git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/branches/MATH_2_X@1035010 13f79535-47bb-0310-9956-ffa450edef68 --- .../math/exception/util/MessageFactory.java | 14 +++++++---- .../exception/util/MessageFactoryTest.java | 24 ++++++++++++++++++- 2 files changed, 32 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 31b5608da..73884f40d 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 class MessageFactory { * 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 class MessageFactory { 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 class MessageFactory { // 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 1bc43f059..5f59c6185 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 @@ import org.junit.Test; 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,26 @@ public class MessageFactoryTest { 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); + } + }