...25import org.testng.Reporter;26import org.testng.internal.ResultMap;27import org.testng.internal.Utils;28import org.testng.reporters.XMLReporterConfig;29import org.testng.reporters.XMLStringBuffer;30import org.testng.reporters.XMLSuiteResultWriter;31import org.testng.xml.XmlSuite;32import java.io.File;33import java.text.SimpleDateFormat;34import java.util.Collection;35import java.util.Date;36import java.util.HashMap;37import java.util.LinkedHashSet;38import java.util.List;39import java.util.Map;40import java.util.Properties;41import java.util.Set;42public class CustomTestNgReporter implements IReporter {43 private final XMLReporterConfig config = new XMLReporterConfig();44 private XMLStringBuffer rootBuffer;45 ITestContext testRootContext = null;46 ISuite testRootSuite = null;47 Exception exception = null;48 public CustomTestNgReporter(ITestContext context, Exception e) {49 testRootContext = context;50 exception = e;51 }52 public CustomTestNgReporter(ISuite suite, Exception e) {53 testRootSuite = suite;54 exception = e;55 }56 public CustomTestNgReporter(ITestContext context) {57 testRootContext = context;58 exception = null;59 }60 public CustomTestNgReporter(ISuite suite) {61 testRootSuite = suite;62 exception = null;63 }64 public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites,65 String outputDirectory) {66 if (Utils.isStringEmpty(config.getOutputDirectory())) {67 config.setOutputDirectory(outputDirectory);68 }69 rootBuffer = new XMLStringBuffer("");70 rootBuffer.push(XMLReporterConfig.TAG_TESTNG_RESULTS);71 writeReporterOutput(rootBuffer);72 for (int i = 0; i < suites.size(); i++) {73 writeSuite(suites.get(i).getXmlSuite(), suites.get(i));74 }75 rootBuffer.pop();76 Utils.writeUtf8File(config.getOutputDirectory(), "testng-results.xml", rootBuffer.toXML());77 }78 private void writeReporterOutput(XMLStringBuffer xmlBuffer) {79 xmlBuffer.push(XMLReporterConfig.TAG_REPORTER_OUTPUT);80 List<String> output = Reporter.getOutput();81 for (String line : output) {82 if (line != null) {83 xmlBuffer.push(XMLReporterConfig.TAG_LINE);84 xmlBuffer.addCDATA(line);85 xmlBuffer.pop();86 }87 }88 xmlBuffer.pop();89 }90 private void writeSuite(XmlSuite xmlSuite, ISuite suite) {91 switch (config.getFileFragmentationLevel()) {92 case XMLReporterConfig.FF_LEVEL_NONE:93 writeSuiteToBuffer(rootBuffer, suite);94 break;95 case XMLReporterConfig.FF_LEVEL_SUITE:96 case XMLReporterConfig.FF_LEVEL_SUITE_RESULT:97 File suiteFile = referenceSuite(rootBuffer, suite);98 writeSuiteToFile(suiteFile, suite);99 }100 }101 private void writeSuiteToFile(File suiteFile, ISuite suite) {102 XMLStringBuffer xmlBuffer = new XMLStringBuffer("");103 writeSuiteToBuffer(xmlBuffer, suite);104 File parentDir = suiteFile.getParentFile();105 if (parentDir.exists() || suiteFile.getParentFile().mkdirs()) {106 Utils.writeFile(parentDir.getAbsolutePath(), "testng-results.xml", xmlBuffer.toXML());107 }108 }109 private File referenceSuite(XMLStringBuffer xmlBuffer, ISuite suite) {110 String relativePath = suite.getName() + File.separatorChar + "testng-results.xml";111 File suiteFile = new File(config.getOutputDirectory(), relativePath);112 Properties attrs = new Properties();113 attrs.setProperty(XMLReporterConfig.ATTR_URL, relativePath);114 xmlBuffer.addEmptyElement(XMLReporterConfig.TAG_SUITE, attrs);115 return suiteFile;116 }117 private void writeSuiteToBuffer(XMLStringBuffer xmlBuffer, final ISuite suite) {118 xmlBuffer.push(XMLReporterConfig.TAG_SUITE, getSuiteAttributes(suite));119 writeSuiteGroups(xmlBuffer, suite);120 final ISuite iSuite = suite;121 final ITestContext testContext2 = testRootContext;122 final IResultMap resultMap = new ResultMap();123 CustomTestngResults results2 = new CustomTestngResults();124 ISuiteResult iSuiteResult = new ISuiteResult() {125 public String getPropertyFileName() {126 return iSuite.getXmlSuite().getFileName();127 }128 public ITestContext getTestContext() {129 return testRootContext;130 }131 };132 final Map<String, ISuiteResult> results = new HashMap<String, ISuiteResult>();133 results.put("Test", iSuiteResult);134 final Map<String, ISuiteResult> resultsFinal = results;135 ReportSuiteSetter suiteSetter = new ReportSuiteSetter();136 ISuite newSuite = suite;137 for (ITestNGMethod method : suite.getAllMethods()) {138 resultMap.addResult(results2.setResults(newSuite, method, 2, exception), method);139 }140 final IResultMap reportmap = resultMap;141 final ISuite reportsuite = newSuite;142 // ReportContextSetter contextSetter = new ReportContextSetter();143 // testRootContext = contextSetter.setContext(testContext2, resultMap, newSuite);144 ISuiteResult iSuiteResult2 = new ISuiteResult() {145 public String getPropertyFileName() {146 return reportsuite.getXmlSuite().getFileName();147 }148 public ITestContext getTestContext() {149 return testRootContext;150 }151 };152 final Map<String, ISuiteResult> reportResults1 = new HashMap<String, ISuiteResult>();153 reportResults1.put("Test", iSuiteResult2);154 XMLSuiteResultWriter suiteResultWriter = new XMLSuiteResultWriter(config);155 if (testContext2 != null) {156 for (Map.Entry<String, ISuiteResult> result : reportResults1.entrySet()) {157 suiteResultWriter.writeSuiteResult(xmlBuffer, result.getValue());158 }159 } else {160 for (Map.Entry<String, ISuiteResult> result : newSuite.getResults().entrySet()) {161 suiteResultWriter.writeSuiteResult(xmlBuffer, result.getValue());162 }163 }164 xmlBuffer.pop();165 }166 private void writeSuiteGroups(XMLStringBuffer xmlBuffer, ISuite suite) {167 xmlBuffer.push(XMLReporterConfig.TAG_GROUPS);168 Map<String, Collection<ITestNGMethod>> methodsByGroups = suite.getMethodsByGroups();169 for (Map.Entry<String, Collection<ITestNGMethod>> entry : methodsByGroups.entrySet()) {170 Properties groupAttrs = new Properties();171 groupAttrs.setProperty(XMLReporterConfig.ATTR_NAME, entry.getKey());172 xmlBuffer.push(XMLReporterConfig.TAG_GROUP, groupAttrs);173 Set<ITestNGMethod> groupMethods = getUniqueMethodSet(entry.getValue());174 for (ITestNGMethod groupMethod : groupMethods) {175 Properties methodAttrs = new Properties();176 methodAttrs.setProperty(XMLReporterConfig.ATTR_NAME, groupMethod.getMethodName());177 methodAttrs.setProperty(XMLReporterConfig.ATTR_METHOD_SIG, groupMethod.toString());178 methodAttrs.setProperty(XMLReporterConfig.ATTR_CLASS, groupMethod.getRealClass().getName());179 xmlBuffer.addEmptyElement(XMLReporterConfig.TAG_METHOD, methodAttrs);180 }...