This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch 10.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/10.0.x by this push:
     new e83a5e68dd Improve MessageFactory number formatting fixed after 
kkolinko's review
e83a5e68dd is described below

commit e83a5e68ddca59a1c91c3eb71499b7c402879ead
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Wed Aug 31 14:34:52 2022 +0100

    Improve MessageFactory number formatting fixed after kkolinko's review
    
    Refactored MessageFactory so it could be tested.
---
 java/org/apache/el/util/MessageFactory.java     | 43 +++++++++++++++++-------
 test/org/apache/el/util/TestMessageFactory.java | 44 +++++++++++++++++++++++++
 test/org/apache/el/util/TestStrings.properties  | 17 ++++++++++
 3 files changed, 93 insertions(+), 11 deletions(-)

diff --git a/java/org/apache/el/util/MessageFactory.java 
b/java/org/apache/el/util/MessageFactory.java
index 8061aa00ac..de20222e03 100644
--- a/java/org/apache/el/util/MessageFactory.java
+++ b/java/org/apache/el/util/MessageFactory.java
@@ -16,7 +16,9 @@
  */
 package org.apache.el.util;
 
+import java.text.Format;
 import java.text.MessageFormat;
+import java.text.NumberFormat;
 import java.util.MissingResourceException;
 import java.util.ResourceBundle;
 
@@ -25,14 +27,25 @@ import java.util.ResourceBundle;
  */
 public final class MessageFactory {
 
-    static final ResourceBundle bundle =
-            ResourceBundle.getBundle("org.apache.el.Messages");
+    private static final ResourceBundle DEFAULT_BUNDLE = 
ResourceBundle.getBundle("org.apache.el.Messages");
 
-    public MessageFactory() {
-        super();
-    }
+    private static final MessageFactory DEFAULT_MESSAGE_FACTORY = new 
MessageFactory(DEFAULT_BUNDLE);
 
     public static String get(final String key) {
+        return DEFAULT_MESSAGE_FACTORY.getInternal(key);
+    }
+
+    public static String get(final String key, final Object... args) {
+        return DEFAULT_MESSAGE_FACTORY.getInternal(key, args);
+    }
+
+    private final ResourceBundle bundle;
+
+    public MessageFactory(ResourceBundle bundle) {
+        this.bundle = bundle;
+    }
+
+    protected String getInternal(final String key) {
         try {
             return bundle.getString(key);
         } catch (MissingResourceException e) {
@@ -40,20 +53,28 @@ public final class MessageFactory {
         }
     }
 
-    public static String get(final String key, final Object... args) {
-        String value = get(key);
+    protected String getInternal(final String key, final Object... args) {
+        String value = getInternal(key);
 
-        // Convert all Number arguments to String else MessageFormat may try to
-        // format them in unexpected ways.
+        MessageFormat mf = new MessageFormat(value);
+        Format[] formats = null;
+
+        // Unless an argument has been explicitly configured to use a number
+        // format, convert all Number arguments to String else MessageFormat 
may
+        // try to format them in unexpected ways.
         if (args != null) {
             for (int i = 0; i < args.length; i++) {
                 if (args[i] instanceof Number) {
-                    args[i] = args[i].toString();
+                    if (formats == null) {
+                        formats = mf.getFormatsByArgumentIndex();
+                    }
+                    if (!(formats[i] instanceof NumberFormat)) {
+                        args[i] = args[i].toString();
+                    }
                 }
             }
         }
 
-        MessageFormat mf = new MessageFormat(value);
         return mf.format(args, new StringBuffer(), null).toString();
     }
 }
diff --git a/test/org/apache/el/util/TestMessageFactory.java 
b/test/org/apache/el/util/TestMessageFactory.java
new file mode 100644
index 0000000000..106a444404
--- /dev/null
+++ b/test/org/apache/el/util/TestMessageFactory.java
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.el.util;
+
+import java.math.BigDecimal;
+import java.util.ResourceBundle;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestMessageFactory {
+
+    MessageFactory messageFactory = new 
MessageFactory(ResourceBundle.getBundle("org.apache.el.util.TestStrings"));
+
+    @Test
+    public void testFormatNone() {
+        String input = "1E+2";
+        String result = 
messageFactory.getInternal("messageFactory.formatNone", new BigDecimal(input));
+        // Should be unchanged
+        Assert.assertEquals(input, result);
+    }
+
+    @Test
+    public void testFormatNumeric() {
+        String input = "1E+2";
+        String result = 
messageFactory.getInternal("messageFactory.formatNumeric", new 
BigDecimal(input));
+        // Should be formatted as an integer
+        Assert.assertEquals("100", result);
+    }
+}
diff --git a/test/org/apache/el/util/TestStrings.properties 
b/test/org/apache/el/util/TestStrings.properties
new file mode 100644
index 0000000000..c9f719d1a2
--- /dev/null
+++ b/test/org/apache/el/util/TestStrings.properties
@@ -0,0 +1,17 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+messageFactory.formatNone={0}
+messageFactory.formatNumeric={0,number}
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to