package ru.yandex.qatools.allure.data;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.RegexFileFilter;
import org.junit.rules.ExternalResource;
import org.junit.rules.TemporaryFolder;
import ru.yandex.qatools.allure.model.TestSuiteResult;

import javax.xml.bind.JAXB;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by eroshenkoam on 12/10/13.
 */
public class AllureReportGenerationRule extends ExternalResource {

    private final File reportDataDir;

    private final File resultsDir;

    private AllureXUnit allureXUnit;

    private List<TestSuiteResult> testSuiteResults;

    public AllureReportGenerationRule(String resultsPath) {
        this(FileUtils.getTempDirectory(), resultsPath);
    }

    public AllureReportGenerationRule(File reportDataDir, String resultsPath) {
        this(reportDataDir, ClassLoader.getSystemResource(resultsPath));
    }

    public AllureReportGenerationRule(File reportDataDir, URL resultsURI) {
        this(reportDataDir, new File(resultsURI.getFile()));
    }

    public AllureReportGenerationRule(File reportDataDir, File resultsDir) {
        this.reportDataDir = reportDataDir;
        this.resultsDir = resultsDir;
    }


    protected void before() throws Throwable {
        AllureReportGenerator reportGenerator = new AllureReportGenerator(resultsDir);
        reportGenerator.generate(reportDataDir);
    }

    protected void after() {
        try {
            FileUtils.deleteDirectory(this.reportDataDir);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public AllureXUnit getXUnitData() throws Exception {
        if (allureXUnit == null) {
            ObjectMapper mapper = new ObjectMapper();
            allureXUnit = mapper.readValue(new File(reportDataDir, "xunit.json"), AllureXUnit.class);
        }
        return allureXUnit;
    }

    public List<TestSuiteResult> getTestSuiteResults() {
        if (testSuiteResults == null) {
            testSuiteResults = new ArrayList<>();
            for (String path : resultsDir.list(new RegexFileFilter(TestSuiteFiles.TEST_SUITES_MASK))) {
                testSuiteResults.add(JAXB.unmarshal(new File(resultsDir, path), TestSuiteResult.class));
            }
        }
        return testSuiteResults;
    }

}

@@ -44,146 +44,161 @@
 ort 
-com.fasterxml.jackson.databind.ObjectMapper;%0Aimport org.apache.commons.io.FileUtils;%0Aimport org.apache.commons.io.filefilter.RegexFileFilt
+org.apache.commons.io.filefilter.RegexFileFilter;%0Aimport ru.yandex.qatools.allure.model.TestSuiteResult;%0Aimport com.fasterxml.jackson.databind.ObjectMapp
 er;%0A
@@ -253,93 +253,66 @@
 org.
-junit.rules.TemporaryFolder
+apache.commons.io.FileUtils
 ;%0A
+%0A
 import 
-ru.yandex.qatools.allure.model.TestSuiteResul
+java.util.ArrayLis
 t;%0A
-%0A
 impo
@@ -347,23 +347,25 @@
 rt java.
-io.File
+util.List
 ;%0Aimport
@@ -377,112 +377,33 @@
 .io.
-IOException;%0Aimport java.net.URI;%0Aimport java.net.URL;%0Aimport java.util.ArrayList;%0Aimport java.util.List
+File;%0Aimport java.net.URL
 ;%0A%0A/
@@ -1500,26 +1500,8 @@
 ) %7B%0A
-        try %7B%0A    
     
@@ -1520,24 +1520,22 @@
 s.delete
-Director
+Quietl
 y(this.r
@@ -1553,95 +1553,8 @@
 r);%0A
-        %7D catch (IOException e) %7B%0A            throw new RuntimeException(e);%0A        %7D%0A
     

Fix folder deletion problem