package ru.yandex.qatools.allure.utils;

import com.sun.xml.bind.marshaller.CharacterEscapeHandler;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import ru.yandex.qatools.allure.config.AllureModelUtils;
import ru.yandex.qatools.allure.model.ObjectFactory;
import ru.yandex.qatools.allure.model.TestSuiteResult;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Validator;
import java.io.File;
import java.util.Arrays;
import java.util.Collection;

/**
 * @author Dmitry Baev charlie@yandex-team.ru
 *         Date: 27.03.14
 */
@RunWith(Parameterized.class)
public class XmlEscapeHandlerTest {

    private File testSuiteResultFile;

    private Marshaller m;

    private TestSuiteResult result;

    private String character;

    @Rule
    public TemporaryFolder folder = new TemporaryFolder();

    public XmlEscapeHandlerTest(String character) {
        this.character = character;
    }

    @Parameterized.Parameters
    public static Collection<Object[]> data() {
        return Arrays.asList(
                new Object[]{"\u0000"},
                new Object[]{"\u0017"}
        );
    }

    @Before
    public void setUp() throws Exception {
        testSuiteResultFile = folder.newFile();
        m = JAXBContext.newInstance(TestSuiteResult.class).createMarshaller();
        m.setProperty(
                CharacterEscapeHandler.class.getName(),
                XmlEscapeHandler.getInstance()
        );

        result = new TestSuiteResult().withName("name");
    }

    @Test
    public void dataWithInvalidCharacterTest() throws Exception {
        result.setTitle(character);
        m.marshal(new ObjectFactory().createTestSuite(result), testSuiteResultFile);
        Validator validator = AllureModelUtils.getAllureSchemaValidator();
        validator.validate(new StreamSource(testSuiteResultFile));
    }
}

@@ -443,16 +443,44 @@
 esult;%0A%0A
+import javax.xml.bind.JAXB;%0A
 import j
@@ -506,16 +506,16 @@
 ontext;%0A
-
 import j
@@ -703,16 +703,142 @@
 ction;%0A%0A
+import static org.hamcrest.core.Is.is;%0Aimport static org.junit.Assert.assertThat;%0Aimport static org.junit.Assert.assertTrue;%0A%0A
 /**%0A * @
@@ -1853,16 +1853,36 @@
 me(%22name
+-and-%D0%BA%D0%B8%D1%80%D0%B8%D0%BB%D0%BB%D0%B8%D1%86%D0%B5%D0%B9-also
 %22);%0A    
@@ -1984,25 +1984,49 @@
 etTitle(
-character
+%22prefix %22 + character + %22 suffix%22
 );%0A     
@@ -2176,24 +2176,24 @@
 alidator();%0A
-
         vali
@@ -2246,16 +2246,312 @@
 tFile));
+%0A%0A        TestSuiteResult testSuite = JAXB.unmarshal(testSuiteResultFile, TestSuiteResult.class);%0A        assertThat(testSuite.getName(), is(%22name-and-%D0%BA%D0%B8%D1%80%D0%B8%D0%BB%D0%BB%D0%B8%D1%86%D0%B5%D0%B9-also%22));%0A        assertTrue(testSuite.getTitle().startsWith(%22prefix %22));%0A        assertTrue(testSuite.getTitle().endsWith(%22 suffix%22));
 %0A    %7D%0A%7D

add test to bug #177