Skip to content

Commit

Permalink
Adding support to read multiple suites from results file
Browse files Browse the repository at this point in the history
  • Loading branch information
yasassri committed Apr 19, 2016
1 parent 48dc1e2 commit 9913b91
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
22 changes: 14 additions & 8 deletions src/main/java/com/tupilabs/testng/parser/TestNGParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
Expand All @@ -50,16 +51,16 @@ public class TestNGParser implements Serializable {
* Parses the content of an input stream and returns a Suite.
*
* @param inputStream the input stream.
* @return Resulting object.
* @return List of Resulting object.
* @throws SAXException
* @throws IOException
* @throws ParserConfigurationException
*/
public Suite parse(File file) throws ParserException {
public List<Suite> parse(File file) throws ParserException {

FileInputStream fileInputStream = null;
final TestNGXmlHandler handler = new TestNGXmlHandler();
final Suite suite;
//final Suite suite;

SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(false);
Expand All @@ -71,17 +72,23 @@ public Suite parse(File file) throws ParserException {
}

SAXParser parser = null;
List<Suite> suites;

try {
fileInputStream = new FileInputStream(file);
parser = factory.newSAXParser();
parser.parse(fileInputStream, handler);

suite = handler.getSuite();
if (suite == null) {
suites = handler.getSuite();
if (suites == null) {
throw new ParserException("Error while parsing file " + file + ": Null");
} else {
// Setting file for all the suites
for (Suite suite : suites) {
suite.setFile(file.getAbsolutePath());
}
}
suite.setFile(file.getAbsolutePath());

} catch (ParserConfigurationException e) {
throw new ParserException(e);
} catch (SAXException e) {
Expand All @@ -97,8 +104,7 @@ public Suite parse(File file) throws ParserException {
}
}
}

return suite;
return suites;
}

}
14 changes: 10 additions & 4 deletions src/main/java/com/tupilabs/testng/parser/TestNGXmlHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
package com.tupilabs.testng.parser;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
Expand Down Expand Up @@ -54,11 +56,13 @@ public class TestNGXmlHandler extends DefaultHandler implements Serializable {
private Test test;
private Class clazz;
private TestMethod testMethod;
List<Suite> suitesList;
/**
* Default constructor.
*/
public TestNGXmlHandler() {
super();
suitesList = new ArrayList<>();
}
/*
* (non-Javadoc)
Expand Down Expand Up @@ -115,13 +119,15 @@ public void endElement(String uri, String localName, String qName)
test.addClass(clazz);
} else if (TEST_METHOD.equals(qName)) {
clazz.addTestMethod(testMethod);
} else if (SUITE.equals(qName)){
this.suitesList.add(this.suite);
}
}
/**
* Retrieves the parsed Suite.
* @return the parsed Suite.
* Retrieves the parsed Suites.
* @return a List of parsed Suites.
*/
public Suite getSuite() {
return this.suite;
public List<Suite> getSuite() {
return suitesList;
}
}

0 comments on commit 9913b91

Please sign in to comment.