1 package test.reports;
2 
3 import javax.xml.parsers.ParserConfigurationException;
4 
5 import org.testng.Assert;
6 import org.testng.TestListenerAdapter;
7 import org.testng.TestNG;
8 import org.testng.annotations.Test;
9 import org.testng.collections.Lists;
10 import org.testng.reporters.FailedReporter;
11 import org.testng.xml.Parser;
12 import org.testng.xml.XmlClass;
13 import org.testng.xml.XmlSuite;
14 import org.testng.xml.XmlTest;
15 import org.xml.sax.SAXException;
16 
17 import test.SimpleBaseTest;
18 
19 import java.io.ByteArrayInputStream;
20 import java.io.File;
21 import java.io.IOException;
22 import java.util.Collection;
23 
24 public class FailedReporterTest extends SimpleBaseTest {
25 
26   private static final String XML = "<suite name=\"Suite\">\n"
27       + "<parameter name=\"n\" value=\"42\"/>\n"
28       + "<test name=\"Test\">\n"
29       + "<classes>\n"
30       + "<parameter name=\"o\" value=\"43\"/>\n"
31       + "<class name=\"test.reports.FailedSampleTest\">\n"
32       + "<parameter name=\"p\" value=\"44\"/>\n"
33       + "</class>"
34       + "</classes>"
35       + "</test></suite>";
36 
37   @Test
failedFile()38   public void failedFile() throws ParserConfigurationException, SAXException, IOException {
39     TestNG tng = new TestNG();
40     tng.setVerbose(0);
41     Collection<XmlSuite> suites =
42         new Parser(new ByteArrayInputStream(XML.getBytes())).parse();
43     tng.setXmlSuites(Lists.newArrayList(suites));
44     TestListenerAdapter tla = new TestListenerAdapter();
45     File f = new File("/tmp");
46     tng.setOutputDirectory(f.getAbsolutePath());
47     tng.addListener(tla);
48     tng.run();
49 
50     Collection<XmlSuite> failedSuites =
51         new Parser(new File(f, FailedReporter.TESTNG_FAILED_XML).getAbsolutePath()).parse();
52     XmlSuite failedSuite = failedSuites.iterator().next();
53     Assert.assertEquals("42", failedSuite.getParameter("n"));
54 
55     XmlTest test = failedSuite.getTests().get(0);
56     Assert.assertEquals("43", test.getParameter("o"));
57 
58     XmlClass c = test.getClasses().get(0);
59     Assert.assertEquals("44", c.getAllParameters().get("p"));
60   }
61 }
62