Skip to content

Commit

Permalink
allow either specific or generic formats to be null
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1035009 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
Luc Maisonobe committed Nov 14, 2010
1 parent 183ad33 commit 318d66e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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) {
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,35 @@
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,
0, 1, 2, 'a', 'b');
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);
}

}

0 comments on commit 318d66e

Please sign in to comment.