Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Path to XML DTD for DBUnit in multi-module Java/Maven project?

I have a multi-module maven project. Within the persist module I have a number of XML files data files that reference a DTD:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE myapp-data SYSTEM "myapp-data.dtd" >

<dataset>
      .....omitted for brevity....
</dataset>

The DTD is stored in the same directory with the XML files and even Eclipse reports these XML files as valid.

However, when I run the application, the DBUnit FlatXMLDataSet throws a FileNotFound exception because it cannot located the DTD. It is apparently looking for the DTD in the root project directory (e.g. myproject/). I would have expected it to look for the DTD in the same directory as the XML file itself (e.g. myproject/persist/target/test-data).

Looking at the DBUnit source code, it has this to say about it "Relative DOCTYPE uri are resolved from the current working dicrectory."

What's a good way to fix this?

like image 542
HDave Avatar asked Jun 10 '10 19:06

HDave


1 Answers

OK, I think I figured this one out. Thank goodness for open source.

There is a method on FlatXmlDataSetBuilder that takes a stream to the DTD. It's crazy that this is a public method IMO, but then again, its crazy that DBUnit doesn't look in the same directory as the XML for the dtd file. So here it is:

String dtdResourceName = "classpath:test-data/myapp-data.dtd";      
Resource res = applicationContext.getResource(dtdResourceName);
builder.setMetaDataSetFromDtd(res.getInputStream());

Now I leave the DOCTYPE declaration with the dtd in the same directory as the XML and use this hack to fool DBUnit into doing the Right Thing.

like image 192
HDave Avatar answered Sep 29 '22 02:09

HDave