Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xml regex value validation #176

Closed
bloodyawei opened this issue Feb 17, 2020 · 3 comments
Closed

xml regex value validation #176

bloodyawei opened this issue Feb 17, 2020 · 3 comments
Milestone

Comments

@bloodyawei
Copy link

Hi all,

I try to validate two xml messages, actual and expected with regex values, and I find DiffBuilder.compare() can't compare regex values. Please tell me how to fix it, thank you.

actual xml :

<Document>
     <CreDtTm>2020-02-14T07:18:57.347+08:00</CreDtTm>
</Document> 

expected xml :

<Document>
    <CreDtTm>2020-02-14T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}\+08:00</CreDtTm>
</Document>

my code :

Diff diff = DiffBuilder.compare(expectedXml)
    .withTest(actualXml)
    .ignoreComments()
    .normalizeWhitespace()
    .checkForSimilar()
    .build();
if(diff.hasDifferences()) {
    Iterator<Difference> it = diff.getDifferences().iterator();
    while(it.hasNext())
        logger.error(it.next().getComparison());
}

console output :

Expected text value '2020-02-14T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}\+08:00' but was '2020-02-14T07:18:57.347+08:00' - comparing <CreDtTm ...>2020-02-14T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}\+08:00</CreDtTm> at /Document[1]/CreDtTm[1]/text()[1] to <CreDtTm ...>2020-02-14T07:18:57.347+08:00</CreDtTm> at /Document[1]/CreDtTm[1]/text()[1]
@bodewig
Copy link
Member

bodewig commented Feb 17, 2020

You are correct, XMLUnit does not do anything with regexes at all. It compares two documents, that's it.

If you want to implement regular expression based comparisons, DifferenceEvaluator is the interface to implement.

At first glance it looks as if it would fit into the placeholders project nicely, so rather than implementing a full DifferenceEvaluator one could implement a PlaceholderHandler that was triggered by ${xmlunit.matches(REGEX)} or something similar. It is not impossible that the placeholders API is not sufficient, though.

If you want to give it a shot and contribute the result back I'll be happy to assist.

@bloodyawei
Copy link
Author

Thanks for the quick response. I will look into it.

@bodewig bodewig added this to the 2.7.0 milestone May 10, 2020
@bodewig
Copy link
Member

bodewig commented May 10, 2020

this has now been implemented with the help of #178

@bodewig bodewig closed this as completed May 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants