-1

I'm trying to use DBUnit to test my repository service. I have this xml that show the expected result:

<?xml version="1.0" encoding="UTF-8"?>
<dataset>
    <product id="1" description="Printer" price="75.0" />
</dataset>

id= String , description = String , price = double.

The problem is I'm used to , not so I don't know how to format an attribute as type double.

I tried: price="75.0" , price=75.0 , price=75

I cannot cast it to double afterwards because I let DBUnit do the parsing:

IDataSet expectedDataset = new FlatXmlDataSetBuilder().build(expectedDataSetFile);

Currently, my assertion fails because of this problem, using the debugger I see the data is put in correctly.


Screenshot

expected enter image description here

Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
Koen Demonie
  • 539
  • 1
  • 7
  • 24

2 Answers2

1

In XML by default everything is a string. You can use an XML Schema Definition to enforce rules over your data and its structure, which would make it possible to state that the price is a double.

However, I don't know DBUnit but I would expect it to provide another easier way to state your data format.

Aaron
  • 24,009
  • 2
  • 33
  • 57
  • He tried `price="75.0"` + What solution are you providing? – Yassin Hajaj Dec 19 '15 at 17:18
  • @YassinHajaj to add an XSD definition to its XML. Please tell me how I was unclear so I edit my answer – Aaron Dec 19 '15 at 17:20
  • The answer is not unclear but what is the concrete solution you're providing here without knowing how he coded to parse the file? – Yassin Hajaj Dec 19 '15 at 17:21
  • I don't see the code for its xml parsing, but any standard XML parser would parse an XML without XSD as String, whereas an XML with an XSD stating the attribute is a double would be correctly parsed as Double – Aaron Dec 19 '15 at 17:24
  • "he coded to parse the file" I'm pretty sure the parsing is handled by DBUnit's `new FlatXmlDataSetBuilder().build()`, and I'm pretty sure this function would behave as expected if an XSD was backing up the XML file – Aaron Dec 19 '15 at 17:26
0

In XML, everything is a string. You can define an attribute as a float in the schema but this will not make the parser deliver it to the application as a floating point value. It will still deliver it as a string.

When you define an attribute value as a type in XML, you are actually defining a string pattern that it must adhere to. In other words, you are saying that the string must contain a value that follows the pattern of a string representation of a floating point value. The validator will flag it as an error is the string does not match this pattern, but the parser will still deliver it as a string.

A downstream application (such as an XSLT script) may look at the schema and use the information it contains to interpret the value as a float, but the parser will not. In other words, you will need to cast it yourself.