<commit_before>package ru.yandex.qatools.allure;

import org.junit.*;
import org.junit.rules.TemporaryFolder;
import org.xml.sax.SAXException;
import ru.yandex.qatools.allure.config.AllureModelUtils;
import ru.yandex.qatools.allure.events.*;
import ru.yandex.qatools.allure.model.*;
import ru.yandex.qatools.allure.utils.AllureResultsUtils;

import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Validator;
import java.io.File;
import java.io.IOException;

import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;
import static ru.yandex.qatools.allure.config.AllureNamingUtils.listTestSuiteFiles;

/**
 * @author <NAME> <EMAIL>
 *         Date: 14.12.13
 */
public class AllureLifecycleTest {

    @Rule
    public TemporaryFolder folder = new TemporaryFolder();

    public File resultsDirectory;

    @Before
    public void setUp() throws Exception {
        resultsDirectory = folder.newFolder();
        AllureResultsUtils.setResultsDirectory(resultsDirectory);
    }

    @Test
    public void allureLifecycleTest() throws Exception {
        TestSuiteResult testSuite = fireTestSuiteStart();
        TestSuiteResult anotherTestSuite = fireCustomTestSuiteEvent();
        assertEquals(testSuite, anotherTestSuite);

        TestCaseResult testCase = fireTestCaseStart();
        TestCaseResult anotherTestCase = fireCustomTestCaseEvent();
        assertEquals(testCase, anotherTestCase);

        assertThat(testSuite.getTestCases(), hasSize(1));
        assertEquals(testSuite.getTestCases().get(0), testCase);

        Step parentStep = fireStepStart();
        Attachment firstAttach = fireMakeAttach();

        assertThat(parentStep.getAttachments(), hasSize(1));
        assertEquals(parentStep.getAttachments().get(0), firstAttach);

        Step nestedStep = fireStepStart();
        Attachment secondAttach = fireMakeAttach();

        assertNotEquals(firstAttach, secondAttach);

        assertThat(nestedStep.getAttachments(), hasSize(1));
        assertEquals(nestedStep.getAttachments().get(0), secondAttach);

        fireStepFinished();

        assertThat(parentStep.getSteps(), hasSize(1));
        assertEquals(parentStep.getSteps().get(0), nestedStep);

        fireStepFinished();

        Attachment testCaseAttachment = fireMakeAttach();

        fireTestCaseFinished();

        assertThat(testCase.getSteps(), hasSize(1));
        assertEquals(testCase.getSteps().get(0), parentStep);

        assertThat(testCase.getAttachments(), hasSize(1));
        assertEquals(testCase.getAttachments().get(0), testCaseAttachment);

        fireTestSuiteFinished();
        validateTestSuite();

        assertThat(testSuite.getTestCases(), hasSize(1));

        TestSuiteResult nextTestSuite = fireTestSuiteStart();
        assertNotEquals(anotherTestSuite, nextTestSuite);
    }

    public TestSuiteResult fireTestSuiteStart() {
        Allure.LIFECYCLE.fire(new TestSuiteStartedEvent("some.uid", "some.suite.name"));
        TestSuiteResult testSuite = Allure.LIFECYCLE.getTestSuiteStorage().get("some.uid");
        assertNotNull(testSuite);
        assertThat(testSuite.getName(), is("some.suite.name"));
        assertThat(testSuite.getTestCases(), hasSize(0));
        return testSuite;
    }<commit_msg>added test for clearing events<commit_after>package ru.yandex.qatools.allure;

import org.junit.*;
import org.junit.rules.TemporaryFolder;
import org.xml.sax.SAXException;
import ru.yandex.qatools.allure.config.AllureModelUtils;
import ru.yandex.qatools.allure.events.*;
import ru.yandex.qatools.allure.events.clearstorage.ClearStepStorageEvent;
import ru.yandex.qatools.allure.events.clearstorage.ClearTestStorageEvent;
import ru.yandex.qatools.allure.model.*;
import ru.yandex.qatools.allure.utils.AllureResultsUtils;

import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Validator;
import java.io.File;
import java.io.IOException;

import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;
import static ru.yandex.qatools.allure.config.AllureNamingUtils.listTestSuiteFiles;

/**
 * @author <NAME> <EMAIL>
 *         Date: 14.12.13
 */
public class AllureLifecycleTest {

    @Rule
    public TemporaryFolder folder = new TemporaryFolder();

    public File resultsDirectory;

    @Before
    public void setUp() throws Exception {
        resultsDirectory = folder.newFolder();
        AllureResultsUtils.setResultsDirectory(resultsDirectory);
    }

