This is an automated email from the ASF dual-hosted git repository. kkolinko pushed a commit to branch 8.5.x in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/8.5.x by this push: new f3d2931ea0 Further improve MessageFactory number formatting fix. f3d2931ea0 is described below commit f3d2931ea090027ed68d0c9d50856979e4480c7a Author: Konstantin Kolinko <kkoli...@apache.org> AuthorDate: Fri Sep 9 13:57:42 2022 +0300 Further improve MessageFactory number formatting fix. Handle the case when message patterns does not print all arguments. E.g. when a translation has not been updated. In that case the array returned by getFormatsByArgumentIndex() call will be shorter than args array. --- java/org/apache/el/util/MessageFactory.java | 2 +- test/org/apache/el/util/TestMessageFactory.java | 20 +++++++++++++++++++- test/org/apache/el/util/TestStrings.properties | 4 +++- webapps/docs/changelog.xml | 2 +- 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/java/org/apache/el/util/MessageFactory.java b/java/org/apache/el/util/MessageFactory.java index de20222e03..b51fb545d1 100644 --- a/java/org/apache/el/util/MessageFactory.java +++ b/java/org/apache/el/util/MessageFactory.java @@ -68,7 +68,7 @@ public final class MessageFactory { if (formats == null) { formats = mf.getFormatsByArgumentIndex(); } - if (!(formats[i] instanceof NumberFormat)) { + if (i < formats.length && !(formats[i] instanceof NumberFormat)) { args[i] = args[i].toString(); } } diff --git a/test/org/apache/el/util/TestMessageFactory.java b/test/org/apache/el/util/TestMessageFactory.java index 106a444404..297ddeac1b 100644 --- a/test/org/apache/el/util/TestMessageFactory.java +++ b/test/org/apache/el/util/TestMessageFactory.java @@ -29,7 +29,10 @@ public class TestMessageFactory { @Test public void testFormatNone() { String input = "1E+2"; - String result = messageFactory.getInternal("messageFactory.formatNone", new BigDecimal(input)); + // Trailing '0" is an extra Number argument, not printed by the + // message pattern. It reflects the case when a translation has not + // been updated with new arguments. + String result = messageFactory.getInternal("messageFactory.formatNone", new BigDecimal(input), 0 /*ignored*/); // Should be unchanged Assert.assertEquals(input, result); } @@ -41,4 +44,19 @@ public class TestMessageFactory { // Should be formatted as an integer Assert.assertEquals("100", result); } + + @Test + public void testFormatChoice() { + String input = "1E+2"; + String result = messageFactory.getInternal("messageFactory.formatChoice", new BigDecimal(input), 0 /*ignored*/); + // Should be formatted as an integer + Assert.assertEquals("100 is enough", result); + } + + @Test + public void testFormatNoArguments() { + String input = "1E+2"; + String result = messageFactory.getInternal("messageFactory.formatNoArguments", new BigDecimal(input), 0 /*ignored*/); + Assert.assertEquals("A message", result); + } } diff --git a/test/org/apache/el/util/TestStrings.properties b/test/org/apache/el/util/TestStrings.properties index c9f719d1a2..c9e7e9544c 100644 --- a/test/org/apache/el/util/TestStrings.properties +++ b/test/org/apache/el/util/TestStrings.properties @@ -14,4 +14,6 @@ # limitations under the License. messageFactory.formatNone={0} -messageFactory.formatNumeric={0,number} \ No newline at end of file +messageFactory.formatNumeric={0,number} +messageFactory.formatChoice={0,choice,0#{0,number} is too few|99#{0,number} is enough|100<{0,number} is too many} +messageFactory.formatNoArguments=A message diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index 74e8e09eb6..04f9d6f844 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -198,7 +198,7 @@ </fix> <fix> Improve handling of EL error messages so instances of Number are not - formatted in unexpected ways. (markt) + formatted in unexpected ways. (markt/kkolinko) </fix> <fix> Switch to using ELException rather than IllegalArgumentException when a --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org