/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.maven.plugin.surefire.report;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.maven.plugin.surefire.booterclient.output.DeserializedStacktraceWriter;
import org.apache.maven.surefire.api.report.ReportEntry;
import org.apache.maven.surefire.api.report.SimpleReportEntry;
import org.apache.maven.surefire.api.report.StackTraceWriter;
import org.apache.maven.surefire.shared.utils.xml.Xpp3Dom;
import org.apache.maven.surefire.shared.utils.xml.Xpp3DomBuilder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.nio.file.Files.readAllLines;
import static org.apache.maven.plugin.surefire.report.ReportEntryType.ERROR;
import static org.apache.maven.plugin.surefire.report.ReportEntryType.SKIPPED;
import static org.apache.maven.plugin.surefire.report.ReportEntryType.SUCCESS;
import static org.apache.maven.surefire.api.report.RunMode.NORMAL_RUN;
import static org.apache.maven.surefire.api.report.RunMode.RERUN_TEST_AFTER_FAILURE;
import static org.apache.maven.surefire.api.util.internal.ObjectUtils.systemProps;
import static org.apache.maven.surefire.api.util.internal.StringUtils.NL;
import static org.apache.maven.surefire.shared.utils.StringUtils.isEmpty;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
 *
 */
@SuppressWarnings({"ResultOfMethodCallIgnored", "checkstyle:magicnumber"})
public class StatelessXmlReporterTest {
    private static final String XSD =
            "https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report.xsd";
    private static final String TEST_ONE = "aTestMethod";
    private static final String TEST_TWO = "bTestMethod";
    private static final String TEST_THREE = "cTestMethod";
    private static final AtomicInteger DIRECTORY_PREFIX = new AtomicInteger();

    private TestSetStats stats;
    private TestSetStats rerunStats;
    private File expectedReportFile;
    private File reportDir;

