Duration.compare(Duration) returns EQUAL when it should return DETERMINATE
--------------------------------------------------------------------------
Key: XERCESJ-1447
URL: https://issues.apache.org/jira/browse/XERCESJ-1447
Project: Xerces2-J
Issue Type: Bug
Components: JAXP (javax.xml.datatype)
Reporter: Kevin Braun
The following code illustrates the issue:
import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.Duration;
import javax.xml.datatype.XMLGregorianCalendar;
class test {
static final String [][] partialOrder = { // partialOrder
{"P1Y", "<>", "P365D"},
{"P1Y", "<>", "P366D"},
{"P1M", "<>", "P28D"},
{"P1M", "<>", "P29D"},
{"P1M", "<>", "P30D"},
{"P1M", "<>", "P31D"},
{"P5M", "<>", "P150D"},
{"P5M", "<>", "P151D"},
{"P5M", "<>", "P152D"},
{"P5M", "<>", "P153D"},
{"PT2419200S", "<>", "P1M"},
{"PT2678400S", "<>", "P1M"},
{"PT31536000S", "<>", "P1Y"},
{"PT31622400S", "<>", "P1Y"},
{"PT525600M", "<>", "P1Y"},
{"PT527040M", "<>", "P1Y"},
{"PT8760H", "<>", "P1Y"},
{"PT8784H", "<>", "P1Y"},
{"P365D", "<>", "P1Y"},
}; // end of partialOrder
public static String cmp2str(int cmp) {
return cmp == DatatypeConstants.LESSER ? "LESSER"
: cmp == DatatypeConstants.GREATER ? "GREATER"
: cmp == DatatypeConstants.EQUAL ? "EQUAL"
: cmp == DatatypeConstants.INDETERMINATE ? "INDETERMINATE"
: "UNDEFINED";
}
public static void main(String [] args) throws Exception {
DatatypeFactory df = DatatypeFactory.newInstance();
String status = "Passed";
for (int valueIndex = 0; valueIndex < partialOrder.length;
++valueIndex) {
Duration duration1 = df.newDuration(partialOrder[valueIndex][0]);
Duration duration2 = df.newDuration(partialOrder[valueIndex][2]);
int cmp = duration1.compare(duration2);
int expected = ">".equals(partialOrder[valueIndex][1])
? DatatypeConstants.GREATER
: "<".equals(partialOrder[valueIndex][1])
? DatatypeConstants.LESSER
: "==".equals(partialOrder[valueIndex][1])
? DatatypeConstants.EQUAL
: DatatypeConstants.INDETERMINATE;
if (cmp != expected) {
status = "Failed";
System.out.println("returned " + cmp2str(cmp)
+ " for durations \'" + duration1 + "\' and "
+ duration2 + "\', but expected " +
cmp2str(expected));
}
}
System.out.println(status + ".");
}
}
The output is:
returned EQUAL for durations 'P1Y' and P365D', but expected INDETERMINATE
returned EQUAL for durations 'P1M' and P28D', but expected INDETERMINATE
returned EQUAL for durations 'P5M' and P150D', but expected INDETERMINATE
returned EQUAL for durations 'PT2419200S' and P1M', but expected INDETERMINATE
returned EQUAL for durations 'PT31536000S' and P1Y', but expected INDETERMINATE
returned EQUAL for durations 'PT525600M' and P1Y', but expected INDETERMINATE
returned EQUAL for durations 'PT8760H' and P1Y', but expected INDETERMINATE
returned EQUAL for durations 'P365D' and P1Y', but expected INDETERMINATE
The problem looks to be the shortcut taken in DurationImpl.compare(Duration).
It does this:
if (lhsCalendar.equals(rhsCalendar)) {
return DatatypeConstants.EQUAL;
}
where it has added the Durations to Feb 1, 1970. The shortcut bypasses the
tests in compareDates(Duration, Duration), which tests using 4 different dates
to avoid just the sort of problem seen in this shortcut. I think the shortcut
should be removed or replaced with a correct one.
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]