Test REporter

import java.io.File;

import java.io.IOException;
import java.io.FilenameFilter;
import java.io.FileFilter;
import java.util.Vector;

import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;

import jxl.CellType;
import jxl.Workbook;

import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class TestReportGenerator {

private Vector excelObjects = null;
private Document doc;
private String directoryPath = null;
private String outputXls = null;
private String schemaFile = "C:/hp/OpenView/ServiceActivator/etc/config/fixed/fixed_activation_request_interface.xsd";

public TestReportGenerator() {
excelObjects = new Vector();
}

private void generateTestReport(String directoryPath, String outputXls) throws Exception {

this.directoryPath = directoryPath;
this.outputXls = outputXls;

// Retrieve all the files
File list[] = getFileList();

// process each file and push excel object into Vector
for (int i = 0; i < list.length; i++) {
System.out.println("Processing test xml " + list[i] + " Starts …");

generateExcelObj(list[i]);
System.out.println("Processing test xml " + list[i] + " Ends …");
}

//System.out.println("Number of Objects in the container is " + excelObjects.size());
// write the entire vector of excel objects into XLS
createXls();

}

private void createXls() {
ExportToExcel test = new ExportToExcel();
try {
//test.setOutputFile("C:\\TestXSL\\Book1.xsl");
test.setOutputFile(outputXls);
test.write(excelObjects);

System.out.println("Successfully exported to " + outputXls );
} catch (Exception e) {
System.err.println(e.getMessage());
}
}

private File[] getFileList() {
File directory = new File(directoryPath);
File[] files = directory.listFiles();
return files;
}


private void generateExcelObj(File inputFilePath) {

String testCaseId = null;
String actiontype = null;
String serviceType = null;

String status = "Passed";
String p = "";
try {

doc = new XmlValidator().getRootDocument(schemaFile, inputFilePath.getAbsolutePath());

// Retrieval of request Id
//testCaseId = "Create_Voice";

// Retrieval of action type
//actiontype = "Create";

//serviceType = "Voice";

// normalize text representation
doc.getDocumentElement().normalize();


/*//System.out.println ("Root element of the doc is " +
doc.getDocumentElement().getNodeName());
Node rootnode = doc.getDocumentElement();
//System.out.println(rootnode.getNodeName());
*/


// Steps for Getting id
NodeList idList = doc.getElementsByTagName("id");
Element idElement = (Element)idList.item(0);
NodeList childNodes = idElement.getChildNodes();
testCaseId= ((Node) childNodes.item(0)).getNodeValue();
//System.out.println(((Node) childNodes.item(0)).getNodeValue());


// Steps for Getting ActionType
NodeList actionList = doc.getElementsByTagName("action");
Element actionElement = (Element)actionList.item(0);
NodeList childNodes1 = actionElement.getChildNodes();
actiontype= ((Node) childNodes1.item(0)).getNodeValue();
//System.out.println(((Node) childNodes1.item(0)).getNodeValue());


// Steps for Getting ServiceType
NodeList serviceList = doc.getElementsByTagName("servicetype");
Element serviceElement = (Element)serviceList.item(0);
NodeList childNodes2 = serviceElement.getChildNodes();
serviceType= ((Node) childNodes2.item(0)).getNodeValue();
//System.out.println(((Node) childNodes2.item(0)).getNodeValue());


NodeList listOfPersons = doc.getElementsByTagName("param");
int totalPersons = listOfPersons.getLength();
////System.out.println("Total no of param : " + totalPersons);

String value1 = null;
String key1 = null;
String temp = null;

for (int s = 0; s < listOfPersons.getLength(); s++) {

Node firstPersonNode = listOfPersons.item(s);
if (firstPersonNode.getNodeType() == Node.ELEMENT_NODE) {

Element firstPersonElement = (Element) firstPersonNode;

// -------
NodeList firstNameList = firstPersonElement
.getElementsByTagName("key");
Element firstNameElement = (Element) firstNameList.item(0);

NodeList textFNList = firstNameElement.getChildNodes();
/*
* //System.out.println("Key : " +
* ((Node)textFNList.item(0)).getNodeValue().trim());
*/
key1 = ((Node) textFNList.item(0)).getNodeValue().trim();
// //System.out.println("Key : " +key1);

// -------
NodeList lastNameList = firstPersonElement
.getElementsByTagName("value");
Element lastNameElement = (Element) lastNameList.item(0);

NodeList textLNList = lastNameElement.getChildNodes();
/*
* //System.out.println("Value : " +
* ((Node)textLNList.item(0)).getNodeValue().trim());
*/
if ((Node) textLNList.item(0) == null)
value1 = "";
else
value1 = ((Node) textLNList.item(0)).getNodeValue().trim();
// //System.out.println("Value : " +value1);


temp = key1 + ":" + value1;
// //System.out.println("temp : " + temp);

p = p + temp + "\n";

}
}

// params = "Test";

ExcelObject excelObj = new ExcelObject();
excelObj.actionType = actiontype;
excelObj.params = p;
excelObj.testCaseId = testCaseId;
excelObj.serviceType = serviceType;
excelObj.status = status;

excelObjects.add(excelObj);
//System.out.println("Size =" + excelObjects.size());
}

/*catch ( NullPointerException npe ) {
//System.out.println("Null Point Exception");
} */

catch (SAXParseException err) {
System.out.println("** Parsing error" + ", line "
+ err.getLineNumber() + ", uri " + err.getSystemId());
System.out.println(" " + err.getMessage());

} catch (SAXException e) {
Exception x = e.getException();
((x == null) ? e : x).printStackTrace();

} catch (Throwable t) {
t.printStackTrace();
}

//System.out.println("param : " + p);
}

public static void main(String[] args) {
//try{
if (args.length < 2) {
//System.err.println("Please Specify both Directory path and output XSL File path");
System.err.println("Error in providing Directory path and output XSL File path" +args[0]+ "and" +args[1]);
System.exit(0);
}

String directoryPath = args[0];
String outputXls = args[1];
/*}catch(ArrayIndexOutOfBoundsException e){
System.out.println("please provide both the path: directoryPath " + args[0] + "and outputFile path" + args[1] + e.getMessage());
}*/
TestReportGenerator testRepGen = new TestReportGenerator();
try {
testRepGen.generateTestReport(directoryPath, outputXls);
} catch (Exception excep) {
System.err.println(excep.getMessage());
}/*catch(ArrayIndexOutOfBoundsException e){
System.out.println("please provide both the path: directoryPath " + args[0] + "and outputFile path" + args[1] + e.getMessage());
}*/
}
}


Post a Comment

Previous Post Next Post