1 package test.reports;
2 
3 import org.testng.Assert;
4 import org.testng.ITestListener;
5 import org.testng.ITestResult;
6 import org.testng.Reporter;
7 import org.testng.TestListenerAdapter;
8 import org.testng.TestNG;
9 import org.testng.annotations.Test;
10 import org.testng.xml.XmlSuite;
11 import org.testng.xml.XmlTest;
12 
13 import test.TestHelper;
14 
15 import java.io.File;
16 import java.util.Arrays;
17 import java.util.List;
18 
19 public class ReportTest {
20 
21   @Test
verifyIndex()22   public void verifyIndex() {
23     File outputDir = TestHelper.createRandomDirectory();
24 
25     String suiteName = "VerifyIndexSuite";
26     String testName = "TmpTest";
27     XmlSuite suite = TestHelper.createSuite(test.simple.SimpleTest.class, suiteName, testName);
28 
29     TestNG tng = TestHelper.createTestNG(suite, outputDir.getAbsolutePath());
30 
31     File f = getHtmlReportFile(outputDir, suiteName, testName);
32 
33     tng.run();
34 
35     Assert.assertTrue(f.exists());
36   }
37 
38   @Test
directoryShouldBeSuiteName()39   public void directoryShouldBeSuiteName() {
40     File outputDirectory = TestHelper.createRandomDirectory();
41 
42     XmlSuite xmlSuite = new XmlSuite();
43     String suiteName = "ReportTestSuite1";
44     xmlSuite.setName(suiteName);
45 
46     XmlTest xmlTest = new XmlTest(xmlSuite);
47     String testName = "Test1";
48     xmlTest.setName(testName);
49 
50     XmlTest xmlTest2 = new XmlTest(xmlSuite);
51     String testName2 = "Test2";
52     xmlTest2.setName(testName2);
53 
54     TestNG testng = new TestNG();
55     testng.setVerbose(0);
56     testng.setOutputDirectory(outputDirectory.getAbsolutePath());
57     testng.setXmlSuites(Arrays.asList(xmlSuite));
58 
59     File f = getHtmlReportFile(outputDirectory, suiteName, testName);
60     File f2 = getHtmlReportFile(outputDirectory, suiteName, testName2);
61 
62     testng.run();
63 
64     Assert.assertTrue(f.exists(), "Expected to find file:" + f);
65     Assert.assertTrue(f2.exists(), "Expected to find file:" + f2);
66   }
67 
68   @Test
oneDirectoryPerSuite()69   public void oneDirectoryPerSuite() {
70     File outputDirectory = TestHelper.createRandomDirectory();
71 
72     String testName = "TmpTest";
73     String suiteNameA = "ReportSuiteA";
74     XmlSuite xmlSuiteA = TestHelper.createSuite(test.reports.A.class, suiteNameA, testName);
75 
76     String suiteNameB = "ReportSuiteB";
77     XmlSuite xmlSuiteB = TestHelper.createSuite(test.reports.B.class, suiteNameB, testName);
78 
79     TestNG testng = TestHelper.createTestNG();
80     testng.setOutputDirectory(outputDirectory.getAbsolutePath());
81     testng.setXmlSuites(Arrays.asList(xmlSuiteA, xmlSuiteB));
82 
83     File f1 = getHtmlReportFile(outputDirectory, suiteNameA, testName);
84     File f2 = getHtmlReportFile(outputDirectory, suiteNameB, testName);
85 
86     testng.run();
87 
88     Assert.assertTrue(f1.exists());
89     Assert.assertTrue(f2.exists());
90   }
91 
getHtmlReportFile(File outputDir, String suiteName, String testName)92   private static File getHtmlReportFile(File outputDir, String suiteName, String testName) {
93     File f = new File(outputDir.getAbsolutePath() + File.separatorChar + suiteName
94                     + File.separatorChar + testName + ".html");
95     if (f.exists()) {
96       f.delete();
97     }
98     return f;
99   }
100 
101   @Test
shouldHonorSuiteName()102   public void shouldHonorSuiteName() {
103     TestNG testng = TestHelper.createTestNG();
104     testng.setTestClasses(new Class[] { A.class, B.class });
105     String outputDir = testng.getOutputDirectory();
106 
107     String dirA = outputDir + File.separatorChar + "SuiteA-JDK5";
108     File fileA = new File(dirA);
109     String dirB = outputDir + File.separatorChar + "SuiteB-JDK5";
110     File fileB = new File(dirB);
111     Assert.assertFalse(fileA.exists());
112     Assert.assertFalse(fileB.exists());
113 
114     testng.run();
115 
116     Assert.assertTrue(fileA.exists(), fileA + " wasn't created");
117     Assert.assertTrue(fileB.exists(), fileB + " wasn't created");
118   }
119 
120   static boolean m_success = false;
121 
122   @Test
reportLogShouldBeAvailableEvenWithTimeOut()123   public void reportLogShouldBeAvailableEvenWithTimeOut() {
124     m_success = false;
125     TestNG tng = new TestNG();
126     tng.setVerbose(0);
127     tng.setTestClasses(new Class[] { ReporterSampleTest.class });
128 
129     ITestListener listener = new TestListenerAdapter() {
130       @Override
131       public void onTestSuccess(ITestResult tr) {
132         super.onTestSuccess(tr);
133         List<String> output = Reporter.getOutput(tr);
134         m_success = output != null && output.size() > 0;
135       }
136     };
137     tng.addListener(listener);
138     tng.run();
139 
140     Assert.assertTrue(m_success);
141   }
142 }
143