package com.github.cstroe.svndumpgui.internal.transform;

import com.github.cstroe.svndumpgui.api.SvnDump;
import com.github.cstroe.svndumpgui.api.SvnDumpMutator;
import com.github.cstroe.svndumpgui.api.SvnDumpWriter;
import com.github.cstroe.svndumpgui.api.SvnNodeHeader;
import com.github.cstroe.svndumpgui.internal.SvnDumpFileParserTest;
import com.github.cstroe.svndumpgui.internal.SvnDumpWriterImpl;
import com.github.cstroe.svndumpgui.internal.SvnDumpWriterImplTest;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;

public class PathChangeTest {

    @Test
    public void file_name_change() throws Exception {
        final String dumpFilePath = "dumps/svn_multi_file_delete.dump";
        {
            SvnDump dump = SvnDumpFileParserTest.parse(dumpFilePath);

            assertThat(dump.getRevisions().size(), is(3));
            assertThat(dump.getRevisions().get(0).getNodes().size(), is(0));
            assertThat(dump.getRevisions().get(1).getNodes().size(), is(3));
            assertThat(dump.getRevisions().get(1).getNodes().get(0).get(SvnNodeHeader.PATH),
                    is(equalTo("README.txt")));
            assertThat(dump.getRevisions().get(2).getNodes().size(), is(3));
            assertThat(dump.getRevisions().get(2).getNodes().get(0).get(SvnNodeHeader.PATH),
                    is(equalTo("README.txt")));
        }
        {
            SvnDumpMutator pathChange = new PathChange("README.txt", "README-changed.txt");
            SvnDump dump = SvnDumpFileParserTest.consume(dumpFilePath, pathChange);

            assertThat(dump.getRevisions().size(), is(3));
            assertThat(dump.getRevisions().get(0).getNodes().size(), is(0));
            assertThat(dump.getRevisions().get(1).getNodes().size(), is(3));
            assertThat(dump.getRevisions().get(1).getNodes().get(0).get(SvnNodeHeader.PATH),
                    is(equalTo("README-changed.txt")));
            assertThat(dump.getRevisions().get(2).getNodes().size(), is(3));
            assertThat(dump.getRevisions().get(2).getNodes().get(0).get(SvnNodeHeader.PATH),
                    is(equalTo("README-changed.txt")));
        }
    }

    @Test
    public void file_name_change_of_copy_path() throws Exception {
        String dumpFilePath = "dumps/svn_copy_file.dump";
        {
            SvnDump dump = SvnDumpFileParserTest.parse(dumpFilePath);

            assertThat(dump.getRevisions().size(), is(3));
            assertThat(dump.getRevisions().get(0).getNodes().size(), is(0));
            assertThat(dump.getRevisions().get(1).getNodes().size(), is(1));
            assertThat(dump.getRevisions().get(1).getNodes().get(0).get(SvnNodeHeader.PATH),
                    is(equalTo("README.txt")));
            assertThat(dump.getRevisions().get(2).getNodes().size(), is(1));
            assertThat(dump.getRevisions().get(2).getNodes().get(0).get(SvnNodeHeader.COPY_FROM_PATH),
                    is(equalTo("README.txt")));
            assertThat(dump.getRevisions().get(2).getNodes().get(0).get(SvnNodeHeader.PATH),
                    is(equalTo("OTHER.txt")));
        }
        {
            SvnDumpMutator pathChange = new PathChange("README.txt", "README-changed.txt");
            SvnDump dump = SvnDumpFileParserTest.consume(dumpFilePath, pathChange);

            assertThat(dump.getRevisions().size(), is(3));
            assertThat(dump.getRevisions().get(0).getNodes().size(), is(0));
            assertThat(dump.getRevisions().get(1).getNodes().size(), is(1));
            assertThat(dump.getRevisions().get(1).getNodes().get(0).get(SvnNodeHeader.PATH),
                    is(equalTo("README-changed.txt")));
            assertThat(dump.getRevisions().get(2).getNodes().size(), is(1));
            assertThat(dump.getRevisions().get(2).getNodes().get(0).get(SvnNodeHeader.COPY_FROM_PATH),
                    is(equalTo("README-changed.txt")));
            assertThat(dump.getRevisions().get(2).getNodes().get(0).get(SvnNodeHeader.PATH),
                    is(equalTo("OTHER.txt")));
        }
    }

    @Test
    public void update_mergeinfo() throws Exception {
        SvnDumpMutator pathChange = new PathChange("branches", "custom_branches");
        SvnDump dump = SvnDumpFileParserTest.consume("dumps/simple_branch_and_merge.dump", pathChange);

        ByteArrayOutputStream changedDump = new ByteArrayOutputStream();
        SvnDumpWriter writer = new SvnDumpWriterImpl();
        writer.write(changedDump, dump);

        final InputStream s = Thread.currentThread().getContextClassLoader()
            .getResourceAsStream("dumps/simple_branch_and_merge_renamed.dump");

        ByteArrayInputStream bis = new ByteArrayInputStream(changedDump.toByteArray());

        SvnDumpWriterImplTest.assertEqualStreams(s, bis);
    }
}
@@ -270,76 +270,8 @@
 er;%0A
-import com.github.cstroe.svndumpgui.internal.SvnDumpFileParserTest;%0A
 impo
@@ -398,16 +398,100 @@
 plTest;%0A
+import com.github.cstroe.svndumpgui.internal.utility.SvnDumpFileParserDoppelganger;%0A
 import o
@@ -946,36 +946,44 @@
 vnDumpFileParser
-Test
+Doppelganger
 .parse(dumpFileP
@@ -1709,36 +1709,44 @@
 vnDumpFileParser
-Test
+Doppelganger
 .consume(dumpFil
@@ -2560,20 +2560,28 @@
 leParser
-Test
+Doppelganger
 .parse(d
@@ -3465,36 +3465,44 @@
 vnDumpFileParser
-Test
+Doppelganger
 .consume(dumpFil
@@ -4464,20 +4464,28 @@
 leParser
-Test
+Doppelganger
 .consume
@@ -4686,16 +4686,18 @@
 er.write
+To
 (changed
@@ -4704,14 +4704,69 @@
 Dump
-, dump
+);%0A        SvnDumpFileParserDoppelganger.consume(dump, writer
 );%0A%0A

Use the doppelganger.