    @Test
    public void allureLifecycleTest() throws Exception {
        TestSuiteResult testSuite = fireTestSuiteStart();
        TestSuiteResult anotherTestSuite = fireCustomTestSuiteEvent();
        assertEquals(testSuite, anotherTestSuite);

        TestCaseResult testCase = fireTestCaseStart();
        TestCaseResult anotherTestCase = fireCustomTestCaseEvent();
        assertEquals(testCase, anotherTestCase);

        assertThat(testSuite.getTestCases(), hasSize(1));
        assertEquals(testSuite.getTestCases().get(0), testCase);

        Step parentStep = fireStepStart();
        Attachment firstAttach = fireMakeAttach();

        assertThat(parentStep.getAttachments(), hasSize(1));
        assertEquals(parentStep.getAttachments().get(0), firstAttach);

        Step nestedStep = fireStepStart();
        Attachment secondAttach = fireMakeAttach();

        assertNotEquals(firstAttach, secondAttach);

        assertThat(nestedStep.getAttachments(), hasSize(1));
        assertEquals(nestedStep.getAttachments().get(0), secondAttach);

        fireStepFinished();

        assertThat(parentStep.getSteps(), hasSize(1));
        assertEquals(parentStep.getSteps().get(0), nestedStep);

        fireStepFinished();

        Attachment testCaseAttachment = fireMakeAttach();

        fireTestCaseFinished();

        assertThat(testCase.getSteps(), hasSize(1));
        assertEquals(testCase.getSteps().get(0), parentStep);

        assertThat(testCase.getAttachments(), hasSize(1));
        assertEquals(testCase.getAttachments().get(0), testCaseAttachment);

        fireTestSuiteFinished();
        validateTestSuite();

        assertThat(testSuite.getTestCases(), hasSize(1));

        TestSuiteResult nextTestSuite = fireTestSuiteStart();
        assertNotEquals(anotherTestSuite, nextTestSuite);
    }

    @Test
    public void allureClearStorageTest(){
        TestSuiteResult testSuite = fireTestSuiteStart();
        TestCaseResult testCase = fireTestCaseStart();
        assertThat(testSuite.getTestCases(), hasSize(1));
        assertEquals(testSuite.getTestCases().get(0), testCase);

        Step parentStep = fireStepStart();
        Step nestedStep = fireStepStart();
        fireStepFinished();

        assertThat(parentStep.getSteps(), hasSize(1));
        assertEquals(parentStep.getSteps().get(0), nestedStep);

        fireStepFinished();
        fireClearStepStorage();

        assertThat(testCase.getSteps(), hasSize(0));
        fireClearTestStorage();
        TestCaseResult afterClearing = Allure.LIFECYCLE.getTestCaseStorage().get();
        assertNotEquals(testCase, afterClearing);
        checkTestCaseIsNew(afterClearing);


    }

    private void checkTestCaseIsNew(TestCaseResult testCaseResult) {
        assertNull(testCaseResult.getName());
        assertNull(testCaseResult.getTitle());
        assertNull(testCaseResult.getDescription());
        assertNull(testCaseResult.getFailure());
        assertNull(testCaseResult.getStatus());
        assertNull(testCaseResult.getSeverity());
        assertTrue(testCaseResult.getSteps().isEmpty());
        assertTrue(testCaseResult.getAttachments().isEmpty());
        assertTrue(testCaseResult.getLabels().isEmpty());
        assertTrue(testCaseResult.getParameters().isEmpty());
        assertTrue(testCaseResult.getStart() == 0 && testCaseResult.getStop()==0);
    }

    private void fireClearTestStorage() {
        Allure.LIFECYCLE.fire(new ClearTestStorageEvent());
    }

    private void fireClearStepStorage() {
        Allure.LIFECYCLE.fire(new ClearStepStorageEvent());
    }

    public TestSuiteResult fireTestSuiteStart() {
        Allure.LIFECYCLE.fire(new TestSuiteStartedEvent("some.uid", "some.suite.name"));
        TestSuiteResult testSuite = Allure.LIFECYCLE.getTestSuiteStorage().get("some.uid");
        assertNotNull(testSuite);
        assertThat(testSuite.getName(), is("some.suite.name"));
        assertThat(testSuite.getTestCases(), hasSize(0));
        return testSuite;
    }
{'fraction_non_alphanumeric': 0.11316127525399977, 'fraction_numerical': 0.005488730585075324, 'mean_word_length': 3.8881278538812785, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '4049783', 'n_tokens_mistral': 2432, 'n_tokens_neox': 2386, 'n_words': 377}