Code with Finding: |
class Parser {
/**
* Parses the TestNG test suite and returns the corresponding XmlSuite,
* and possibly, other XmlSuite that are pointed to by <suite-files>
* tags.
*
* @return the parsed TestNG test suite.
*
* @throws ParserConfigurationException
* @throws SAXException
* @throws IOException if an I/O error occurs while parsing the test suite file or
* if the default testng.xml file is not found.
*/
public Collection<XmlSuite> parse()
throws ParserConfigurationException, SAXException, IOException
{
// Each suite found is put in this list, using their canonical
// path to make sure we don't add a same file twice
// (e.g. "testng.xml" and "./testng.xml")
List<String> processedSuites = Lists.newArrayList();
XmlSuite resultSuite = null;
List<String> toBeParsed = Lists.newArrayList();
List<String> toBeAdded = Lists.newArrayList();
List<String> toBeRemoved = Lists.newArrayList();
if (m_fileName != null) {
File mainFile = new File(m_fileName);
toBeParsed.add(mainFile.getCanonicalPath());
}
/*
* Keeps a track of parent XmlSuite for each child suite
*/
Map<String, XmlSuite> childToParentMap = Maps.newHashMap();
while (toBeParsed.size() > 0) {
for (String currentFile : toBeParsed) {
File currFile = new File(currentFile);
File parentFile = currFile.getParentFile();
InputStream inputStream = m_inputStream != null
? m_inputStream
: new FileInputStream(currentFile);
IFileParser<XmlSuite> fileParser = getParser(currentFile);
XmlSuite currentXmlSuite = fileParser.parse(currentFile, inputStream, m_loadClasses);
processedSuites.add(currentFile);
toBeRemoved.add(currentFile);
if (childToParentMap.containsKey(currentFile)) {
XmlSuite parentSuite = childToParentMap.get(currentFile);
//Set parent
currentXmlSuite.setParentSuite(parentSuite);
//append children
parentSuite.getChildSuites().add(currentXmlSuite);
}
if (null == resultSuite) {
resultSuite = currentXmlSuite;
}
List<String> suiteFiles = currentXmlSuite.getSuiteFiles();
if (suiteFiles.size() > 0) {
for (String path : suiteFiles) {
String canonicalPath;
if (parentFile != null && new File(parentFile, path).exists()) {
canonicalPath = new File(parentFile, path).getCanonicalPath();
} else {
canonicalPath = new File(path).getCanonicalPath();
}
if (!processedSuites.contains(canonicalPath)) {
toBeAdded.add(canonicalPath);
childToParentMap.put(canonicalPath, currentXmlSuite);
}
}
}
}
//
// Add and remove files from toBeParsed before we loop
//
for (String s : toBeRemoved) {
toBeParsed.remove(s);
}
toBeRemoved = Lists.newArrayList();
for (String s : toBeAdded) {
toBeParsed.add(s);
}
toBeAdded = Lists.newArrayList();
}
//returning a list of single suite to keep changes minimum
List<XmlSuite> resultList = Lists.newArrayList();
resultList.add(resultSuite);
boolean postProcess = true;
if (postProcess && m_postProcessor != null) {
return m_postProcessor.process(resultList);
} else {
return resultList;
}
}
}
|