protected void setUp() throws Exception {
super.setUp();
URL impactsUrl = ImpactsParserTest.class.getResource("test-impacts.xml");
ImpactsParser parser = new ImpactsParser();
this.impacts = parser.parseAsList(impactsUrl);
}
public void testImpactsParsing() {
System.out.println(this.impactInfo = (ImpactInfo)this.impacts.get(0));
System.out.println(this.impactInfo = (ImpactInfo)this.impacts.get(1));
it gives different values than my test xml given below as:
ImpactInfo onObject='impact1' onAction='show' toObject='impacted1' toAction='onshow' value='true' reversible='true'
ConditionInfo: onObject='impact1.dcr1' onAction='show' value='true'
ConditionInfo: onObject='impact1.dcr2' onAction='show' value='false'
ConditionInfo: onObject='impact1.dcr3' onAction='onshow' value='true'
ImpactInfo onObject='impact1.dcr1' onAction='onshow' toObject='impacted1' toAction='onshow' value='true' reversible='true'
ConditionInfo: onObject='impact1' onAction='show' value='true'
ConditionInfo: onObject='impact1.dcr2' onAction='show' value='false'
ConditionInfo: onObject='impact1.dcr3' onAction='onshow' value='true'
whereas xml file contains,
test-impacts.xml:
<impacts>
<impact onObject="impact1" onAction="show" toObject="impacted1" toAction="onshow" value="true">
<condition onObject="impact1.dcr1" onAction="show" value="true"/>
<condition onObject="impact1.dcr2" onAction="show" value="false"/>
<condition onObject="impact1.dcr3" onAction="onshow"/>
</impact>
<impact onObject="impact2" onAction="select" toObject="impacted2" toAction="onselect" value="false">
<condition onObject="impact2.dcr1" onAction="onshow" value="true"/>
<condition onObject="impact2.dcr2" onAction="onselect" value="false"/>
<condition onObject="impact2.dcr3" onAction="show"/>
<condition onObject="impact2.dcr4" onAction="select"/>
</impact>
</impacts>
why i am getting different values than original xml values ?
ImpactsParser.java:
public List<ImpactInfo> parseAsList(URL url) {
SAXReader reader = new SAXReader();
Document doc = reader.read(url);
Element descriptionsTag = doc.getRootElement();
return parseImpacts(descriptionsTag);
}
private List<ImpactInfo> parseImpacts(Element descriptionsTag) {
LinkedList<ImpactInfo> impacts = new LinkedList<ImpactInfo>();
for (Iterator<Element> iter = descriptionsTag.elementIterator(TAG_IMPACT); iter.hasNext();) {
Element ele = iter.next();
// iterate on all onObjects
Set<String> onObjectsSet = getValuesFromElementAttribute(ele, ATT_ON_OBJECT, ",");
Set<String> toObjectsSet = getValuesFromElementAttribute(ele, ATT_TO_OBJECT, ",");
for (Iterator<String> it = onObjectsSet.iterator(); it.hasNext();) {
String onObject = it.next();
for (Iterator<String> itt = toObjectsSet.iterator(); itt.hasNext();) {
ImpactInfo impactInfo = parseImpactInfo(onObject, itt.next(), ele);
impacts.add(impactInfo);
// create inverted impacts for impacts with conditions
if ((impactInfo.getConditions()开发者_如何学Go != null) && (impactInfo.getConditions().size() > 0)) {
impacts.addAll(createInvertedImpacts(impactInfo));
}
}
}
}
return impacts;
}
protected Set<String> getValuesFromElementAttribute(Element element, String attributeName, String separator) {
Set<String> names = new HashSet<String>();
if (element.attribute(attributeName) != null) {
String[] dcrs = element.attribute(attributeName).getValue().split(separator);
for (int index = 0; index < dcrs.length; index++) {
if (dcrs[index] != null) {
names.add(dcrs[index].trim());
}
}
}
return names;
}
Most likely the code parser.parseAsList(impactsUrl)
parses differently from what you expect, and this test is detecting an error in the code it's testing.
Post the code for ImpactsParser, and we might be able to help figure out where it's going wrong.
Edit:
Based on the code now posted for ImpactsParser, you're producing many more elements in your list of ImpactInfo
items than there are <impact>
tags in your document, which may be exactly what you want to do, but may be something you're misunderstanding.
Every time you find an <impact>
tag, you're getting sets of onObject
and toObject
tags from the attributes, looping over the combined pairs (which will be only one in your sample xml shown), and creating one ImpactInfo
object for each by a call to a function not shown.
If that object created has "conditions", presumably triggered by the presence of <condition>
tags, you're creating another ImpactInfo
object for each condition, and adding them to the list. This is what I suspect you may really not want, but I of course don't really know exactly what you want.
It's hard to go further than this in saying what's wrong, because it's not clear to me what the expected correct working is.
What you really ought to do is define in a collection of tests the expected results for different bits of input (using assertions instead of System.out.println
) and try to get your tests to pass by modifying the code. Start with the simplest cases.
精彩评论