我创建了自己的xml来存储测试用例。每个元素中的所有属性都是拥有完整的最终Test对象所必需的。
不想一次又一次地重写相同的信息,因此使所有子项都应继承子项中不可用的父项的属性(属性)的方式。
TestSuite.xml
<TestSuite project="PRJ_Test" sim="sim_1.file">
<LogicalLink linkName="LL_1">
<TestJob expected="JobExp" actual="JobAct" >
<TestCase name="TC_1"/>
</TestJob>
<TestJob expected="JobExp" actual="JobAct" sim="sim_2.file" >
<TestCase name="TC_2"/>
<TestCase name="TC_3" sim="sim_3.file"/>
</TestJob>
</LogicalLink>
<LogicalLink linkName="LL_2" sim="sim_4.file">
<TestJob expected="JobExp" actual="JobAct">
<TestCase name="TC_4"/>
</TestJob>
</LogicalLink>
</TestSuite>
型号:
@XmlRootElement(name = "TestSuite")
public class TestSuite {
private String project;
protected String sim;
private List<LogicalLink> logicalLinks;
@XmlAttribute
public String getProject() {
return project;
}
public void setProject(String project) {
this.project = project;
}
@XmlElement(name="LogicalLink")
public List<LogicalLink> getLogicalLinks() {
return logicalLinks;
}
public void setLogicalLinks(List<LogicalLink> logicalLink) {
this.logicalLinks = logicalLink;
}
@XmlAttribute
public String getSim() {
return sim;
}
public void setSim(String sim) {
this.sim = sim;
}
}
public class LogicalLink extends TestSuite {
private String linkName;
private List<TestJob> testJobs;
@XmlAttribute
public String getLinkName() {
return linkName;
}
public void setLinkName(String linkName) {
this.linkName = linkName;
}
@XmlElement(name = "TestJob")
public List<TestJob> getTestJobs() {
return testJobs;
}
public void setTestJobs(List<TestJob> testJobs) {
this.testJobs = testJobs;
}
}
public class TestJob extends LogicalLink {
private String expected;
private String actual;
private List<TestCase> testCases;
@XmlAttribute
public String getExpected() {
return expected;
}
public void setExpected(String jobOld) {
this.expected = jobOld;
}
@XmlAttribute
public String getActual() {
return actual;
}
public void setActual(String jobNew) {
this.actual = jobNew;
}
@XmlElement(name = "TestCase")
public List<TestCase> getTestCases() {
return testCases;
}
public void setTestCases(List<TestCase> testCases) {
this.testCases = testCases;
}
}
public class TestCase extends TestJob{
private String name;
@XmlAttribute
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
解析:
JAXBContext jaxbContext = JAXBContext.newInstance(TestSuite.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
TestSuite testSuite = (TestSuite) jaxbUnmarshaller.unmarshal("TestSuite.xml");
当我通过遍历TestSuite来获取List时,TestCase元素中所有不可用的属性(如sim属性)都是空的,应从Parent元素中选择。
最后,我想获得如下的TestCases列表(焦点是'sim'属性)。
TestCase1:
name="TC_1"
expected="JobExp1"
actual="JobAct1"
linkName="LL_1"
project="PRJ_Test"
sim="sim_1.file" // because given in TestSuite (parent) and not overriden till TC_1
TestCase2:
name="TC_2"
expected="JobExp2"
actual="JobAct2"
linkName="LL_1"
project="PRJ_Test"
sim="sim_2.file" //because overriden in TestJob (parent) element
TestCase3:
name="TC_3"
expected="JobExp2"
actual="JobAct2"
linkName="LL_1"
project="PRJ_Test"
sim="sim_3.file" //because overriden in TestCase (self) element
TestCase4:
name="TC_4"
expected="JobExp3"
actual="JobAct3"
linkName="LL_2"
project="PRJ_Test"
sim="sim_4.file"//because overriden in LogicalLink (parent) element