开发者

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 F开发者_如何学ClatXMLDataSet 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?


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.


Always use the correct variables to access special directories, because multi-module builds have a different working directory than local builds:

So

  • instead of mydir use ${project.basedir}/mydir
  • instead of target/mydir use ${project.build.directory}/mydir
  • instead of target/classes/mydir use ${project.build.outputDirectory}/mydir

These variables always evaluate to the current project, no matter where it is called from. Here is an Overview of POM variables (not complete but the most important stuff is in there)

Also, if you ever want to do some interactive query-style debugging, the help:evaluate mojo comes in handy:

just call

mvn help:evaluate

and you will be prompted for an expression. If you enter an expression e.g. ${project.build.plugins[0]} , the merged dom for the specified element will be listed


EDIT:

ok, now I think I see the problem. then why not just reference the directory in the xml:

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

I know it's not pretty, but it should work, multi-module or not. the current directory for unit tests is always the current ${project.basedir}, not the parent project dir.


You could publish the DTD to a web server and then put its HTTP URL into the DOCTYPE, e.g.:

<!DOCTYPE myapp-data SYSTEM "-//The Owner//The Description//EN" "http://host/path/to/myapp-data.dtd">


Try using "File" instead of "FileInputStream" when opening an XML file.

For example:

ReplacementDataSet dataSet = new ReplacementDataSet(new FlatXmlDataSet(new File(fileName)));

This way, relative path to DTD should start with directory of the XML file.

And if you use

ReplacementDataSet dataSet = new ReplacementDataSet(new FlatXmlDataSet(new FileInputStream(fileName)));

path should be relative to current working directory.


It involves some ugly duplication, but you could paste the contents of the DTD into the XML file(s) in question and then use them as internal DTDs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