I'm trying to use the simpleframework to serialize a third party (parasoft) xml report.
<TestSuite authChange="" authFail="" change="0" changePass="0" changeTotal="0" fail="0" pass="42" total="42">
<TestSuite authChange="" authFail="" change="0" changePass="0" changeTotal="0" fail="0" name="Test Suite: APIs">
<TestSuite authChange="" authFail="" change="0" changePass="0" changeTotal="0" fail="0">
<Test authChange="" authFail="" change="0" changePass="0" changeTotal="0" fail="0" />
<Test authChange="" authFail="" change="0" changePass="0" changeTotal="0" fail="0" id="wk:///2" name="Test 2: GetControlParams" pass="1" tool="GetControlParams" total="1" />
</TestSuite>
<TestSuite authChange="" authFail="" change="0" changePass="0" changeTotal="0" fail="0">
<Test authChange="" authFail="" change="0" changePass="0" changeTotal="0" fail="0" name="Test 1: GetHouseInfo" pass="1" tool="GetHouseInfo" total="1" />
<Test authChange="" authFail="" change="0" changePass="0" changeTotal="0" fail="0" name="Test 2: GetHouseInfo" pass="1" tool="GetHouseInfo" total="1" />
</TestSuite>
</TestSuite>
</TestSuite>
How do I handle the recursive relationship for TestSuite?
I've tried
@Element(name = "TestSuite", required = false)
private TestSuite testSuite;
@ElementList(inline = true, entry = "Test", required = false)
private List<Test> test;
@ElementList(inline = true, entry = "TestSuite", required = false)
private List<TestSuite> testSuites;
But am running up against the error
Exception in thread "main" org.simpleframework.xml.core.PersistenceException: Duplicate annotation of name 'TestSuite' on field 'testSuites' private java.util.List TestSuite.testSuites
Any thoughts?
I've tried [...] But am running up against the error
This is intended: You have two annotations with the same (tag-)name but different types. Which one should the serializer choose?
There are two issues to address:
required
argument of simples annotations) I have reduced the problem a bit for this answer. Let's assume a Xml like this:
<TestSuite change="1" name="suite lvl 2">
<TestSuite change="0">
<Test name="test1" />
<Test name="test2" fail="0" />
</TestSuite>
</TestSuite>
The trick to this: use @ElementListUnion
and some kind of Interface (or abstract base class)
The code to this:
public interface TestEntry
{
}
@Root(name = "Test")
public class TestCase implements TestEntry
{
@Attribute(required = false)
private int fail;
@Attribute
private String name;
// Getter etc.
}
@Root(name = "TestSuite")
public class TestSuite implements TestEntry
{
@Attribute
private int change;
@Attribute(required = false)
private String name;
@ElementListUnion({
@ElementList(inline = true, required = false, type = TestCase.class, name = "TestCase"),
@ElementList(inline = true, required = false, type = TestSuite.class, name = "TestSuite")
})
private List<TestEntry> content;
// Getter etc.
}
You see the trick? Now you can have either a TestCase
or TestSuite
in that list.
Finally, you can add the outer TestSuite
and - of course - complete the attributes.
Btw. if you run into trouble deciding whether an elements is of Type X or Y - a Converter
is still an option (but more to write manually).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.