<commit_before>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 <NAME> <EMAIL>
 *         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>() {<commit_msg>Use new method for setting Allure results directory, make tests independent<commit_after>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 ru.yandex.qatools.allure.utils.AllureResultsUtils;

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 <NAME> <EMAIL>
 *         Date: 04.02.14
 */
public class AllureTestListenerXmlValidationTest {

    private static final String DEFAULT_SUITE_NAME = "suite";
    private static final String ALLURE_RESULTS = "allure-results";

    private static Path resultsDir;

    @Before
    public void setUp() throws IOException {
        resultsDir = Files.createTempDirectory(ALLURE_RESULTS);
        AllureResultsUtils.setResultsDirectory(resultsDir.toFile());

        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();
    }

    @After
    public void tearDown() throws IOException {
        AllureResultsUtils.setResultsDirectory(null);
        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>() {
{'fraction_non_alphanumeric': 0.09638811711904254, 'fraction_numerical': 0.002992092327420389, 'mean_word_length': 4.037674919268031, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '540478', 'n_tokens_mistral': 1353, 'n_tokens_neox': 1316, 'n_words': 262}