Path to XML DTD for DBUnit in multi
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 (eg myproject/). I would have expected it to look for the DTD in the same directory as the XML file itself (eg 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
mydir
use ${project.basedir}/mydir
target/mydir
use ${project.build.directory}/mydir
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 eg ${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.
您可以将DTD发布到Web服务器,然后将其HTTP URL放入DOCTYPE中,例如:
<!DOCTYPE myapp-data SYSTEM "-//The Owner//The Description//EN" "http://host/path/to/myapp-data.dtd">
链接地址: http://www.djcxy.com/p/34896.html
上一篇: 忽略DOCTYPE .dtd,但.dtd文件仍然存在
下一篇: 多个DBUnit的XML DTD路径