package ru.yandex.qatools.allure.testng;

import org.junit.*;
import org.testng.TestNG;
import ru.yandex.qatools.allure.config.AllureModelUtils;
import ru.yandex.qatools.allure.testng.testdata.TestDataClass;

import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Validator;
import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static ru.yandex.qatools.allure.config.AllureNamingUtils.listTestSuiteFiles;
import static java.nio.file.FileVisitResult.*;

/**
 * @author Kirill Kozlov kozlov.k.e@gmail.com
 *         Date: 04.02.14
 */
public class AllureTestListenerXmlValidationTest {

    private static final String DEFAULT_SUITE_NAME = "suite";

    private static final String ALLURE_RESULTS_DIRECTORY_PROP = "allure.results.directory";
    private static final String ALLURE_RESULTS = "allure-results";

    private static Path resultsDir;

    @BeforeClass
    public static void setUpClass() throws IOException {
        resultsDir = Files.createTempDirectory(ALLURE_RESULTS);
        System.setProperty(ALLURE_RESULTS_DIRECTORY_PROP, resultsDir.toAbsolutePath().toString());

        AllureTestListener allureListener = new AllureTestListener();
        TestNG testNG = new TestNG();
        testNG.setDefaultSuiteName(DEFAULT_SUITE_NAME);
        testNG.setTestClasses(new Class[] { TestDataClass.class });
        testNG.setUseDefaultListeners(false);
        testNG.addListener(allureListener);

        testNG.run();
    }

    @AfterClass
    public static void tearDownClass() throws IOException {
        System.clearProperty(ALLURE_RESULTS_DIRECTORY_PROP);
        deleteNotEmptyDirectory(resultsDir);
    }

    @Test
    public void suiteFilesCountTest() throws Exception {
        assertThat(listTestSuiteFiles(resultsDir.toFile()).size(), is(1));
    }

    @Test
    public void validateSuiteFilesTest() throws Exception {
        Validator validator = AllureModelUtils.getAllureSchemaValidator();

        for (File each : listTestSuiteFiles(resultsDir.toFile())) {
            validator.validate(new StreamSource(each));
        }
    }

    private static void deleteNotEmptyDirectory(Path path) throws IOException {
        Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                Files.delete(file);
                return CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                if (exc == null) {
                    Files.delete(dir);
                    return CONTINUE;
                } else {
                    throw exc;
                }
            }
        });
    }
}

@@ -200,16 +200,74 @@
 taClass;
+%0Aimport ru.yandex.qatools.allure.utils.AllureResultsUtils;
 %0A%0Aimport
@@ -853,101 +853,8 @@
 te%22;
-%0A%0A    private static final String ALLURE_RESULTS_DIRECTORY_PROP = %22allure.results.directory%22;
 %0A   
@@ -966,21 +966,16 @@
  @Before
-Class
 %0A    pub
@@ -974,31 +974,24 @@
 %0A    public 
-static 
 void setUpCl
@@ -988,21 +988,16 @@
 id setUp
-Class
 () throw
@@ -1088,94 +1088,64 @@
     
-System.setProperty(ALLURE_RESULTS_DIRECTORY_PROP, resultsDir.toAbsolutePath().toString
+AllureResultsUtils.setResultsDirectory(resultsDir.toFile
 ());
@@ -1355,11 +1355,9 @@
 ss%5B%5D
- %7B 
+%7B
 Test
@@ -1371,17 +1371,16 @@
 ss.class
- 
 %7D);%0A    
@@ -1505,21 +1505,16 @@
   @After
-Class
 %0A    pub
@@ -1517,23 +1517,16 @@
  public 
-static 
 void tea
@@ -1530,21 +1530,16 @@
 tearDown
-Class
 () throw
@@ -1566,58 +1566,51 @@
     
-System.clearProperty(ALLURE_RESULTS_DIRECTORY_PROP
+AllureResultsUtils.setResultsDirectory(null
 );%0A 

Use new method for setting Allure results directory, make tests independent