    @SuppressWarnings("unchecked")
    private static <T> T getInternalState(Object target, String fieldName) {
        try {
            Class<?> clazz = target.getClass();
            while (clazz != null) {
                try {
                    Field field = clazz.getDeclaredField(fieldName);
                    field.setAccessible(true);
                    return (T) field.get(target);
                } catch (NoSuchFieldException e) {
                    clazz = clazz.getSuperclass();
                }
            }
            throw new NoSuchFieldException(fieldName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static void setInternalState(Object target, String fieldName, Object value) {
        try {
            Class<?> clazz = target.getClass();
            while (clazz != null) {
                try {
                    Field field = clazz.getDeclaredField(fieldName);
                    field.setAccessible(true);
                    field.set(target, value);
                    return;
                } catch (NoSuchFieldException e) {
                    clazz = clazz.getSuperclass();
                }
            }
            throw new NoSuchFieldException(fieldName);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    @SuppressWarnings("unchecked")
    private static <T> T invokeMethod(Object target, String methodName, Object... args) throws Exception {
        Class<?> clazz = target.getClass();
        while (clazz != null) {
            for (Method method : clazz.getDeclaredMethods()) {
                if (method.getName().equals(methodName) && method.getParameterCount() == args.length) {
                    method.setAccessible(true);
                    return (T) method.invoke(target, args);
                }
            }
            clazz = clazz.getSuperclass();
        }
        throw new NoSuchMethodException(methodName);
    }

    @BeforeEach
    protected void setUp() throws Exception {
        stats = new TestSetStats(false, true);
        rerunStats = new TestSetStats(false, true);

        File basedir = new File(".");
        File target = new File(basedir.getCanonicalFile(), "target");
        target.mkdir();
        String reportRelDir = getClass().getSimpleName() + "-" + DIRECTORY_PREFIX.incrementAndGet();
        reportDir = new File(target, reportRelDir);
        reportDir.mkdir();
    }

    @AfterEach
    protected void tearDown() {
        if (expectedReportFile != null) {
            expectedReportFile.delete();
        }
    }

    @Test
    public void testFileNameWithoutSuffix() {
        StatelessXmlReporter reporter = new StatelessXmlReporter(
                reportDir,
                null,
                false,
                0,
                new ConcurrentHashMap<String, Deque<WrappedReportEntry>>(),
                XSD,
                "3.0.2",
                false,
                false,
                false,
                false,
                true,
                true,
                false);
        reporter.cleanTestHistoryMap();

        ReportEntry reportEntry = new SimpleReportEntry(
                NORMAL_RUN, 0L, getClass().getName(), null, getClass().getName(), null, 12);
        WrappedReportEntry testSetReportEntry = new WrappedReportEntry(
                reportEntry, ReportEntryType.SUCCESS, 1771085631L, 12, null, null, systemProps());
        stats.testSucceeded(testSetReportEntry);
        reporter.testSetCompleted(testSetReportEntry, stats);

        expectedReportFile = new File(reportDir, "TEST-" + getClass().getName() + ".xml");
        assertTrue(
                expectedReportFile.exists(),
                "Report file (" + expectedReportFile.getAbsolutePath() + ") doesn't exist");
    }

    @Test
    public void testAllFieldsSerialized() throws IOException {
        ReportEntry reportEntry =
                new SimpleReportEntry(NORMAL_RUN, 0L, getClass().getName(), null, TEST_ONE, null, 12);
        WrappedReportEntry testSetReportEntry =
                new WrappedReportEntry(reportEntry, SUCCESS, 1771085631L, 12, null, null, systemProps());
        expectedReportFile = new File(reportDir, "TEST-" + getClass().getName() + ".xml");

        stats.testSucceeded(testSetReportEntry);
        StackTraceWriter stackTraceWriter = new DeserializedStacktraceWriter("A fud msg", "trimmed", "fail at foo");
        Utf8RecodingDeferredFileOutputStream stdOut = new Utf8RecodingDeferredFileOutputStream("fds");
        String stdOutPrefix;
        String stdErrPrefix;
        if (defaultCharsetSupportsSpecialChar()) {
            stdErrPrefix = "std-\u0115rr";
            stdOutPrefix = "st]]>d-o\u00DCt";
        } else {
            stdErrPrefix = "std-err";
            stdOutPrefix = "st]]>d-out";
        }

        stdOut.write(stdOutPrefix + "<null>!\u0020\u0000\u001F", false, null);

        Utf8RecodingDeferredFileOutputStream stdErr = new Utf8RecodingDeferredFileOutputStream("fds");

        stdErr.write(stdErrPrefix + "?&-&amp;&#163;\u0020\u0000\u001F", false, null);
        WrappedReportEntry t2 = new WrappedReportEntry(
                new SimpleReportEntry(NORMAL_RUN, 0L, getClass().getName(), null, TEST_TWO, null, stackTraceWriter, 13),
                ReportEntryType.ERROR,
                1771085631L,
                13,
                stdOut,
                stdErr);

        stats.testSucceeded(t2);
        StatelessXmlReporter reporter = new StatelessXmlReporter(
                reportDir,
                null,
                false,
                0,
                new ConcurrentHashMap<String, Deque<WrappedReportEntry>>(),
                XSD,
                "3.0.2",
                false,
                false,
                false,
                false,
                true,
                true,
                false);
        reporter.testSetCompleted(testSetReportEntry, stats);

        Xpp3Dom testSuite;
        try (FileInputStream fileInputStream = new FileInputStream(expectedReportFile);
                InputStreamReader reader = new InputStreamReader(fileInputStream, UTF_8)) {
            testSuite = Xpp3DomBuilder.build(reader);
        }
        assertEquals("testsuite", testSuite.getName());
        Xpp3Dom properties = testSuite.getChild("properties");
        assertEquals(System.getProperties().size(), properties.getChildCount());
        Xpp3Dom child = properties.getChild(1);
        assertFalse(isEmpty(child.getAttribute("value")));
        assertFalse(isEmpty(child.getAttribute("name")));

        Xpp3Dom[] testcase = testSuite.getChildren("testcase");
        Xpp3Dom tca = testcase[0];
        assertEquals(TEST_ONE, tca.getAttribute("name"));
        assertEquals("0.012", tca.getAttribute("time"));
        assertEquals(getClass().getName(), tca.getAttribute("classname"));

        Xpp3Dom tcb = testcase[1];
        assertEquals(TEST_TWO, tcb.getAttribute("name"));
        assertEquals("0.013", tcb.getAttribute("time"));
        assertEquals(getClass().getName(), tcb.getAttribute("classname"));
        Xpp3Dom errorNode = tcb.getChild("error");
        assertNotNull(errorNode);
        assertEquals("A fud msg", errorNode.getAttribute("message"));
        assertEquals("fail at foo", errorNode.getAttribute("type"));
        assertEquals(
                stdOutPrefix + "<null>! &amp#0;&amp#31;",
                tcb.getChild("system-out").getValue());

        assertEquals(
                stdErrPrefix + "?&-&amp;&#163; &amp#0;&amp#31;",
                tcb.getChild("system-err").getValue());
    }

    @Test
    public void testOutputRerunFlakyFailure() throws IOException {
        WrappedReportEntry testSetReportEntry = new WrappedReportEntry(
                new SimpleReportEntry(NORMAL_RUN, 0L, getClass().getName(), null, TEST_ONE, null, 12),
                ReportEntryType.SUCCESS,
                1771085631L,
                12,
                null,
                null,
                systemProps());
        expectedReportFile = new File(reportDir, "TEST-" + getClass().getName() + ".xml");

        stats.testSucceeded(testSetReportEntry);
        StackTraceWriter stackTraceWriterOne = new DeserializedStacktraceWriter("A fud msg", "trimmed", "fail at foo");
        StackTraceWriter stackTraceWriterTwo =
                new DeserializedStacktraceWriter("A fud msg two", "trimmed two", "fail at foo two");

        String firstRunOut = "first run out";
        String firstRunErr = "first run err";
        String secondRunOut = "second run out";
        String secondRunErr = "second run err";

        String cls = getClass().getName();
        WrappedReportEntry testTwoFirstError = new WrappedReportEntry(
                new SimpleReportEntry(NORMAL_RUN, 0L, cls, null, TEST_TWO, null, stackTraceWriterOne, 5),
                ReportEntryType.ERROR,
                1771085631L,
                5,
                createStdOutput(firstRunOut),
                createStdOutput(firstRunErr));

        WrappedReportEntry testTwoSecondError = new WrappedReportEntry(
                new SimpleReportEntry(RERUN_TEST_AFTER_FAILURE, 1L, cls, null, TEST_TWO, null, stackTraceWriterTwo, 13),
                ReportEntryType.ERROR,
                1771085631L,
                13,
                createStdOutput(secondRunOut),
                createStdOutput(secondRunErr));

        WrappedReportEntry testThreeFirstRun = new WrappedReportEntry(
                new SimpleReportEntry(NORMAL_RUN, 2L, cls, null, TEST_THREE, null, stackTraceWriterOne, 13),
                ReportEntryType.FAILURE,
                1771085631L,
                13,
                createStdOutput(firstRunOut),
                createStdOutput(firstRunErr));

        WrappedReportEntry testThreeSecondRun = new WrappedReportEntry(
                new SimpleReportEntry(
                        RERUN_TEST_AFTER_FAILURE, 3L, cls, null, TEST_THREE, null, stackTraceWriterTwo, 2),
                ReportEntryType.SUCCESS,
                1771085631L,
                2,
                createStdOutput(secondRunOut),
                createStdOutput(secondRunErr));

        stats.testSucceeded(testTwoFirstError);
        stats.testSucceeded(testThreeFirstRun);
        rerunStats.testSucceeded(testTwoSecondError);
        rerunStats.testSucceeded(testThreeSecondRun);

        StatelessXmlReporter reporter = new StatelessXmlReporter(
                reportDir,
                null,
                false,
                1,
                new HashMap<String, Deque<WrappedReportEntry>>(),
                XSD,
                "3.0.2",
                false,
                false,
                false,
                false,
                true,
                true,
                false);

        reporter.testSetCompleted(testSetReportEntry, stats);
        reporter.testSetCompleted(testSetReportEntry, rerunStats);

        Xpp3Dom testSuite;
        try (FileInputStream fileInputStream = new FileInputStream(expectedReportFile);
                InputStreamReader reader = new InputStreamReader(fileInputStream, UTF_8)) {
            testSuite = Xpp3DomBuilder.build(reader);
        }
        assertEquals("testsuite", testSuite.getName());
        assertEquals("0.012", testSuite.getAttribute("time"));
        Xpp3Dom properties = testSuite.getChild("properties");
        assertEquals(System.getProperties().size(), properties.getChildCount());
        Xpp3Dom child = properties.getChild(1);
        assertFalse(isEmpty(child.getAttribute("value")));
        assertFalse(isEmpty(child.getAttribute("name")));

        Xpp3Dom[] testcase = testSuite.getChildren("testcase");
        Xpp3Dom testCaseOne = testcase[0];
        assertEquals(TEST_ONE, testCaseOne.getAttribute("name"));
        assertEquals("0.012", testCaseOne.getAttribute("time"));
        assertEquals(getClass().getName(), testCaseOne.getAttribute("classname"));

        Xpp3Dom testCaseTwo = testcase[1];
        assertEquals(TEST_TWO, testCaseTwo.getAttribute("name"));
        // Run time for a rerun failing test is the run time of the first run
        assertEquals("0.005", testCaseTwo.getAttribute("time"));
        assertEquals(getClass().getName(), testCaseTwo.getAttribute("classname"));
        Xpp3Dom errorNode = testCaseTwo.getChild("error");
        Xpp3Dom rerunErrorNode = testCaseTwo.getChild("rerunError");
        assertNotNull(errorNode);
        assertNotNull(rerunErrorNode);

        assertEquals("A fud msg", errorNode.getAttribute("message"));
        assertEquals("fail at foo", errorNode.getAttribute("type"));

        // Check rerun error node contains all the information
        assertEquals(firstRunOut, testCaseTwo.getChild("system-out").getValue());
        assertEquals(firstRunErr, testCaseTwo.getChild("system-err").getValue());
        assertEquals(secondRunOut, rerunErrorNode.getChild("system-out").getValue());
        assertEquals(secondRunErr, rerunErrorNode.getChild("system-err").getValue());
        assertEquals("A fud msg two", rerunErrorNode.getAttribute("message"));
        assertEquals("fail at foo two", rerunErrorNode.getAttribute("type"));

        // Check flaky failure node
        Xpp3Dom testCaseThree = testcase[2];
        assertEquals(TEST_THREE, testCaseThree.getAttribute("name"));
        // Run time for a flaky test is the run time of the first successful run
        assertEquals("0.002", testCaseThree.getAttribute("time"));
        assertEquals(getClass().getName(), testCaseThree.getAttribute("classname"));
        Xpp3Dom flakyFailureNode = testCaseThree.getChild("flakyFailure");
        assertNotNull(flakyFailureNode);
        assertEquals(firstRunOut, flakyFailureNode.getChild("system-out").getValue());
        assertEquals(firstRunErr, flakyFailureNode.getChild("system-err").getValue());
        // system-out and system-err should not be present for flaky failures
        assertNull(testCaseThree.getChild("system-out"));
        assertNull(testCaseThree.getChild("system-err"));
    }

    @Test
    public void testOutputRerunFlakyAssumption() throws IOException {
        expectedReportFile = new File(reportDir, "TEST-" + getClass().getName() + ".xml");

        StackTraceWriter stackTraceWriterOne = new DeserializedStacktraceWriter("A fud msg", "trimmed", "fail at foo");

        StackTraceWriter stackTraceWriterTwo =
                new DeserializedStacktraceWriter("A fud msg two", "trimmed two", "fail at foo two");

        String firstRunOut = "first run out";
        String firstRunErr = "first run err";
        String secondRunOut = "second run out";
        String secondRunErr = "second run err";

        WrappedReportEntry testTwoFirstError = new WrappedReportEntry(
                new SimpleReportEntry(
                        NORMAL_RUN, 1L, getClass().getName(), null, TEST_TWO, null, stackTraceWriterOne, 5),
                ERROR,
                1771085631L,
                5,
                createStdOutput(firstRunOut),
                createStdOutput(firstRunErr));

        stats.testSucceeded(testTwoFirstError);

        WrappedReportEntry testTwoSecondError = new WrappedReportEntry(
                new SimpleReportEntry(
                        RERUN_TEST_AFTER_FAILURE,
                        1L,
                        getClass().getName(),
                        null,
                        TEST_TWO,
                        null,
                        stackTraceWriterTwo,
                        13),
                SKIPPED,
                1771085631L,
                13,
                createStdOutput(secondRunOut),
                createStdOutput(secondRunErr));

        rerunStats.testSucceeded(testTwoSecondError);

        StatelessXmlReporter reporter = new StatelessXmlReporter(
                reportDir,
                null,
                false,
                1,
                new HashMap<>(),
                XSD,
                "3.0.2",
                false,
                false,
                false,
                false,
                true,
                true,
                false);

        WrappedReportEntry testSetReportEntry = new WrappedReportEntry(
                new SimpleReportEntry(
                        RERUN_TEST_AFTER_FAILURE, 1L, getClass().getName(), null, null, null, stackTraceWriterOne, 5),
                ERROR,
                1771085631L,
                20,
                createStdOutput(firstRunOut),
                createStdOutput(firstRunErr));

        reporter.testSetCompleted(testSetReportEntry, stats);
        reporter.testSetCompleted(testSetReportEntry, rerunStats);

        Xpp3Dom testSuite;
        try (FileInputStream fileInputStream = new FileInputStream(expectedReportFile);
                InputStreamReader reader = new InputStreamReader(fileInputStream, UTF_8)) {
            testSuite = Xpp3DomBuilder.build(reader);
        }
        assertEquals("testsuite", testSuite.getName());
        assertEquals("0.02", testSuite.getAttribute("time"));

        Xpp3Dom[] testcase = testSuite.getChildren("testcase");
        assertEquals(1, testcase.length);
        Xpp3Dom testCaseOne = testcase[0];
        assertEquals(getClass().getName(), testCaseOne.getAttribute("classname"));
        assertEquals(TEST_TWO, testCaseOne.getAttribute("name"));
        assertEquals("0.005", testCaseOne.getAttribute("time"));

        Xpp3Dom[] testCaseElements = testCaseOne.getChildren();
        assertEquals(3, testCaseElements.length);
        assertEquals("error", testCaseElements[0].getName());
        assertEquals("system-out", testCaseElements[1].getName());
        assertEquals("system-err", testCaseElements[2].getName());
        long linesWithComments = readAllLines(expectedReportFile.toPath(), UTF_8).stream()
                .filter(line -> line.contains("<!-- a skipped test execution in re-run phase -->"))
                .count();
        assertEquals(1, linesWithComments);
    }

    @Test
    public void testNoWritesOnDeferredFile() throws Exception {
        Utf8RecodingDeferredFileOutputStream out = new Utf8RecodingDeferredFileOutputStream("test");
        out.free();
        out.write("a", false, null);
        assertThat((boolean) getInternalState(out, "isDirty")).isFalse();
    }

    @Test
    public void testLengthOnDeferredFile() throws Exception {
        Utf8RecodingDeferredFileOutputStream out = new Utf8RecodingDeferredFileOutputStream("test");

        assertThat(out.getByteCount()).isZero();

        File f = File.createTempFile("test", "tmp");
        RandomAccessFile storage = new RandomAccessFile(f, "rw");
        setInternalState(out, "storage", storage);
        setInternalState(out, "file", f.toPath());
        storage.writeByte(0);
        storage.getFD().sync();
        assertThat(out.getByteCount()).isEqualTo(1);

        storage.close();
        assertThat(f.delete()).isTrue();
        assertThat(out.getByteCount()).isZero();
        out.free();
    }

    @Test
    @SuppressWarnings("checkstyle:magicnumber")
    public void testWritesOnDeferredFile() throws Exception {
        Utf8RecodingDeferredFileOutputStream out = new Utf8RecodingDeferredFileOutputStream("test");
        for (int i = 0; i < 33_000; i++) {
            out.write("A", false, null);
            out.write("B", true, null);
        }
        out.write(null, false, null);
        out.write(null, true, null);

        assertThat(out.getByteCount()).isEqualTo(33_000 * (1 + 1 + NL.length()) + 4 + 4 + NL.length());

        StringBuilder expectedContent = new StringBuilder(150_000);
        for (int i = 0; i < 33_000; i++) {
            expectedContent.append('A').append('B').append(NL);
        }
        expectedContent.append("null").append("null").append(NL);
        ByteArrayOutputStream read = new ByteArrayOutputStream(150_000);
        out.writeTo(read);
        assertThat(read.toString()).isEqualTo(expectedContent.toString());

        out.free();
    }

    @Test
    public void testFreeOnDeferredFile() throws Exception {
        Utf8RecodingDeferredFileOutputStream out = new Utf8RecodingDeferredFileOutputStream("test");
        setInternalState(out, "cache", ByteBuffer.allocate(0));
        Path path = mock(Path.class);
        File file = mock(File.class);
        when(path.toFile()).thenReturn(file);
        setInternalState(out, "file", path);
        RandomAccessFile storage = mock(RandomAccessFile.class);
        doThrow(IOException.class).when(storage).close();
        setInternalState(out, "storage", storage);
        out.free();
        assertThat((boolean) getInternalState(out, "closed")).isTrue();
        verify(file, times(1)).deleteOnExit();
    }

    @Test
    public void testCacheOnDeferredFile() throws Exception {
        Utf8RecodingDeferredFileOutputStream out = new Utf8RecodingDeferredFileOutputStream("test");
        byte[] b1 = invokeMethod(out, "getLargeCache", 1);
        byte[] b2 = invokeMethod(out, "getLargeCache", 1);
        assertThat(b1).isSameAs(b2);
        assertThat(b1).hasSize(1);

        byte[] b3 = invokeMethod(out, "getLargeCache", 2);
        assertThat(b3).isNotSameAs(b1);
        assertThat(b3).hasSize(2);

        byte[] b4 = invokeMethod(out, "getLargeCache", 1);
        assertThat(b4).isSameAs(b3);
        assertThat(b3).hasSize(2);
    }

    @Test
    public void testSyncOnDeferredFile() throws Exception {
        Utf8RecodingDeferredFileOutputStream out = new Utf8RecodingDeferredFileOutputStream("test");
        Buffer cache = ByteBuffer.wrap(new byte[] {1, 2, 3});
        cache.position(3);
        setInternalState(out, "cache", cache);
        assertThat((boolean) getInternalState(out, "isDirty")).isFalse();
        setInternalState(out, "isDirty", true);
        File file = new File(reportDir, "test");
        setInternalState(out, "file", file.toPath());
        RandomAccessFile storage = new RandomAccessFile(file, "rw");
        setInternalState(out, "storage", storage);
        invokeMethod(out, "sync");
        assertThat((boolean) getInternalState(out, "isDirty")).isFalse();
        storage.seek(0L);
        assertThat(storage.read()).isEqualTo(1);
        assertThat(storage.read()).isEqualTo(2);
        assertThat(storage.read()).isEqualTo(3);
        assertThat(storage.read()).isEqualTo(-1);
        assertThat(storage.length()).isEqualTo(3L);
        assertThat(cache.position()).isEqualTo(0);
        assertThat(cache.limit()).isEqualTo(3);
        storage.seek(3L);
        invokeMethod(out, "sync");
        assertThat((boolean) getInternalState(out, "isDirty")).isFalse();
        assertThat(storage.length()).isEqualTo(3L);
        assertThat(out.getByteCount()).isEqualTo(3L);
        assertThat((boolean) getInternalState(out, "closed")).isFalse();
        out.free();
        assertThat((boolean) getInternalState(out, "closed")).isTrue();
        // todo assertThat( file ).doesNotExist();
        out.free();
        assertThat((boolean) getInternalState(out, "closed")).isTrue();
    }

    @Test
    public void testReporterHandlesATestWithoutMessageAndWithEmptyStackTrace() {
        StackTraceWriter stackTraceWriterOne = new DeserializedStacktraceWriter(null, null, "");

        WrappedReportEntry testReport = new WrappedReportEntry(
                new SimpleReportEntry(
                        NORMAL_RUN, 1L, getClass().getName(), null, "a test name", null, stackTraceWriterOne, 5),
                ERROR,
                1771085631L,
                5,
                null,
                null);

        StatelessXmlReporter reporter = new StatelessXmlReporter(
                reportDir,
                null,
                false,
                1,
                new HashMap<>(),
                XSD,
                "3.0.2",
                false,
                false,
                false,
                false,
                true,
                true,
                false);

        reporter.testSetCompleted(testReport, stats);
    }

    @Test
    public void testClassnameUsesActualClassNameWhenPhrasedClassNameDisabled() throws IOException {
        String actualClassName = "MyTest";
        String displayName = "NewName";

        ReportEntry reportEntry = new SimpleReportEntry(
                NORMAL_RUN,
                0L,
                actualClassName,
                displayName,
                actualClassName,
                TEST_ONE,
                null,
                null,
                12,
                null,
                Collections.<String, String>emptyMap());
        WrappedReportEntry testSetReportEntry =
                new WrappedReportEntry(reportEntry, SUCCESS, 1771085631L, 12, null, null, systemProps());
        expectedReportFile = new File(reportDir, "TEST-" + actualClassName + ".xml");

        stats.testSucceeded(testSetReportEntry);

        StatelessXmlReporter reporter = new StatelessXmlReporter(
                reportDir,
                null,
                false,
                0,
                new ConcurrentHashMap<>(),
                XSD,
                "3.0.2",
                false,
                false,
                false, // phrasedClassName = false
                false,
                true,
                true,
                false);
        reporter.testSetCompleted(testSetReportEntry, stats);

        Xpp3Dom testSuite;
        try (FileInputStream fileInputStream = new FileInputStream(expectedReportFile);
                InputStreamReader reader = new InputStreamReader(fileInputStream, UTF_8)) {
            testSuite = Xpp3DomBuilder.build(reader);
        }
        Xpp3Dom testcase = testSuite.getChildren("testcase")[0];
        assertEquals(
                actualClassName,
                testcase.getAttribute("classname"),
                "classname should be the actual class name, not the @DisplayName value");
    }

    private boolean defaultCharsetSupportsSpecialChar() {
        // some charsets are not able to deal with \u0115 on both ways of the conversion
        return "\u0115\u00DC".equals(new String("\u0115\u00DC".getBytes()));
    }

    private Utf8RecodingDeferredFileOutputStream createStdOutput(String content) throws IOException {
        Utf8RecodingDeferredFileOutputStream stdOut = new Utf8RecodingDeferredFileOutputStream("fds2");
        stdOut.write(content, false, null);
        return stdOut;
    }
}
