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

XMLGregorianCalendar.compare makes mistakes for some values

XMLWordPrintable

    • b35
    • generic
    • generic
    • Verified



      Name: erR10175 Date: 12/25/2003


       
        The following method of the class javax.xml.datatype.XMLGregorianCalendar
        
      public static int compare(XMLGregorianCalendar lhs, XMLGregorianCalendar rhs)

      returns wrong results for the following pairs of XMLGregorianCalendar:
      --------------------------------------------------------------------
                     lhs relation rhs
                               expected
      --------------------------------------------------------------------
           '1999-12-31T23:00:00Z' <> '2000-01-01T12:00:00'
           '2000-01-16T12:00:00Z' <> '2000-01-16T12:00:00'
           '2000-01-16T12:00:00Z' <> '2000-01-16T00:00:00'
      '2000-01-16T00:00:00-05:00' <> '2000-01-16T10:00:00'
                       '13:20:00' < '13:20:00.000000000000000000000000001'
      --------------------------------------------------------------------

      The relation for the first four pairs is INDETERMINATE because
      rhs has unspecified timezone and difference between rhs and normalized lhs
      is less than 14 hours. Note, that RI's results are not symmetric (see the
      log below).

      This bug affects new test in JCK 1.5 (not integrated yet)
          api/javax_xml/datatype/XMLGregorianCalendar/index.html#Compare[Compare001]

      The bug is found in jdk1.5.0/beta/b32.

      To reproduce the bug compile and run the following code as shown
      in the log below:
      ------------------------------------------ test.java
      import javax.xml.datatype.XMLGregorianCalendar;

      class test {
          
          static final String [][] partialOrder = { // partialOrder
             {"2000-01-01T12:00:00", "<>", "1999-12-31T23:00:00Z"},
             {"2000-01-16T12:00:00", "<>", "2000-01-16T12:00:00Z"},
             {"2000-01-16T00:00:00", "<>", "2000-01-16T12:00:00Z"},
             {"2000-01-16T00:00:00-05:00", "<>", "2000-01-16T10:00:00"},
             {"13:20:00.000000000000000000000000001", ">", "13:20:00"},
          
          }; // end of partialOrder

          static String cmp2Str(int cmp) {
              return cmp == XMLGregorianCalendar.GREATER ? "GREATER" :
                     cmp == XMLGregorianCalendar.LESSER ? "LESSER" :
                     cmp == XMLGregorianCalendar.EQUAL ? "EQUAL" :
                     cmp == XMLGregorianCalendar.INDETERMINATE ? "INDETERMINATE" :
                                     "UNSPECIFIED";
          }

          public static void main(String [] args) {
              for(int i = 0; i < partialOrder.length; ++i) {
                  try {
                      XMLGregorianCalendar lhs = XMLGregorianCalendar.parse(partialOrder[i][0]);
                      XMLGregorianCalendar rhs = XMLGregorianCalendar.parse(partialOrder[i][2]);
                      int expected = partialOrder[i][1].equals("<") ? XMLGregorianCalendar.LESSER :
                             partialOrder[i][1].equals(">") ? XMLGregorianCalendar.GREATER :
                             partialOrder[i][1].equals("==") ? XMLGregorianCalendar.EQUAL :
                                                          XMLGregorianCalendar.INDETERMINATE;
                      int returned = XMLGregorianCalendar.compare(lhs, rhs);
                      if (returned != expected) {
                          System.out.println(i + ": compare(" + partialOrder[i][0]
                                      + ", " + partialOrder[i][2] + ") returns "
                                      + cmp2Str(returned) + ", but expected " + cmp2Str(expected));
                      }
                      expected = expected == XMLGregorianCalendar.GREATER ? XMLGregorianCalendar.LESSER :
                                 expected == XMLGregorianCalendar.LESSER ? XMLGregorianCalendar.GREATER :
                                 expected;
                      returned = XMLGregorianCalendar.compare(rhs, lhs);
                      if (returned != expected) {
                          System.out.println(i + ": compare(" + partialOrder[i][2]
                                      + ", " + partialOrder[i][0] + ") returns "
                                      + cmp2Str(returned) + ", but expected " + cmp2Str(expected));
                      }
                  } catch (IllegalArgumentException e) {
                      System.out.println(i + ": throws " + e + " trying to parse and compare "
                                  + partialOrder[i][0] + " and " + partialOrder[i][2]);
                  }

              }
          }
      }
      ----------------------------------------------------

      ------------------------------------------------ log
      $javac test.java && java -cp . -showversion test
      java version "1.5.0-beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta-b32)
      Java HotSpot(TM) Client VM (build 1.5.0-beta-b32, mixed mode)

      0: compare(1999-12-31T23:00:00Z, 2000-01-01T12:00:00) returns GREATER, but expected INDETERMINATE
      1: compare(2000-01-16T12:00:00Z, 2000-01-16T12:00:00) returns GREATER, but expected INDETERMINATE
      2: compare(2000-01-16T12:00:00Z, 2000-01-16T00:00:00) returns GREATER, but expected INDETERMINATE
      3: compare(2000-01-16T00:00:00-05:00, 2000-01-16T10:00:00) returns GREATER, but expected INDETERMINATE
      4: compare(13:20:00.000000000000000000000000001, 13:20:00) returns INDETERMINATE, but expected GREATER
      4: compare(13:20:00, 13:20:00.000000000000000000000000001) returns INDETERMINATE, but expected LESSER
      ----------------------------------------------------

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

            jfialli Joe Fialli
            reysunw Rey Rey (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: