Menu

#17 use tolerance for floating point values in XML

closed-fixed
nobody
5
2008-03-13
2007-07-27
Ken Estes
No

We produce XML which has numerical values scattered through out the
XML both in nodes and attributes.

<svg xmlns="http://www.w3.org/2000/svg" width="0.085in" height="0.085in">
<text text-anchor="end" y="92" x="15">36</text>
<fo:block-container height="2pc" width="46.5pc" left="2.5pc" top="64pc">

The numerical values can be produced by computations which are carried
out in software or hardware. Because of round off errors we need to
have a custom string equals function which will allow for round off in
the numeric portions of the string.

Enclosed please find a file which will perform such numeric comparisons.

Here is my suggestion for how to hook it into XMLUnit.

/**
* Test two non-null values for inequality
* @param expected
* @param actual
* @return TRUE if the values are not equals() equal (taking whitespace
* into account if necessary)
*/
private boolean unequalNotNull(Object expected, Object actual) {
boolean bothStrings = (expected instanceof String
&& actual instanceof String);
if (!bothStrings) {
return !(expected.equals(actual));
}

String expectedString = (String) expected;
String actualString = (String) actual;

if (XMLUnit.getIgnoreWhitespace()) {
expectedString = expected.trim();
actualString = actual.trim();
}
if (XMLUnit.getNormalizeWhitespace()) {
expectedString = normalizeWhitespace(expectedString);
actualString = normalizeWhitespace(actualString);
}

if (XMLUnit.getTolerantNumericCompare) {
return !StringCmpNumericTolerance.equals(expected,actualString);
}
return !expectedString.equals(actualString);
}

Discussion

  • Ken Estes

    Ken Estes - 2007-07-27

    string comparison with tolerance for floats

     
  • Stefan Bodewig

    Stefan Bodewig - 2008-01-04

    Logged In: YES
    user_id=113148
    Originator: NO

    Hi Ken,

    to be honest I feel we would be stretching XMLUnit quite a bit if we added special purpose options like handling of numeric values to its core. The same could be said for case insensitive comparisons of element and attribute names (HTML as a use-case) or texts and so on.

    It should be possible to use a DifferenceListener of your own that wrapped whatever DifferenceListener would get used normally and performed some special sort of comparision for ATTR_VALUE Differences. We might even add such a DifferenceListener in XMLUnit's examples package.

     
  • Ken Estes

    Ken Estes - 2008-01-28

    Logged In: YES
    user_id=1855117
    Originator: YES

    It would be simplest for my case to wrap the standard DifferenceEngine with another class, because what I wish to
    change is the method unequalNotNull(Object expected, Object actual) in this class. However this is difficult
    with the current code structure.

    1) The DifferenceEngine class has methods which are private/final and this makes extending the object diffcult

    2) it is not clear how to pass my new class into the Diff(,) object to be used instead of the normal differenceEngine.

    I was hoping to use the diff constructor Diff(Document controlDoc, Document testDoc, DifferenceEngine comparator)
    But this is a tangle of dependencies. My DifferenceEngine needs to be constructed first, so that I could pass it into the Diff. However to construct my class I need to pass in the Diff (this is the only class which
    implements ComparisonController) that it will be used for as a parameter. I suggest that you should separate the
    implementations of DifferenceListener, ComparisonController and not keep diff as an implementation of both since
    the dependency issues for both are quite separate.

    And Worse there is no setDifferenceEngine() method for Diff() so that I could create my own DifferenceEngine
    after the diff was constructed (and pass in the already constructed diff as a parameter) then override the default difference engine with what I just created.

    In sort overriding the DifferenceEngine class is quite difficult.

     
  • Stefan Bodewig

    Stefan Bodewig - 2008-02-28
    • labels: --> XMLUnit for Java
     
  • Stefan Bodewig

    Stefan Bodewig - 2008-03-13

    Logged In: YES
    user_id=113148
    Originator: NO

    Like I said, this can be done using a DifferenceListener.

    A simplisitic one (for usage see its unit test) is now part of the examples package.

    http://xmlunit.svn.sourceforge.net/viewvc/xmlunit?view=rev&revision=249

     
  • Stefan Bodewig

    Stefan Bodewig - 2008-03-13
    • status: open --> closed-fixed
     

Log in to post a comment.