Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4503190

[Fmt-Nu] Method java.text.DecimalFormat.applyPattern(String pattern) works incorrectly.

    XMLWordPrintable

Details

    • sparc
    • solaris_2.6

    Description

      Name: dsR10051 Date: 09/14/2001



      The method of class java.text.DecimalFormat
      public void applyPattern(String pattern)
      works incorrectly if the suffix of the pattern
      contains the special characters.
      In this case method can throw IllegalArgumentException
      or can ignore the special characters and continue normal job processing.

      The javadoc for pattern syntax of java.text.DecimalFormat says:
      (captured from jdk1.3 on account of simplicity of declaration.

      pattern := pos_pattern{';' neg_pattern}
      pos_pattern := {prefix}number{suffix}
      suffix := '\u0000'..'\uFFFD' - special_characters

      and for Special Pattern Characters:
      "They must be quoted, unless noted otherwise, if
      they are to appear in the prefix or suffix as literals."

      So, suffix that contains any special characters should be considered as invalid
      and IllegalArgumentException should be thrown, unless noted otherwise.

      If suffix contains the special character that is first entry in the pattern,
      the IllegalArgumentException is thrown by the applyPattern method.
      But if number (shown above in the second line of the pattern syntax) contains
      this special character, applyPattern ignores it and set suffix
      of the DecimalFormat object to the suffix pattern without this character.
      For example, if "#.#suffix0" pattern value is passed to the applyPattern method,
      the IllegalArgumentException is thrown by it. But if the pattern is
      "0.0suffix0", DecimalFormat suffix is setted to "suffix" and no
      exception is thrown.

      Here is minimized test:

      import java.text.*;

      public class DecimalFormatTest01 {

          public static void main (String[] args) {
              String pattern1 = "#.#suffix0";
              String pattern2 = "0.0suffix0";
              DecimalFormat df = new DecimalFormat();
              try {
                  df.applyPattern(pattern1);
                  System.out.println("FAILED: IllegalArgumentException expected for pattern " + pattern1);
                  System.out.println("Suffix: " + df.getPositiveSuffix());
              } catch (IllegalArgumentException iae) {
                  // OKAY
              }
              try {
                  df.applyPattern(pattern2);
                  System.out.println("FAILED: IllegalArgumentException expected for pattern " + pattern2);
                  System.out.println("Suffix: " + df.getPositiveSuffix());
              } catch (IllegalArgumentException iae) {
                  // OKAY
              }
          }
      }
      --- Output ---
      $ /set/jdk-builds/JDK1.4.0beta2-b78/solaris/bin/java DecimalFormatTest01
      FAILED: IllegalArgumentException expected for pattern 0.0suffix0
      Suffix: suffix
      $
      ________________

      Here is the letter from IBM that sent to jck-comments.

      12) api/java_text/DecimalFormat/index.html#Pattern: DecimalFormat
      applyPattern and toPattern tests subtests: 0044, 0055, 0058

      Test Description:

      These tests perform domain testing of input and output conditions, and
      external pre-conditions for class DecimalFormat, method public void
      applyPattern(String pattern).

      Specification:

      The Java? 2 Platform, Standard Edition, v1.3 API Specification
      (java.lang.DecimalFormat): "Illegal patterns, such as "#.#.#" or "#.###,
      ###", will cause DecimalFormat to throw an IllegalArgumentException with a
      message that describes the problem."
      The Java? 2 Platform, Standard Edition, v1.3 API Specification
      (java.lang.DecimalFormat pattern syntax):
      "number := integer{'.' fraction}{exponent}
      prefix := '\u0000'..'\uFFFD' - special_characters
      suffix := '\u0000'..'\uFFFD' - special_characters
      integer := min_int | '#' | '#' integer | '#' ',' integer
      min_int := '0' | '0' min_int | '0' ',' min_int
      fraction := '0'* '#'* exponent := 'E' '0' '0'*"

      Rationale to Exclude:

      Two of the applyPattern tests call the method applyPattern with string
      argument "###,000.000suffix2##". By definintion in the API specification,
      only characters from '\u0000' through '\uFFFD', excluding the special
      characters, are valid for the suffix. The specificaiton defines '#' as a
      special character, hence pattern "###,000.000suffix2##" is invalid and
      should throw an IllegalArgumentException. For this reason, this test should
      be excluded because it is testing Sun's implementation of the applyPattern
      () method, which allows for such an invalid pattern, and not the
      specification.
      --- end of letter ---

      ======================================================================

      Attachments

        Issue Links

          Activity

            People

              nishjain Nishit Jain
              sdasunw Sda Sda (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: