
...6import org.testng.annotations.AfterClass;7import org.testng.annotations.BeforeClass;8import org.testng.annotations.DataProvider;9import org.testng.annotations.Test;10import org.testng.reporters.EmailableReporter;11import org.testng.reporters.EmailableReporter2;12import test.SimpleBaseTest;13import java.io.File;14import java.lang.reflect.Method;15import java.security.Permission;16public class EmailableReporterTest extends SimpleBaseTest {17    private SecurityManager manager;18    @BeforeClass(alwaysRun = true)19    public void setup() {20        manager = System.getSecurityManager();21        System.setSecurityManager(new MySecurityManager(manager));22    }23    @AfterClass(alwaysRun = true)24    public void cleanup() {25        System.setSecurityManager(manager);26    }27    @Test(dataProvider = "getReporterInstances", priority = 1)28    public void testReportsNameCustomizationViaRunMethodInvocationAndJVMArguments(IReporter reporter, String jvm) {29        runTestViaRunMethod(reporter, jvm);30    }31    @Test(dataProvider = "getReporterInstances", priority = 2)32    public void testReportsNameCustomizationViaRunMethodInvocation(IReporter reporter) {33        runTestViaRunMethod(reporter, null /* no jvm arguments */);34    }35    @Test(dataProvider = "getReporterNames", priority = 3)36    public void testReportsNameCustomizationViaMainMethodInvocation(String clazzName) {37        runTestViaMainMethod(clazzName, null /* no jvm arguments */);38    }39    @Test(dataProvider = "getReporterNames", priority = 4)40    public void testReportsNameCustomizationViaMainMethodInvocationAndJVMArguments(String clazzName, String jvm) {41        runTestViaMainMethod(clazzName, jvm);42    }43    @DataProvider(name = "getReporterInstances")44    public Object[][] getReporterInstances(Method method) {45        if (method.getName().toLowerCase().contains("jvmarguments")) {46            return new Object[][] {47                {new EmailableReporter(), "emailable.report.name"},48                {new EmailableReporter2(), "emailable.report2.name"}49            };50        }51        return new Object[][] {52            {new EmailableReporter()},53            {new EmailableReporter2()}54        };55    }56    @DataProvider(name = "getReporterNames")57    public Object[][] getReporterNames(Method method) {58        if (method.getName().toLowerCase().contains("jvmarguments")) {59            return new Object[][] {60                {EmailableReporter.class.getName(), "emailable.report.name"},61                {EmailableReporter2.class.getName(), "emailable.report2.name"}62            };63        }64        return new Object[][] {65            {EmailableReporter.class.getName()},66            {EmailableReporter2.class.getName()}67        };68    }69    private void runTestViaMainMethod(String clazzName, String jvm) {70        String name = Long.toString(System.currentTimeMillis());71        File output = createDirInTempDir(name);72        String filename = "report" + name + ".html";73        String[] args = {"-d", output.getAbsolutePath(), "-reporter", clazzName +74            ":fileName=" + filename, "src/test/resources/1332.xml"};75        try {76            if (jvm != null) {77                System.setProperty(jvm, filename);78            }79            TestNG.main(args);80            if (jvm != null) {81                //reset the jvm arguments82                System.setProperty(jvm, "");83            }84        } catch (SecurityException t) {85            //Gobble Security exception86        }87        File actual = new File(output.getAbsolutePath(), filename);88        Assert.assertEquals(actual.exists(), true);89    }90    private void runTestViaRunMethod(IReporter reporter, String jvm) {91        String name = Long.toString(System.currentTimeMillis());92        File output = createDirInTempDir(name);93        String filename = "report" + name + ".html";94        if (jvm != null) {95            System.setProperty(jvm, filename);96        }97        TestNG testNG = create();98        testNG.setOutputDirectory(output.getAbsolutePath());99        if (reporter instanceof EmailableReporter2) {100            ((EmailableReporter2) reporter).setFileName(filename);101        }102        if (reporter instanceof EmailableReporter) {103            ((EmailableReporter) reporter).setFileName(filename);104        }105        testNG.addListener((ITestNGListener) reporter);106        testNG.setTestClasses(new Class[] {ReporterSample.class});107        testNG.run();108        if (jvm != null) {109            //reset the jvm argument if it was set110            System.setProperty(jvm, "");111        }112        File actual = new File(output.getAbsolutePath(), filename);113        Assert.assertEquals(actual.exists(), true);114    }115    public static class MySecurityManager extends SecurityManager {116        private SecurityManager baseSecurityManager;117        MySecurityManager(SecurityManager baseSecurityManager) {...
